1- import { eq , avg , min , max , and , isNotNull } from "drizzle-orm"
1+ import { eq } from "drizzle-orm"
22
33import { RecordNotFoundError , RecordNotCreatedError } from "./errors.js"
44import type { InsertTaskMetrics , UpdateTaskMetrics } from "../schema.js"
5- import { insertTaskMetricsSchema , taskMetrics , tasks , runs } from "../schema.js"
5+ import { insertTaskMetricsSchema , taskMetrics } from "../schema.js"
66import { db } from "../db.js"
77
8- const table = taskMetrics
9-
108export const findTaskMetrics = async ( id : number ) => {
11- const run = await db . query . taskMetrics . findFirst ( { where : eq ( table . id , id ) } )
9+ const run = await db . query . taskMetrics . findFirst ( { where : eq ( taskMetrics . id , id ) } )
1210
1311 if ( ! run ) {
1412 throw new RecordNotFoundError ( )
@@ -19,7 +17,7 @@ export const findTaskMetrics = async (id: number) => {
1917
2018export const createTaskMetrics = async ( args : InsertTaskMetrics ) => {
2119 const records = await db
22- . insert ( table )
20+ . insert ( taskMetrics )
2321 . values ( {
2422 ...insertTaskMetricsSchema . parse ( args ) ,
2523 createdAt : new Date ( ) ,
@@ -36,7 +34,7 @@ export const createTaskMetrics = async (args: InsertTaskMetrics) => {
3634}
3735
3836export const updateTaskMetrics = async ( id : number , values : UpdateTaskMetrics ) => {
39- const records = await db . update ( table ) . set ( values ) . where ( eq ( table . id , id ) ) . returning ( )
37+ const records = await db . update ( taskMetrics ) . set ( values ) . where ( eq ( taskMetrics . id , id ) ) . returning ( )
4038 const record = records [ 0 ]
4139
4240 if ( ! record ) {
@@ -45,18 +43,3 @@ export const updateTaskMetrics = async (id: number, values: UpdateTaskMetrics) =
4543
4644 return record
4745}
48-
49- export const successfulTaskDurations = async ( ) => {
50- return db
51- . select ( {
52- runId : tasks . runId ,
53- avgDuration : avg ( taskMetrics . duration ) . mapWith ( Number ) ,
54- minDuration : min ( taskMetrics . duration ) . mapWith ( Number ) ,
55- maxDuration : max ( taskMetrics . duration ) . mapWith ( Number ) ,
56- } )
57- . from ( tasks )
58- . innerJoin ( taskMetrics , eq ( tasks . taskMetricsId , taskMetrics . id ) )
59- . innerJoin ( runs , eq ( tasks . runId , runs . id ) )
60- . where ( and ( eq ( tasks . passed , true ) , isNotNull ( runs . taskMetricsId ) ) )
61- . groupBy ( tasks . runId )
62- }
0 commit comments