fix: wrong month stats and db cache undefined handling
This commit is contained in:
parent
ed9612fce5
commit
d87d9fb301
4 changed files with 54 additions and 67 deletions
110
src/db.ts
110
src/db.ts
|
@ -1,6 +1,6 @@
|
|||
import { type Accessor, createEffect, createMemo, createRoot, createSignal, DEV, type Setter } from "solid-js";
|
||||
import { createEffect, createMemo, createRoot, createSignal } from "solid-js";
|
||||
|
||||
import { Kysely, type NotNull, sql } from "kysely";
|
||||
import { Kysely, sql, type NotNull } from "kysely";
|
||||
import type { DB } from "kysely-codegen";
|
||||
import { SqlJsDialect } from "kysely-wasm";
|
||||
import initSqlJS, { type Database } from "sql.js";
|
||||
|
@ -14,25 +14,10 @@ export const SQL = await initSqlJS({
|
|||
locateFile: () => wasmURL,
|
||||
});
|
||||
|
||||
let rawDb: Accessor<Database | undefined> = () => undefined,
|
||||
setRawDb: Setter<Database | undefined> = () => undefined;
|
||||
|
||||
if (DEV) {
|
||||
const file = await import("./assets/database.sqlite?url").then((result) => {
|
||||
return fetch(result.default).then((res) => res.arrayBuffer());
|
||||
});
|
||||
|
||||
const testDb = new SQL.Database(new Uint8Array(file));
|
||||
|
||||
[rawDb, setRawDb] = createSignal<Database | undefined>(testDb);
|
||||
} else {
|
||||
[rawDb, setRawDb] = createSignal<Database | undefined>();
|
||||
}
|
||||
|
||||
export { rawDb as db, setRawDb as setDb };
|
||||
export const [db, setDb] = createSignal<Database | undefined>();
|
||||
|
||||
const sqlJsDialect = () => {
|
||||
const currentDb = rawDb();
|
||||
const currentDb = db();
|
||||
|
||||
if (currentDb) {
|
||||
return new SqlJsDialect({
|
||||
|
@ -43,10 +28,10 @@ const sqlJsDialect = () => {
|
|||
|
||||
const kyselyDb = createRoot(() => {
|
||||
createEffect(() => {
|
||||
const db = rawDb();
|
||||
const currentDb = db();
|
||||
|
||||
if (db) {
|
||||
db.create_function("is_not_empty", (str: string | null) => {
|
||||
if (currentDb) {
|
||||
currentDb.create_function("is_not_empty", (str: string | null) => {
|
||||
return str !== null && str !== "";
|
||||
});
|
||||
}
|
||||
|
@ -56,7 +41,7 @@ const kyselyDb = createRoot(() => {
|
|||
const currentSqlJsDialect = sqlJsDialect();
|
||||
|
||||
if (!currentSqlJsDialect) {
|
||||
throw new Error("no db selected!");
|
||||
return;
|
||||
}
|
||||
|
||||
return new Kysely<DB>({
|
||||
|
@ -65,46 +50,47 @@ const kyselyDb = createRoot(() => {
|
|||
});
|
||||
});
|
||||
|
||||
const allThreadsOverviewQueryRaw = kyselyDb()
|
||||
.selectFrom("thread")
|
||||
.innerJoin(
|
||||
(eb) =>
|
||||
eb
|
||||
.selectFrom("message")
|
||||
.select((eb) => ["message.thread_id", eb.fn.countAll().as("message_count")])
|
||||
.where((eb) => {
|
||||
return eb.and([eb("message.body", "is not", null), eb("message.body", "is not", "")]);
|
||||
})
|
||||
.groupBy("message.thread_id")
|
||||
.as("message"),
|
||||
(join) => join.onRef("message.thread_id", "=", "thread._id"),
|
||||
)
|
||||
.innerJoin("recipient", "thread.recipient_id", "recipient._id")
|
||||
.leftJoin("groups", "recipient._id", "groups.recipient_id")
|
||||
.select([
|
||||
"thread._id as thread_id",
|
||||
"thread.recipient_id",
|
||||
"thread.archived",
|
||||
"recipient.profile_joined_name",
|
||||
"recipient.system_joined_name",
|
||||
"groups.title",
|
||||
"message_count",
|
||||
"thread.date as last_message_date",
|
||||
"recipient.nickname_joined_name",
|
||||
])
|
||||
.where("message_count", ">", 0)
|
||||
.$narrowType<{
|
||||
thread_id: NotNull;
|
||||
archived: NotNull;
|
||||
message_count: number;
|
||||
}>()
|
||||
.compile();
|
||||
const allThreadsOverviewQueryRaw = () =>
|
||||
kyselyDb()
|
||||
?.selectFrom("thread")
|
||||
.innerJoin(
|
||||
(eb) =>
|
||||
eb
|
||||
.selectFrom("message")
|
||||
.select((eb) => ["message.thread_id", eb.fn.countAll().as("message_count")])
|
||||
.where((eb) => {
|
||||
return eb.and([eb("message.body", "is not", null), eb("message.body", "is not", "")]);
|
||||
})
|
||||
.groupBy("message.thread_id")
|
||||
.as("message"),
|
||||
(join) => join.onRef("message.thread_id", "=", "thread._id"),
|
||||
)
|
||||
.innerJoin("recipient", "thread.recipient_id", "recipient._id")
|
||||
.leftJoin("groups", "recipient._id", "groups.recipient_id")
|
||||
.select([
|
||||
"thread._id as thread_id",
|
||||
"thread.recipient_id",
|
||||
"thread.archived",
|
||||
"recipient.profile_joined_name",
|
||||
"recipient.system_joined_name",
|
||||
"groups.title",
|
||||
"message_count",
|
||||
"thread.date as last_message_date",
|
||||
"recipient.nickname_joined_name",
|
||||
])
|
||||
.where("message_count", ">", 0)
|
||||
.$narrowType<{
|
||||
thread_id: NotNull;
|
||||
archived: NotNull;
|
||||
message_count: number;
|
||||
}>()
|
||||
.execute();
|
||||
|
||||
export const allThreadsOverviewQuery = cached(() => kyselyDb().executeQuery(allThreadsOverviewQueryRaw));
|
||||
export const allThreadsOverviewQuery = cached(allThreadsOverviewQueryRaw);
|
||||
|
||||
const overallSentMessagesQueryRaw = (recipientId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("message")
|
||||
?.selectFrom("message")
|
||||
.select((eb) => eb.fn.countAll().as("messageCount"))
|
||||
.where((eb) =>
|
||||
eb.and([
|
||||
|
@ -119,7 +105,7 @@ export const overallSentMessagesQuery = cached(overallSentMessagesQueryRaw);
|
|||
|
||||
const dmPartnerRecipientQueryRaw = (dmId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("recipient")
|
||||
?.selectFrom("recipient")
|
||||
.select([
|
||||
"recipient._id",
|
||||
"recipient.system_joined_name",
|
||||
|
@ -137,7 +123,7 @@ export const dmPartnerRecipientQuery = cached(dmPartnerRecipientQueryRaw);
|
|||
|
||||
const threadSentMessagesOverviewQueryRaw = (threadId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("message")
|
||||
?.selectFrom("message")
|
||||
.select(["from_recipient_id", sql<string>`datetime(date_sent / 1000, 'unixepoch')`.as("message_datetime")])
|
||||
.orderBy(["message_datetime"])
|
||||
.where((eb) => eb.and([eb("body", "is not", null), eb("body", "!=", ""), eb("thread_id", "=", threadId)]))
|
||||
|
@ -147,7 +133,7 @@ export const threadSentMessagesOverviewQuery = cached(threadSentMessagesOverview
|
|||
|
||||
const threadMostUsedWordsQueryRaw = (threadId: number, limit = 10) =>
|
||||
kyselyDb()
|
||||
.withRecursive("words", (eb) => {
|
||||
?.withRecursive("words", (eb) => {
|
||||
return eb
|
||||
.selectFrom("message")
|
||||
.select([
|
||||
|
|
|
@ -142,7 +142,9 @@ export const cached = <T extends unknown[], R, TT>(fn: (...args: T) => R, self?:
|
|||
isPromise = promisified == newValue;
|
||||
|
||||
void promisified.then((result) => {
|
||||
cache.set(cacheName, cacheKey, result);
|
||||
if (result !== undefined) {
|
||||
cache.set(cacheName, cacheKey, result);
|
||||
}
|
||||
});
|
||||
|
||||
return newValue;
|
||||
|
|
|
@ -57,8 +57,7 @@ export const createMessageStatsSources = (
|
|||
person[message.fromRecipientId] += 1;
|
||||
|
||||
// increment the message count of the message's month for this recipient
|
||||
// months are an array from 0 - 11
|
||||
month[messageDate.getMonth() - 1][message.fromRecipientId] += 1;
|
||||
month[messageDate.getMonth()][message.fromRecipientId] += 1;
|
||||
|
||||
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
||||
const dateStatsEntry = date.find(({ date }) => isSameDay(date, messageDate))!;
|
||||
|
|
|
@ -10,8 +10,8 @@ import { Title } from "@solidjs/meta";
|
|||
export const Overview: Component<RouteSectionProps> = () => {
|
||||
const [allSelfSentMessagesCount] = createResource(() => overallSentMessagesQuery(SELF_ID));
|
||||
|
||||
const [roomOverview] = createResource<RoomOverview[]>(async () => {
|
||||
return (await allThreadsOverviewQuery()).rows.map((row) => {
|
||||
const [roomOverview] = createResource<RoomOverview[] | undefined>(async () => {
|
||||
return (await allThreadsOverviewQuery())?.map((row) => {
|
||||
const isGroup = row.title !== null;
|
||||
|
||||
let name = "";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue