Skip to content

Commit a7bc5df

Browse files
committed
use the new JobRun.internal flag in usage stats
1 parent 1c524c5 commit a7bc5df

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PrismaClient, prisma } from "~/db.server";
2+
import { logger } from "~/services/logger.server";
23

34
export class OrgUsagePresenter {
45
#prismaClient: PrismaClient;
@@ -33,6 +34,7 @@ export class OrgUsagePresenter {
3334
createdAt: {
3435
gte: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
3536
},
37+
internal: false,
3638
},
3739
});
3840

@@ -44,6 +46,7 @@ export class OrgUsagePresenter {
4446
gte: startOfLastMonth,
4547
lt: startOfMonth,
4648
},
49+
internal: false,
4750
},
4851
});
4952

@@ -63,7 +66,7 @@ export class OrgUsagePresenter {
6366
month: string;
6467
count: number;
6568
}[]
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`;
69+
>`SELECT TO_CHAR("createdAt", 'YYYY-MM') as month, COUNT(*) as count FROM "JobRun" WHERE "organizationId" = ${organization.id} AND "createdAt" >= NOW() - INTERVAL '6 months' AND "internal" = FALSE GROUP BY month ORDER BY month ASC`;
6770

6871
const chartData = chartDataRaw.map((obj) => ({
6972
name: obj.month,
@@ -139,11 +142,13 @@ export class OrgUsagePresenter {
139142
},
140143
});
141144

145+
const chartDataDisplay = fillInMissingMonthlyData(chartData, 6);
146+
142147
return {
143148
id: organization.id,
144149
runsCount,
145150
runsCountLastMonth,
146-
chartData: fillInMissingMonthlyData(chartData, 6),
151+
chartData: chartDataDisplay,
147152
totalJobs,
148153
totalJobsLastMonth,
149154
totalIntegrations,
@@ -166,7 +171,7 @@ function fillInMissingMonthlyData(
166171

167172
const startMonth = new Date(
168173
new Date(currentMonth).getFullYear(),
169-
new Date(currentMonth).getMonth() - totalNumberOfMonths,
174+
new Date(currentMonth).getMonth() - (totalNumberOfMonths - 2),
170175
1
171176
)
172177
.toISOString()
@@ -182,17 +187,36 @@ function fillInMissingMonthlyData(
182187
return completeData;
183188
}
184189

190+
// Start month will be like 2023-03 and endMonth will be like 2023-10
191+
// The result should be an array of months between these two months, including the start and end month
192+
// So for example, if startMonth is 2023-03 and endMonth is 2023-10, the result should be:
193+
// ["2023-03", "2023-04", "2023-05", "2023-06", "2023-07", "2023-08", "2023-09", "2023-10"]
185194
function getMonthsBetween(startMonth: string, endMonth: string): string[] {
186-
const startDate = new Date(startMonth);
187-
const endDate = new Date(endMonth);
195+
// Initialize result array
196+
const result: string[] = [];
197+
198+
// Parse the year and month from startMonth and endMonth
199+
let [startYear, startMonthNum] = startMonth.split("-").map(Number);
200+
let [endYear, endMonthNum] = endMonth.split("-").map(Number);
188201

189-
const months = [];
190-
let currentDate = startDate;
202+
// Loop through each month between startMonth and endMonth
203+
for (let year = startYear; year <= endYear; year++) {
204+
let monthStart = year === startYear ? startMonthNum : 1;
205+
let monthEnd = year === endYear ? endMonthNum : 12;
191206

192-
while (currentDate <= endDate) {
193-
months.push(currentDate.toISOString().slice(0, 7));
194-
currentDate = new Date(currentDate.setMonth(currentDate.getMonth() + 1));
207+
for (let month = monthStart; month <= monthEnd; month++) {
208+
// Format the month into a string and add it to the result array
209+
result.push(`${year}-${String(month).padStart(2, "0")}`);
210+
}
195211
}
196212

197-
return months;
213+
return result;
214+
}
215+
216+
function getLastSecondOfMonth(endMonth: string) {
217+
const [year, month] = endMonth.split("-").map(Number);
218+
const nextMonthFirstDay = new Date(year, month, 1);
219+
nextMonthFirstDay.setDate(0);
220+
nextMonthFirstDay.setHours(23, 59, 59);
221+
return nextMonthFirstDay;
198222
}

0 commit comments

Comments
 (0)