Skip to content

Commit 80d40e4

Browse files
committed
fix(monitors): handle dropped and re-added tables
1 parent b930b69 commit 80d40e4

3 files changed

Lines changed: 63 additions & 17 deletions

File tree

testgen/template/data_chars/data_chars_update.sql

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,37 @@ WITH new_chars AS (
1717
schema_name,
1818
table_name,
1919
run_date
20+
),
21+
updated_records AS (
22+
UPDATE data_table_chars
23+
SET approx_record_ct = n.approx_record_ct,
24+
record_ct = n.record_ct,
25+
column_ct = n.column_ct,
26+
last_refresh_date = n.run_date,
27+
drop_date = NULL
28+
FROM new_chars n
29+
INNER JOIN data_table_chars d ON (
30+
n.table_groups_id = d.table_groups_id
31+
AND n.schema_name = d.schema_name
32+
AND n.table_name = d.table_name
33+
)
34+
WHERE data_table_chars.table_id = d.table_id
35+
RETURNING data_table_chars.*, d.drop_date as old_drop_date
36+
)
37+
INSERT INTO data_structure_log (
38+
table_groups_id,
39+
table_id,
40+
table_name,
41+
change_date,
42+
change
2043
)
21-
UPDATE data_table_chars
22-
SET approx_record_ct = n.approx_record_ct,
23-
record_ct = n.record_ct,
24-
column_ct = n.column_ct,
25-
last_refresh_date = n.run_date,
26-
drop_date = NULL
27-
FROM new_chars n
28-
INNER JOIN data_table_chars d ON (
29-
n.table_groups_id = d.table_groups_id
30-
AND n.schema_name = d.schema_name
31-
AND n.table_name = d.table_name
32-
)
33-
WHERE data_table_chars.table_id = d.table_id;
44+
SELECT u.table_groups_id,
45+
u.table_id,
46+
u.table_name,
47+
u.last_refresh_date,
48+
'A'
49+
FROM updated_records u
50+
WHERE u.old_drop_date IS NOT NULL;
3451

3552
-- Add new records
3653
WITH new_chars AS (
@@ -170,7 +187,7 @@ update_chars AS (
170187
)
171188
WHERE data_column_chars.table_id = d.table_id
172189
AND data_column_chars.column_name = d.column_name
173-
RETURNING data_column_chars.*, d.db_data_type as old_data_type
190+
RETURNING data_column_chars.*, d.db_data_type as old_data_type, d.drop_date as old_drop_date, n.run_date as run_date
174191
)
175192
INSERT INTO data_structure_log (
176193
table_groups_id,
@@ -193,7 +210,20 @@ SELECT u.table_groups_id,
193210
u.old_data_type,
194211
u.db_data_type
195212
FROM update_chars u
196-
WHERE u.old_data_type <> u.db_data_type;
213+
WHERE u.old_data_type <> u.db_data_type
214+
AND u.old_drop_date IS NULL
215+
UNION ALL
216+
SELECT u.table_groups_id,
217+
u.table_id,
218+
u.column_id,
219+
u.table_name,
220+
u.column_name,
221+
u.run_date,
222+
'A',
223+
NULL,
224+
u.db_data_type
225+
FROM update_chars u
226+
WHERE u.old_drop_date IS NOT NULL;
197227

198228

199229
-- Add new records

testgen/ui/components/frontend/js/pages/table_monitoring_trends.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,27 @@ const TableMonitoringTrend = (props) => {
108108
], []);
109109
const freshnessEvents = (getValue(props.freshness_events) ?? []).map(e => ({ ...e, time: parseDate(e.time) }));
110110
const schemaChangeEvents = (getValue(props.schema_events) ?? []).map(e => ({ ...e, time: parseDate(e.time), window_start: parseDate(e.window_start) }));
111-
const volumeTrendEvents = (getValue(props.volume_events) ?? []).map(e => ({ ...e, time: parseDate(e.time) }));
112111
const schemaChangesMaxValue = schemaChangeEvents.reduce((currentValue, e) => Math.max(currentValue, e.additions, e.deletions), 10);
113112

113+
// Compute dropped periods from schema events to hide volume/metric data between table drop and re-add
114+
const droppedPeriods = [];
115+
let dropStart = null;
116+
const sorted = [...schemaChangeEvents].sort((a, b) => a.time - b.time);
117+
for (const event of sorted) {
118+
if (event.table_change === 'D' && dropStart === null) {
119+
dropStart = event.time;
120+
} else if (event.table_change === 'A' && dropStart !== null) {
121+
droppedPeriods.push({ start: dropStart, end: event.time });
122+
dropStart = null;
123+
}
124+
}
125+
const isInDroppedPeriod = (time) => droppedPeriods.some(p => time >= p.start && time <= p.end);
126+
127+
const volumeTrendEvents = (getValue(props.volume_events) ?? []).map(e => ({ ...e, time: parseDate(e.time) })).filter(e => !isInDroppedPeriod(e.time));
128+
114129
const metricEventGroups = metricEvents.map(group => ({
115130
...group,
116-
events: group.events.map(e => ({ ...e, time: parseDate(e.time) })),
131+
events: group.events.map(e => ({ ...e, time: parseDate(e.time) })).filter(e => !isInDroppedPeriod(e.time)),
117132
}));
118133

119134
const volumes = [

testgen/ui/views/monitors_dashboard.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ def get_monitor_events_for_table(test_suite_id: str, table_name: str) -> dict:
834834
],
835835
"schema_events": [
836836
{
837+
"table_change": signals[0] or None,
837838
"additions": signals[1],
838839
"deletions": signals[2],
839840
"modifications": signals[3],

0 commit comments

Comments
 (0)