/** * Dashboard Database Schema * PostgreSQL tables for dashboard metrics and cache history * * Migration file: Create tables for metrics and cache history tracking */ export declare const UP_SQL = "\n-- Metrics History Table\n-- Stores hourly snapshots of dashboard metrics\nCREATE TABLE IF NOT EXISTS metrics_history (\n id SERIAL PRIMARY KEY,\n timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n total_requests BIGINT NOT NULL DEFAULT 0,\n cache_hits BIGINT NOT NULL DEFAULT 0,\n cache_misses BIGINT NOT NULL DEFAULT 0,\n hit_rate FLOAT NOT NULL DEFAULT 0,\n average_latency FLOAT NOT NULL DEFAULT 0,\n tokens_saved BIGINT NOT NULL DEFAULT 0,\n cost_saved FLOAT NOT NULL DEFAULT 0,\n active_agents INT NOT NULL DEFAULT 0,\n total_agents INT NOT NULL DEFAULT 0,\n uptime_ms BIGINT NOT NULL DEFAULT 0,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Agent History Table\n-- Tracks agent status changes and activity over time\nCREATE TABLE IF NOT EXISTS agent_history (\n id SERIAL PRIMARY KEY,\n agent_id VARCHAR(255) NOT NULL,\n agent_name VARCHAR(255) NOT NULL,\n status VARCHAR(50) NOT NULL, -- 'active', 'idle', 'error'\n tasks_completed INT NOT NULL DEFAULT 0,\n error_count INT NOT NULL DEFAULT 0,\n cache_hit_rate FLOAT NOT NULL DEFAULT 0,\n last_seen TIMESTAMP NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE\n);\n\n-- Cache Entries Archive Table\n-- Archives of cached queries and their performance metrics\nCREATE TABLE IF NOT EXISTS cache_entries_archive (\n id SERIAL PRIMARY KEY,\n query_hash VARCHAR(64) NOT NULL UNIQUE,\n query_text TEXT NOT NULL,\n response_hash VARCHAR(64),\n tokens_saved BIGINT NOT NULL DEFAULT 0,\n hit_count INT NOT NULL DEFAULT 0,\n total_tokens INT NOT NULL DEFAULT 0,\n cost_saved FLOAT NOT NULL DEFAULT 0,\n first_cached TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n last_hit TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Event Log Table\n-- Real-time events from the orchestration engine\nCREATE TABLE IF NOT EXISTS event_log (\n id SERIAL PRIMARY KEY,\n event_id VARCHAR(255) UNIQUE NOT NULL,\n event_type VARCHAR(50) NOT NULL, -- 'cache_hit', 'cache_miss', 'agent_join', 'agent_leave', 'error'\n agent_id VARCHAR(255),\n query_hash VARCHAR(64),\n latency_ms INT,\n tokens_saved INT,\n cost_saved FLOAT,\n metadata JSONB,\n timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Indexes for performance\nCREATE INDEX IF NOT EXISTS idx_metrics_history_timestamp ON metrics_history(timestamp DESC);\nCREATE INDEX IF NOT EXISTS idx_metrics_history_created ON metrics_history(created_at DESC);\nCREATE INDEX IF NOT EXISTS idx_agent_history_agent_id ON agent_history(agent_id);\nCREATE INDEX IF NOT EXISTS idx_agent_history_timestamp ON agent_history(created_at DESC);\nCREATE INDEX IF NOT EXISTS idx_cache_entries_query_hash ON cache_entries_archive(query_hash);\nCREATE INDEX IF NOT EXISTS idx_cache_entries_last_hit ON cache_entries_archive(last_hit DESC);\nCREATE INDEX IF NOT EXISTS idx_event_log_timestamp ON event_log(timestamp DESC);\nCREATE INDEX IF NOT EXISTS idx_event_log_type ON event_log(event_type);\nCREATE INDEX IF NOT EXISTS idx_event_log_agent_id ON event_log(agent_id);\n\n-- Retention Policy View\n-- Returns records older than 30 days for cleanup\nCREATE OR REPLACE VIEW stale_records AS\nSELECT \n 'metrics_history' as table_name,\n id,\n created_at\nFROM metrics_history\nWHERE created_at < NOW() - INTERVAL '30 days'\nUNION ALL\nSELECT \n 'agent_history' as table_name,\n id,\n created_at\nFROM agent_history\nWHERE created_at < NOW() - INTERVAL '30 days'\nUNION ALL\nSELECT \n 'cache_entries_archive' as table_name,\n id,\n created_at\nFROM cache_entries_archive\nWHERE created_at < NOW() - INTERVAL '30 days'\nUNION ALL\nSELECT \n 'event_log' as table_name,\n id,\n created_at\nFROM event_log\nWHERE created_at < NOW() - INTERVAL '30 days';\n"; export declare const DOWN_SQL = "\n-- Rollback: Drop all tables and views\nDROP VIEW IF EXISTS stale_records;\nDROP TABLE IF EXISTS event_log;\nDROP TABLE IF EXISTS cache_entries_archive;\nDROP TABLE IF EXISTS agent_history;\nDROP TABLE IF EXISTS metrics_history;\n"; /** * Cleanup function to remove records older than 30 days * Should be run as a cron job (e.g., daily) */ export declare const CLEANUP_SQL = "\n-- Delete stale metrics\nDELETE FROM metrics_history\nWHERE created_at < NOW() - INTERVAL '30 days';\n\n-- Delete stale agent history\nDELETE FROM agent_history\nWHERE created_at < NOW() - INTERVAL '30 days';\n\n-- Delete stale cache entries\nDELETE FROM cache_entries_archive\nWHERE created_at < NOW() - INTERVAL '30 days';\n\n-- Delete stale events\nDELETE FROM event_log\nWHERE created_at < NOW() - INTERVAL '30 days';\n"; /** * Sample seed data for testing */ export declare const SEED_DATA_SQL = "\n-- Insert sample metrics\nINSERT INTO metrics_history (\n total_requests, cache_hits, cache_misses, hit_rate, \n average_latency, tokens_saved, cost_saved, active_agents, total_agents, uptime_ms\n) VALUES\n (1000, 750, 250, 0.75, 45.5, 50000, 120.50, 5, 10, 3600000),\n (2000, 1600, 400, 0.8, 42.3, 85000, 210.75, 8, 10, 7200000),\n (3000, 2550, 450, 0.85, 38.9, 135000, 345.80, 10, 10, 10800000);\n\n-- Insert sample cache entries\nINSERT INTO cache_entries_archive (\n query_hash, query_text, tokens_saved, hit_count, total_tokens, cost_saved\n) VALUES\n ('qh_001', 'What is machine learning?', 4200, 156, 28800, 24.50),\n ('qh_002', 'How to implement LLM caching?', 3150, 98, 30900, 18.90),\n ('qh_003', 'Explain neural networks', 2800, 87, 24360, 16.80);\n\n-- Insert sample events\nINSERT INTO event_log (event_id, event_type, agent_id, query_hash, latency_ms, tokens_saved, cost_saved) VALUES\n ('evt_001', 'cache_hit', 'agent_001', 'qh_001', 25, 4200, 24.50),\n ('evt_002', 'cache_miss', 'agent_002', 'qh_002', 450, 0, 0),\n ('evt_003', 'agent_join', 'agent_003', NULL, NULL, NULL, NULL),\n ('evt_004', 'agent_leave', 'agent_001', NULL, NULL, NULL, NULL);\n"; //# sourceMappingURL=001-dashboard-schema.d.ts.map