Skip to content

Commit

Permalink
fix serverinfo querying database every second
Browse files Browse the repository at this point in the history
  • Loading branch information
wsrvn committed Jan 20, 2024
1 parent 1cb40e9 commit 128ae47
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 2 additions & 5 deletions plugins/ui/dynamic_components/ServerInfo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class ServerInfoWindow extends PopupWindow {
serverMapCount, serverVisitorsCount]
}

private async getHostInfo(): Promise<string[]> {
private getHostInfo(): string[] {
// Host system information
const osUptime: string = tm.utils.getVerboseTime(~~uptime() * 1000) // Seconds
const osArch: string = arch()
Expand All @@ -62,12 +62,9 @@ export default class ServerInfoWindow extends PopupWindow {
// Node information
const nodeVersion: string = version
const nodeRAMUsage: string = String(~~(memoryUsage().heapTotal / (1024 ** 2)) + ` MB`)
// Postgres information
const postgresVersion: string = String(`v` + (await tm.db.query(`select version();`) as any)[0].version).split(` `)[1]
const postgresDBSize: string = String((await tm.db.query(`select pg_size_pretty(pg_database_size('${process.env.DB_NAME}'));`) as any)[0].pg_size_pretty)
return [osUptime, osArch, osCPU, osCPULoad, osRAM, osKernel,
trakmanVersion, trakmanUptime, nodeVersion, nodeRAMUsage,
postgresVersion, postgresDBSize]
tm.db.dbVersion, tm.db.dbSize]
}

protected onOpen(info: tm.ManialinkClickInfo): void {
Expand Down
6 changes: 5 additions & 1 deletion src/Trakman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ namespace trakman {
}
return res.rows
}
}
},

get dbSize() { return Database.dbSize },

get dbVersion() { return Database.dbVersion },

}

Expand Down
4 changes: 4 additions & 0 deletions src/database/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export class Database {
port: Number(process.env.DB_PORT)
})
private client: postgres.Pool | postgres.PoolClient = Database.pool
static dbVersion: string
static dbSize: string

static async initialize(): Promise<void> {
for (const e of createQueries) {
await this.pool.query(e).catch(async (err: Error) => {
await Logger.fatal(`Database create query failed.`, `Error: ${err.message}`, err.stack, `Query:`, e)
})
}
this.dbVersion = String((await this.pool.query(`select version();`) as any)?.rows[0]?.version?.split(` `, 2)[1])
this.dbSize = String((await this.pool.query(`select pg_size_pretty(pg_database_size('${process.env.DB_NAME}'));`) as any)?.rows[0]?.pg_size_pretty)
}

async enableClient(): Promise<void> {
Expand Down

0 comments on commit 128ae47

Please sign in to comment.