Skip to content

Commit 75795bc

Browse files
authored
Merge pull request #80 from outerbase/bwilmoth/internal-stats
feat: Statistics and information about instance
2 parents f96721f + 87e4db3 commit 75795bc

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

plugins/stats/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { DataSource } from '../../dist'
2+
import { StarbaseApp, StarbaseDBConfiguration } from '../../src/handler'
3+
import { StarbasePlugin } from '../../src/plugin'
4+
5+
export class StatsPlugin extends StarbasePlugin {
6+
// Prefix route
7+
prefix: string = '/_internal/stats'
8+
// Configuration details about the request and user
9+
private config?: StarbaseDBConfiguration
10+
// Data source to run internal RPC queries
11+
dataSource?: DataSource
12+
13+
constructor() {
14+
super('starbasedb:stats', {
15+
requiresAuth: true,
16+
})
17+
}
18+
19+
override async register(app: StarbaseApp) {
20+
app.use(async (c, next) => {
21+
this.config = c?.get('config')
22+
this.dataSource = c?.get('dataSource')
23+
await next()
24+
})
25+
26+
app.get(this.prefix, async (c, next) => {
27+
// Only admin authorized users are permitted to subscribe to CDC events.
28+
if (this.config?.role !== 'admin') {
29+
return new Response('Unauthorized request', { status: 400 })
30+
}
31+
32+
// Get stats from internal source
33+
const stats = await this.dataSource?.rpc.getStatistics()
34+
const additionalStats = {
35+
...stats,
36+
plugins: this.dataSource?.registry?.currentPlugins(),
37+
}
38+
return new Response(JSON.stringify(additionalStats), {
39+
headers: {
40+
'Content-Type': 'application/json',
41+
},
42+
})
43+
})
44+
}
45+
}

src/do.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,35 @@ export class StarbaseDBDurableObject extends DurableObject {
6363

6464
init() {
6565
return {
66+
getStatistics: this.getStatistics.bind(this),
6667
executeQuery: this.executeQuery.bind(this),
6768
}
6869
}
6970

71+
public async getStatistics(): Promise<{
72+
databaseSize: number
73+
activeConnections: number
74+
recentQueries: number
75+
}> {
76+
const sql = `SELECT COUNT(*) as count
77+
FROM tmp_query_log
78+
WHERE created_at >= datetime('now', '-24 hours')`
79+
const result = (await this.executeQuery({
80+
sql,
81+
isRaw: false,
82+
})) as Record<string, SqlStorageValue>[]
83+
const row = result.length ? result[0] : { count: 0 }
84+
85+
return {
86+
// Size in bytes
87+
databaseSize: this.sql.databaseSize,
88+
// Count of persistent web socket connections
89+
activeConnections: this.connections.keys.length,
90+
// Assuming the `QueryLogPlugin` is in use, count is of the last 24 hours
91+
recentQueries: Number(row.count),
92+
}
93+
}
94+
7095
async fetch(request: Request) {
7196
const url = new URL(request.url)
7297

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { StudioPlugin } from '../plugins/studio'
99
import { SqlMacrosPlugin } from '../plugins/sql-macros'
1010
import { ChangeDataCapturePlugin } from '../plugins/cdc'
1111
import { QueryLogPlugin } from '../plugins/query-log'
12+
import { StatsPlugin } from '../plugins/stats'
1213

1314
export { StarbaseDBDurableObject } from './do'
1415

@@ -190,6 +191,7 @@ export default {
190191
}),
191192
new QueryLogPlugin({ ctx }),
192193
cdcPlugin,
194+
new StatsPlugin(),
193195
] satisfies StarbasePlugin[]
194196

195197
const starbase = new StarbaseDB({

src/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class StarbasePluginRegistry {
7070
}
7171
}
7272

73+
public currentPlugins(): string[] {
74+
return this.plugins.map((plugin) => plugin.name)
75+
}
76+
7377
public async beforeQuery(opts: {
7478
sql: string
7579
params?: unknown[]

worker-configuration.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ interface Env {
88
STUDIO_PASS: '123456'
99
ENABLE_ALLOWLIST: 0
1010
ENABLE_RLS: 0
11-
EXTERNAL_DB_TYPE: 'postgresql'
1211
AUTH_ALGORITHM: 'RS256'
1312
AUTH_JWKS_ENDPOINT: ''
1413
DATABASE_DURABLE_OBJECT: DurableObjectNamespace<

wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ENABLE_RLS = 0
4949
# External database source details
5050
# This enables Starbase to connect to an external data source
5151
# OUTERBASE_API_KEY = ""
52-
EXTERNAL_DB_TYPE = "postgresql"
52+
# EXTERNAL_DB_TYPE = "postgresql"
5353
# EXTERNAL_DB_HOST = ""
5454
# EXTERNAL_DB_PORT = 0
5555
# EXTERNAL_DB_USER = ""

0 commit comments

Comments
 (0)