Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 1 addition & 5 deletions apps/webapp/app/components/runs/v3/ScheduleFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export const ScheduleListFilters = z.object({
.string()
.optional()
.transform((value) => (value ? value.split(",") : undefined)),
environments: z
.string()
.optional()
.transform((value) => (value ? value.split(",") : undefined)),
type: z.union([z.literal("declarative"), z.literal("imperative")]).optional(),
search: z.string().optional(),
});
Expand All @@ -44,7 +40,7 @@ export function ScheduleFilters({ possibleTasks }: ScheduleFiltersProps) {
const navigate = useNavigate();
const location = useOptimisticLocation();
const searchParams = new URLSearchParams(location.search);
const { environments, tasks, page, search, type } = ScheduleListFilters.parse(
const { tasks, page, search, type } = ScheduleListFilters.parse(
Object.fromEntries(searchParams.entries())
);

Expand Down
58 changes: 41 additions & 17 deletions apps/webapp/app/presenters/v3/ScheduleListPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { getLimit } from "~/services/platform.v3.server";
import { CheckScheduleService } from "~/v3/services/checkSchedule.server";
import { calculateNextScheduledTimestamp } from "~/v3/utils/calculateNextSchedule.server";
import { BasePresenter } from "./basePresenter.server";
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
import { ServiceValidationError } from "~/v3/services/baseService.server";

type ScheduleListOptions = {
projectId: string;
environmentId: string;
userId?: string;
pageSize?: number;
} & ScheduleListFilters;
Expand Down Expand Up @@ -42,8 +45,8 @@ export class ScheduleListPresenter extends BasePresenter {
public async call({
userId,
projectId,
environmentId,
tasks,
environments,
search,
page,
type,
Expand Down Expand Up @@ -84,16 +87,45 @@ export class ScheduleListPresenter extends BasePresenter {
},
});

const environment = project.environments.find((env) => env.id === environmentId);
if (!environment) {
throw new ServiceValidationError("No matching environment for project", 404);
}

const schedulesCount = await CheckScheduleService.getUsedSchedulesCount({
prisma: this._replica,
environments: project.environments,
});

const limit = await getLimit(project.organizationId, "schedules", 100_000_000);

//get the latest BackgroundWorker
const latestWorker = await findCurrentWorkerFromEnvironment(environment, this._replica);
if (!latestWorker) {
return {
currentPage: 1,
totalPages: 1,
totalCount: 0,
schedules: [],
possibleTasks: [],
hasFilters,
limits: {
used: schedulesCount,
limit,
},
filters: {
tasks,
search,
},
};
}

//get all possible scheduled tasks
const possibleTasks = await this._replica.backgroundWorkerTask.findMany({
distinct: ["slug"],
where: {
workerId: latestWorker.id,
projectId: project.id,
runtimeEnvironmentId: environmentId,
triggerSource: "SCHEDULED",
},
});
Expand All @@ -107,7 +139,7 @@ export class ScheduleListPresenter extends BasePresenter {
taskIdentifier: tasks ? { in: tasks } : undefined,
instances: {
some: {
environmentId: environments ? { in: environments } : undefined,
environmentId,
},
},
type: filterType,
Expand Down Expand Up @@ -168,13 +200,11 @@ export class ScheduleListPresenter extends BasePresenter {
where: {
projectId: project.id,
taskIdentifier: tasks ? { in: tasks } : undefined,
instances: environments
? {
some: {
environmentId: environments ? { in: environments } : undefined,
},
}
: undefined,
instances: {
some: {
environmentId,
},
},
type: filterType,
AND: search
? {
Expand Down Expand Up @@ -242,25 +272,19 @@ export class ScheduleListPresenter extends BasePresenter {
};
});

const limit = await getLimit(project.organizationId, "schedules", 100_000_000);

return {
currentPage: page,
totalPages: Math.ceil(totalCount / pageSize),
totalCount: totalCount,
schedules,
possibleTasks: possibleTasks.map((task) => task.slug),
possibleEnvironments: project.environments.map((environment) => {
return displayableEnvironment(environment, userId);
}),
possibleTasks: possibleTasks.map((task) => task.slug).sort((a, b) => a.localeCompare(b)),
hasFilters,
limits: {
used: schedulesCount,
limit,
},
filters: {
tasks,
environments,
search,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,21 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const url = new URL(request.url);
const s = Object.fromEntries(url.searchParams.entries());
const filters = ScheduleListFilters.parse(s);
filters.environments = [environment.id];

const presenter = new ScheduleListPresenter();
const list = await presenter.call({
userId,
projectId: project.id,
environmentId: environment.id,
...filters,
});

return typedjson(list);
};

export default function Page() {
const {
schedules,
possibleTasks,
possibleEnvironments,
hasFilters,
limits,
currentPage,
totalPages,
} = useTypedLoaderData<typeof loader>();
const { schedules, possibleTasks, hasFilters, limits, currentPage, totalPages } =
useTypedLoaderData<typeof loader>();
const location = useLocation();
const organization = useOrganization();
const project = useProject();
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/app/routes/api.v1.schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export async function loader({ request }: LoaderFunctionArgs) {

const result = await presenter.call({
projectId: authenticationResult.environment.projectId,
environmentId: authenticationResult.environment.id,
page: params.data.page ?? 1,
pageSize: params.data.perPage,
environments: [authenticationResult.environment.id],
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX CONCURRENTLY IF NOT EXISTS "BackgroundWorker_runtimeEnvironmentId_createdAt_idx" ON "BackgroundWorker" ("runtimeEnvironmentId", "createdAt" DESC);
2 changes: 2 additions & 0 deletions internal-packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,8 @@ model BackgroundWorker {

@@unique([projectId, runtimeEnvironmentId, version])
@@index([runtimeEnvironmentId])
// Get the latest worker for a given environment
@@index([runtimeEnvironmentId, createdAt(sort: Desc)])
}

model BackgroundWorkerFile {
Expand Down
Loading