-
-
Notifications
You must be signed in to change notification settings - Fork 276
feat: fix known download anomalies with interpolation #1636
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest renaming this file to something like 'chart-data-correction' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /** | ||
| * Bidirectional moving average. Blends a trailing (left-anchored) and leading | ||
| * (right-anchored) average by position so transitions from both fixed endpoints | ||
| * are smooth. | ||
| * First and last points are preserved. | ||
| * | ||
| * @param halfWindow - number of points on each side (0 = disabled) | ||
| */ | ||
| export function movingAverage<T extends { value: number }>(data: T[], halfWindow: number): T[] { | ||
| if (halfWindow <= 0 || data.length < 3) return data | ||
|
|
||
| const n = data.length | ||
|
|
||
| // Trailing average (anchored to start): average of [max(0, i-halfWindow), i] | ||
| const trailing: number[] = Array.from({ length: n }) | ||
| for (let i = 0; i < n; i++) { | ||
| const lo = Math.max(0, i - halfWindow) | ||
| let sum = 0 | ||
| for (let j = lo; j <= i; j++) sum += data[j]!.value | ||
| trailing[i] = sum / (i - lo + 1) | ||
| } | ||
|
|
||
| // Leading average (anchored to end): average of [i, min(n-1, i+halfWindow)] | ||
| const leading: number[] = Array.from({ length: n }) | ||
| for (let i = 0; i < n; i++) { | ||
| const hi = Math.min(n - 1, i + halfWindow) | ||
| let sum = 0 | ||
| for (let j = i; j <= hi; j++) sum += data[j]!.value | ||
| leading[i] = sum / (hi - i + 1) | ||
| } | ||
|
|
||
| // Position-based blend: near start → mostly trailing, near end → mostly leading | ||
| const result = data.map(d => ({ ...d })) | ||
| for (let i = 1; i < n - 1; i++) { | ||
| const t = i / (n - 1) | ||
| result[i]!.value = (1 - t) * trailing[i]! + t * leading[i]! | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| /** | ||
| * Forward-backward exponential smoothing (zero-phase). | ||
| * Smooths without introducing lag — preserves the dynamics/timing of trends. | ||
| * First and last points are preserved. | ||
| * | ||
| * @param tau - time constant (0 = disabled, higher = smoother) | ||
| */ | ||
| export function smoothing<T extends { value: number }>(data: T[], tau: number): T[] { | ||
| if (tau <= 0 || data.length < 3) return data | ||
|
|
||
| const alpha = 1 / (1 + tau) | ||
| const n = data.length | ||
|
|
||
| // Forward pass | ||
| const forward: number[] = Array.from({ length: n }) | ||
| forward[0] = data[0]!.value | ||
| for (let i = 1; i < n; i++) { | ||
| forward[i] = alpha * data[i]!.value + (1 - alpha) * forward[i - 1]! | ||
| } | ||
|
|
||
| // Backward pass | ||
| const backward: number[] = Array.from({ length: n }) | ||
| backward[n - 1] = data[n - 1]!.value | ||
| for (let i = n - 2; i >= 0; i--) { | ||
| backward[i] = alpha * data[i]!.value + (1 - alpha) * backward[i + 1]! | ||
| } | ||
|
|
||
| // Position-based blend: near start → mostly forward, near end → mostly backward | ||
| // This ensures smooth transitions from both fixed endpoints | ||
| const result = data.map(d => ({ ...d })) | ||
| for (let i = 1; i < n - 1; i++) { | ||
| const t = i / (n - 1) | ||
| result[i]!.value = (1 - t) * forward[i]! + t * backward[i]! | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| export interface ChartFilterSettings { | ||
| averageWindow: number | ||
| smoothingTau: number | ||
| } | ||
|
|
||
| /** | ||
| * Applies moving average then smoothing in sequence. | ||
| */ | ||
| export function applyDownloadFilter<T extends { value: number }>( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest renaming this function to something like |
||
| data: T[], | ||
| settings: ChartFilterSettings, | ||
| ): T[] { | ||
| let result = data | ||
| result = movingAverage(result, settings.averageWindow) | ||
| result = smoothing(result, settings.smoothingTau) | ||
| return result | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { DownloadAnomaly } from './download-anomalies' | ||
|
|
||
| export const DOWNLOAD_ANOMALIES: DownloadAnomaly[] = [ | ||
| // vite rogue CI spike | ||
| { | ||
| packageName: 'vite', | ||
| start: { date: '2025-08-04', weeklyDownloads: 33_913_132 }, | ||
| end: { date: '2025-09-08', weeklyDownloads: 38_665_727 }, | ||
| }, | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest renaming this var to something like
correctedDownloads