aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/branch.scm
blob: 587412098ebf15e60d02ac3b6b3a5d19e898bec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
;;; Guix QA Frontpage
;;;
;;; Copyright © 2022, 2023 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-qa-frontpage branch)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-43)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 threads)
  #:use-module (prometheus)
  #:use-module ((guix-build-coordinator utils)
                #:select (with-time-logging))
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-qa-frontpage mumi)
  #:use-module (guix-qa-frontpage git-repository)
  #:use-module (guix-qa-frontpage guix-data-service)
  #:use-module (guix-qa-frontpage derivation-changes)
  #:use-module (guix-qa-frontpage database)
  #:use-module (guix-qa-frontpage manage-builds)
  #:export (list-non-master-branches

            branch-data
            master-branch-data

            get-systems-with-low-substitute-availability

            start-refresh-non-patch-branches-data-thread))

(define (list-non-master-branches)
  (define (issue-title->branch title)
    (and=> (string-match ".* \"([^\"]*)\".*" title)
           (lambda (m)
             (match:substring m 1))))

  (define merge-issues-by-branch
    (filter-map
     (lambda (issue)
       (let ((branch (issue-title->branch
                      (assoc-ref issue "title")))
             (issue-number
              (assoc-ref issue "number")))
         (when (and branch (assoc-ref issue "open"))
           (cons branch
                 `(("issue_number" . ,issue-number)
                   ("issue_date" . ,(assoc-ref issue "date"))
                   ("blocked_by" . ,(assoc-ref issue "blocked_by")))))))
     (vector->list
      (mumi-search-issues
       ;; TODO: subject: doesn't seem to work for issues where the
       ;; subject/title has changed
       "\"Request for merging\" is:open"))))

  (let ((branches
         (map
          (lambda (branch)
            (let ((name (assoc-ref branch "name")))
              (cons name
                    (append
                     (or (assoc-ref merge-issues-by-branch name)
                         '())
                     (alist-delete "name" branch)))))
          (remove
           (lambda (branch)
             (or (string=? (assoc-ref branch "name")
                           "master")
                 (string-prefix? "version-"
                                 (assoc-ref branch "name"))
                 (string=? (assoc-ref branch "commit")
                           "")))
           (list-branches
            (list-branches-url 2))))))
    (let* ((initial-ordered-branches
            (stable-sort
             branches
             (lambda (a b)
               (let ((a-has-issue
                      (->bool (assoc-ref (cdr a) "issue_number")))
                     (b-has-issue
                      (->bool (assoc-ref (cdr b) "issue_number"))))
                 (if (and a-has-issue b-has-issue)
                     (let ((a-date
                            (assoc-ref (cdr a) "issue_date"))
                           (b-date
                            (assoc-ref (cdr b) "issue_date")))
                       (string<? a-date b-date))
                     a-has-issue)))))
           (initial-ordering-index-by-branch
            (map (lambda (index branch)
                   (cons (car branch) index))
                 (iota (length initial-ordered-branches))
                 initial-ordered-branches))
           (initial-ordering-index-by-issue-number
            (filter-map
             (lambda (index branch)
               (and=> (assoc-ref (cdr branch) "issue_number")
                      (lambda (issue-number)
                        (cons issue-number index))))
             (iota (length initial-ordered-branches))
             initial-ordered-branches)))

      ;; The idea with issues blocking others is to create a linked list,
      ;; however I think it's possible to have a loop in the blocking directed
      ;; graph, so try to not completely fail if this is the case.
      (stable-sort
       initial-ordered-branches
       (lambda (a b)
         (let ((a-initial-ordering-index
                (assq-ref initial-ordering-index-by-branch
                          (car a)))
               (b-initial-ordering-index
                (assq-ref initial-ordering-index-by-branch
                          (car b)))

               (a-blocked-by
                (map (lambda (issue)
                       (assoc-ref issue "number"))
                     (or (and=> (assoc-ref (cdr a) "blocked_by")
                                vector->list)
                         '())))
               (b-blocked-by
                (map (lambda (issue)
                       (assoc-ref issue "number"))
                     (or (and=> (assoc-ref (cdr b) "blocked_by")
                                vector->list)
                         '()))))
           (<
            (if (null? a-blocked-by)
                a-initial-ordering-index
                (let ((ordering-indexes
                       (filter-map
                        (lambda (blocking-issue)
                          (assq-ref initial-ordering-index-by-issue-number
                                    blocking-issue))
                        a-blocked-by)))
                  (if (null? ordering-indexes)
                      a-initial-ordering-index
                      (apply max ordering-indexes))))
            (if (null? b-blocked-by)
                b-initial-ordering-index
                (let ((ordering-indexes
                       (filter-map
                        (lambda (blocking-issue)
                          (assq-ref initial-ordering-index-by-issue-number
                                    blocking-issue))
                        b-blocked-by)))
                  (if (null? ordering-indexes)
                      b-initial-ordering-index
                      (apply max ordering-indexes)))))))))))

(define* (branch-data branch-name)
  (define branch-commit
    (get-commit
     (string-append "origin/" branch-name)))

  (if
   branch-commit
   (let* ((merge-base
           (get-git-merge-base
            (get-commit "origin/master")
            branch-commit))

          (revisions
           `((base   . ,merge-base)
             (target . ,branch-commit)))

          (up-to-date-with-master?
           (with-exception-handler guix-data-service-error->sexp
             (lambda ()
               (let* ((master-revision
                       (get-latest-processed-branch-revision "master"))
                      (changes
                       (length
                        (compare-package-derivations
                         (compare-package-derivations-url
                          `((base   . ,merge-base)
                            (target . ,master-revision))
                          ;; TODO: Maybe do something smarter here?
                          #:systems '("x86_64-linux"))))))
                 `((up-to-date? . ,(< changes 400))
                   (changes     . ,changes)
                   (master      . ,master-revision))))
             #:unwind? #t
             #:unwind-for-type &guix-data-service-error))

          (derivation-changes-data
           (with-exception-handler guix-data-service-error->sexp
             (lambda ()
               (let ((data
                      (compare-package-derivations
                       (compare-package-derivations-url
                        revisions
                        #:systems %systems-to-submit-builds-for))))

                 (with-throw-handler #t
                   (lambda ()
                     (derivation-changes
                      data
                      %systems-to-submit-builds-for))
                   (lambda _
                     (backtrace)))))
             #:unwind? #t
             #:unwind-for-type &guix-data-service-error))

          (substitute-availability
           (with-exception-handler guix-data-service-error->sexp
             (lambda ()
               (package-substitute-availability
                (package-substitute-availability-url
                 branch-commit)))
             #:unwind? #t
             #:unwind-for-type &guix-data-service-error))

          (package-reproducibility
           (guix-data-service-request
            (package-reproducibility-url branch-commit))))
     (values
      revisions
      derivation-changes-data
      substitute-availability
      package-reproducibility
      up-to-date-with-master?))

   (values #f #f #f #f #f #f)))

(define* (master-branch-data)
  (let* ((substitute-availability
          (package-substitute-availability
           "https://data.qa.guix.gnu.org/repository/2/branch/master/latest-processed-revision/package-substitute-availability.json"))

         (package-reproducibility
          (guix-data-service-request
           "https://data.qa.guix.gnu.org/repository/2/branch/master/latest-processed-revision/package-reproducibility.json"))

         (systems-with-low-substitute-availability
          (get-systems-with-low-substitute-availability
           substitute-availability
           (lset-difference
            string=?
            %systems-to-submit-builds-for
            %systems-with-expected-low-substitute-availability))))

    (values
     substitute-availability
     systems-with-low-substitute-availability
     package-reproducibility)))

(define* (get-systems-with-low-substitute-availability substitute-availability
                                                       systems
                                                       #:key (threshold 0.8))
  (filter-map
   (lambda (system-and-target-details)
     (let ((system (assoc-ref system-and-target-details "system")))
       (if (and (member system systems)
                (string-null? (assoc-ref system-and-target-details "target")))
           (let* ((known
                   (assoc-ref system-and-target-details "known"))
                  (unknown
                   (assoc-ref system-and-target-details "unknown"))
                  (availability
                   (/ known
                      (+ known unknown))))
             (if (< availability threshold)
                 system
                 #f))
           #f)))
   (vector->list
    (assoc-ref (find
                (lambda (details)
                  ;; TODO: Don't hardcode this
                  (string=?
                   "https://bordeaux.guix.gnu.org"
                   (assoc-ref
                    (assoc-ref details "server")
                    "url")))
                (vector->list substitute-availability))
               "availability"))))

(define (start-refresh-non-patch-branches-data-thread database
                                                      metrics-registry)
  (define frequency
    (* 30 60))

  (define branch-substitutes-known
    (make-gauge-metric metrics-registry
                       "branch_substitutes_known"
                       #:labels '(server branch system target)))

  (define branch-substitutes-unknown
    (make-gauge-metric metrics-registry
                       "branch_substitutes_unknown"
                       #:labels '(server branch system target)))

  (define (update-branch-substitute-availability-metrics
           branch-name
           substitute-availability)
    (for-each
     (lambda (server-details)
       (let ((server-url
              (assoc-ref
               (assoc-ref server-details "server")
               "url")))

         (for-each
          (lambda (system-and-target-details)
            (let ((label-values
                   `((server . ,server-url)
                     (branch . ,branch-name)
                     (system
                      . ,(assoc-ref system-and-target-details
                                    "system"))
                     (target
                      . ,(assoc-ref system-and-target-details
                                    "target")))))

              (metric-set branch-substitutes-known
                          (assoc-ref system-and-target-details
                                     "known")
                          #:label-values label-values)
              (metric-set branch-substitutes-unknown
                          (assoc-ref system-and-target-details
                                     "unknown")
                          #:label-values label-values)))
          (vector->list
           (assoc-ref server-details "availability")))))
     (vector->list
      substitute-availability)))

  (define (refresh-data)
    (simple-format (current-error-port)
                   "refreshing non-patch branches data...\n")
    (update-repository!)

    (let ((branches
           (with-sqlite-cache
            database
            'branches
            (lambda ()
              (remove
               (lambda (branch)
                 (or (string=? (assoc-ref branch "name")
                               "master")
                     (string-prefix? "version-"
                                     (assoc-ref branch "name"))))
               (list-branches
                (list-branches-url 2))))
            #:ttl 0)))

      (n-par-for-each
       1
       (lambda (branch)
         (let ((branch-name
                (assoc-ref branch "name")))
           (simple-format (current-error-port)
                          "refreshing data for ~A branch\n"
                          branch-name)

           (with-exception-handler
               (lambda (exn)
                 (simple-format
                  (current-error-port)
                  "failed fetching derivation changes for branch ~A: ~A\n"
                  branch-name
                  exn)

                 #f)
             (lambda ()
               (with-throw-handler #t
                 (lambda ()
                   (let ((revisions
                          derivation-change-counts
                          substitute-availability
                          package-reproducibility
                          up-to-date-with-master?
                          (with-sqlite-cache
                           database
                           'branch-data
                           branch-data
                           #:args
                           (list branch-name)
                           #:version 3
                           #:ttl (/ frequency 2))))

                     (unless (or (not substitute-availability)
                                 (assq-ref substitute-availability 'exception))
                       (update-branch-substitute-availability-metrics
                        branch-name
                        substitute-availability))))
                 (lambda _
                   (backtrace))))
             #:unwind? #t))
         #t)
       branches))

    (let ((master-branch-substitute-availability
           master-branch-systems-with-low-substitute-availability
           master-branch-package-reproducibility
           (with-sqlite-cache
            database
            'master-branch-data
            master-branch-data
            #:ttl 0
            #:version 2)))
      (update-branch-substitute-availability-metrics
       "master"
       master-branch-substitute-availability)))

  (call-with-new-thread
   (lambda ()
     (catch 'system-error
       (lambda ()
         (set-thread-name "branch data refresh"))
       (const #t))

     (while #t
       (let ((start-time (current-time)))
         (with-exception-handler
             (lambda (exn)
               (simple-format
                (current-error-port)
                "exception in branch data refresh thread: ~A\n"
                exn))
           (lambda ()
             (with-time-logging "refreshing branch data"
               (with-throw-handler #t
                 refresh-data
                 (lambda args
                   (display (backtrace) (current-error-port))
                   (newline (current-error-port))))))
           #:unwind? #t)

         (let ((time-taken
                (- (current-time) start-time)))
           (if (>= time-taken frequency)
               (simple-format (current-error-port)
                              "warning: refreshing branch data is behind\n")
               (sleep
                (- frequency time-taken)))))))))