Skip to content

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

feat(web): show deltas on graphs (#4424)

Elijah Potter487c02887192
diff --git a/packages/web/src/lib/adminUtils.ts b/packages/web/src/lib/adminUtils.ts
new file mode 100644
index 000000000..525185ed0
--- /dev/null
+++ b/packages/web/src/lib/adminUtils.ts
@@ -0,0 +1,30 @@
+/** Given a slug of the form `"lastweek" | "last30days" | "lastday" | "all"`, compute a representative duration. */
+export function computeDurationFromSlug(slug: string): number | null {
+ switch (slug) {
+ case 'last30days':
+ return 30 * 24 * 60 * 60 * 1000;
+ case 'lastday':
+ return 24 * 60 * 60 * 1000;
+ case 'lastweek':
+ return 24 * 60 * 60 * 1000 * 7;
+ case 'all':
+ return Date.now();
+ default:
+ return null;
+ }
+}
+
+/** Count the occurances of unique strings in an array. */
+export function countOccurances(arr: string[]): Record<string, number> {
+ const counts: Record<string, number> = {};
+
+ for (const item of arr) {
+ if (counts[item] === undefined) {
+ counts[item] = 1;
+ } else {
+ counts[item] += 1;
+ }
+ }
+
+ return counts;
+}
diff --git a/packages/web/src/lib/components/AnalyticsPage.svelte b/packages/web/src/lib/components/AnalyticsPage.svelte
index b81446219..4bfe59a14 100644
--- a/packages/web/src/lib/components/AnalyticsPage.svelte
+++ b/packages/web/src/lib/components/AnalyticsPage.svelte
@@ -4,14 +4,27 @@ import BarChart from './BarChart.svelte';
type Props = {
data: Record<string, number>;
+ /** The data from the previous period, used to show deltas. */
+ prevPeriodData?: Record<string, number>;
title: string;
links: Record<string, string>;
};
-let { data, title, links }: Props = $props();
+let { data, prevPeriodData, title, links }: Props = $props();
let counts = $derived(data);
+let prevPeriodCounts = $derived(prevPeriodData);
+
let entries = $derived(Object.entries(counts).toSorted(([_a, a], [_b, b]) => b - a));
+
+let deltas = $derived(
+ Object.fromEntries(
+ entries.map(([key, val]) => {
+ let prevVal = prevPeriodCounts ? (prevPeriodCounts[key] ?? 0) : 0;
+ return [key, val - prevVal];
+ }),
+ ),
+);
</script>
<Isolate>
@@ -25,6 +38,9 @@ let entries = $derived(Object.entries(counts).toSorted(([_a, a], [_b, b]) => b -
<GutterCenter >
<BarChart data={counts} title={title}/>
+ {#if prevPeriodCounts != null}
+ <BarChart data={deltas} title="Change" />
+ {/if}
<table>
<thead>
diff --git a/packages/web/src/lib/components/LineChart.svelte b/packages/web/src/lib/components/LineChart.svelte
new file mode 100644
index 000000000..b3c73e513
--- /dev/null
+++ b/packages/web/src/lib/components/LineChart.svelte
@@ -0,0 +1,153 @@
+<script module lang="ts">
+/** A timeseries observation with a valid Date and a finite numeric value. */
+export interface TimeSeriesPoint {
+ date: Date;
+ value: number;
+}
+</script>
+
+<script lang="ts">
+import type { Point } from 'chart.js';
+import Chart from 'chart.js/auto';
+import { onMount } from 'svelte';
+
+/** Props for a single-series chart. Dates are displayed in the viewer's local timezone. */
+interface Props {
+ /** Observations in any order; defaults to empty. Invalid dates and nonfinite values are omitted. */
+ data?: TimeSeriesPoint[];
+ title: string;
+}
+
+let { data = [], title }: Props = $props();
+
+let chartCanvas = $state<HTMLCanvasElement>();
+let chart: Chart<'line', Point[]> | null = null;
+let points = $derived(toChartPoints(data));
+
+const dateFormatter = new Intl.DateTimeFormat(undefined, {
+ dateStyle: 'short',
+ timeStyle: 'short',
+});
+
+/** Convert dates to milliseconds and sort chronologically without mutating input or merging duplicates. */
+function toChartPoints(data: TimeSeriesPoint[]): Point[] {
+ return data
+ .map(({ date, value }) => ({ x: date.getTime(), y: value }))
+ .filter(({ x, y }) => Number.isFinite(x) && Number.isFinite(y))
+ .toSorted((a, b) => a.x - b.x);
+}
+
+onMount(() => {
+ chart = new Chart(chartCanvas!, {
+ type: 'line',
+ data: {
+ datasets: [
+ {
+ data: points,
+ borderColor: 'rgba(80, 80, 80, 1)',
+ backgroundColor: 'rgba(80, 80, 80, 1)',
+ borderWidth: 2,
+ pointRadius: 3,
+ fill: false,
+ tension: 0,
+ },
+ ],
+ },
+ options: {
+ responsive: true,
+ maintainAspectRatio: false,
+ plugins: {
+ title: {
+ display: true,
+ text: title,
+ color: '#444',
+ font: {
+ size: 18,
+ weight: 'bold',
+ },
+ },
+ legend: {
+ display: false,
+ },
+ tooltip: {
+ callbacks: {
+ title: (items) =>
+ items.length ? dateFormatter.format(items[0].parsed.x) : '',
+ },
+ },
+ },
+ scales: {
+ x: {
+ // Numeric timestamps preserve elapsed-time spacing without a date adapter.
+ type: 'linear',
+ grid: {
+ color: '#ddd',
+ },
+ ticks: {
+ callback: (value) => dateFormatter.format(Number(value)),
+ maxTicksLimit: 6,
+ color: '#333',
+ font: {
+ size: 14,
+ },
+ },
+ },
+ y: {
+ beginAtZero: true,
+ grid: {
+ color: '#ddd',
+ },
+ ticks: {
+ color: '#333',
+ font: {
+ size: 14,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ return () => {
+ chart?.destroy();
+ chart = null;
+ };
+});
+
+/** Synchronize data and title on the existing chart rather than recreating its canvas resources. */
+function updateChart(points: Point[], title: string) {
+ if (!chart) return;
+
+ chart.data.datasets[0].data = points;
+ if (chart.options.plugins?.title) {
+ chart.options.plugins.title.text = title;
+ }
+ chart.update();
+}
+
+$effect(() => {
+ updateChart(points, title);
+});
+</script>
+
+<style>
+ .chart-container {
+ background: #f9f9f9;
+ border: 1px solid #ccc;
+ border-radius: 8px;
+ padding: 1rem;
+ width: 100%;
+ max-width: 700px;
+ height: 400px;
+ margin: 0 auto;
+ }
+
+ canvas {
+ width: 100%;
+ height: 100%;
+ }
+</style>
+
+<div class="chart-container">
+ <canvas bind:this={chartCanvas} aria-label={title}>{title}</canvas>
+</div>
diff --git a/packages/web/src/lib/db/models/DomainReviews.ts b/packages/web/src/lib/db/models/DomainReviews.ts
index bd2a1462e..a96a9b2f4 100644
--- a/packages/web/src/lib/db/models/DomainReviews.ts
+++ b/packages/web/src/lib/db/models/DomainReviews.ts
@@ -1,4 +1,4 @@
-import { gte } from 'drizzle-orm';
+import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { db } from '..';
import { domainReviewTable } from '../schema';
@@ -22,4 +22,11 @@ export default class DomainReviews {
public static async getAllSince(date: Date) {
return await db.select().from(domainReviewTable).where(gte(domainReviewTable.timestamp, date));
}
+
+ public static async getAllBetween(start: Date, end: Date) {
+ return await db
+ .select()
+ .from(domainReviewTable)
+ .where(and(gte(domainReviewTable.timestamp, start), lte(domainReviewTable.timestamp, end)));
+ }
}
diff --git a/packages/web/src/lib/db/models/ProblematicLints.ts b/packages/web/src/lib/db/models/ProblematicLints.ts
index b7e6a4f81..ec10ce4be 100644
--- a/packages/web/src/lib/db/models/ProblematicLints.ts
+++ b/packages/web/src/lib/db/models/ProblematicLints.ts
@@ -1,4 +1,4 @@
-import { gte } from 'drizzle-orm';
+import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { db } from '..';
import { problematicLintTable } from '../schema';
@@ -29,4 +29,13 @@ export default class ProblematicLints {
.from(problematicLintTable)
.where(gte(problematicLintTable.timestamp, date));
}
+
+ public static async getAllBetween(start: Date, end: Date) {
+ return await db
+ .select()
+ .from(problematicLintTable)
+ .where(
+ and(gte(problematicLintTable.timestamp, start), lte(problematicLintTable.timestamp, end)),
+ );
+ }
}
diff --git a/packages/web/src/lib/db/models/UninstallFeedback.ts b/packages/web/src/lib/db/models/UninstallFeedback.ts
index b7bcdbb4a..30ff71857 100644
--- a/packages/web/src/lib/db/models/UninstallFeedback.ts
+++ b/packages/web/src/lib/db/models/UninstallFeedback.ts
@@ -1,4 +1,4 @@
-import { gte } from 'drizzle-orm';
+import { and, gte, lte } from 'drizzle-orm';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { db } from '..';
import { uninstallFeedbackTable } from '../schema';
@@ -25,4 +25,16 @@ export default class UninstallFeedback {
.from(uninstallFeedbackTable)
.where(gte(uninstallFeedbackTable.timestamp, date));
}
+
+ public static async getAllBetween(start: Date, end: Date) {
+ return await db
+ .select()
+ .from(uninstallFeedbackTable)
+ .where(
+ and(
+ gte(uninstallFeedbackTable.timestamp, start),
+ lte(uninstallFeedbackTable.timestamp, end),
+ ),
+ );
+ }
}
diff --git a/packages/web/src/lib/db/models/UpdateCheckCounts.ts b/packages/web/src/lib/db/models/UpdateCheckCounts.ts
index 12ea3df6d..25f2b6c7f 100644
--- a/packages/web/src/lib/db/models/UpdateCheckCounts.ts
+++ b/packages/web/src/lib/db/models/UpdateCheckCounts.ts
@@ -6,9 +6,9 @@ import { updateCheckCountTable } from '../schema';
export type UpdateCheckCountRow = typeof updateCheckCountTable.$inferSelect;
const UpdateCheckCountRowParser = createSelectSchema(updateCheckCountTable);
-export default class DomainReviews {
+export default class UpdateCheckCounts {
public static async incrementForToday() {
- DomainReviews.incrementForDate(new Date());
+ UpdateCheckCounts.incrementForDate(new Date());
}
public static async incrementForDate(date: Date) {
@@ -39,4 +39,8 @@ export default class DomainReviews {
return first.count;
}
}
+
+ public static async getAll(): Promise<UpdateCheckCountRow[]> {
+ return await db.select().from(updateCheckCountTable);
+ }
}
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 ad60ed4ee..bf6c92b1d 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
@@ -1,47 +1,33 @@
import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug, countOccurances } from '$lib/adminUtils';
import DomainReviews from '$lib/db/models/DomainReviews';
export const load = async ({ params }) => {
const slug = params.slug;
- let date = null;
+ const duration = computeDurationFromSlug(slug);
- switch (slug) {
- case 'last30days':
- date = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
- break;
- case 'lastday':
- date = new Date(Date.now() - 24 * 60 * 60 * 1000);
- break;
- case 'all':
- date = new Date(0);
- break;
- }
-
- if (date == null) {
+ if (duration == null) {
redirect(302, '/admin/ext-site-problems/all');
}
- const domainReviews = await DomainReviews.getAllSince(date);
-
- const counts: Record<string, number> = {};
+ const date = Date.now() - duration;
- for (const item of domainReviews) {
- const id = item.domain ?? 'OTHER';
+ const domainReviews = await DomainReviews.getAllSince(new Date(date));
+ const prevDomainReviews = await DomainReviews.getAllBetween(
+ new Date(date - duration),
+ new Date(date),
+ );
- // We are looking for _problematic_ domains, not ones that Harper already works on.
- if (item.works) {
- continue;
- }
-
- if (counts[id] === undefined) {
- counts[id] = 1;
- } else {
- counts[id] += 1;
- }
- }
+ const counts: Record<string, number> = countOccurances(
+ domainReviews.map((i) => i.domain ?? 'OTHER'),
+ );
+ const prevCounts: Record<string, number> = countOccurances(
+ prevDomainReviews.map((i) => i.domain ?? 'OTHER'),
+ );
return {
counts,
+ prevCounts,
};
};
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 2bbae4802..9dbde3cf3 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
@@ -6,8 +6,9 @@ let { data }: PageProps = $props();
let links = {
All: '/admin/ext-site-problems/all',
'Last 30 Days': '/admin/ext-site-problems/last30days',
+ 'Last week': '/admin/ext-site-problems/lastweek',
'Last Day': '/admin/ext-site-problems/lastday',
};
</script>
-<AnalyticsPage data={data.counts} title="Domains with the most reported problems." {links}/>
+<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Domains with the most reported problems." {links}/>
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 39747eed3..bf3c8020b 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
@@ -1,42 +1,33 @@
import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug, countOccurances } from '$lib/adminUtils';
import UninstallFeedback from '$lib/db/models/UninstallFeedback';
export const load = async ({ params }) => {
const slug = params.slug;
- let date = null;
+ const duration = computeDurationFromSlug(slug);
- switch (slug) {
- case 'last30days':
- date = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
- break;
- case 'lastday':
- date = new Date(Date.now() - 24 * 60 * 60 * 1000);
- break;
- case 'all':
- date = new Date(0);
- break;
- }
-
- if (date == null) {
+ if (duration == null) {
redirect(302, '/admin/ext-uninstall-reasons/all');
}
- const problematicLints = await UninstallFeedback.getAllSince(date);
+ const date = Date.now() - duration;
- const counts: Record<string, number> = {};
+ const uninstallFeedback = await UninstallFeedback.getAllSince(new Date(date));
+ const prevUninstallFeedback = await UninstallFeedback.getAllBetween(
+ new Date(date - duration),
+ new Date(date),
+ );
- for (const item of problematicLints) {
- const id = item.feedback ?? 'OTHER';
-
- if (counts[id] === undefined) {
- counts[id] = 1;
- } else {
- counts[id] += 1;
- }
- }
+ const counts: Record<string, number> = countOccurances(
+ uninstallFeedback.map((i) => i.feedback ?? 'OTHER'),
+ );
+ const prevCounts: Record<string, number> = countOccurances(
+ prevUninstallFeedback.map((i) => i.feedback ?? 'OTHER'),
+ );
return {
counts,
+ prevCounts,
};
};
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 9575991d9..641869a1e 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
@@ -6,8 +6,9 @@ let { data }: PageProps = $props();
let links = {
All: '/admin/ext-uninstall-reasons/all',
'Last 30 Days': '/admin/ext-uninstall-reasons/last30days',
+ 'Last week': '/admin/ext-uninstall-reasons/lastweek',
'Last Day': '/admin/ext-uninstall-reasons/lastday',
};
</script>
-<AnalyticsPage data={data.counts} 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}/>
diff --git a/packages/web/src/routes/admin/install-graph/+page.server.ts b/packages/web/src/routes/admin/install-graph/+page.server.ts
new file mode 100644
index 000000000..6d85fe0dd
--- /dev/null
+++ b/packages/web/src/routes/admin/install-graph/+page.server.ts
@@ -0,0 +1,9 @@
+import UpdateCheckCounts from '$lib/db/models/UpdateCheckCounts';
+
+export const load = async () => {
+ const all = await UpdateCheckCounts.getAll();
+
+ return {
+ all,
+ };
+};
diff --git a/packages/web/src/routes/admin/install-graph/+page.svelte b/packages/web/src/routes/admin/install-graph/+page.svelte
new file mode 100644
index 000000000..74629dfb3
--- /dev/null
+++ b/packages/web/src/routes/admin/install-graph/+page.svelte
@@ -0,0 +1,11 @@
+<script lang="ts">
+import { Isolate } from 'components';
+import LineChart from '$lib/components/LineChart.svelte';
+import type { PageProps } from './$types';
+
+let { data }: PageProps = $props();
+</script>
+
+<Isolate>
+ <LineChart title="Installs Per Day" data={data.all.map(({date, count}) => { return {date, value: count}})} />
+</Isolate>
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 32f684802..cb9494289 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
@@ -1,42 +1,33 @@
import { redirect } from '@sveltejs/kit';
+import { computeDurationFromSlug, countOccurances } from '$lib/adminUtils';
import ProblematicLints from '$lib/db/models/ProblematicLints';
export const load = async ({ params }) => {
const slug = params.slug;
- let date = null;
+ const duration = computeDurationFromSlug(slug);
- switch (slug) {
- case 'last30days':
- date = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
- break;
- case 'lastday':
- date = new Date(Date.now() - 24 * 60 * 60 * 1000);
- break;
- case 'all':
- date = new Date(0);
- break;
- }
-
- if (date == null) {
+ if (duration == null) {
redirect(302, '/admin/problematic-lints/all');
}
- const problematicLints = await ProblematicLints.getAllSince(date);
+ const date = Date.now() - duration;
- const counts: Record<string, number> = {};
+ const problematicLints = await ProblematicLints.getAllSince(new Date(date));
+ const prevProblematicLints = await ProblematicLints.getAllBetween(
+ new Date(date - duration),
+ new Date(date),
+ );
- for (const item of problematicLints) {
- const id = item.rule_id ?? 'OTHER';
-
- if (counts[id] === undefined) {
- counts[id] = 1;
- } else {
- counts[id] += 1;
- }
- }
+ const counts: Record<string, number> = countOccurances(
+ problematicLints.map((i) => i.rule_id ?? 'OTHER'),
+ );
+ const prevCounts: Record<string, number> = countOccurances(
+ prevProblematicLints.map((i) => i.rule_id ?? 'OTHER'),
+ );
return {
counts,
+ prevCounts,
};
};
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 6e8f103a4..4ad19f5f5 100644
--- a/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte
+++ b/packages/web/src/routes/admin/problematic-lints/[slug]/+page.svelte
@@ -6,8 +6,10 @@ let { data }: PageProps = $props();
let links = {
All: '/admin/problematic-lints/all',
'Last 30 Days': '/admin/problematic-lints/last30days',
+ 'Last week': '/admin/problematic-lints/lastweek',
'Last Day': '/admin/problematic-lints/lastday',
};
</script>
-<AnalyticsPage data={data.counts} title="Most Reported Erroring Lint IDs" {links}/>
+
+<AnalyticsPage data={data.counts} prevPeriodData={data.prevCounts} title="Most Reported Erroring Lint IDs" {links}/>