aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/model/guix-revision-package-derivation.scm
blob: 63c23e5ccf9433b7a80af765f71c42738168492d (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
;;; 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 model guix-revision-package-derivation)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (squee)
  #:use-module (guix-data-service database)
  #:export (insert-guix-revision-package-derivations

            insert-guix-revision-package-derivation-distribution-counts
            backfill-guix-revision-package-derivation-distribution-counts

            get-sql-to-select-package-and-related-derivations-for-revision))

(define (insert-guix-revision-package-derivations
         conn guix-revision-id package-derivation-ids)
  (define insert
    (string-append "INSERT INTO guix_revision_package_derivations "
                   "(revision_id, package_derivation_id) "
                   "VALUES "
                   (string-join (map (lambda (package-derivation-id)
                                       (simple-format
                                        #f "(~A, ~A)"
                                        guix-revision-id
                                        package-derivation-id))
                                     package-derivation-ids)
                                ", ")
                   ";"))

  (exec-query conn insert))

(define (insert-guix-revision-package-derivation-distribution-counts
         conn
         guix-revision-id)
  (define system-ids-and-targets
    (exec-query
     conn
     "
SELECT DISTINCT system_id, target
FROM package_derivations
INNER JOIN guix_revision_package_derivations
  ON package_derivations.id = guix_revision_package_derivations.package_derivation_id
WHERE revision_id = $1"
     (list guix-revision-id)))

  (define (get-count-for-next-level system-id target level-counts)
    (define next-level
      (length level-counts))

    (define query
      (string-append
       (simple-format
        #f
        "
WITH l0 AS (
  SELECT derivation_id
  FROM package_derivations
  INNER JOIN guix_revision_package_derivations
    ON package_derivations.id
        = guix_revision_package_derivations.package_derivation_id
  WHERE revision_id = ~A
    AND system_id = ~A
    AND target = $STR$~A$STR$~A
)"
        guix-revision-id
        system-id
        target
        (if (= next-level 0)
            ""
            (simple-format
             #f
             "
  LIMIT ~A"
             (car level-counts))))
       (if (= next-level 0)
           ""
           (string-join
            (map
             (match-lambda*
               ((level count)
                (simple-format
                 #f
                 ", l~A AS (
  (
    SELECT derivation_outputs.derivation_id
    FROM derivation_outputs WHERE derivation_outputs.id IN (
      SELECT DISTINCT derivation_inputs.derivation_output_id
      FROM l~A
      INNER JOIN derivation_inputs
        ON l~A.derivation_id = derivation_inputs.derivation_id
    )
  ) EXCEPT (~A
  )~A
)"
                 level
                 (- level 1)
                 (- level 1)
                 (string-join
                  (map
                   (lambda (level)
                     (simple-format
                      #f
                      "      SELECT derivation_id FROM l~A"
                      level))
                   (iota level))
                  "
    UNION ALL")
                 (if count
                     (simple-format
                      #f
                      "
  LIMIT ~A"
                      count)
                     ""))))
             (iota (length level-counts) 1)
             (append (cdr level-counts) '(#f)))
            ""))
       (simple-format
        #f
        "
SELECT COUNT(*) FROM l~A"
        (length level-counts))))

    (string->number
     (caar
      (exec-query
       conn
       query))))

  (define (insert-level-count system-id target level count)
    (exec-query
     conn
     "
INSERT INTO guix_revision_package_derivation_distribution_counts
VALUES ($1, $2, $3, $4, $5)"
     (list guix-revision-id
           system-id
           target
           (number->string level)
           (number->string count))))

  (for-each
   (match-lambda
     ((system-id target)

      (let loop ((level-counts '()))
        (let ((level (length level-counts))
              (count (get-count-for-next-level system-id target level-counts)))
          (unless (= count 0)
            (insert-level-count system-id target level count)
            (loop (append level-counts (list count))))))))
   system-ids-and-targets))

(define (backfill-guix-revision-package-derivation-distribution-counts conn)
  (define revision-ids
    (map
     car
     (exec-query
      conn
      "
  SELECT id
  FROM guix_revisions
EXCEPT
  SELECT guix_revision_id
  FROM guix_revision_package_derivation_distribution_counts
ORDER BY id DESC")))

  (for-each
   (lambda (revision-id)
     (simple-format #t "backfilling guix_revision_package_derivation_distribution_counts for revision ~A\n" revision-id)
     (with-postgresql-transaction
      conn
      (lambda (conn)
        (insert-guix-revision-package-derivation-distribution-counts
         conn
         revision-id))))
   revision-ids))

(define* (get-sql-to-select-package-and-related-derivations-for-revision
          conn
          guix-revision-id
          #:key system-id target)
  (define level-counts
    (map
     (match-lambda
       ((level count)
        (list
         (string->number level)
         (string->number count))))
     (exec-query
      conn
      "
SELECT level, distinct_derivations
FROM guix_revision_package_derivation_distribution_counts
WHERE guix_revision_id = $1
  AND system_id = $2
  AND target = $3
ORDER BY level ASC"
      (list guix-revision-id
            (number->string system-id)
            target))))

  (define (query level-counts)
    (string-append
     (simple-format
      #f
      "
WITH l0 AS (
  SELECT derivation_id
  FROM package_derivations
  INNER JOIN guix_revision_package_derivations
    ON package_derivations.id
        = guix_revision_package_derivations.package_derivation_id
  WHERE revision_id = ~A
    AND system_id = ~A
    AND target = $STR$~A$STR$
  LIMIT ~A
)"
      guix-revision-id
      system-id
      target
      (cdr (car level-counts)))
     (string-join
      (map
       (match-lambda*
         ((level count)
          (simple-format
           #f
           ", l~A AS (
  (
    SELECT derivation_outputs.derivation_id
    FROM derivation_outputs WHERE derivation_outputs.id IN (
      SELECT DISTINCT derivation_inputs.derivation_output_id
      FROM l~A
      INNER JOIN derivation_inputs
        ON l~A.derivation_id = derivation_inputs.derivation_id
    )
  ) EXCEPT (~A
  )~A
)"
           level
           (- level 1)
           (- level 1)
           (string-join
            (map
             (lambda (level)
               (simple-format
                #f
                "      SELECT derivation_id FROM l~A"
                level))
             (iota level))
            "
    UNION ALL")
           (simple-format
            #f
            "
  LIMIT ~A"
            count))))
       (iota (- (length level-counts) 1) 1)
       (cdr (map cdr level-counts)))
      "")
     ", all_derivations AS (
  SELECT *
  FROM l0"
     (string-join
      (map (lambda (level)
             (simple-format #f "  UNION (SELECT * FROM l~A)" level))
           (iota (- (length level-counts) 1) 1))
      "\n")
     "
)"))

  (if level-counts
      (query level-counts)
      #f))