aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/compare/controller.scm
blob: 6380651c221685d0d41a34c7d3527437f270eda1 (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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 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-data-service web compare controller)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-19)
  #:use-module (ice-9 match)
  #:use-module (ice-9 vlist)
  #:use-module (texinfo)
  #:use-module (texinfo html)
  #:use-module (texinfo plain-text)
  #:use-module (guix-data-service utils)
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service web sxml)
  #:use-module (guix-data-service web util)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service web query-parameters)
  #:use-module (guix-data-service web controller)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service comparison)
  #:use-module (guix-data-service jobs load-new-guix-revision)
  #:use-module (guix-data-service model guix-revision)
  #:use-module (guix-data-service model system)
  #:use-module (guix-data-service model git-repository)
  #:use-module (guix-data-service model derivation)
  #:use-module (guix-data-service model build-server)
  #:use-module (guix-data-service model build-status)
  #:use-module (guix-data-service model lint-warning-message)
  #:use-module (guix-data-service web compare html)
  #:export (compare-controller))

(define cache-control-default-max-age
  (* 60 60 24)) ; One day

(define http-headers-for-unchanging-content
  `((cache-control
     . (public
        (max-age . ,cache-control-default-max-age)))))

(define (parse-build-status s)
  s)

(define (parse-commit s)
  (with-resource-from-pool (connection-pool) conn
    (let* ((job-details
            (select-job-for-commit conn s))
           (job-state
            (assq-ref job-details 'state)))
      (if job-details
          (cond
           ((string=? job-state "succeeded")
            s)
           ((string=? job-state "queued")
            (make-invalid-query-parameter
             s
             `("data unavailable, "
               (a (@ (href ,(string-append
                             "/revision/" s)))
                  "yet to process revision"))))
           ((string=? job-state "failed")
            (make-invalid-query-parameter
             s
             `("data unavailable, "
               (a (@ (href ,(string-append
                             "/revision/" s)))
                  "failed to process revision"))))
           (else
            (make-invalid-query-parameter
             s "unknown job state")))
          (make-invalid-query-parameter
           s "unknown commit")))))

(define (parse-derivation file-name)
  (if (with-resource-from-pool (connection-pool) conn
        (select-derivation-by-file-name conn file-name))
      file-name
      (make-invalid-query-parameter
       file-name "unknown derivation")))

(define (parse-build-change val)
  (or (if (member val '("broken" "fixed"
                        "still-working"
                        "still-failing"
                        "unknown"))
          val
          #f)
      (make-invalid-query-parameter
       val
       "unknown build change value")))

(define (compare-controller request
                            method-and-path-components
                            mime-types
                            body)
  (match method-and-path-components
    (('GET "compare")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_commit   ,parse-commit #:required)
                (target_commit ,parse-commit #:required)
                (locale        ,identity #:default "en_US.UTF-8")))))
       (render-compare mime-types
                       parsed-query-parameters)))
    (('GET "compare-by-datetime")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_branch     ,identity #:required)
                (base_datetime   ,parse-datetime
                                 #:default ,(current-date))
                (target_branch   ,identity #:required)
                (target_datetime ,parse-datetime
                                 #:default ,(current-date))
                (locale          ,identity #:default "en_US.UTF-8")))))
       (render-compare-by-datetime mime-types
                                   parsed-query-parameters)))
    (('GET "compare" "derivation")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_derivation   ,parse-derivation #:required)
                (target_derivation ,parse-derivation #:required)))))
       (render-compare/derivation mime-types
                                  parsed-query-parameters)))
    (('GET "compare" "package-derivations")
     (let* ((parsed-query-parameters
             (guard-against-mutually-exclusive-query-parameters
              (parse-query-parameters
               request
               `((base_commit   ,parse-commit #:required)
                 (target_commit ,parse-commit #:required)
                 (system        ,parse-system #:multi-value)
                 (target        ,parse-target #:multi-value)
                 (build_status  ,parse-build-status #:multi-value)
                 (build_change  ,parse-build-change)
                 (after_name    ,identity)
                 (limit_results ,parse-result-limit
                                #:no-default-when (all_results)
                                #:default 40)
                 (all_results   ,parse-checkbox-value)))
              '((limit_results all_results)))))

       (render-compare/package-derivations mime-types
                                           parsed-query-parameters)))
    (('GET "compare-by-datetime" "package-derivations")
     (let* ((parsed-query-parameters
             (guard-against-mutually-exclusive-query-parameters
              (parse-query-parameters
               request
               `((base_branch     ,identity #:required)
                 (base_datetime   ,parse-datetime
                                  #:default ,(current-date))
                 (target_branch   ,identity #:required)
                 (target_datetime ,parse-datetime
                                  #:default ,(current-date))
                 (system          ,parse-system #:multi-value)
                 (target          ,parse-target #:multi-value)
                 (build_status    ,parse-build-status #:multi-value)
                 (build_change    ,parse-build-change)
                 (after_name    ,identity)
                 (limit_results ,parse-result-limit
                                #:no-default-when (all_results)
                                #:default 40)
                 (all_results   ,parse-checkbox-value)))
              '((base_commit base_datetime)
                (target_commit target_datetime)
                (limit_results all_results)))))
       (render-compare-by-datetime/package-derivations mime-types
                                                       parsed-query-parameters)))
    (('GET "compare" "packages")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_commit   ,parse-commit #:required)
                (target_commit ,parse-commit #:required)))))
       (render-compare/packages mime-types
                                parsed-query-parameters)))
    (('GET "compare" "system-test-derivations")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_commit   ,parse-commit #:required)
                (target_commit ,parse-commit #:required)
                (system        ,parse-system #:default "x86_64-linux")))))

       (render-compare/system-test-derivations mime-types
                                               parsed-query-parameters)))
    (('GET "compare-by-datetime" "system-test-derivations")
     (let* ((parsed-query-parameters
             (parse-query-parameters
              request
              `((base_branch     ,identity #:required)
                 (base_datetime   ,parse-datetime
                                  #:default ,(current-date))
                 (target_branch   ,identity #:required)
                 (target_datetime ,parse-datetime
                                  #:default ,(current-date))
                 (system        ,parse-system #:default "x86_64-linux")))))

       (render-compare-by-datetime/system-test-derivations
        mime-types
        parsed-query-parameters)))
    (_ #f)))

(define (texinfo->variants-alist s)
  (let ((stexi (texi-fragment->stexi s)))
    `((source . ,s)
      (html   . ,(with-output-to-string
                   (lambda ()
                     (sxml->html (stexi->shtml stexi)))))
      (plain . ,(stexi->plain-text stexi)))))

(define (render-compare mime-types
                        query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (letpar& ((base-job
                 (match (assq-ref query-parameters 'base_commit)
                   (($ <invalid-query-parameter> value)
                    (with-resource-from-pool (connection-pool) conn
                      (and (string? value)
                           (select-job-for-commit conn value))))
                   (_ #f)))
                (target-job
                 (match (assq-ref query-parameters 'target_commit)
                   (($ <invalid-query-parameter> value)
                    (with-resource-from-pool (connection-pool) conn
                      (and (string? value)
                           (select-job-for-commit conn value))))
                   (_ #f))))
        (case (most-appropriate-mime-type
               '(application/json text/html)
               mime-types)
          ((application/json)
           (render-json
            `((error      . "invalid query")
              (query_parameters
               . ,(map
                   (match-lambda
                     ((key . val)
                      (cons key
                            (match val
                              (($ <invalid-query-parameter> value message)
                               `((invalid_value . ,value)
                                 (message       . ,(call-with-output-string
                                                     (lambda (port)
                                                       (sxml->html message port))))))
                              (val val)))))
                   query-parameters))
              (base_job   . ,base-job)
              (target_job . ,target-job))))
          (else
           (render-html
            #:sxml (compare query-parameters
                            'revision
                            #f
                            #f
                            #f
                            #f
                            #f
                            #f
                            #f)))))
      (letpar& ((base-revision-id
                 (with-resource-from-pool (connection-pool) conn
                   (commit->revision-id
                    conn
                    (assq-ref query-parameters 'base_commit))))
                (target-revision-id
                 (with-resource-from-pool (connection-pool) conn
                   (commit->revision-id
                    conn
                    (assq-ref query-parameters 'target_commit))))
                (locale
                 (assq-ref query-parameters 'locale)))
        (let-values
            (((base-packages-vhash target-packages-vhash)
              (package-data->package-data-vhashes
               (with-resource-from-pool (connection-pool) conn
                 (package-differences-data conn
                                           base-revision-id
                                           target-revision-id)))))
          (let ((new-packages
                 (package-data-vhashes->new-packages base-packages-vhash
                                                     target-packages-vhash))
                (removed-packages
                 (package-data-vhashes->removed-packages base-packages-vhash
                                                         target-packages-vhash))
                (version-changes
                 (package-data-version-changes base-packages-vhash
                                               target-packages-vhash)))
            (letpar& ((lint-warnings-data
                       (with-resource-from-pool (connection-pool) conn
                         (group-list-by-first-n-fields
                          2
                          (lint-warning-differences-data conn
                                                         base-revision-id
                                                         target-revision-id
                                                         locale))))
                      (channel-news-data
                       (with-resource-from-pool (connection-pool) conn
                         (channel-news-differences-data conn
                                                        base-revision-id
                                                        target-revision-id))))
              (case (most-appropriate-mime-type
                     '(application/json text/html)
                     mime-types)
                ((application/json)
                 (render-json
                  `((base-commit    . ,(assq-ref query-parameters 'base_commit))
                    (target-commit  . ,(assq-ref query-parameters 'target_commit))
                    (channel-news . ,(list->vector
                                      (map
                                       (match-lambda
                                         ((commit tag title_text body_text change)
                                          `(,@(if (null? commit)
                                                  '()
                                                  `((commit . ,commit)))
                                            ,@(if (null? tag)
                                                  '()
                                                  `((tag . ,tag)))
                                            (title-text
                                             . ,(map
                                                 (match-lambda
                                                   ((lang . text)
                                                    (cons
                                                     lang
                                                     (texinfo->variants-alist text))))
                                                 title_text))
                                            (body-text
                                             . ,(map
                                                 (match-lambda
                                                   ((lang . text)
                                                    (cons
                                                     lang
                                                     (texinfo->variants-alist text))))
                                                 body_text))
                                            (change . ,change))))
                                       channel-news-data)))
                    (new-packages . ,(list->vector new-packages))
                    (removed-packages . ,(list->vector removed-packages))
                    (version-changes . ,(list->vector
                                         (map
                                          (match-lambda
                                            ((name data ...)
                                             `((name . ,name)
                                               ,@data)))
                                          version-changes)))
                    (lint_warnings
                     . ,(list->vector
                         (map
                          (match-lambda
                            (((package-name package-version)
                              . warnings)

                             `((package
                                . ((name . ,package-name)
                                   (version . ,package-version)))
                               (warnings
                                . ,(list->vector
                                    (map
                                     (match-lambda
                                       ((lint-checker-name
                                         message
                                         lint-checker-description
                                         lint-checker-network-dependent
                                         file line column-number
                                         change)

                                        `((change  . ,change)
                                          (checker
                                           . ((name . ,lint-checker-name)
                                              (description
                                               . ,lint-checker-description)))
                                          (message . ,message)
                                          (location
                                           . ((file   . ,file)
                                              (line   . ,(string->number line))
                                              (column . ,(string->number
                                                          column-number)))))))
                                     warnings))))))
                          lint-warnings-data))))
                  #:extra-headers http-headers-for-unchanging-content))
                (else
                 (letpar& ((lint-warnings-locale-options
                            (map
                             (match-lambda
                               ((locale)
                                locale))
                             (with-resource-from-pool (connection-pool) conn
                               (lint-warning-message-locales-for-revision
                                conn
                                (assq-ref query-parameters 'target_commit)))))
                           (cgit-url-bases
                            (with-resource-from-pool (connection-pool) conn
                              (guix-revisions-cgit-url-bases
                               conn
                               (list base-revision-id
                                     target-revision-id)))))
                   (render-html
                    #:sxml (compare query-parameters
                                    'revision
                                    cgit-url-bases
                                    new-packages
                                    removed-packages
                                    version-changes
                                    lint-warnings-data
                                    lint-warnings-locale-options
                                    channel-news-data)
                    #:extra-headers http-headers-for-unchanging-content))))))))))

(define (render-compare-by-datetime mime-types
                                    query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (render-html
          #:sxml (compare query-parameters
                          'datetime
                          #f
                          #f
                          #f
                          #f
                          #f
                          #f
                          #f))))

      (let ((base-branch     (assq-ref query-parameters 'base_branch))
            (base-datetime   (assq-ref query-parameters 'base_datetime))
            (target-branch   (assq-ref query-parameters 'target_branch))
            (target-datetime (assq-ref query-parameters 'target_datetime))
            (locale          (assq-ref query-parameters 'locale)))
        (letpar& ((base-revision-details
                   (with-resource-from-pool (connection-pool) conn
                     (select-guix-revision-for-branch-and-datetime
                      conn
                      base-branch
                      base-datetime)))
                  (target-revision-details
                   (with-resource-from-pool (connection-pool) conn
                     (select-guix-revision-for-branch-and-datetime
                      conn
                      target-branch
                      target-datetime))))
          (let ((lint-warnings-locale-options
                 (map
                  (match-lambda
                    ((locale)
                     locale))
                  (with-resource-from-pool (connection-pool) conn
                    (lint-warning-message-locales-for-revision
                     conn
                     (second base-revision-details))))))
            (let ((base-revision-id
                   (first base-revision-details))
                  (target-revision-id
                   (first target-revision-details)))
              (let-values
                  (((base-packages-vhash target-packages-vhash)
                    (package-data->package-data-vhashes
                      (with-resource-from-pool (connection-pool) conn
                        (package-differences-data conn
                                                  base-revision-id
                                                  target-revision-id)))))
                (let* ((new-packages
                        (package-data-vhashes->new-packages base-packages-vhash
                                                            target-packages-vhash))
                       (removed-packages
                        (package-data-vhashes->removed-packages base-packages-vhash
                                                                target-packages-vhash))
                       (version-changes
                        (package-data-version-changes base-packages-vhash
                                                      target-packages-vhash))
                       (channel-news-data
                        (with-resource-from-pool (connection-pool) conn
                          (channel-news-differences-data conn
                                                         base-revision-id
                                                         target-revision-id))))
                  (case (most-appropriate-mime-type
                         '(application/json text/html)
                         mime-types)
                    ((application/json)
                     (render-json
                      `((revisions
                         . ((base
                             . ((commit . ,(second base-revision-details))
                                (datetime . ,(fourth base-revision-details))))
                            (target
                             . ((commit . ,(second target-revision-details))
                                (datetime . ,(fourth target-revision-details))))))
                        (channel-news . ,(list->vector
                                          (map
                                           (match-lambda
                                             ((commit tag title_text body_text change)
                                              `(,@(if (null? commit)
                                                      '()
                                                      `((commit . ,commit)))
                                                ,@(if (null? tag)
                                                      '()
                                                      `((tag . ,tag)))
                                                (title-text
                                                 . ,(map
                                                     (match-lambda
                                                       ((lang . text)
                                                        (cons
                                                         lang
                                                         (texinfo->variants-alist text))))
                                                     title_text))
                                                (body-text
                                                 . ,(map
                                                     (match-lambda
                                                       ((lang . text)
                                                        (cons
                                                         lang
                                                         (texinfo->variants-alist text))))
                                                     body_text))
                                                (change . ,change))))
                                           channel-news-data)))
                        (new-packages . ,(list->vector new-packages))
                        (removed-packages . ,(list->vector removed-packages))
                        (version-changes . ,(list->vector
                                             (map
                                              (match-lambda
                                                ((name data ...)
                                                 `((name . ,name)
                                                   ,@data)))
                                              version-changes))))
                      #:extra-headers http-headers-for-unchanging-content))
                    (else
                     (render-html
                      #:sxml (compare
                              `(,@query-parameters
                                (base_commit . ,(second base-revision-details))
                                (target_commit . ,(second target-revision-details)))
                              'datetime
                              (with-resource-from-pool (connection-pool) conn
                                (guix-revisions-cgit-url-bases
                                 conn
                                 (list base-revision-id
                                       target-revision-id)))
                              new-packages
                              removed-packages
                              version-changes
                              (group-list-by-first-n-fields
                               2
                               (with-resource-from-pool (connection-pool) conn
                                 (lint-warning-differences-data
                                  conn
                                  base-revision-id
                                  target-revision-id
                                  locale)))
                              lint-warnings-locale-options
                              channel-news-data)
                      #:extra-headers http-headers-for-unchanging-content)))))))))))

(define (render-compare/derivation mime-types
                                   query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (render-html
          #:sxml (compare/derivation
                  query-parameters
                  '()))))

      (let ((base-derivation    (assq-ref query-parameters 'base_derivation))
            (target-derivation  (assq-ref query-parameters 'target_derivation)))
        (let ((data
               (with-resource-from-pool (connection-pool) conn
                 (derivation-differences-data conn
                                              base-derivation
                                              target-derivation))))
          (case (most-appropriate-mime-type
                 '(application/json text/html)
                 mime-types)
            ((application/json)
             (render-json
              `((base                  . ((derivation . ,base-derivation)))
                (target                . ((derivation . ,target-derivation)))
                (outputs               . ,(assq-ref data 'outputs))
                (inputs                . ,(assq-ref data 'inputs))
                (sources               . ,(assq-ref data 'sources))
                (system                . ,(assq-ref data 'system))
                (builder               . ,(assq-ref data 'builder))
                (arguments             . ,(assq-ref data 'arguments))
                (environment-variables . ,(assq-ref
                                           data 'environment-variables)))
              #:extra-headers http-headers-for-unchanging-content))
            (else
             (render-html
              #:sxml (compare/derivation
                      query-parameters
                      data)
              #:extra-headers http-headers-for-unchanging-content)))))))

(define (render-compare/package-derivations mime-types
                                            query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (letpar& ((base-job
                    (and=> (match (assq-ref query-parameters 'base_commit)
                             (($ <invalid-query-parameter> value)
                              (and (string? value) value))
                             ((? string? value) value)
                             (_ #f))
                           (lambda (commit)
                             (with-resource-from-pool (connection-pool) conn
                               (select-job-for-commit conn commit)))))
                   (target-job
                    (and=> (match (assq-ref query-parameters 'target_commit)
                             (($ <invalid-query-parameter> value)
                              (and (string? value) value))
                             ((? string? value) value)
                             (_ #f))
                           (lambda (commit)
                             (with-resource-from-pool (connection-pool) conn
                               (select-job-for-commit conn commit))))))
           (render-json
            `((error . "invalid query")
              (query_parameters
               .
               ,(map
                 (match-lambda
                   ((key . val)
                    (cons key
                          (match val
                            (($ <invalid-query-parameter> value message)
                             `((invalid_value . ,value)
                               (message       . ,(call-with-output-string
                                                   (lambda (port)
                                                     (sxml->html message port))))))
                            ((? list? val)
                             (list->vector val))
                            (val val)))))
                 query-parameters))
              (base_job   . ,base-job)
              (target_job . ,target-job)))))
        (else
         (letpar& ((systems
                    (call-with-resource-from-pool (connection-pool)
                      list-systems))
                   (targets
                    (call-with-resource-from-pool (connection-pool)
                      valid-targets))
                   (build-server-urls
                    (call-with-resource-from-pool (connection-pool)
                      select-build-server-urls-by-id)))
         (render-html
          #:sxml (compare/package-derivations
                  query-parameters
                  'revision
                  systems
                  (valid-targets->options targets)
                  build-status-strings
                  build-server-urls
                  '())))))

      (let ((base-commit    (assq-ref query-parameters 'base_commit))
            (target-commit  (assq-ref query-parameters 'target_commit))
            (systems        (assq-ref query-parameters 'system))
            (targets        (assq-ref query-parameters 'target))
            (build-change   (and=>
                             (assq-ref query-parameters 'build_change)
                             string->symbol))
            (after-name     (assq-ref query-parameters 'after_name))
            (limit-results  (assq-ref query-parameters 'limit_results)))
        (letpar& ((data
                   (with-resource-from-pool (connection-pool) conn
                     (package-derivation-differences-data
                      conn
                      (commit->revision-id conn base-commit)
                      (commit->revision-id conn target-commit)
                      #:systems systems
                      #:targets targets
                      #:build-change build-change
                      #:after-name after-name
                      #:limit-results limit-results)))
                  (build-server-urls
                   (with-resource-from-pool (connection-pool) conn
                    select-build-server-urls-by-id)))
          (let ((names-and-versions
                 (package-derivation-data->names-and-versions data)))
            (let-values
                (((base-packages-vhash target-packages-vhash)
                  (package-derivation-data->package-derivation-data-vhashes data)))
              (let ((derivation-changes
                     (package-derivation-data-changes names-and-versions
                                                      base-packages-vhash
                                                      target-packages-vhash)))
                (case (most-appropriate-mime-type
                       '(application/json text/html)
                       mime-types)
                  ((application/json)
                   (render-json
                    `((revisions
                       . ((base
                           . ((commit . ,base-commit)))
                          (target
                           . ((commit . ,target-commit)))))
                      (derivation_changes
                       . ,derivation-changes))))
                  (else
                   (letpar& ((systems
                              (call-with-resource-from-pool (connection-pool)
                                list-systems))
                             (targets
                              (call-with-resource-from-pool (connection-pool)
                                valid-targets)))
                     (render-html
                      #:sxml (compare/package-derivations
                              query-parameters
                              'revision
                              systems
                              (valid-targets->options targets)
                              build-status-strings
                              build-server-urls
                              derivation-changes))))))))))))

(define (render-compare-by-datetime/package-derivations mime-types
                                                        query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (render-html
          #:sxml (compare/package-derivations
                  query-parameters
                  'datetime
                  (call-with-resource-from-pool (connection-pool)
                    list-systems)
                  (valid-targets->options
                   (call-with-resource-from-pool (connection-pool)
                     valid-targets))
                  build-status-strings
                  '()
                  '()
                  '()))))

      (let ((base-branch     (assq-ref query-parameters 'base_branch))
            (base-datetime   (assq-ref query-parameters 'base_datetime))
            (target-branch   (assq-ref query-parameters 'target_branch))
            (target-datetime (assq-ref query-parameters 'target_datetime))
            (systems         (assq-ref query-parameters 'system))
            (targets         (assq-ref query-parameters 'target))
            (build-change    (and=>
                              (assq-ref query-parameters 'build_change)
                              string->symbol))
            (after-name     (assq-ref query-parameters 'after_name))
            (limit-results  (assq-ref query-parameters 'limit_results)))
        (letpar&
            ((base-revision-details
              (with-resource-from-pool (connection-pool) conn
                (select-guix-revision-for-branch-and-datetime conn
                                                              base-branch
                                                              base-datetime)))
             (target-revision-details
              (with-resource-from-pool (connection-pool) conn
                (select-guix-revision-for-branch-and-datetime conn
                                                              target-branch
                                                              target-datetime))))
          (letpar&
              ((data
                (with-resource-from-pool (connection-pool) conn
                  (package-derivation-differences-data
                   conn
                   (first base-revision-details)
                   (first target-revision-details)
                   #:systems systems
                   #:targets targets
                   #:build-change build-change
                   #:after-name after-name
                   #:limit-results limit-results))))
            (let ((names-and-versions
                   (package-derivation-data->names-and-versions data)))
              (let-values
                  (((base-packages-vhash target-packages-vhash)
                    (package-derivation-data->package-derivation-data-vhashes data)))
                (let ((derivation-changes
                       (package-derivation-data-changes names-and-versions
                                                        base-packages-vhash
                                                        target-packages-vhash)))
                  (case (most-appropriate-mime-type
                         '(application/json text/html)
                         mime-types)
                    ((application/json)
                     (render-json
                      `((revisions
                         . ((base
                             . ((commit . ,(second base-revision-details))
                                (datetime . ,(fourth base-revision-details))))
                            (target
                             . ((commit . ,(second target-revision-details))
                                (datetime . ,(fourth target-revision-details))))))
                        (derivation_changes
                         . ,derivation-changes))))
                    (else
                     (render-html
                      #:sxml (compare/package-derivations
                              query-parameters
                              'datetime
                              (call-with-resource-from-pool
                               (connection-pool)
                               list-systems)
                              (valid-targets->options
                               (call-with-resource-from-pool
                                (connection-pool)
                                valid-targets))
                              build-status-strings
                              (call-with-resource-from-pool
                               (connection-pool)
                               select-build-server-urls-by-id)
                              derivation-changes
                              base-revision-details
                              target-revision-details))))))))))))

(define (render-compare/packages mime-types
                                 query-parameters)
  (define (package-data-vhash->json vh)
    (delete-duplicates
     (vhash-fold (lambda (name data result)
                   (cons `((name . ,name)
                           (version . ,(car data)))
                         result))
                 '()
                 vh)))

  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (letpar& ((base-job
                    (match (assq-ref query-parameters 'base_commit)
                      (($ <invalid-query-parameter> value)
                       (with-resource-from-pool (connection-pool) conn
                         (select-job-for-commit conn value)))
                      (_ #f)))
                   (target-job
                    (match (assq-ref query-parameters 'target_commit)
                      (($ <invalid-query-parameter> value)
                       (with-resource-from-pool (connection-pool) conn
                         (select-job-for-commit conn value)))
                      (_ #f))))
         (render-html
          #:sxml (compare-invalid-parameters
                  query-parameters
                  base-job
                  target-job)))))

      (let ((base-commit    (assq-ref query-parameters 'base_commit))
            (target-commit  (assq-ref query-parameters 'target_commit)))
        (letpar& ((base-revision-id
                   (with-resource-from-pool (connection-pool) conn
                     (commit->revision-id
                      conn
                      base-commit)))
                  (target-revision-id
                   (with-resource-from-pool (connection-pool) conn
                     (commit->revision-id
                      conn
                      target-commit))))
          (let-values
              (((base-packages-vhash target-packages-vhash)
                (package-data->package-data-vhashes
                 (with-resource-from-pool (connection-pool) conn
                   (package-differences-data conn
                                             base-revision-id
                                             target-revision-id)))))
            (case (most-appropriate-mime-type
                   '(application/json text/html)
                   mime-types)
              ((application/json)
               (render-json
                `((base
                   . ((commit . ,base-commit)
                      (packages . ,(list->vector
                                    (package-data-vhash->json base-packages-vhash)))))
                  (target
                   . ((commit . ,target-commit)
                      (packages . ,(list->vector
                                    (package-data-vhash->json target-packages-vhash))))))
                #:extra-headers http-headers-for-unchanging-content))
              (else
               (render-html
                #:sxml (compare/packages
                        query-parameters
                        base-packages-vhash
                        target-packages-vhash)
                #:extra-headers http-headers-for-unchanging-content))))))))

(define (render-compare/system-test-derivations mime-types
                                                query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (letpar& ((systems
                    (with-resource-from-pool (connection-pool) conn
                     list-systems))
                   (build-server-urls
                    (with-resource-from-pool (connection-pool) conn
                     select-build-server-urls-by-id)))
         (render-html
          #:sxml (compare/system-test-derivations
                  query-parameters
                  'revision
                  systems
                  build-server-urls
                  '()
                  '()
                  '())))))

      (let ((base-commit    (assq-ref query-parameters 'base_commit))
            (target-commit  (assq-ref query-parameters 'target_commit))
            (system         (assq-ref query-parameters 'system)))
        (letpar& ((data
                   (with-resource-from-pool (connection-pool) conn
                     (system-test-derivations-differences-data
                      conn
                      (commit->revision-id conn base-commit)
                      (commit->revision-id conn target-commit)
                      system)))
                  (build-server-urls
                   (with-resource-from-pool (connection-pool) conn
                    select-build-server-urls-by-id))
                  (base-git-repositories
                   (with-resource-from-pool (connection-pool) conn
                     (git-repositories-containing-commit conn base-commit)))
                  (target-git-repositories
                   (with-resource-from-pool (connection-pool) conn
                     (git-repositories-containing-commit conn target-commit)))
                  (systems
                   (with-resource-from-pool (connection-pool) conn
                    list-systems)))
          (case (most-appropriate-mime-type
                 '(application/json text/html)
                 mime-types)
            ((application/json)
             (render-json
              `((revisions
                 . ((base
                     . ((commit . ,base-commit)))
                    (target
                     . ((commit . ,target-commit)))))
                (changes . ,(list->vector data)))))
            (else
             (render-html
              #:sxml (compare/system-test-derivations
                      query-parameters
                      'revision
                      systems
                      build-server-urls
                      base-git-repositories
                      target-git-repositories
                      data))))))))

(define (render-compare-by-datetime/system-test-derivations mime-types
                                                            query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          '((error . "invalid query"))))
        (else
         (letpar& ((systems
                    (with-resource-from-pool (connection-pool) conn
                     list-systems))
                   (build-server-urls
                    (with-resource-from-pool (connection-pool) conn
                     select-build-server-urls-by-id)))
         (render-html
          #:sxml (compare/system-test-derivations
                  query-parameters
                  'datetime
                  systems
                  build-server-urls
                  '()
                  '()
                  '())))))

      (let ((base-branch     (assq-ref query-parameters 'base_branch))
            (base-datetime   (assq-ref query-parameters 'base_datetime))
            (target-branch   (assq-ref query-parameters 'target_branch))
            (target-datetime (assq-ref query-parameters 'target_datetime))
            (system         (assq-ref query-parameters 'system)))
        (letpar&
            ((base-revision-details
              (with-resource-from-pool (connection-pool) conn
                (select-guix-revision-for-branch-and-datetime conn
                                                              base-branch
                                                              base-datetime)))
             (target-revision-details
              (with-resource-from-pool (connection-pool) conn
                (select-guix-revision-for-branch-and-datetime conn
                                                              target-branch
                                                              target-datetime))))
          (letpar& ((data
                     (with-resource-from-pool (connection-pool) conn
                       (system-test-derivations-differences-data
                        conn
                        (first base-revision-details)
                        (first target-revision-details)
                        system)))
                    (build-server-urls
                     (with-resource-from-pool (connection-pool) conn
                      select-build-server-urls-by-id))
                    (base-git-repositories
                     (with-resource-from-pool (connection-pool) conn
                       (git-repositories-containing-commit
                        conn
                        (second base-revision-details))))
                    (target-git-repositories
                     (with-resource-from-pool (connection-pool) conn
                       (git-repositories-containing-commit
                        conn
                        (second target-revision-details))))
                    (systems
                     (with-resource-from-pool (connection-pool) conn
                      list-systems)))
            (case (most-appropriate-mime-type
                   '(application/json text/html)
                   mime-types)
              ((application/json)
               (render-json
                `((revisions
                   . ((base
                       . ((commit . ,(second base-revision-details))
                          (datetime . ,(fourth base-revision-details))))
                      (target
                       . ((commit . ,(second target-revision-details))
                          (datetime . ,(fourth target-revision-details))))))
                  (changes . ,(list->vector data)))))
              (else
               (render-html
                #:sxml (compare/system-test-derivations
                        query-parameters
                        'datetime
                        systems
                        build-server-urls
                        base-git-repositories
                        target-git-repositories
                        data
                        base-revision-details
                        target-revision-details)))))))))