Skip to content

Commit

Permalink
feat: remove postgres query from workspace stat
Browse files Browse the repository at this point in the history
  • Loading branch information
vklimontovich committed Dec 4, 2023
1 parent c3528fd commit fc74bf7
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions webapps/ee-api/pages/api/report/workspace-stat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getLog, namedParameters, SqlQueryParameters, unrollParams } from "juava";
import { namedParameters, SqlQueryParameters, unrollParams } from "juava";
import { withErrorHandler } from "../../../lib/error-handler";
import { auth } from "../../../lib/auth";
import { clickhouse, pg } from "../../../lib/services";
Expand Down Expand Up @@ -39,28 +39,6 @@ function isoDateTOClickhouse(date: ISODate): string {
return date.replace("T", " ").replace("Z", "").split(".")[0];
}

async function getPostgresPart({ granularity, start, end, workspaceId }: ReportParams): Promise<WorkspaceReportRow[]> {
const sql = `select
obj."workspaceId" as "workspaceId",
date_trunc(:granularity, (m.timestamp::TIMESTAMPTZ)) as period,
sum(m.value) as events
from newjitsuee.bulker_metrics m
left join newjitsu."ConfigurationObjectLink" obj
on obj."id" = m."destinationId"
where m.metric_name = 'bulkerapp_ingest_messages' and m.status = 'success' and m.timestamp <= '2023-07-28T00:00:00Z' and ${
workspaceId ? 'obj."workspaceId" = :workspaceId and' : ""
} date_trunc(:granularity, "timestamp") >= date_trunc(:granularity, :start::timestamp) and
date_trunc(:granularity, "timestamp") <= date_trunc(:granularity, :end::timestamp)
group by period, "workspaceId"
order by period, "workspaceId" desc;;
`;
const params = removeUndefined({ start, end, granularity, workspaceId });
const { query, values } = namedParameters(sql, params);
return await pg.query({ text: query, values }).then(res => {
return res.rows;
});
}

async function getClickhousePart({
granularity,
start,
Expand Down Expand Up @@ -136,11 +114,7 @@ export async function buildWorkspaceReport(
granularity: "day",
workspaceId: string | undefined
): Promise<WorkspaceReportRow[]> {
const [pgRes, chRes] = await Promise.all([
getPostgresPart({ start, end, granularity, workspaceId }),
getClickhousePart({ start, end, granularity, workspaceId }),
]);
return [...pgRes.map(s => ({ ...s, src: "pg" })), ...chRes.map(s => ({ ...s, src: "ch" }))];
return (await getClickhousePart({ start, end, granularity, workspaceId })).map(s => ({ ...s, src: "ch" }));
}

async function extend(reportResult: WorkspaceReportRow[]): Promise<ExtendedWorkspaceReportRow[]> {
Expand Down

1 comment on commit fc74bf7

@vercel
Copy link

@vercel vercel bot commented on fc74bf7 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

new-jitsu-ee-api – ./webapps/ee-api

new-jitsu-ee-api-jitsu.vercel.app
new-jitsu-ee-api-git-newjitsu-jitsu.vercel.app
onetag-ee-api.vercel.app
ee.jitsu.dev

Please sign in to comment.