aboutsummaryrefslogtreecommitdiff
path: root/bffe/view/activity.scm
blob: 5a97bd2f4b5f71d2af4a9fb747a578aaab893614 (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
(define-module (bffe view activity)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (ice-9 match)
  #:use-module (bffe view util)
  #:use-module ((guix store) #:select (%store-prefix))
  #:export (activity))

(define (activity title state)
  (define categorised-agents
    (let ((categories
           '(("x86_64-linux" "i686-linux")
             ("aarch64-linux" "armhf-linux")
             ("powerpc64le-linux")
             ("riscv64-linux")
             ("i586-gnu"))))
      (map
       (lambda (systems)
         (cons systems
               (sort
                (filter
                 (lambda (agent)
                   (not
                    (null?
                     (lset-intersection
                      string=?
                      (vector->list
                       (assoc-ref agent "requested_systems"))
                      systems))))
                 (vector->list
                  (assoc-ref state "agents")))
                (lambda (a b)
                  (string<? (or (assoc-ref a "name") "")
                            (or (assoc-ref b "name") ""))))))
       categories)))

  (define (abbreviate-derivation drv)
    (let ((base (string-drop
                 drv
                 (+ 1 (string-length (%store-prefix))))))
      (string-drop-right
       (string-drop base (+ 32 1))      ;32 hash part + 1 hyphen
       4)))                             ;.drv

  (define (git-commit-hash-length? str)
    (= (string-length str) 40))

  (layout
   #:title (string-append "Activity — " title)
   #:head
   `((style "
table tr:nth-child(even) {
    background-color: var(--color-bg);
}

.agents {
    display: grid;
    column-gap: 5px;
    row-gap: 5px;
    grid-template-columns: repeat(auto-fill, minmax(355px, 355px));
}

.agent {
    border: 2px solid var(--color-bg-secondary);
    border-radius: var(--border-radius);
    padding: 8px;
}

.agent h4 {
    margin-block-start: 0px;
    font-size: 22px;
}

.agent-load {
    border-width: 0px 12px 0px 0px;
    border-style: solid;
    padding-right: 3px;

    border-radius: 3px;
}

.agent-load-normal {
    border-color: #28a745;
}

.agent-load-medium {
    border-color: orange;
}

.agent-load-high {
    border-color: red;
}

.agent-plan-size {
    float: right;
    clear: right;

    border-radius: 3px;
}

.build-tag {
    display: inline-block;
    padding: .2em .4em .3em;
    font-size: 77%;
    font-weight: 700;
    line-height: 1;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    border-radius: .25em;

    background-color: var(--color-secondary-accent);
}

.build {
    overflow-x: auto;
    white-space: nowrap;

    padding-top: 3px;
    padding-bottom: 4px;
    margin-bottom: 3px;
}

.build:not(:first-child) {
    border-top: 2px solid var(--color-bg-secondary);
}

.hidden {
    display: none;
}

#recent-activity {
    width: 100%;
    height: 200px;
    overflow-y: scroll;
}

"))
   #:body
   `((main
      (@ (id "main")
         (data-stateid ,(assoc-ref state "state_id")))

      (h2 "Recent activity")
      (table
       (@ (id "recent-activity"))
       (tbody
        (@ (id "recent-activity-body"))))

      (h2 "Agents")
      ,@(append-map
         (match-lambda
           ((systems . agents)
            `((h3 (@ (class "monospace"))
                  ,(string-join systems ", "))

              (div
               (@ (class "agents"))

               ,@(filter-map
                  (lambda (agent)
                    (if (assoc-ref agent "active")
                        (let ((all-builds
                               (vector->list
                                (assoc-ref agent "builds"))))
                          `(div
                            (@ (class "agent")
                               (id
                                ,(string-append
                                  "agent-"
                                  (assoc-ref agent "uuid")))
                               (data-name
                                ,(assoc-ref agent "name")))
                            ,(let ((last-status-update
                                    (assoc-ref
                                     agent
                                     "last_status_update")))
                               (if (and last-status-update
                                        (and=>
                                         (assoc-ref last-status-update
                                                    "timestamp")
                                         (lambda (timestamp)
                                           (let ((seconds
                                                  (time-second
                                                   (time-difference
                                                    (current-time time-utc)
                                                    (date->time-utc
                                                     (string->date
                                                      (string-append timestamp "Z")
                                                      "~Y-~m-~d ~H:~M:~S~z"))))))
                                             (< seconds (* 60 5))))))
                                   (let* ((1min
                                           (assoc-ref
                                            last-status-update
                                            "1min_load_average"))
                                          (cores
                                           (assoc-ref
                                            last-status-update
                                            "processor_count"))
                                          (load-percentage
                                           (/ (* 100 1min)
                                              cores)))
                                     `(span
                                       (@ (style "float: right;")
                                          (id
                                           ,(string-append
                                             "agent-"
                                             (assoc-ref agent "uuid")
                                             "-load"))
                                          (class
                                            ,(string-append
                                              "agent-load "
                                              (cond
                                               ((< load-percentage 150)
                                                "agent-load-normal")
                                               ((< load-percentage 250)
                                                "agent-load-medium")
                                               (else
                                                "agent-load-high"))))
                                          (data-value
                                           ,load-percentage))
                                       "Load: "
                                       ,(number->string
                                         (round load-percentage))
                                       "%"))
                                   `(span
                                     (@ (style "float: right;")
                                        (id
                                         ,(string-append
                                           "agent-"
                                           (assoc-ref agent "uuid")
                                           "-load"))
                                        (class "agent-load-unknown"))
                                     "Status unknown")))
                            (span
                             (@ (class "agent-plan-size")
                                (id
                                 ,(string-append
                                   "agent-"
                                   (assoc-ref agent "uuid")
                                   "-plan-size"))
                                (data-value
                                 ,(assoc-ref
                                   (assoc-ref agent "allocation_plan")
                                   "count")))
                             "Plan size: "
                             ,(assoc-ref
                               (assoc-ref agent "allocation_plan")
                               "count"))
                            (a (@ (href ,(string-append
                                          "/agent/"
                                          (assoc-ref agent "uuid"))))
                               (h4 (@ (style "display: block;"))
                                   ,(assoc-ref agent "name")))
                            (div
                             (@ (class "agent-builds")
                                (id
                                 ,(string-append
                                   "agent-"
                                   (assoc-ref agent "uuid")
                                   "-builds")))
                             ,@(map
                                (lambda (build index)
                                  (let ((tags
                                         (sort
                                          (vector->list
                                           (assoc-ref build "tags"))
                                          (lambda (a b)
                                            (string<?
                                             (assoc-ref a "key")
                                             (assoc-ref b "key"))))))
                                    `(div
                                      (@ (id
                                          ,(string-append
                                            "build-"
                                            (assoc-ref build "uuid")))
                                         (data-derivation
                                          ,(or (assoc-ref build "derivation_name")
                                               (assoc-ref build "derivation-name")))
                                         (class
                                           ,(string-append
                                             "build"
                                             (if (> index 3)
                                                 " hidden"
                                                 ""))))
                                      (a (@ (href
                                             ,(string-append
                                               "/build/"
                                               (assoc-ref build "uuid")))
                                            (style "display: block;"))
                                         (span
                                          (@ (class "monospace"))
                                          ,(abbreviate-derivation
                                            (or (assoc-ref build "derivation_name")
                                                (assoc-ref build "derivation-name")))))
                                      ,@(append-map
                                         (lambda (tag)
                                           `((span
                                              (@ (class "build-tag"))
                                              ,(assoc-ref tag "key")
                                              ": "
                                              ,(let ((val (assoc-ref
                                                           tag "value")))
                                                 (if (git-commit-hash-length? val)
                                                     (string-take val 8)
                                                     val)))
                                             (*ENTITY* nbsp)))
                                         tags))))
                                ;; TODO Sort builds by priority?
                                all-builds
                                (iota (length all-builds))))
                            (span
                             (@ (id ,(string-append
                                      "agent-"
                                      (assoc-ref agent "uuid")
                                      "-plus-x-builds"))
                                (style ,(string-append
                                         (if (> (length all-builds) 4)
                                             "display: block;"
                                             "display: none;")
                                         "margin-top: 10px; text-align: center;")))
                             ,(let ((additional-builds
                                     (- (length all-builds)
                                        4)))
                                (if (= additional-builds 1)
                                    (simple-format #f "Plus ~A other build"
                                                   additional-builds)
                                    (simple-format #f "Plus ~A other builds"
                                                   additional-builds))))
                            (span
                             (@ (id ,(string-append
                                      "agent-"
                                      (assoc-ref agent "uuid")
                                      "-no-allocated-builds"))
                                (style ,(string-append
                                         (if (= 0 (length all-builds))
                                             "display: block;"
                                             "display: none;")
                                         "margin-top: 10px; text-align: center;")))
                             "No allocated builds")))
                        #f))
                  agents)))))
         categorised-agents)

      (script (@ (src "/assets/js/activity.js")))))))