Skip to content

Commit

Permalink
add panel stats
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7d8 committed Dec 30, 2024
1 parent 553439d commit f524dc5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/global/stats/flags.ts
Original file line number Diff line number Diff line change
@@ -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('/')
Expand Down
71 changes: 71 additions & 0 deletions src/api/routes/global/stats/panels.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
)

0 comments on commit f524dc5

Please sign in to comment.