From 213683ad279934b16f806db57c3766edb31f1625 Mon Sep 17 00:00:00 2001 From: Mathieu Othacehe <othacehe@gnu.org> Date: Sat, 30 Jan 2021 14:23:29 +0100 Subject: [PATCH] database: Add "db-get-build-percentage". * src/cuirass/database.scm (db-get-build-percentage): New procedure. * tests/database.scm ("db-get-build-percentage"): New test. --- src/cuirass/database.scm | 20 ++++++++++++++++++++ tests/database.scm | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/cuirass/database.scm b/src/cuirass/database.scm index 075db1ca..f59f9f1a 100644 --- a/src/cuirass/database.scm +++ b/src/cuirass/database.scm @@ -64,6 +64,7 @@ db-get-output db-get-outputs db-get-time-since-previous-build + db-get-build-percentage db-register-builds db-update-build-status! db-update-build-worker! @@ -667,6 +668,25 @@ WHERE job_name = " job-name "AND specification = " specification (string->number time)) (else #f)))) +(define (db-get-build-percentage build-id) + "Return the build completion percentage for BUILD-ID." + (with-db-worker-thread db + (match (expect-one-row + (exec-query/bind db " +SELECT LEAST(duration::float/last_duration * 100, 100)::int AS percentage FROM +(SELECT (extract(epoch from now())::int - starttime) as duration, +last_build.duration AS last_duration FROM builds, +(SELECT (stoptime - starttime) AS duration FROM Builds +WHERE job_name IN +(SELECT job_name from Builds WHERE id = " build-id ") +AND status = 0 +ORDER BY Builds.timestamp DESC LIMIT 1) last_build +where id = " build-id ") d; +")) + ((time) + (string->number time)) + (else #f)))) + (define (db-register-builds jobs eval-id specification) (define (new-outputs? outputs) (let ((new-outputs diff --git a/tests/database.scm b/tests/database.scm index 85acbafc..126481e5 100644 --- a/tests/database.scm +++ b/tests/database.scm @@ -402,6 +402,37 @@ timestamp, checkouttime, evaltime) VALUES ('guix', 0, 0, 0, 0);") #:outputs `(("out" . "/bar")))) (sort (db-get-pending-derivations) string<?))) + (test-assert "db-get-build-percentage" + (begin + (let* ((ts (time-second (current-time time-utc))) + (old `((#:derivation . "/last.drv") + (#:eval-id . 2) + (#:job-name . "job") + (#:timestamp . ,(- ts 10)) + (#:status . 0) + (#:starttime . 10) + (#:stoptime . 20) + (#:system . "x86_64-linux") + (#:nix-name . "foo") + (#:log . "log") + (#:outputs . ()))) + (new `((#:derivation . "/cur.drv") + (#:eval-id . 2) + (#:job-name . "job") + (#:timestamp . ,(- ts 5)) + (#:starttime . ,(- ts 5)) + (#:system . "x86_64-linux") + (#:nix-name . "foo") + (#:log . "log") + (#:outputs . ())))) + (db-add-build old) + (db-add-build new) + (let ((last-id + (match (expect-one-row + (exec-query (%db) "SELECT MAX(id) FROM Builds;")) + ((id) (string->number id))))) + (>= (db-get-build-percentage last-id) 50))))) + (test-assert "db-close" (begin (exec-query (%db) (format #f "DROP OWNED BY CURRENT_USER;")) -- GitLab