Skip to content

Commit 06a05dc

Browse files
committed
Switch to read replica: OrgUsagePresenter
1 parent b5bce44 commit 06a05dc

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { estimate } from "@trigger.dev/billing";
2-
import { sqlDatabaseSchema, PrismaClient, prisma } from "~/db.server";
2+
import { sqlDatabaseSchema } from "~/db.server";
33
import { featuresForRequest } from "~/features.server";
44
import { BillingService } from "~/services/billing.server";
5+
import { BasePresenter } from "./v3/basePresenter.server";
56

6-
export class OrgUsagePresenter {
7-
#prismaClient: PrismaClient;
8-
9-
constructor(prismaClient: PrismaClient = prisma) {
10-
this.#prismaClient = prismaClient;
11-
}
12-
7+
export class OrgUsagePresenter extends BasePresenter {
138
public async call({ userId, slug, request }: { userId: string; slug: string; request: Request }) {
14-
const organization = await this.#prismaClient.organization.findFirst({
9+
const organization = await this._replica.organization.findFirst({
1510
where: {
1611
slug,
1712
members: {
@@ -27,7 +22,7 @@ export class OrgUsagePresenter {
2722
}
2823

2924
// Get count of runs since the start of the current month
30-
const runsCount = await this.#prismaClient.jobRun.count({
25+
const runsCount = await this._replica.jobRun.count({
3126
where: {
3227
organizationId: organization.id,
3328
createdAt: {
@@ -48,7 +43,7 @@ export class OrgUsagePresenter {
4843
// ]
4944
// This will be used to generate the chart on the usage page
5045
// Use prisma queryRaw for this since prisma doesn't support grouping by month
51-
const monthlyRunsDataRaw = await this.#prismaClient.$queryRaw<
46+
const monthlyRunsDataRaw = await this._replica.$queryRaw<
5247
{
5348
month: string;
5449
count: number;
@@ -64,7 +59,7 @@ export class OrgUsagePresenter {
6459
const monthlyRunsDataDisplay = fillInMissingRunMonthlyData(monthlyRunsData, 6);
6560

6661
// Max concurrency each day over past 30 days
67-
const concurrencyChartRawData = await this.#prismaClient.$queryRaw<
62+
const concurrencyChartRawData = await this._replica.$queryRaw<
6863
{ day: Date; max_concurrent_runs: BigInt }[]
6964
>`
7065
WITH time_boundaries AS (
@@ -115,7 +110,7 @@ export class OrgUsagePresenter {
115110
concurrencyChartRawData
116111
);
117112

118-
const dailyRunsRawData = await this.#prismaClient.$queryRaw<
113+
const dailyRunsRawData = await this._replica.$queryRaw<
119114
{ day: Date; runs: BigInt }[]
120115
>`SELECT date_trunc('day', "createdAt") as day, COUNT(*) as runs FROM ${sqlDatabaseSchema}."JobRun" WHERE "organizationId" = ${organization.id} AND "createdAt" >= NOW() - INTERVAL '30 days' AND "internal" = FALSE GROUP BY day`;
121116

0 commit comments

Comments
 (0)