Skip to content

Commit b95a066

Browse files
committed
fix: sql filters
1 parent ce1f0d3 commit b95a066

2 files changed

Lines changed: 93 additions & 3 deletions

File tree

apps/api/src/app/metrics/services/dora-metrics.integration.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,51 @@ describe("DORA Metrics", () => {
16151615
// Should only see workspace 1's data: 1 deployment, 1 incident = 100%
16161616
expect(result.currentAmount).toBeCloseTo(100, 1);
16171617
});
1618+
1619+
it("excludes archived incidents from the failure rate", async () => {
1620+
const ctx = await createTestContextWithGitProfile();
1621+
const gitProfile = await seedGitProfile(ctx);
1622+
const repository = await seedRepository(ctx);
1623+
const application = await seedApplication(ctx, repository.repositoryId);
1624+
const environment = await seedEnvironment(ctx, { isProduction: true });
1625+
1626+
const pr = await seedPullRequest(
1627+
ctx,
1628+
repository.repositoryId,
1629+
gitProfile.gitProfileId,
1630+
{ createdAt: new Date("2024-01-15T10:00:00Z") }
1631+
);
1632+
const deployment = await seedDeployment(
1633+
ctx,
1634+
application.applicationId,
1635+
environment.environmentId,
1636+
{
1637+
deployedAt: new Date("2024-01-15T14:00:00Z"),
1638+
authorId: gitProfile.gitProfileId,
1639+
}
1640+
);
1641+
await seedDeploymentPullRequest(
1642+
ctx,
1643+
deployment.deploymentId,
1644+
pr.pullRequestId
1645+
);
1646+
const archived = await seedIncident(ctx, deployment.deploymentId, {
1647+
detectedAt: new Date("2024-01-15T15:00:00Z"),
1648+
resolvedAt: new Date("2024-01-15T16:00:00Z"),
1649+
});
1650+
await getPrisma(ctx.workspaceId).incident.update({
1651+
where: { id: archived.incidentId },
1652+
data: { archivedAt: new Date("2024-01-16T00:00:00Z") },
1653+
});
1654+
1655+
const result = await getChangeFailureRateMetric({
1656+
workspaceId: ctx.workspaceId,
1657+
dateRange: { from: "2024-01-15T00:00:00Z", to: "2024-01-16T00:00:00Z" },
1658+
period: Period.DAILY,
1659+
});
1660+
1661+
expect(result.currentAmount).toBe(0);
1662+
});
16181663
});
16191664

16201665
describe("Mean Time To Recover (MTTR)", () => {
@@ -2325,6 +2370,51 @@ describe("DORA Metrics", () => {
23252370
// Should only see workspace 1's data: 2 hours = 7,200,000 ms
23262371
expect(result.currentAmount).toBe(BigInt(7200000));
23272372
});
2373+
2374+
it("excludes incidents whose cause deployment is archived", async () => {
2375+
const ctx = await createTestContextWithGitProfile();
2376+
const gitProfile = await seedGitProfile(ctx);
2377+
const repository = await seedRepository(ctx);
2378+
const application = await seedApplication(ctx, repository.repositoryId);
2379+
const environment = await seedEnvironment(ctx, { isProduction: true });
2380+
2381+
const pr = await seedPullRequest(
2382+
ctx,
2383+
repository.repositoryId,
2384+
gitProfile.gitProfileId,
2385+
{ createdAt: new Date("2024-01-15T10:00:00Z") }
2386+
);
2387+
const deployment = await seedDeployment(
2388+
ctx,
2389+
application.applicationId,
2390+
environment.environmentId,
2391+
{
2392+
deployedAt: new Date("2024-01-15T10:00:00Z"),
2393+
authorId: gitProfile.gitProfileId,
2394+
}
2395+
);
2396+
await seedDeploymentPullRequest(
2397+
ctx,
2398+
deployment.deploymentId,
2399+
pr.pullRequestId
2400+
);
2401+
await seedIncident(ctx, deployment.deploymentId, {
2402+
detectedAt: new Date("2024-01-15T11:00:00Z"),
2403+
resolvedAt: new Date("2024-01-15T13:00:00Z"),
2404+
});
2405+
await getPrisma(ctx.workspaceId).deployment.update({
2406+
where: { id: deployment.deploymentId },
2407+
data: { archivedAt: new Date("2024-01-16T00:00:00Z") },
2408+
});
2409+
2410+
const result = await getMeanTimeToRecoverMetric({
2411+
workspaceId: ctx.workspaceId,
2412+
dateRange: { from: "2024-01-15T00:00:00Z", to: "2024-01-16T00:00:00Z" },
2413+
period: Period.DAILY,
2414+
});
2415+
2416+
expect(result.currentAmount).toBe(BigInt(0));
2417+
});
23282418
});
23292419

23302420
describe("Deployment Frequency", () => {

apps/api/src/app/metrics/services/dora-metrics.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export const getChangeFailureRateMetric = async (
236236

237237
const allJoins = [
238238
...joins,
239-
Prisma.sql`LEFT JOIN "Incident" i ON i."causeDeploymentId" = d."id" AND i."workspaceId" = d."workspaceId"`,
239+
Prisma.sql`LEFT JOIN "Incident" i ON i."causeDeploymentId" = d."id" AND i."workspaceId" = d."workspaceId" AND i."archivedAt" IS NULL`,
240240
];
241241

242242
const chartConditions = [
@@ -286,7 +286,7 @@ export const getChangeFailureRateMetric = async (
286286

287287
const allJoins = [
288288
...joins,
289-
Prisma.sql`LEFT JOIN "Incident" i ON i."causeDeploymentId" = d."id" AND i."workspaceId" = d."workspaceId"`,
289+
Prisma.sql`LEFT JOIN "Incident" i ON i."causeDeploymentId" = d."id" AND i."workspaceId" = d."workspaceId" AND i."archivedAt" IS NULL`,
290290
];
291291

292292
conditions.push(
@@ -488,7 +488,7 @@ const buildIncidentFilters = (
488488
conditions.push(Prisma.sql`i."resolvedAt" IS NOT NULL`);
489489

490490
joins.push(
491-
Prisma.sql`INNER JOIN "Deployment" cd ON i."causeDeploymentId" = cd."id" AND cd."workspaceId" = i."workspaceId"`
491+
Prisma.sql`INNER JOIN "Deployment" cd ON i."causeDeploymentId" = cd."id" AND cd."workspaceId" = i."workspaceId" AND cd."archivedAt" IS NULL`
492492
);
493493

494494
if (filters.environmentIds && filters.environmentIds.length > 0) {

0 commit comments

Comments
 (0)