Improve binary decoding, make binary default.

This commit is contained in:
Mario Zechner 2023-06-16 20:03:08 +02:00
parent d8a9da7692
commit 222720365c
3 changed files with 50 additions and 8 deletions

View File

@ -63,6 +63,46 @@ exports.today = () => {
return `${year}-${month}-${day}`;
};
exports.isoDate = (daysSince2000) => {
// Number of days in each month (non-leap year)
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Number of days in each month (leap year)
const daysInMonthLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Start date: January 1, 2000
const startYear = 2000;
const startMonth = 1;
const startDay = 1;
// Calculate the number of leap years
const numLeapYears = Math.floor(daysSince2000 / 365) - Math.floor((startYear - 2000) / 4);
// Calculate the number of non-leap years
const numNonLeapYears = Math.floor(daysSince2000 / 365) - numLeapYears;
// Calculate the number of days remaining after accounting for years
const remainingDays = daysSince2000 - (numLeapYears * 366 + numNonLeapYears * 365);
// Determine the current year
const currentYear = startYear + numLeapYears * 4 + numNonLeapYears;
const isLeapYear = currentYear % 4 === 0 && (currentYear % 100 !== 0 || currentYear % 400 === 0);
const daysInMonthArray = isLeapYear ? daysInMonthLeap : daysInMonth;
// Determine the current month and day
let currentMonth = 1;
let currentDay = remainingDays + 1;
for (const days of daysInMonthArray) {
if (currentDay <= days) {
break;
}
currentMonth++;
currentDay -= days;
}
return `${currentYear}-${currentMonth.toString().padStart(2, "0")}-${currentDay.toString().padStart(2, "0")}`;
};
exports.fetchJSON = async (url) => {
const response = await fetch(url);
return await response.json();

View File

@ -1,4 +1,4 @@
const { deltaTime, log } = require("../js/misc");
const { deltaTime, log, isoDate } = require("../js/misc");
const { stores, STORE_KEYS } = require("./stores");
const { Model } = require("./model");
const { Settings } = require("./settings");
@ -61,11 +61,10 @@ function decompressBinary(buffer) {
// Deserialize days as a 32-bit integer
const daysSince2000 = view.getInt32(offset, true);
offset += 4;
// Calculate the date from days since 2000-01-01
const entryDate = new Date(baseDate.getTime() + daysSince2000 * 24 * 60 * 60 * 1000);
const dateStr = isoDate(daysSince2000);
obj.priceHistory[i] = { date: entryDate.toISOString().substring(0, 10), price };
obj.priceHistory[i] = { date: dateStr, price };
}
objects.push(obj);
@ -143,14 +142,15 @@ class Items extends Model {
}
processItems(items) {
const lookup = {};
const lookup = new Set();
const start = performance.now();
for (const item of items) {
lookup[item.store + item.id] = item;
item.search = item.name + " " + item.quantity + " " + item.unit;
item.search = item.search.toLowerCase().replace(",", ".");
item.unitPrice = (item.price / item.quantity) * (item.unit == "g" || item.unit == "ml" ? 1000 : 1);
const unitPriceFactor = item.unit == "g" || item.unit == "ml" ? 1000 : 1;
item.unitPrice = (item.price / item.quantity) * unitPriceFactor;
item.numPrices = item.priceHistory.length;
item.priceOldest = item.priceHistory[item.priceHistory.length - 1].price;
item.dateOldest = item.priceHistory[item.priceHistory.length - 1].date;
@ -159,7 +159,7 @@ class Items extends Model {
let lowestPriceBefore = 100000;
for (let i = 0; i < item.priceHistory.length; i++) {
const price = item.priceHistory[i];
price.unitPrice = (price.price / item.quantity) * (item.unit == "g" || item.unit == "ml" ? 1000 : 1);
price.unitPrice = (price.price / item.quantity) * unitPriceFactor;
if (i == 0) continue;
if (i < 10) {
item["price" + i] = price.price;
@ -201,6 +201,7 @@ class Items extends Model {
const settings = new Settings();
const compressedItemsPerStore = [];
for (const store of STORE_KEYS) {
// if (["reweDe", "dmDe", "sparSi"].includes(store)) continue;
compressedItemsPerStore.push(
new Promise(async (resolve) => {
let start = performance.now();
@ -234,6 +235,7 @@ class Items extends Model {
log(`Items - loaded ${items.length} items took ${deltaTime(start).toFixed(4)} secs`);
this.processItems(items);
log(`Items - total loading took ${deltaTime(start).toFixed(4)} secs`);
}
}

View File

@ -9,7 +9,7 @@ class Settings extends Model {
STORE_KEYS.forEach((store) => {
this[store] = stores[store].defaultChecked;
});
this.jsonData = true;
this.jsonData = false;
let settings = localStorage.getItem("settings");
if (settings) {