forked from sneeuwballen/benchpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchpress_server.ml
More file actions
1841 lines (1757 loc) · 58.9 KB
/
benchpress_server.ml
File metadata and controls
1841 lines (1757 loc) · 58.9 KB
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
(* run tests, or compare results *)
open Common
module T = Test
module H = Tiny_httpd
module U = Tiny_httpd.Util
module PB = PrintBox
module Log = (val Logs.src_log (Logs.Src.create "benchpress-serve"))
let spf = Printf.sprintf
let[@inline] ( let@ ) f x = f x
module Logger = struct
let show_lvl = function
| Logs.Debug -> "<7>DEBUG"
| Logs.Info -> "<6>INFO"
| Logs.Error -> "<3>ERROR"
| Logs.Warning -> "<4>WARNING"
| Logs.App -> "<5>APP"
let make_stdout () : Logs.reporter =
let app = Format.std_formatter in
let dst = Format.std_formatter in
let pp_header out (lvl, src) : unit =
let src =
match src with
| None -> ""
| Some s -> spf "[%s]" s
in
Fmt.fprintf out "%s%s: " (show_lvl lvl) src
in
Logs.format_reporter ~pp_header ~app ~dst ()
let setup (lvl : Logs.level option) =
let m = Mutex.create () in
Logs.set_reporter_mutex
~lock:(fun () -> Mutex.lock m)
~unlock:(fun () -> Mutex.unlock m);
Logs.set_level ~all:true lvl;
Logs.set_reporter @@ make_stdout ()
end
type expect_filter =
| TD_expect_improved
| TD_expect_ok
| TD_expect_disappoint
| TD_expect_bad
| TD_expect_error
type t = {
mutable defs: Definitions.t;
server: H.t;
task_q: Task_queue.t;
data_dir: string;
meta_cache: Meta_cache.t;
allow_delete: bool;
}
(** {2 printbox -> html} *)
module PB_html : sig
open Tiny_httpd_html
val style : elt
val to_html : PrintBox.t -> elt
end = struct
module B = PrintBox
module H = Tiny_httpd_html
module A = H.A
let style =
let l =
[
"table.framed { border: 2px solid black; }";
"table.framed th, table.framed td { border: 1px solid black; }";
"th, td { padding: 3px; }";
]
in
H.style [] (CCList.map H.txt l)
let attrs_of_style (s : B.Style.t) : _ list * _ =
let open B.Style in
let { bold; bg_color; fg_color; _ } = s in
let encode_color = function
| Red -> "red"
| Blue -> "blue"
| Green -> "green"
| Yellow -> "yellow"
| Cyan -> "cyan"
| Black -> "black"
| Magenta -> "magenta"
| White -> "white"
in
let s =
(match bg_color with
| None -> []
| Some c -> [ "background-color", encode_color c ])
@
match fg_color with
| None -> []
| Some c -> [ "color", encode_color c ]
in
let a =
match s with
| [] -> []
| s ->
[
A.style @@ String.concat ";"
@@ CCList.map (fun (k, v) -> k ^ ": " ^ v) s;
]
in
a, bold
let rec to_html_rec (b : B.t) : H.elt =
match B.view b with
| B.Empty -> H.span [] []
| B.Text { l; style } ->
let a, bold = attrs_of_style style in
let l = CCList.map H.txt l in
let l =
if bold then
CCList.map (fun x -> H.b [] [ x ]) l
else
l
in
H.div a l
| B.Pad (_, b) | B.Frame b -> to_html_rec b
| B.Align { h = `Right; inner = b; v = _ } ->
H.div [ A.class_ "align-right" ] [ to_html_rec b ]
| B.Align { h = `Center; inner = b; v = _ } ->
H.div [ A.class_ "center" ] [ to_html_rec b ]
| B.Align { inner = b; _ } -> to_html_rec b
| B.Grid (bars, a) ->
let class_ =
match bars with
| `Bars -> "table-bordered framed"
| `None -> "table-borderless"
in
let to_row a =
Array.to_list a
|> CCList.map (fun b -> H.td [ A.class_ "thead" ] [ to_html_rec b ])
|> fun x -> H.tr [] x
in
let rows = Array.to_list a |> CCList.map to_row in
H.table [ A.class_ @@ "table table-hover table-striped " ^ class_ ] rows
| B.Tree (_, b, l) ->
let l = Array.to_list l in
H.div []
[
to_html_rec b;
H.ul [] (CCList.map (fun x -> H.li [] [ to_html_rec x ]) l);
]
| B.Link { uri; inner } ->
H.div [] [ H.a [ A.class_ "btn-link"; A.href uri ] [ to_html_rec inner ] ]
| _ ->
(* catch-all to be more resilient to newer versions of printbox *)
H.div [] [ H.pre [] [ H.txt @@ PrintBox_text.to_string b ] ]
(* remaining cases *)
[@@warning "-11"]
let to_html b = H.div [] [ to_html_rec b ]
end
module Html = struct
include Tiny_httpd_html
let b_style = link [ A.rel "stylesheet"; A.href "/css/" ]
let mk_page_ ?meta:(my_meta = []) ~title:my_title my_body =
html []
[
head []
[
title [] [ txt my_title ];
b_style;
PB_html.style;
link [ A.rel "icon"; A.href "/favicon.png" ];
meta (A.charset "utf-8" :: my_meta);
meta
[
A.name "viewport";
A.content "width=device-width, initial-scale=1";
];
script [ A.src "/js"; "type", "module" ] [ txt "" ];
script [ A.src "https://unpkg.com/htmx.org@1.7.0" ] [ txt "" ];
];
body [] [ my_body ];
]
let mk_page ?meta ~title my_body =
mk_page_ ?meta ~title @@ div [ A.class_ "container" ] my_body
let mk_page' ?meta ~title my_body =
mk_page_ ?meta ~title @@ div' [ A.class_ "container" ] my_body
let div1 a x = div a [ x ]
let mk_a ?(cls = "btn-link") al x = a (A.class_ ("btn " ^ cls) :: al) x
let mk_row ?(cls = "") al x = div ((A.class_ @@ "row " ^ cls) :: al) x
let mk_col ?(cls = "") al x = div ((A.class_ @@ "col " ^ cls) :: al) x
let mk_li al x = li (A.class_ "list-group-item" :: al) x
let mk_ul al l = ul (A.class_ "list-group" :: al) l
let mk_button ?(cls = "") al x =
button (A.type_ "submit" :: A.class_ ("btn " ^ cls) :: al) x
(** [hx-…] attribute *)
let a_hx key v = "hx-" ^ key, v
let pb_html pb = div [ A.class_ "table" ] [ PB_html.to_html pb ]
let to_string_elt h = to_string ~top:false h
let to_string = to_string_top
end
let html_redirect ~href (str : string) : Html.elt =
let open Html in
mk_page
~meta:[ A.http_equiv "Refresh"; A.content (spf "0; url=%s" href) ]
~title:str
[ txt str ]
(* navigation bar *)
let mk_navigation ?(btns = []) path =
let open Html in
let path = ("/", "root", false) :: path in
div1 [ A.class_ "sticky-top container" ]
@@ nav'
[ A.class_ "breadcrumb" ]
[
sub_e
(ol [ A.class_ "breadcrumb navbar-header col-sm-6 m-1" ]
@@ CCList.map
(fun (uri, descr, active) ->
li
[
A.class_
("breadcrumb-item "
^
if active then
"active"
else
"");
]
[ mk_a [ A.href uri ] [ txt descr ] ])
path);
(if btns = [] then
`Nil
else
sub_e
(div
[
A.class_
"btn-group-vertical col-sm-1 align-items-center \
navbar-right m-2";
]
btns));
]
(* default reply headers *)
let default_html_headers =
H.Headers.([] |> set "content-type" "text/html; charset=utf-8")
let uri_show file =
Printf.sprintf "/show/%s/" (U.percent_encode ~skip:(fun c -> c = '/') file)
let uri_show_single db_file prover path =
spf "/show_single/%s/%s/%s/" (U.percent_encode db_file)
(U.percent_encode prover) (U.percent_encode path)
let link_show_single db_file prover path =
PB.link (PB.text path) ~uri:(uri_show_single db_file prover path)
let uri_get_file pb = spf "/get-file/%s/" (U.percent_encode pb)
let uri_gnuplot pb = spf "/show-gp/%s/" (U.percent_encode pb)
let uri_error_bad pb = spf "/show-err/%s/" (U.percent_encode pb)
let uri_invalid pb = spf "/show-invalid/%s/" (U.percent_encode pb)
let gnuplot_img ?(alt = "cactus plot of provers") pb =
let open Html in
img
[
A.src (uri_gnuplot pb); A.class_ "img-fluid"; "loading", "lazy"; A.alt alt;
]
let uri_show_detailed ?(offset = 0) ?(filter_prover = "") ?(filter_pb = "")
?(filter_res = "") ?(filter_expect = "") pb =
spf "/show_detailed/%s/?%s%s%s%soffset=%d" (U.percent_encode pb)
(if filter_prover = "" then
""
else
spf "prover=%s&" @@ U.percent_encode filter_prover)
(if filter_pb = "" then
""
else
spf "pb=%s&" @@ U.percent_encode filter_pb)
(if filter_res = "" then
""
else
spf "res=%s&" @@ U.percent_encode filter_res)
(if filter_expect = "" then
""
else
spf "expect=%s&" @@ U.percent_encode filter_expect)
offset
let uri_list_benchs ~off ?limit () : string =
spf "/list-benchs/?%s"
(String.concat "&"
@@ List.flatten
[
[ spf "off=%d" off ];
(match limit with
| None -> []
| Some l -> [ spf "limit=%d" l ]);
])
let enc_params ?(params = []) s =
List.fold_left
(fun s (k, v) ->
Printf.sprintf "%s&%s=%s" s (U.percent_encode k) (U.percent_encode v))
s params
let uri_prover_in file prover =
spf "/prover-in/%s/%s/" (U.percent_encode file) (U.percent_encode prover)
let uri_show_table ?params ?(offset = 0) file =
spf "/show_table/%s/?offset=%d" (U.percent_encode file) offset
|> enc_params ?params
let uri_show_csv file = spf "/show_csv/%s" (U.percent_encode file)
let link_get_file pb = PB.link (PB.text pb) ~uri:(uri_get_file pb)
exception E of Error.t * int
let fail code e = raise (E (e, code))
let failf code fmt = Fmt.kasprintf (fun s -> fail code (Error.make s)) fmt
let guardf code wrap f =
try f () with
| Error.E err -> raise (E (wrap err, code))
| E (err, code) -> raise (E (wrap err, code))
(* wrap the query to turn results into failed queries
@param f takes a chrono and a [scope] for failing *)
let query_wrap wrap (f : Misc.Chrono.t -> _) : H.Response.t =
Profile.with_ "query" @@ fun () ->
let chrono = Misc.Chrono.start () in
let f' () =
try f chrono
with Sqlite3_utils.Type_error d ->
failf 500 "db type error on %s" (Sqlite3_utils.Data.to_string_debug d)
in
match guardf 500 wrap f' with
| h ->
let code = h.H.Response.code in
let succ = code >= 200 && code < 300 in
let duration = Misc.Chrono.elapsed chrono in
Log.debug (fun k ->
k "%s (code %d) after %.3fs"
(if succ then
"successful reply"
else
"failure")
code duration);
h
| exception E (e, code) ->
let duration = Misc.Chrono.elapsed chrono in
let err = wrap e in
Log.err (fun k ->
k "error after %.3fs (code %d):\n%a" duration code Error.pp err);
H.Response.fail ~code "internal error after %.3fs:\n%s" duration
(Error.show err)
let to_str_with_errcode i err = Error.show err, i
let add_errcode i err = err, i
(* show individual files *)
let handle_show (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show" @/ string_urlencoded @/ return)
@@ fun file _req ->
let@ chrono = query_wrap (Error.wrapf "serving %s" @@ uri_show file) in
Log.debug (fun k -> k "----- start show %s -----" file);
let _file_full, cr = Bin_utils.load_file_summary ~full:false file in
Log.debug (fun k ->
k "show: loaded summary in %.3fs" (Misc.Chrono.since_last chrono));
let box_meta =
(* link to the prover locally *)
let link prover =
PB.link (PB.text prover) ~uri:(uri_prover_in file prover)
in
Test_metadata.to_printbox ~link cr.cr_meta
in
let box_summary =
Test_analyze.to_printbox_l
~link:(fun p r ->
uri_show_detailed ~filter_prover:p ~filter_expect:r file)
cr.cr_analyze
in
let box_stat =
let to_link prover tag =
uri_show_detailed ~filter_prover:prover ~filter_res:tag file
in
Test_stat.to_printbox_l ~to_link cr.cr_stat
in
(* TODO: make one table instead? with links to detailed comparison
(i.e. as-table with proper filters) *)
let box_compare_l = Test_comparison_short.to_printbox_l cr.cr_comparison in
let uri_plot = uri_gnuplot file in
let uri_err = uri_error_bad file in
let uri_invalid = uri_invalid file in
Log.debug (fun k ->
k "rendered to PB in %.3fs" (Misc.Chrono.since_last chrono));
let h =
let open Html in
mk_page' ~title:"show"
[
sub_l
[
mk_navigation [ uri_show file, "show", true ];
h3 [] [ txt file ];
mk_row []
(CCList.map
(fun x -> mk_col ~cls:"col-auto" [] [ x ])
[
mk_a ~cls:"btn-info btn-sm"
[ A.href (uri_show_detailed file) ]
[ txt "show individual results" ];
mk_a ~cls:"btn-info btn-sm"
[ A.href (uri_show_csv file) ]
[ txt "download as csv" ];
mk_a ~cls:"btn-info btn-sm"
[ A.href (uri_show_table file) ]
[ txt "show table of results" ];
]);
h3 [] [ txt "Summary" ];
div [] [ pb_html box_meta ];
h3 [] [ txt "stats" ];
div [] [ pb_html box_stat ];
h3 [] [ txt "summary" ];
mk_a ~cls:"btn-link btn-sm h-50"
[
A.href (Printf.sprintf "/show_csv/%s/" (U.percent_encode file));
]
[ txt "download as csv" ];
mk_a ~cls:"btn-link btn-sm"
[ A.href (uri_show_detailed file) ]
[ txt "see detailed results" ];
div [] [ pb_html box_summary ];
];
sub_l
[
div [ A.class_ "lazy-load"; "x_src", uri_err ] [];
div [ A.class_ "lazy-load"; "x_src", uri_invalid ] [];
img
[
A.src uri_plot;
A.class_ "img-fluid";
"loading", "lazy";
A.alt "cactus plot of provers";
];
];
(if box_compare_l = PB.empty then
`Nil
else
sub_l
[ h3 [] [ txt "comparisons" ]; div [] [ pb_html box_compare_l ] ]);
]
in
Log.debug (fun k ->
k "show: turned into html in %.3fs" (Misc.Chrono.since_last chrono));
Log.debug (fun k -> k "show: successful reply for %S" file);
H.Response.make_string ~headers:default_html_headers (Ok (Html.to_string h))
(* prover in a given file *)
let handle_prover_in (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(
exact "prover-in" @/ string_urlencoded @/ string_urlencoded @/ return)
@@ fun file p_name _req ->
let@ _chrono = query_wrap (Error.wrapf "prover-in-file/%s/%s" file p_name) in
Log.debug (fun k -> k "----- start prover-in %s %s -----" file p_name);
let@ db =
Bin_utils.with_file_as_db
~map_err:(Error.wrapf "reading file '%s'" file)
file
in
let prover = Prover.of_db db p_name in
let open Html in
let h =
mk_page ~title:"prover"
[
mk_navigation
[
uri_show file, "file", false;
uri_prover_in file p_name, "prover", true;
];
div []
[
pre [] [ txt @@ Format.asprintf "@[<v>%a@]" Prover.pp prover ];
(match prover.Prover.defined_in with
| None -> span [] []
| Some f ->
div []
[ txt "defined in"; mk_a [ A.href (uri_get_file f) ] [ txt f ] ]);
];
]
in
H.Response.make_string (Ok (Html.to_string h))
(* gnuplot for a file *)
let handle_show_gp (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show-gp" @/ string_urlencoded @/ return)
@@ fun q_arg _req ->
let@ chrono = query_wrap (Error.wrapf "serving /show-gp/%s" q_arg) in
Log.debug (fun k -> k "----- start show-gp %s -----" q_arg);
let files = CCString.split_on_char ',' q_arg |> List.map String.trim in
let files_full =
CCList.map
(fun file ->
match CCString.split_on_char '/' file with
| [ file; prover ] -> Bin_utils.mk_file_full file, Some [ prover ]
| _ -> Bin_utils.mk_file_full file, None)
files
in
let plot =
let plot =
match files_full with
| [ (f, _provers) ] -> Cactus_plot.of_file f
| fs ->
fs
|> List.mapi (fun i (file, provers) ->
guardf 500 (Error.wrapf "building cactus plot for %s" file)
@@ fun () ->
let p = Cactus_plot.of_file ?provers file in
spf "file %d (%s)" i (Filename.basename file), p)
|> Cactus_plot.combine
in
Cactus_plot.to_png plot
in
Log.info (fun k ->
k "rendered to gplot in %.3fs" (Misc.Chrono.since_last chrono));
Log.debug (fun k -> k "encode png file of %d bytes" (String.length plot));
Log.debug (fun k -> k "successful reply for show-gp/%S" q_arg);
H.Response.make_string
~headers:H.Headers.([] |> set "content-type" "image/png")
(Ok plot)
let handle_show_errors (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show-err" @/ string_urlencoded @/ return)
@@ fun file _req ->
let@ chrono = query_wrap (Error.wrapf "serving show-err/%s" file) in
Log.debug (fun k -> k "----- start show-err %s -----" file);
let _file_full, cr = Bin_utils.load_file_summary ~full:true file in
Log.debug (fun k ->
k "show-err: loaded full summary in %.3fs" (Misc.Chrono.since_last chrono));
let link_file = link_show_single file in
let bad = Test_analyze.to_printbox_bad_l ~link:link_file cr.cr_analyze in
let errors =
Test_analyze.to_printbox_errors_l ~link:link_file cr.cr_analyze
in
Log.debug (fun k ->
k "rendered to PB in %.3fs" (Misc.Chrono.since_last chrono));
let mk_dl_file l =
let open Html in
let data =
"data:text/plain;base64, " ^ Base64.encode_string (String.concat "\n" l)
in
mk_a
[ A.class_ "btn btn-link btn-sm"; A.download "problems.txt"; A.href data ]
[ txt "download list" ]
in
let h =
let open Html in
(* FIXME: only optional? *)
div' []
[
(* mk_page ~title:"show-err" @@ *)
sub_l
(CCList.flat_map
(fun (n, l, p) ->
[
h3 [] [ txt ("bad for " ^ n) ];
details
[ A.open_ "" ]
[
summary
[ A.class_ "alert alert-danger" ]
[ txt "list of bad results" ];
div [] [ mk_dl_file l; pb_html p ];
];
])
bad);
sub_l
(CCList.flat_map
(fun (n, l, p) ->
[
h3 [] [ txt ("errors for " ^ n) ];
details []
[
summary
[ A.class_ "alert alert-warning" ]
[ txt "list of errors" ];
div [] [ mk_dl_file l; pb_html p ];
];
])
errors);
]
in
Log.debug (fun k ->
k "show: turned into html in %.3fs" (Misc.Chrono.since_last chrono));
Log.debug (fun k -> k "successful reply for %S" file);
H.Response.make_string (Ok (Html.to_string_elt h))
let handle_show_invalid (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show-invalid" @/ string_urlencoded @/ return)
@@ fun file _req ->
let@ chrono = query_wrap (Error.wrapf "serving show-invalid/%s" file) in
Log.debug (fun k -> k "----- start show-invalid %s -----" file);
let _file_full, cr = Bin_utils.load_file_summary ~full:true file in
Log.debug (fun k ->
k "show-invalid: loaded full summary in %.3fs"
(Misc.Chrono.since_last chrono));
let link_file = link_show_single file in
let invalid =
Test_analyze.to_printbox_invalid_proof_l ~link:link_file cr.cr_analyze
in
Log.debug (fun k ->
k "rendered to PB in %.3fs" (Misc.Chrono.since_last chrono));
let mk_dl_file l =
let open Html in
let data =
"data:text/plain;base64, " ^ Base64.encode_string (String.concat "\n" l)
in
mk_a ~cls:"btn btn-link btn-sm"
[ A.download "problems.txt"; A.href data ]
[ txt "download list" ]
in
let h =
let open Html in
(* FIXME: only optional? *)
div []
((* mk_page ~title:"show-err" @@ *)
CCList.flat_map
(fun (n, l, p) ->
[
h3 [] [ txt ("bad for " ^ n) ];
details
[ A.open_ "" ]
[
summary
[ A.class_ "alert alert-danger" ]
[ txt "list of invalid proofs" ];
div [] [ mk_dl_file l; pb_html p ];
];
])
invalid)
in
Log.debug (fun k ->
k "show-info: turned into html in %.3fs" (Misc.Chrono.since_last chrono));
Log.debug (fun k -> k "successful reply for %S" file);
H.Response.make_string (Ok (Html.to_string_elt h))
let trf_of_string = function
| "bad" -> Some Test_top_result.TRF_bad
| "different" -> Some Test_top_result.TRF_different
| "all" -> Some Test_top_result.TRF_all
| s ->
Log.warn (fun k -> k "unknown table filter: %S" s);
None
(* show full table for a file *)
let handle_show_as_table (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show_table" @/ string_urlencoded @/ return)
@@ fun file req ->
let@ chrono = query_wrap (Error.wrapf "serving show-table/%s" file) in
let params = H.Request.query req in
Logs.debug (fun k ->
k "serving /show_table/, params=%s"
(String.concat ";"
@@ List.map (fun (x, y) -> Printf.sprintf "%s=%s" x y) params));
let offset =
try List.assoc "offset" params |> int_of_string with Not_found -> 0
in
let filter_pb = try List.assoc "pb" params with Not_found -> "" in
let filter_res =
try trf_of_string @@ List.assoc "res" params with Not_found -> None
in
let page_size = 25 in
let@ db =
Bin_utils.with_file_as_db ~map_err:(Error.wrapf "using DB '%s'" file) file
in
let full_table =
let link_res prover pb ~res =
PB.link ~uri:(uri_show_single file prover pb) (PB.text res)
in
Test_top_result.db_to_printbox_table ?filter_res ~filter_pb ~offset
~link_pb:link_get_file ~page_size ~link_res db
in
Log.debug (fun k ->
k "loaded table[offset=%d] in %.3fs" offset
(Misc.Chrono.since_last chrono));
let h =
let open Html in
(* pagination buttons *)
(* FIXME: only display next if not complete *)
let params = List.remove_assoc "offset" params in
let btns =
[
mk_a
~cls:
((if offset > 0 then
""
else
"disabled ")
^ "page-link link-sm my-1 p-1")
[
A.href
(uri_show_table ~params ~offset:(max 0 (offset - page_size)) file);
]
[ txt "prev" ];
mk_a ~cls:"page-link link-sm my-1 p-1"
[ A.href (uri_show_table ~params ~offset:(offset + page_size) file) ]
[ txt "next" ];
]
in
mk_page ~title:"show full table"
[
mk_navigation ~btns
[
uri_show file, "file", false;
( uri_show_table file,
(if offset = 0 then
"full"
else
spf "full[%d..]" offset),
true );
];
div
[ A.class_ "container-fluid" ]
[
form
[
A.action (uri_show_table file);
A.method_ "GET";
A.class_ "form-row form-inline";
]
[
input
[
A.name "pb";
A.class_ "form-control form-control-sm m-3 p-3";
A.value filter_pb;
A.placeholder "problem";
A.type_ "text";
];
select
[ A.name "res"; A.class_ "form-control select m-3" ]
(List.map
(fun trf ->
let sel =
if Some trf = filter_res then
[ A.selected "" ]
else
[]
in
let s = Test_top_result.string_of_trf trf in
option (sel @ [ A.value s ]) [ txt s ])
[ Test_top_result.TRF_all; TRF_bad; TRF_different ]);
mk_button ~cls:"btn-info btn-sm btn-success m-3" []
[ txt "filter" ];
];
];
h3 [] [ txt "full results" ];
div [] [ pb_html full_table ];
]
in
Log.debug (fun k -> k "successful reply for %S" file);
H.Response.make_string (Ok (Html.to_string h))
(* html for the summary of [file] with metadata [m] *)
let mk_file_summary filename (m : Test_metadata.t) : Html.elt list =
let open Html in
let add_title =
let title = [ A.title (Test_metadata.to_string m) ] in
let url_show = uri_show filename in
fun x -> mk_a (A.href url_show :: title) [ x ]
in
let nres =
let hd = add_title @@ txt (spf "%d res" m.n_results)
and tl =
if m.n_bad > 0 then
[ span [ A.class_ "badge bg-danger" ] [ txt (spf "%d bad" m.n_bad) ] ]
else
[]
in
span [ A.class_ "col-md-3" ] (hd :: tl)
and provers =
span
[ A.class_ "col-md-3" ]
[ txt (spf "{%s}" @@ String.concat "," m.provers) ]
and date =
span
[ A.class_ "col-md-3 text-secondary" ]
[
txt
@@ CCOpt.map_or ~default:"<unknown date>" Misc.human_datetime
m.timestamp;
]
and dirs =
if CCList.is_empty m.dirs then
[]
else (
let title = String.concat "\n" m.dirs in
[
span
[ A.title title ]
[
txt @@ spf "dirs {%s}" @@ String.concat ","
@@ List.map (Misc.truncate_left 10) m.dirs;
];
]
)
in
let fields = List.flatten [ [ nres; provers; date ]; dirs ] in
fields
let l_all_expect = [ "improved"; "ok"; "disappoint"; "bad"; "error" ]
let expect_of_string s =
match String.trim s with
| "improved" -> Some Test_detailed_res.TD_expect_improved
| "ok" -> Some Test_detailed_res.TD_expect_ok
| "disappoint" -> Some Test_detailed_res.TD_expect_disappoint
| "bad" -> Some Test_detailed_res.TD_expect_bad
| "error" -> Some Test_detailed_res.TD_expect_error
| "" -> None
| e -> Error.failf "unknown 'expect' filter: %S" e
(* show list of individual results with URLs to single results for a file *)
let handle_show_detailed (self : t) : unit =
H.add_route_handler self.server ~meth:`GET
H.Route.(exact "show_detailed" @/ string_urlencoded @/ return)
@@ fun db_file req ->
let@ chrono = query_wrap (Error.wrapf "serving show_detailed/%s" db_file) in
let params = H.Request.query req in
let offset =
try List.assoc "offset" params |> int_of_string with Not_found -> 0
in
let filter_res = try List.assoc "res" params with Not_found -> "" in
let filter_expect =
try expect_of_string @@ List.assoc "expect" params with Not_found -> None
in
let filter_prover = try List.assoc "prover" params with Not_found -> "" in
let filter_pb = try List.assoc "pb" params with Not_found -> "" in
let page_size = 25 in
Log.debug (fun k ->
k "-- show detailed file=%S offset=%d pb=`%s` res=`%s` prover=`%s` --"
db_file offset filter_pb filter_res filter_prover);
let@ db =
Bin_utils.with_file_as_db
~map_err:(Error.wrapf "using DB '%s'" db_file)
db_file
in
let l, n, complete =
Test_detailed_res.list_keys ~page_size ~offset ~filter_prover ~filter_res
?filter_expect ~filter_pb db
in
Log.debug (fun k ->
k "got %d results in %.3fs, complete=%B" (List.length l)
(Misc.Chrono.elapsed chrono)
complete);
let open Html in
(* pagination buttons *)
let btns =
[
mk_a
~cls:
((if offset > 0 then
""
else
"disabled ")
^ "page-link link-sm my-1 p-1")
[
A.href
(uri_show_detailed
~offset:(max 0 (offset - page_size))
~filter_res ~filter_pb ~filter_prover db_file);
]
[ txt "prev" ];
mk_a ~cls:"page-link link-sm my-1 p-1"
[
A.href
(uri_show_detailed ~offset:(offset + page_size) ~filter_res
~filter_pb ~filter_prover db_file);
]
[ txt "next" ];
]
in
mk_page ~title:"detailed results"
@@ List.flatten
[
[
mk_navigation ~btns
[
uri_show db_file, "file", false;
( uri_show_detailed db_file,
(if offset = 0 then
"detailed"
else
spf "detailed [%d..%d]" offset (offset + List.length l - 1)),
true );
];
div
[ A.class_ "container" ]
[
h2 [] [ txt (spf "detailed results (%d total)" n) ];
div [ A.class_ "navbar navbar-expand-lg" ]
@@ [
div [ A.class_ "container-fluid" ]
@@ [
form
[
A.action (uri_show_detailed db_file);
A.method_ "GET";
A.class_ "form-row form-inline";
]
[
input
[
A.name "prover";
A.class_ "form-control form-control-sm m-1 p-1";
A.value filter_prover;
A.placeholder "prover";
A.type_ "text";
];
input
[
A.name "pb";
A.class_ "form-control form-control-sm m-1 p-1";
A.value filter_pb;
A.placeholder "problem";
A.type_ "text";
];
input
[
A.name "res";
A.class_ "form-control form-control-sm m-1 p-1";
A.value filter_res;
A.placeholder "result";
A.type_ "text";
];
input
[
A.name "expect";
A.class_ "form-control form-control-sm m-1 p-1";
A.value
(try List.assoc "expect" params
with _ -> "");
A.list "expect_l";
];
mk_button
~cls:"btn-info btn-sm btn-success m-1 p-1" []
[ txt "filter" ];
];
datalist
[ A.id "expect_l"; A.class_ "datalist m-1" ]
(List.map
(fun v -> option [ A.value v ] [ txt v ])
l_all_expect);
];
];
];
(let rows =
CCList.map
(fun {
Test_detailed_res.prover;
file = pb_file;
res;
file_expect;
rtime;
} ->
let url_file_res = uri_show_single db_file prover pb_file in
let url_file = uri_get_file pb_file in
tr []
[
td [] [ txt prover ];
td []
[
mk_a
[ A.href url_file_res; A.title pb_file ]
[ txt pb_file ];
mk_a
[ A.href url_file; A.title pb_file ]
[ txt "(content)" ];