diff --git a/package.json b/package.json index c21b3ee..8705c78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api", - "version": "1.2.1", + "version": "1.3.0", "scripts": { "build": "rm -rf lib && esbuild `find src \\( -name '*.ts' -o -name '*.tsx' \\)` --platform='node' --sourcemap --ignore-annotations --format='cjs' --target='es2022' --outdir='lib' && esbuild src/index.ts --platform='node' --sourcemap --ignore-annotations --format='cjs' --target='es2022' --outdir='lib' --banner:js='require(\"module-alias\").addAlias(\"@\", __dirname);'", "kit": "drizzle-kit", diff --git a/src/api/routes/global/stats/flags.ts b/src/api/routes/global/stats/flags.ts index 56387e0..2ccc202 100644 --- a/src/api/routes/global/stats/flags.ts +++ b/src/api/routes/global/stats/flags.ts @@ -1,5 +1,5 @@ import { globalAPIRouter } from "@/api" -import { number, time } from "@rjweb/utils" +import { time } from "@rjweb/utils" import { and, inArray, sql } from "drizzle-orm" export = new globalAPIRouter.Path('/') diff --git a/src/api/routes/global/stats/panels.ts b/src/api/routes/global/stats/panels.ts new file mode 100644 index 0000000..4ba2de3 --- /dev/null +++ b/src/api/routes/global/stats/panels.ts @@ -0,0 +1,71 @@ +import { globalAPIRouter } from "@/api" +import { number, time } from "@rjweb/utils" +import { and, avg, count, inArray, max, sql, sum } from "drizzle-orm" + +export = new globalAPIRouter.Path('/') + .http('GET', '/', (http) => http + .document({ + description: 'Get the share of various blueprint panel stats', + responses: { + 200: { + description: 'Success', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + total: { + type: 'integer' + }, docker: { + type: 'integer' + }, standalone: { + type: 'integer' + }, + + extensions: { + type: 'object', + properties: { + max: { + type: 'integer' + }, average: { + type: 'number' + } + }, required: ['max', 'average'] + } + }, required: ['total', 'docker', 'extensions'] + } + } + } + } + } + }) + .onRequest(async(ctr) => { + const [ stats ] = await ctr["@"].cache.use('stats::all', () => ctr["@"].database.select({ + totalPanels: count(), + dockerPanels: sum(sql`(data->'blueprint'->>'docker')::boolean::int`).mapWith(Number), + maxExtensions: max(sql`jsonb_array_length(data->'blueprint'->'extensions')`).mapWith(Number), + avgExtensions: avg(sql`jsonb_array_length(data->'blueprint'->'extensions')`).mapWith(Number) + }) + .from(ctr["@"].database.schema.telemetryData) + .where(and( + inArray( + ctr["@"].database.schema.telemetryData.id, + ctr["@"].database.select({ id: ctr["@"].database.schema.telemetryPanelsWithLatest.latest.latestTelemetryDataId }) + .from(ctr["@"].database.schema.telemetryPanelsWithLatest) + ), + sql`created > NOW() - INTERVAL '2 days'` + )), + time(5).m() + ) + + return ctr.print({ + total: stats.totalPanels, + docker: stats.dockerPanels, + standalone: stats.totalPanels - stats.dockerPanels, + extensions: { + max: stats.maxExtensions, + average: number.round(stats.avgExtensions, 2) + } + }) + }) + ) \ No newline at end of file