diff --git a/scripts/db-streak-recompute.ts b/scripts/db-streak-recompute.ts index e202baa5..a05eb08b 100644 --- a/scripts/db-streak-recompute.ts +++ b/scripts/db-streak-recompute.ts @@ -1,7 +1,9 @@ +import { Conf } from '@/config.ts'; import { Storages } from '@/storages.ts'; const kysely = await Storages.kysely(); const statsQuery = kysely.selectFrom('author_stats').select('pubkey'); +const { streakWindow } = Conf; for await (const { pubkey } of statsQuery.stream(10)) { const eventsQuery = kysely @@ -21,14 +23,14 @@ for await (const { pubkey } of statsQuery.stream(10)) { if (!end) { const now = Math.floor(Date.now() / 1000); - if (now - createdAt > 86400) { + if (now - createdAt > streakWindow) { break; // streak broken } end = createdAt; } - if (start && (start - createdAt > 86400)) { + if (start && (start - createdAt > streakWindow)) { break; // streak broken } diff --git a/src/config.ts b/src/config.ts index 0164182a..cdd88705 100644 --- a/src/config.ts +++ b/src/config.ts @@ -357,6 +357,10 @@ class Conf { return Number(Deno.env.get('PROFILE_FIELDS_VALUE_LENGTH') || 2047); }, }; + /** Maximum time between events before a streak is broken, *in seconds*. */ + static get streakWindow(): number { + return Number(Deno.env.get('STREAK_WINDOW') || 129600); + } } const optionalBooleanSchema = z diff --git a/src/utils/stats.ts b/src/utils/stats.ts index 64e7986d..0821fed2 100644 --- a/src/utils/stats.ts +++ b/src/utils/stats.ts @@ -3,6 +3,7 @@ import { Insertable, Kysely, UpdateObject } from 'kysely'; import { SetRequired } from 'type-fest'; import { z } from 'zod'; +import { Conf } from '@/config.ts'; import { DittoTables } from '@/db/DittoTables.ts'; import { findQuoteTag, findReplyTag, getTagSet } from '@/utils/tags.ts'; @@ -46,7 +47,7 @@ async function handleEvent1(kysely: Kysely, event: NostrEvent, x: n if (start && end) { // Streak exists. if (now <= end) { // Streak cannot go backwards in time. Skip it. - } else if (now - end > 86400) { + } else if (now - end > Conf.streakWindow) { // Streak is broken. Start a new streak. start = now; end = now; diff --git a/src/views/mastodon/accounts.ts b/src/views/mastodon/accounts.ts index 1261de92..0c2d1dcc 100644 --- a/src/views/mastodon/accounts.ts +++ b/src/views/mastodon/accounts.ts @@ -72,15 +72,16 @@ async function renderAccount( let streakDays = 0; let streakStart = event.author_stats?.streak_start ?? null; let streakEnd = event.author_stats?.streak_end ?? null; + const { streakWindow } = Conf; if (streakStart && streakEnd) { - const broken = nostrNow() - streakEnd > 86400; + const broken = nostrNow() - streakEnd > streakWindow; if (broken) { streakStart = null; streakEnd = null; } else { const delta = streakEnd - streakStart; - streakDays = Math.max(Math.ceil(delta / 86400), 1); + streakDays = Math.max(Math.ceil(delta / streakWindow), 1); } }