Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(web): add time-series graph to the dashboard for reported lint frequency (#4712)

Elijah Potterd8b6eab881fa
diff --git a/packages/web/src/lib/adminUtils.ts b/packages/web/src/lib/adminUtils.ts
index 525185ed0..81a04903a 100644
--- a/packages/web/src/lib/adminUtils.ts
+++ b/packages/web/src/lib/adminUtils.ts
@@ -14,7 +14,7 @@ export function computeDurationFromSlug(slug: string): number | null {
}
}
-/** Count the occurances of unique strings in an array. */
+/** Count the occurrences of unique strings in an array. */
export function countOccurances(arr: string[]): Record<string, number> {
const counts: Record<string, number> = {};
@@ -28,3 +28,30 @@ export function countOccurances(arr: string[]): Record<string, number> {
return counts;
}
+
+/**
+ * Count reports by UTC day in date order, including zero-count days.
+ * Reports must already be filtered to [start, end); both bounds must be UTC midnight.
+ * A null start uses the first report's day, or returns no points if none exist.
+ */
+export function countReportsByDay(
+ reports: readonly { timestamp: Date }[],
+ start: Date | null,
+ end: Date,
+): { date: Date; value: number }[] {
+ const dayLength = 24 * 60 * 60 * 1000;
+ const counts = new Map<number, number>();
+ let firstDay = start?.getTime() ?? Number.POSITIVE_INFINITY;
+
+ for (const report of reports) {
+ const day = Math.floor(report.timestamp.getTime() / dayLength) * dayLength;
+ counts.set(day, (counts.get(day) ?? 0) + 1);
+ firstDay = Math.min(firstDay, day);
+ }
+
+ const points: { date: Date; value: number }[] = [];
+ for (let day = firstDay; day < end.getTime(); day += dayLength) {
+ points.push({ date: new Date(day), value: counts.get(day) ?? 0 });
+ }
+ return points;
+}
diff --git a/packages/web/src/lib/components/AnalyticsPage.svelte b/packages/web/src/lib/components/AnalyticsPage.svelte
index 4bfe59a14..7019d51f1 100644
--- a/packages/web/src/lib/components/AnalyticsPage.svelte
+++ b/packages/web/src/lib/components/AnalyticsPage.svelte
@@ -8,9 +8,12 @@ type Props = {
prevPeriodData?: Record<string, number>;
title: string;
links: Record<string, string>;
+ /** If there is a page giving more information about a specific key. */
+ keyLinkGenerator?: (key: string) => string;
+ keyLabel: string;
};
-let { data, prevPeriodData, title, links }: Props = $props();
+let { data, prevPeriodData, title, links, keyLabel, keyLinkGenerator }: Props = $props();
let counts = $derived(data);
let prevPeriodCounts = $derived(prevPeriodData);
@@ -45,14 +48,22 @@ let deltas = $derived(
<table>
<thead>
<tr>
- <th>Lint ID</th>
+ <th>{keyLabel}</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{#each entries as [key, value] }
<tr>
- <th scope="row">{key}</th>
+ <th scope="row">
+ {#if keyLinkGenerator != null}
+ <a href={keyLinkGenerator(key)}>
+ {key}
+ </a>
+ {:else}
+ {key}
+ {/if}
+ </th>
<td>{value}</td>
</tr>
{/each}
diff --git a/packages/web/src/lib/components/TimeseriesAnalyticsPage.svelte b/packages/web/src/lib/components/TimeseriesAnalyticsPage.svelte
new file mode 100644
index 000000000..8cbd63d8e
--- /dev/null
+++ b/packages/web/src/lib/components/TimeseriesAnalyticsPage.svelte
@@ -0,0 +1,16 @@
+<script lang="ts">
+import { Isolate } from 'components';
+import LineChart, { type TimeSeriesPoint } from './LineChart.svelte';
+
+interface Props {
+ heading: string;
+ datapoints: TimeSeriesPoint[];
+}
+
+let { heading, datapoints }: Props = $props();
+</script>
+
+<Isolate>
+ <h1>{heading}</h1>
+ <LineChart title="Reports per day (UTC)" data={datapoints} />
+</Isolate>
diff --git a/packages/web/src/lib/db/models/DomainReviews.ts b/packages/web/src/lib/db/models/DomainReviews.ts
index a96a9b2f4..a9708ba31 100644
--- a/packages/web/src/lib/db/models/DomainReviews.ts
+++ b/packages/web/src/lib/db/models/DomainReviews.ts
@@ -1,5 +1,6 @@
import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
+import { countReportsByDay } from '$lib/adminUtils';
import { db } from '..';
import { domainReviewTable } from '../schema';
@@ -23,6 +24,25 @@ export default class DomainReviews {
return await db.select().from(domainReviewTable).where(gte(domainReviewTable.timestamp, date));
}
+ /**
+ * Count one domain's reports by UTC day, including zero-count days.
+ * Start is inclusive and end is exclusive; both must be UTC midnight.
+ * A null start uses the first matching report's day, or returns no points if none exist.
+ */
+ public static async getDailyReportCounts(
+ domain: string,
+ start: Date | null,
+ end: Date,
+ ): Promise<{ date: Date; value: number }[]> {
+ const endTime = end.getTime();
+ const reports = await this.getAllBetween(start ?? new Date(0), new Date(endTime - 1));
+ return countReportsByDay(
+ reports.filter((report) => (report.domain ?? 'OTHER') === domain),
+ start,
+ end,
+ );
+ }
+
public static async getAllBetween(start: Date, end: Date) {
return await db
.select()
diff --git a/packages/web/src/lib/db/models/ProblematicLints.ts b/packages/web/src/lib/db/models/ProblematicLints.ts
index ec10ce4be..48ca844f4 100644
--- a/packages/web/src/lib/db/models/ProblematicLints.ts
+++ b/packages/web/src/lib/db/models/ProblematicLints.ts
@@ -1,5 +1,6 @@
import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
+import { countReportsByDay } from '$lib/adminUtils';
import { db } from '..';
import { problematicLintTable } from '../schema';
@@ -30,6 +31,20 @@ export default class ProblematicLints {
.where(gte(problematicLintTable.timestamp, date));
}
+ public static async getDailyReportCounts(
+ lintId: string,
+ start: Date | null,
+ end: Date,
+ ): Promise<{ date: Date; value: number }[]> {
+ const endTime = end.getTime();
+ const reports = await this.getAllBetween(start ?? new Date(0), new Date(endTime - 1));
+ return countReportsByDay(
+ reports.filter((report) => (report.rule_id ?? 'OTHER') === lintId),
+ start,
+ end,
+ );
+ }
+
public static async getAllBetween(start: Date, end: Date) {
return await db
.select()
diff --git a/packages/web/src/lib/db/models/UninstallFeedback.ts b/packages/web/src/lib/db/models/UninstallFeedback.ts
index 30ff71857..08501085a 100644
--- a/packages/web/src/lib/db/models/UninstallFeedback.ts
+++ b/packages/web/src/lib/db/models/UninstallFeedback.ts
@@ -1,5 +1,6 @@
import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
+import { countReportsByDay } from '$lib/adminUtils';
import { db } from '..';
import { uninstallFeedbackTable } from '../schema';
@@ -26,6 +27,25 @@ export default class UninstallFeedback {
.where(gte(uninstallFeedbackTable.timestamp, date));
}
+ /**
+ * Count one uninstall reason's reports by UTC day, including zero-count days.
+ * Start is inclusive and end is exclusive; both must be UTC midnight.
+ * A null start uses the first matching report's day, or returns no points if none exist.
+ */
+ public static async getDailyReportCounts(
+ reason: string,
+ start: Date | null,
+ end: Date,
+ ): Promise<{ date: Date; value: number }[]> {
+ const endTime = end.getTime();
+ const reports = await this.getAllBetween(start ?? new Date(0), new Date(endTime - 1));
+ return countReportsByDay(
+ reports.filter((report) => (report.feedback ?? 'OTHER') === reason),
+ start,
+ end,
+ );
+ }
+
public static async getAllBetween(start: Date, end: Date) {
return await db
.select()
diff --git a/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.server.ts b/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.server.ts
index bf6c92b1d..54339dcde 100644
--- a/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.server.ts
+++ b/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.server.ts
@@ -29,5 +29,6 @@ export const load = async ({ params }) => {
return {
counts,
prevCounts,
+ timePeriod: slug,
};
};
diff --git a/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.svelte b/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.svelte
index 9dbde3cf3..2bfb84a3f 100644
--- a/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.svelte
+++ b/packages/web/src/routes/admin/ext-site-problems/[slug]/+page.svelte
@@ -11,4 +11,4 @@ let links = {
};
</script>
-<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Domains with the most reported problems." {links}/>
+<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Domains with the most reported problems." {links} keyLabel="Domain" keyLinkGenerator={(key) => `/admin/ext-site-problems/${data.timePeriod}/${encodeURIComponent(key)}`}/>
diff --git a/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.server.ts b/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.server.ts
new file mode 100644
index 000000000..b56d77008
--- /dev/null
+++ b/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.server.ts
@@ -0,0 +1,22 @@
+import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug } from '$lib/adminUtils';
+import DomainReviews from '$lib/db/models/DomainReviews';
+import type { PageServerLoad } from './$types';
+
+export const load: PageServerLoad = async ({ params }) => {
+ const { slug, domain } = params;
+ const duration = computeDurationFromSlug(slug);
+
+ if (duration === null) {
+ redirect(302, `/admin/ext-site-problems/all/${encodeURIComponent(domain)}`);
+ }
+
+ const end = new Date();
+ end.setUTCHours(0, 0, 0, 0);
+ const start = slug === 'all' ? null : new Date(end.getTime() - duration);
+
+ return {
+ domain,
+ points: await DomainReviews.getDailyReportCounts(domain, start, end),
+ };
+};
diff --git a/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.svelte b/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.svelte
new file mode 100644
index 000000000..5dc1e2b1c
--- /dev/null
+++ b/packages/web/src/routes/admin/ext-site-problems/[slug]/[domain]/+page.svelte
@@ -0,0 +1,8 @@
+<script lang="ts">
+import TimeseriesAnalyticsPage from '$lib/components/TimeseriesAnalyticsPage.svelte';
+import type { PageProps } from './$types';
+
+let { data }: PageProps = $props();
+</script>
+
+<TimeseriesAnalyticsPage heading={data.domain} datapoints={data.points} />
diff --git a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.server.ts b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.server.ts
index bf3c8020b..9d66ff837 100644
--- a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.server.ts
+++ b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.server.ts
@@ -29,5 +29,6 @@ export const load = async ({ params }) => {
return {
counts,
prevCounts,
+ timePeriod: slug,
};
};
diff --git a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.svelte b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.svelte
index 641869a1e..bf95f866b 100644
--- a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.svelte
+++ b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/+page.svelte
@@ -11,4 +11,4 @@ let links = {
};
</script>
-<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Most Common Reasons to Uninstall the Chrome Ext" {links}/>
+<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Most Common Reasons to Uninstall the Chrome Ext" {links} keyLabel="Reason" keyLinkGenerator={(key) => `/admin/ext-uninstall-reasons/${data.timePeriod}/${encodeURIComponent(key)}`}/>
diff --git a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.server.ts b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.server.ts
new file mode 100644
index 000000000..17c5ab4f8
--- /dev/null
+++ b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.server.ts
@@ -0,0 +1,22 @@
+import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug } from '$lib/adminUtils';
+import UninstallFeedback from '$lib/db/models/UninstallFeedback';
+import type { PageServerLoad } from './$types';
+
+export const load: PageServerLoad = async ({ params }) => {
+ const { slug, reason } = params;
+ const duration = computeDurationFromSlug(slug);
+
+ if (duration === null) {
+ redirect(302, `/admin/ext-uninstall-reasons/all/${encodeURIComponent(reason)}`);
+ }
+
+ const end = new Date();
+ end.setUTCHours(0, 0, 0, 0);
+ const start = slug === 'all' ? null : new Date(end.getTime() - duration);
+
+ return {
+ reason,
+ points: await UninstallFeedback.getDailyReportCounts(reason, start, end),
+ };
+};
diff --git a/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.svelte b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.svelte
new file mode 100644
index 000000000..52702d430
--- /dev/null
+++ b/packages/web/src/routes/admin/ext-uninstall-reasons/[slug]/[reason]/+page.svelte
@@ -0,0 +1,8 @@
+<script lang="ts">
+import TimeseriesAnalyticsPage from '$lib/components/TimeseriesAnalyticsPage.svelte';
+import type { PageProps } from './$types';
+
+let { data }: PageProps = $props();
+</script>
+
+<TimeseriesAnalyticsPage heading={data.reason} datapoints={data.points} />
diff --git a/packages/web/src/routes/admin/problematic-lints/[slug]/+page.server.ts b/packages/web/src/routes/admin/problematic-lints/[slug]/+page.server.ts
index cb9494289..4e93dccb3 100644
--- a/packages/web/src/routes/admin/problematic-lints/[slug]/+page.server.ts
+++ b/packages/web/src/routes/admin/problematic-lints/[slug]/+page.server.ts
@@ -29,5 +29,6 @@ export const load = async ({ params }) => {
return {
counts,
prevCounts,
+ timePeriod: slug,
};
};
diff --git a/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte b/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte
index 4ad19f5f5..2ca473332 100644
--- a/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte
+++ b/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte
@@ -12,4 +12,4 @@ let links = {
</script>
-<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Most Reported Erroring Lint IDs" {links}/>
+<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Most Reported Erroring Lint IDs" {links} keyLabel="Lint ID" keyLinkGenerator={(key) => `/admin/problematic-lints/${data.timePeriod}/${key}`}/>
diff --git a/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.server.ts b/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.server.ts
new file mode 100644
index 000000000..f7ec19ab8
--- /dev/null
+++ b/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.server.ts
@@ -0,0 +1,26 @@
+import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug } from '$lib/adminUtils';
+import ProblematicLints from '$lib/db/models/ProblematicLints';
+import type { PageServerLoad } from './$types';
+
+/**
+ * Load one lint ID's counts for complete UTC days, ending with yesterday.
+ * All-time data starts at its first report's day and is empty if none exist.
+ */
+export const load: PageServerLoad = async ({ params }) => {
+ const { slug, lintId } = params;
+ const duration = computeDurationFromSlug(slug);
+
+ if (duration === null) {
+ redirect(302, `/admin/problematic-lints/all/${encodeURIComponent(lintId)}`);
+ }
+
+ const end = new Date();
+ end.setUTCHours(0, 0, 0, 0);
+ const start = slug === 'all' ? null : new Date(end.getTime() - duration);
+
+ return {
+ lintId,
+ points: await ProblematicLints.getDailyReportCounts(lintId, start, end),
+ };
+};
diff --git a/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.svelte b/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.svelte
new file mode 100644
index 000000000..806004b60
--- /dev/null
+++ b/packages/web/src/routes/admin/problematic-lints/[slug]/[lintId]/+page.svelte
@@ -0,0 +1,8 @@
+<script lang="ts">
+import TimeseriesAnalyticsPage from '$lib/components/TimeseriesAnalyticsPage.svelte';
+import type { PageProps } from './$types';
+
+let { data }: PageProps = $props();
+</script>
+
+<TimeseriesAnalyticsPage heading={data.lintId} datapoints={data.points} />