Skip to content

The only tool you need to know what is happening and how to fix it.

testing metrics sqls

dduxacf05b7f2a73
diff --git a/testing/test_metric_data.sql b/testing/test_metric_data.sql
new file mode 100644
index 00000000..5bddd20b
--- /dev/null
+++ b/testing/test_metric_data.sql
@@ -0,0 +1,111 @@
+-- Test metric data for verifying widget types and aggregation methods
+-- Run: clickhouse-client --multiquery < backend/test_metric_data.sql
+-- Then visit /metrics with time range "3h" to see the data
+--
+-- Project ID: 5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e
+-- Duration: 3 hours, 30-second intervals = 360 points per metric
+
+USE traceway;
+
+-- 1. test.sine — Sine wave, 3 full cycles, range 10–90
+-- Purpose: Line chart verification (smooth oscillation)
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.sine' AS name,
+ 50 + 40 * sin(2 * pi() * 3 * number / 360) AS value,
+ map('source', 'test') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- 2. test.ramp — Linear increase, range 0–100
+-- Purpose: Area chart verification (triangular fill, steady climb)
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.ramp' AS name,
+ 100 * number / 359 AS value,
+ map('source', 'test') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- 3. test.stairs — 6 flat steps of 30 min each: 20→40→60→80→60→40
+-- Purpose: Bar chart comparison, step rendering
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.stairs' AS name,
+ multiIf(
+ step = 0, 20,
+ step = 1, 40,
+ step = 2, 60,
+ step = 3, 80,
+ step = 4, 60,
+ 40
+ ) AS value,
+ map('source', 'test') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM (
+ SELECT number, intDiv(number, 60) AS step
+ FROM numbers(360)
+);
+
+-- 4. test.spikes — Baseline 10, spike to 90 every ~30 min (every 60th point)
+-- Purpose: Aggregation test (avg≈15, min=10, max=90)
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.spikes' AS name,
+ if(number % 60 = 0, 90, 10) AS value,
+ map('source', 'test') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- 5. test.multi_server — 3 sine waves with different phases (server=web-1/web-2/web-3)
+-- Purpose: GroupBy test, should render 3 distinct colored lines
+
+-- web-1: phase offset 0
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.multi_server' AS name,
+ 50 + 40 * sin(2 * pi() * 2 * number / 360) AS value,
+ map('source', 'test', 'server', 'web-1') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- web-2: phase offset 2π/3 (120°)
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.multi_server' AS name,
+ 50 + 40 * sin(2 * pi() * 2 * number / 360 + 2 * pi() / 3) AS value,
+ map('source', 'test', 'server', 'web-2') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- web-3: phase offset 4π/3 (240°)
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.multi_server' AS name,
+ 50 + 40 * sin(2 * pi() * 2 * number / 360 + 4 * pi() / 3) AS value,
+ map('source', 'test', 'server', 'web-3') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- 6. test.constant — Flat line at 42
+-- Purpose: Single value shows "42", table shows all aggregations = 42
+INSERT INTO metric_points (project_id, name, value, tags, recorded_at)
+SELECT
+ '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e' AS project_id,
+ 'test.constant' AS name,
+ 42 AS value,
+ map('source', 'test') AS tags,
+ now() - toIntervalSecond((360 - number) * 30) AS recorded_at
+FROM numbers(360);
+
+-- Total: 8 INSERT statements, 2880 rows (360 × 8)
+--
+-- Cleanup (run when done testing):
+-- ALTER TABLE metric_points DELETE WHERE tags['source'] = 'test' AND project_id = '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e';
diff --git a/testing/test_metric_widgets.sql b/testing/test_metric_widgets.sql
new file mode 100644
index 00000000..d1a41222
--- /dev/null
+++ b/testing/test_metric_widgets.sql
@@ -0,0 +1,66 @@
+-- PostgreSQL widget groups + widgets for the test metric data
+-- Run: psql -d traceway -f backend/test_metric_widgets.sql
+-- Requires: test_metric_data.sql loaded into ClickHouse first
+--
+-- Project ID: 5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e
+
+-- Step 1: Register test metrics
+INSERT INTO metric_registry (project_id, name, metric_type, unit, description)
+VALUES
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.sine', 'gauge', '', 'Sine wave, 3 cycles, range 10-90'),
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.ramp', 'gauge', '', 'Linear ramp 0-100'),
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.stairs', 'gauge', '', '6-step staircase'),
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.spikes', 'gauge', '', 'Baseline 10, spikes to 90'),
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.multi_server', 'gauge', '', '3 sine waves with server tag'),
+ ('5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e', 'test.constant', 'gauge', '', 'Flat line at 42')
+ON CONFLICT (project_id, name) DO NOTHING;
+
+-- Step 2: Create widget groups and widgets
+DO $$
+DECLARE
+ pid UUID := '5f6ca55f-a6b6-417e-b941-ee78d5fd5b6e';
+ g1_id INT;
+ g2_id INT;
+ g3_id INT;
+BEGIN
+ -- Group 1: Type Tests
+ INSERT INTO widget_groups (project_id, name, description, is_default, created_by)
+ VALUES (pid, 'Type Tests', 'One widget per chart type', false, NULL)
+ RETURNING id INTO g1_id;
+
+ INSERT INTO widget_group_widgets (widget_group_id, title, widget_type, config, position) VALUES
+ (g1_id, 'Sine Wave', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.sine", "aggregation": "avg"}]}', 0),
+ (g1_id, 'Ramp', 'area_chart',
+ '{"sources": [{"type": "metric", "name": "test.ramp", "aggregation": "avg"}]}', 1),
+ (g1_id, 'All Metrics', 'bar_chart',
+ '{"sources": [{"type": "metric", "name": "test.sine", "aggregation": "avg"}, {"type": "metric", "name": "test.ramp", "aggregation": "avg"}, {"type": "metric", "name": "test.stairs", "aggregation": "avg"}, {"type": "metric", "name": "test.constant", "aggregation": "avg"}]}', 2),
+ (g1_id, 'Constant', 'single_value',
+ '{"sources": [{"type": "metric", "name": "test.constant", "aggregation": "avg"}]}', 3),
+ (g1_id, 'Spikes', 'table',
+ '{"sources": [{"type": "metric", "name": "test.spikes", "aggregation": "avg"}]}', 4);
+
+ -- Group 2: Aggregations
+ INSERT INTO widget_groups (project_id, name, description, is_default, created_by)
+ VALUES (pid, 'Aggregations', 'Same metric with different aggregation methods', false, NULL)
+ RETURNING id INTO g2_id;
+
+ INSERT INTO widget_group_widgets (widget_group_id, title, widget_type, config, position) VALUES
+ (g2_id, 'Spikes (avg)', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.spikes", "aggregation": "avg"}]}', 0),
+ (g2_id, 'Spikes (max)', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.spikes", "aggregation": "max"}]}', 1),
+ (g2_id, 'Spikes (min)', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.spikes", "aggregation": "min"}]}', 2),
+ (g2_id, 'Spikes (count)', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.spikes", "aggregation": "count"}]}', 3);
+
+ -- Group 3: Multi-Series
+ INSERT INTO widget_groups (project_id, name, description, is_default, created_by)
+ VALUES (pid, 'Multi-Series', 'GroupBy tag to split into multiple series', false, NULL)
+ RETURNING id INTO g3_id;
+
+ INSERT INTO widget_group_widgets (widget_group_id, title, widget_type, config, position) VALUES
+ (g3_id, 'By Server', 'line_chart',
+ '{"sources": [{"type": "metric", "name": "test.multi_server", "aggregation": "avg", "groupBy": "server"}]}', 0);
+END $$;