GeezerVison is showing github.com — fix(stats): reduce database query load · krumpstead/opencode@08c483d
File treeExpand file treeCollapse file tree Open diff view settings migrations/20260903161929_parched_patriot
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/lib/stats-proxy.ts +43-2Lines changed: 43 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { APIEvent } from "@solidjs/start/server" | 2 | | -import { Resource } from "@opencode-ai/console-resource" | | 2 | +import { Resource, waitUntil } from "@opencode-ai/console-resource" | 3 | 3 | import { LOCALE_HEADER, cookie, localeFromRequest, route, tag } from "~/lib/language" | 4 | 4 |
| 5 | 5 | const dataPath = "/data" | | 6 | +const statsCacheParam = "__opencode_stats_locale" | 6 | 7 |
| 7 | 8 | export async function statsProxy(evt: APIEvent) { | 8 | 9 | const req = evt.request.clone() | 9 | 10 | const locale = localeFromRequest(req) | 10 | 11 | const redirect = redirectToLocalizedData(req, new URL(req.url), locale) | 11 | 12 | if (redirect) return redirect | 12 | 13 |
| | 14 | + const cache = defaultCache(caches) | | 15 | + const cacheKey = statsCacheKey(req, locale) | | 16 | + if (cacheKey) { | | 17 | + const cached = await cache.match(cacheKey) | | 18 | + if (cached) return withStatsCacheStatus(cached, "HIT") | | 19 | + } | | 20 | + | 13 | 21 | const targetUrl = new URL(req.url) | 14 | 22 | targetUrl.protocol = "https:" | 15 | 23 | targetUrl.hostname = Resource.App.stage === "production" ? "stats.opencode.ai" : "stats.dev.opencode.ai" | @@ -40,13 +48,18 @@ export async function statsProxy(evt: APIEvent) { | 40 | 48 | headers.delete("content-encoding") | 41 | 49 | headers.delete("content-length") | 42 | 50 | headers.delete("etag") | | 51 | + headers.delete("set-cookie") | 43 | 52 | appendVary(headers, "Accept-Language", "Cookie", LOCALE_HEADER) | 44 | 53 |
| 45 | | - return new Response(rewriteStatsHtml(await response.text()), { | | 54 | + const result = new Response(rewriteStatsHtml(await response.text()), { | 46 | 55 | status: response.status, | 47 | 56 | statusText: response.statusText, | 48 | 57 | headers, | 49 | 58 | }) | | 59 | + if (!cacheKey || !response.ok) return result | | 60 | + | | 61 | + void waitUntil(cache.put(cacheKey, result.clone())) | | 62 | + return withStatsCacheStatus(result, "MISS") | 50 | 63 | } | 51 | 64 |
| 52 | 65 | export function statsRedirect(evt: APIEvent) { | @@ -64,6 +77,34 @@ function rewriteStatsHtml(html: string) { | 64 | 77 | return html.replaceAll('"/_build/', `"${dataPath}/_build/`).replaceAll("'/_build/", `'${dataPath}/_build/`) | 65 | 78 | } | 66 | 79 |
| | 80 | +function statsCacheKey(request: Request, locale: ReturnType<typeof localeFromRequest>): Request | undefined { | | 81 | + if (request.method !== "GET") return undefined | | 82 | + if (!acceptsHtml(request)) return undefined | | 83 | + if (isDataBypassPath(new URL(request.url).pathname)) return undefined | | 84 | + | | 85 | + const url = new URL(request.url) | | 86 | + const additionalModels = url.searchParams.get("add") | | 87 | + url.search = "" | | 88 | + if (additionalModels) url.searchParams.set("add", additionalModels) | | 89 | + url.searchParams.set(statsCacheParam, locale) | | 90 | + return new Request(url) | | 91 | +} | | 92 | + | | 93 | +function defaultCache(storage: CacheStorage) { | | 94 | + if (!isCloudflareCacheStorage(storage)) throw new Error("Cloudflare default cache is unavailable") | | 95 | + return storage.default | | 96 | +} | | 97 | + | | 98 | +function isCloudflareCacheStorage(storage: CacheStorage): storage is CacheStorage & { default: Cache } { | | 99 | + return "default" in storage | | 100 | +} | | 101 | + | | 102 | +function withStatsCacheStatus(response: Response, status: "HIT" | "MISS") { | | 103 | + const headers = new Headers(response.headers) | | 104 | + headers.set("x-opencode-stats-cache", status) | | 105 | + return new Response(response.body, { status: response.status, statusText: response.statusText, headers }) | | 106 | +} | | 107 | + | 67 | 108 | function redirectToLocalizedData(request: Request, url: URL, locale: ReturnType<typeof localeFromRequest>) { | 68 | 109 | if (locale === "en") return null | 69 | 110 | if (request.headers.get(LOCALE_HEADER)) return null | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/component/model-compare-detail.tsx +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | type StatsModelComparisonInput, | 8 | 8 | type StatsModelComparisonEntry, | 9 | 9 | } from "@opencode-ai/stats-core/domain/home" | 10 | | -import { runtime } from "@opencode-ai/stats-core/runtime" | 11 | 10 | import { createAsync, query, useParams, useSearchParams } from "@solidjs/router" | 12 | 11 | import { createEffect, createMemo, createSignal, For, onMount, Show } from "solid-js" | 13 | 12 | import { getRequestEvent } from "solid-js/web" | @@ -46,6 +45,7 @@ import { | 46 | 45 | type ResolvedComparisonFamily, | 47 | 46 | } from "../lib/comparison-pages" | 48 | 47 | import { baseUrl } from "../lib/language" | | 48 | +import { runStatsEffect } from "../stats-runtime" | 49 | 49 |
| 50 | 50 | const compareHeaderLinks: readonly HeaderLink[] = [ | 51 | 51 | { href: `${import.meta.env.BASE_URL}#top-models`, label: "Top Models" }, | @@ -110,7 +110,7 @@ export type ModelCompareDetailPageProps = { | 110 | 110 |
| 111 | 111 | const getComparisonData = query(async (models: StatsModelComparisonInput[]) => { | 112 | 112 | "use server" | 113 | | - return runtime.runPromise(getStatsModelsComparisonData(models)) | | 113 | + return runStatsEffect(getStatsModelsComparisonData(models)) | 114 | 114 | }, "getStatsModelComparisonDetailData") | 115 | 115 |
| 116 | 116 | export default function ModelCompareDetailPage(props: ModelCompareDetailPageProps = {}) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/[lab]/[model].tsx +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ type ModelPageCatalog = { | 52 | 52 | labs: { id: string; name: string }[] | 53 | 53 | labModels: ModelCatalogOption[] | 54 | 54 | } | 55 | | -type StatsModelPageData = Omit<StatsModelData, "country"> & { country: CountryEntry[] } | | 55 | +type StatsModelPageData = StatsModelData | 56 | 56 | type ModelPageData = { catalog: ModelPageCatalog; stats: StatsModelPageData | null } | 57 | 57 |
| 58 | 58 | const countryNumericIds = new Map( | @@ -75,7 +75,7 @@ const getModelPageData = query(async (labParam: string, modelParam: string) => { | 75 | 75 | .find((item) => item.id === (entry?.lab ?? providerSlug(labParam))) | 76 | 76 | ?.models.map((item) => ({ id: item.id, lab: item.lab, slug: item.slug, name: item.name })) ?? [], | 77 | 77 | }, | 78 | | - stats: stats ? { ...stats, country: stats.country["2M"] } : null, | | 78 | + stats, | 79 | 79 | } satisfies ModelPageData | 80 | 80 | }, "getStatsModelPageData") | 81 | 81 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/index.tsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,7 +91,7 @@ const getData = query(async () => { | 91 | 91 | cacheRatio: stats.cacheRatio.Go, | 92 | 92 | sessionCost: stats.sessionCost.Go, | 93 | 93 | retention: stats.retention, | 94 | | - country: stats.country["2M"], | | 94 | + country: stats.country, | 95 | 95 | } satisfies StatsHomePageData | 96 | 96 | }, "getStatsHomeData") | 97 | 97 |
| |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +CREATE INDEX `idx_country_model_range` ON `geo_stat` (`model`,`provider`,`grain`,`dataset`,`client`,`source`,`tier`,`period_key`); |
|