| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| import { getDbInstance } from "./core";
|
|
|
| export interface PluginMetricRow {
|
| pluginName: string;
|
| event: string;
|
| calls: number;
|
| errors: number;
|
| totalDurationMs: number;
|
| lastCalledAt: string | null;
|
| }
|
|
|
| function rowToMetric(row: Record<string, unknown>): PluginMetricRow {
|
| return {
|
| pluginName: row.plugin_name as string,
|
| event: row.event as string,
|
| calls: row.calls as number,
|
| errors: row.errors as number,
|
| totalDurationMs: row.total_duration_ms as number,
|
| lastCalledAt: row.last_called_at as string | null,
|
| };
|
| }
|
|
|
| |
| |
|
|
| export function recordPluginMetric(
|
| pluginName: string,
|
| event: string,
|
| durationMs: number,
|
| isError: boolean
|
| ): void {
|
| try {
|
| const db = getDbInstance();
|
| const now = new Date().toISOString();
|
|
|
| db.prepare(
|
| `INSERT INTO plugin_metrics (plugin_name, event, calls, errors, total_duration_ms, last_called_at)
|
| VALUES (?, ?, 1, ?, ?, ?)
|
| ON CONFLICT(plugin_name, event) DO UPDATE SET
|
| calls = calls + 1,
|
| errors = errors + excluded.errors,
|
| total_duration_ms = total_duration_ms + excluded.total_duration_ms,
|
| last_called_at = excluded.last_called_at`
|
| ).run(pluginName, event, isError ? 1 : 0, durationMs, now);
|
| } catch {
|
|
|
| }
|
| }
|
|
|
| |
| |
|
|
| export function getPluginMetrics(pluginName?: string): PluginMetricRow[] {
|
| try {
|
| const db = getDbInstance();
|
| const rows = pluginName
|
| ? db.prepare("SELECT * FROM plugin_metrics WHERE plugin_name = ? ORDER BY event").all(pluginName)
|
| : db.prepare("SELECT * FROM plugin_metrics ORDER BY plugin_name, event").all();
|
| return (rows as Record<string, unknown>[]).map(rowToMetric);
|
| } catch {
|
| return [];
|
| }
|
| }
|
|
|
| |
| |
|
|
| export function clearPluginMetrics(pluginName?: string): number {
|
| const db = getDbInstance();
|
| const result = pluginName
|
| ? db.prepare("DELETE FROM plugin_metrics WHERE plugin_name = ?").run(pluginName)
|
| : db.prepare("DELETE FROM plugin_metrics").run();
|
| return result.changes;
|
| }
|
|
|