aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/view/shared.scm
blob: 3cf92b8c2f579a3a0874a2ec1adef7a962db8735 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
;;; Guix QA Frontpage
;;;
;;; Copyright © 2022 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 view shared)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-43)
  #:use-module (ice-9 match)
  #:use-module (web uri)
  #:use-module ((guix-data-service web util)
                #:select (uri-encode-filename))
  #:use-module ((guix-data-service model utils) #:select (group-to-alist))
  #:use-module (guix-qa-frontpage manage-builds)
  #:use-module (guix-qa-frontpage view util)
  #:export (package-changes-view
            package-cross-changes-view
            package-changes-summary-table
            package-cross-changes-summary-table))

(define (builds->overall-status builds)
  (if (eq? #f builds)
      'not-present
      (let ((build-statuses
             (map
              (lambda (build)
                (let ((status
                       (assoc-ref build "status")))
                  (if (and (string=? status "scheduled")
                           (assoc-ref build "potentially_blocked"))
                      "blocked"
                      status)))
              builds)))
        (cond
         ((member "succeeded" build-statuses)
          'succeeding)
         ((and (not (member "succeeded" build-statuses))
               (member "failed" build-statuses))
          'failing)
         ((member "blocked" build-statuses)
          'blocked)
         (else
          'unknown)))))

(define %changes
  '(broken
    fixed
    blocked
    still-working
    still-failing
    still-blocked
    new-working
    new-failing
    new-blocked
    removed-working
    removed-failing
    removed-blocked
    unknown-to-working
    unknown-to-failing
    unknown-to-blocked
    unknown))

(define (builds->change-by-system builds-by-system)
  (map
   (match-lambda
     ((system . builds)
      (let ((base-status (builds->overall-status
                          (assoc-ref builds "base")))
            (target-status (builds->overall-status
                            (assoc-ref builds "target"))))
        (cons
         system
         (cond
          ((and (eq? base-status 'succeeding)
                (eq? target-status 'failing))
           'broken)
          ((and (eq? base-status 'succeeding)
                (eq? target-status 'blocked))
           'blocked)
          ((and (or (eq? base-status 'failing)
                    (eq? base-status 'blocked))
                (eq? target-status 'succeeding))
           'fixed)
          ((and (eq? 'not-present base-status)
                (eq? 'succeeding target-status))
           'new-working)
          ((and (eq? 'not-present base-status)
                (eq? 'failing target-status))
           'new-failing)
          ((and (eq? 'not-present base-status)
                (eq? 'blocked target-status))
           'new-blocked)
          ((and (eq? 'succeeding base-status)
                (eq? 'not-present target-status))
           'removed-working)
          ((and (eq? 'failing base-status)
                (eq? 'not-present target-status))
           'removed-failing)
          ((and (eq? 'blocked base-status)
                (eq? 'not-present target-status))
           'removed-blocked)
          ((and (eq? base-status target-status 'succeeding))
           'still-working)
          ((and (eq? base-status target-status 'failing))
           'still-failing)
          ((and (eq? base-status target-status 'blocked))
           'still-blocked)
          ((and (or (eq? 'unknown base-status)
                    (eq? 'blocked base-status))
                (eq? 'succeeding target-status))
           'unknown-to-working)
          ((and (or (eq? 'unknown base-status)
                    (eq? 'blocked base-status))
                (eq? 'failing target-status))
           'unknown-to-failing)
          ((and (eq? 'unknown base-status)
                (eq? 'blocked target-status))
           'unknown-to-blocked)
          (else 'unknown))))))
   builds-by-system))

(define (display-builds builds derivations change)
  (define %color-for-change
    '((fixed              . "green")
      (broken             . "red")
      (blocked            . "yellow")
      (still-working      . "lightgreen")
      (still-failing      . "#FFCCCB")
      (still-blocked      . "lightyellow")
      (new-working        . "lightgreen")
      (new-failing        . "red")
      (new-blocked        . "lightyellow")
      (removed-working    . "")
      (removed-failing    . "")
      (removed-blocked    . "")
      (unknown-to-working . "lightgreen")
      (unknown-to-failing . "#FFCCCB")
      (unknown-to-blocked . "lightyellow")
      (unknown            . "lightgrey")))

  (if builds
      (let ((base-status
             (builds->overall-status
              (assoc-ref builds "base")))
            (target-status
             (builds->overall-status
              (assoc-ref builds "target"))))
        (if (and
             (eq? base-status 'not-present)
             (eq? target-status 'not-present))
            '(td)
            `(td
              (@ (style ,(simple-format
                          #f
                          "background-color: ~A;"
                          (assq-ref
                           %color-for-change
                           change))))
              ,(if (eq? base-status 'not-present)
                   `(div "was not present")
                   `(div
                     (a (@ (href ,(string-append
                                   "https://data.qa.guix.gnu.org"
                                   (uri-encode-filename
                                    (assoc-ref derivations "base")))))
                        "was " ,base-status)))
              ,(if (eq? target-status 'not-present)
                   `(div "now not present")
                   `(div
                     (a (@ (href ,(string-append
                                   "https://data.qa.guix.gnu.org"
                                   (uri-encode-filename
                                    (assoc-ref derivations "target")))))
                        "now " ,target-status))))))
      '(td)))

(define (package-changes-view title
                              derivation-changes
                              query-parameters)
  (define (derivation-for-system derivations system)
    (vector-any
     (lambda (derivation)
       (if (string=? (assoc-ref derivation "system")
                     system)
           derivation
           #f))
     derivations))

  (define (builds-by-system base target)
    (map
     (lambda (system)
       (cons
        system
        `(("base" . ,(and=>
                      (derivation-for-system base system)
                      (lambda (derivation)
                        (vector->list
                         (assoc-ref derivation "builds")))))
          ("target" . ,(and=>
                        (derivation-for-system target system)
                        (lambda (derivation)
                          (vector->list
                           (assoc-ref derivation "builds"))))))))
     %systems-to-submit-builds-for))

  (define (derivations-by-system base target)
    (map
     (lambda (system)
       (cons
        system
        `(("base" . ,(and=>
                      (derivation-for-system base system)
                      (lambda (derivation)
                        (assoc-ref derivation "derivation-file-name"))))
          ("target" . ,(and=>
                        (derivation-for-system target system)
                        (lambda (derivation)
                          (assoc-ref derivation "derivation-file-name")))))))
     %systems-to-submit-builds-for))

  (define grouped-query-parameters
    (group-to-alist
     identity
     query-parameters))

  (define system-change
    (map
     (lambda (system)
       (cons (string-append system "-change")
             system))
     %systems-to-submit-builds-for))

  (define (display? package-and-version change-by-system)
    (every
     (match-lambda
       ((key . vals)
        (cond
         ((assoc-ref system-change key)
          (let ((system (assoc-ref system-change key)))
            (->bool
             (member (assoc-ref change-by-system system)
                     (map string->symbol vals)))))
         (else #t))))
     grouped-query-parameters))

  (layout
   #:title title
   #:body
   `((main
      (@ (style "max-width: 98%;"))
      (table
       (form
        (@ (id "filter-form")
           (method "get"))
        (thead
         (tr
          (td "Name")
          (td "Version")
          ,@(map
             (lambda (system)
               `(td (span (@ (style "font-size: 1.5em; font-family: monospace;"))
                          ,system)
                    (select
                     (@ (name
                         ,(simple-format #f "~A-change"
                                         system))
                        (style "margin-bottom: 0;")
                        (multiple #t))
                     ,@(let ((system-change-selected-options
                              (or (assoc-ref
                                   grouped-query-parameters
                                   (string-append system "-change"))
                                  '())))
                         (map
                          (match-lambda
                            ((value . label)
                             `(option
                               (@ (value ,value)
                                  ,@(if (member (symbol->string value)
                                                system-change-selected-options)
                                        '((selected ""))
                                        '()))
                               ,label)))
                          (map
                           (lambda (change)
                             (cons change change))
                           %changes))))
                    (button
                     (@ (type "submit")
                        (style "padding: 0; width: 100%;"))
                     "Update")))
             %systems-to-submit-builds-for))
         (tr
          (td)
          (td)
          ,@(map
             (lambda (system)
               (let* ((system-change-selected-options
                       (or (assoc-ref
                            grouped-query-parameters
                            (string-append system "-change"))
                           '()))
                      (selected-labels
                       (filter-map
                        (match-lambda
                          ((value . label)
                           (if (member (symbol->string value)
                                       system-change-selected-options)
                               label
                               #f)))
                        (map
                         (lambda (change)
                           (cons change change))
                         %changes))))
                 (if (null? selected-labels)
                     '(td)
                     `(td
                       "Filtering for:"
                       (ul
                        (@ (style "margin: 0;"))
                        ,@(map (lambda (label)
                                 `(li ,label))
                               selected-labels))))))
             %systems-to-submit-builds-for))))
       (tbody
        (@ (style "overflow: auto; max-height: 40em;"))
        ,@(vector-fold-right
           (lambda (_ result package-and-version)
             (let* ((builds
                     (builds-by-system
                      (assoc-ref package-and-version "base")
                      (assoc-ref package-and-version "target")))
                    (change-by-system
                     (builds->change-by-system builds))
                    (derivations
                     (derivations-by-system
                      (assoc-ref package-and-version "base")
                      (assoc-ref package-and-version "target"))))
               (cons
                `(tr
                  (@ ,@(if (display? package-and-version
                                     change-by-system)
                           '()
                           '((style "display: none;"))))
                  (td ,(assoc-ref package-and-version "name"))
                  (td ,(assoc-ref package-and-version "version"))
                  ,@(map
                     (lambda (system)
                       (display-builds (assoc-ref builds system)
                                       (assoc-ref derivations system)
                                       (assoc-ref change-by-system system)))
                     %systems-to-submit-builds-for))
                result)))
           '()
           (assoc-ref derivation-changes "derivation_changes"))))))))

(define (package-cross-changes-view title
                                    system
                                    derivation-changes
                                    query-parameters)
  (define (derivation-for-target derivations target)
    (vector-any
     (lambda (derivation)
       (if (string=? (assoc-ref derivation "target")
                     target)
           derivation
           #f))
     derivations))

  ;; TODO This probably performs poorly when there are lots of changes
  (define all-targets
    (delete-duplicates!
     (vector-fold-right
      (lambda (_ result package-and-version)
        (vector-fold-right
         (lambda (_ result derivation)
           (let ((target
                  (assoc-ref derivation "target")))
             (if (string-null? target)
                 result
                 (cons target result))))
         (vector-fold-right
          (lambda (_ result derivation)
            (let ((target
                   (assoc-ref derivation "target")))
              (if (string-null? target)
                  result
                  (cons target result))))
          result
          (assoc-ref package-and-version "target"))
         (assoc-ref package-and-version "base")))
      '()
      (assoc-ref derivation-changes "derivation_changes"))))

  (define (builds-by-target base-data target-data)
    (map
     (lambda (target)
       (cons
        target
        `(("base" . ,(and=>
                      (derivation-for-target base-data target)
                      (lambda (derivation)
                        (vector->list
                         (assoc-ref derivation "builds")))))
          ("target" . ,(and=>
                        (derivation-for-target target-data target)
                        (lambda (derivation)
                          (vector->list
                           (assoc-ref derivation "builds"))))))))
     all-targets))

  (define (derivations-by-target base-data target-data)
    (map
     (lambda (target)
       (cons
        target
        `(("base" . ,(and=>
                      (derivation-for-target base-data target)
                      (lambda (derivation)
                        (assoc-ref derivation "derivation-file-name"))))
          ("target" . ,(and=>
                        (derivation-for-target target-data target)
                        (lambda (derivation)
                          (assoc-ref derivation "derivation-file-name")))))))
     all-targets))

  (define grouped-query-parameters
    (group-to-alist
     identity
     query-parameters))

  (define target-change
    (map
     (lambda (target)
       (cons (string-append target "-change")
             target))
     all-targets))

  (define (display? package-and-version change-by-target)
    (every
     (match-lambda
       ((key . vals)
        (cond
         ((assoc-ref target-change key)
          (let ((system (assoc-ref target-change key)))
            (->bool
             (member (assoc-ref change-by-target system)
                     (map string->symbol vals)))))
         (else #t))))
     grouped-query-parameters))

  (layout
   #:title title
   #:body
   `((main
      (@ (style "max-width: 98%;"))
      (table
       (form
        (@ (id "filter-form")
           (method "get"))
        (thead
         (tr
          (td "Name")
          (td "Version")
          ,@(map
             (lambda (target)
               `(td (span (@ (style "font-size: 1.5em; font-family: monospace;"))
                          ,target)
                    (select
                     (@ (name
                         ,(simple-format #f "~A-change"
                                         target))
                        (style "margin-bottom: 0;")
                        (multiple #t))
                     ,@(let ((target-change-selected-options
                              (or (assoc-ref
                                   grouped-query-parameters
                                   (string-append target "-change"))
                                  '())))
                         (map
                          (match-lambda
                            ((value . label)
                             `(option
                               (@ (value ,value)
                                  ,@(if (member (symbol->string value)
                                                target-change-selected-options)
                                        '((selected ""))
                                        '()))
                               ,label)))
                          (map
                           (lambda (change)
                             (cons change change))
                           %changes))))
                    (button
                     (@ (type "submit")
                        (style "padding: 0; width: 100%;"))
                     "Update")))
             all-targets))
         (tr
          (td)
          (td)
          ,@(map
             (lambda (target)
               (let* ((target-change-selected-options
                       (or (assoc-ref
                            grouped-query-parameters
                            (string-append target "-change"))
                           '()))
                      (selected-labels
                       (filter-map
                        (match-lambda
                          ((value . label)
                           (if (member (symbol->string value)
                                       target-change-selected-options)
                               label
                               #f)))
                        (map
                         (lambda (change)
                           (cons change change))
                         %changes))))
                 (if (null? selected-labels)
                     '(td)
                     `(td
                       "Filtering for:"
                       (ul
                        (@ (style "margin: 0;"))
                        ,@(map (lambda (label)
                                 `(li ,label))
                               selected-labels))))))
             all-targets))))
       (tbody
        (@ (style "overflow: auto; max-height: 40em;"))
        ,@(vector-fold-right
           (lambda (_ result package-and-version)
             (let* ((builds
                     (builds-by-target
                      (assoc-ref package-and-version "base")
                      (assoc-ref package-and-version "target")))
                    (derivations
                     (derivations-by-target
                      (assoc-ref package-and-version "base")
                      (assoc-ref package-and-version "target")))
                    (change-by-target
                     ;; This works, even though the naming is wrong as it's
                     ;; being used to group builds by target
                     (builds->change-by-system builds)))
               (cons
                `(tr
                  (@ ,@(if (display? package-and-version
                                     change-by-target)
                           '()
                           '((style "display: none;"))))
                  (td ,(assoc-ref package-and-version "name"))
                  (td ,(assoc-ref package-and-version "version"))
                  ,@(map
                     (lambda (target)
                       (display-builds (assoc-ref builds target)
                                       (assoc-ref derivations target)
                                       (assoc-ref change-by-target target)))
                     all-targets))
                result)))
           '()
           (assoc-ref derivation-changes "derivation_changes"))))))))

(define (package-changes-summary-table revisions
                                       derivation-changes-counts
                                       package-changes-url-prefix)

  (define* (package-derivations-comparison-link system
                                                #:key build-change)
    (string-append
     (simple-format #f "https://data.qa.guix.gnu.org/compare/package-derivations?base_commit=~A&target_commit=~A"
                    (assq-ref revisions 'base)
                    (assq-ref revisions 'target))
     (match system
       ((system . target)
        (simple-format #f "&system=~A&target=~A"
                       system
                       target))
       (system
        (simple-format #f "&system=~A&target=none"
                       system)))
     (if build-change
         (simple-format #f "&build_change=~A" build-change)
         "")))

  `(table
    (@ (style "border-collapse: collapse;"))
    (thead
     (tr
      (th (@ (rowspan 3)) "System")
      (th (@ (colspan 8)) "Package build status")
      (th))
     (tr
      (th (@ (colspan 4)) "Base")
      (th (@ (colspan 4)
             (style "border-left-width: 0.1em; border-left-style: solid; border-left-color: black"))
          "With branch changes")
      (th))
     (tr
      ,@(let ((header-style
               "font-size: 80%; min-width: 3.5rem;"))
          `((th (@ (style ,header-style))
                "Succeeding")
            (th (@ (style ,header-style))
                "Failing")
            (th (@ (style ,header-style))
                "Blocked")
            (th (@ (style ,header-style))
                "Unknown")
            (th (@ (style
                       ,(string-append
                         header-style
                         " border-left-width: 0.125em; border-left-style: solid; border-left-color: black;")))
                "Succeeding")
            (th (@ (style ,header-style))
                "Failing")
            (th (@ (style ,header-style))
                "Blocked")
            (th (@ (style ,header-style))
                "Unknown")
            (th)))))
    (tbody
     ,@(if (and derivation-changes-counts
                (not (assq-ref derivation-changes-counts 'exception)))
           (if (null? derivation-changes-counts)
               `((tr
                  (td (@ (colspan 7))
                      "No package derivation changes")))
               (map
                (match-lambda
                  ((system . derivations)

                   (define (count side status)
                     (assoc-ref (assoc-ref
                                 derivations
                                 side)
                                status))
                   `(tr
                     (td (@ (class "monospace")) ,system)
                     ,@(map (lambda (status)
                              `(td ,(count 'base status)))
                            '(succeeding failing blocked unknown))
                     (td ,@(if (and (>= (count 'target 'succeeding)
                                        (count 'base 'succeeding))
                                    (> (count 'target 'succeeding)
                                       0))
                               `((@ (class "good")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-changes?"
                                  system "-change=fixed&"
                                  system "-change=still-working&"
                                  system "-change=unknown-to-working&"
                                  system "-change=new-working")))
                            ,(count 'target 'succeeding)))
                     (td ,@(if (> (count 'target 'failing)
                                  (count 'base 'failing))
                               '((@ (class "bad")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-changes?"
                                  system "-change=broken&"
                                  system "-change=still-failing&"
                                  system "-change=unknown-to-failing&"
                                  system "-change=new-failing")))
                            ,(count 'target 'failing)))
                     (td ,@(if (> (count 'target 'blocked)
                                  (count 'base 'blocked))
                               '((@ (class "bad")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-changes?"
                                  system "-change=blocked&"
                                  system "-change=still-blocked&"
                                  system "-change=unknown-to-blocked&"
                                  system "-change=new-blocked")))
                            ,(count 'target 'blocked)))
                     (td (@ ,@(if (> (count 'target 'unknown)
                                     (count 'base 'unknown))
                                  '((class "bad"))
                                  '()))
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-changes?"
                                  system "-change=unknown")))
                            ,(count 'target 'unknown)))
                     (td (a (@ (href
                                ,(package-derivations-comparison-link system)))
                            "View comparison")))))
                derivation-changes-counts))
           `((tr
              (td (@ (colspan 10)
                     (class "bad"))
                  "Comparison unavailable"
                  ,@(or (and=>
                         (assq-ref derivation-changes-counts
                                   'invalid_query_parameters)
                         (lambda (params)
                           (append-map
                            (match-lambda
                              ((param . details)
                               (let ((error
                                      (assq-ref details 'error)))
                                 (cond
                                  ((member param '("base_commit"
                                                   "target_commit"))
                                   `((br)
                                     (a
                                      (@ (href
                                          ,(string-append
                                            "https://data.qa.guix.gnu.org"
                                            "/revision/"
                                            (assq-ref
                                             revisions
                                             (if (string=? param "base_commit")
                                                 'base
                                                 'target)))))
                                      ,(cond
                                        ((eq? error 'unknown-commit)
                                         (string-append
                                          (if (string=? param "base_commit")
                                              "Base revision "
                                              "Target revision ")
                                          "unknown to the data service."))
                                        ((member error
                                                 '(yet-to-process-revision
                                                   failed-to-process-revision))
                                         (simple-format
                                          #f "~A to process ~A"
                                          (if (eq? error 'yet-to-process-revision)
                                              "Yet"
                                              "Failed")
                                          (if (string=? param "base_commit")
                                              "base revision (from master branch)"
                                              "target revision")))
                                        (else
                                         (string-append
                                          "Error with "
                                          (if (string=? param "base_commit")
                                              "base revision."
                                              "target revision.")))))))))))
                            params)))
                        '()))))))))

(define (package-cross-changes-summary-table revisions
                                             cross-derivation-changes-counts
                                             package-changes-url-prefix)

  (define* (package-derivations-comparison-link system target
                                                #:key build-change)
    (string-append
     (simple-format #f "https://data.qa.guix.gnu.org/compare/package-derivations?base_commit=~A&target_commit=~A"
                    (assq-ref revisions 'base)
                    (assq-ref revisions 'target))
     (simple-format #f "&system=~A&target=~A"
                    system
                    target)
     (if build-change
         (simple-format #f "&build_change=~A" build-change)
         "")))

  `(table
    (@ (style "border-collapse: collapse;"))
    (thead
     (tr
      (th (@ (rowspan 3)) "Target")
      (th (@ (colspan 8)) "Package build status")
      (th))
     (tr
      (th (@ (colspan 4)) "Base")
      (th (@ (colspan 4)
             (style "border-left-width: 0.1em; border-left-style: solid; border-left-color: black"))
          "With branch changes")
      (th))
     (tr
      ,@(let ((header-style
               "font-size: 80%; min-width: 3.5rem;"))
          `((th (@ (style ,header-style))
                "Succeeding")
            (th (@ (style ,header-style))
                "Failing")
            (th (@ (style ,header-style))
                "Blocked")
            (th (@ (style ,header-style))
                "Unknown")
            (th (@ (style
                       ,(string-append
                         header-style
                         " border-left-width: 0.125em; border-left-style: solid; border-left-color: black;")))
                "Succeeding")
            (th (@ (style ,header-style))
                "Failing")
            (th (@ (style ,header-style))
                "Blocked")
            (th (@ (style ,header-style))
                "Unknown")
            (th)))))
    (tbody
     ,@(if (and cross-derivation-changes-counts
                (not (assq-ref cross-derivation-changes-counts 'exception)))
           (if (null? cross-derivation-changes-counts)
               `((tr
                  (td (@ (colspan 7))
                      "No package derivation changes")))
               (map
                (match-lambda
                  (((system . target) . derivations)

                   (define (count side status)
                     (assoc-ref (assoc-ref
                                 derivations
                                 side)
                                status))

                   `(tr
                     (td (@ (class "monospace")) ,target)
                     ,@(map (lambda (status)
                              `(td ,(count 'base status)))
                            '(succeeding failing blocked unknown))
                     (td ,@(if (and (>= (count 'target 'succeeding)
                                        (count 'base 'succeeding))
                                    (> (count 'target 'succeeding)
                                       0))
                               `((@ (class "good")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-cross-changes?"
                                  target "-change=fixed&"
                                  target "-change=still-working&"
                                  target "-change=unknown-to-working&"
                                  target "-change=new-working")))
                            ,(count 'target 'succeeding)))
                     (td ,@(if (> (count 'target 'failing)
                                  (count 'base 'failing))
                               '((@ (class "bad")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-cross-changes?"
                                  target "-change=broken&"
                                  target "-change=still-failing&"
                                  target "-change=unknown-to-failing&"
                                  target "-change=new-failing")))
                            ,(count 'target 'failing)))
                     (td ,@(if (> (count 'target 'blocked)
                                  (count 'base 'blocked))
                               '((@ (class "bad")))
                               '())
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-cross-changes?"
                                  target "-change=blocked&"
                                  target "-change=still-blocked&"
                                  target "-change=unknown-to-blocked&"
                                  target "-change=new-blocked")))
                            ,(count 'target 'blocked)))
                     (td (@ ,@(if (> (count 'target 'unknown)
                                     (count 'base 'unknown))
                                  '((class "bad"))
                                  '()))
                         (a (@ (href
                                ,(string-append
                                  package-changes-url-prefix
                                  "/package-cross-changes?"
                                  target "-change=unknown")))
                            ,(count 'target 'unknown)))
                     (td (a (@ (href
                                ,(package-derivations-comparison-link system
                                                                      target)))
                            "View comparison")))))
                cross-derivation-changes-counts))
           `((tr
              (td (@ (colspan 10)
                     (class "bad"))
                  "Comparison unavailable"
                  ,@(or (and=>
                         (assq-ref cross-derivation-changes-counts
                                   'invalid_query_parameters)
                         (lambda (params)
                           (append-map
                            (match-lambda
                              ((param . details)
                               (let ((error
                                      (assq-ref details 'error)))
                                 (cond
                                  ((member param '("base_commit"
                                                   "target_commit"))
                                   `((br)
                                     (a
                                      (@ (href
                                          ,(string-append
                                            "https://data.qa.guix.gnu.org"
                                            "/revision/"
                                            (assq-ref
                                             revisions
                                             (if (string=? param "base_commit")
                                                 'base
                                                 'target)))))
                                      ,(cond
                                        ((eq? error 'unknown-commit)
                                         (string-append
                                          (if (string=? param "base_commit")
                                              "Base revision "
                                              "Target revision ")
                                          "unknown to the data service."))
                                        ((member error
                                                 '(yet-to-process-revision
                                                   failed-to-process-revision))
                                         (simple-format
                                          #f "~A to process ~A"
                                          (if (eq? error 'yet-to-process-revision)
                                              "Yet"
                                              "Failed")
                                          (if (string=? param "base_commit")
                                              "base revision (from master branch)"
                                              "target revision")))
                                        (else
                                         (string-append
                                          "Error with "
                                          (if (string=? param "base_commit")
                                              "base revision."
                                              "target revision.")))))))))))
                            params)))
                        '()))))))))