branch: main
commit 197a45c5226b8e679b3ec95c8f2a273635f99b2b
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Sun Jan 5 18:07:16 2025 +0100

    metrics: ‘db-get-metrics-with-id’ can restrict to a field value.
    
    * src/cuirass/metrics.scm (db-get-metrics-with-id): Add #:value and
    honor it.  Change default #:timestamp? value.
---
 src/cuirass/metrics.scm | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/cuirass/metrics.scm b/src/cuirass/metrics.scm
index 28bf128..5a3db4f 100644
--- a/src/cuirass/metrics.scm
+++ b/src/cuirass/metrics.scm
@@ -378,20 +378,30 @@ DELETE FROM Metrics WHERE type =" index " AND field = " 
name ";")))
 
 (define* (db-get-metrics-with-id id
                                  #:key
-                                 timestamp?
+                                 value
+                                 (timestamp? (and value #t))
                                  limit
                                  (order "id DESC"))
   "Return the metrics with the given ID as a key/value alist or, when
-TIMESTAMP? is true, as a list of timestamp/key/value tuples.  If LIMIT is set,
-the resulting list is restricted to LIMIT records."
+TIMESTAMP? is true, as a list of timestamp/key/value tuples.  When VALUE is
+true, restrict to metrics whose field equals VALUE and omit it from the return
+value.  If LIMIT is set, the resulting list is restricted to LIMIT records."
+  (define include-field?
+    (not value))
+
   (with-db-connection db
     (let* ((metric (find-metric id))
            (type (metric->type metric))
            (field-type (metric-field-type metric))
            (limit (or limit "ALL")))
       (let ((query (format #f "SELECT field, value, timestamp FROM Metrics
-WHERE type = :type ORDER BY ~a LIMIT ~a" order limit))
-            (params `((#:type . ,type))))
+WHERE type = :type ~a ORDER BY ~a LIMIT ~a"
+                           (if value "AND field = :field" "")
+                           order limit))
+            (params `((#:type . ,type)
+                      ,@(if value
+                            `((#:field . ,value))
+                            '()))))
         (let loop ((rows (exec-query/bind-params db query params))
                    (metrics '()))
           (match rows
@@ -402,10 +412,15 @@ WHERE type = :type ORDER BY ~a LIMIT ~a" order limit))
                             (else field))))
                (loop rest
                      `(,(if timestamp?
-                            `(,(string->number timestamp)
-                              ,field
-                              ,(locale-string->inexact value))
-                            `(,field . ,(locale-string->inexact value)))
+                            (if include-field?
+                                `(,(string->number timestamp)
+                                  ,field
+                                  ,(locale-string->inexact value))
+                                (cons (string->number timestamp)
+                                      (locale-string->inexact value)))
+                            (if include-field?
+                                `(,field . ,(locale-string->inexact value))
+                                (locale-string->inexact value)))
                        ,@metrics))))))))))
 
 (define* (db-update-metric id #:optional field

Reply via email to