Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/lib/analyses.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
27 changes: 24 additions & 3 deletions src/lib/analyses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export const Graphs: Record<AnalysisEnum, { transform: (counts: Counts) => 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,
},
};

Expand All @@ -41,6 +41,27 @@ function buildZeroedMonths(currentDate: string, nextDate: string, size: number)
return zeroedMonths;
}

export function toMonthlyBucketedCounts(counts: Counts) {
return counts.reduce<Counts>((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<Counts>((coalescedCounts, count) => {
const lastCount = coalescedCounts[coalescedCounts.length - 1];
Expand Down