package sihl-queue

  1. Overview
  2. Docs

Source file admin_ui.ml

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
open Tyxml

let cancel scope csrf id =
  let path = Format.sprintf "%s/%s/cancel" scope id in
  [%html
    {|
<form style="display: inline;" action="|}
      path
      {|" method="Post">
  <input type="hidden" name="csrf" value="|}
      csrf
      {|">
  <input type="hidden" id="|}
      id
      {|">
  <button type="submit" class="sihl-admin-ui-table-row-cancel sihl-admin-ui-queue-table-row-cancel sihl-admin-ui-queue-table-button">Cancel</button>
</form>
|}]
;;

let requeue scope csrf id =
  let path = Format.sprintf "%s/%s/requeue" scope id in
  [%html
    {|
<form style="display: inline;" action="|}
      path
      {|" method="Post">
  <input type="hidden" name="csrf" value="|}
      csrf
      {|">
  <input type="hidden" id="|}
      id
      {|">
  <button type="submit" class="sihl-admin-ui-table-row-requeue sihl-admin-ui-queue-table-row-requeue sihl-admin-ui-queue-table-button">Re-queue</button>
</form>
|}]
;;

let status scope csrf (job : Sihl.Contract.Queue.instance) =
  let now = Ptime_clock.now () in
  match job.status with
  | Sihl.Contract.Queue.Succeeded ->
    [%html
      {|<div><span class="sihl-admin-ui-table-row-success sihl-admin-ui-queue-table-row-success">Succeeded</span>|}
        [ requeue scope csrf job.id ]
        {|</div>|}]
  | Sihl.Contract.Queue.Failed ->
    [%html
      {|<div><span class="sihl-admin-ui-table-row-failed sihl-admin-ui-queue-table-row-failed">Failed</span>|}
        [ requeue scope csrf job.id ]
        {|</div>|}]
  | Sihl.Contract.Queue.Cancelled ->
    [%html
      {|<div><span class="sihl-admin-ui-table-row-failed sihl-admin-ui-queue-table-row-failed">Cancelled</span>|}
        [ requeue scope csrf job.id ]
        {|</div>|}]
  | Sihl.Contract.Queue.Pending ->
    let next_try_in =
      if Ptime.is_earlier now ~than:job.next_run_at
      then
        Some
          (Ptime.Span.round
             ~frac_s:0
             (Ptime.Span.sub
                (Ptime.to_span job.next_run_at)
                (Ptime.to_span now)))
      else None
    in
    let next_try_in =
      next_try_in
      |> Option.map (Format.asprintf "%a" Ptime.Span.pp)
      |> Option.value ~default:"0s"
      |> Format.sprintf "Next try in: %s"
    in
    [%html
      {|<div><span class="sihl-admin-ui-table-row-pending sihl-admin-ui-queue-table-row-pending">|}
        [ Html.txt next_try_in ]
        {|</span>|}
        [ cancel scope csrf job.id ]
        {|</div>|}]
;;

let pre_style =
  {|
    white-space: pre-wrap;
    white-space: -moz-pre-wrap;
    white-space: -pre-wrap;
    white-space: -o-pre-wrap;
    word-wrap: break-word;
|}
;;

let row scope csrf (job : Sihl.Contract.Queue.instance) =
  let last_error_at =
    job.last_error_at
    |> Option.map Ptime.to_rfc3339
    |> Option.value ~default:"Never"
  in
  let status_class =
    match job.status with
    | Sihl.Contract.Queue.Succeeded -> "succeeded"
    | Sihl.Contract.Queue.Failed -> "failed"
    | Sihl.Contract.Queue.Cancelled -> "cancelled"
    | Sihl.Contract.Queue.Pending -> "pending"
  in
  let classes =
    [ "sihl-admin-ui-table-body-row"
    ; "sihl-admin-ui-queue-table-body-row"
    ; Format.asprintf
        "sihl-admin-ui-queue-table-body-row-status-%s"
        status_class
    ]
  in
  let input =
    if String.equal job.input ""
    then
      [%html
        {|<td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
          [ Html.txt "" ]
          {| </td>|}]
    else
      [%html
        {|<td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell"><pre style="|}
          pre_style
          {|">|}
          [ Html.txt job.input ]
          {|</pre></td>|}]
  in
  [%html
    {|
<tr class="|}
      classes
      {|">
      <td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
      [ Html.txt job.id ]
      {|</td>
      <td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
      [ Html.txt job.name ]
      {|</td>|}
      [ input ]
      {|<td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
      [ Html.txt (Format.sprintf "%d/%d" job.tries job.max_tries) ]
      {|</td>
      <td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell"><pre style="|}
      pre_style
      {|">|}
      [ Html.txt (Option.value ~default:"" job.last_error) ]
      {|</pre>
      </td>
      <td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
      [ Html.txt last_error_at ]
      {|
      </td>
      <td class="sihl-admin-ui-table-body-cell sihl-admin-ui-queue-table-body-cell">|}
      [ status scope csrf job ]
      {|</td>
</tr>
|}]
;;

let table scope csrf (jobs : Sihl.Contract.Queue.instance list) =
  let path = Format.sprintf "%s/html/index" scope in
  [%html
    {|
<table data-hx-get="|}
      path
      {|" data-hx-trigger="every 5s" data-hx-swap="outerHTML" class="sihl-admin-ui-table sihl-admin-ui-queue-table">
  <thead class="sihl-admin-ui-table-header sihl-admin-ui-queue-table-header">
    <tr class="sihl-admin-ui-table-header-row sihl-admin-ui-queue-table-header-row">
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">ID</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Job type</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Input</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Tries</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Last error</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Last error at</th>
      <th class="sihl-admin-ui-table-header-cell sihl-admin-ui-queue-table-header-cell">Status</th>
    </tr>
  </thead>
   <tbody class="sihl-admin-ui-table-body sihl-admin-ui-queue-table-body">
   |}
      (List.map (row scope csrf) jobs)
      {|
   </tbody>
</table>
|}]
;;

let base =
  [%html
    {|
body {
    font-family: sans-serif;
}
.sihl-admin-ui-queue-back {
    text-decoration: none;
    font-size: 1.5rem;
}

.sihl-admin-ui-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
}

.sihl-admin-ui-table-header-cell, .sihl-admin-ui-table-body-cell {
    text-align: left;
    padding: 12px 15px;
}

.sihl-admin-ui-queue-table-button {
    cursor: pointer;
    margin-top: 5px;
    padding: 5px 10px;
    border-radius: 0.2em;
    text-decoration: none;
    text-align: center;
    -webkit-transition: all 0.2s;
    -o-transition: all 0.2s;
    transition: all 0.2s;
}
|}]
;;

let light =
  [%html
    {|
.sihl-admin-ui-table-header-cell, .sihl-admin-ui-table-body-cell {
    border: 1px solid black;
}

.sihl-admin-ui-queue-table-header {
    background-color: #DDDDDD;
}

.sihl-admin-ui-queue-table-body-row-status-failed {
    background-color: #FFCCCC;
}

.sihl-admin-ui-queue-table-body-row-status-succeeded {
    background-color: #CCFFCC;
}

.sihl-admin-ui-queue-table-button {
    border: 1px solid black;
}
.sihl-admin-ui-queue-table-button:hover{
    color: #FFFFFF;
    background-color: #777777;
}
|}]
;;

let dark =
  [%html
    {|
body {
    background-color: #282828;
}

.sihl-admin-ui-queue-back {
    color: white;
}

.sihl-admin-ui-table-header-cell, .sihl-admin-ui-table-body-cell {
    border: 1px solid #282828;
    color: #D8D8D8;
}

.sihl-admin-ui-queue-table-header {
    background-color: #404040;
}

.sihl-admin-ui-queue-table-body-row-status-failed {
    background-color: #5d3030;
}

.sihl-admin-ui-queue-table-body-row-status-succeeded {
    background-color: #305430;
}

.sihl-admin-ui-queue-table-button {
    border: 1px solid #282828;
    background-color: #282828;
    color: white;
}
.sihl-admin-ui-queue-table-button:hover{
    color: black;
    background-color: #EEEEEE;
}
|}]
;;

let page ?back ?theme body =
  let body =
    match back with
    | Some back ->
      let back_button =
        [%html
          {|<a href="|}
            back
            {|" class="sihl-admin-ui-back sihl-admin-ui-queue-back">← Go back</a>|}]
      in
      List.cons back_button body
    | None -> body
  in
  let body =
    match theme with
    | Some `Light ->
      let theme = [%html {|<style>|} [ base; light ] {|</style>|}] in
      List.concat [ body; [ theme ] ]
    | Some `Dark ->
      let theme = [%html {|<style>|} [ base; dark ] {|</style>|}] in
      List.concat [ body; [ theme ] ]
    | Some (`Custom _) -> body
    | None ->
      let theme = [%html {|<style>|} [ base; light ] {|</style>|}] in
      List.concat [ body; [ theme ] ]
  in
  let body =
    match Sihl.Configuration.read_string "HTMX_SCRIPT_URL" with
    | Some htmx ->
      let htmx_script = [%html {|<script src="|} htmx {|"></script>|}] in
      List.concat [ body; [ htmx_script ] ]
    | None -> body
  in
  match theme with
  | Some (`Custom url) ->
    [%html
      {|
       <!doctype html>
       <html lang="en">
         <head>
           <meta charset="UTF-8"/>
           <meta name="viewport" content="width=device-width, initial-scale=1">
           <link href="|}
        url
        {|" rel="stylesheet">
           <title>Hello world!</title>
         </head>
           <body>|}
        body
        {|
           </body>
       </html>
      |}]
  | _ ->
    [%html
      {|
       <!doctype html>
       <html lang="en">
         <head>
           <meta charset="UTF-8"/>
           <meta name="viewport" content="width=device-width, initial-scale=1">
           <title>Hello world!</title>
         </head>
           <body>|}
        body
        {|
           </body>
       </html>
      |}]
;;

let index ?back ?theme scope find_jobs =
  Sihl.Web.get "" (fun req ->
      let csrf =
        match Sihl.Web.Csrf.find req with
        | Some csrf -> csrf
        | None -> failwith "No CSRF token found"
      in
      let%lwt jobs = find_jobs () in
      Lwt.return
      @@ Sihl.Web.Response.of_html (page ?back ?theme [ table scope csrf jobs ]))
;;

let html_index scope find_jobs =
  Sihl.Web.get "/html/index" (fun req ->
      let csrf =
        match Sihl.Web.Csrf.find req with
        | Some csrf -> csrf
        | None -> failwith "No CSRF token found"
      in
      let%lwt jobs = find_jobs () in
      let html =
        Format.asprintf "%a" Tyxml.Html._pp_elt (table scope csrf jobs)
      in
      Lwt.return @@ Sihl.Web.Response.of_plain_text html)
;;

let cancel scope find_job cancel_job =
  Sihl.Web.post "/:id/cancel" (fun req ->
      let id = Sihl.Web.Router.param req "id" in
      let%lwt job = find_job id in
      let%lwt _ = cancel_job job in
      Lwt.return @@ Sihl.Web.Response.redirect_to scope)
;;

let requeue scope find_job requeue_job =
  Sihl.Web.post "/:id/requeue" (fun req ->
      let id = Sihl.Web.Router.param req "id" in
      let%lwt job = find_job id in
      let%lwt _ = requeue_job job in
      Lwt.return @@ Sihl.Web.Response.redirect_to scope)
;;

let middlewares =
  [ Opium.Middleware.content_length
  ; Opium.Middleware.etag
  ; Sihl.Web.Middleware.csrf ()
  ; Sihl.Web.Middleware.flash ()
  ]
;;

let router search_jobs find_job cancel_job requeue_job ?back ?theme scope =
  Sihl.Web.choose
    ~middlewares
    ~scope
    [ index ?back ?theme scope search_jobs
    ; html_index scope search_jobs
    ; cancel scope find_job cancel_job
    ; requeue scope find_job requeue_job
    ]
;;
OCaml

Innovation. Community. Security.