Skip to content

Commit

Permalink
add installed panels count to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7d8 committed Dec 29, 2024
1 parent 0234168 commit 6c3be96
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 39 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.1.2",
"version": "1.1.3",
"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
15 changes: 11 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ server.schema('Extension', {
},

banner: { type: 'string', format: 'uri' },
created: { type: 'string', format: 'date-time' }
}, required: ['id', 'type', 'author', 'name', 'identifier', 'summary', 'platforms', 'banner', 'created']
created: { type: 'string', format: 'date-time' },

stats: {
type: 'object',
properties: {
panels: {
type: 'integer'
}
}, required: ['panels']
}
}, required: ['id', 'type', 'author', 'name', 'identifier', 'summary', 'platforms', 'banner', 'created', 'stats']
})

export const globalAPIRouter = new server.FileLoader('/api')
Expand Down Expand Up @@ -122,8 +131,6 @@ export const authorAPIRouter = new server.FileLoader('/api/author')
)
.export()

const allowedId = /^[0-9a-f]{23}$/

server.path('/', (path) => path
.http('GET', '/openapi.json', (http) => http
.onRequest((ctr) => {
Expand Down
33 changes: 0 additions & 33 deletions src/api/routes/global/extensions.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/api/routes/global/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { globalAPIRouter } from "@/api"
import { time } from "@rjweb/utils"
import { asc, eq, sql } from "drizzle-orm"

export = new globalAPIRouter.Path('/')
.http('GET', '/', (http) => http
.document({
description: 'Get all blueprint extensions',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Extension'
}
}
}
}
}
}
})
.onRequest(async(ctr) => {
const extensions = await ctr["@"].cache.use('extensions::all', () => ctr["@"].database.select(ctr["@"].database.fields.extension)
.from(ctr["@"].database.schema.extensions)
.innerJoin(ctr["@"].database.schema.authors, eq(ctr["@"].database.schema.extensions.authorId, ctr["@"].database.schema.authors.id))
.where(eq(ctr["@"].database.schema.extensions.hidden, false))
.orderBy(asc(ctr["@"].database.schema.extensions.id)),
time(5).m()
)

return ctr.print(extensions)
})
)
58 changes: 58 additions & 0 deletions src/api/routes/global/extensions/{id}/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { globalAPIRouter } from "@/api"
import { time } from "@rjweb/utils"
import { and, eq, or, sql } from "drizzle-orm"

export = new globalAPIRouter.Path('/')
.http('GET', '/', (http) => http
.document({
description: 'Get a blueprint extension',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Extension'
}
}
}
}
}, parameters: [
{
name: 'id',
in: 'path',
description: 'The ID or identifier of the extension',
required: true,
schema: {
anyOf: [
{ type: 'integer' },
{ type: 'string' }
]
}
}
]
})
.onRequest(async(ctr) => {
const id = ctr.params.get('id', '')
if (!id) return ctr.status(ctr.$status.BAD_REQUEST).print({ errors: ['Invalid ID'] })

const idInt = parseInt(id)

const [ extension ] = await ctr["@"].cache.use(`extension::${id}`, () => ctr["@"].database.select(ctr["@"].database.fields.extension)
.from(ctr["@"].database.schema.extensions)
.innerJoin(ctr["@"].database.schema.authors, eq(ctr["@"].database.schema.extensions.authorId, ctr["@"].database.schema.authors.id))
.where(and(
eq(ctr["@"].database.schema.extensions.hidden, false),
or(
!isNaN(idInt) ? eq(ctr["@"].database.schema.extensions.id, idInt) : undefined,
eq(ctr["@"].database.schema.extensions.identifier, id)
)
)),
time(5).m()
)

if (!extension) return ctr.status(ctr.$status.NOT_FOUND).print({ errors: ['Extension not found'] })

return ctr.print(extension)
})
)
19 changes: 18 additions & 1 deletion src/globals/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as schema from "@/schema"
import env from "@/globals/env"
import logger from "@/globals/logger"
import { Pool } from "pg"
import { sql } from "drizzle-orm"

const pool = new Pool({
connectionString: env.DATABASE_URL
Expand Down Expand Up @@ -51,7 +52,23 @@ export default Object.assign(db as DbWithoutWrite, {

banner: schema.extensions.banner,

created: schema.extensions.created
created: schema.extensions.created,

stats: Object.freeze({
panels: sql<number>`(
SELECT COUNT(*)
FROM (
SELECT jsonb_array_elements(data->'blueprint'->'extensions') as ext
FROM telemetry_data
WHERE id IN (
SELECT latest_telemetry_data_id
FROM telemetry_panels_with_latest
)
AND created > NOW() - INTERVAL '2 days'
) subq
WHERE subq.ext->>'identifier' = ${schema.extensions.identifier}
)`.mapWith(Number)
})
})
})
})

0 comments on commit 6c3be96

Please sign in to comment.