branch: main
commit 724bef1703ebda7a807aec0b28184743e4e98901
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Sun Jan 5 22:09:54 2025 +0100

    metrics: Change pending builds sampling period to 6h.
    
    So far ‘pending-builds’ was effectively sampled every 24h.
    
    * src/cuirass/metrics.scm (%pending-builds-sampling-period): New variable.
    (timestamp): New procedure.
    (db-current-day-timestamp): Remove.
    (%metrics): Change ‘field-proc’ of ‘pending-builds’ to use ‘timestamp’.
    (db-update-metrics): Update the ‘pending-builds’ and
    ‘pending-builds-per-system’ metrics with the same timestamp.
    * tests/metrics.scm (pending-builds-timestamp): New variable.
    ("pending-builds", "pending-builds-per-system")
    ("pending-builds-per-system, aarch64-linux")
    ("pending-builds-per-system, x86_64-linux")
    ("db-update-metrics"): Use it instead of ‘today’.
---
 src/cuirass/metrics.scm | 35 +++++++++++++++++++++--------------
 tests/metrics.scm       | 21 +++++++++++++--------
 2 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/src/cuirass/metrics.scm b/src/cuirass/metrics.scm
index 0578da3..12bfc11 100644
--- a/src/cuirass/metrics.scm
+++ b/src/cuirass/metrics.scm
@@ -39,6 +39,8 @@
             metric->type
             compute-metric
 
+            %pending-builds-sampling-period       ;for tests
+
             db-get-metric
             db-get-metrics-with-id
             db-update-metric
@@ -204,6 +206,19 @@ LEFT JOIN Builds as B
 ON E.id = B.evaluation and B.stoptime > 0
 GROUP BY E.id, E.evaltime;"))))
 
+(define %pending-builds-sampling-period
+  ;; Sampling period in seconds for the 'pending-builds' and
+  ;; 'pending-builds-per-system' metrics.  More precisely, any additional
+  ;; sample taken during that period overwrites the previous sample of that
+  ;; same period.
+  (* 6 3600))
+
+(define (timestamp period)
+  "Return a timestamp (seconds since the Epoch) rounded to the previous
+PERIOD."
+  (let ((now (time-second (current-time time-utc))))
+    (- now (modulo now period))))
+
 (define (db-previous-day-timestamp)
   "Return the timestamp of the previous day."
   (with-db-connection db
@@ -211,13 +226,6 @@ GROUP BY E.id, E.evaltime;"))))
      (exec-query
       db "SELECT extract(epoch from 'yesterday'::date);"))))
 
-(define (db-current-day-timestamp)
-  "Return the timestamp of the current day."
-  (with-db-connection db
-    (return-exact
-     (exec-query
-      db "SELECT extract(epoch from 'today'::date);"))))
-
 (define* (db-latest-evaluations #:key (days 3))
   "Return the successful evaluations added during the previous DAYS."
   (with-db-connection db
@@ -289,7 +297,7 @@ to_timestamp(stoptime)::date > 'today'::date - interval '1 
day'"))))
    (metric
     (id 'pending-builds)
     (compute-proc (lambda (_) (db-pending-builds)))
-    (field-proc db-current-day-timestamp))
+    (field-proc (cut timestamp %pending-builds-sampling-period)))
 
    ;; New derivations per day.
    (metric
@@ -474,15 +482,14 @@ UPDATE SET value = " value ", timestamp = " timestamp 
";"))
       (with-transaction db
         (db-update-metric 'builds-per-day)
         (db-update-metric 'new-derivations-per-day)
-        (db-update-metric 'pending-builds)
 
-        ;; Update per-system pending build metrics; use the today-at-midnight
-        ;; as the timestamp, just like for the 'pending-builds' metric.
-        (let* ((now (time-second (current-time time-utc)))
-               (today (- now (modulo now 86400))))
+        ;; Update per-system pending build metrics; use the same timestamp as
+        ;; for the 'pending-builds' metric.
+        (let ((time (timestamp %pending-builds-sampling-period)))
+          (db-update-metric 'pending-builds time)
           (for-each (lambda (system)
                       (db-update-metric 'pending-builds-per-system system
-                                        #:timestamp today))
+                                        #:timestamp time))
                     %cuirass-supported-systems))
 
         ;; Update specification related metrics.
diff --git a/tests/metrics.scm b/tests/metrics.scm
index de2d88b..195b043 100644
--- a/tests/metrics.scm
+++ b/tests/metrics.scm
@@ -32,6 +32,10 @@
 (define yesterday
   (- today 86400))
 
+(define pending-builds-timestamp
+  (let ((time (current-time)))
+    (- time (modulo time %pending-builds-sampling-period))))
+
 (test-group-with-cleanup "database"
   (test-assert "db-init"
     (begin
@@ -99,28 +103,29 @@ nix_name, log, status, timestamp, starttime, stoptime) 
VALUES
       (db-get-metric 'builds-per-day yesterday)))
 
   (test-equal "pending-builds"
-    `((,today . 1.0))
+    `((,pending-builds-timestamp . 1.0))
     (begin
-      (db-update-metric 'pending-builds)
+      (db-update-metric 'pending-builds pending-builds-timestamp)
       (db-get-metrics-with-id 'pending-builds)))
 
   (test-equal "pending-builds-per-system"
-    `((,today "aarch64-linux" 1.0) (,today "x86_64-linux" 0.0))
+    `((,pending-builds-timestamp "aarch64-linux" 1.0)
+      (,pending-builds-timestamp "x86_64-linux" 0.0))
     (begin
       (db-update-metric 'pending-builds-per-system "x86_64-linux"
-                        #:timestamp today)
+                        #:timestamp pending-builds-timestamp)
       (db-update-metric 'pending-builds-per-system "aarch64-linux"
-                        #:timestamp today)
+                        #:timestamp pending-builds-timestamp)
       (db-get-metrics-with-id 'pending-builds-per-system
                               #:timestamp? #t)))
 
   (test-equal "pending-builds-per-system, aarch64-linux"
-    `((,today . 1.0))
+    `((,pending-builds-timestamp . 1.0))
     (db-get-metrics-with-id 'pending-builds-per-system
                             #:value "aarch64-linux"))
 
   (test-equal "pending-builds-per-system, x86_64-linux"
-    `((,today . 0.0))
+    `((,pending-builds-timestamp . 0.0))
     (db-get-metrics-with-id 'pending-builds-per-system
                             #:value "x86_64-linux"))
 
@@ -137,7 +142,7 @@ nix_name, log, status, timestamp, starttime, stoptime) 
VALUES
       (db-get-metrics-with-id 'percentage-failed-eval-per-spec)))
 
   (test-equal "db-update-metrics"
-    `((,today . 2.0))
+    `((,pending-builds-timestamp . 2.0))
     (begin
       (exec-query (%db) (format #f "\
 INSERT INTO Builds (id, derivation, evaluation, job_name, system,

Reply via email to