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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
|
// file : mod/mod-builds.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <mod/mod-builds.hxx>
#include <set>
#include <libstudxml/serializer.hxx>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <libbutl/utility.hxx> // compare_c_string
#include <libbutl/timestamp.hxx> // to_string()
#include <libbutl/path-pattern.hxx>
#include <libbbot/manifest.hxx> // to_result_status(), to_string(result_status)
#include <web/server/module.hxx>
#include <web/server/mime-url-encoding.hxx>
#include <web/xhtml/serialization.hxx>
#include <libbrep/build.hxx>
#include <libbrep/build-odb.hxx>
#include <libbrep/build-package.hxx>
#include <libbrep/build-package-odb.hxx>
#include <mod/page.hxx>
#include <mod/module-options.hxx>
using namespace std;
using namespace butl;
using namespace web;
using namespace odb::core;
using namespace brep::cli;
// While currently the user-defined copy constructor is not required (we don't
// need to deep copy nullptr's), it is a good idea to keep the placeholder
// ready for less trivial cases.
//
brep::builds::
builds (const builds& r)
: database_module (r),
build_config_module (r),
options_ (r.initialized_ ? r.options_ : nullptr)
{
}
void brep::builds::
init (scanner& s)
{
HANDLER_DIAG;
options_ = make_shared<options::builds> (
s, unknown_mode::fail, unknown_mode::fail);
if (options_->build_config_specified ())
{
database_module::init (*options_, options_->build_db_retry ());
build_config_module::init (*options_);
}
if (options_->root ().empty ())
options_->root (dir_path ("/"));
}
// Transform the wildcard to the SIMILAR TO-pattern.
//
static string
transform (const string& pattern)
{
if (pattern.empty ())
return "%";
string r;
for (const path_pattern_term& pt: path_pattern_iterator (pattern))
{
switch (pt.type)
{
case path_pattern_term_type::question: r += '_'; break;
case path_pattern_term_type::star: r += '%'; break;
case path_pattern_term_type::bracket:
{
// Copy the bracket expression translating the inverse character, if
// present.
//
size_t n (r.size ());
r.append (pt.begin, pt.end);
if (r[n + 1] == '!') // ...[!... ?
r[n + 1] = '^';
break;
}
case path_pattern_term_type::literal:
{
char c (get_literal (pt));
// Escape the special characters.
//
// Note that '.' is not a special character for SIMILAR TO.
//
switch (c)
{
case '\\':
case '%':
case '_':
case '|':
case '+':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']': r += '\\'; break;
}
r += c;
break;
}
}
}
return r;
}
template <typename T, typename C>
static inline query<T>
match (const C qc, const string& pattern)
{
return qc + "SIMILAR TO" + query<T>::_val (transform (pattern));
}
// If tenant is absent, then query builds from all the public tenants.
//
template <typename T>
static inline query<T>
build_query (const brep::vector<brep::build_target_config_id>* config_ids,
const brep::params::builds& params,
const brep::optional<brep::string>& tenant,
const brep::optional<bool>& archived)
{
using namespace brep;
using query = query<T>;
using qb = typename query::build;
using qt = typename query::build_tenant;
const auto& pid (qb::id.package);
query q (tenant ? pid.tenant == *tenant : !qt::private_);
if (archived)
q = q && qt::archived == *archived;
if (config_ids != nullptr)
{
query sq (false);
for (const auto& id: *config_ids)
sq = sq || (qb::id.target == id.target &&
qb::id.target_config_name == id.config);
q = q && sq;
}
// Note that there is no error reported if the filter parameters parsing
// fails. Instead, it is considered that no package builds match such a
// query.
//
try
{
// Package name.
//
if (!params.name ().empty ())
q = q && match<T> (pid.name, params.name ());
// Package version.
//
if (!params.version ().empty () && params.version () != "*")
{
// May throw invalid_argument.
//
version v (params.version (), version::none);
q = q && compare_version_eq (pid.version,
canonical_version (v),
v.revision.has_value ());
}
// Build toolchain name/version.
//
const string& th (params.toolchain ());
if (th != "*")
{
size_t p (th.find ('-'));
if (p == string::npos) // Invalid format.
throw invalid_argument ("");
// Note that the toolchain version is selected from the list and denotes
// the exact version revision, so an absent and zero revisions have the
// same semantics and the zero revision is folded.
//
string tn (th, 0, p);
version tv (string (th, p + 1)); // May throw invalid_argument.
q = q &&
qb::id.toolchain_name == tn &&
compare_version_eq (qb::id.toolchain_version,
canonical_version (tv),
true /* revision */);
}
// Build target.
//
if (!params.target ().empty ())
q = q && match<T> (qb::id.target, params.target ());
// Build target configuration name.
//
if (!params.target_config ().empty ())
q = q && match<T> (qb::id.target_config_name, params.target_config ());
// Build package configuration name.
//
if (!params.package_config ().empty ())
q = q && match<T> (qb::id.package_config_name, params.package_config ());
// Build result.
//
const string& rs (params.result ());
if (rs != "*")
{
if (rs == "pending")
q = q && qb::force != "unforced";
else if (rs == "building")
q = q && qb::state == "building";
else
{
query sq (qb::status == rs);
// May throw invalid_argument.
//
result_status st (bbot::to_result_status (rs));
if (st != result_status::success)
{
auto next = [&st] () -> bool
{
if (st == result_status::abnormal)
return false;
st = static_cast<result_status> (static_cast<uint8_t> (st) + 1);
return true;
};
while (next ())
sq = sq || qb::status == to_string (st);
}
// Note that the result status may present for the building state as
// well (rebuild).
//
q = q && qb::state == "built" && sq;
}
}
}
catch (const invalid_argument&)
{
return query (false);
}
return q;
}
// If tenant is absent, then query packages from all the public tenants.
//
template <typename T>
static inline query<T>
package_query (const brep::params::builds& params,
const brep::optional<brep::string>& tenant,
const brep::optional<bool>& archived)
{
using namespace brep;
using query = query<T>;
using qp = typename query::build_package;
using qt = typename query::build_tenant;
query q (tenant ? qp::id.tenant == *tenant : !qt::private_);
if (archived)
q = q && qt::archived == *archived;
// Note that there is no error reported if the filter parameters parsing
// fails. Instead, it is considered that no packages match such a query.
//
try
{
// Package name.
//
if (!params.name ().empty ())
q = q && match<T> (qp::id.name, params.name ());
// Package version.
//
if (!params.version ().empty () && params.version () != "*")
{
// May throw invalid_argument.
//
version v (params.version (), version::none);
q = q && compare_version_eq (qp::id.version,
canonical_version (v),
v.revision.has_value ());
}
}
catch (const invalid_argument&)
{
return query (false);
}
return q;
}
static const vector<pair<string, string>> build_results ({
{"unbuilt", "<unbuilt>"},
{"*", "*"},
{"pending", "pending"},
{"building", "building"},
{"success", "success"},
{"warning", "warning"},
{"error", "error"},
{"abort", "abort"},
{"abnormal", "abnormal"}});
bool brep::builds::
handle (request& rq, response& rs)
{
using brep::version;
using namespace web::xhtml;
HANDLER_DIAG;
if (build_db_ == nullptr)
throw invalid_request (501, "not implemented");
const size_t page_configs (options_->build_page_entries ());
const string& host (options_->host ());
const dir_path& root (options_->root ());
const string& tenant_name (options_->tenant_name ());
params::builds params;
try
{
name_value_scanner s (rq.parameters (8 * 1024));
params = params::builds (s, unknown_mode::fail, unknown_mode::fail);
}
catch (const cli::exception& e)
{
throw invalid_request (400, e.what ());
}
const char* title ("Builds");
xml::serializer s (rs.content (), title);
s << HTML
<< HEAD
<< TITLE << title << ~TITLE
<< CSS_LINKS (path ("builds.css"), root)
//
// This hack is required to avoid the "flash of unstyled content", which
// happens due to the presence of the autofocus attribute in the input
// element of the filter form. The problem appears in Firefox and has a
// (4-year old, at the time of this writing) bug report:
//
// https://bugzilla.mozilla.org/show_bug.cgi?id=712130.
//
<< SCRIPT << " " << ~SCRIPT
<< ~HEAD
<< BODY
<< DIV_HEADER (options_->logo (), options_->menu (), root, tenant)
<< DIV(ID="content");
// If the tenant is empty then we are in the global view and will display
// builds from all the public tenants.
//
optional<string> tn;
if (!tenant.empty ())
tn = tenant;
// Return the list of distinct toolchain name/version pairs. The build db
// transaction must be started.
//
using toolchains = vector<pair<string, version>>;
auto query_toolchains = [this, &tn] () -> toolchains
{
using query = query<toolchain>;
toolchains r;
for (auto& t: build_db_->query<toolchain> (
(tn ? query::build::id.package.tenant == *tn : query (true)) +
"ORDER BY" + query::build::id.toolchain_name +
order_by_version_desc (query::build::id.toolchain_version,
false /* first */)))
r.emplace_back (move (t.name), move (t.version));
return r;
};
auto print_form = [&s, ¶ms, this] (const toolchains& toolchains,
size_t build_count)
{
// Print the package builds filter form on the first page only.
//
if (params.page () == 0)
{
// Populate the toolchains list with the distinct list of toolchain
// name/version pairs from all the existing package builds. Make sure
// the selected toolchain is still present in the database. Otherwise
// fallback to the * wildcard selection.
//
string cth ("*");
vector<pair<string, string>> toolchain_opts ({{"*", "*"}});
{
for (const auto& t: toolchains)
{
string th (t.first + '-' + t.second.string ());
toolchain_opts.emplace_back (th, th);
if (th == params.toolchain ())
cth = move (th);
}
}
// The 'action' attribute is optional in HTML5. While the standard
// doesn't specify browser behavior explicitly for the case the
// attribute is omitted, the only reasonable behavior is to default it
// to the current document URL. Note that we specify the function name
// using the "hidden" <input/> element since the action url must not
// contain the query part.
//
s << FORM
<< TABLE(ID="filter", CLASS="proplist")
<< TBODY
<< TR_INPUT ("name", "builds", params.name (), "*", true)
<< TR_INPUT ("version", "pv", params.version (), "*")
<< TR_SELECT ("toolchain", "th", cth, toolchain_opts)
<< TR_INPUT ("target", "tg", params.target (), "*")
<< TR(CLASS="tgt-config")
<< TH << "tgt config" << ~TH
<< TD
<< *INPUT(TYPE="text",
NAME="tc",
VALUE=params.target_config (),
PLACEHOLDER="*",
LIST="target-configs")
<< DATALIST(ID="target-configs")
<< *OPTION(VALUE="*");
// Print unique config names from the target config map.
//
set<const char*, butl::compare_c_string> conf_names;
for (const auto& c: *target_conf_map_)
{
if (conf_names.insert (c.first.config.get ().c_str ()).second)
s << *OPTION(VALUE=c.first.config.get ());
}
s << ~DATALIST
<< ~TD
<< ~TR
<< TR(CLASS="pkg-config")
<< TH << "pkg config" << ~TH
<< TD
<< *INPUT(TYPE="text",
NAME="pc",
VALUE=params.package_config (),
PLACEHOLDER="*")
<< ~TD
<< ~TR
<< TR_SELECT ("result", "rs", params.result (), build_results)
<< ~TBODY
<< ~TABLE
<< TABLE(CLASS="form-table")
<< TBODY
<< TR
<< TD(ID="build-count")
<< DIV_COUNTER (build_count, "Build", "Builds")
<< ~TD
<< TD(ID="filter-btn")
<< *INPUT(TYPE="submit", VALUE="Filter")
<< ~TD
<< ~TR
<< ~TBODY
<< ~TABLE
<< ~FORM;
}
else
s << DIV_COUNTER (build_count, "Build", "Builds");
};
const string& tgt (params.target ());
const string& tgt_cfg (params.target_config ());
const string& pkg_cfg (params.package_config ());
// We will not display hidden configurations, unless the configuration is
// specified explicitly.
//
bool exclude_hidden (tgt_cfg.empty () || path_pattern (tgt_cfg));
vector<build_target_config_id> conf_ids;
conf_ids.reserve (target_conf_map_->size ());
for (const auto& c: *target_conf_map_)
{
if (!exclude_hidden || belongs (*c.second, "all"))
conf_ids.push_back (c.first);
}
size_t count;
size_t page (params.page ());
if (params.result () != "unbuilt") // Print package build configurations.
{
// It seems impossible to filter out the package-excluded configuration
// builds via the database query. Thus, we will traverse through builds
// that pass the form filter and match them against expressions and
// constraints of a package they are builds of.
//
// We will calculate the total builds count and cache build objects for
// printing on the same pass. Note that we need to print the count before
// printing the builds.
//
count = 0;
vector<package_build> builds;
builds.reserve (page_configs);
// Prepare the package build prepared query.
//
using query = query<package_build>;
using prep_query = prepared_query<package_build>;
query q (build_query<package_build> (
&conf_ids, params, tn, nullopt /* archived */));
// Specify the portion. Note that we will be querying builds in chunks,
// not to hold locks for too long.
//
// Also note that for each build we also load the corresponding
// package. Nevertheless, we use a fairly large portion to speed-up the
// builds traversal but also cache the package objects (see below).
//
size_t offset (0);
// Print package build configurations ordered by the timestamp (later goes
// first).
//
q += "ORDER BY" + query::build::timestamp + "DESC" +
"OFFSET" + query::_ref (offset) + "LIMIT 500";
connection_ptr conn (build_db_->connection ());
prep_query pq (
conn->prepare_query<package_build> ("mod-builds-query", q));
// Note that we can't skip the proper number of builds in the database
// query for a page numbers greater than one. So we will query builds from
// the very beginning and skip the appropriate number of them while
// iterating through the query result.
//
size_t skip (page * page_configs);
size_t print (page_configs);
// Cache the build package objects that would otherwise be loaded multiple
// times for different configuration/toolchain combinations. Note that the
// build package is a subset of the package object and normally has a
// small memory footprint.
//
session sn;
for (bool ne (true); ne; )
{
transaction t (conn->begin ());
// Query package builds (and cache the result).
//
auto bs (pq.execute ());
if ((ne = !bs.empty ()))
{
offset += bs.size ();
// Iterate over builds and cache build objects that should be printed.
// Skip the appropriate number of them (for page number greater than
// one).
//
for (auto& pb: bs)
{
shared_ptr<build>& b (pb.build);
auto i (target_conf_map_->find (
build_target_config_id {b->target,
b->target_config_name}));
assert (i != target_conf_map_->end ());
// Match the target configuration against the package build
// configuration expressions/constraints.
//
shared_ptr<build_package> p (
build_db_->load<build_package> (b->id.package));
const build_package_config* pc (find (b->package_config_name,
p->configs));
// The package configuration should be present since the
// configurations set cannot change if the package version doesn't
// change. If that's not the case, then the database has probably
// been manually amended. In this case let's just skip such a build
// as if it excluded and log the warning.
//
if (pc == nullptr)
{
warn << "cannot find configuration '" << b->package_config_name
<< "' for package " << p->id.name << '/' << p->version;
continue;
}
if (!exclude (*pc, p->builds, p->constraints, *i->second))
{
if (skip != 0)
--skip;
else if (print != 0)
{
// As we query builds in multiple transactions we may see the
// same build multiple times. Let's skip the duplicates. Note:
// we don't increment the counter in this case.
//
if (find_if (builds.begin (), builds.end (),
[&b] (const package_build& pb)
{
return b->id == pb.build->id;
}) != builds.end ())
continue;
if (b->state == build_state::built)
{
build_db_->load (*b, b->results_section);
// Let's clear unneeded result logs for builds being cached.
//
for (operation_result& r: b->results)
r.log.clear ();
}
builds.push_back (move (pb));
--print;
}
++count;
}
}
}
//
// Print the filter form after the build count is calculated. Note:
// query_toolchains() must be called inside the build db transaction.
//
else
print_form (query_toolchains (), count);
t.commit ();
}
// Finally, print the cached package build configurations.
//
timestamp now (system_clock::now ());
// Enclose the subsequent tables to be able to use nth-child CSS selector.
//
s << DIV;
for (const package_build& pb: builds)
{
const build& b (*pb.build);
string ts (butl::to_string (b.timestamp,
"%Y-%m-%d %H:%M:%S %Z",
true /* special */,
true /* local */) +
" (" + butl::to_string (now - b.timestamp, false) + " ago");
if (pb.archived)
ts += ", archived";
ts += ')';
s << TABLE(CLASS="proplist build")
<< TBODY
<< TR_NAME (b.package_name, string (), root, b.tenant)
<< TR_VERSION (b.package_name, b.package_version, root, b.tenant)
<< TR_VALUE ("toolchain",
b.toolchain_name + '-' +
b.toolchain_version.string ())
<< TR_VALUE ("target", b.target.string ())
<< TR_VALUE ("tgt config", b.target_config_name)
<< TR_VALUE ("pkg config", b.package_config_name)
<< TR_VALUE ("timestamp", ts);
if (b.interactive) // Note: can only be present for the building state.
s << TR_VALUE ("login", *b.interactive);
s << TR_BUILD_RESULT (b, pb.archived, host, root);
// In the global view mode add the tenant builds link. Note that the
// global view (and the link) makes sense only in the multi-tenant mode.
//
if (!tn && !b.tenant.empty ())
s << TR_TENANT (tenant_name, "builds", root, b.tenant);
s << ~TBODY
<< ~TABLE;
}
s << ~DIV;
}
else // Print unbuilt package configurations.
{
// Parameters to use for package build configurations queries. Note that
// we cleanup the result filter argument, as it is irrelevant for unbuilt
// configurations.
//
params::builds bld_params (params);
bld_params.result () = "*";
// Query toolchains, filter build target configurations and toolchains,
// and create the set of target configuration/toolchain combinations, that
// we will print for package configurations. Also calculate the number of
// unbuilt package configurations.
//
toolchains toolchains;
// Target configuration/toolchain combination.
//
// Note: all members are the shallow references.
//
struct target_config_toolchain
{
const butl::target_triplet& target;
const string& target_config;
const string& toolchain_name;
const bpkg::version& toolchain_version;
};
vector<target_config_toolchain> config_toolchains;
{
transaction t (build_db_->begin ());
toolchains = query_toolchains ();
string th_name;
version th_version;
const string& th (params.toolchain ());
if (th != "*")
try
{
size_t p (th.find ('-'));
if (p == string::npos) // Invalid format.
throw invalid_argument ("");
th_name.assign (th, 0, p);
// May throw invalid_argument.
//
// Note that an absent and zero revisions have the same semantics,
// so the zero revision is folded (see above for details).
//
th_version = version (string (th, p + 1));
}
catch (const invalid_argument&)
{
// This is unlikely to be the user fault, as he selects the toolchain
// from the list.
//
throw invalid_request (400, "invalid toolchain");
}
vector<const build_target_config*> target_configs;
for (const auto& c: *target_conf_)
{
// Filter by name.
//
if ((tgt_cfg.empty () || path_match (c.name, tgt_cfg)) &&
// Filter by target.
//
(tgt.empty () || path_match (c.target.string (), tgt)) &&
(!exclude_hidden || belongs (c, "all"))) // Filter hidden.
{
target_configs.push_back (&c);
for (const auto& t: toolchains)
{
// Filter by toolchain.
//
if (th == "*" || (t.first == th_name && t.second == th_version))
config_toolchains.push_back (
target_config_toolchain {c.target, c.name, t.first, t.second});
}
}
}
if (!config_toolchains.empty ())
{
// Calculate the number of unbuilt package configurations as a
// difference between the possible number of unbuilt configurations
// and the number of existing package builds.
//
// Note that some existing builds can now be excluded by package
// configurations due to the build target configuration class set
// change. We should deduct such builds count from the number of
// existing package configurations builds.
//
// The only way to calculate both numbers is to traverse through the
// package configurations and match their build
// expressions/constraints against our target configurations.
//
size_t npos (0);
size_t ncur = build_db_->query_value<package_build_count> (
build_query<package_build_count> (
&conf_ids, bld_params, tn, false /* archived */));
// From now we will be using specific values for the below filters for
// each build database query. Note that the toolchain is the only
// filter left in bld_params.
//
bld_params.name ().clear ();
bld_params.version ().clear ();
bld_params.target ().clear ();
bld_params.target_config ().clear ();
bld_params.package_config ().clear ();
// Prepare the build count prepared query.
//
// For each package-excluded configuration we will query the number of
// existing builds.
//
using bld_query = query<package_build_count>;
using prep_bld_query = prepared_query<package_build_count>;
package_id id;
target_triplet target;
string target_config_name;
string package_config_name;
const auto& bid (bld_query::build::id);
bld_query bq (
equal<package_build_count> (bid.package, id) &&
bid.target == bld_query::_ref (target) &&
bid.target_config_name == bld_query::_ref (target_config_name) &&
bid.package_config_name == bld_query::_ref (package_config_name) &&
// Note that the query already constrains configurations via the
// configuration name and target.
//
// Also note that while the query already constrains the tenant via
// the build package id, we still need to pass the tenant not to
// erroneously filter out the private tenants.
//
build_query<package_build_count> (nullptr /* config_ids */,
bld_params,
tn,
false /* archived */));
prep_bld_query bld_prep_query (
build_db_->prepare_query<package_build_count> (
"mod-builds-build-count-query", bq));
// Number of possible builds per package configuration.
//
size_t nt (th == "*" ? toolchains.size () : 1);
// The number of packages can potentially be large, and we may
// implement some caching in the future. However, the caching will not
// be easy as the cached values depend on the filter form parameters.
//
query<buildable_package> q (
package_query<buildable_package> (params, tn, false /* archived */));
for (auto& bp: build_db_->query<buildable_package> (q))
{
id = move (bp.id);
shared_ptr<build_package> p (build_db_->load<build_package> (id));
for (const build_package_config& c: p->configs)
{
// Filter by package config name.
//
if (pkg_cfg.empty () || path_match (c.name, pkg_cfg))
{
for (const auto& tc: target_configs)
{
if (exclude (c, p->builds, p->constraints, *tc))
{
target = tc->target;
target_config_name = tc->name;
package_config_name = c.name;
ncur -= bld_prep_query.execute_value ();
}
else
npos += nt;
}
}
}
}
assert (npos >= ncur);
count = npos - ncur;
}
else
count = 0;
t.commit ();
}
// Print the filter form.
//
print_form (toolchains, count);
// Print unbuilt package configurations with the following sort priority:
//
// 1: package name
// 2: package version (descending)
// 3: package tenant
// 4: toolchain name
// 5: toolchain version (descending)
// 6: target
// 7: target configuration name
// 8: package configuration name
//
// Prepare the build package prepared query.
//
// Note that we can't skip the proper number of packages in the database
// query for a page numbers greater than one. So we will query packages
// from the very beginning and skip the appropriate number of them while
// iterating through the query result.
//
// Also note that such an approach has a security implication. An HTTP
// request with a large page number will be quite expensive to process, as
// it effectively results in traversing all the build package and all the
// built configurations. To address this problem we may consider to reduce
// the pager to just '<Prev' '1' 'Next>' links, and pass the offset as a
// URL query parameter. Alternatively, we can invent the page number cap.
//
using pkg_query = query<buildable_package>;
using prep_pkg_query = prepared_query<buildable_package>;
pkg_query pq (
package_query<buildable_package> (params, tn, false /* archived */));
// Specify the portion. Note that we will still be querying packages in
// chunks, not to hold locks for too long. For each package we will query
// its builds, so let's keep the portion small.
//
size_t offset (0);
pq += "ORDER BY" +
pkg_query::build_package::id.name +
order_by_version_desc (pkg_query::build_package::id.version,
false /* first */) + "," +
pkg_query::build_package::id.tenant +
"OFFSET" + pkg_query::_ref (offset) + "LIMIT 50";
connection_ptr conn (build_db_->connection ());
prep_pkg_query pkg_prep_query (
conn->prepare_query<buildable_package> ("mod-builds-package-query", pq));
// Prepare the build prepared query.
//
// For each package we will generate a set of all possible builds. Then,
// iterating over the actual builds for the package we will exclude them
// from the set of possible ones. The resulted set represents unbuilt
// package configurations, and so will be printed.
//
using bld_query = query<package_build>;
using prep_bld_query = prepared_query<package_build>;
package_id id;
bld_query bq (equal<package_build> (bld_query::build::id.package, id) &&
// Note that while the query already constrains the tenant
// via the build package id, we still need to pass the
// tenant not to erroneously filter out the private tenants.
//
build_query<package_build> (
&conf_ids, bld_params, tn, false /* archived */));
prep_bld_query bld_prep_query (
conn->prepare_query<package_build> ("mod-builds-build-query", bq));
size_t skip (page * page_configs);
size_t print (page_configs);
// Enclose the subsequent tables to be able to use nth-child CSS selector.
//
s << DIV;
while (print != 0)
{
transaction t (conn->begin ());
// Query (and cache) buildable packages.
//
auto packages (pkg_prep_query.execute ());
if (packages.empty ())
print = 0;
else
{
offset += packages.size ();
// Iterate over packages and print unbuilt configurations. Skip the
// appropriate number of them first (for page number greater than one).
//
for (auto& p: packages)
{
id = move (p.id);
shared_ptr<build_package> bp (build_db_->load<build_package> (id));
// Copy configuration/toolchain combinations for this package,
// skipping excluded configurations.
//
set<config_toolchain> unbuilt_configs;
for (const build_package_config& pc: bp->configs)
{
// Filter by package config name.
//
if (pkg_cfg.empty () || path_match (pc.name, pkg_cfg))
{
for (const target_config_toolchain& ct: config_toolchains)
{
auto i (
target_conf_map_->find (
build_target_config_id {ct.target, ct.target_config}));
assert (i != target_conf_map_->end ());
if (!exclude (pc, bp->builds, bp->constraints, *i->second))
unbuilt_configs.insert (
config_toolchain {ct.target,
ct.target_config,
pc.name,
ct.toolchain_name,
ct.toolchain_version});
}
}
}
// Iterate through the package configuration builds and erase them
// from the unbuilt configurations set.
//
for (const auto& pb: bld_prep_query.execute ())
{
const build& b (*pb.build);
unbuilt_configs.erase (config_toolchain {b.target,
b.target_config_name,
b.package_config_name,
b.toolchain_name,
b.toolchain_version});
}
// Print unbuilt package configurations.
//
for (const auto& ct: unbuilt_configs)
{
if (skip != 0)
{
--skip;
continue;
}
s << TABLE(CLASS="proplist build")
<< TBODY
<< TR_NAME (id.name, string (), root, id.tenant)
<< TR_VERSION (id.name, p.version, root, id.tenant)
<< TR_VALUE ("toolchain",
string (ct.toolchain_name) + '-' +
ct.toolchain_version.string ())
<< TR_VALUE ("target", ct.target.string ())
<< TR_VALUE ("tgt config", ct.target_config)
<< TR_VALUE ("pkg config", ct.package_config);
// In the global view mode add the tenant builds link. Note that
// the global view (and the link) makes sense only in the
// multi-tenant mode.
//
if (!tn && !id.tenant.empty ())
s << TR_TENANT (tenant_name, "builds", root, id.tenant);
s << ~TBODY
<< ~TABLE;
if (--print == 0) // Bail out the configuration loop.
break;
}
if (print == 0) // Bail out the package loop.
break;
}
}
t.commit ();
}
s << ~DIV;
}
string u (tenant_dir (root, tenant).string () + "?builds");
if (!params.name ().empty ())
{
u += '=';
u += mime_url_encode (params.name ());
}
auto add_filter = [&u] (const char* pn,
const string& pv,
const char* def = "")
{
if (pv != def)
{
u += '&';
u += pn;
u += '=';
u += mime_url_encode (pv);
}
};
add_filter ("pv", params.version ());
add_filter ("th", params.toolchain (), "*");
add_filter ("tg", tgt);
add_filter ("tc", tgt_cfg);
add_filter ("pc", pkg_cfg);
add_filter ("rs", params.result (), "*");
s << DIV_PAGER (page, count, page_configs, options_->build_pages (), u)
<< ~DIV
<< ~BODY
<< ~HTML;
return true;
}
|