Skip to content

Clickhouse performance improvements #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 14, 2025
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
12 changes: 10 additions & 2 deletions apps/webapp/app/components/primitives/CopyableText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { SimpleTooltip } from "~/components/primitives/Tooltip";
import { useCopy } from "~/hooks/useCopy";
import { cn } from "~/utils/cn";

export function CopyableText({ value, className }: { value: string; className?: string }) {
export function CopyableText({
value,
copyValue,
className,
}: {
value: string;
copyValue?: string;
className?: string;
}) {
const [isHovered, setIsHovered] = useState(false);
const { copy, copied } = useCopy(value);
const { copy, copied } = useCopy(copyValue ?? value);

return (
<span
Expand Down
21 changes: 18 additions & 3 deletions apps/webapp/app/components/runs/v3/TaskRunsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import {
TaskRunStatusCombo,
} from "./TaskRunStatus";
import { useEnvironment } from "~/hooks/useEnvironment";
import { CopyableText } from "~/components/primitives/CopyableText";
import { ClipboardField } from "~/components/primitives/ClipboardField";

type RunsTableProps = {
total: number;
Expand Down Expand Up @@ -134,7 +136,7 @@ export function TaskRunsTable({
)}
</TableHeaderCell>
)}
<TableHeaderCell alignment="right">Run #</TableHeaderCell>
<TableHeaderCell>ID</TableHeaderCell>
<TableHeaderCell>Task</TableHeaderCell>
<TableHeaderCell>Version</TableHeaderCell>
<TableHeaderCell
Expand Down Expand Up @@ -306,8 +308,21 @@ export function TaskRunsTable({
/>
</TableCell>
)}
<TableCell to={path} alignment="right" isTabbableCell>
{formatNumber(run.number)}
<TableCell to={path} isTabbableCell>
<SimpleTooltip
content={run.friendlyId}
button={
<span className="flex h-6 items-center gap-1">
<CopyableText
value={run.friendlyId.slice(-8)}
copyValue={run.friendlyId}
className="font-mono"
/>
</span>
}
asChild
disableHoverableContent
/>
</TableCell>
<TableCell to={path}>
<span className="flex items-center gap-x-1">
Expand Down
2 changes: 2 additions & 0 deletions apps/webapp/app/presenters/v3/NextRunListPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class NextRunListPresenter {
) {}

public async call(
organizationId: string,
environmentId: string,
{
userId,
Expand Down Expand Up @@ -190,6 +191,7 @@ export class NextRunListPresenter {
});

const { runs, pagination } = await runsRepository.listRuns({
organizationId,
environmentId,
projectId,
tasks,
Expand Down
22 changes: 16 additions & 6 deletions apps/webapp/app/presenters/v3/TaskListPresenter.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
PrismaClientOrTransaction,
RuntimeEnvironmentType,
type PrismaClientOrTransaction,
type RuntimeEnvironmentType,
type TaskTriggerSource,
} from "@trigger.dev/database";
import { $replica } from "~/db.server";
import { clickhouseClient } from "~/services/clickhouseInstance.server";
import {
AverageDurations,
type AverageDurations,
ClickHouseEnvironmentMetricsRepository,
CurrentRunningStats,
DailyTaskActivity,
EnvironmentMetricsRepository,
type CurrentRunningStats,
type DailyTaskActivity,
type EnvironmentMetricsRepository,
PostgrestEnvironmentMetricsRepository,
} from "~/services/environmentMetricsRepository.server";
import { singleton } from "~/utils/singleton";
Expand All @@ -32,9 +32,13 @@ export class TaskListPresenter {
) {}

public async call({
organizationId,
projectId,
environmentId,
environmentType,
}: {
organizationId: string;
projectId: string;
environmentId: string;
environmentType: RuntimeEnvironmentType;
}) {
Expand Down Expand Up @@ -76,18 +80,24 @@ export class TaskListPresenter {
// IMPORTANT: Don't await these, we want to return the promises
// so we can defer the loading of the data
const activity = this.environmentMetricsRepository.getDailyTaskActivity({
organizationId,
projectId,
environmentId,
days: 6, // This actually means 7 days, because we want to show the current day too
tasks: slugs,
});

const runningStats = this.environmentMetricsRepository.getCurrentRunningStats({
organizationId,
projectId,
environmentId,
days: 6,
tasks: slugs,
});

const durations = this.environmentMetricsRepository.getAverageDurations({
organizationId,
projectId,
environmentId,
days: 6,
tasks: slugs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {

try {
const { tasks, activity, runningStats, durations } = await taskListPresenter.call({
organizationId: project.organizationId,
projectId: project.id,
environmentId: environment.id,
environmentType: environment.type,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
}

const presenter = new NextRunListPresenter($replica, clickhouseClient);
const list = presenter.call(environment.id, {
const list = presenter.call(project.organizationId, environment.id, {
userId,
projectId: project.id,
tasks,
Expand Down
24 changes: 24 additions & 0 deletions apps/webapp/app/services/environmentMetricsRepository.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ export type AverageDurations = Record<string, number>;

export interface EnvironmentMetricsRepository {
getDailyTaskActivity(options: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
}): Promise<DailyTaskActivity>;

getCurrentRunningStats(options: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
}): Promise<CurrentRunningStats>;

getAverageDurations(options: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
Expand Down Expand Up @@ -177,10 +183,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
constructor(private readonly options: ClickHouseEnvironmentMetricsRepositoryOptions) {}

public async getDailyTaskActivity({
organizationId,
projectId,
environmentId,
days,
tasks,
}: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
Expand All @@ -190,6 +200,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
}

const [queryError, activity] = await this.options.clickhouse.taskRuns.getTaskActivity({
organizationId,
projectId,
environmentId,
days,
});
Expand All @@ -210,10 +222,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
}

public async getCurrentRunningStats({
organizationId,
projectId,
environmentId,
days,
tasks,
}: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
Expand All @@ -223,6 +239,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
}

const [queryError, stats] = await this.options.clickhouse.taskRuns.getCurrentRunningStats({
organizationId,
projectId,
environmentId,
days,
});
Expand All @@ -242,10 +260,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
}

public async getAverageDurations({
organizationId,
projectId,
environmentId,
days,
tasks,
}: {
organizationId: string;
projectId: string;
environmentId: string;
days: number;
tasks: string[];
Expand All @@ -255,6 +277,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
}

const [queryError, durations] = await this.options.clickhouse.taskRuns.getAverageDurations({
organizationId,
projectId,
environmentId,
days,
});
Expand Down
75 changes: 37 additions & 38 deletions apps/webapp/app/services/runsRepository.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ClickHouse } from "@internal/clickhouse";
import { Tracer } from "@internal/tracing";
import { Logger, LogLevel } from "@trigger.dev/core/logger";
import { TaskRunStatus } from "@trigger.dev/database";
import { PrismaClient } from "~/db.server";
import { type ClickHouse } from "@internal/clickhouse";
import { type Tracer } from "@internal/tracing";
import { type Logger, type LogLevel } from "@trigger.dev/core/logger";
import { type TaskRunStatus } from "@trigger.dev/database";
import { type PrismaClient } from "~/db.server";

export type RunsRepositoryOptions = {
clickhouse: ClickHouse;
Expand All @@ -13,6 +13,7 @@ export type RunsRepositoryOptions = {
};

export type ListRunsOptions = {
organizationId: string;
projectId: string;
environmentId: string;
//filters
Expand Down Expand Up @@ -43,11 +44,14 @@ export class RunsRepository {
async listRuns(options: ListRunsOptions) {
const queryBuilder = this.options.clickhouse.taskRuns.queryBuilder();
queryBuilder
.where("environment_id = {environmentId: String}", {
environmentId: options.environmentId,
.where("organization_id = {organizationId: String}", {
organizationId: options.organizationId,
})
.where("project_id = {projectId: String}", {
projectId: options.projectId,
})
.where("environment_id = {environmentId: String}", {
environmentId: options.environmentId,
});

if (options.tasks && options.tasks.length > 0) {
Expand Down Expand Up @@ -115,17 +119,17 @@ export class RunsRepository {
if (options.page.direction === "forward") {
queryBuilder
.where("run_id < {runId: String}", { runId: options.page.cursor })
.orderBy("run_id DESC")
.orderBy("created_at DESC, run_id DESC")
.limit(options.page.size + 1);
} else {
queryBuilder
.where("run_id > {runId: String}", { runId: options.page.cursor })
.orderBy("run_id DESC")
.orderBy("created_at ASC, run_id ASC")
.limit(options.page.size + 1);
}
} else {
// Initial page - no cursor provided
queryBuilder.orderBy("run_id DESC").limit(options.page.size + 1);
queryBuilder.orderBy("created_at DESC, run_id DESC").limit(options.page.size + 1);
}

const [queryError, result] = await queryBuilder.execute();
Expand All @@ -143,38 +147,33 @@ export class RunsRepository {
let previousCursor: string | null = null;

//get cursors for next and previous pages
if (options.page.cursor) {
switch (options.page.direction) {
case "forward":
previousCursor = runIds.at(0) ?? null;
if (hasMore) {
// The next cursor should be the last run ID from this page
nextCursor = runIds[options.page.size - 1];
}
break;
case "backward":
// No need to reverse since we're using DESC ordering consistently
if (hasMore) {
previousCursor = runIds[options.page.size - 1];
}
nextCursor = runIds.at(0) ?? null;
break;
default:
// This shouldn't happen if cursor is provided, but handle it
if (hasMore) {
nextCursor = runIds[options.page.size - 1];
}
break;
const direction = options.page.direction ?? "forward";
switch (direction) {
case "forward": {
previousCursor = options.page.cursor ? runIds.at(0) ?? null : null;
if (hasMore) {
// The next cursor should be the last run ID from this page
nextCursor = runIds[options.page.size - 1];
}
break;
}
} else {
// Initial page - no cursor
if (hasMore) {
// The next cursor should be the last run ID from this page
nextCursor = runIds[options.page.size - 1];
case "backward": {
const reversedRunIds = [...runIds].reverse();
if (hasMore) {
previousCursor = reversedRunIds.at(1) ?? null;
nextCursor = reversedRunIds.at(options.page.size) ?? null;
} else {
nextCursor = reversedRunIds.at(options.page.size - 1) ?? null;
}

break;
}
}

const runIdsToReturn = hasMore ? runIds.slice(0, -1) : runIds;
const runIdsToReturn =
options.page.direction === "backward" && hasMore
? runIds.slice(1, options.page.size + 1)
: runIds.slice(0, options.page.size);

const runs = await this.options.prisma.taskRun.findMany({
where: {
Expand Down
Loading