From f0f39989f7eb02a8e2ec6469c1ec9cd73dda03b1 Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Sat, 1 Feb 2025 15:26:59 +0100 Subject: [PATCH 1/3] Move over dates to string --- README.md | 1 + .../api/projects/[projectId]/graphs/route.ts | 2 +- src/lib/analyses.spec.ts | 24 +++++++++---------- src/lib/analyses.ts | 6 ++--- src/queries/occurances/countsQueries.ts | 4 ++-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c95eaea..be3be37 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ xdg-open http://localhost:3000 To do: ------ +switch from returning dates to strings in the api, make it iso whatever and turn them into strings. Add jest test / come up with paths for loading state, error state and crit vs non-crit errors in useGet diff --git a/src/app/api/projects/[projectId]/graphs/route.ts b/src/app/api/projects/[projectId]/graphs/route.ts index 551ca74..9ca42af 100644 --- a/src/app/api/projects/[projectId]/graphs/route.ts +++ b/src/app/api/projects/[projectId]/graphs/route.ts @@ -12,7 +12,7 @@ const getParamsValidator = z.object({ }); type FoundOccuranceCounts = { - [key: string]: { count: number; date: Date }[]; + [key: string]: { count: number; date: string }[]; }; const toGraphs = ( diff --git a/src/lib/analyses.spec.ts b/src/lib/analyses.spec.ts index bee6433..5e2c3bb 100644 --- a/src/lib/analyses.spec.ts +++ b/src/lib/analyses.spec.ts @@ -3,8 +3,8 @@ import { toCoalescedCounts } from "./analyses"; describe("toCoalescedCounts", () => { it("adds counts of previous day to the next day in same month", () => { const counts = [ - { date: new Date("2024-01-01"), count: 41 }, - { date: new Date("2024-01-02"), count: 1 }, + { date: "2024-01-01", count: 41 }, + { date: "2024-01-02", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); @@ -14,8 +14,8 @@ describe("toCoalescedCounts", () => { it("does NOT add count of previous day to the next day in new month", () => { const counts = [ - { date: new Date("2024-01-01"), count: 41 }, - { date: new Date("2024-02-01"), count: 1 }, + { date: "2024-01-01", count: 41 }, + { date: "2024-02-01", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); @@ -25,8 +25,8 @@ describe("toCoalescedCounts", () => { it("does NOT reset count of first of month when a month has a value on the first of the month", () => { const counts = [ - { date: new Date("2024-01-01"), count: 41 }, - { date: new Date("2024-03-01"), count: 1 }, + { date: "2024-01-01", count: 41 }, + { date: "2024-03-01", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); expect(coalescedCounts[0].count).toEqual(41); @@ -36,8 +36,8 @@ describe("toCoalescedCounts", () => { it("resets count of first of month when a month is skipped", () => { const counts = [ - { date: new Date("2024-01-01"), count: 41 }, - { date: new Date("2024-03-10"), count: 1 }, + { date: "2024-01-01", count: 41 }, + { date: "2024-03-10", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); expect(coalescedCounts[0].count).toEqual(41); @@ -48,8 +48,8 @@ describe("toCoalescedCounts", () => { it("resets count of first of month when a month", () => { const counts = [ - { date: new Date("2023-12-31"), count: 41 }, - { date: new Date("2024-01-01"), count: 1 }, + { date: "2023-12-31", count: 41 }, + { date: "2024-01-01", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); expect(coalescedCounts[0].count).toEqual(41); @@ -58,8 +58,8 @@ describe("toCoalescedCounts", () => { it("sets count to 0 for multiple missing months", () => { const counts = [ - { date: new Date("2024-01-01"), count: 41 }, - { date: new Date("2024-04-10"), count: 1 }, + { date: "2024-01-01", count: 41 }, + { date: "2024-04-10", count: 1 }, ]; const coalescedCounts = toCoalescedCounts(counts); diff --git a/src/lib/analyses.ts b/src/lib/analyses.ts index a5b48f4..c49635c 100644 --- a/src/lib/analyses.ts +++ b/src/lib/analyses.ts @@ -22,9 +22,9 @@ export const Graphs = { }, }; -type Counts = Array<{ date: Date; count: number }>; +type Counts = Array<{ date: string; count: number }>; -function buildZeroedMonths(currentDate: Date, nextDate: Date, size: number) { +function buildZeroedMonths(currentDate: string, nextDate: string, size: number) { const currentday = day(currentDate); const nextDay = day(nextDate); const zeroedMonths: Counts = []; @@ -34,7 +34,7 @@ function buildZeroedMonths(currentDate: Date, nextDate: Date, size: number) { break; } zeroedMonths.push({ - date: currentday.utc().add(month, "month").startOf("month").toDate(), + date: currentday.utc().add(month, "month").startOf("month").toISOString(), count: 0, }); } diff --git a/src/queries/occurances/countsQueries.ts b/src/queries/occurances/countsQueries.ts index 2a2ca4d..e419c0a 100644 --- a/src/queries/occurances/countsQueries.ts +++ b/src/queries/occurances/countsQueries.ts @@ -8,7 +8,7 @@ async function findCountsById(analysisId: number, timeframe: TimeFrames) { return (await prisma.$queryRaw` SELECT - DATE(occurred_at)::timestamp as date, + DATE(occurred_at)::text as date, SUM(amount)::INT as count, a.id FROM occurances o @@ -16,7 +16,7 @@ async function findCountsById(analysisId: number, timeframe: TimeFrames) { WHERE a.id = ${analysisId} and o.occurred_at > ${moment().subtract(times.span.amount, times.span.frame).toDate()} GROUP BY DATE(occurred_at), a.id ORDER BY date asc - `) as { count: number; date: Date }[]; + `) as { count: number; date: string }[]; } export default { From 233043c4a28d8145d1562022fd35dc8bb99cdd83 Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Sat, 1 Feb 2025 15:51:41 +0100 Subject: [PATCH 2/3] Add concrete type to Graph types --- src/lib/analyses.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/analyses.ts b/src/lib/analyses.ts index c49635c..bc0224d 100644 --- a/src/lib/analyses.ts +++ b/src/lib/analyses.ts @@ -7,7 +7,7 @@ export enum AnalysisEnum { LOCRemoved = "LOCRemoved", } -export const Graphs = { +export const Graphs: Record Counts }> = { [AnalysisEnum.ReleaseCandidates]: { transform: toCoalescedCounts, }, From 1d9a7a5dac0cd363ccfed4fb1cdbc3fc883c3342 Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Sat, 1 Feb 2025 15:55:41 +0100 Subject: [PATCH 3/3] Updated readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index be3be37..c95eaea 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ xdg-open http://localhost:3000 To do: ------ -switch from returning dates to strings in the api, make it iso whatever and turn them into strings. Add jest test / come up with paths for loading state, error state and crit vs non-crit errors in useGet