-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache conclusion and stats in database
- Loading branch information
Showing
24 changed files
with
330 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const up = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.enum("conclusion", ["no-changes", "changes-detected"]); | ||
table.jsonb("stats"); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const down = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.dropColumn("conclusion"); | ||
table.dropColumn("stats"); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { assertNever } from "@argos/util/assertNever"; | ||
import { invariant } from "@argos/util/invariant"; | ||
|
||
import { job as buildNotificationJob } from "@/build-notification/job.js"; | ||
import { transaction } from "@/database/index.js"; | ||
import { Build, BuildConclusion, BuildNotification } from "@/database/models"; | ||
|
||
/** | ||
* Concludes the build by updating the conclusion and the stats. | ||
* Called when all diffs are processed. | ||
*/ | ||
export async function concludeBuild(input: { buildId: string }) { | ||
const { buildId } = input; | ||
const statuses = await Build.getScreenshotDiffsStatuses([buildId]); | ||
const [[conclusion], [stats]] = await Promise.all([ | ||
Build.getConclusions([buildId], statuses), | ||
Build.getStats([buildId]), | ||
]); | ||
invariant(stats !== undefined, "No stats found"); | ||
invariant(conclusion !== undefined, "No conclusion found"); | ||
// If the build is not yet concluded, we don't want to update it. | ||
if (conclusion === null) { | ||
return; | ||
} | ||
const [, buildNotification] = await transaction(async (trx) => { | ||
return Promise.all([ | ||
Build.query(trx).findById(buildId).patch({ | ||
conclusion, | ||
stats, | ||
}), | ||
BuildNotification.query(trx).insert({ | ||
buildId, | ||
type: getNotificationType(conclusion), | ||
jobStatus: "pending", | ||
}), | ||
]); | ||
}); | ||
await buildNotificationJob.push(buildNotification.id); | ||
} | ||
|
||
function getNotificationType(conclusion: BuildConclusion) { | ||
switch (conclusion) { | ||
case "changes-detected": | ||
return "diff-detected"; | ||
case "no-changes": | ||
return "no-diff-detected"; | ||
default: | ||
return assertNever(conclusion); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.