Skip to content

Commit ed7f8f2

Browse files
fix: the weekly data anomaly detection was broken for the Svelte anomalies (#1983)
Co-authored-by: Alex Savelyev <91429106+alexdln@users.noreply.github.com>
1 parent bd73ea9 commit ed7f8f2

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

app/utils/download-anomalies.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ function getDateString(point: Record<string, any>, granularity: ChartTimeGranula
2626
}
2727

2828
/**
29-
* For daily/weekly the point date falls strictly between the anomaly bounds.
29+
* For daily the point date falls strictly between the anomaly bounds.
30+
* For weekly the point date is the week start, and the full 7-day range is
31+
* checked so any overlapping week is affected.
3032
* For monthly/yearly the anomaly bounds are truncated to the same resolution
3133
* so that any period overlapping the anomaly is caught (inclusive).
3234
*/
@@ -37,8 +39,15 @@ function isDateAffected(
3739
): boolean {
3840
switch (granularity) {
3941
case 'daily':
40-
case 'weekly':
4142
return date > anomaly.start.date && date < anomaly.end.date
43+
case 'weekly': {
44+
const startWeek = date
45+
const weekStartDate = new Date(`${date}T00:00:00Z`)
46+
const weekEndDate = new Date(weekStartDate)
47+
weekEndDate.setUTCDate(weekEndDate.getUTCDate() + 6)
48+
const endWeek = weekEndDate.toISOString().slice(0, 10)
49+
return startWeek <= anomaly.end.date && endWeek >= anomaly.start.date
50+
}
4251
case 'monthly': {
4352
const startMonth = anomaly.start.date.slice(0, 7) + '-01'
4453
const endMonth = anomaly.end.date.slice(0, 7) + '-01'
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { applyBlocklistCorrection } from '../../../../app/utils/download-anomalies'
3+
import type { WeeklyDataPoint } from '../../../../app/types/chart'
4+
5+
describe('applyBlocklistCorrection', () => {
6+
it('treats overlapping weekly buckets as affected anomalies', () => {
7+
const data: WeeklyDataPoint[] = [
8+
{
9+
value: 100,
10+
weekKey: '2022-11-07_2022-11-13',
11+
weekStart: '2022-11-07',
12+
weekEnd: '2022-11-13',
13+
timestampStart: 0,
14+
timestampEnd: 0,
15+
},
16+
{
17+
value: 999,
18+
weekKey: '2022-11-14_2022-11-20',
19+
weekStart: '2022-11-14',
20+
weekEnd: '2022-11-20',
21+
timestampStart: 0,
22+
timestampEnd: 0,
23+
},
24+
{
25+
value: 999,
26+
weekKey: '2022-11-21_2022-11-27',
27+
weekStart: '2022-11-21',
28+
weekEnd: '2022-11-27',
29+
timestampStart: 0,
30+
timestampEnd: 0,
31+
},
32+
{
33+
value: 999,
34+
weekKey: '2022-11-28_2022-12-04',
35+
weekStart: '2022-11-28',
36+
weekEnd: '2022-12-04',
37+
timestampStart: 0,
38+
timestampEnd: 0,
39+
},
40+
{
41+
value: 200,
42+
weekKey: '2022-12-05_2022-12-11',
43+
weekStart: '2022-12-05',
44+
weekEnd: '2022-12-11',
45+
timestampStart: 0,
46+
timestampEnd: 0,
47+
},
48+
]
49+
50+
expect(
51+
applyBlocklistCorrection({
52+
data,
53+
packageName: 'svelte',
54+
granularity: 'weekly',
55+
}),
56+
).toEqual([
57+
data[0],
58+
{ ...data[1], value: 125, hasAnomaly: true },
59+
{ ...data[2], value: 150, hasAnomaly: true },
60+
{ ...data[3], value: 175, hasAnomaly: true },
61+
data[4],
62+
])
63+
})
64+
})

0 commit comments

Comments
 (0)