feat: cache stats sources

This commit is contained in:
Samuel 2024-12-18 19:03:44 +01:00
parent ec1ea5cfe8
commit cad305aa66
14 changed files with 98 additions and 92 deletions

View file

@ -1,4 +1,5 @@
import { createRoot, on, createDeferred } from "solid-js";
import { serialize, deserialize } from "seroval";
const DATABASE_HASH_PREFIX = "database";
@ -64,10 +65,12 @@ class LocalStorageCacheAdapter {
this.keys.add(fullKey);
try {
localStorage.setItem(fullKey, JSON.stringify(value));
localStorage.setItem(fullKey, serialize(value));
} catch (error: unknown) {
if (error instanceof DOMException && error.name === "QUOTA_EXCEEDED_ERR") {
console.error("Storage quota exceeded, not caching new function calls");
} else {
console.error(error);
}
}
}
@ -80,7 +83,7 @@ class LocalStorageCacheAdapter {
get<R>(cacheName: string, key: string): R | undefined {
const item = localStorage.getItem(this.#createKey(cacheName, key));
if (item) {
return JSON.parse(item) as R;
return deserialize(item) as R;
}
}
}

View file

@ -1,7 +1,7 @@
import { createMemo, type Accessor } from "solid-js";
import { getDateList, getHourList, getMonthList, getWeekdayList } from "./date";
import type { MessageOverview, MessageStats, Recipients } from "~/types";
import { isSameDay } from "date-fns";
import { cached } from "./db-cache";
export const hourNames = getHourList();
@ -15,16 +15,14 @@ export const weekdayNames = getWeekdayList();
const initialWeekdayMap = [...weekdayNames.keys()];
export const createMessageStatsSources = (
messageOverview: Accessor<MessageOverview>,
recipients: Accessor<Recipients>,
) => {
const initialRecipientMap = () => Object.fromEntries(recipients().map(({ recipientId }) => [recipientId, 0]));
const createMessageStatsSourcesRaw = (messageOverview: MessageOverview, recipients: Recipients) => {
const initialRecipientMap = () => {
return Object.fromEntries(recipients.map(({ recipientId }) => [recipientId, 0]));
};
const dateList = () => {
const currentDmMessagesOverview = messageOverview();
const firstDate = currentDmMessagesOverview?.at(0)?.messageDate;
const lastDate = currentDmMessagesOverview?.at(-1)?.messageDate;
const firstDate = messageOverview?.at(0)?.messageDate;
const lastDate = messageOverview?.at(-1)?.messageDate;
if (firstDate && lastDate) {
return getDateList(firstDate, lastDate).map((date) => ({
totalMessages: 0,
@ -34,52 +32,51 @@ export const createMessageStatsSources = (
}
};
return createMemo(() => {
const currentMessageOverview = messageOverview();
const currentDateList = dateList();
const currentInitialRecipientMap = initialRecipientMap();
const currentDateList = dateList();
const currentInitialRecipientMap = initialRecipientMap();
const messageStats: MessageStats = {
person: { ...currentInitialRecipientMap },
month: initialMonthMap.map(() => ({ ...currentInitialRecipientMap })),
date: currentDateList ?? [],
weekday: initialWeekdayMap.map(() => ({ ...currentInitialRecipientMap })),
daytime: initialHoursMap.map(() => ({ ...currentInitialRecipientMap })),
};
const messageStats: MessageStats = {
person: { ...currentInitialRecipientMap },
month: initialMonthMap.map(() => ({ ...currentInitialRecipientMap })),
date: currentDateList ?? [],
weekday: initialWeekdayMap.map(() => ({ ...currentInitialRecipientMap })),
daytime: initialHoursMap.map(() => ({ ...currentInitialRecipientMap })),
};
if (currentMessageOverview && currentDateList) {
const { person, month, date, weekday, daytime } = messageStats;
if (currentDateList) {
const { person, month, date, weekday, daytime } = messageStats;
for (const message of currentMessageOverview) {
const { messageDate } = message;
for (const message of messageOverview) {
const { messageDate } = message;
// increment overall message count of a person
person[message.fromRecipientId] += 1;
// increment overall message count of a person
person[message.fromRecipientId] += 1;
// increment the message count of the message's month for this recipient
month[messageDate.getMonth()][message.fromRecipientId] += 1;
// increment the message count of the message's month for this recipient
month[messageDate.getMonth()][message.fromRecipientId] += 1;
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const dateStatsEntry = date.find(({ date }) => isSameDay(date, messageDate))!;
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const dateStatsEntry = date.find(({ date }) => isSameDay(date, messageDate))!;
// increment the message count of the message's date for this recipient
dateStatsEntry[message.fromRecipientId] += 1;
// increment the message count of the message's date for this recipient
dateStatsEntry[message.fromRecipientId] += 1;
// increment the overall message count of the message's date
dateStatsEntry.totalMessages += 1;
// increment the overall message count of the message's date
dateStatsEntry.totalMessages += 1;
const weekdayOfDate = messageDate.getDay();
// we index starting with monday while the `Date` object indexes starting with Sunday
const weekdayIndex = weekdayOfDate === 0 ? 6 : weekdayOfDate - 1;
const weekdayOfDate = messageDate.getDay();
// we index starting with monday while the `Date` object indexes starting with Sunday
const weekdayIndex = weekdayOfDate === 0 ? 6 : weekdayOfDate - 1;
// increment the message count of the message's weekday for this recipient
weekday[weekdayIndex][message.fromRecipientId] += 1;
// increment the message count of the message's weekday for this recipient
weekday[weekdayIndex][message.fromRecipientId] += 1;
// increment the message count of the message's daytime for this recipient
daytime[messageDate.getHours()][message.fromRecipientId] += 1;
}
// increment the message count of the message's daytime for this recipient
daytime[messageDate.getHours()][message.fromRecipientId] += 1;
}
}
return messageStats;
});
return messageStats;
};
export const createMessageStatsSources = cached(createMessageStatsSourcesRaw);