Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Statistics and information about instance #80

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions plugins/stats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DataSource } from '../../dist'
import { StarbaseApp, StarbaseDBConfiguration } from '../../src/handler'
import { StarbasePlugin } from '../../src/plugin'

export class StatsPlugin extends StarbasePlugin {
// Prefix route
prefix: string = '/_internal/stats'
// Configuration details about the request and user
private config?: StarbaseDBConfiguration
// Data source to run internal RPC queries
dataSource?: DataSource

constructor() {
super('starbasedb:stats', {
requiresAuth: true,
})
}

override async register(app: StarbaseApp) {
app.use(async (c, next) => {
this.config = c?.get('config')
this.dataSource = c?.get('dataSource')
await next()
})

app.get(this.prefix, async (c, next) => {
// Only admin authorized users are permitted to subscribe to CDC events.
if (this.config?.role !== 'admin') {
return new Response('Unauthorized request', { status: 400 })
}

// Get stats from internal source
const stats = await this.dataSource?.rpc.getStatistics()
const additionalStats = {
...stats,
plugins: this.dataSource?.registry?.currentPlugins(),
}
return new Response(JSON.stringify(additionalStats), {
headers: {
'Content-Type': 'application/json',
},
})
})
}
}
25 changes: 25 additions & 0 deletions src/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,35 @@ export class StarbaseDBDurableObject extends DurableObject {

init() {
return {
getStatistics: this.getStatistics.bind(this),
executeQuery: this.executeQuery.bind(this),
}
}

public async getStatistics(): Promise<{
databaseSize: number
activeConnections: number
recentQueries: number
}> {
const sql = `SELECT COUNT(*) as count
FROM tmp_query_log
WHERE created_at >= datetime('now', '-24 hours')`
const result = (await this.executeQuery({
sql,
isRaw: false,
})) as Record<string, SqlStorageValue>[]
const row = result.length ? result[0] : { count: 0 }

return {
// Size in bytes
databaseSize: this.sql.databaseSize,
// Count of persistent web socket connections
activeConnections: this.connections.keys.length,
// Assuming the `QueryLogPlugin` is in use, count is of the last 24 hours
recentQueries: Number(row.count),
}
}

async fetch(request: Request) {
const url = new URL(request.url)

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { StudioPlugin } from '../plugins/studio'
import { SqlMacrosPlugin } from '../plugins/sql-macros'
import { ChangeDataCapturePlugin } from '../plugins/cdc'
import { QueryLogPlugin } from '../plugins/query-log'
import { StatsPlugin } from '../plugins/stats'

export { StarbaseDBDurableObject } from './do'

Expand Down Expand Up @@ -190,6 +191,7 @@ export default {
}),
new QueryLogPlugin({ ctx }),
cdcPlugin,
new StatsPlugin(),
] satisfies StarbasePlugin[]

const starbase = new StarbaseDB({
Expand Down
4 changes: 4 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class StarbasePluginRegistry {
}
}

public currentPlugins(): string[] {
return this.plugins.map((plugin) => plugin.name)
}

public async beforeQuery(opts: {
sql: string
params?: unknown[]
Expand Down
1 change: 0 additions & 1 deletion worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ interface Env {
STUDIO_PASS: '123456'
ENABLE_ALLOWLIST: 0
ENABLE_RLS: 0
EXTERNAL_DB_TYPE: 'postgresql'
AUTH_ALGORITHM: 'RS256'
AUTH_JWKS_ENDPOINT: ''
DATABASE_DURABLE_OBJECT: DurableObjectNamespace<
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ENABLE_RLS = 0
# External database source details
# This enables Starbase to connect to an external data source
# OUTERBASE_API_KEY = ""
EXTERNAL_DB_TYPE = "postgresql"
# EXTERNAL_DB_TYPE = "postgresql"
# EXTERNAL_DB_HOST = ""
# EXTERNAL_DB_PORT = 0
# EXTERNAL_DB_USER = ""
Expand Down