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 src/app/api/projects/[projectId]/graphs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getParamsValidator = z.object({
});

type FoundOccuranceCounts = {
[key: string]: { count: number; date: Date }[];
[key: string]: { count: number; date: string }[];
};

const toGraphs = (
Expand Down
24 changes: 12 additions & 12 deletions src/lib/analyses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/analyses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum AnalysisEnum {
LOCRemoved = "LOCRemoved",
}

export const Graphs = {
export const Graphs: Record<AnalysisEnum, { transform: (counts: Counts) => Counts }> = {
[AnalysisEnum.ReleaseCandidates]: {
transform: toCoalescedCounts,
},
Expand All @@ -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 = [];
Expand All @@ -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,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/queries/occurances/countsQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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
JOIN analyses a on o.analysis_id = a.id
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 {
Expand Down