From d23dbbf22d49d0f91341fe863850c1f4d26fa6c8 Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Mon, 3 Feb 2025 09:42:41 +0100 Subject: [PATCH] Bucketize some graphs --- README.md | 2 +- src/lib/analyses.spec.ts | 23 ++++++++++++++++++++++- src/lib/analyses.ts | 27 ++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c95eaea..7b6bff1 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ To do: ------ Add jest test / come up with paths for loading state, error state and crit vs non-crit errors in useGet - +Add lOC per language over time I want to decide from the creation of the graph data whether to coalesce the data or not I want to have the axes on the graphs on the front-end be filled from some lib data structure I want to decide what type of graph to use based on the analysis type. Decide on colors to use based on the analysis type too diff --git a/src/lib/analyses.spec.ts b/src/lib/analyses.spec.ts index 5e2c3bb..419a764 100644 --- a/src/lib/analyses.spec.ts +++ b/src/lib/analyses.spec.ts @@ -1,4 +1,25 @@ -import { toCoalescedCounts } from "./analyses"; +import { toCoalescedCounts, toMonthlyBucketedCounts } from "./analyses"; + +describe("toMonthlyBucketedCounts", () => { + it("adds counts for a given month", () => { + const counts = [ + { date: "2024-01-01", count: 41 }, + { date: "2024-01-02", count: 1 }, + ]; + const monthlyCounts = toMonthlyBucketedCounts(counts); + expect(monthlyCounts[0].count).toEqual(42); + }); + + it("splits counts by month", () => { + const counts = [ + { date: "2024-01-01", count: 41 }, + { date: "2024-02-02", count: 1 }, + ]; + const monthlyCounts = toMonthlyBucketedCounts(counts); + expect(monthlyCounts[0].count).toEqual(41); + expect(monthlyCounts[1].count).toEqual(1); + }); +}); describe("toCoalescedCounts", () => { it("adds counts of previous day to the next day in same month", () => { diff --git a/src/lib/analyses.ts b/src/lib/analyses.ts index bc0224d..9fc950d 100644 --- a/src/lib/analyses.ts +++ b/src/lib/analyses.ts @@ -12,13 +12,13 @@ export const Graphs: Record Count transform: toCoalescedCounts, }, [AnalysisEnum.LOCChanged]: { - transform: (counts: Counts) => counts, + transform: toMonthlyBucketedCounts, }, [AnalysisEnum.LOCAdded]: { - transform: (counts: Counts) => counts, + transform: toMonthlyBucketedCounts, }, [AnalysisEnum.LOCRemoved]: { - transform: (counts: Counts) => counts, + transform: toMonthlyBucketedCounts, }, }; @@ -41,6 +41,27 @@ function buildZeroedMonths(currentDate: string, nextDate: string, size: number) return zeroedMonths; } +export function toMonthlyBucketedCounts(counts: Counts) { + return counts.reduce((bucketedCounts, count) => { + count.date = day(count.date).startOf("month").format("YYYY-MM"); + + const [theBucketedCounts, lastCount] = [ + bucketedCounts.slice(0, bucketedCounts.length - 1), + bucketedCounts.slice(bucketedCounts.length - 1)[0], + ]; + + if (lastCount === undefined) { + return [count]; + } + lastCount.date = day(lastCount.date).startOf("month").format("YYYY-MM"); + + if (lastCount.date === count.date) { + return [...theBucketedCounts, { ...count, count: count.count + lastCount.count }]; + } + return [...theBucketedCounts, lastCount, count]; + }, []); +} + export function toCoalescedCounts(counts: Counts) { return counts.reduce((coalescedCounts, count) => { const lastCount = coalescedCounts[coalescedCounts.length - 1];