Skip to content

Commit 89d0bbe

Browse files
committed
Fill in missing months with 0s
1 parent b989270 commit 89d0bbe

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

apps/webapp/app/presenters/OrgUsagePresenter.server.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class OrgUsagePresenter {
6363
month: string;
6464
count: number;
6565
}[]
66-
>`SELECT TO_CHAR("createdAt", 'YYYY-MM') as month, COUNT(*) as count FROM "JobRun" WHERE "organizationId" = ${organization.id} AND "createdAt" >= NOW() - INTERVAL '12 months' GROUP BY month ORDER BY month ASC`;
66+
>`SELECT TO_CHAR("createdAt", 'YYYY-MM') as month, COUNT(*) as count FROM "JobRun" WHERE "organizationId" = ${organization.id} AND "createdAt" >= NOW() - INTERVAL '6 months' GROUP BY month ORDER BY month ASC`;
6767

6868
const chartData = chartDataRaw.map((obj) => ({
6969
name: obj.month,
@@ -143,7 +143,7 @@ export class OrgUsagePresenter {
143143
id: organization.id,
144144
runsCount,
145145
runsCountLastMonth,
146-
chartData,
146+
chartData: fillInMissingMonthlyData(chartData, 6),
147147
totalJobs,
148148
totalJobsLastMonth,
149149
totalIntegrations,
@@ -153,3 +153,48 @@ export class OrgUsagePresenter {
153153
};
154154
}
155155
}
156+
157+
// This will fill in missing chart data with zeros
158+
// So for example, if data is [{ name: "2021-01", total: 10 }, { name: "2021-03", total: 30 }] and the totalNumberOfMonths is 6
159+
// And the current month is "2021-04", then this function will return:
160+
// [{ name: "2020-11", total: 0 }, { name: "2020-12", total: 0 }, { name: "2021-01", total: 10 }, { name: "2021-02", total: 0 }, { name: "2021-03", total: 30 }, { name: "2021-04", total: 0 }]
161+
function fillInMissingMonthlyData(
162+
data: Array<{ name: string; total: number }>,
163+
totalNumberOfMonths: number
164+
): Array<{ name: string; total: number }> {
165+
const currentMonth = new Date().toISOString().slice(0, 7);
166+
167+
const startMonth = new Date(
168+
new Date(currentMonth).getFullYear(),
169+
new Date(currentMonth).getMonth() - totalNumberOfMonths,
170+
1
171+
)
172+
.toISOString()
173+
.slice(0, 7);
174+
175+
const months = getMonthsBetween(startMonth, currentMonth);
176+
177+
let completeData = months.map((month) => {
178+
let foundData = data.find((d) => d.name === month);
179+
return foundData ? { ...foundData } : { name: month, total: 0 };
180+
});
181+
182+
return completeData;
183+
}
184+
185+
function getMonthsBetween(startMonth: string, endMonth: string): string[] {
186+
const startDate = new Date(startMonth);
187+
const endDate = new Date(endMonth);
188+
189+
const months = [];
190+
let currentDate = startDate;
191+
192+
while (currentDate <= endDate) {
193+
months.push(currentDate.toISOString().slice(0, 7));
194+
currentDate = new Date(currentDate.setMonth(currentDate.getMonth() + 1));
195+
}
196+
197+
months.push(endMonth);
198+
199+
return months;
200+
}

0 commit comments

Comments
 (0)