-
Notifications
You must be signed in to change notification settings - Fork 6
test(data): spike check for collab activity vs per-user trailing baseline (#1321) #1355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SharedQA
wants to merge
6
commits into
constructorfabric:main
Choose a base branch
from
SharedQA:claude/dbt-collab-activity-anomaly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4a2bd2
test(data): spike check for collab activity vs per-user trailing base…
nullpointerks f7886f5
test(data): bound the spike check by recency (perf + stale-baseline) …
nullpointerks cd5ad8d
Merge branch 'main' into claude/dbt-collab-activity-anomaly
SharedQA d7e676a
test(data): isolate spike baseline per source + cap output by collect…
nullpointerks 595e13e
Merge remote-tracking branch 'origin/main' into u-1355
SharedQA e1d4575
Merge branch 'main' into claude/dbt-collab-activity-anomaly
SharedQA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/ingestion/dbt/tests/collaboration/assert_collab_document_counts_no_spike.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| {{ config( | ||
| tags=['data_quality'], | ||
| severity='warn', | ||
| store_failures=true, | ||
| meta={ | ||
| 'title': 'Collab document activity spike vs per-user trailing baseline', | ||
| 'domain': 'collab', | ||
| 'category': 'anomaly', | ||
| 'tier': 'warn', | ||
| 'remediation': 'A recent user-day activity count is far above the recent history for that same user, product and metric (z-score > 5 AND > 10x the trailing mean over the last 28 observed days, with at least 14 days of history and a baseline that is itself recent). Not necessarily wrong — it can be a genuine burst — but a sudden 100x-style jump usually means a feeder double-count, a unit change, or a backfill landing on one date. Inspect the stored rows, compare value against base_mean, and trace the m365 feeder. Thresholds are heuristic and advisory (warn only); tune them once observed against real tenant data.' | ||
| } | ||
| ) }} | ||
| -- Statistical spike guard for collaboration document activity (#1321 follow-up). | ||
| -- Catches "too high" anomalies the non-negative check cannot: a count that is | ||
| -- plausible in isolation but wildly out of line with the same user's own recent | ||
| -- history. Deliberately NOT a fixed upper limit — the baseline is derived per | ||
| -- (tenant, user, product, metric) from each series' own trailing window, so it | ||
| -- adapts to heavy and light users alike instead of one global number. | ||
| -- | ||
| -- Bounded by design, so the cost stays flat as history grows: | ||
| -- * input is capped to the last 120 days (the `recent` CTE). A 28-observed-day | ||
| -- baseline for any reasonably active user fits well inside 120 calendar days, | ||
| -- so the window sort/scan never grows with total history. A user with fewer | ||
| -- than 14 active days in that window simply isn't evaluated (too little | ||
| -- signal), which the prior_n guard below enforces. | ||
| -- * output is capped to rows collected in the last 3 days (collected_at, the | ||
| -- row arrival time -- not activity date), so a scheduled daily run reports | ||
| -- newly-arrived spikes once, and a late backfill landing today for an older | ||
| -- activity date is still caught instead of being silently dropped. | ||
| -- | ||
| -- Method: unpivot the five activity counts, then for each series compute a | ||
| -- trailing baseline over the previous 28 observed days (excluding the current | ||
| -- day). A day is flagged only when ALL of these hold: | ||
| -- * at least 14 prior observations exist -- cold-start guard, no baseline yet | ||
| -- * the baseline has non-zero spread -- avoids divide-by-noise on flat series | ||
| -- * the most recent prior observation is within 35 days -- baseline is not stale | ||
| -- * value > mean + 5 * stddev -- z-score outlier | ||
| -- * value > 10 * mean -- massive relative jump | ||
| -- The window is ROWS-based (observed days, not calendar days), so without the | ||
| -- staleness guard a user returning from a long leave would be compared against a | ||
| -- months-old baseline and could false-fire; `date - prev_date <= 35` prevents | ||
| -- that. Advisory only (severity=warn): it emits a finding and stores the rows, it | ||
| -- never fails the pipeline. Read FINAL so transient ReplacingMergeTree duplicates | ||
| -- can't look like spikes. | ||
| WITH recent AS ( | ||
| SELECT | ||
| tenant_id, | ||
| insight_source_id, | ||
| person_key, | ||
| product, | ||
| data_source, | ||
| date, | ||
| collected_at, | ||
| viewed_or_edited_count, | ||
| synced_count, | ||
| shared_internally_count, | ||
| shared_externally_count, | ||
| visited_page_count | ||
| FROM {{ ref('class_collab_document_activity') }} FINAL | ||
| WHERE date >= today() - 120 | ||
| ), | ||
| unpivoted AS ( | ||
| SELECT | ||
| tenant_id, | ||
| insight_source_id, | ||
| person_key, | ||
| product, | ||
| data_source, | ||
| date, | ||
| collected_at, | ||
| m.1 AS metric, | ||
| m.2 AS value | ||
| FROM recent | ||
| ARRAY JOIN | ||
| [ | ||
| ('viewed_or_edited_count', CAST(viewed_or_edited_count AS Nullable(Float64))), | ||
| ('synced_count', CAST(synced_count AS Nullable(Float64))), | ||
| ('shared_internally_count', CAST(shared_internally_count AS Nullable(Float64))), | ||
| ('shared_externally_count', CAST(shared_externally_count AS Nullable(Float64))), | ||
| ('visited_page_count', CAST(visited_page_count AS Nullable(Float64))) | ||
| ] AS m | ||
| WHERE m.2 IS NOT NULL | ||
| ), | ||
| baselined AS ( | ||
| SELECT | ||
| tenant_id, | ||
| insight_source_id, | ||
| person_key, | ||
| product, | ||
| data_source, | ||
| date, | ||
| collected_at, | ||
| metric, | ||
| value, | ||
| count() OVER w AS prior_n, | ||
| avg(value) OVER w AS base_mean, | ||
| stddevSamp(value) OVER w AS base_sd, | ||
| max(date) OVER w AS prev_date | ||
| FROM unpivoted | ||
| WINDOW w AS ( | ||
| PARTITION BY tenant_id, insight_source_id, person_key, product, data_source, metric | ||
| ORDER BY date | ||
| ROWS BETWEEN 28 PRECEDING AND 1 PRECEDING | ||
| ) | ||
| ) | ||
| SELECT | ||
| tenant_id, | ||
| insight_source_id, | ||
| person_key, | ||
| product, | ||
| data_source, | ||
| date, | ||
| metric, | ||
| value, | ||
| prior_n, | ||
| base_mean, | ||
| base_sd | ||
| FROM baselined | ||
| WHERE prior_n >= 14 | ||
| AND base_sd > 0 | ||
| AND (date - prev_date) <= 35 | ||
| AND value > base_mean + 5 * base_sd | ||
| AND value > base_mean * 10 | ||
| AND collected_at >= today() - 3 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.