Commit

Costs: counts are compared with Cloudflare's from the day g1t started counting, so days before a meter existed are not drift

syntaqxcommitted Parent9aa8de4Browse files
1 file+22−10/1 viewed
+22−1
399399 let cf_cost = sum(&|d| d.cf_cost_micros as f64);
400400 let own_cost = sum(&|d| d.own_cost_micros as f64);
401401 let value = sum(&|d| d.value_micros as f64);
402− let (cf_quantity, own_quantity) = (sum(&|d| d.cf_quantity), sum(&|d| d.own_quantity));
402+ // Counts are compared from the first day g1t counted: before its meter
403+ // was deployed there is only Cloudflare's side. A meter that never
404+ // counted anything is compared over every day, so it still shows.
405+ let first_counted = days.iter().filter(|d| d.own_quantity > 0.0).map(|d| d.day.as_str()).min();
406+ let compared = |d: &&ProductDay| first_counted.is_none_or(|from| d.day.as_str() >= from);
407+ let (cf_quantity, own_quantity) = days.iter().filter(compared).fold((0.0, 0.0), |(cf, own), d| (cf + d.cf_quantity, own + d.own_quantity));
403408 let mut out = Vec::new();
404409 if counted && cf_quantity > 0.0 {
405410 let delta = delta_percent(own_quantity, cf_quantity);
18241829 }
18251830
18261831 #[test]
1832+ fn counts_are_compared_from_the_day_g1t_started_counting() {
1833+ let on = |day: &str, cf: f64, own: f64| ProductDay { day: day.into(), bucket: "git".into(), cf_quantity: cf, own_quantity: own, ..ProductDay::default() };
1834+ // Five days of Cloudflare's count before g1t's meter, then two that match.
1835+ let days = vec![on("2026-10-01", 500.0, 0.0), on("2026-10-05", 300.0, 0.0), on("2026-10-06", 210.0, 231.0), on("2026-10-07", 450.0, 458.0)];
1836+ assert!(drifts("git", &days, 10.0, true, 0).iter().all(|d| d.kind != DriftKind::Count));
1837+ // A real gap on the days both counted still shows.
1838+ let days = vec![on("2026-10-01", 500.0, 0.0), on("2026-10-06", 400.0, 231.0), on("2026-10-07", 600.0, 300.0)];
1839+ let found = drifts("git", &days, 10.0, true, 0);
1840+ let count = found.iter().find(|d| d.kind == DriftKind::Count).unwrap();
1841+ assert_eq!((count.ours, count.cloudflare), (531.0, 1000.0));
1842+ // A meter that never counted is compared over every day.
1843+ let days = vec![on("2026-10-06", 400.0, 0.0)];
1844+ assert!(drifts("git", &days, 10.0, true, 0).iter().any(|d| d.kind == DriftKind::Count));
1845+ }
1846+
1847+ #[test]
18271848 fn what_g1t_gives_away_is_kept_apart_from_what_it_sells() {
18281849 let map = BTreeMap::new();
18291850 // A comped workspace (all of it given), one in its trial (half paid