Skip to content

Commit

Permalink
bug: Move DB table stats feature behind config flag (temp workaround …
Browse files Browse the repository at this point in the history
…for #158) (#234)
  • Loading branch information
maxmilton authored Jun 4, 2022
1 parent 3c53289 commit 49ba162
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
15 changes: 12 additions & 3 deletions packages/trackx-api/src/routes/dash/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AppError,
config,
humanFileSize,
humanizeTime,
logger,
sessions,
Status,
Expand Down Expand Up @@ -280,8 +281,7 @@ export const get: Middleware = async (req, res, next) => {
throw new AppError('Unexpected param', Status.BAD_REQUEST);
}

const [tableSizes, dbFileSize, dbWalFileSize] = await Promise.all([
getTableSizes(),
const [dbFileSize, dbWalFileSize] = await Promise.all([
fs.promises.stat(config.DB_PATH).then(humanizeSize),
fs.promises.stat(`${config.DB_PATH}-wal`).then(humanizeSize),
]);
Expand All @@ -291,7 +291,16 @@ export const get: Middleware = async (req, res, next) => {
data.api_uptime = Math.floor(process.uptime());
data.db_size = dbFileSize;
data.dbwal_size = dbWalFileSize;
data.db_tables = tableSizes;
// data.db_tables = tableSizes;

// FIXME: Generating DB table stats is extremely slow on systems with slow disks
// ↳ https://github.com/maxmilton/trackx/issues/158
if (config.ENABLE_DB_TABLE_STATS) {
const t0 = performance.now();
data.db_tables = await getTableSizes();
const t1 = performance.now();
logger.info(`Generated DB table stats in ${humanizeTime(t1 - t0)}`);
}

send(res, Status.OK, data);
} catch (error) {
Expand Down
12 changes: 11 additions & 1 deletion packages/trackx-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ export interface TrackXAPIConfig {
* token expires, the user must login again.
*/
readonly SESSION_TTL: number;

/**
* Show database table size statistics on the dash `/stats` page.
*
* @remarks Currently has known performance issues. Enabling this feature may
* be useful for debugging and observability but should be disabled in
* production to prevent a potential denial-of-service attack vector.
*/
readonly ENABLE_DB_TABLE_STATS?: boolean;
}

export type ReqBodyData = Record<string, unknown>;
Expand Down Expand Up @@ -325,7 +334,8 @@ export interface Stats {
dash_session_c: number;
db_size: string;
dbwal_size: string;
db_tables: StatsDBTableInfo[];
// Optional because it's behind the config flag "ENABLE_DB_TABLE_STATS"
db_tables?: StatsDBTableInfo[];
}

export interface Logs {
Expand Down
20 changes: 20 additions & 0 deletions packages/trackx-api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ export function generateSalt(rounds: number): string {
.slice(0, rounds);
}

export function humanizeTime(ms: number): string {
const periods = {
d: Math.floor(ms / (1000 * 60 * 60 * 24)),
h: Math.floor((ms % (1000 * 60 * 60)) / 24),
m: Math.floor((ms % (1000 * 60)) / 60),
s: Math.floor((ms % 1000) / 60),
ms: Math.floor(ms % 1000),
};
const keep = [];
let key: keyof typeof periods;

for (key in periods) {
if (periods[key] > 0) {
keep.push(`${periods[key]}${key}`);
}
}

return keep.join(' ');
}

export function humanFileSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
let b = bytes;
Expand Down
2 changes: 2 additions & 0 deletions packages/trackx-api/trackx.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ module.exports = {
NET_TIMEOUT: 30_000, // 30 seconds
SCHEDULED_JOB_INTERVAL: 21_600_000, // 6 hours; 6h * 60m * 60s * 1000ms
SESSION_TTL: 2_400_000, // 40 minutes; 40m * 60s * 1000ms

ENABLE_DB_TABLE_STATS: true,
};
2 changes: 2 additions & 0 deletions packages/trackx-api/trackx.config.js.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ module.exports = {
NET_TIMEOUT: 30000, // 30 seconds
SCHEDULED_JOB_INTERVAL: 21600000, // 6 hours
SESSION_TTL: 2400000, // 40 minutes

ENABLE_DB_TABLE_STATS: false,
};
7 changes: 5 additions & 2 deletions packages/trackx-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const CONFIG_SCHEMA = [
['NET_TIMEOUT', ['Number']],
['SCHEDULED_JOB_INTERVAL', ['Number']],
['SESSION_TTL', ['Number']],

['ENABLE_DB_TABLE_STATS', ['Boolean', 'Undefined']],
] as const;
const configExpectedKeys: string[] = CONFIG_SCHEMA.map((item) => item[0]);

Expand Down Expand Up @@ -84,8 +86,9 @@ function validateConfig(
}

for (const [key, types] of CONFIG_SCHEMA) {
if (!(key in rawConfig)) {
errors.push(new ReferenceError(`Config missing "${key}" key`));
// @ts-expect-error - FIXME: "types" type
if (!(key in rawConfig) && !types.includes('Undefined')) {
errors.push(new ReferenceError(`Config missing required "${key}" key`));
}

const valueType = toStr
Expand Down
38 changes: 22 additions & 16 deletions packages/trackx-dash/src/pages/stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,34 @@ const StatsPage: Component = () => {
<div>
<h2>Database</h2>

<p>
<p class="wsn">
{data.db_size}
<span class="muted"> + </span>
{data.dbwal_size}
<span class="muted"> (wal)</span>
</p>

<h3>Tables</h3>

<div class="table-wrapper">
<table class="table wi tr tnum">
<For each={data.db_tables} fallback="No data">
{([name, size, percent]) => (
<tr>
<td class="tl break" textContent={name} />
<td class="wsn" textContent={size} />
<td textContent={percent} />
</tr>
)}
</For>
</table>
</div>
{/* FIXME: Generating DB table stats is extremely slow on systems
with slow disks -- https://github.com/maxmilton/trackx/issues/158 */}
{data.db_tables && (
<>
<h3>Tables</h3>

<div class="table-wrapper">
<table class="table wi tr tnum">
<For each={data.db_tables} fallback="No data">
{([name, size, percent]) => (
<tr>
<td class="tl break" textContent={name} />
<td class="wsn" textContent={size} />
<td textContent={percent} />
</tr>
)}
</For>
</table>
</div>
</>
)}
</div>
</div>
)}
Expand Down

0 comments on commit 49ba162

Please sign in to comment.