blib/lib/Database/BI/Controller/Dashboard.pm

filter: Statementstmt 78.6% 5277 / 6710 Branchbran 64.3% 2693 / 4188 Conditioncond 54.9% 2192 / 3988 MC/DCmcdc 35.0% 763 / 2175 Subroutinesub 91.9% 421 / 458 Podpod 98.8% 82 / 83 totaltotal 64.9% 11428 / 17602 CC 3984 SCAR 80.9
CC
3984
Cov
68%
_render_visual_markup
38758.0
_map_data
8661.9
pivot_view
4017.3
CRAP 3256.0
↑ Summary blib/lib/Database/BI/Model/Boundary.pm »
1 package Database::BI::Controller::Dashboard;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.023.1';
7  
8 5 ▶use Mojo::Base 'Mojolicious::Controller', -strict, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use List::Util qw(min max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use CGI::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use CGI::Lingua;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Database::BI::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Database::BI::Tutorial;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Database::BI::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Mojo::JSON qw(encode_json decode_json from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Mojo::Util qw(url_escape encode xml_escape hmac_sha1_sum secure_compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Database::BI::Password ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use Sub::Protected;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # ---------------------------------------------------------------------------
35 # Constants
36 # ---------------------------------------------------------------------------
37  
38 # File extensions that Database::Abstraction can probe, in probe order.
39 # .sql, .sqlite, and .sqlite3 are all SQLite databases; .db is Berkeley DB.
40 # As many digits as a request parameter may have and still be exact in a Perl
41 # number. More than this is not a page number or a limit, it is someone probing.
42 5 Readonly my $MAX_WHOLE_NUMBER_DIGITS => 15;
43 5 Readonly my %ANALYSIS_KEYS => map { $_ => 1 } qw(id version created updated name datasets joins combines fields measures params visuals layout bookmarks hierarchies);
44 5 Readonly my $MAX_HISTOGRAM_BINS => 200;
45  
46 # What a text card on a canvas may be styled with. The fonts are ones browsers
47 # already have, so a card needs no CDN, and a stored card names a font by its
48 # key: only the CSS written here ever reaches a style attribute.
49 5 Readonly my @TEXT_FONTS => (
50 { value => '', label => 'Same as the page', css => '' },
51 { value => 'arial', label => 'Arial', css => 'Arial, Helvetica, sans-serif' },
52 { value => 'georgia', label => 'Georgia', css => 'Georgia, "Times New Roman", serif' },
53 { value => 'times', label => 'Times New Roman', css => '"Times New Roman", Times, serif' },
54 { value => 'verdana', label => 'Verdana', css => 'Verdana, Geneva, sans-serif' },
55 { value => 'trebuchet', label => 'Trebuchet MS', css => '"Trebuchet MS", Helvetica, sans-serif' },
56 { value => 'courier', label => 'Courier New', css => '"Courier New", Courier, monospace' },
57 );
58 5 Readonly my %TEXT_FONT_CSS => map { $_->{value} => $_->{css} } grep { length $_->{value} } @TEXT_FONTS;
59 5 Readonly my %TEXT_ALIGNS => map { $_ => 1 } qw(left center right);
60 5 Readonly my $MIN_TEXT_POINTS => 8;
61 5 Readonly my $MAX_TEXT_POINTS => 96;
62 5 Readonly my $MAX_TEXT_CHARS => 20_000;
63 5 Readonly my $TEXT_TITLE_CHARS => 60;
64  
65 # What HTML::D3 calls its heatmap colour scales.
66 5 Readonly my %SEQUENTIAL_SCHEMES => map { $_ => 1 } qw(YlOrRd Blues Greens Purples RdPu YlGnBu);
67 5 Readonly my $DEFAULT_SEQUENTIAL_SCHEME => 'YlOrRd';
68 5 Readonly my $DEFAULT_CATEGORICAL_SCHEME => 'tableau10';
69  
70 5 Readonly my @SUPPORTED_EXT => qw( csv db sql sqlite sqlite3 xml tsv psv xlsx json );
71 # Use \z (absolute end-of-string) not $ (which permits a trailing \n before \z).
72 # A query param decoded from "file.csv%0A" has basename "sales.csv\n"; without \z
73 # that passes the extension guard and reaches realpath with an embedded newline.
74 5 Readonly my $EXT_RE => do { my $p = join '|', @SUPPORTED_EXT; qr/\.(?:$p)\z/i };
75  
76 # Table name safe-identifier pattern -- must match DataSource's TABLE_NAME_RE
77 # exactly: first character must be a letter or underscore (not a digit), so
78 # that every table name the controller accepts can also be opened by DataSource
79 # without an unhandled croak.
80 5 Readonly my $TABLE_NAME_RE => qr/\A[A-Za-z_][A-Za-z0-9_]*\z/;
81  
82 # URL spec pattern shared by _open_spec and _spec_to_url.
83 # qr{} delimiter avoids the \/ escaping noise required by the // form.
84 # /s: makes . match \n so a percent-decoded newline inside a URL does not
85 # silently truncate the captured URL before the \z end-of-string anchor.
86 5 Readonly my $URL_SPEC_RE => qr{\Aurl:(https?://.+)\z}si;
87  
88 # Redis spec pattern: redis:tablename|redis://[password@]host[:port][/db_index]
89 # The pipe separates the table name from the connection URL so the URL's own
90 # colons and slashes do not require extra quoting.
91 5 Readonly my $REDIS_SPEC_RE => qr{\Aredis:([^|]+)\|(redis://.+)\z}si;
92  
93 # Bare Redis URL -- no table name -- used to list available keys.
94 # Excludes '|' so it cannot match the redis:table|redis://... full spec.
95 5 Readonly my $REDIS_URL_RE => qr{\Aredis://[^\s|]+\z}si;
96  
97 # MariaDB spec: mariadb:server/table, where server names an entry in the
98 # mariadb_servers configuration. A spec travels in URLs, links and saved
99 # canvases, so it holds no address or password. It has no '|', which also lets
100 # it be the right side of a j= join.
101 5 Readonly my $MARIADB_SPEC_RE => qr{\Amariadb:([A-Za-z_][A-Za-z0-9_]*)/([A-Za-z_][A-Za-z0-9_]*)\z};
102  
103 # A server with no table: lists its tables.
104 5 Readonly my $MARIADB_SERVER_RE => qr{\Amariadb:([A-Za-z_][A-Za-z0-9_]*)\z};
105  
106 # Hostname pattern accepted in /../hostname/... remote paths.
107 # Mirrors Database::Abstraction's own host validation regex.
108 # Accepts 'hostname', 'user@hostname', and IPv4 addresses (IPv6 is excluded
109 # because the [...] notation would be ambiguous in the path syntax).
110 5 Readonly my $REMOTE_HOST_RE => qr/\A(?:[a-zA-Z0-9][a-zA-Z0-9._-]*\@)?[a-zA-Z0-9][a-zA-Z0-9._-]*\z/;
111  
112 # The tile layer HTML::OSM 0.10 hardcodes. HTML::OSM >= 0.14 is required;
113 # 0.12 introduced search => 0, max_zoom, and tile_url (0.11), 0.13 the scale
114 # bar, the key on the map and fit, and 0.14 the nonce on its scripts.
115 5 Readonly my $DEFAULT_TILE_URL => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
116  
117 # The language the distribution ships templates for. It is what a request falls
118 # back to when the language it resolved has no template directory of its own;
119 # see _resolve_language. Keep it equal to Database::BI's $DEFAULT_LANGUAGE.
120 5 Readonly my $DEFAULT_LANGUAGE => 'en';
121  
122 # The floor this controller passes options for. HTML::OSM validates its
123 # constructor arguments strictly, so an older install does not ignore the new
124 # keys - it croaks on them. _map_data therefore checks the version before
125 # building a map, and reports a missing or old renderer instead of a 500.
126 # 0.13 draws the scale bar and the key on the map, listing only what is in view
127 # (scale, legend, and group on each marker), and opens on every marker (fit).
128 # 0.14 puts the Content-Security-Policy nonce on its scripts (nonce).
129 5 Readonly my $MIN_HTML_OSM_VERSION => '0.14';
130  
131 # ---------------------------------------------------------------------------
132 # I18N message dictionary for all user-visible strings in this controller.
133 # To plug in a Locale::Maketext backend, extend _i18n() below.
134 # ---------------------------------------------------------------------------
135  
136 5 Readonly my %MESSAGES => (
137 error_table_invalid => 'Invalid table name "%s"',
138 error_table_open => 'Could not open table "%s": %s',
139 error_table_missing => 'There is no table called "%s".',
140 error_file_open => 'Could not open "%s": %s',
141 error_no_path => 'No path specified',
142 error_not_found => 'File or directory not found: %s',
143 error_dir_not_found => 'Directory not found: %s',
144 error_login_required => 'Log in first: this server asks for a password.',
145 error_login_failed => 'That is not the password.',
146 error_login_locked => 'Too many wrong passwords from this address. Try again in %d minutes.',
147 error_hidden_target => 'Refused to save into "%s": a hidden file or folder (one whose name starts with a dot) holds settings, not exports.',
148 error_outside_roots => 'Refused: "%s" is outside the folders this server may use (file_roots in database_bi.conf).',
149 error_outside_export_dirs => 'Refused: exports are saved only in the export folders (export_dirs in database_bi.conf), and "%s" is not one of them.',
150 error_export_not_file => 'Refused: "%s" is already there and is not a plain file, so an export does not replace it.',
151 error_remote_needs_password => 'Refused: this server has no password, so it answers only the machine it runs on. Whoever runs it can set one with "script/database-bi password".',
152 error_cross_site => 'Refused: this request came from another site (%s). Only pages of this application can change what it keeps or write files.',
153 error_host_refused => 'Refused: this server does not answer to the name "%s". Open it as localhost or by its IP address, or add the name to allowed_hosts in database_bi.conf.',
154 error_write_failed => 'Write failed: %s',
155 error_ext_required => 'Use a .csv, .sql, or .json filename extension',
156 error_upload_none => 'No file received',
157 error_upload_ext => 'Unsupported file type. Accepted: CSV, TSV, PSV, XML, JSON, SQLite (.sql, .sqlite, .sqlite3), Berkeley DB (.db), XLSX',
158 error_upload_too_large => 'File too large (maximum %s MiB)',
159 error_upload_save => 'Could not save uploaded file',
160 error_uploads_clear => 'Could not clear the uploaded files',
161 error_export_sqlite => 'Could not build the SQLite export; the server log has the reason',
162 error_path_required => '"path" parameter is required',
163 error_url_required => 'Please enter a URL',
164 error_url_invalid => '"%s" is not a valid http:// or https:// URL',
165 error_url_fetch => 'Could not load HTML table from "%s": %s',
166 error_url_ssrf => '"%s" resolves to a private or reserved address and cannot be fetched',
167 warn_combine_sort => 'Sorting is applied in memory for /combine; result order may differ from a backend sort',
168 error_sort_column => 'Unknown sort column "%s"',
169 warn_measure_fallback => 'Measure "%s" on column "%s" is not uniformly numeric; totalled in Perl',
170 error_measure_column => 'Measure column "%s" is not in the view',
171 error_pivot_rows_required => 'At least one rows= dimension is required',
172 error_pivot_measure_req => 'At least one m= measure (col:fn:as) is required',
173 error_pivot_measure_invalid => 'Invalid measure spec "%s"; expected col:fn:as where fn is one of: %s, or p1 to p100 for a percentile',
174 error_stats_percentile => 'The percentile must be a whole number from 1 to 100; "%s" is not',
175 error_pivot_dim_not_found => 'Row/column dimension "%s" is not in the view',
176 error_pivot_dim_comma => 'Dimension name "%s" must not contain a comma',
177 error_calc_compile => 'Calculated field "%s": %s',
178 error_calc_name => 'Calculated field name "%s" is not a valid identifier',
179 error_topn_col => 'Top-N column "%s" is not a measure alias in this view',
180 warn_calc_undef => 'Calculated field "%s" returned undef for %d row(s)',
181 error_date_pattern => 'Unsupported date format pattern "%s"; allowed: %%m/%%d/%%Y, %%d/%%m/%%Y, %%d-%%b-%%Y',
182 error_date_time_pattern => 'Unsupported time format pattern "%s"; allowed: %%H:%%M, %%H:%%M:%%S',
183 error_date_as => 'as= must be "timestamp" or absent; "%s" is neither',
184 error_lookup_name => 'Lookup name "%s" is not a valid identifier',
185 error_lookup_unknown => 'Lookup "%s" is not stored for any dataset',
186 error_lookup_remote => 'Lookup "%s": "%s" is not a file; a lookup reads a file under data_dir (use Import for a URL)',
187 error_lookup_outside => 'Lookup "%s": "%s" is not under data_dir',
188 error_lookup_not_found => 'Lookup "%s": file "%s" not found, or not a supported data file',
189 error_lookup_too_large => 'Lookup "%s": "%s" is %d bytes, more than a lookup may be (%d)',
190 error_lookup_unreadable => 'Lookup "%s": cannot read "%s": %s',
191 error_lookup_key_unknown => 'Lookup "%s": key column "%s" is not in "%s"',
192 error_lookup_field_unknown => 'Lookup "%s": field "%s" is not in "%s"',
193 error_lookup_col_unknown => 'Lookup "%s": column "%s" is not in this dataset (nolk=%s skips the lookup)',
194 error_lookup_name_taken => 'Lookup "%s": field "%s" cannot be added, because "%s" is a column already',
195 warn_lookup_duplicate_key => 'Lookup "%s": %d duplicate key(s) in "%s"; the first row of each is used',
196 error_extract_name => 'Extraction name "%s" is not a valid identifier',
197 error_extract_pattern => 'Extraction "%s": %s',
198 error_extract_no_names => 'Extraction "%s": the pattern names no group; write one as (?<name>...) for each column',
199 error_extract_field => 'Extraction "%s": the pattern has no group named "%s"',
200 error_extract_col_unknown => 'Extraction "%s": column "%s" is not in this dataset',
201 error_extract_name_taken => 'Extraction "%s": group "%s" cannot be added, because "%s" is a column already',
202 error_accel_spec => 'Cannot accelerate "%s": it must be a table: or path: spec naming a local file that exists',
203 error_accel_sqlite => 'Cannot accelerate "%s": it is a SQLite database already',
204 error_accel_open => 'Cannot accelerate "%s": the file cannot be opened: %s',
205 error_accel_store => 'Cannot accelerate "%s": the model store cannot be written (model_dir inside data_dir, or not writable)',
206 error_accel_build => 'Cannot accelerate "%s": %s',
207 error_analysis_not_found => 'Analysis "%s" not found',
208 error_analysis_invalid => 'Invalid analysis spec: %s',
209 error_analysis_version => 'Unsupported analysis version %s; please upgrade the application',
210 error_analysis_storage => 'Analysis storage is not writable; use import/export instead',
211 error_analysis_json => 'Invalid JSON body: %s',
212 error_analysis_id_invalid => 'Invalid analysis id',
213 error_text_card_empty => 'A text card needs some text',
214 error_text_card_long => 'A text card can hold at most %d characters',
215 error_text_card_font => 'That font is not one of the choices',
216 error_text_card_size => 'The size must be a whole number of points from %d to %d',
217 error_text_card_colour => 'A colour must be written #rrggbb',
218 error_text_card_align => 'The alignment must be left, center or right',
219 error_text_card_options => 'options must be given as an object',
220 error_text_card_columns => 'A text card has no columns to change',
221 error_text_card_no_data => 'A text card has no data to download',
222 error_visual_not_text => 'Only a text card can be removed',
223 error_param_range => "Parameter '%s' is out of range (min %s, max %s)",
224 error_param_type => "Parameter '%s' must be a number",
225 error_map_source => 'A map needs loc=<column> to geocode, or both lat= and lon= coordinate columns',
226 error_map_coordinates => 'A map with lat= needs lon= as well, and a map with lon= needs lat=',
227 error_map_loc_column => 'Column "%s" is not in this view, so there is nothing to geocode',
228 error_map_coordinate_range => 'Column "%s" holds only values outside the range of %s; check that lat= and lon= are not swapped',
229 error_map_coordinate_numeric => 'Columns "%s" and "%s" hold no numeric coordinate pairs; a map needs numbers, or loc=<column> to geocode places',
230 error_map_no_markers => 'No row has a location that could be plotted',
231 error_map_renderer_missing => 'Drawing a map needs HTML::OSM 0.14 or later, and it is not installed',
232 error_map_renderer_failed => 'The map could not be drawn; the server log has the reason',
233 error_map_renderer_old => 'Drawing a map needs HTML::OSM 0.14 or later, but the installed HTML::OSM is %s',
234 warn_join_schema_mismatch => 'Column "%s" has type %s in %s but %s in %s; the join keeps both columns, so check which one a calculation uses',
235 warn_map_coordinate_rows => 'Plotted %d of %d rows; %d hold no usable coordinate',
236 warn_map_markers_capped => 'Showing the first %d of %d markers; add f= filters to see the rest',
237 map_legend_empty => 'Nothing in this view',
238 map_bounds_layer => '%s boundaries',
239 map_bounds_noun_country => 'country',
240 map_bounds_noun_state => 'state',
241 map_bounds_noun_region => 'region',
242 map_bounds_noun_county => 'county',
243 map_bounds_noun_city => 'city',
244 map_bounds_noun_town => 'town',
245 map_bounds_noun_neighbourhood => 'neighbourhood',
246 map_bounds_noun_district => 'district',
247 map_bounds_noun_province => 'province',
248 map_bounds_noun_prefecture => 'prefecture',
249 map_bounds_noun_department => 'department',
250 map_bounds_noun_area => 'area',
251 map_bounds_total => '%s: %s %s',
252 map_bounds_row => '%s: 1 row',
253 map_bounds_rows => '%s: %s rows',
254 map_bounds_key_total => '%s in each %s',
255 map_bounds_key_rows => 'Rows in each %s',
256 map_bounds_key_range => '%s to %s',
257 warn_map_cache_unwritten => 'The resolved places could not be remembered in the model store, so the next request will look them up again',
258 error_map_tiles_unknown => 'Unknown tile source "%s"; define it under tile_sources in database_bi.conf',
259 error_map_geocode_mode => 'geocode= must be "wait" (look places up before the page is sent) or "cached" (look nothing up), not "%s"',
260 error_geo_lookup_place => 'A lookup needs place= and the sig= the map page gave it',
261 error_geo_lookup_unsigned => 'This place was not listed by a map page, or the server has restarted since; open the map again',
262 error_map_loc_text => 'loc_text= is the text added to the end of each place, at most %d characters on one line, not "%s"',
263 error_map_bounds_unknown => 'bounds= must be one of %s, not "%s"',
264 warn_map_bounds_pending => '%d of %d places have no %s outline yet',
265 warn_map_bounds_pending_one => '%d of %d places has no %s outline yet',
266 warn_map_bounds_offline => '%d of %d places have no %s outline: outlines come from OpenStreetMap, which this server does not ask (map_geocode_online is off, or LWP::UserAgent::Throttled is not installed)',
267 warn_map_bounds_offline_one => '%d of %d places has no %s outline: outlines come from OpenStreetMap, which this server does not ask (map_geocode_online is off, or LWP::UserAgent::Throttled is not installed)',
268 warn_map_bounds_failed => 'OpenStreetMap did not answer, so %d of %d places have no %s outline; they are tried again next time',
269 warn_map_bounds_failed_one => 'OpenStreetMap did not answer, so %d of %d places has no %s outline; it is tried again next time',
270 error_geo_boundary_point => 'A lookup needs point= and the sig= the map page gave it',
271 error_geo_boundary_unsigned => 'This point was not listed by a map page, or the server has restarted since; open the map again',
272 error_insights_no_data => 'No data available to analyse',
273 insights_insufficient_data => 'Insufficient data for this insight type (need more rows or periods)',
274 ask_unavailable => 'Ask a question is not available: no AI provider is configured',
275 ask_no_question => 'Please enter a question',
276 ask_question_too_long => 'The question is longer than %d characters; please shorten it',
277 ai_limit_reached => 'The AI provider has been asked %d times in the last hour, the most this server allows (ai_calls_per_hour); please try again later',
278 ask_provider_error => 'AI provider error; could not generate an analysis',
279 ask_unparseable => 'Could not parse a valid visualization from the response; please rephrase your question',
280 ask_invalid_spec => 'Generated spec contains an unknown column: %s',
281 summary_consent_required => 'AI consent is required before sending data to an external provider',
282 summary_ai_unavailable => 'AI provider not configured; using template summary',
283 summary_provider_error => 'AI provider error; using template summary',
284 error_token_invalid => 'Share token is invalid or has expired',
285 error_token_missing => 'A share token is required to view this analysis',
286 freshness_stale_msg => 'Source data has changed since this analysis was last saved',
287 error_agg_fn => 'Unknown aggregate function "%s"; expected one of: %s, or p1 to p100 for a percentile',
288 error_range_name => 'Unknown time range "%s"; expected one of: %s',
289 error_range_column => 'A time range is applied to one column: add rcol=<column> to the URL',
290 error_filter_op => 'Unknown filter operator "%s" in f=%s; expected one of: %s',
291 error_filter_column => 'There is no column called "%s" to filter on (f=%s); the columns are: %s',
292 error_range_column_unknown => 'There is no column called "%s" for the time range (rcol=%s); the columns are: %s',
293 error_span_interval => 'Unknown time bucket "%s" for span=; use a count and one of s, m, h, d, w, mon, q or y, for example 1h or 15m',
294 error_span_no_column => 'A time bucket needs one column to bucket: add tcol=<column>, or name exactly one of the dimensions this page already takes',
295 error_span_tcol_orphan => 'tcol=%s names the column to bucket, but span=<interval> is missing',
296 error_span_tcol_unknown => 'Column "%s" is not in this view, so there is nothing to bucket',
297 error_span_conflict => 'The bucket column "%s" is already defined by a calculated field; add as=<name> to bucket under a name of its own',
298 error_span_name => 'Bucket column name "%s" is not a usable column name; add as=<name>',
299 error_cs_spec => 'cs=%s must be <column>:<function>:<alias>, optionally followed by :<parameter>',
300 error_cs_column => 'cs=%s names the column "%s", which is not in this view',
301 error_cs_fn => 'Unknown cross-row function "%s"; use one of: %s',
302 error_cs_alias => 'Cross-row alias "%s" is not a usable column name',
303 error_cs_conflict => 'Cross-row alias "%s" is already a column of this view, or another cs= already writes it; choose another name',
304 error_cs_arg => 'cs=%s: %s takes a whole number greater than zero, as in %s:%s:ma7:7',
305 error_cs_no_arg => 'cs=%s: %s takes no parameter',
306 error_cs_by => 'by=%s names "%s", which is not a column of this view',
307 error_cs_order => 'order=%s must be <column>, <column>:asc or <column>:desc',
308 error_cs_order_col => 'order=%s names "%s", which is not a column of this view',
309 error_cs_order_need => 'cs=%s needs order=: %s reads its rows in order, so the order has to be named',
310 );
311  
312 # Maximum accepted upload body size. Enforced both here (application layer)
313 # and via Mojolicious max_request_size (transport layer) set in startup().
314 # Must match $MAX_REQUEST_SIZE in BI.pm.
315 5 Readonly my $MAX_UPLOAD_MIB => 50;
316 5 Readonly my $MAX_UPLOAD_BYTES => $MAX_UPLOAD_MIB * 1_048_576;
317 5 Readonly my $DEFAULT_JOIN_MAX_ROWS => 10_000;
318 5 Readonly my $LOOKUP_MAX_BYTES => 8 * 1_048_576; # a bigger file is a join, not a lookup
319 5 Readonly my $LOOKUP_NAME_RE => qr/\A[A-Za-z_][A-Za-z0-9_]*\z/;
320 5 Readonly my $DEFAULT_MAP_GEOCODE_MAX => 500; # new lookups per request (map_geocode_max)
321 # Places looked up online (OpenStreetMap's Nominatim) per request. Nominatim
322 # allows one request a second, so this is roughly the seconds a page can wait
323 # for it: twenty keeps a page well inside hypnotoad's 50-second heartbeat, and
324 # script/bi-geocode, which has no page to hold, asks without a limit.
325 5 Readonly my $DEFAULT_MAP_GEOCODE_ONLINE_MAX => 20; # map_geocode_online_max
326 5 Readonly my $DEFAULT_MAP_MAX_MARKERS => 2_000; # markers drawn per request (map_max_markers)
327 5 Readonly my $DEFAULT_MAP_POPUP_ROWS => 10; # rows listed in one marker's popup (map_popup_rows)
328 5 Readonly my $DEFAULT_MARKER_COLOR => '#3388ff'; # Leaflet default blue
329 # A bubble map's size key: the bubbles' own colour when they have one, and
330 # this grey when the colour says something else (a colour column)
331 5 Readonly my $MAP_SIZE_KEY_COLOR => '#888888';
332 5 Readonly my %NAMED_COLORS => map { $_ => 1 } qw(
333 red orange yellow green teal cyan blue navy purple
334 magenta pink brown maroon olive silver gray grey gold lime white black
335 );
336 5 Readonly my $MAX_PREFIX_LEN => 20;
337 5 Readonly my $DEFAULT_PER_PAGE => 200;
338 5 Readonly my $MAX_PER_PAGE => 2_000;
339 # The page sizes offered under a table or pivot and on /preferences; 0 is every
340 # row on one page. The preference is the cookie $PAGE_SIZE_COOKIE (the server
341 # must see it before it reads the page), and only these values are taken from it.
342 5 Readonly my @PAGE_SIZES => (25, 50, 100, 200, 0);
343 5 Readonly my %PAGE_SIZE_OK => map { $_ => 1 } @PAGE_SIZES;
344 5 Readonly my $PAGE_SIZE_COOKIE => 'bi_per_page';
345 # per_page=0 (all rows) as a page size the arithmetic can use: one page that
346 # holds any table this application can read.
347 5 Readonly my $ALL_ROWS => 2_000_000_000;
348 5 Readonly my %SCATTER_SCHEMES => map { $_ => 1 } qw(tableau10 category10 set2 set3 paired);
349  
350 # Categorical palettes for map series colouring, matching the D3 schemes the
351 # chart pages offer. The map draws per-marker icons rather than D3 marks, so
352 # the hexes live here instead of in a template.
353 5 Readonly my %CATEGORY_PALETTES => (
354 tableau10 => [ '#4e79a7', '#f28e2c', '#e15759', '#76b7b2', '#59a14f',
355 '#edc949', '#af7aa1', '#ff9da7', '#9c755f', '#bab0ab' ],
356 category10 => [ '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
357 '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf' ],
358 set2 => [ '#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854',
359 '#ffd92f', '#e5c494', '#b3b3b3' ],
360 set3 => [ '#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3',
361 '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd',
362 '#ccebc5', '#ffed6f' ],
363 paired => [ '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99',
364 '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a',
365 '#ffff99', '#b15928' ],
366 );
367  
368 # add_circle_marker() accepts only hex colours, while the icon path also takes
369 # the named colours _parse_color() allows. Map the names once, here.
370 5 Readonly my %NAMED_COLOR_HEX => (
371 red => '#ff0000', orange => '#ffa500', yellow => '#ffff00',
372 green => '#008000', teal => '#008080', cyan => '#00ffff',
373 blue => '#0000ff', navy => '#000080', purple => '#800080',
374 magenta => '#ff00ff', pink => '#ffc0cb', brown => '#a52a2a',
375 maroon => '#800000', olive => '#808000', silver => '#c0c0c0',
376 gray => '#808080', grey => '#808080', gold => '#ffd700',
377 lime => '#00ff00', white => '#ffffff', black => '#000000',
378 );
379  
380 # The aggregate function vocabulary shared by _measure, _grouped_totals, the
381 # /pivot measure-spec parser and the /stats view.
382 #
383 # It has two halves. @PUSHED_MEASURE_FNS are the functions
384 # Database::Abstraction's aggregate() computes in the backend: adding one here
385 # means adding it there as well. @PERL_MEASURE_FNS are computed by the Perl
386 # fold in _grouped_totals from all the values in a group, because a subtotal has
387 # to be recomputed from the rows rather than combined out of the cells (a median
388 # is not the sum of the medians of its parts), and because the app already holds
389 # the rows: the fold is one pass over records that are in memory, with no second
390 # implementation of the same statistic to agree with. Database::Abstraction
391 # does accept a median and a stddev measure, computing them in Perl over rows it
392 # materialises itself -- close enough to be tempting, and exactly the second
393 # implementation to avoid, so _measure_pushable refuses to send them.
394 #
395 # A percentile is spelled p<N>, for N from 1 to 100: p90, p99, p1. The level
396 # rides in the function name because the spec dialect is col:fn:as, with no
397 # room for a parameter in it; p50 is the median, and median is kept as its own
398 # name because that is what a reader looks for.
399 5 Readonly my @PUSHED_MEASURE_FNS => qw(sum avg min max count count_distinct);
400 5 Readonly my @PERL_MEASURE_FNS => qw(median stdev first last range);
401 5 Readonly my @MEASURE_FNS => (@PUSHED_MEASURE_FNS, @PERL_MEASURE_FNS);
402 5 Readonly my %PUSHED_MEASURE_FN_OK => map { $_ => 1 } @PUSHED_MEASURE_FNS;
403 5 Readonly my %MEASURE_FN_OK => map { $_ => 1 } @MEASURE_FNS;
404  
405 # _measure_percentile($fn) -> $level | undef
406 #
407 # The percentile level a measure function names, or undef when it names none:
408 # 'p95' -> 95, 'p50' -> 50, 'percentile' -> undef, 'sum' -> undef. A level
409 # outside 1..100 is not a percentile, so an unknown function still fails as an
410 # unknown function rather than as a percentile of -40.
411 37 ▶sub _measure_percentile {
Subroutine _measure_percentile: called CC=6 SCAR=17.9
412 37 my $fn = shift;
413 37 ▶ return undef unless defined $fn && length $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and length $fn 2 35 37
Condition100%3/3
defined $fn and length $fn
!ll&&!rl&&r
1135
MC/DC100%2/2
defined $fn and length $fn
defined $fn length $fn
Truth table100%3/3
A and B
A= defined $fnB= length $fn
ABresult
0X0
100
111
414 35 my ($level) = $fn =~ /\Ap(\d{1,3})\z/;
415 35 ▶ return undef unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 18 17 35
416 17 ▶ return ($level >= 1 && $level <= 100) ? $level + 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$level >= 1 && $level <= 100 ? : 12 5 17
Condition100%3/3
$level >= 1 and $level <= 100
!ll&&!rl&&r
2312
MC/DC100%2/2
$level >= 1 and $level <= 100
$level >= 1 $level <= 100
Truth table100%3/3
A and B
A= $level >= 1B= $level <= 100
ABresult
0X0
100
111
417 }
418  
419 # _measure_fn_ok($fn) -> bool
420 #
421 # True for every function _grouped_totals can compute: the named ones and any
422 # percentile level. This is the single answer to "is this a measure function?"
423 # for the parser, and the vocabulary the 400 message prints comes from the same
424 # two lists, so a message can never list a function the fold cannot compute.
425 47 ▶sub _measure_fn_ok {
Subroutine _measure_fn_ok: called CC=5 SCAR=16.1
426 47 my $fn = shift;
427 47 ▶ return 0 unless defined $fn && length $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and length $fn 2 45 47
Condition100%3/3
defined $fn and length $fn
!ll&&!rl&&r
1145
MC/DC100%2/2
defined $fn and length $fn
defined $fn length $fn
Truth table100%3/3
A and B
A= defined $fnB= length $fn
ABresult
0X0
100
111
428 45 ▶ return 1 if $MEASURE_FN_OK{$fn};
Branch: 1 branch
BranchTrueFalseTotal
if $MEASURE_FN_OK{$fn} 28 17 45
429 17 ▶ return defined _measure_percentile($fn) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined _measure_percentile($fn) ? : 7 10 17
430 }
431  
432 # ---------------------------------------------------------------------------
433 # Relative time ranges
434 #
435 # A relative time range is a named window worked out from the clock when the
436 # request arrives -- "the last 30 days", "this quarter" -- so a link, a
437 # bookmark or a saved canvas that carries one keeps meaning the same thing
438 # every time it is opened instead of freezing the dates it first resolved to.
439 #
440 # The range is named in the URL (?range=<name>&rcol=<column>) and resolved to
441 # two f= filters on that column, which every data route already applies, so the
442 # table view, the charts, the pivot, the stats page, the insights page, the
443 # exports and a saved canvas all see the same rows.
444 #
445 # The vocabulary is day-granular except last_24_hours, which is to the second.
446 # ---------------------------------------------------------------------------
447  
448 5 Readonly my @TIME_RANGES => (
449 { name => 'last_24_hours', label => 'Last 24 hours', hours => 24 },
450 { name => 'today', label => 'Today', day => 0 },
451 { name => 'yesterday', label => 'Yesterday', day => -1 },
452 { name => 'last_7_days', label => 'Last 7 days', days => 7 },
453 { name => 'last_30_days', label => 'Last 30 days', days => 30 },
454 { name => 'last_90_days', label => 'Last 90 days', days => 90 },
455 { name => 'this_month', label => 'This month', month => 0 },
456 { name => 'last_month', label => 'Last month', month => -1 },
457 { name => 'this_quarter', label => 'This quarter', quarter => 0 },
458 { name => 'last_quarter', label => 'Last quarter', quarter => -1 },
459 { name => 'this_year', label => 'This year', year => 0 },
460 { name => 'last_year', label => 'Last year', year => -1 },
461 );
462 5 Readonly my %TIME_RANGE => map { $_->{name} => $_ } @TIME_RANGES;
463  
464 # The operators an f= filter may name (_apply_filter_spec), as written: an
465 # operator in capitals is refused with the rest, not read as another one.
466 5 Readonly my @FILTER_OPS => qw(eq ne contains starts lt le gt ge empty notempty between in);
467 5 Readonly my %FILTER_OP => map { $_ => 1 } @FILTER_OPS;
468  
469 # How many column names a refusal lists before it says how many more there are.
470 5 Readonly my $FILTER_COLUMNS_LISTED => 20;
471  
472 # Allowed strptime-style date format patterns for the /dates/save endpoint, and
473 # the two time patterns that may be given beside one of them for a column that
474 # holds a clock time as well as a date.
475 5 Readonly my %ALLOWED_DATE_PATTERNS => map { $_ => 1 }
476 qw(%m/%d/%Y %d/%m/%Y %d-%b-%Y);
477 5 Readonly my %ALLOWED_TIME_PATTERNS => map { $_ => 1 }
478 qw(%H:%M %H:%M:%S);
479  
480 # Default share token lifetime: 30 days in seconds.
481 5 Readonly my $SHARE_TOKEN_TTL => 30 * 86_400;
482  
483 # The AI routes spend the configured ai_api_key on every call. A question is
484 # at most this long, and each server process makes at most ai_calls_per_hour
485 # provider calls in any hour (0: no limit).
486 5 Readonly my $ASK_MAX_QUESTION => 500;
487 5 Readonly my $AI_CALLS_PER_HOUR => 60;
488 5 Readonly my $AI_WINDOW_S => 3600;
489  
490 # ---------------------------------------------------------------------------
491 # Protected helpers
492 # ---------------------------------------------------------------------------
493  
494 # _whole_number($value) -> $integer | undef
495 #
496 # A request parameter as an integer, or undef if it does not spell one. int()
497 # on the raw text warns on 'abc' and turns it into 0, and reads a long run of
498 # digits or '1e400' as infinity, which no LIMIT or OFFSET can use.
499 727 ▶sub _whole_number {
Subroutine _whole_number: called CC=3 SCAR=11.0
500 727 my $value = $_[0];
501 727 ▶ return undef unless defined($value) && $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u 672 55 727
Condition100%3/3
defined $value and $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u
!ll&&!rl&&r
6225055
MC/DC100%2/2
defined $value and $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u
defined $value $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\A-?\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u
ABresult
0X0
100
111
502 55 return $value + 0;
503 }
504  
505 # _categorical_scheme($name) -> $name
506 # _sequential_scheme($name) -> $name
507 #
508 # A colour scheme the chart can use, or the default. These must be checked here:
509 # HTML::D3 writes a categorical name into its script as SCHEMES['name'], so an
510 # unchecked value is script on the page, and it dies on a sequential name it does
511 # not know, which is a 500. An unknown name has always drawn the default colours.
512 113 ▶sub _categorical_scheme {
Subroutine _categorical_scheme: called CC=4 SCAR=13.9
513 113 my $name = $_[0];
514 113 ▶ return (defined($name) && !ref($name) && $SCATTER_SCHEMES{$name}) ? $name : $DEFAULT_CATEGORICAL_SCHEME;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && !ref($name) && $SCATTER_SCHEMES{$name} ? : 1 112 113
Condition100%6/6
defined $name and not ref $name and $SCATTER_SCHEMES{$name}
!ll&&!rl&&r
100121
defined $name and not ref $name
!ll&&!rl&&r
99113
MC/DC100%3/3
defined $name and not ref $name and $SCATTER_SCHEMES{$name}
defined $name not ref $name $SCATTER_SCHEMES{$name}
Truth table100%4/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $SCATTER_SCHEMES{$name}
ABCresult
0XX0
10X0
1100
1111
515 }
516  
517 115 ▶sub _sequential_scheme {
Subroutine _sequential_scheme: called CC=4 SCAR=13.9
518 115 my $name = $_[0];
519 115 ▶ return (defined($name) && !ref($name) && $SEQUENTIAL_SCHEMES{$name}) ? $name : $DEFAULT_SEQUENTIAL_SCHEME;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && !ref($name) && $SEQUENTIAL_SCHEMES{$name} ? : 1 114 115
Condition100%6/6
defined $name and not ref $name and $SEQUENTIAL_SCHEMES{$name}
!ll&&!rl&&r
104101
defined $name and not ref $name
!ll&&!rl&&r
103111
MC/DC100%3/3
defined $name and not ref $name and $SEQUENTIAL_SCHEMES{$name}
defined $name not ref $name $SEQUENTIAL_SCHEMES{$name}
Truth table100%4/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $SEQUENTIAL_SCHEMES{$name}
ABCresult
0XX0
10X0
1100
1111
520 }
521  
522 # _optional_scheme($name) -> $name | undef
523 #
524 # A categorical scheme for a chart that draws its own colours without one:
525 # a histogram's bars are steelblue, a combo's bars and line steelblue and red,
526 # and a scheme replaces them with its first colours. Unlike
527 # _categorical_scheme there is no scheme by default, so a name that is not one
528 # of the five is undef and the chart keeps the colours it had before scheme=
529 # existed. /histogram, /combo and their canvas cards all read it here.
530 109 ▶sub _optional_scheme {
Subroutine _optional_scheme: called CC=4 SCAR=13.9
531 109 my $name = $_[0];
532 109 ▶ return (defined($name) && !ref($name) && $SCATTER_SCHEMES{$name}) ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && !ref($name) && $SCATTER_SCHEMES{$name} ? : 5 104 109
Condition100%6/6
defined $name and not ref $name and $SCATTER_SCHEMES{$name}
!ll&&!rl&&r
9595
defined $name and not ref $name
!ll&&!rl&&r
94114
MC/DC100%3/3
defined $name and not ref $name and $SCATTER_SCHEMES{$name}
defined $name not ref $name $SCATTER_SCHEMES{$name}
Truth table100%4/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $SCATTER_SCHEMES{$name}
ABCresult
0XX0
10X0
1100
1111
533 }
534  
535 # _scatter_radii($min, $max) -> ($min, $max)
536 #
537 # A scatter's bubble radius range in pixels: each value a whole number clamped
538 # to 1..40, anything else (absent, empty, 0, text) the default 3 or 15, and the
539 # pair put in order rather than refused. /scatter and a canvas scatter card
540 # both read it here, so a card saved from the page draws the same bubbles.
541 53 ▶sub _scatter_radii {
Subroutine _scatter_radii: called CC=8 SCAR=20.8
542 53 ▶ my ($min, $max) = map { _whole_number(ref $_ ? undef : $_) } @_[0, 1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 105 106
543 53 ▶ $min = 3 unless $min;
Branch: 1 branch
BranchTrueFalseTotal
unless $min 51 2 53
544 53 ▶ $max = 15 unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 51 2 53
545 53 for ($min, $max) {
546 106 ▶ $_ = 1 if $_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 1 1 105 106
547 106 ▶ $_ = 40 if $_ > 40;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 40 1 105 106
548 }
549 53 ▶ return $min > $max ? ($max, $min) : ($min, $max);
Branch: 1 branch
BranchTrueFalseTotal
$min > $max ? : 1 52 53
550 }
551  
552 # _pagination($self) -> ($page, $per_page, $offset)
553 #
554 # Reads ?page= and ?per_page= from the current request, clamps them to sane
555 # ranges, and returns the 1-based page number, the page size, and the
556 # 0-based row offset. Without per_page= the size is the reader's preference
557 # (_page_size_preference), else DEFAULT_PER_PAGE. 0 is every row: one page of
558 # $ALL_ROWS, so it bypasses MAX_PER_PAGE, which bounds only a number typed in
559 # the URL.
560 #
561 # Also stashes, for layouts/page_size.html.tt: page_size (0 for every row),
562 # page_size_asked (the URL named one) and page_size_choices.
563 234 ▶sub _pagination {
Subroutine _pagination: called CC=12 SCAR=25.1
564 234 my $self = $_[0];
565  
566 234 ▶ my $asked_in_url = defined $self->param('per_page') && length $self->param('per_page');
Condition100%3/3
defined $self->param('per_page') && length $self->param('per_page')
!ll&&!rl&&r
215316
MC/DC100%2/2
defined $self->param('per_page') && length $self->param('per_page')
defined $self->param('per_page') length $self->param('per_page')
Truth table100%3/3
A && B
A= defined $self->param('per_page')B= length $self->param('per_page')
ABresult
0X0
100
111
567 234 my $asked = _whole_number($self->param('per_page'));
568 234 ▶ $asked = _page_size_preference($self) unless defined $asked;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asked 231 3 234
569  
570 234 my ($per_page, $page);
571 234 ▶ if (defined $asked && $asked == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $asked and $asked == 0) { } 0 234 234
Condition66%2/3
defined $asked and $asked == 0
!ll&&!rl&&r
23130
MC/DC0%0/2
defined $asked and $asked == 0
defined $asked $asked == 0
Truth table66%2/3
A and B
A= defined $askedB= $asked == 0
ABresult
0X0
100
111
572 0 ($per_page, $page) = ($ALL_ROWS, 1);
573 } else {
574 234 ▶ $per_page = $asked || $DEFAULT_PER_PAGE;
Condition66%2/3
$asked || $DEFAULT_PER_PAGE
l!l&&r!l&&!r
32310
MC/DC0%0/2
$asked || $DEFAULT_PER_PAGE
$asked $DEFAULT_PER_PAGE
Truth table66%2/3
A || B
A= $askedB= $DEFAULT_PER_PAGE
ABresult
1X1
011
000
575 234 ▶ $per_page = 1 if $per_page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $per_page < 1 1 233 234
576 234 ▶ $per_page = $MAX_PER_PAGE if $per_page > $MAX_PER_PAGE;
Branch: 1 branch
BranchTrueFalseTotal
if $per_page > $MAX_PER_PAGE 1 233 234
577 234 ▶ $page = _whole_number($self->param('page')) || 1;
Condition100%2/2
_whole_number($self->param('page')) || 1
l!l
2232
MC/DC100%1/1
_whole_number($self->param('page')) || 1
_whole_number($self->param('page'))
Truth table100%2/2
A
A= _whole_number($self->param('page'))
Aresult
11
00
578 234 ▶ $page = 1 if $page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $page < 1 1 233 234
579 }
580 234 my $offset = ($page - 1) * $per_page;
581  
582 234 ▶ $self->stash(
Branch: 2 branches
BranchTrueFalseTotal
$per_page == $ALL_ROWS ? : 0 234 234
$asked_in_url ? : 16 218 234
583 page_size => ($per_page == $ALL_ROWS ? 0 : $per_page),
584 page_size_asked => ($asked_in_url ? 1 : 0),
585 page_size_choices => [ @PAGE_SIZES ],
586 );
587 234 return ($page, $per_page, $offset);
588 }
589  
590 # _page_size_preference($self) -> $size | undef
591 #
592 # The page size set on /preferences, from the bi_per_page cookie: one of
593 # @PAGE_SIZES (0 = every row), or undef when there is none or it is anything
594 # else.
595 232 ▶sub _page_size_preference {
Subroutine _page_size_preference: called CC=4 SCAR=19.7
596 232 my $self = $_[0];
597 232 my $value = $self->cookie($PAGE_SIZE_COOKIE);
598 232 ▶ return undef unless defined $value && $value =~ /\A\d{1,4}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A\d{1,4}\z/u 232 0 232
Condition33%1/3
defined $value and $value =~ /\A\d{1,4}\z/u
!ll&&!rl&&r
23200
MC/DC0%0/2
defined $value and $value =~ /\A\d{1,4}\z/u
defined $value $value =~ /\A\d{1,4}\z/u
Truth table33%1/3
A and B
A= defined $valueB= $value =~ /\A\d{1,4}\z/u
ABresult
0X0
100
111
599 0 $value += 0;
600 0 ▶ return $PAGE_SIZE_OK{$value} ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$PAGE_SIZE_OK{$value} ? : 0 0 0
601 }
602  
603 # _message_text($key) -> $template | undef
604 #
605 # The %MESSAGES template for $key in the reader's language (Database::BI::I18N,
606 # which CGI::Lingua chose from the request's Accept-Language). A plain function:
607 # the package-level checks (request_refusal, _outside_roots) have no $self.
608 690 ▶sub _message_text ($key) {
Subroutine _message_text: called CC=1 SCAR=0.0
609 690 return Database::BI::I18N::text(__PACKAGE__, \%MESSAGES, $key);
610 }
611  
612 # _i18n($self, $key, @sprintf_args) -> $string
613 #
614 # Purpose: Look up a user-visible string from %MESSAGES and apply sprintf
615 # for positional arguments. The entry point for future i18n
616 # backend integration (e.g. Locale::Maketext).
617 # Entry: $key must be a key in %MESSAGES; @args are sprintf positionals.
618 # Exit: Returns the formatted string, or a fallback containing $key.
619 316 ▶sub _i18n :Protected ($self, $key, @args) {
Subroutine _i18n: called CC=3 SCAR=11.0
620 316 ▶ my $tmpl = _message_text($key) // return "Internal error: unknown message key '$key'";
Condition100%2/2
_message_text($key) // (return "Internal error: unknown message key '${key}'")
l!l
3151
MC/DC100%1/1
_message_text($key) // (return "Internal error: unknown message key '${key}'")
_message_text($key)
Truth table100%2/2
A
A= _message_text($key)
Aresult
11
00
621 315 ▶ return @args ? sprintf($tmpl, @args) : $tmpl;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 151 164 315
622 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
623  
624 # _file_roots($self) -> @roots
625 #
626 # Purpose: The folders a reader may browse, open files in and export to.
627 # Entry: Config file_roots: unset or [] for the whole filesystem, else a list
628 # of folders (a relative one is under the application home).
629 # Exit: () when unrestricted; otherwise the realpath of each root that
630 # exists, plus data_dir and uploads_dir, which the application itself
631 # uses (the home page lists data_dir; dropped files go to uploads_dir).
632 425 ▶sub _file_roots :Protected ($self) {
Subroutine _file_roots: called CC=3 SCAR=15.1
633 425 my $roots = $self->app->config('file_roots');
634 0 ▶ return () unless ref($roots) eq 'ARRAY' && @{$roots};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $roots eq "ARRAY" and @{$roots;} 425 0 425
Condition33%1/3
ref $roots eq "ARRAY" and @{$roots;}
!ll&&!rl&&r
42500
MC/DC0%0/2
ref $roots eq "ARRAY" and @{$roots;}
ref $roots eq "ARRAY" @{$roots;}
Truth table33%1/3
A and B
A= ref $roots eq "ARRAY"B= @{$roots;}
ABresult
0X0
100
111
635 0 return _real_folders($self, $roots, _data_dir_for($self->app), $self->app->_uploads_dir);
636 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
637  
638 # _export_dirs($self) -> @realpaths
639 #
640 # Config export_dirs, as realpaths: the only folders POST /export may write
641 # in, and the Export panel may list, when set. Empty when unset.
642 34 ▶sub _export_dirs :Protected ($self) {
Subroutine _export_dirs: called CC=3 SCAR=15.1
643 34 my $dirs = $self->app->config('export_dirs');
644 0 ▶ return () unless ref($dirs) eq 'ARRAY' && @{$dirs};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dirs eq "ARRAY" and @{$dirs;} 34 0 34
Condition33%1/3
ref $dirs eq "ARRAY" and @{$dirs;}
!ll&&!rl&&r
3400
MC/DC0%0/2
ref $dirs eq "ARRAY" and @{$dirs;}
ref $dirs eq "ARRAY" @{$dirs;}
Truth table33%1/3
A and B
A= ref $dirs eq "ARRAY"B= @{$dirs;}
ABresult
0X0
100
111
645 0 return _real_folders($self, $dirs);
646 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
647  
648 # _real_folders($self, \@configured, @always) -> @realpaths
649 #
650 # The configured folders (relative ones under the application home) and
651 # @always, resolved, without repeats. A list none of which resolves gives a
652 # folder that cannot exist, so that it fences everything out rather than
653 # nothing. Resolved as a requested path is (_resolved_path), so a folder
654 # configured through a link is compared as the folder it leads to: on
655 # Windows realpath keeps the link, while _resolved_path follows it, and every
656 # path inside such a folder was refused.
657 0 ▶sub _real_folders {
Subroutine _real_folders: not called CC=9 SCAR=45.0
658 0 my ($self, $list, @always) = @_;
659 0 my $home = $self->app->home;
660 0 ▶ my @dirs = map { File::Spec->file_name_is_absolute($_) ? Mojo::File->new($_) : $home->child($_) }
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 0 0 0
661 0 ▶ grep { defined && !ref && length } @{$list};
Condition0%0/6
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
Truth table0%0/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
662 0 my (%seen, @real);
663 0 for my $dir (@dirs, @always) {
664 0 ▶ next unless -e "$dir";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$dir" 0 0 0
665 0 ▶ my $real = _resolved_path("$dir") // next;
Condition0%0/2
_resolved_path("$dir") // (next)
l!l
00
MC/DC0%0/1
_resolved_path("$dir") // (next)
_resolved_path("$dir")
Truth table0%0/2
A
A= _resolved_path("$dir")
Aresult
11
00
666 0 ▶ push @real, $real unless $seen{$real}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$real}++ 0 0 0
667 }
668 0 ▶ return @real ? @real : ('/nonexistent-file-root');
Branch: 1 branch
BranchTrueFalseTotal
@real ? : 0 0 0
669 }
670  
671 # _inside_any($path, @folders) -> 1 | 0
672 #
673 # Whether a path, resolved as _resolved_path does, is one of the folders or
674 # inside one.
675 2 ▶sub _inside_any {
Subroutine _inside_any: called CC=9 SCAR=24.6
676 2 my ($path, @folders) = @_;
677 2 ▶ my $abs = defined $path && length $path ? _resolved_path($path) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $path && length $path ? : 2 0 2
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
002
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
678 2 ▶ return 0 unless defined $abs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $abs 0 2 2
679 2 $abs = _path_key($abs);
680 2 for my $root (map { _path_key($_) } @folders) {
681 2 ▶ return 1 if $root eq '/' || $abs eq $root
Branch: 2 branches
BranchTrueFalseTotal
if $root eq "/" or $abs eq $root or CORE::index($abs, $root =~ m[/\z]u ? $root : "$root/") == 0 1 1 2
$root =~ m[/\z]u ? : 0 2 2
Condition50%3/6
$root eq "/" or $abs eq $root or CORE::index($abs, $root =~ m[/\z]u ? $root : "$root/") == 0
l!l&&r!l&&!r
011
$root eq "/" or $abs eq $root
l!l&&r!l&&!r
002
MC/DC33%1/3
$root eq "/" or $abs eq $root or CORE::index($abs, $root =~ m[/\z]u ? $root : "$root/") == 0
$root eq "/" $abs eq $root CORE::index($abs, $root =~ m[/\z]u ? $root : "$root/") == 0
Truth table50%2/4
A or B or C
A= $root eq "/"B= $abs eq $rootC= CORE::index($abs, $root =~ m[/\z]u ? $root : "$root/") == 0
ABCresult
1XX1
01X1
0011
0000
682 || index($abs, $root =~ m{/\z} ? $root : "$root/") == 0;
683 }
684 1 return 0;
685 }
686  
687 # _path_key($path) -> $string
688 #
689 # A path in the form two spellings of one place share, for comparing. On
690 # Windows, Cwd::realpath gives C:/dir while File::Spec gives C:\dir, and names
691 # are not case-sensitive, so separators become / and letters lower case.
692 4 ▶sub _path_key {
Subroutine _path_key: called CC=2 SCAR=7.0
693 4 my ($path) = @_;
694 4 ▶ return $path unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 0 4 4
695 4 (my $key = $path) =~ tr{\\}{/};
696 4 return lc $key;
697 }
698  
699 # _outside_export_dirs($self, $path) -> $message | undef
700 #
701 # Purpose: Refuse an export folder outside config export_dirs.
702 # Exit: undef when export_dirs is unset or the folder is inside one.
703 33 ▶sub _outside_export_dirs :Protected ($self, $path) {
Subroutine _outside_export_dirs: called CC=4 SCAR=24.4
704 33 ▶ my @dirs = _export_dirs($self) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(@dirs) = _export_dirs($self) or return undef 0 33 33
705 0 ▶ return undef if _inside_any($path, @dirs);
Branch: 1 branch
BranchTrueFalseTotal
if _inside_any($path, @dirs) 0 0 0
706 0 ▶ return sprintf(_message_text('error_outside_export_dirs'), $path // '');
Condition0%0/2
$path // ""
l!l
00
MC/DC0%0/1
$path // ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
707 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
708  
709 # _resolved_path($path) -> $absolute | undef
710 #
711 # Purpose: Where a path really leads, whether or not it exists, so that a
712 # path outside the roots is refused the same way whether it names
713 # something or not (else the refusal would say what exists there).
714 # Entry: A path as given.
715 # Exit: The realpath of its longest existing part, with the rest appended;
716 # undef when the rest climbs with '..', which cannot be followed
717 # without the folders it would climb out of, or when, on Windows,
718 # the links it goes through loop.
719 5 ▶sub _resolved_path {
Subroutine _resolved_path: called CC=9 SCAR=22.5
720 5 my ($path) = @_;
721 5 my $abs = File::Spec->rel2abs($path);
722 # Cwd::realpath on Windows leaves a symlink as it is (Strawberry Perl
723 # 5.42 gives the link's own path back), so a link inside file_roots or
724 # export_dirs that points outside would be taken as inside. The links are
725 # followed first, as _canonical_path does; a chain that loops is refused.
726 5 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 5 0 5
727 5 ▶ $abs = _follow_links($abs) // return undef;
Condition100%2/2
_follow_links($abs) // (return undef)
l!l
41
MC/DC100%1/1
_follow_links($abs) // (return undef)
_follow_links($abs)
Truth table100%2/2
A
A= _follow_links($abs)
Aresult
11
00
728 }
729 4 my @parts = File::Spec->splitdir($abs);
730 4 my @rest;
731 4 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 6 0 6
732 6 my $try = File::Spec->catdir(@parts);
733 6 ▶ $try = File::Spec->rootdir unless length $try;
Branch: 1 branch
BranchTrueFalseTotal
unless length $try 0 6 6
734 6 ▶ if (-e $try) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $try) 4 2 6
735 4 ▶ my $real = eval { Mojo::File->new($try)->realpath->to_string } // return undef;
Condition50%1/2
eval { do { "Mojo::File"->new($try)->realpath->to_string } } // (return undef)
l!l
40
MC/DC0%0/1
eval { do { "Mojo::File"->new($try)->realpath->to_string } } // (return undef)
eval { do { "Mojo::File"->new($try)->realpath->to_string } }
Truth table50%1/2
A
A= eval { do { "Mojo::File"->new($try)->realpath->to_string } }
Aresult
11
00
736 4 ▶ return undef if grep { $_ eq '..' } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "..";} @rest 0 4 4
737 4 ▶ return @rest ? File::Spec->catdir($real, @rest) : $real;
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 1 3 4
738 }
739 2 unshift @rest, pop @parts;
740 }
741 0 return undef;
742 }
743  
744 # _outside_roots($self, $path) -> $message | undef
745 #
746 # Purpose: Refuse a local path outside config file_roots.
747 # Entry: A path a request named (a file or a folder).
748 # Exit: undef when there are no roots or the path is inside one; otherwise
749 # error_outside_roots naming the path as it was given.
750 425 ▶sub _outside_roots :Protected ($self, $path) {
Subroutine _outside_roots: called CC=4 SCAR=24.4
751 425 ▶ my @roots = _file_roots($self) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(@roots) = _file_roots($self) or return undef 0 425 425
752 0 ▶ return undef if _inside_any($path, @roots);
Branch: 1 branch
BranchTrueFalseTotal
if _inside_any($path, @roots) 0 0 0
753 0 ▶ return sprintf(_message_text('error_outside_roots'), $path // '');
Condition0%0/2
$path // ""
l!l
00
MC/DC0%0/1
$path // ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
754 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
755  
756 # _first_root($self) -> $folder
757 #
758 # Where /browse and /api/dirs start with no path=: the home folder, unless
759 # that is outside the roots, when the first root is.
760 44 ▶sub _first_root :Protected ($self) {
Subroutine _first_root: called CC=3 SCAR=14.2
761 44 ▶ my $home = $ENV{HOME} // File::Spec->rootdir;
Condition33%1/3
$ENV{"HOME"} // "File::Spec"->rootdir
l!l&&r!l&&!r
4400
MC/DC0%0/2
$ENV{"HOME"} // "File::Spec"->rootdir
$ENV{"HOME"} "File::Spec"->rootdir
Truth table33%1/3
A // B
A= $ENV{"HOME"}B= "File::Spec"->rootdir
ABresult
1X1
011
000
762 44 ▶ return $home unless _outside_roots($self, $home);
Branch: 1 branch
BranchTrueFalseTotal
unless _outside_roots($self, $home) 44 0 44
763 0 return (_file_roots($self))[0];
764 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
765  
766 # request_refusal($c) -> $message | undef
767 #
768 # Purpose: Refuse a request that a hostile web page made through the reader's
769 # browser. The application has no login, so whatever can reach it
770 # can write files (POST /export) and read any file (/open); listening
771 # on 127.0.0.1 keeps other machines out but not other sites' pages.
772 # Entry: A controller of the application, before dispatch.
773 # Exit: The refusal message, or undef to let the request through.
774 #
775 # Three checks:
776 # * No password, so only this machine: a request from an address that is
777 # not loopback (as a trusted proxy reports it) is refused.
778 # * DNS rebinding. A page on evil.example whose name is made to resolve to
779 # 127.0.0.1 is the same origin as this server to the browser, so it can
780 # read the replies too. It still names its own host, so the Host must be
781 # localhost, *.localhost, an IP address, or a name in config allowed_hosts
782 # (['*'] lets every name in). Behind a trusted reverse proxy, whose Host is
783 # whatever the proxy sends, only allowed_hosts is checked, when it is set.
784 # * Cross-site writes. A form POST from another site needs no preflight.
785 # For any method but GET, HEAD and OPTIONS, Sec-Fetch-Site (set by the
786 # browser, not the page) must be same-origin or none; without it, an
787 # Origin must name this host and port. A request with neither is a
788 # script or an old browser, and is let through: it is not a page of
789 # another site acting with the reader's browser.
790 2184 ▶sub request_refusal {
Subroutine request_refusal: called CC=35 SCAR=59.3
Pod documented
791 2184 my ($c) = @_;
792 2184 my $req = $c->req;
793  
794 # With no password, only this machine. Startup already refuses to listen
795 # anywhere else, but a reverse proxy on this machine can still bring other
796 # machines' requests in; trusted, it names them in X-Forwarded-For, which
797 # is what remote_address then reads. A Unix socket has no address, and is
798 # this machine.
799 2184 my $hash = $c->app->config('password_hash');
800 2184 ▶ unless (defined $hash && length $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash and length $hash) 2184 0 2184
Condition33%1/3
defined $hash and length $hash
!ll&&!rl&&r
218400
MC/DC0%0/2
defined $hash and length $hash
defined $hash length $hash
Truth table33%1/3
A and B
A= defined $hashB= length $hash
ABresult
0X0
100
111
801 2184 ▶ my $from = $c->tx->remote_address // '';
Condition50%1/2
$c->tx->remote_address // ""
l!l
21840
MC/DC0%0/1
$c->tx->remote_address // ""
$c->tx->remote_address
Truth table50%1/2
A
A= $c->tx->remote_address
Aresult
11
00
802 2184 ▶ return _message_text('error_remote_needs_password')
Branch: 1 branch
BranchTrueFalseTotal
unless $from eq "" or $from =~ /\A(?:::ffff:)?127\./iu or $from eq "::1" 0 2184 2184
Condition33%2/6
$from eq "" or $from =~ /\A(?:::ffff:)?127\./iu or $from eq "::1"
l!l&&r!l&&!r
218400
$from eq "" or $from =~ /\A(?:::ffff:)?127\./iu
l!l&&r!l&&!r
021840
MC/DC0%0/3
$from eq "" or $from =~ /\A(?:::ffff:)?127\./iu or $from eq "::1"
$from eq "" $from =~ /\A(?:::ffff:)?127\./iu $from eq "::1"
Truth table25%1/4
A or B or C
A= $from eq ""B= $from =~ /\A(?:::ffff:)?127\./iuC= $from eq "::1"
ABCresult
1XX1
01X1
0011
0000
803 unless $from eq '' || $from =~ /\A(?:::ffff:)?127\./i || $from eq '::1';
804 }
805  
806 2184 ▶ my $host = lc($req->url->to_abs->host // '');
Condition50%1/2
$req->url->to_abs->host // ""
l!l
21840
MC/DC0%0/1
$req->url->to_abs->host // ""
$req->url->to_abs->host
Truth table50%1/2
A
A= $req->url->to_abs->host
Aresult
11
00
807 2184 ▶ if (length $host) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $host) 2184 0 2184
808 2184 my $allowed = $c->app->config('allowed_hosts');
809 0 ▶ my @allowed = ref($allowed) eq 'ARRAY' ? map { lc } grep { defined && !ref } @{$allowed} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $allowed eq 'ARRAY' ? : 0 2184 2184
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
810 0 ▶ my $listed = grep { $_ eq '*' || $_ eq $host || (/\A\*(\..+)\z/ && substr($host, -length $1) eq $1) } @allowed;
Condition0%0/9
$_ eq '*' || $_ eq $host || /\A\*(\..+)\z/u && substr($host, -length($1)) eq $1
l!l&&r!l&&!r
000
$_ eq "*" or $_ eq $host
l!l&&r!l&&!r
000
/\A\*(\..+)\z/u and substr($host, -length($1)) eq $1
!ll&&!rl&&r
000
MC/DC0%0/4
$_ eq '*' || $_ eq $host || /\A\*(\..+)\z/u && substr($host, -length($1)) eq $1
$_ eq "*" $_ eq $host /\A\*(\..+)\z/u substr($host, -length($1)) eq $1
Truth table0%0/5
A or B || C and D
A= $_ eq "*"B= $_ eq $hostC= /\A\*(\..+)\z/uD= substr($host, -length($1)) eq $1
ABCDresult
1XXX1
01XX1
00111
000X0
00100
811 2184 ▶ my $local = $host eq 'localhost' || $host =~ /\.localhost\z/
Condition33%3/9
$host eq 'localhost' || $host =~ /\.localhost\z/u || $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/u || $host =~ /[:\[]/u
l!l&&r!l&&!r
218400
$host eq "localhost" or $host =~ /\.localhost\z/u or $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/u
l!l&&r!l&&!r
021840
$host eq "localhost" or $host =~ /\.localhost\z/u
l!l&&r!l&&!r
002184
MC/DC0%0/4
$host eq 'localhost' || $host =~ /\.localhost\z/u || $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/u || $host =~ /[:\[]/u
$host eq "localhost" $host =~ /\.localhost\z/u $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/u $host =~ /[:\[]/u
Truth table20%1/5
A or B or C || D
A= $host eq "localhost"B= $host =~ /\.localhost\z/uC= $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/uD= $host =~ /[:\[]/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
812 || $host =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/ || $host =~ /[:\[]/;
813 2184 ▶ my $ok = $req->reverse_proxy ? (!@allowed || $listed) : ($local || $listed);
Branch: 1 branch
BranchTrueFalseTotal
$req->reverse_proxy ? : 0 2184 2184
Condition16%1/6
not @allowed or $listed
l!l&&r!l&&!r
000
$local or $listed
l!l&&r!l&&!r
218400
MC/DC0%0/2
not @allowed or $listed
not @allowed $listed
MC/DC0%0/2
$local or $listed
$local $listed
Truth table0%0/3
not(A) or B
A= not @allowedB= $listed
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $localB= $listed
ABresult
1X1
011
000
814 2184 ▶ return sprintf(_message_text('error_host_refused'), $host) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2184 2184
815 }
816  
817 2184 ▶ return if $req->method =~ /\A(?:GET|HEAD|OPTIONS)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $req->method =~ /\A(?:GET|HEAD|OPTIONS)\z/u 1946 238 2184
818  
819 238 my $headers = $req->headers;
820 238 ▶ my $site = lc($headers->header('Sec-Fetch-Site') // '');
Condition50%1/2
$headers->header("Sec-Fetch-Site") // ""
l!l
0238
MC/DC0%0/1
$headers->header("Sec-Fetch-Site") // ""
$headers->header("Sec-Fetch-Site")
Truth table50%1/2
A
A= $headers->header("Sec-Fetch-Site")
Aresult
11
00
821 238 ▶ if (length $site) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $site) 0 238 238
822 0 ▶ return if $site eq 'same-origin' || $site eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $site eq "same-origin" or $site eq "none" 0 0 0
Condition0%0/3
$site eq "same-origin" or $site eq "none"
l!l&&r!l&&!r
000
MC/DC0%0/2
$site eq "same-origin" or $site eq "none"
$site eq "same-origin" $site eq "none"
Truth table0%0/3
A or B
A= $site eq "same-origin"B= $site eq "none"
ABresult
1X1
011
000
823 0 return sprintf(_message_text('error_cross_site'), $site);
824 }
825 238 my $origin = $headers->origin;
826 238 ▶ return unless defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 238 0 238
827 0 ▶ my $from = lc(Mojo::URL->new($origin)->host_port // '');
Condition0%0/2
"Mojo::URL"->new($origin)->host_port // ""
l!l
00
MC/DC0%0/1
"Mojo::URL"->new($origin)->host_port // ""
"Mojo::URL"->new($origin)->host_port
Truth table0%0/2
A
A= "Mojo::URL"->new($origin)->host_port
Aresult
11
00
828 0 ▶ my $to = lc($req->url->to_abs->host_port // '');
Condition0%0/2
$req->url->to_abs->host_port // ""
l!l
00
MC/DC0%0/1
$req->url->to_abs->host_port // ""
$req->url->to_abs->host_port
Truth table0%0/2
A
A= $req->url->to_abs->host_port
Aresult
11
00
829 0 ▶ my $fwd = $req->reverse_proxy ? lc($headers->header('X-Forwarded-Host') // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$req->reverse_proxy ? : 0 0 0
Condition0%0/2
$headers->header("X-Forwarded-Host") // ""
l!l
00
MC/DC0%0/1
$headers->header("X-Forwarded-Host") // ""
$headers->header("X-Forwarded-Host")
Truth table0%0/2
A
A= $headers->header("X-Forwarded-Host")
Aresult
11
00
830 0 ▶ return if length($from) && ($from eq $to || (length($fwd) && $from eq $fwd));
Branch: 1 branch
BranchTrueFalseTotal
if length $from and $from eq $to || length $fwd && $from eq $fwd 0 0 0
Condition0%0/9
length $from and $from eq $to || length $fwd && $from eq $fwd
!ll&&!rl&&r
000
$from eq $to or length $fwd and $from eq $fwd
l!l&&r!l&&!r
000
length $fwd and $from eq $fwd
!ll&&!rl&&r
000
MC/DC0%0/4
length $from and $from eq $to || length $fwd && $from eq $fwd
length $from $from eq $to length $fwd $from eq $fwd
Truth table0%0/5
A and B or C and D
A= length $fromB= $from eq $toC= length $fwdD= $from eq $fwd
ABCDresult
0XXX0
100X0
10100
11XX1
10111
831 0 return sprintf(_message_text('error_cross_site'), $origin);
832 }
833  
834 # _is_safe_url($url) -> bool
835 #
836 # Purpose: The quick SSRF check on a URL a request names (/import, url:
837 # specs), before anything is fetched, so the reader is told at once.
838 # Entry: The URL.
839 # Exit: 0 for anything but http(s), no host, localhost or *.localhost, or a
840 # host written as an address that is not public; else 1.
841 #
842 # It reads the URL as LWP will (URI: so http://user@127.0.0.1/ is host
843 # 127.0.0.1), and an address in every spelling the resolver accepts
844 # (2130706433, 0x7f000001, 0177.0.0.1, 127.1, [::ffff:127.0.0.1]). It does not
845 # look names up: a name such as localtest.me, or one whose DNS answers
846 # 127.0.0.1, is refused when it is fetched, by Database::BI::SafeUA, which
847 # resolves each request (and each redirect), checks every address, and
848 # connects to the one it checked, so a second DNS answer cannot differ.
849 32 ▶sub _is_safe_url {
Subroutine _is_safe_url: called CC=13 SCAR=26.8
850 32 my ($url) = @_;
851 32 ▶ return 0 unless defined $url && $url =~ m{\Ahttps?://}i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and $url =~ m[\Ahttps?://]iu 3 29 32
Condition66%2/3
defined $url and $url =~ m[\Ahttps?://]iu
!ll&&!rl&&r
0329
MC/DC50%1/2
defined $url and $url =~ m[\Ahttps?://]iu
defined $url $url =~ m[\Ahttps?://]iu
Truth table66%2/3
A and B
A= defined $urlB= $url =~ m[\Ahttps?://]iu
ABresult
0X0
100
111
852 29 require URI;
853 29 require Database::BI::SafeUA;
854 29 my $uri = URI->new($url);
855 29 ▶ my $host = eval { lc($uri->host // '') } // '';
Condition50%2/4
eval { do { lc($uri->host // "") } } // ""
l!l
290
$uri->host // ""
l!l
290
MC/DC0%0/1
eval { do { lc($uri->host // "") } } // ""
eval { do { lc($uri->host // "") } }
MC/DC0%0/1
$uri->host // ""
$uri->host
Truth table50%1/2
A
A= eval { do { lc($uri->host // "") } }
Aresult
11
00
Truth table50%1/2
A
A= $uri->host
Aresult
11
00
856 29 $host =~ s/\.\z//;
857 29 ▶ return 0 unless length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless length $host 0 29 29
858 # A host that is neither a name nor an IPv6 address is not a URL LWP
859 # would read the same way (http://::1/ has the host ':').
860 29 ▶ return 0 unless $host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/u or $host =~ /:/u and defined Database::BI::SafeUA::numeric_host($host) 0 29 29
Condition50%3/6
$host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/u or $host =~ /:/u and defined Database::BI::SafeUA::numeric_host($host)
l!l&&r!l&&!r
2810
$host =~ /:/u and defined Database::BI::SafeUA::numeric_host($host)
!ll&&!rl&&r
001
MC/DC0%0/3
$host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/u or $host =~ /:/u and defined Database::BI::SafeUA::numeric_host($host)
$host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/u $host =~ /:/u defined Database::BI::SafeUA::numeric_host($host)
Truth table50%2/4
A or B and C
A= $host =~ /\A[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?\z/uB= $host =~ /:/uC= defined Database::BI::SafeUA::numeric_host($host)
ABCresult
1XX1
0111
00X0
0100
861 || ($host =~ /:/ && defined Database::BI::SafeUA::numeric_host($host));
862 29 ▶ return 0 if $host eq 'localhost' || $host =~ /\.localhost\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "localhost" or $host =~ /\.localhost\z/u 4 25 29
Condition66%2/3
$host eq "localhost" or $host =~ /\.localhost\z/u
l!l&&r!l&&!r
4025
MC/DC50%1/2
$host eq "localhost" or $host =~ /\.localhost\z/u
$host eq "localhost" $host =~ /\.localhost\z/u
Truth table66%2/3
A or B
A= $host eq "localhost"B= $host =~ /\.localhost\z/u
ABresult
1X1
011
000
863 25 my $ip = Database::BI::SafeUA::numeric_host($host);
864 25 ▶ return 0 if defined $ip && Database::BI::SafeUA::blocked_address($ip);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip and Database::BI::SafeUA::blocked_address($ip) 15 10 25
Condition100%3/3
defined $ip and Database::BI::SafeUA::blocked_address($ip)
!ll&&!rl&&r
4615
MC/DC100%2/2
defined $ip and Database::BI::SafeUA::blocked_address($ip)
defined $ip Database::BI::SafeUA::blocked_address($ip)
Truth table100%3/3
A and B
A= defined $ipB= Database::BI::SafeUA::blocked_address($ip)
ABresult
0X0
100
111
865 10 return 1;
866 }
867  
868 # _safe_back_url($url) -> $url | undef
869 #
870 # Purpose: Sanitise a user-supplied back-link URL before it is passed to the
871 # template and rendered inside an href="" attribute. Template Toolkit's
872 # | html filter encodes <>&" but does NOT block the javascript: or
873 # data: URI schemes, so a raw param('back') = "javascript:alert(1)"
874 # would be rendered verbatim inside the href and execute on click.
875 #
876 # Rule: Only absolute http(s) URLs and root-relative paths (starting with /)
877 # are permitted. Anything else (javascript:, data:, vbscript:, empty
878 # strings, undef) returns undef so the template renders no href.
879 #
880 4806 ▶sub _safe_back_url {
Subroutine _safe_back_url: called CC=4 SCAR=13.9
881 4806 my ($url) = @_;
882 4806 ▶ return undef unless defined $url && length($url);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 3786 1020 4806
Condition100%3/3
defined $url and length $url
!ll&&!rl&&r
3741451020
MC/DC100%2/2
defined $url and length $url
defined $url length $url
Truth table100%3/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
883 1020 ▶ return $url if $url =~ m{\A(?:/(?!/)|https?://)}i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[\A(?:/(?!/)|https?://)]iu 958 62 1020
884 62 return undef;
885 }
886  
887 # ---------------------------------------------------------------------------
888 # Breadcrumb trail
889 # ---------------------------------------------------------------------------
890 #
891 # Every page shows Home > ... > the page itself. The trail is not passed in:
892 # each page's URL already names the page it was opened from (a chart's back=,
893 # a table's back2= when it was drilled into from a chart), and that URL names
894 # its own, so the whole path is recovered by following the links up. Each
895 # step is named from its own URL by one function, so a crumb always reads the
896 # same whichever page it appears on.
897  
898 # The chart and analysis routes, which name their parent with back=.
899 5 Readonly my %CRUMB_KIND => (
900 graph => 'Line graph', line => 'Line graph',
901 pie => 'Pie chart', heatmap => 'Heatmap',
902 bar => 'Bar chart', stacked => 'Stacked bar chart',
903 scatter => 'Scatter plot', combo => 'Combo chart',
904 treemap => 'Treemap', histogram => 'Histogram',
905 wordcloud => 'Word cloud', map => 'Map',
906 pivot => 'Pivot table', stats => 'Column stats',
907 insights => 'Insights', folder => 'Folder view',
908 );
909  
910 # How a filter reads in a crumb: "region = East".
911 5 Readonly my %CRUMB_OP => (
912 eq => '=', ne => '!=', contains => 'contains', starts => 'starts with',
913 lt => '<', le => '<=', gt => '>', ge => '>=', between => 'between',
914 empty => 'is empty', notempty => 'is not empty', in => 'is one of',
915 );
916  
917 # A trail is never longer than this, whatever the URLs say.
918 5 Readonly my $CRUMB_MAX_DEPTH => 8;
919  
920 # A column name or filter value that a crumb will repeat. The URL is the
921 # user's, so a crafted one could put any text in the trail; markup and
922 # over-long values are left out and the crumb names the page more generally.
923 5 Readonly my $CRUMB_TEXT_RE => qr/\A[^<>"\x00-\x1f]{1,64}\z/;
924  
925 # _breadcrumb_trail($c) -> \@crumbs
926 #
927 # Purpose: The pages between Home and this one, this one last: a list of
928 # { label, url }. Home itself is not in the list; the template
929 # always puts it first.
930 # Entry: $c -- the controller for the request. A plain function, called
931 # from the before_render hook in Database::BI, so every page gets a
932 # trail without per-action code.
933 # Exit: An arrayref, empty for Home and for a controller that is not this
934 # class (a 404 page rendered by Mojolicious itself).
935 # Notes: A parent is followed only when _safe_back_url accepts it and it is
936 # on this host; a loop or the depth cap ends the trail, so a crafted
937 # URL can make it short but never wrong or endless.
938 2195 ▶sub _breadcrumb_trail {
Subroutine _breadcrumb_trail: called CC=8 SCAR=20.8
939 2195 my ($c) = @_;
940 2195 ▶ return [] unless ref $c && $c->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->isa(__PACKAGE__) 225 1970 2195
Condition100%3/3
ref $c and $c->isa(__PACKAGE__)
!ll&&!rl&&r
22231970
MC/DC100%2/2
ref $c and $c->isa(__PACKAGE__)
ref $c $c->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= ref $cB= $c->isa(__PACKAGE__)
ABresult
0X0
100
111
941  
942 1970 my @trail;
943 my %seen;
944 1970 my $url = Mojo::URL->new($c->req->url->path_query);
945 1970 ▶ while ($url && @trail < $CRUMB_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url and @trail < $CRUMB_MAX_DEPTH) 2980 1210 4190
Condition100%3/3
$url and @trail < $CRUMB_MAX_DEPTH
!ll&&!rl&&r
120912980
MC/DC100%2/2
$url and @trail < $CRUMB_MAX_DEPTH
$url @trail < $CRUMB_MAX_DEPTH
Truth table100%3/3
A and B
A= $urlB= @trail < $CRUMB_MAX_DEPTH
ABresult
0X0
100
111
946 2980 my $key = $url->path_query;
947 2980 ▶ last if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 0 2980 2980
948 2980 my $label = $c->_crumb_label($url);
949 2980 ▶ last unless defined $label && length $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label and length $label 760 2220 2980
Condition100%3/3
defined $label and length $label
!ll&&!rl&&r
75912220
MC/DC100%2/2
defined $label and length $label
defined $label length $label
Truth table100%3/3
A and B
A= defined $labelB= length $label
ABresult
0X0
100
111
950 2220 unshift @trail, { label => $label, url => $key };
951 2220 $url = $c->_crumb_parent($url);
952 }
953 1970 return \@trail;
954 }
955  
956 # _preferences_url($c) -> $href
957 #
958 # Where the nav's Preferences link goes: /preferences?back=<this page>, so
959 # the preferences page's trail ends with a way back here. This page is the
960 # last crumb of its trail; a page with no trail (Home, an error page) gets the
961 # plain link, and so does the preferences page, which keeps its own back=.
962 # A plain function, called from the before_render hook in Database::BI.
963 2186 ▶sub _preferences_url {
Subroutine _preferences_url: called CC=8 SCAR=21.1
964 2186 my ($c) = @_;
965 2186 my $plain = '/preferences';
966 2186 ▶ return $plain unless ref $c && $c->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->isa(__PACKAGE__) 223 1963 2186
Condition66%2/3
ref $c and $c->isa(__PACKAGE__)
!ll&&!rl&&r
02231963
MC/DC50%1/2
ref $c and $c->isa(__PACKAGE__)
ref $c $c->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $cB= $c->isa(__PACKAGE__)
ABresult
0X0
100
111
967 1963 my $trail = $c->stash('trail');
968 1963 ▶ return $plain unless ref $trail eq 'ARRAY' && @$trail;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $trail eq "ARRAY" and @$trail 759 1204 1963
Condition66%2/3
ref $trail eq "ARRAY" and @$trail
!ll&&!rl&&r
07591204
MC/DC50%1/2
ref $trail eq "ARRAY" and @$trail
ref $trail eq "ARRAY" @$trail
Truth table66%2/3
A and B
A= ref $trail eq "ARRAY"B= @$trail
ABresult
0X0
100
111
969 1204 ▶ my $here = $trail->[-1]{url} // '';
Condition50%1/2
$trail->[-1]{"url"} // ""
l!l
12040
MC/DC0%0/1
$trail->[-1]{"url"} // ""
$trail->[-1]{"url"}
Truth table50%1/2
A
A= $trail->[-1]{"url"}
Aresult
11
00
970 1204 ▶ return $plain if !length($here) || Mojo::URL->new($here)->path->to_string eq $plain;
Branch: 1 branch
BranchTrueFalseTotal
if not length $here or "Mojo::URL"->new($here)->path->to_string eq $plain 1 1203 1204
Condition66%2/3
not length $here or "Mojo::URL"->new($here)->path->to_string eq $plain
l!l&&r!l&&!r
011203
MC/DC50%1/2
not length $here or "Mojo::URL"->new($here)->path->to_string eq $plain
not length $here "Mojo::URL"->new($here)->path->to_string eq $plain
Truth table66%2/3
not(A) or B
A= not length $hereB= "Mojo::URL"->new($here)->path->to_string eq $plain
ABresult
1X1
011
000
971 1203 return "$plain?back=" . url_escape(encode('UTF-8', $here));
972 }
973  
974 # _crumb_local($self, $raw) -> Mojo::URL | undef
975 #
976 # A parent link as a URL on this site, or undef when there is none to follow:
977 # empty, refused by _safe_back_url, on another host, or Home (which the trail
978 # shows anyway). Absolute links -- the panels pass window.location.href --
979 # are reduced to their path and query.
980 2997 ▶sub _crumb_local :Protected ($self, $raw) {
Subroutine _crumb_local: called CC=8 SCAR=21.0
981 2997 ▶ my $safe = _safe_back_url($raw) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $safe = _safe_back_url($raw) or return undef 946 2051 2997
982 946 my $url = Mojo::URL->new($safe);
983 946 ▶ if (defined $url->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url->host) 4 942 946
984 4 my $here = $self->req->url->to_abs;
985 4 ▶ return undef unless lc($url->host_port // '') eq lc($here->host_port // '');
Branch: 1 branch
BranchTrueFalseTotal
unless lc($url->host_port // "") eq lc($here->host_port // "") 3 1 4
Condition50%2/4
$url->host_port // ""
l!l
40
$here->host_port // ""
l!l
40
MC/DC0%0/1
$url->host_port // ""
$url->host_port
MC/DC0%0/1
$here->host_port // ""
$here->host_port
Truth table50%1/2
A
A= $url->host_port
Aresult
11
00
Truth table50%1/2
A
A= $here->host_port
Aresult
11
00
986 }
987 943 my $path = $url->path->to_string;
988 943 ▶ return undef if $path eq '' || $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" or $path eq "/" 53 890 943
Condition66%2/3
$path eq "" or $path eq "/"
l!l&&r!l&&!r
053890
MC/DC50%1/2
$path eq "" or $path eq "/"
$path eq "" $path eq "/"
Truth table66%2/3
A or B
A= $path eq ""B= $path eq "/"
ABresult
1X1
011
000
989 890 return Mojo::URL->new($url->path_query);
990 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
991  
992 # _crumb_parent($self, $url) -> Mojo::URL | undef
993 #
994 # The page $url was opened from. A chart names it with back=. A table page
995 # names it with back2= when it was drilled into from a chart, and otherwise
996 # has a natural parent: the left table for a merge or a combine, the schema
997 # page or the folder for a file, nothing for a table in data_dir.
998 2233 ▶sub _crumb_parent :Protected ($self, $url) {
Subroutine _crumb_parent: called CC=23 SCAR=31.5
999 2233 my $path = $url->path->to_string;
1000 2233 my $q = $url->query;
1001  
1002 2233 ▶ if ($path =~ m{\A/([a-z]+)\z} && $CRUMB_KIND{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}) 837 1396 2233
Condition100%3/3
$path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}
!ll&&!rl&&r
948448837
MC/DC100%2/2
$path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}
$path =~ m[\A/([a-z]+)\z]u $CRUMB_KIND{$1}
Truth table100%3/3
A and B
A= $path =~ m[\A/([a-z]+)\z]uB= $CRUMB_KIND{$1}
ABresult
0X0
100
111
1003 837 ▶ return $self->_crumb_local($q->param('back'))
Condition100%5/5
$self->_crumb_local($q->param("back")) // $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
l!l&&r!l&&!r
1377252
$q->param("l") // ""
l!l
8186
MC/DC100%2/2
$self->_crumb_local($q->param("back")) // $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
$self->_crumb_local($q->param("back")) $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
MC/DC100%1/1
$q->param("l") // ""
$q->param("l")
Truth table100%3/3
A // B
A= $self->_crumb_local($q->param("back"))B= $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
ABresult
1X1
011
000
Truth table100%2/2
A
A= $q->param("l")
Aresult
11
00
1004 // $self->_crumb_local($self->_spec_to_url($q->param('l') // ''));
1005 }
1006 1396 ▶ if ($path =~ m{\A/view/} || $path eq '/import') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/view/]u or $path eq "/import") 912 484 1396
Condition100%3/3
$path =~ m[\A/view/]u or $path eq "/import"
l!l&&r!l&&!r
9075484
MC/DC100%2/2
$path =~ m[\A/view/]u or $path eq "/import"
$path =~ m[\A/view/]u $path eq "/import"
Truth table100%3/3
A or B
A= $path =~ m[\A/view/]uB= $path eq "/import"
ABresult
1X1
011
000
1007 912 return $self->_crumb_local($q->param('back2'));
1008 }
1009 484 ▶ if ($path eq '/join' || $path eq '/combine') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/join" or $path eq "/combine") 97 387 484
Condition100%3/3
$path eq "/join" or $path eq "/combine"
l!l&&r!l&&!r
5146387
MC/DC100%2/2
$path eq "/join" or $path eq "/combine"
$path eq "/join" $path eq "/combine"
Truth table100%3/3
A or B
A= $path eq "/join"B= $path eq "/combine"
ABresult
1X1
011
000
1010 97 ▶ return $self->_crumb_local($q->param('back2'))
Condition40%2/5
$self->_crumb_local($q->param("back2")) // $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
l!l&&r!l&&!r
0970
$q->param("l") // ""
l!l
970
MC/DC0%0/2
$self->_crumb_local($q->param("back2")) // $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
$self->_crumb_local($q->param("back2")) $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
MC/DC0%0/1
$q->param("l") // ""
$q->param("l")
Truth table33%1/3
A // B
A= $self->_crumb_local($q->param("back2"))B= $self->_crumb_local($self->_spec_to_url($q->param("l") // ""))
ABresult
1X1
011
000
Truth table50%1/2
A
A= $q->param("l")
Aresult
11
00
1011 // $self->_crumb_local($self->_spec_to_url($q->param('l') // ''));
1012 }
1013 387 ▶ if ($path eq '/open') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/open") 89 298 387
1014 89 ▶ my $parent = $self->_crumb_local($q->param('back2'))
Condition66%2/3
$self->_crumb_local($q->param("back2")) // $self->_crumb_local($q->param("back"))
l!l&&r!l&&!r
0188
MC/DC50%1/2
$self->_crumb_local($q->param("back2")) // $self->_crumb_local($q->param("back"))
$self->_crumb_local($q->param("back2")) $self->_crumb_local($q->param("back"))
Truth table66%2/3
A // B
A= $self->_crumb_local($q->param("back2"))B= $self->_crumb_local($q->param("back"))
ABresult
1X1
011
000
1015 // $self->_crumb_local($q->param('back'));
1016 89 ▶ return $parent if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 1 88 89
1017 88 ▶ my $file = $q->param('path') // '';
Condition50%1/2
$q->param("path") // ""
l!l
880
MC/DC0%0/1
$q->param("path") // ""
$q->param("path")
Truth table50%1/2
A
A= $q->param("path")
Aresult
11
00
1018 88 ▶ return undef unless length $file && $file !~ m{\A/\.\./};
Branch: 1 branch
BranchTrueFalseTotal
unless length $file and not $file =~ m[\A/\.\./]u 6 82 88
Condition100%3/3
length $file and not $file =~ m[\A/\.\./]u
!ll&&!rl&&r
1582
MC/DC100%2/2
length $file and not $file =~ m[\A/\.\./]u
length $file not $file =~ m[\A/\.\./]u
Truth table100%3/3
A and not(B)
A= length $fileB= not $file =~ m[\A/\.\./]u
ABresult
0X0
100
111
1019 82 return Mojo::URL->new('/browse?path=' . url_escape(Mojo::File->new($file)->dirname->to_string));
1020 }
1021 298 ▶ if ($path eq '/tables') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/tables") 56 242 298
1022 56 ▶ my $spec = $q->param('l') // '';
Condition50%1/2
$q->param("l") // ""
l!l
560
MC/DC0%0/1
$q->param("l") // ""
$q->param("l")
Truth table50%1/2
A
A= $q->param("l")
Aresult
11
00
1023 56 ▶ return undef unless $spec =~ /\Apath:(.+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /\Apath:(.+)\z/u 49 7 56
1024 7 my ($file) = _split_path($1);
1025 7 return Mojo::URL->new('/browse?path=' . url_escape(Mojo::File->new($file)->dirname->to_string));
1026 }
1027 # Preferences came from the page whose nav link opened it (_preferences_url).
1028 242 ▶ if ($path eq '/preferences') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/preferences") 1 241 242
1029 1 return $self->_crumb_local($q->param('back'));
1030 }
1031 # A canvas came from where its back= says, else from the list of canvases.
1032 241 ▶ if ($path =~ m{\A/a/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/a/]u) 41 200 241
1033 41 ▶ return $self->_crumb_local($q->param('back')) // Mojo::URL->new('/canvases');
Condition66%2/3
$self->_crumb_local($q->param("back")) // "Mojo::URL"->new("/canvases")
l!l&&r!l&&!r
1400
MC/DC0%0/2
$self->_crumb_local($q->param("back")) // "Mojo::URL"->new("/canvases")
$self->_crumb_local($q->param("back")) "Mojo::URL"->new("/canvases")
Truth table66%2/3
A // B
A= $self->_crumb_local($q->param("back"))B= "Mojo::URL"->new("/canvases")
ABresult
1X1
011
000
1034 }
1035 200 return undef;
1036 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1037  
1038 # _crumb_label($self, $url) -> $text | undef
1039 #
1040 # What a page is called in a trail. undef for a page that has no place in
1041 # one, which ends the trail there.
1042 2998 ▶sub _crumb_label :Protected ($self, $url) {
Subroutine _crumb_label: called CC=34 SCAR=35.3
1043 2998 my $path = $url->path->to_string;
1044 2998 my $q = $url->query;
1045 2998 my $view = _crumb_view_suffix($q);
1046  
1047 2998 ▶ return _crumb_spec_name("table:$1") . $view if $path =~ m{\A/view/([^/]+)\z};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[\A/view/([^/]+)\z]u 906 2092 2998
1048  
1049 2092 ▶ if ($path eq '/open') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/open") 132 1960 2092
1050 132 ▶ my $file = $q->param('path') // '';
Condition100%2/2
$q->param("path") // ""
l!l
8943
MC/DC100%1/1
$q->param("path") // ""
$q->param("path")
Truth table100%2/2
A
A= $q->param("path")
Aresult
11
00
1051 132 ▶ return undef unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 44 88 132
1052 88 my $name = Mojo::File->new($file)->basename;
1053 88 my $it = $q->param('it');
1054 88 ▶ $name .= ": $it" if defined $it && length $it;
Branch: 1 branch
BranchTrueFalseTotal
if defined $it and length $it 2 86 88
Condition66%2/3
defined $it and length $it
!ll&&!rl&&r
8602
MC/DC50%1/2
defined $it and length $it
defined $it length $it
Truth table66%2/3
A and B
A= defined $itB= length $it
ABresult
0X0
100
111
1055 88 ▶ return length $name ? "$name$view" : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 88 0 88
1056 }
1057 # Either route takes both joins and combined files.
1058 1960 ▶ if ($path eq '/join' || $path eq '/combine') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/join" or $path eq "/combine") 107 1853 1960
Condition100%3/3
$path eq "/join" or $path eq "/combine"
l!l&&r!l&&!r
56511853
MC/DC100%2/2
$path eq "/join" or $path eq "/combine"
$path eq "/join" $path eq "/combine"
Truth table100%3/3
A or B
A= $path eq "/join"B= $path eq "/combine"
ABresult
1X1
011
000
1059 113 ▶ my @names = map { _crumb_spec_name($_) } grep { /\A(?:table|path|url|redis|mariadb):/i }
Condition100%2/2
$q->param("l") // ""
l!l
1043
MC/DC100%1/1
$q->param("l") // ""
$q->param("l")
Truth table100%2/2
A
A= $q->param("l")
Aresult
11
00
1060 ($q->param('l') // ''),
1061 13 ▶ (map { (split /\|/, $_, 2)[0] // '' } @{ $q->every_param('j') }),
Condition100%2/2
(split(/\|/u, $_, 2))[0] // ""
l!l
112
MC/DC100%1/1
(split(/\|/u, $_, 2))[0] // ""
(split(/\|/u, $_, 2))[0]
Truth table100%2/2
A
A= (split(/\|/u, $_, 2))[0]
Aresult
11
00
1062 107 @{ $q->every_param('c') };
1063 107 ▶ return @names ? join(' + ', @names) . $view : undef;
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 99 8 107
1064 }
1065 1853 ▶ if ($path eq '/import') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/import") 52 1801 1853
1066 52 ▶ my $src = Mojo::URL->new($q->param('url') // '');
Condition100%2/2
$q->param("url") // ""
l!l
646
MC/DC100%1/1
$q->param("url") // ""
$q->param("url")
Truth table100%2/2
A
A= $q->param("url")
Aresult
11
00
1067 52 ▶ return defined $src->host ? $src->host . $view : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $src->host ? : 6 46 52
1068 }
1069 1801 ▶ if ($path =~ m{\A/([a-z]+)\z} && $CRUMB_KIND{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}) 835 966 1801
Condition100%3/3
$path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}
!ll&&!rl&&r
585381835
MC/DC100%2/2
$path =~ m[\A/([a-z]+)\z]u and $CRUMB_KIND{$1}
$path =~ m[\A/([a-z]+)\z]u $CRUMB_KIND{$1}
Truth table100%3/3
A and B
A= $path =~ m[\A/([a-z]+)\z]uB= $CRUMB_KIND{$1}
ABresult
0X0
100
111
1070 835 my $what = _crumb_chart_subject($1, $q);
1071 835 ▶ return $CRUMB_KIND{$1} . (length $what ? ": $what" : '');
Branch: 1 branch
BranchTrueFalseTotal
length $what ? : 682 153 835
1072 }
1073 966 ▶ if ($path eq '/tables') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/tables") 60 906 966
1074 60 ▶ my $spec = $q->param('l') // '';
Condition100%2/2
$q->param("l") // ""
l!l
573
MC/DC100%1/1
$q->param("l") // ""
$q->param("l")
Truth table100%2/2
A
A= $q->param("l")
Aresult
11
00
1075 60 ▶ return length $spec ? _crumb_spec_name($spec) . ' schema' : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $spec ? : 55 5 60
1076 }
1077 906 ▶ if ($path eq '/browse') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/browse") 138 768 906
1078 138 ▶ my $dir = $q->param('path') // '';
Condition100%2/2
$q->param("path") // ""
l!l
9444
MC/DC100%1/1
$q->param("path") // ""
$q->param("path")
Truth table100%2/2
A
A= $q->param("path")
Aresult
11
00
1079 138 ▶ return length $dir ? 'Files: ' . Mojo::File->new($dir)->basename : 'Files';
Branch: 1 branch
BranchTrueFalseTotal
length $dir ? : 92 46 138
1080 }
1081 768 ▶ return 'Catalog' if $path eq '/catalog';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/catalog" 19 749 768
1082 749 ▶ return 'Canvases' if $path eq '/canvases';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/canvases" 42 707 749
1083 707 ▶ return 'Tutorial' if $path eq '/tutorial';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/tutorial" 4 703 707
1084 703 ▶ return 'Preferences' if $path eq '/preferences';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/preferences" 1 702 703
1085 702 ▶ return 'Log in' if $path eq '/login';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/login" 0 702 702
1086 702 ▶ if ($path =~ m{\A/a/([A-Za-z0-9_-]+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/a/([A-Za-z0-9_-]+)]u) 41 661 702
1087 41 my $a = $self->_read_analyses->{$1};
1088 41 ▶ return (ref $a eq 'HASH' && length($a->{name} // '')) ? $a->{name} : 'Canvas';
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'HASH' && length($a->{'name'} // '') ? : 30 11 41
Condition100%5/5
ref $a eq "HASH" and length($a->{"name"} // "")
!ll&&!rl&&r
9230
$a->{"name"} // ""
l!l
302
MC/DC100%2/2
ref $a eq "HASH" and length($a->{"name"} // "")
ref $a eq "HASH" $a->{"name"}
Truth table100%3/3
A and B
A= ref $a eq "HASH"B= $a->{"name"}
ABresult
0X0
100
111
1089 }
1090 661 return undef;
1091 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1092  
1093 # _crumb_spec_name($spec) -> $name
1094 #
1095 # A data source as a person would name it: "sales" for table:sales,
1096 # "store: orders" for an internal table, the file name for path:, the host
1097 # for a web page.
1098 1082 ▶sub _crumb_spec_name {
Subroutine _crumb_spec_name: called CC=4 SCAR=13.9
1099 1082 my ($spec) = @_;
1100 1082 ▶ $spec //= '';
Condition100%2/2
$spec //= ""
l!l
10811
MC/DC100%1/1
$spec //= ""
$spec
Truth table100%2/2
A
A= $spec
Aresult
11
00
1101 1082 ▶ if ($spec =~ m{\Apath:(.+)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\Apath:(.+)\z/u) 18 1064 1082
1102 18 my ($file, $internal) = _split_path($1);
1103 18 my $name = Mojo::File->new($file)->basename;
1104 18 ▶ return defined $internal ? "$name: $internal" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 0 18 18
1105 }
1106 1064 return _crumb_spec_name_other($spec);
1107 }
1108  
1109 # _split_path($path) -> ($file, $internal)
1110 #
1111 # Purpose: What follows "path:" in a spec: the file, and the table it names
1112 # after a "#" (Database::BI::Model::DataSource::split_path).
1113 544 ▶sub _split_path {
Subroutine _split_path: called CC=1 SCAR=0.0
1114 544 my ($path) = @_;
1115 544 require Database::BI::Model::DataSource;
1116 544 return Database::BI::Model::DataSource::split_path($path);
1117 }
1118  
1119 1064 ▶sub _crumb_spec_name_other {
Subroutine _crumb_spec_name_other: called CC=9 SCAR=22.5
1120 1064 my ($spec) = @_;
1121 1064 ▶ $spec //= '';
Condition50%1/2
$spec //= ""
l!l
10640
MC/DC0%0/1
$spec //= ""
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
1122 1064 ▶ return "$1: $2" if $spec =~ m{\Atable:([^/]+)/(.+)\z};
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ m[\Atable:([^/]+)/(.+)\z]u 1 1063 1064
1123 1063 ▶ return $1 if $spec =~ m{\Atable:(.+)\z};
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /\Atable:(.+)\z/u 1054 9 1063
1124 9 ▶ if ($spec =~ m{\Aurl:(.+)\z}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\Aurl:(.+)\z/iu) 3 6 9
1125 3 my $host = Mojo::URL->new($1)->host;
1126 3 ▶ return $host // $1;
Condition33%1/3
$host // $1
l!l&&r!l&&!r
300
MC/DC0%0/2
$host // $1
$host $1
Truth table33%1/3
A // B
A= $hostB= $1
ABresult
1X1
011
000
1127 }
1128 6 ▶ return 'Redis' if $spec =~ /\Aredis:/i;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /\Aredis:/iu 1 5 6
1129 5 ▶ return "$1: $2" if $spec =~ $MARIADB_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /$MARIADB_SPEC_RE/u 0 5 5
1130 5 ▶ return $1 if $spec =~ $MARIADB_SERVER_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /$MARIADB_SERVER_RE/u 0 5 5
1131 5 return $spec;
1132 }
1133  
1134 # _crumb_view_suffix($query) -> " (region = East, last 90 days)" | ''
1135 #
1136 # The part of a table's view that makes it a different page from the whole
1137 # table: its filters and its time range. Two filters are shown; more are
1138 # counted, so a crumb stays short.
1139 3011 ▶sub _crumb_view_suffix {
Subroutine _crumb_view_suffix: called CC=22 SCAR=31.0
1140 3011 my ($q) = @_;
1141 3011 my @bits;
1142 3011 for my $spec (@{ $q->every_param('f') }) {
1143 97 my ($col, $op, $val) = split /:/, $spec, 3;
1144 97 ▶ next unless length($col // '') && length($op // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and length($op // "") 63 34 97
Condition100%7/7
length($col // "") and length($op // "")
!ll&&!rl&&r
422134
$col // ""
l!l
5740
$op // ""
l!l
3421
MC/DC100%2/2
length($col // "") and length($op // "")
$col $op
Truth table100%3/3
A and B
A= $colB= $op
ABresult
0X0
100
111
1145 34 ▶ next unless $col =~ $CRUMB_TEXT_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $col =~ /$CRUMB_TEXT_RE/u 2 32 34
1146 # A list is JSON, which the crumb text pattern refuses: say how long it is.
1147 32 ▶ if ($op eq 'in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "in") 2 30 32
1148 2 my $n = () = _filter_in_values($val);
1149 2 ▶ push @bits, "$col $CRUMB_OP{in} $n value" . ($n == 1 ? '' : 's');
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 0 2 2
1150 2 next;
1151 }
1152 30 ▶ next if length($val // '') && $val !~ $CRUMB_TEXT_RE;
Branch: 1 branch
BranchTrueFalseTotal
if length($val // "") and not $val =~ /$CRUMB_TEXT_RE/u 1 29 30
Condition100%5/5
length($val // "") and not $val =~ /$CRUMB_TEXT_RE/u
!ll&&!rl&&r
2271
$val // ""
l!l
282
MC/DC100%2/2
length($val // "") and not $val =~ /$CRUMB_TEXT_RE/u
$val not $val =~ /$CRUMB_TEXT_RE/u
Truth table100%3/3
A and not(B)
A= $valB= not $val =~ /$CRUMB_TEXT_RE/u
ABresult
0X0
100
111
1153 29 ▶ my $word = $CRUMB_OP{$op} // $op;
Condition33%1/3
$CRUMB_OP{$op} // $op
l!l&&r!l&&!r
2900
MC/DC0%0/2
$CRUMB_OP{$op} // $op
$CRUMB_OP{$op} $op
Truth table33%1/3
A // B
A= $CRUMB_OP{$op}B= $op
ABresult
1X1
011
000
1154 29 ▶ push @bits, ($op eq 'empty' || $op eq 'notempty')
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'empty' || $op eq 'notempty' ? : 2 27 29
Condition60%3/5
$op eq "empty" or $op eq "notempty"
l!l&&r!l&&!r
2027
$val // ""
l!l
270
MC/DC50%1/2
$op eq "empty" or $op eq "notempty"
$op eq "empty" $op eq "notempty"
MC/DC0%0/1
$val // ""
$val
Truth table66%2/3
A or B
A= $op eq "empty"B= $op eq "notempty"
ABresult
1X1
011
000
Truth table50%1/2
A
A= $val
Aresult
11
00
1155 ? "$col $word" : "$col $word " . (($val // '') =~ s/:/ and /r);
1156 }
1157 3011 ▶ my $extra = @bits > 2 ? @bits - 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@bits > 2 ? : 1 3010 3011
1158 3011 ▶ splice @bits, 2 if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 1 3010 3011
1159 3011 ▶ push @bits, "+$extra more" if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 1 3010 3011
1160 3011 ▶ if (my $range = $TIME_RANGE{ $q->param('range') // '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $TIME_RANGE{$q->param("range") // ""}) 1 3010 3011
Condition100%2/2
$q->param("range") // ""
l!l
422969
MC/DC100%1/1
$q->param("range") // ""
$q->param("range")
Truth table100%2/2
A
A= $q->param("range")
Aresult
11
00
1161 1 push @bits, lc $range->{label};
1162 }
1163 3011 ▶ return @bits ? ' (' . join(', ', @bits) . ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@bits ? : 26 2985 3011
1164 }
1165  
1166 # _crumb_chart_subject($kind, $query) -> $text
1167 #
1168 # What a chart shows, from its own parameters: "amount by region",
1169 # "Count by region", "amount". Empty for the pages that are about the
1170 # whole table (stats, insights).
1171 849 ▶sub _crumb_chart_subject {
Subroutine _crumb_chart_subject: called CC=28 SCAR=33.3
1172 849 my ($kind, $q) = @_;
1173 my %p = map {
1174 # loc= may repeat, for a place spread over columns (address, country).
1175 849 ▶ my $v = ($_ eq 'loc') ? join(', ', grep { length } @{ $q->every_param('loc') }) : ($q->param($_) // '');
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'loc' ? : 849 9339 10188
Condition100%2/2
$q->param($_) // ""
l!l
30746265
MC/DC100%1/1
$q->param($_) // ""
$q->param($_)
Truth table100%2/2
A
A= $q->param($_)
Aresult
11
00
1176 10188 ▶ ($_ => ($v =~ $CRUMB_TEXT_RE ? $v : ''));
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /$CRUMB_TEXT_RE/u ? : 2904 7284 10188
1177 } qw(x y cat val seg col rows bar_val line_val loc lat lon);
1178  
1179 849 ▶ return "$p{y} by $p{x}" if ($kind eq 'graph' || $kind eq 'line' || $kind eq 'scatter') && length $p{x} && length $p{y};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "graph" || $kind eq "line" || $kind eq "scatter" and length $p{"x"} and length $p{"y"} 139 710 849
Condition100%12/12
$kind eq "graph" || $kind eq "line" || $kind eq "scatter" and length $p{"x"} and length $p{"y"}
!ll&&!rl&&r
7082139
$kind eq "graph" || $kind eq "line" || $kind eq "scatter" and length $p{"x"}
!ll&&!rl&&r
6999141
$kind eq "graph" or $kind eq "line" or $kind eq "scatter"
l!l&&r!l&&!r
10149699
$kind eq "graph" or $kind eq "line"
l!l&&r!l&&!r
5744748
MC/DC100%5/5
$kind eq "graph" || $kind eq "line" || $kind eq "scatter" and length $p{"x"} and length $p{"y"}
$kind eq "graph" $kind eq "line" $kind eq "scatter" length $p{"x"} length $p{"y"}
Truth table90%9/10
A or B or C and D and E
A= $kind eq "graph"B= $kind eq "line"C= $kind eq "scatter"D= length $p{"x"}E= length $p{"y"}
ABCDEresult
000XX0
1XX0X0
01X0X0
0010X0
1XX100
01X100
001100
1XX111
01X111
001111
1180 710 ▶ return "$p{x} by $p{y}" if $kind eq 'heatmap' && length $p{x} && length $p{y};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "heatmap" and length $p{"x"} and length $p{"y"} 53 657 710
Condition100%6/6
$kind eq "heatmap" and length $p{"x"} and length $p{"y"}
!ll&&!rl&&r
656153
$kind eq "heatmap" and length $p{"x"}
!ll&&!rl&&r
652454
MC/DC100%3/3
$kind eq "heatmap" and length $p{"x"} and length $p{"y"}
$kind eq "heatmap" length $p{"x"} length $p{"y"}
Truth table100%4/4
A and B and C
A= $kind eq "heatmap"B= length $p{"x"}C= length $p{"y"}
ABCresult
0XX0
10X0
1100
1111
1181 657 ▶ return "$p{cat} by $p{seg}" if $kind eq 'stacked' && length $p{cat} && length $p{seg};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "stacked" and length $p{"cat"} and length $p{"seg"} 2 655 657
Condition100%6/6
$kind eq "stacked" and length $p{"cat"} and length $p{"seg"}
!ll&&!rl&&r
611442
$kind eq "stacked" and length $p{"cat"}
!ll&&!rl&&r
608346
MC/DC100%3/3
$kind eq "stacked" and length $p{"cat"} and length $p{"seg"}
$kind eq "stacked" length $p{"cat"} length $p{"seg"}
Truth table100%4/4
A and B and C
A= $kind eq "stacked"B= length $p{"cat"}C= length $p{"seg"}
ABCresult
0XX0
10X0
1100
1111
1182 655 ▶ return $p{rows} =~ s/,/, /gr if $kind eq 'pivot';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "pivot" 50 605 655
1183 605 ▶ return length $p{loc} ? $p{loc} : (length $p{lat} ? "$p{lat}, $p{lon}" : '') if $kind eq 'map';
Branch: 3 branches
BranchTrueFalseTotal
if $kind eq "map" 50 555 605
length $p{'loc'} ? : 2 48 50
length $p{'lat'} ? : 3 45 48
1184  
1185 555 ▶ my $dim = (grep { length } @p{qw(cat x col)})[0] // '';
Condition100%2/2
(grep {length $_;} @p{"cat", "x", "col"})[0] // ""
l!l
48768
MC/DC100%1/1
(grep {length $_;} @p{"cat", "x", "col"})[0] // ""
(grep {length $_;} @p{"cat", "x", "col"})[0]
Truth table100%2/2
A
A= (grep {length $_;} @p{"cat", "x", "col"})[0]
Aresult
11
00
1186 555 ▶ my $measure = (grep { length } @p{qw(val bar_val y)})[0] // '';
Condition100%2/2
(grep {length $_;} @p{"val", "bar_val", "y"})[0] // ""
l!l
444111
MC/DC100%1/1
(grep {length $_;} @p{"val", "bar_val", "y"})[0] // ""
(grep {length $_;} @p{"val", "bar_val", "y"})[0]
Truth table100%2/2
A
A= (grep {length $_;} @p{"val", "bar_val", "y"})[0]
Aresult
11
00
1187 555 ▶ $measure = 'Count' if $measure eq '__count__';
Branch: 1 branch
BranchTrueFalseTotal
if $measure eq "__count__" 3 552 555
1188 555 ▶ $measure = 'Count' if !length $measure && ($kind eq 'pie' || $kind eq 'bar' || $kind eq 'treemap');
Branch: 1 branch
BranchTrueFalseTotal
if not length $measure and $kind eq "pie" || $kind eq "bar" || $kind eq "treemap" 36 519 555
Condition100%9/9
not length $measure and $kind eq "pie" || $kind eq "bar" || $kind eq "treemap"
!ll&&!rl&&r
4447536
$kind eq "pie" or $kind eq "bar" or $kind eq "treemap"
l!l&&r!l&&!r
29775
$kind eq "pie" or $kind eq "bar"
l!l&&r!l&&!r
42582
MC/DC100%4/4
not length $measure and $kind eq "pie" || $kind eq "bar" || $kind eq "treemap"
not length $measure $kind eq "pie" $kind eq "bar" $kind eq "treemap"
Truth table100%5/5
not(A) and B or C or D
A= not length $measureB= $kind eq "pie"C= $kind eq "bar"D= $kind eq "treemap"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1189 555 ▶ return length $measure && length $dim ? "$measure by $dim" : $dim;
Branch: 1 branch
BranchTrueFalseTotal
length $measure && length $dim ? : 451 104 555
Condition100%3/3
length $measure and length $dim
!ll&&!rl&&r
7529451
MC/DC100%2/2
length $measure and length $dim
length $measure length $dim
Truth table100%3/3
A and B
A= length $measureB= length $dim
ABresult
0X0
100
111
1190 }
1191  
1192 # _resolve_template($self) -> ($platform, $language)
1193 #
1194 # Purpose: Read platform and language from config, then resolve the Accept-Language
1195 # header to a language code -- falling back to the config language, and
1196 # then to the default language, when no template directory exists for
1197 # the code that was resolved.
1198 # Exit: Returns ($platform, $language) -- both guaranteed non-empty strings.
1199 962 ▶sub _resolve_template :Protected ($self) {
Subroutine _resolve_template: called CC=5 SCAR=16.1
1200 962 my $conf = $self->app->config();
1201 962 ▶ my $cfg_platform = $conf->{platform} // 'web';
Condition100%2/2
$conf->{"platform"} // "web"
l!l
9611
MC/DC100%1/1
$conf->{"platform"} // "web"
$conf->{"platform"}
Truth table100%2/2
A
A= $conf->{"platform"}
Aresult
11
00
1202 962 ▶ my $cfg_language = $conf->{language} // 'en';
Condition100%2/2
$conf->{"language"} // "en"
l!l
9611
MC/DC100%1/1
$conf->{"language"} // "en"
$conf->{"language"}
Truth table100%2/2
A
A= $conf->{"language"}
Aresult
11
00
1203  
1204 # Detect platform from the request User-Agent via CGI::Info.
1205 # Falls back to the configured platform when:
1206 # (a) CGI::Info is unavailable or throws, or
1207 # (b) no templates/<detected>/ directory exists.
1208 962 ▶ my $platform = _detect_platform(
Condition100%2/2
$self->req->headers->user_agent // ""
l!l
9602
MC/DC100%1/1
$self->req->headers->user_agent // ""
$self->req->headers->user_agent
Truth table100%2/2
A
A= $self->req->headers->user_agent
Aresult
11
00
1209 $self->req->headers->user_agent // '',
1210 $cfg_platform,
1211 );
1212 962 ▶ unless (-d $self->app->home->child("templates/$platform")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->app->home->child("templates/$platform")) 2 960 962
1213 2 $platform = $cfg_platform;
1214 }
1215  
1216 962 my $language = $self->_resolve_language($platform, $cfg_language);
1217 962 return ($platform, $language);
1218 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1219  
1220 # _detect_platform($user_agent, $fallback) -> $platform_string
1221 #
1222 # Purpose: Map a raw User-Agent string to a VWF platform dimension
1223 # ('mobile', 'tablet', or 'web') via CGI::Info.
1224 # Entry: $user_agent -- the raw HTTP User-Agent header value (may be empty).
1225 # $fallback -- value to return if detection fails.
1226 # Exit: Returns 'mobile', 'tablet', 'web', or $fallback.
1227 # Side Effects: temporarily sets $ENV{HTTP_USER_AGENT} with local().
1228 966 ▶sub _detect_platform ($user_agent, $fallback) {
Subroutine _detect_platform: called CC=4 SCAR=14.5
1229 966 my $platform = eval {
1230 966 local $ENV{HTTP_USER_AGENT} = $user_agent;
1231 966 my $info = CGI::Info->new();
1232 966 ▶ $info->is_mobile() ? 'mobile'
Branch: 2 branches
BranchTrueFalseTotal
$info->is_mobile ? : 4 962 966
$info->is_tablet ? : 0 962 962
1233 : $info->is_tablet() ? 'tablet'
1234 : 'web';
1235 };
1236 966 ▶ return $platform // $fallback;
Condition33%1/3
$platform // $fallback
l!l&&r!l&&!r
96600
MC/DC0%0/2
$platform // $fallback
$platform $fallback
Truth table33%1/3
A // B
A= $platformB= $fallback
ABresult
1X1
011
000
1237 }
1238  
1239 # _resolve_language($self, $platform, $default) -> $language_code
1240 #
1241 # Purpose: Determine the best ISO 639-1 language code for this request.
1242 # When Accept-Language is present, CGI::Lingua selects the best
1243 # match from the languages that have a template directory under
1244 # templates/<platform>/. $default answers when the header is absent,
1245 # no templates exist for the matched language, or CGI::Lingua is
1246 # unavailable. The result has a template directory whenever one
1247 # exists: a code without one - the configured language included -
1248 # becomes the default language, and only when that has no directory
1249 # either is the resolved code returned, so the missing template names
1250 # what was asked for.
1251 # Entry: $platform is the resolved VWF platform string.
1252 # $default is a non-empty fallback language code (e.g. 'en').
1253 # Exit: Returns a two-letter ISO 639-1 language code string, or a code
1254 # and region (en-us) when the browser names that region and
1255 # templates/<platform>/<code>-<region>/ exists.
1256 # Side Effects: filesystem stats for template directories;
1257 # temporarily sets $ENV{HTTP_ACCEPT_LANGUAGE} with local().
1258 967 ▶sub _resolve_language :Protected ($self, $platform, $default) {
Subroutine _resolve_language: called CC=7 SCAR=19.6
1259 967 ▶ my $accept = $self->req->headers->accept_language() // '';
Condition100%2/2
$self->req->headers->accept_language // ""
l!l
25942
MC/DC100%1/1
$self->req->headers->accept_language // ""
$self->req->headers->accept_language
Truth table100%2/2
A
A= $self->req->headers->accept_language
Aresult
11
00
1260  
1261 967 my $language = $default;
1262 967 ▶ if ($accept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept) 24 943 967
1263 # Discover supported languages from template directories so CGI::Lingua
1264 # only returns a code we can actually serve. The configured language is
1265 # offered as well, so a request that asks for it gets it, whether or not
1266 # it can be served - the check below decides that.
1267 24 my @supported = _template_languages($self->app->home, $platform);
1268 24 ▶ push @supported, $default unless grep { $_ eq $default } @supported;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $default;} @supported 0 24 24
1269  
1270 # A regional variant (templates/web/en-us/ for a browser asking for
1271 # en-US) is chosen when it has a directory, otherwise the language
1272 # itself: en-GB, en-CA and plain en get en. Database::BI::I18N makes
1273 # the same CGI::Lingua call for the messages, so the two agree.
1274 24 my $chosen = Database::BI::I18N::lingua_choice($accept, \@supported);
1275 24 ▶ $language = $chosen if defined $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chosen 18 6 24
1276 }
1277  
1278 # Confirm a template directory exists for the language we are about to
1279 # render from. Naming a language does not give it templates, and rendering
1280 # a directory that is not there is a 500 rather than a translation. This
1281 # covers the configured language itself and not just the one Accept-Language
1282 # produced: the documented fallback used to happen only when the two
1283 # differed, so the ordinary case - an operator setting language => 'de'
1284 # before adding templates/web/de/ - reached the renderer unchecked.
1285 967 my $home = $self->app->home;
1286 967 ▶ return $language if _has_templates($home, $platform, $language);
Branch: 1 branch
BranchTrueFalseTotal
if _has_templates($home, $platform, $language) 966 1 967
1287 1 ▶ return $DEFAULT_LANGUAGE
Branch: 1 branch
BranchTrueFalseTotal
if _has_templates($home, $platform, $DEFAULT_LANGUAGE) 1 0 1
1288 if _has_templates($home, $platform, $DEFAULT_LANGUAGE);
1289  
1290 # Neither exists. Keep what was resolved, so the missing template names the
1291 # language the request asked for rather than one nobody configured.
1292 0 return $language;
1293 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1294  
1295 # _template_languages($home, $platform) -> @codes
1296 #
1297 # The languages that have a template directory for the platform, sorted so that
1298 # the answer does not depend on the order the filesystem returns entries in.
1299 # Plain functions taking the application home rather than methods: the tests
1300 # call _resolve_language with a mock controller, and a method call on it would
1301 # look for the helper in the mock's package rather than this one.
1302 26 ▶sub _template_languages ($home, $platform) {
Subroutine _template_languages: called CC=2 SCAR=6.9
1303 26 my $base = $home->child("templates/$platform");
1304 26 ▶ return () unless -d $base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $base 1 25 26
1305  
1306 50 return sort map { Mojo::File->new($_)->basename }
1307 25 grep { -d $_ } @{ $base->list({ dir => 1 }) };
1308 }
1309  
1310 # _has_templates($home, $platform, $language) -> 1 | 0
1311 #
1312 # Whether the application can render a language: the controller builds template
1313 # names as "<platform>/<language>/<page>", so a language without a directory
1314 # under templates/<platform>/ cannot be served at all.
1315 973 ▶sub _has_templates ($home, $platform, $language) {
Subroutine _has_templates: called CC=4 SCAR=13.9
1316 973 ▶ return 0 unless defined $language && length $language;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $language and length $language 2 971 973
Condition100%3/3
defined $language and length $language
!ll&&!rl&&r
11971
MC/DC100%2/2
defined $language and length $language
defined $language length $language
Truth table100%3/3
A and B
A= defined $languageB= length $language
ABresult
0X0
100
111
1317  
1318 971 ▶ return -d $home->child("templates/$platform/$language") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-d $home->child("templates/$platform/$language") ? : 968 3 971
1319 }
1320  
1321 # _data_dir_for($app) -> Mojo::File
1322 #
1323 # The configured data directory. A relative value is resolved against the
1324 # application home, which is the historical behaviour; an absolute one is used
1325 # as given, because Mojo::File->child() appends an absolute path rather than
1326 # replacing the base. It takes the app rather than $self because the unit
1327 # tests call the scanning helpers with a bare controller.
1328 7541 ▶sub _data_dir_for ($app) {
Subroutine _data_dir_for: called CC=3 SCAR=11.0
1329 7541 ▶ my $dir = $app->config->{data_dir} // 'data';
Condition100%2/2
$app->config->{"data_dir"} // "data"
l!l
75401
MC/DC100%1/1
$app->config->{"data_dir"} // "data"
$app->config->{"data_dir"}
Truth table100%2/2
A
A= $app->config->{"data_dir"}
Aresult
11
00
1330 7541 my $file = Mojo::File->new($dir);
1331 7541 ▶ return $file if $file->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $file->is_abs 320 7221 7541
1332 7221 return $app->home->child($dir);
1333 }
1334  
1335 # _scan_data_dir($self) -> \@table_list
1336 #
1337 # Purpose: Return an arrayref of { name, file } hashrefs for every supported
1338 # data file in the configured data_dir. Used by the index action; the
1339 # join panel uses _scan_join_tables, which also lists internal tables.
1340 # Entry: None.
1341 # Exit: Returns [] when data_dir does not exist or contains no supported files.
1342 # Side Effects: Filesystem directory read.
1343 #
1344 # Optimisation: single-pass map replaces a two-pass grep+map chain, and calls
1345 # basename() only once per entry (the original chain called it twice: once in
1346 # grep to check the extension and again in map to extract the stem).
1347 # ->to_array avoids the extra flat-list expansion that ->each produced.
1348 312 ▶sub _scan_data_dir :Protected ($self) {
Subroutine _scan_data_dir: called CC=3 SCAR=11.3
1349 312 my $dir = _data_dir_for($self->app);
1350 312 ▶ return [] unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 310 312
1351  
1352 # A directory that exists but may not be read holds nothing this application
1353 # can show, and must not take the home page with it.
1354 310 my $files = eval { $dir->list };
1355 310 ▶ unless ($files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($files) 0 310 310
1356 0 $self->app->log->error("Cannot read data_dir $dir: " . _tidy_error($@));
1357 0 return [];
1358 }
1359 3400 ▶ return $files->map(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1360 3400 my $base = $_->basename;
1361 3400 ▶ return unless $base =~ $EXT_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /$EXT_RE/u 0 3400 3400
1362 3400 (my $name = $base) =~ s/\.[^.]+\z//;
1363 3400 { name => $name, file => $base, path => $_->to_string }
1364 310 })->to_array;
1365 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1366  
1367 # _scan_join_tables($self) -> \@table_list
1368 #
1369 # Purpose: Like _scan_data_dir, but each multi-table SQLite file contributes
1370 # one entry per internal table as well as the file entry. The join
1371 # and combine panels need this: "table:store/customer" names a table
1372 # inside store.db, which a plain file name cannot do.
1373 # Entry: None.
1374 # Exit: Returns [] when data_dir does not exist or has no supported files.
1375 # Every entry has a spec suitable for the panel's option value.
1376 # Side Effects: Reads data_dir and opens each SQLite file to list its tables.
1377 204 ▶sub _scan_join_tables :Protected ($self) {
Subroutine _scan_join_tables: called CC=8 SCAR=21.2
1378 204 my $dir = _data_dir_for($self->app);
1379 204 my $tables = $self->_scan_data_dir;
1380 204 my @all;
1381  
1382 204 for my $table (@$tables) {
1383 2223 push @all, { %$table, spec => "table:$table->{name}" };
1384 2223 ▶ next unless $table->{file} =~ /\.(?:db|sql|sqlite|sqlite3)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $table->{"file"} =~ /\.(?:db|sql|sqlite|sqlite3)\z/iu 2021 202 2223
1385  
1386 202 my $source = eval { $self->open_table($table->{name}, directory => $dir->to_string) };
1387 202 ▶ next unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 202 202
1388  
1389 202 my $internal_tables = eval { $source->sqlite_tables };
1390 202 ▶ next unless $internal_tables && @$internal_tables > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $internal_tables and @$internal_tables > 1 0 202 202
Condition33%1/3
$internal_tables and @$internal_tables > 1
!ll&&!rl&&r
00202
MC/DC0%0/2
$internal_tables and @$internal_tables > 1
$internal_tables @$internal_tables > 1
Truth table33%1/3
A and B
A= $internal_tablesB= @$internal_tables > 1
ABresult
0X0
100
111
1391  
1392 202 my @internal = @$internal_tables;
1393  
1394 202 for my $name (@internal) {
1395 606 ▶ next unless $name =~ $TABLE_NAME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$TABLE_NAME_RE/u 0 606 606
1396 606 push @all, {
1397 %$table,
1398 name => "$table->{name} / $name",
1399 internal => $name,
1400 spec => "table:$table->{name}/$name",
1401 };
1402 }
1403 }
1404  
1405 204 return \@all;
1406 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1407  
1408 # _tidy_error($error) -> $string
1409 #
1410 # Purpose: A backend error as a reader should see it: without the "at FILE line N"
1411 # that croak adds each time the error is passed up, or the list of
1412 # library directories perl adds when a module is missing.
1413 49 ▶sub _tidy_error {
Subroutine _tidy_error: called CC=2 SCAR=6.9
1414 49 ▶ my $error = $_[0] // '';
Condition100%2/2
$_[0] // ""
l!l
472
MC/DC100%1/1
$_[0] // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
1415 49 $error =~ s/\s*\(\@INC (?:contains|entries checked):.*?\)(?=\s+at\s|\s*\z)//sg;
1416 49 $error =~ s/(?:\s+at \S.*? line \d+\.?)+\s*\z//;
1417 49 return $error;
1418 }
1419  
1420 # _analysis_json_error($error) -> $string
1421 #
1422 # Purpose: The message for a canvas request whose body is not a JSON object.
1423 # Entry: the error from decode_json, which is empty when the body parsed
1424 # but was an array or a scalar, and otherwise ends in a library's path.
1425 31 ▶sub _analysis_json_error {
Subroutine _analysis_json_error: called CC=2 SCAR=6.9
1426 31 my $reason = _tidy_error($_[0]);
1427 31 ▶ return sprintf(_message_text('error_analysis_json'), length($reason) ? $reason : 'expected a JSON object');
Branch: 1 branch
BranchTrueFalseTotal
length $reason ? : 13 18 31
1428 }
1429  
1430 # ---------------------------------------------------------------------------
1431 # Accelerated views (Phase 7 item 7.5)
1432 #
1433 # A dataset marked in models.json accelerated.<spec> is read from a SQLite copy
1434 # of itself, built by script/bi-extract through Database::BI::Extract, as long
1435 # as the copy's fingerprint (the spec, and the file's mtime and size) still
1436 # matches the file. When it does not, the file itself is read and the table
1437 # page says the extract is stale: stale numbers are never served. An extract is
1438 # a cache. Deleting <model_dir>/extracts/ only makes every accelerated dataset
1439 # read its file again, and nothing is ever written under data_dir.
1440 # ---------------------------------------------------------------------------
1441  
1442 # _model_dir_of($app) -> Mojo::File
1443 #
1444 # Purpose: model_dir as a path: absolute as configured, or under the app home.
1445 # Nothing is created and nothing is checked; the writers check.
1446 7 ▶sub _model_dir_of {
Subroutine _model_dir_of: called CC=3 SCAR=11.0
1447 7 my ($app) = @_;
1448 7 ▶ my $cfg = Mojo::File->new($app->config('model_dir') // '.bi');
Condition100%2/2
$app->config("model_dir") // ".bi"
l!l
61
MC/DC100%1/1
$app->config("model_dir") // ".bi"
$app->config("model_dir")
Truth table100%2/2
A
A= $app->config("model_dir")
Aresult
11
00
1449 7 ▶ return $cfg->is_abs ? $cfg : $app->home->child($cfg->to_string);
Branch: 1 branch
BranchTrueFalseTotal
$cfg->is_abs ? : 5 2 7
1450 }
1451  
1452 # _table_stem($data_dir, $name, @exts) -> $stem | undef
1453 #
1454 # Purpose: The file stem table:<name> means in data_dir: the name as written
1455 # when a file has it, else in lower case, which is what every reader
1456 # once looked for (so table:Orders still opens orders.csv); undef
1457 # when neither has a file with one of @exts (default @SUPPORTED_EXT).
1458 # Reading only the lower case made a file named with a capital,
1459 # BandMusic.csv, open on its own page (/view/BandMusic) and nowhere
1460 # else: a pivot, chart or merge of table:BandMusic was a 404.
1461 896 ▶sub _table_stem ($data_dir, $name, @exts) {
Subroutine _table_stem: called CC=4 SCAR=13.9
1462 896 ▶ @exts = @SUPPORTED_EXT unless @exts;
Branch: 1 branch
BranchTrueFalseTotal
unless @exts 852 44 896
1463 896 for my $stem ($name, lc $name) {
1464 961 ▶ return $stem if grep { -f $data_dir->child("$stem.$_") } @exts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {-f $data_dir->child("$stem.$_");} @exts 831 130 961
1465 }
1466 65 return undef; ## no critic (ProhibitExplicitReturnUndef)
1467 }
1468  
1469 # _accel_file($self, $spec) -> $path | undef
1470 #
1471 # Purpose: The local file a spec can be accelerated from: table:<name> in
1472 # data_dir, or path:<file>. An internal table, a remote path and
1473 # every other kind of spec have none.
1474 16 ▶sub _accel_file :Protected ($self, $spec) {
Subroutine _accel_file: called CC=9 SCAR=22.0
1475 16 ▶ $spec //= '';
Condition100%2/2
$spec //= ""
l!l
151
MC/DC100%1/1
$spec //= ""
$spec
Truth table100%2/2
A
A= $spec
Aresult
11
00
1476 16 ▶ if ($spec =~ m{\Atable:([A-Za-z0-9_]+)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\Atable:([A-Za-z0-9_]+)\z/u) 4 12 16
1477 4 my $data_dir = _data_dir_for($self->app);
1478 4 ▶ my $stem = _table_stem($data_dir, $1) // return undef;
Condition100%2/2
_table_stem($data_dir, $1) // (return undef)
l!l
22
MC/DC100%1/1
_table_stem($data_dir, $1) // (return undef)
_table_stem($data_dir, $1)
Truth table100%2/2
A
A= _table_stem($data_dir, $1)
Aresult
11
00
1479 2 my ($file) = grep { -f } map { $data_dir->child("$stem.$_")->to_string } @SUPPORTED_EXT;
1480 2 return $file;
1481 }
1482 12 ▶ if ($spec =~ m{\Apath:(?!/\.\./)(.+)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[\Apath:(?!/\.\./)(.+)\z]u) 7 5 12
1483 7 my ($path) = _split_path($1);
1484 7 ▶ return undef if _outside_roots($self, $path);
Branch: 1 branch
BranchTrueFalseTotal
if _outside_roots($self, $path) 0 7 7
1485 7 my $file = eval { Mojo::File->new($path)->realpath };
1486 7 ▶ return (defined $file && -f $file && $file->basename =~ $EXT_RE) ? $file->to_string : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $file && -f $file && $file->basename =~ /$EXT_RE/u ? : 5 2 7
Condition66%4/6
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
!ll&&!rl&&r
205
defined $file and -f $file
!ll&&!rl&&r
025
MC/DC33%1/3
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
defined $file -f $file $file->basename =~ /$EXT_RE/u
Truth table50%2/4
A and B and C
A= defined $fileB= -f $fileC= $file->basename =~ /$EXT_RE/u
ABCresult
0XX0
10X0
1100
1111
1487 }
1488 5 return undef;
1489 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1490  
1491 # _accelerated_source($self, $spec) -> $DataSource | undef
1492 #
1493 # Purpose: The extract of an accelerated dataset, when it still matches the file.
1494 # Exit: The extract opened as a source, or undef: the spec is not accelerated,
1495 # or its extract is missing or stale, and the caller reads the file.
1496 # The first call in a request stashes acceleration => { state =>
1497 # 'fresh' | 'stale', built => epoch, spec => $spec } for the table page.
1498 1084 ▶sub _accelerated_source :Protected ($self, $spec) {
Subroutine _accelerated_source: called CC=14 SCAR=26.8
1499 1084 ▶ return undef if $self->stash('_no_acceleration');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_no_acceleration") 2 1082 1084
1500 1082 ▶ return undef unless defined $spec && length $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and length $spec 7 1075 1082
Condition66%2/3
defined $spec and length $spec
!ll&&!rl&&r
071075
MC/DC50%1/2
defined $spec and length $spec
defined $spec length $spec
Truth table66%2/3
A and B
A= defined $specB= length $spec
ABresult
0X0
100
111
1501 # Only this application's controller has a model store; a bare controller
1502 # (a test's, calling _open_spec as a function) has nothing accelerated.
1503 1075 ▶ return undef unless $self->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa(__PACKAGE__) 7 1068 1075
1504 1068 $spec = canonical_spec($spec);
1505 1068 my $entry = $self->_read_models->{accelerated}{$spec};
1506 1068 ▶ return undef unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 1066 2 1068
1507 2 ▶ my $file = $self->_accel_file($spec) // return undef;
Condition50%1/2
$self->_accel_file($spec) // (return undef)
l!l
20
MC/DC0%0/1
$self->_accel_file($spec) // (return undef)
$self->_accel_file($spec)
Truth table50%1/2
A
A= $self->_accel_file($spec)
Aresult
11
00
1508  
1509 2 my @st = stat $file;
1510 2 my $cache = _extract_store($self->app);
1511 2 my $path = $cache->get($spec, $st[9], $st[7]);
1512 2 my $src;
1513 2 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 1 1 2
1514 1 my $x = Mojo::File->new($path);
1515 1 $src = eval { $self->open_table($x->basename =~ s/\.db\z//r, directory => $x->dirname->to_string,
1516 1 ▶ (ref $entry->{columns} eq 'ARRAY' ? (column_order => $entry->{columns}) : ())) };
Branch: 1 branch
BranchTrueFalseTotal
ref $entry->{'columns'} eq 'ARRAY' ? : 1 0 1
1517 }
1518 2 require POSIX;
1519 2 ▶ my $built = $src ? (stat $path)[9] : $entry->{built};
Branch: 1 branch
BranchTrueFalseTotal
$src ? : 1 1 2
1520 2 ▶ $self->stash(acceleration => {
Branch: 3 branches
BranchTrueFalseTotal
unless $self->stash("acceleration") 2 0 2
$src ? : 1 1 2
defined $built && $built =~ /\A\d+\z/u ? : 2 0 2
Condition33%1/3
defined $built and $built =~ /\A\d+\z/u
!ll&&!rl&&r
002
MC/DC0%0/2
defined $built and $built =~ /\A\d+\z/u
defined $built $built =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= defined $builtB= $built =~ /\A\d+\z/u
ABresult
0X0
100
111
1521 spec => $spec,
1522 state => ($src ? 'fresh' : 'stale'),
1523 built => $built,
1524 built_text => (defined $built && $built =~ /\A\d+\z/ ? Database::BI::Util::minute_stamp(localtime $built) : ''),
1525 }) unless $self->stash('acceleration');
1526 2 return $src;
1527 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1528  
1529 # _extract_store($app) -> Database::BI::Extract
1530 #
1531 # Purpose: The extract store, in the cache directory Database::BI::CacheDir
1532 # chooses (Object::Configure, CACHEDIR, CACHE_DIR, cache_dir in the
1533 # config), else in <model_dir>/extracts as before. A cache directory
1534 # inside data_dir is not used for them: each extract is a .db file,
1535 # which the home page would list as a table of the user's own, and
1536 # nothing is written under data_dir. They then go in model_dir, whose
1537 # own check (_calc_models_path) stops a store inside data_dir.
1538 4 ▶sub _extract_store {
Subroutine _extract_store: called CC=2 SCAR=7.5
1539 4 my ($app) = @_;
1540 4 require Database::BI::Extract;
1541 4 my $model_dir = _model_dir_of($app);
1542 4 my $store = Database::BI::Extract->new(
1543 model_dir => $model_dir->to_string,
1544 config => $app->config,
1545 home => $app->home->to_string,
1546 );
1547 4 ▶ if (_inside_dir(_canonical_path(Mojo::File->new($store->dir)), _canonical_path(_data_dir_for($app)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inside_dir(_canonical_path("Mojo::File"->new($store->dir)), _canonical_path(_data_dir_for($app)))) 0 4 4
1548 0 carp 'The cache directory is inside data_dir; extracts are kept in model_dir instead';
1549 0 $store = Database::BI::Extract->new(dir => $model_dir->child('extracts')->to_string);
1550 }
1551 4 return $store;
1552 }
1553  
1554 # _is_sqlite_file($path) -> $bool
1555 #
1556 # Purpose: True for a SQLite database, whatever its extension (a .db may be one).
1557 9 ▶sub _is_sqlite_file {
Subroutine _is_sqlite_file: called CC=4 SCAR=13.9
1558 9 my ($path) = @_;
1559 9 ▶ open my $fh, '<:raw', $path or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or return 0 7 2 9
1560 7 my $n = read $fh, my $head, 16;
1561 7 close $fh;
1562 7 ▶ return ($n && $head eq "SQLite format 3\0") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n && $head eq "SQLite format 3\000" ? : 2 5 7
Condition100%3/3
$n and $head eq "SQLite format 3\0"
!ll&&!rl&&r
142
MC/DC100%2/2
$n and $head eq "SQLite format 3\0"
$n $head eq "SQLite format 3\0"
Truth table100%3/3
A and B
A= $nB= $head eq "SQLite format 3\0"
ABresult
0X0
100
111
1563 }
1564  
1565 # accelerate($c, $spec) -> \%built
1566 #
1567 # Purpose: Mark a dataset accelerated and build its extract: the rows its file
1568 # gives, in a SQLite file in the extract store (_extract_store). Called by
1569 # script/bi-extract; not a route.
1570 # Entry: $c is a controller of the application (Mojolicious build_controller);
1571 # $spec is a table: or path: spec naming a local, non-SQLite file.
1572 # Exit: { spec, path, rows, columns, built } -- the extract written and when.
1573 # The model store records accelerated.<spec> = { built, file, rows }.
1574 # Croaks error_accel_spec, error_accel_sqlite, error_accel_open,
1575 # error_accel_store or error_accel_build.
1576 # Side Effects: Writes the extract, removes the one it replaces, and writes
1577 # models.json.
1578 5 ▶sub accelerate {
Subroutine accelerate: called CC=17 SCAR=30.6
Pod documented
1579 5 my ($c, $spec) = @_;
1580 5 ▶ croak 'accelerate() needs a controller and a spec' unless ref $c && defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and defined $spec 1 4 5
Condition66%2/3
ref $c and defined $spec
!ll&&!rl&&r
104
MC/DC50%1/2
ref $c and defined $spec
ref $c defined $spec
Truth table66%2/3
A and B
A= ref $cB= defined $spec
ABresult
0X0
100
111
1581 # Filed under the resolved spec, which is what the file's page asks for;
1582 # messages name the spec as the caller wrote it.
1583 4 my $key = canonical_spec($spec);
1584 4 ▶ my $file = $c->_accel_file($spec)
Condition100%2/2
$c->_accel_file($spec) // croak($c->_i18n("error_accel_spec", $spec))
l!l
31
MC/DC100%1/1
$c->_accel_file($spec) // croak($c->_i18n("error_accel_spec", $spec))
$c->_accel_file($spec)
Truth table100%2/2
A
A= $c->_accel_file($spec)
Aresult
11
00
1585 // croak $c->_i18n('error_accel_spec', $spec);
1586 3 ▶ croak $c->_i18n('error_accel_sqlite', $spec) if $file =~ /\.(?:sql|sqlite3?)\z/i || _is_sqlite_file($file);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.(?:sql|sqlite3?)\z/iu or _is_sqlite_file($file) 1 2 3
Condition66%2/3
$file =~ /\.(?:sql|sqlite3?)\z/iu or _is_sqlite_file($file)
l!l&&r!l&&!r
012
MC/DC50%1/2
$file =~ /\.(?:sql|sqlite3?)\z/iu or _is_sqlite_file($file)
$file =~ /\.(?:sql|sqlite3?)\z/iu _is_sqlite_file($file)
Truth table66%2/3
A or B
A= $file =~ /\.(?:sql|sqlite3?)\z/iuB= _is_sqlite_file($file)
ABresult
1X1
011
000
1587 2 ▶ defined $c->_calc_models_path or croak $c->_i18n('error_accel_store', $spec);
Branch: 1 branch
BranchTrueFalseTotal
defined $c->_calc_models_path or croak($c->_i18n("error_accel_store", $spec)) 1 1 2
1588  
1589 # Read the file itself, not the extract being replaced.
1590 1 local $c->stash->{_no_acceleration} = 1;
1591 1 my @st = stat $file;
1592 1 my ($src) = $c->_open_spec($spec);
1593 1 ▶ croak $c->_i18n('error_accel_open', $spec, _tidy_error($c->stash('open_spec_error') // 'unreadable')) unless $src;
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 1 1
Condition0%0/2
$c->stash("open_spec_error") // "unreadable"
l!l
00
MC/DC0%0/1
$c->stash("open_spec_error") // "unreadable"
$c->stash("open_spec_error")
Truth table0%0/2
A
A= $c->stash("open_spec_error")
Aresult
11
00
1594 1 my ($records, @columns);
1595 1 my $ok = eval {
1596 1 $records = $src->fetch_all;
1597 1 @columns = _get_columns($src, $records);
1598 1 1;
1599 };
1600 1 ▶ croak $c->_i18n('error_accel_open', $spec, _tidy_error($@)) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
1601 1 my $bytes = eval { $c->_write_sqlite_db($records, \@columns, 'extract') };
1602 1 ▶ croak $c->_i18n('error_accel_build', $spec, _tidy_error($@)) unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 1 1
1603  
1604 1 my $cache = _extract_store($c->app);
1605 1 my $path = eval { $cache->put($key, $st[9], $st[7], $bytes) };
1606 1 ▶ croak $c->_i18n('error_accel_build', $spec, _tidy_error($@)) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 1 1
1607  
1608 1 my $models = $c->_read_models;
1609 1 my $old = $models->{accelerated}{$key};
1610 1 my $name = Mojo::File->new($path)->basename;
1611 1 ▶ if (ref $old eq 'HASH' && defined $old->{file} && $old->{file} ne $name && $old->{file} =~ /\A[0-9a-f]{32}\.db\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old eq "HASH" and defined $old->{"file"} and $old->{"file"} ne $name and $old->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u) 0 1 1
Condition33%3/9
ref $old eq "HASH" and defined $old->{"file"} and $old->{"file"} ne $name and $old->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
!ll&&!rl&&r
100
ref $old eq "HASH" and defined $old->{"file"} and $old->{"file"} ne $name
!ll&&!rl&&r
100
ref $old eq "HASH" and defined $old->{"file"}
!ll&&!rl&&r
100
MC/DC0%0/4
ref $old eq "HASH" and defined $old->{"file"} and $old->{"file"} ne $name and $old->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
ref $old eq "HASH" defined $old->{"file"} $old->{"file"} ne $name $old->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
Truth table20%1/5
A and B and C and D
A= ref $old eq "HASH"B= defined $old->{"file"}C= $old->{"file"} ne $nameD= $old->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1612 0 unlink Mojo::File->new($path)->dirname->child($old->{file})->to_string;
1613 }
1614 1 my $built = time;
1615 # The file's own column order: the extract would list its columns alphabetically.
1616 1 $models->{accelerated}{$key} = { built => $built, file => $name, rows => scalar(@$records), columns => \@columns };
1617 1 ▶ $c->_write_models($models) or croak $c->_i18n('error_accel_store', $spec);
Branch: 1 branch
BranchTrueFalseTotal
$c->_write_models($models) or croak($c->_i18n("error_accel_store", $spec)) 1 0 1
1618 1 return { spec => $key, path => $path, rows => scalar(@$records), columns => scalar(@columns), built => $built };
1619 }
1620  
1621 # unaccelerate($c, $spec) -> $bool
1622 #
1623 # Purpose: Stop reading a dataset from an extract, and remove its extract.
1624 # Exit: True when the spec was accelerated.
1625 3 ▶sub unaccelerate {
Subroutine unaccelerate: called CC=6 SCAR=18.3
Pod documented
1626 3 my ($c, $spec) = @_;
1627 3 ▶ croak 'unaccelerate() needs a controller and a spec' unless ref $c && defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and defined $spec 1 2 3
Condition66%2/3
ref $c and defined $spec
!ll&&!rl&&r
012
MC/DC50%1/2
ref $c and defined $spec
ref $c defined $spec
Truth table66%2/3
A and B
A= ref $cB= defined $spec
ABresult
0X0
100
111
1628 2 $spec = canonical_spec($spec); # unaccelerate names no spec in a message
1629 2 my $models = $c->_read_models;
1630 2 my $entry = delete $models->{accelerated}{$spec};
1631 2 ▶ return 0 unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 1 1 2
1632 1 ▶ if (defined $entry->{file} && $entry->{file} =~ /\A[0-9a-f]{32}\.db\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->{"file"} and $entry->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u) 1 0 1
Condition33%1/3
defined $entry->{"file"} and $entry->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
!ll&&!rl&&r
001
MC/DC0%0/2
defined $entry->{"file"} and $entry->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
defined $entry->{"file"} $entry->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
Truth table33%1/3
A and B
A= defined $entry->{"file"}B= $entry->{"file"} =~ /\A[0-9a-f]{32}\.db\z/u
ABresult
0X0
100
111
1633 1 unlink Mojo::File->new(_extract_store($c->app)->dir)->child($entry->{file})->to_string;
1634 }
1635 1 $c->_write_models($models);
1636 1 return 1;
1637 }
1638  
1639 # accelerated_specs($c) -> @specs
1640 #
1641 # Purpose: Every spec marked accelerated, sorted, for script/bi-extract --all.
1642 3 ▶sub accelerated_specs {
Subroutine accelerated_specs: called CC=2 SCAR=6.9
Pod documented
1643 3 my ($c) = @_;
1644 3 ▶ croak 'accelerated_specs() needs a controller' unless ref $c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c 1 2 3
1645 2 return sort keys %{ $c->_read_models->{accelerated} };
1646 }
1647  
1648 # _open_spec($self, $spec) -> ($DataSource, $label) or ()
1649 #
1650 # Purpose: Parse a "table:name", "table:file/table" or "path:/abs/path" spec,
1651 # verify the underlying file exists (Database::Abstraction creates empty objects silently for
1652 # missing files, so we must check ourselves), and return a live
1653 # DataSource object and a display label.
1654 # Entry: $spec is a non-empty string.
1655 # Exit: Returns ($datasource, $label) on success; an empty list on any failure
1656 # (invalid spec format, file missing, DataSource init error).
1657 # Side Effects: Filesystem stat; DataSource construction (reads file header).
1658 968 ▶sub _open_spec :Protected ($self, $spec) {
Subroutine _open_spec: called CC=38 SCAR=51.6
1659 # An accelerated dataset whose extract still matches its file is read from
1660 # the extract, under the file's own label.
1661 968 ▶ if (my $fast = _accelerated_source($self, $spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fast = _accelerated_source($self, $spec)) 0 968 968
1662 my $label = $spec =~ m{\Atable:(.+)\z} ? (_table_stem(_data_dir_for($self->app), $1) // $1)
1663 0 ▶ : do { my ($f, $i) = _split_path($spec =~ s/\Apath://r); $i // Mojo::File->new($f)->basename };
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ /\Atable:(.+)\z/u ? : 0 0 0
Condition0%0/6
_table_stem(_data_dir_for($self->app), $1) // $1
l!l&&r!l&&!r
000
$i // "Mojo::File"->new($f)->basename
l!l&&r!l&&!r
000
MC/DC0%0/2
_table_stem(_data_dir_for($self->app), $1) // $1
_table_stem(_data_dir_for($self->app), $1) $1
MC/DC0%0/2
$i // "Mojo::File"->new($f)->basename
$i "Mojo::File"->new($f)->basename
Truth table0%0/3
A // B
A= _table_stem(_data_dir_for($self->app), $1)B= $1
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $iB= "Mojo::File"->new($f)->basename
ABresult
1X1
011
000
1664 0 return ($fast, $label);
1665 }
1666 968 ▶ if ($spec =~ m{\Atable:([A-Za-z0-9_]+)(?:/([A-Za-z0-9_]+))?\z}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($spec =~ m[\Atable:([A-Za-z0-9_]+)(?:/([A-Za-z0-9_]+))?\z]u) { } 842 126 968
elsif ($spec =~ /\Apath:(.+)\z/u) { } 85 41 126
elsif ($spec =~ /$URL_SPEC_RE/u) { } 1 40 41
elsif ($spec =~ /$REDIS_SPEC_RE/u) { } 0 40 40
elsif ($spec =~ /$MARIADB_SPEC_RE/u) { } 0 40 40
1667 842 my ($name, $internal) = ($1, $2);
1668 842 my $data_dir = _data_dir_for($self->app);
1669 842 ▶ my $table = _table_stem($data_dir, $name) // return ();
Condition100%2/2
_table_stem($data_dir, $name) // (return ())
l!l
82220
MC/DC100%1/1
_table_stem($data_dir, $name) // (return ())
_table_stem($data_dir, $name)
Truth table100%2/2
A
A= _table_stem($data_dir, $name)
Aresult
11
00
1670 822 my $src = eval {
1671 822 ▶ $self->open_table($table, directory => $data_dir->to_string,
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 0 822 822
1672 defined $internal ? (internal_table => $internal) : ())
1673 };
1674 822 ▶ return ($src, defined $internal ? $internal : $table) if $src && !$@;
Branch: 2 branches
BranchTrueFalseTotal
if $src and not $@ 822 0 822
defined $internal ? : 0 822 822
Condition33%1/3
$src and not $@
!ll&&!rl&&r
00822
MC/DC0%0/2
$src and not $@
$src not $@
Truth table33%1/3
A and not(B)
A= $srcB= not $@
ABresult
0X0
100
111
1675 } elsif ($spec =~ /\Apath:(.+)\z/) {
1676 # path:<file>#<table> names one table of the file, as table:<file>/<table>
1677 # does in data_dir.
1678 85 my ($path_arg, $internal) = _split_path($1);
1679 85 ▶ my @it = defined $internal ? (internal_table => $internal) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 0 85 85
1680 # Remote path: /../hostname/dir/file -- delegate to DataSource with host param.
1681 # No EXT_RE check: security comes from REMOTE_HOST_RE. The original
1682 # extension (if any) is passed as file_ext so _init_backend tries it first.
1683 85 ▶ if ($path_arg =~ m{\A/\.\./([^/]+)((?:/.+)?)/([^/]+)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_arg =~ m[\A/\.\./([^/]+)((?:/.+)?)/([^/]+)\z]u) 5 80 85
1684 5 ▶ my ($host, $dir_part, $file) = ($1, $2 // '', $3);
Condition50%1/2
$2 // ""
l!l
50
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
1685 5 ▶ $dir_part = '/' unless length $dir_part;
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir_part 0 5 5
1686 5 ▶ return () unless $host =~ $REMOTE_HOST_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /$REMOTE_HOST_RE/u 1 4 5
1687 4 ▶ my ($table, $ext) = $file =~ /\A(.+)\.([^.]+)\z/ ? ($1, $2) : ($file, undef);
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\A(.+)\.([^.]+)\z/u ? : 4 0 4
1688 4 ▶ my $src = eval { $self->open_table($table, directory => $dir_part, host => $host,
Branch: 1 branch
BranchTrueFalseTotal
defined $ext ? : 4 0 4
1689 defined $ext ? (file_ext => $ext) : (), @it) };
1690 4 ▶ return ($src, $internal // $file) if $src && !$@;
Branch: 1 branch
BranchTrueFalseTotal
if $src and not $@ 3 1 4
Condition50%3/6
$src and not $@
!ll&&!rl&&r
103
$internal // $file
l!l&&r!l&&!r
030
MC/DC50%1/2
$src and not $@
$src not $@
MC/DC0%0/2
$internal // $file
$internal $file
Truth table66%2/3
A and not(B)
A= $srcB= not $@
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $internalB= $file
ABresult
1X1
011
000
1691 1 return ();
1692 }
1693 80 ▶ return () if _outside_roots($self, $path_arg);
Branch: 1 branch
BranchTrueFalseTotal
if _outside_roots($self, $path_arg) 0 80 80
1694 80 my $file = eval { Mojo::File->new($path_arg)->realpath };
1695 80 ▶ if (defined $file && -f $file && $file->basename =~ $EXT_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file and $file->basename =~ /$EXT_RE/u) 75 5 80
Condition83%5/6
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
!ll&&!rl&&r
1475
defined $file and -f $file
!ll&&!rl&&r
1079
MC/DC66%2/3
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
defined $file -f $file $file->basename =~ /$EXT_RE/u
Truth table75%3/4
A and B and C
A= defined $fileB= -f $fileC= $file->basename =~ /$EXT_RE/u
ABCresult
0XX0
10X0
1100
1111
1696 75 my $dir = $file->dirname->to_string;
1697 75 (my $table = $file->basename) =~ s/\.[^.]+\z//;
1698 75 my $src = eval { $self->open_table($table, directory => $dir, @it) };
1699 75 ▶ return ($src, $internal // $file->basename) if $src && !$@;
Branch: 1 branch
BranchTrueFalseTotal
if $src and not $@ 75 0 75
Condition33%2/6
$src and not $@
!ll&&!rl&&r
0075
$internal // $file->basename
l!l&&r!l&&!r
0750
MC/DC0%0/2
$src and not $@
$src not $@
MC/DC0%0/2
$internal // $file->basename
$internal $file->basename
Truth table33%1/3
A and not(B)
A= $srcB= not $@
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $internalB= $file->basename
ABresult
1X1
011
000
1700 }
1701 } elsif($spec =~ $URL_SPEC_RE) {
1702 1 my $url = $1;
1703 1 ▶ return () unless _is_safe_url($url);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_safe_url($url) 1 0 1
1704 0 my $src = eval { $self->open_table('', url => $url) };
1705 0 ▶ return ($src, $src->table_name) if $src && !$@;
Branch: 1 branch
BranchTrueFalseTotal
if $src and not $@ 0 0 0
Condition0%0/3
$src and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
$src and not $@
$src not $@
Truth table0%0/3
A and not(B)
A= $srcB= not $@
ABresult
0X0
100
111
1706 } elsif($spec =~ $REDIS_SPEC_RE) {
1707 0 my ($table, $redis_url) = ($1, $2);
1708 0 my $src = eval { $self->open_table($table, redis => $redis_url) };
1709 0 ▶ return ($src, $table) if $src && !$@;
Branch: 1 branch
BranchTrueFalseTotal
if $src and not $@ 0 0 0
Condition0%0/3
$src and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
$src and not $@
$src not $@
Truth table0%0/3
A and not(B)
A= $srcB= not $@
ABresult
0X0
100
111
1710 } elsif($spec =~ $MARIADB_SPEC_RE) {
1711 0 my ($server, $table) = ($1, $2);
1712 0 my $src = eval { $self->open_table($table, mariadb => $server) };
1713 0 ▶ return ($src, $table) if $src;
Branch: 1 branch
BranchTrueFalseTotal
if $src 0 0 0
1714 # A server has more ways to fail than a file has; /open says which.
1715 0 $self->stash(open_spec_error => _tidy_error($@));
1716 }
1717 45 return ();
1718 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1719  
1720 # _schema_type_name($entry) -> $type
1721 #
1722 # Purpose: Normalise one schema() entry to an upper-case type name.
1723 # Entry: A hashref such as { type => 'INTEGER' }, a plain string, or undef.
1724 # Exit: The type name, or '' when the backend did not declare one.
1725 24 ▶sub _schema_type_name {
Subroutine _schema_type_name: called CC=6 SCAR=17.9
1726 24 my ($entry) = @_;
1727 24 ▶ return '' unless defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 1 23 24
1728  
1729 23 ▶ my $type = ref($entry) eq 'HASH' ? $entry->{type} : $entry;
Branch: 1 branch
BranchTrueFalseTotal
ref $entry eq 'HASH' ? : 15 8 23
1730 23 ▶ return '' unless defined($type) && !ref($type) && length($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type and length $type 6 17 23
Condition100%6/6
defined $type and not ref $type and length $type
!ll&&!rl&&r
3317
defined $type and not ref $type
!ll&&!rl&&r
2120
MC/DC100%3/3
defined $type and not ref $type and length $type
defined $type not ref $type length $type
Truth table100%4/4
A and not(B) and C
A= defined $typeB= not ref $typeC= length $type
ABCresult
0XX0
10X0
1100
1111
1731  
1732 17 return uc $type;
1733 }
1734  
1735 # _schema_type_warnings($self, $left, $right, $left_label, $right_label,
1736 # $left_key, $right_key) -> @warning_text
1737 #
1738 # Purpose: Describe columns whose declared types disagree across one join step.
1739 # Entry: $left and $right are open sources; the labels name them in the message.
1740 # The two join keys are excluded: Database::Join itself treats their
1741 # types as free to differ, because the key is not a data column.
1742 # Exit: Zero or more formatted warning strings for the schema_warnings banner.
1743 #
1744 # Database::Join's warn_schema_type_mismatch cannot fire here: every join this
1745 # controller builds passes a collision_prefix for the right table, so the
1746 # mismatched columns are published under distinct names and the check correctly
1747 # stays quiet. The difference still matters to a reader - a sum over a text
1748 # column yields nothing - so the comparison is made here, on the two sides as
1749 # the user sees them in the join panel.
1750 17 ▶sub _schema_type_warnings :Protected ($self, $left, $right, $left_label, $right_label,
Subroutine _schema_type_warnings: called CC=15 SCAR=27.4
1751 $left_key, $right_key) {
1752 17 ▶ my $left_schema = eval { $left->schema } // {};
Condition100%2/2
eval { do { $left->schema } } // {}
l!l
161
MC/DC100%1/1
eval { do { $left->schema } } // {}
eval { do { $left->schema } }
Truth table100%2/2
A
A= eval { do { $left->schema } }
Aresult
11
00
1753 17 ▶ my $right_schema = eval { $right->schema } // {};
Condition50%1/2
eval { do { $right->schema } } // {}
l!l
170
MC/DC0%0/1
eval { do { $right->schema } } // {}
eval { do { $right->schema } }
Truth table50%1/2
A
A= eval { do { $right->schema } }
Aresult
11
00
1754 17 ▶ return () unless %{ $left_schema // {} } && %{ $right_schema // {} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$left_schema // {};} and %{$right_schema // {};} 2 15 17
Condition57%4/7
%{$left_schema // {};} and %{$right_schema // {};}
!ll&&!rl&&r
2015
$left_schema // {}
l!l
170
$right_schema // {}
l!l
150
MC/DC50%1/2
%{$left_schema // {};} and %{$right_schema // {};}
%{$left_schema // {};} %{$right_schema // {};}
MC/DC0%0/1
$left_schema // {}
$left_schema
MC/DC0%0/1
$right_schema // {}
$right_schema
Truth table66%2/3
A and B
A= %{$left_schema // {};}B= %{$right_schema // {};}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $left_schema
Aresult
11
00
Truth table50%1/2
A
A= $right_schema
Aresult
11
00
1755  
1756 15 ▶ my %is_key = map { (defined($_) && length($_)) ? ($_ => 1) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && length $_ ? : 24 6 30
Condition66%2/3
defined $_ and length $_
!ll&&!rl&&r
0624
MC/DC50%1/2
defined $_ and length $_
defined $_ length $_
Truth table66%2/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
1757 ($left_key, $right_key);
1758  
1759 15 my @warnings;
1760 15 for my $col (sort keys %$right_schema) {
1761 35 ▶ next if $is_key{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $is_key{$col} 12 23 35
1762 23 ▶ next unless exists $left_schema->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $left_schema->{$col} 15 8 23
1763  
1764 8 my $left_type = _schema_type_name($left_schema->{$col});
1765 8 my $right_type = _schema_type_name($right_schema->{$col});
1766 8 ▶ next unless length($left_type) && length($right_type);
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_type and length $right_type 1 7 8
Condition66%2/3
length $left_type and length $right_type
!ll&&!rl&&r
107
MC/DC50%1/2
length $left_type and length $right_type
length $left_type length $right_type
Truth table66%2/3
A and B
A= length $left_typeB= length $right_type
ABresult
0X0
100
111
1767 7 ▶ next if $left_type eq $right_type;
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq $right_type 6 1 7
1768  
1769 1 push @warnings, $self->_i18n('warn_join_schema_mismatch',
1770 $col, $left_type, $left_label, $right_type, $right_label);
1771 }
1772  
1773 15 return @warnings;
1774 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1775  
1776 # _spec_to_url($self, $spec) -> $url_string
1777 #
1778 # Purpose: Convert a table/path spec back to a browseable URL (used for the
1779 # back-link on join and export views).
1780 # Entry: $spec is a "table:name", "table:file/table" or "path:/abs" string.
1781 # Exit: Returns a URL string beginning with '/'.
1782 1070 ▶sub _spec_to_url :Protected ($self, $spec) {
Subroutine _spec_to_url: called CC=10 SCAR=27.3
1783 1070 ▶ if ($spec =~ m{\Atable:([A-Za-z0-9_]+)(?:/([A-Za-z0-9_]+))?\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[\Atable:([A-Za-z0-9_]+)(?:/([A-Za-z0-9_]+))?\z]u) 959 111 1070
1784 959 my ($table, $internal) = ($1, $2);
1785 959 ▶ return "/view/$table" unless defined $internal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $internal 959 0 959
1786  
1787 0 my $data_dir = _data_dir_for($self->app);
1788 0 for my $ext (@SUPPORTED_EXT) {
1789 0 my $file = $data_dir->child("$table.$ext");
1790 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
1791 0 return '/open?path=' . url_escape($file->to_string) . '&it=' . url_escape($internal);
1792 }
1793 0 return "/view/$table";
1794 }
1795 111 ▶ if ($spec =~ /\Apath:(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\Apath:(.+)\z/u) 55 56 111
1796 55 my ($file, $internal) = _split_path($1);
1797 55 ▶ return '/open?path=' . url_escape($file) . (defined $internal ? '&it=' . url_escape($internal) : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 0 55 55
1798 }
1799 56 ▶ return '/import?url=' . url_escape($1) if $spec =~ $URL_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /$URL_SPEC_RE/u 2 54 56
1800 54 ▶ return '/' if $spec =~ $REDIS_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /$REDIS_SPEC_RE/u 0 54 54
1801 54 ▶ return '/open?path=' . url_escape($spec) if $spec =~ $MARIADB_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /$MARIADB_SPEC_RE/u 0 54 54
1802 54 return '/';
1803 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1804  
1805 # _url_attribution($self) -> ($url_string, $date_string) or (undef, undef)
1806 #
1807 # Purpose: Return source URL and access date for chart attribution when the
1808 # left table was imported from a web URL. The date is the time of
1809 # the current request (which is when the data is being accessed),
1810 # formatted as "D Month YYYY".
1811 # Entry: Reads the 'l' query param.
1812 # Exit: ($url, $date) when the spec matches $URL_SPEC_RE; (undef, undef)
1813 # for table: and path: specs.
1814 300 ▶sub _url_attribution :Protected ($self) {
Subroutine _url_attribution: called CC=3 SCAR=11.0
1815 300 ▶ my $spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
2991
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
1816 300 ▶ return (undef, undef) unless $spec =~ $URL_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /$URL_SPEC_RE/u 299 1 300
1817 1 my $url = $1;
1818 1 my @MON = qw(January February March April May June July August
1819 September October November December);
1820 1 my (undef, undef, undef, $mday, $mon, $year) = localtime;
1821 1 return ($url, sprintf('%d %s %d', $mday, $MON[$mon], $year + 1900));
1822 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1823  
1824 # _get_columns($source, $records) -> @column_names
1825 #
1826 # Purpose: Return an ordered column list from a DataSource object.
1827 # For CSV/TSV/PSV the DataSource stores the original file-header order.
1828 # For SQLite/XML where no file-header order is available, the fallback
1829 # is: id_column first (only if it actually appears in the data), then
1830 # the remaining columns sorted alphabetically.
1831 # Entry: $source is a DataSource; $records is an arrayref of hashrefs (may be empty).
1832 # Exit: Returns a flat list of column-name strings (may be empty).
1833 #
1834 # Reduction: columns() is a pure accessor -- cache once to avoid a redundant
1835 # method dispatch on the second reference.
1836 985 ▶sub _get_columns {
Subroutine _get_columns: called CC=5 SCAR=16.1
1837 985 my ($source, $records) = @_;
1838 985 my $cols = $source->columns;
1839 985 ▶ return @$cols if $cols;
Branch: 1 branch
BranchTrueFalseTotal
if $cols 980 5 985
1840 5 ▶ return () unless $records->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $records->[0] 2 3 5
1841 3 my %all = map { $_ => 1 } keys %{ $records->[0] };
1842 3 my $c = $source->id_column;
1843 3 ▶ my $id = ($c && $all{$c}) ? $c : (sort keys %all)[0];
Branch: 1 branch
BranchTrueFalseTotal
$c && $all{$c} ? : 1 2 3
Condition66%2/3
$c and $all{$c}
!ll&&!rl&&r
201
MC/DC50%1/2
$c and $all{$c}
$c $all{$c}
Truth table66%2/3
A and B
A= $cB= $all{$c}
ABresult
0X0
100
111
1844 3 delete $all{$id};
1845 3 return ($id, sort keys %all);
1846 }
1847  
1848 # _apply_filter_spec($records, $spec) -> \@filtered_records
1849 #
1850 # Purpose: Apply one "col:op:val" filter to an arrayref of record hashrefs and
1851 # return a new (possibly shorter) arrayref. The original is not mutated.
1852 # Value may contain colons: "col:op:val:with:colons" -- split limit 3
1853 # puts everything after the second colon into $val.
1854 # Unknown operators fall through to 1 (all rows pass) so new operators
1855 # added in future do not break existing callers.
1856 # Entry: $records is an arrayref of hashrefs; $spec is a colon-delimited string.
1857 # Exit: Returns filtered arrayref (same reference if spec is invalid/unparseable).
1858 #
1859 # Domain Constraints (operator names):
1860 # Operator names are CASE-SENSITIVE. Only the exact lowercase forms below are
1861 # recognised; an uppercase or mixed-case name (e.g. "EQ", "Lt") falls through
1862 # to the default `1` branch and all rows pass unfiltered.
1863 # Valid operators:
1864 # String (value comparison is case-insensitive via lc()):
1865 # eq, ne, contains, starts
1866 # Numeric (Perl <, <=, >, >= coercion):
1867 # lt (strict), le (inclusive), gt (strict), ge (inclusive)
1868 # Unary (val is ignored):
1869 # empty (cell eq ''), notempty (cell ne '')
1870 # List (val = a JSON array of strings; compared as eq is):
1871 # in -- lc(cell) is one of them; a val that is not such an array matches nothing
1872 # Range (val = lo:hi, both ends included, whichever is written first;
1873 # see _between_bounds for where a timestamp's colons leave it):
1874 # between -- two numbers compare as numbers, two dates or timestamps
1875 # as times (at the precision of the bounds), anything else as text,
1876 # case-insensitively
1877 # Boundary rules for numeric operators:
1878 # le/ge include the boundary value; lt/gt exclude it.
1879 # contains/starts with empty val ('') match every record (index always 0).
1880 #
1881 # Reduction: split(/:/, $spec, 3) always produces at least one defined element,
1882 # so "defined $col" is structurally guaranteed true and is removed.
1883 # "$_->{$col} // ''" replaces the equivalent ternary form.
1884 #
1885 # Optimisation: $lval = lc($val) is computed once before the grep. Without this
1886 # precompute, every case-insensitive operator (eq, ne, contains, starts) would
1887 # call lc($val) O(N) times for N records -- a pure O(N) waste since $val is
1888 # constant within a single filter application.
1889 # _is_iso_date($value) -> bool
1890 #
1891 # Purpose: Report whether a filter value names a point in time rather than a
1892 # plain string, so the date comparisons can be used instead of the
1893 # string ones. A bare date (YYYY-MM-DD) qualifies, and so does an ISO
1894 # timestamp (YYYY-MM-DDTHH:MM:SS) or the same with a space separator,
1895 # which is what CSV exports and SQLite text columns usually hold, and
1896 # one with a fraction of a second or a zone (Z, +01:00), which is what
1897 # JSON APIs write. The time is taken as written (Util::iso_datetime).
1898 2104 ▶sub _is_iso_date {
Subroutine _is_iso_date: called CC=2 SCAR=6.9
1899 2104 ▶ return scalar(() = Database::BI::Util::iso_datetime($_[0])) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar(() = Database::BI::Util::iso_datetime($_[0])) ? : 1055 1049 2104
1900 }
1901  
1902 # Convert M/D/YYYY or YYYY-MM-DD to YYYY-MM-DD; returns undef for other formats.
1903 # A value that is already ISO is returned unchanged, including its time part: a
1904 # timestamp cell is the caller's to compare, not this sub's to truncate.
1905 1947 ▶sub _to_iso_date {
Subroutine _to_iso_date: called CC=4 SCAR=13.9
1906 1947 my ($v) = @_;
1907 1947 ▶ return unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 1946 1947
1908 1946 ▶ return $v if _is_iso_date($v);
Branch: 1 branch
BranchTrueFalseTotal
if _is_iso_date($v) 1009 937 1946
1909 937 ▶ if (my ($m, $d, $y) = $v =~ m{\A(\d{1,2})/(\d{1,2})/(\d{4})\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $v =~ m[\A(\d{1,2})/(\d{1,2})/(\d{4})\z]u) 4 933 937
1910 4 return sprintf '%04d-%02d-%02d', $y, $m, $d;
1911 }
1912 933 return;
1913 }
1914  
1915 # _time_columns(\@columns, \@records) -> \@names
1916 #
1917 # Purpose: The columns a time range can be applied to: those whose every
1918 # non-empty cell on the page is a date or a date and time that the
1919 # range filter reads (_to_iso_date: YYYY-MM-DD, with an optional
1920 # "THH:MM[:SS]" or " HH:MM[:SS]", or M/D/YYYY), with a month and day
1921 # that can exist, and at least one such cell. The dashboard offers
1922 # Time range only when this is not empty, and lists only these.
1923 # Entry: $columns is the page's column order; $records its rows (hashes).
1924 # Exit: Returns the qualifying names, in column order; [] when there are
1925 # none, no rows, or either argument is not an array.
1926 # Notes: It reads the rows drawn on the page, as the table's own date
1927 # detection does: reading every row of the view for this would cost a
1928 # second pass over the data on every page.
1929 202 ▶sub _time_columns {
Subroutine _time_columns: called CC=15 SCAR=29.1
1930 202 my ($columns, $records) = @_;
1931 202 ▶ return [] unless ref($columns) eq 'ARRAY' && ref($records) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $columns eq "ARRAY" and ref $records eq "ARRAY" 0 202 202
Condition33%1/3
ref $columns eq "ARRAY" and ref $records eq "ARRAY"
!ll&&!rl&&r
00202
MC/DC0%0/2
ref $columns eq "ARRAY" and ref $records eq "ARRAY"
ref $columns eq "ARRAY" ref $records eq "ARRAY"
Truth table33%1/3
A and B
A= ref $columns eq "ARRAY"B= ref $records eq "ARRAY"
ABresult
0X0
100
111
1932 202 my @found;
1933 202 COLUMN: for my $col (@{$columns}) {
1934 1105 ▶ next unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 1105 1105
1935 1105 my $seen = 0;
1936 1105 for my $row (@{$records}) {
1937 1974 ▶ next unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 1974 1974
1938 1974 my $v = $row->{$col};
1939 1974 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 3 1971 1974
1940 1971 $v =~ s/\A\s+|\s+\z//g;
1941 1971 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 72 1899 1971
1942 1899 my $iso = _to_iso_date($v);
1943 1899 ▶ next COLUMN unless defined $iso;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iso 932 967 1899
1944 967 my ($m, $d) = $iso =~ /\A\d{4}-(\d{2})-(\d{2})/;
1945 967 ▶ next COLUMN unless $m >= 1 && $m <= 12 && $d >= 1 && $d <= 31;
Branch: 1 branch
BranchTrueFalseTotal
unless $m >= 1 and $m <= 12 and $d >= 1 and $d <= 31 0 967 967
Condition33%3/9
$m >= 1 and $m <= 12 and $d >= 1 and $d <= 31
!ll&&!rl&&r
00967
$m >= 1 and $m <= 12 and $d >= 1
!ll&&!rl&&r
00967
$m >= 1 and $m <= 12
!ll&&!rl&&r
00967
MC/DC0%0/4
$m >= 1 and $m <= 12 and $d >= 1 and $d <= 31
$m >= 1 $m <= 12 $d >= 1 $d <= 31
Truth table20%1/5
A and B and C and D
A= $m >= 1B= $m <= 12C= $d >= 1D= $d <= 31
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1946 967 $seen = 1;
1947 }
1948 173 ▶ push @found, $col if $seen;
Branch: 1 branch
BranchTrueFalseTotal
if $seen 170 3 173
1949 }
1950 202 return \@found;
1951 }
1952  
1953 # _date_key($value, $precision) -> comparable key | undef
1954 #
1955 # Purpose: Put a cell and a filter bound on the same footing so that a
1956 # lexicographic comparison answers the question the filter asked.
1957 # $precision is 10 for a day and 19 for a second: a cell holding a
1958 # timestamp is compared at the day it falls in when the bound is a
1959 # day, and to the second when the bound carries a time. A value that
1960 # is not date-shaped at all returns undef, and the caller keeps the
1961 # raw cell instead.
1962 68 ▶sub _date_key {
Subroutine _date_key: called CC=7 SCAR=19.5
1963 68 my ($value, $precision) = @_;
1964 68 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 67 68
1965 67 my ($day, $hour, $min, $sec) = Database::BI::Util::iso_datetime($value);
1966 67 ▶ return undef unless defined $day;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $day 1 66 67
1967 66 ▶ return $day if $precision < 19;
Branch: 1 branch
BranchTrueFalseTotal
if $precision < 19 43 23 66
1968 23 ▶ return sprintf '%sT%s:%s:%s', $day, $hour // '00', $min // '00', $sec // '00';
Condition100%6/6
$hour // "00"
l!l
212
$min // "00"
l!l
212
$sec // "00"
l!l
194
MC/DC100%1/1
$hour // "00"
$hour
MC/DC100%1/1
$min // "00"
$min
MC/DC100%1/1
$sec // "00"
$sec
Truth table100%2/2
A
A= $hour
Aresult
11
00
Truth table100%2/2
A
A= $min
Aresult
11
00
Truth table100%2/2
A
A= $sec
Aresult
11
00
1969 }
1970  
1971 # ---------------------------------------------------------------------------
1972 # Relative time ranges
1973 # ---------------------------------------------------------------------------
1974  
1975 # _iso_day($epoch) -> 'YYYY-MM-DD' in local time
1976 48098 ▶sub _iso_day {
Subroutine _iso_day: called CC=1 SCAR=0.0
1977 48098 my @lt = localtime($_[0]);
1978 48098 return sprintf '%04d-%02d-%02d', $lt[5] + 1900, $lt[4] + 1, $lt[3];
1979 }
1980  
1981 # _iso_second($epoch) -> 'YYYY-MM-DDTHH:MM:SS' in local time
1982 4373 ▶sub _iso_second {
Subroutine _iso_second: called CC=1 SCAR=0.0
1983 4373 my @lt = localtime($_[0]);
1984 4373 return sprintf '%04d-%02d-%02dT%02d:%02d:%02d',
1985 $lt[5] + 1900, $lt[4] + 1, $lt[3], $lt[2], $lt[1], $lt[0];
1986 }
1987  
1988 # _midday($mday, $mon, $year) -> epoch
1989 #
1990 # Noon, so a boundary never lands in a daylight-saving gap, and mktime's own
1991 # normalisation (day 0 is the last day of the previous month, month -1 is
1992 # December, and so on) is what makes the month, quarter and year arithmetic
1993 # below one line each. $mon is 0-based, $year is years since 1900, matching
1994 # localtime's own convention.
1995 54657 ▶sub _midday {
Subroutine _midday: called CC=1 SCAR=0.0
1996 54657 require POSIX;
1997 54657 return POSIX::mktime(12, 0, 0, $_[0], $_[1], $_[2]);
1998 }
1999  
2000 # _time_range_bounds($preset, $now) -> ($lo, $hi)
2001 #
2002 # Purpose: Resolve one preset to the window it names, as two strings that the
2003 # filter engine can compare against: 'YYYY-MM-DD' for a day-granular
2004 # window, 'YYYY-MM-DDTHH:MM:SS' for last_24_hours.
2005 # Entry: $preset -- one entry of @TIME_RANGES.
2006 # $now -- epoch second; defaults to the clock, and a caller may
2007 # pass one so a test can name the day it is asking about.
2008 # Exit: Two-element list, or an empty list for a preset with no window.
2009 # Notes: "This month" and "this quarter" end today rather than at the end of
2010 # the period, which is what the same names mean in a search bar; the
2011 # completed period is last_month / last_quarter.
2012 26234 ▶sub _time_range_bounds {
Subroutine _time_range_bounds: called CC=11 SCAR=24.0
2013 26234 my ($preset, $now) = @_;
2014 26234 ▶ $now = time() unless defined $now;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $now 26209 25 26234
2015  
2016 26234 my @lt = localtime($now);
2017 26234 my ($mday, $mon, $year) = @lt[3, 4, 5]; # mon is 0-based
2018  
2019 26234 ▶ if ($preset->{hours}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preset->{"hours"}) 2186 24048 26234
2020 2186 return (_iso_second($now - $preset->{hours} * 3600), _iso_second($now));
2021 }
2022  
2023 24048 my $today = _midday($mday, $mon, $year);
2024  
2025 24048 ▶ if (defined $preset->{day}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset->{"day"}) 4372 19676 24048
2026 4372 my $day = _midday($mday + $preset->{day}, $mon, $year);
2027 4372 return (_iso_day($day), _iso_day($day));
2028 }
2029  
2030 19676 ▶ if (defined $preset->{days}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset->{"days"}) 6557 13119 19676
2031 6557 return (_iso_day(_midday($mday - $preset->{days} + 1, $mon, $year)),
2032 _iso_day($today));
2033 }
2034  
2035 13119 ▶ if (defined $preset->{month}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset->{"month"}) 4373 8746 13119
2036 4373 my $m = $mon + $preset->{month};
2037 4373 ▶ return $preset->{month} == 0
Branch: 1 branch
BranchTrueFalseTotal
$preset->{'month'} == 0 ? : 2186 2187 4373
2038 ? (_iso_day(_midday(1, $mon, $year)), _iso_day($today))
2039 : (_iso_day(_midday(1, $m, $year)), _iso_day(_midday(0, $m + 1, $year)));
2040 }
2041  
2042 8746 ▶ if (defined $preset->{quarter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset->{"quarter"}) 4373 4373 8746
2043 4373 my $q = int($mon / 3) * 3 + 3 * $preset->{quarter};
2044 4373 ▶ return $preset->{quarter} == 0
Branch: 1 branch
BranchTrueFalseTotal
$preset->{'quarter'} == 0 ? : 2186 2187 4373
2045 ? (_iso_day(_midday(1, $q, $year)), _iso_day($today))
2046 : (_iso_day(_midday(1, $q, $year)), _iso_day(_midday(0, $q + 3, $year)));
2047 }
2048  
2049 4373 ▶ if (defined $preset->{year}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset->{"year"}) 4372 1 4373
2050 4372 my $y = $preset->{year}; # 0 = this year, -1 = last year
2051 4372 my $start = _midday(1, 0, $year + $y);
2052 4372 ▶ return $preset->{year} == 0
Branch: 1 branch
BranchTrueFalseTotal
$preset->{'year'} == 0 ? : 2186 2186 4372
2053 ? (_iso_day($start), _iso_day($today))
2054 : (_iso_day($start), _iso_day(_midday(31, 11, $year + $y)));
2055 }
2056  
2057 1 return ();
2058 }
2059  
2060 # _time_range_specs($col, $lo, $hi) -> \@specs
2061 #
2062 # Purpose: Express a window as the f= filters the pipeline applies.
2063 # Notes: Two bounds -- col:ge:lo and col:le:hi -- rather than one between
2064 # spec. A between value is "lo:hi", and its colon has to be found
2065 # among a timestamp's own (_between_bounds; last_24_hours ends in
2066 # HH:MM:SS); one bound per spec needs no such search. Two specs are
2067 # inclusive at both ends exactly once, whatever precision the bounds
2068 # carry.
2069 6 ▶sub _time_range_specs {
Subroutine _time_range_specs: called CC=7 SCAR=19.6
2070 6 my ($col, $lo, $hi) = @_;
2071 6 ▶ return [] unless length($col // '') && length($lo // '') && length($hi // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and length($lo // "") and length($hi // "") 4 2 6
Condition83%10/12
length($col // "") and length($lo // "") and length($hi // "")
!ll&&!rl&&r
312
length($col // "") and length($lo // "")
!ll&&!rl&&r
213
$col // ""
l!l
51
$lo // ""
l!l
40
$hi // ""
l!l
30
MC/DC33%1/3
length($col // "") and length($lo // "") and length($hi // "")
$col $lo $hi
Truth table50%2/4
A and B and C
A= $colB= $loC= $hi
ABCresult
0XX0
10X0
1100
1111
2072 2 return [ "$col:ge:$lo", "$col:le:$hi" ];
2073 }
2074  
2075 # _time_range_preset($name) -> \%preset | undef
2076 #
2077 # The named entry of the vocabulary above, or undef for a name that is not in
2078 # it. A URL value reaches this, so it answers undef rather than croaking.
2079 17 ▶sub _time_range_preset {
Subroutine _time_range_preset: called CC=2 SCAR=6.9
2080 17 my ($name) = @_;
2081 17 ▶ return $TIME_RANGE{$name // ''};
Condition100%2/2
$name // ""
l!l
161
MC/DC100%1/1
$name // ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
2082 }
2083  
2084 # _time_range_presets($now) -> \@presets
2085 #
2086 # Purpose: The menu a filter bar offers, with the dates each entry currently
2087 # resolves to, so the page can show a range and, where it has asked
2088 # for the dataset's own extent, disable an entry the data cannot
2089 # answer.
2090 2185 ▶sub _time_range_presets {
Subroutine _time_range_presets: called CC=1 SCAR=0.0
2091 2185 my ($now) = @_;
2092 return [ map {
2093 2185 my ($lo, $hi) = _time_range_bounds($_, $now);
2094 26220 { name => $_->{name}, label => $_->{label}, lo => $lo, hi => $hi }
2095 } @TIME_RANGES ];
2096 }
2097  
2098 # _apply_time_range($records, $name, $col) -> \@records
2099 #
2100 # Purpose: Apply a named relative range to a record set. Used by the canvas,
2101 # which renders its cards from its own pipeline rather than through a
2102 # route's f= params.
2103 # Entry: $name and $col are the URL values; neither is trusted. An unknown
2104 # name or an unusable column leaves the records alone, so a stale
2105 # bookmark shows unfiltered data rather than an error page.
2106 31 ▶sub _apply_time_range {
Subroutine _apply_time_range: called CC=7 SCAR=19.5
2107 31 my ($records, $name, $col) = @_;
2108 31 ▶ my $preset = $TIME_RANGE{$name // ''} or return $records;
Branch: 1 branch
BranchTrueFalseTotal
my $preset = $TIME_RANGE{$name // ""} or return $records 4 27 31
Condition100%2/2
$name // ""
l!l
625
MC/DC100%1/1
$name // ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
2109 4 ▶ return $records unless defined $col && length $col && $col !~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and length $col and not $col =~ /:/u 3 1 4
Condition100%6/6
defined $col and length $col and not $col =~ /:/u
!ll&&!rl&&r
211
defined $col and length $col
!ll&&!rl&&r
112
MC/DC100%3/3
defined $col and length $col and not $col =~ /:/u
defined $col length $col not $col =~ /:/u
Truth table100%4/4
A and B and not(C)
A= defined $colB= length $colC= not $col =~ /:/u
ABCresult
0XX0
10X0
1100
1111
2110 1 my ($lo, $hi) = _time_range_bounds($preset);
2111 1 for my $spec (@{ _time_range_specs($col, $lo, $hi) }) {
2112 2 $records = _apply_filter_spec($records, $spec);
2113 }
2114 1 return $records;
2115 }
2116  
2117 # _time_range_request($c) -> bool
2118 #
2119 # Purpose: Turn the relative time range a request names -- ?range=<name> and
2120 # ?rcol=<column> -- into the f= filters every data route already
2121 # applies, and describe the range on the stash so a template can show
2122 # a chip and the preset menu. Registered as a before_dispatch hook in
2123 # Database::BI::startup, so one implementation covers the table view,
2124 # every chart, the pivot, stats, insights, the exports and the canvas,
2125 # and a request that carries a range needs no per-route code.
2126 # Entry: $c -- the controller for the request.
2127 # Exit: Records the resolved bounds on the stash, where _filter_specs picks
2128 # them up for whichever pipeline the route runs, and returns true --
2129 # or renders 400 for a range that cannot be honoured and returns
2130 # false, which stops the route from running against the wrong rows.
2131 # The bounds are not written back into the request's f= params, so the
2132 # reader's own filter list -- the chips, the panel forms and the
2133 # filter UI -- never shows a range as dates it did not choose.
2134 2184 ▶sub _time_range_request ($c) {
Subroutine _time_range_request: called CC=7 SCAR=25.7
2135 2184 my $name = $c->param('range');
2136 2184 my $col = $c->param('rcol');
2137  
2138 2184 $c->stash(time_range_presets => _time_range_presets());
2139 2184 $c->stash(range_specs => []);
2140  
2141 2184 ▶ return 1 unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1967 217 2184
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
192740217
MC/DC100%2/2
defined $name and length $name
defined $name length $name
Truth table100%3/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
2142  
2143 217 my $preset = $TIME_RANGE{$name};
2144 217 ▶ unless ($preset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($preset) 217 0 217
2145 $c->render(
2146 text => sprintf(_message_text('error_range_name'), $name,
2147 217 join(', ', map { $_->{name} } @TIME_RANGES)),
2148 status => 400,
2149 );
2150 217 return 0;
2151 }
2152 0 ▶ unless (defined $col && length $col && $col !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col and length $col and not $col =~ /:/u) 0 0 0
Condition0%0/6
defined $col and length $col and not $col =~ /:/u
!ll&&!rl&&r
000
defined $col and length $col
!ll&&!rl&&r
000
MC/DC0%0/3
defined $col and length $col and not $col =~ /:/u
defined $col length $col not $col =~ /:/u
Truth table0%0/4
A and B and not(C)
A= defined $colB= length $colC= not $col =~ /:/u
ABCresult
0XX0
10X0
1100
1111
2153 0 $c->render(text => _message_text('error_range_column'), status => 400);
2154 0 return 0;
2155 }
2156  
2157 0 my ($lo, $hi) = _time_range_bounds($preset);
2158 0 my $specs = _time_range_specs($col, $lo, $hi);
2159  
2160 $c->stash(
2161 time_range => {
2162 name => $preset->{name},
2163 label => $preset->{label},
2164 0 col => $col,
2165 lo => $lo,
2166 hi => $hi,
2167 },
2168 range_specs => $specs,
2169 );
2170 0 return 1;
2171 }
2172  
2173 # _filter_request($c) -> bool
2174 #
2175 # Purpose: Refuse an f= filter whose operator is not one _apply_filter_spec
2176 # knows. Until 0.021.0 such a filter kept every row, with nothing to
2177 # say it had done nothing (f=amount:GT:5, f=amount:gtx:5). Registered
2178 # as a before_dispatch hook in Database::BI::startup beside the time
2179 # range's, so every route that reads f= refuses it the same way.
2180 # Entry: $c -- the controller for the request.
2181 # Exit: True when every filter names a known operator, or has no column or
2182 # operator at all (such a spec has always been skipped); otherwise
2183 # renders 400 (error_filter_op, which lists the operators) and
2184 # returns false.
2185 # Notes: A filter on a column the view does not have is refused later, once
2186 # the columns are known (_unknown_filter_column). A canvas card's own
2187 # stored filters never come through here: they are ignored when stale,
2188 # by design.
2189 1967 ▶sub _filter_request ($c) {
Subroutine _filter_request: called CC=8 SCAR=20.9
2190 1967 ▶ for my $spec (@{ $c->every_param('f') // [] }) {
Condition50%1/2
$c->every_param("f") // []
l!l
19670
MC/DC0%0/1
$c->every_param("f") // []
$c->every_param("f")
Truth table50%1/2
A
A= $c->every_param("f")
Aresult
11
00
2191 87 my ($col, $op) = split /:/, $spec, 3;
2192 87 ▶ next unless length($col // '') && defined $op && length $op;
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and defined $op and length $op 61 26 87
Condition87%7/8
length($col // "") and defined $op and length $op
!ll&&!rl&&r
61026
length($col // "") and defined $op
!ll&&!rl&&r
412026
$col // ""
l!l
4740
MC/DC66%2/3
length($col // "") and defined $op and length $op
$col defined $op length $op
Truth table75%3/4
A and B and C
A= $colB= defined $opC= length $op
ABCresult
0XX0
10X0
1100
1111
2193 26 ▶ next if $FILTER_OP{$op};
Branch: 1 branch
BranchTrueFalseTotal
if $FILTER_OP{$op} 22 4 26
2194 4 $c->render(
2195 text => sprintf(_message_text('error_filter_op'), $op, $spec, join(', ', @FILTER_OPS)),
2196 status => 400,
2197 );
2198 4 return 0;
2199 }
2200 1963 return 1;
2201 }
2202  
2203 # _unknown_filter_column($self, \@columns, \%later) -> $message | undef
2204 #
2205 # Purpose: The refusal for a filter, or the time range, on a column the view
2206 # does not have. Such a filter used to read every cell as empty and
2207 # keep no rows (or every row, for ne and empty), so a mistyped name
2208 # looked like an empty file.
2209 # Entry: \@columns -- the view's columns at the filter stage; \%later -- the
2210 # columns stages 6 and 7 will add (a filter on one waits for them).
2211 # Exit: undef when every filter names a column there, or when the columns
2212 # are not known (no list, or an empty one: a URL source's cache, or
2213 # a view with no rows); else the error_filter_column message (or
2214 # error_range_column_unknown for rcol=), naming the columns there.
2215 # Notes: A plain function, like _reduce_records, which calls it; _fetch_page
2216 # calls it too, for the table pages.
2217 654 ▶sub _unknown_filter_column {
Subroutine _unknown_filter_column: called CC=14 SCAR=32.2
2218 654 my ($self, $columns, $later) = @_;
2219 654 ▶ return undef unless ref $columns eq 'ARRAY' && @{$columns};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $columns eq "ARRAY" and @{$columns;} 9 645 654
Condition66%2/3
ref $columns eq "ARRAY" and @{$columns;}
!ll&&!rl&&r
90645
MC/DC50%1/2
ref $columns eq "ARRAY" and @{$columns;}
ref $columns eq "ARRAY" @{$columns;}
Truth table66%2/3
A and B
A= ref $columns eq "ARRAY"B= @{$columns;}
ABresult
0X0
100
111
2220 645 ▶ my %known = ((map { $_ => 1 } @{$columns}), %{ $later // {} });
Condition50%1/2
$later // {}
l!l
6450
MC/DC0%0/1
$later // {}
$later
Truth table50%1/2
A
A= $later
Aresult
11
00
2221 0 ▶ my $listed = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2222 0 my @names = @{$columns};
2223 0 ▶ return join(', ', @names) if @names <= $FILTER_COLUMNS_LISTED;
Branch: 1 branch
BranchTrueFalseTotal
if @names <= $FILTER_COLUMNS_LISTED 0 0 0
2224 0 return join(', ', @names[0 .. $FILTER_COLUMNS_LISTED - 1])
2225 . sprintf(' and %d more', @names - $FILTER_COLUMNS_LISTED);
2226 645 };
2227 645 ▶ for my $spec (@{ $self->every_param('f') // [] }) {
Condition50%1/2
$self->every_param("f") // []
l!l
6450
MC/DC0%0/1
$self->every_param("f") // []
$self->every_param("f")
Truth table50%1/2
A
A= $self->every_param("f")
Aresult
11
00
2228 36 my ($col, $op) = split /:/, $spec, 3;
2229 36 ▶ next unless length($col // '') && defined $op && length $op;
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and defined $op and length $op 11 25 36
Condition62%5/8
length($col // "") and defined $op and length $op
!ll&&!rl&&r
11025
length($col // "") and defined $op
!ll&&!rl&&r
01125
$col // ""
l!l
360
MC/DC33%1/3
length($col // "") and defined $op and length $op
$col defined $op length $op
Truth table50%2/4
A and B and C
A= $colB= defined $opC= length $op
ABCresult
0XX0
10X0
1100
1111
2230 25 ▶ next if $known{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $known{$col} 25 0 25
2231 0 return sprintf(_message_text('error_filter_column'), $col, $spec, $listed->());
2232 }
2233 645 my $range = $self->stash('time_range');
2234 645 ▶ if (ref $range eq 'HASH' && !$known{ $range->{col} // '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $range eq "HASH" and not $known{$range->{"col"} // ""}) 0 645 645
Condition20%1/5
ref $range eq "HASH" and not $known{$range->{"col"} // ""}
!ll&&!rl&&r
64500
$range->{"col"} // ""
l!l
00
MC/DC0%0/2
ref $range eq "HASH" and not $known{$range->{"col"} // ""}
ref $range eq "HASH" not $known{$range->{"col"} // ""}
MC/DC0%0/1
$range->{"col"} // ""
$range->{"col"}
Truth table33%1/3
A and not(B)
A= ref $range eq "HASH"B= not $known{$range->{"col"} // ""}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $range->{"col"}
Aresult
11
00
2235 0 return sprintf(_message_text('error_range_column_unknown'), $range->{col}, $range->{col}, $listed->());
2236 }
2237 645 return undef;
2238 }
2239  
2240 # _filter_in_values($val) -> @strings
2241 #
2242 # The values an f=col:in:<val> filter lists: $val is a JSON array, and only its
2243 # plain scalars count (a null is the empty cell). Anything else lists nothing,
2244 # so a damaged link matches no row rather than every row.
2245 32 ▶sub _filter_in_values {
Subroutine _filter_in_values: called CC=5 SCAR=16.1
2246 32 my ($val) = @_;
2247 32 local $@;
2248 32 my $list;
2249 32 ▶ return () unless eval { $list = from_json($val // q{}); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $list = from_json($val // ""); 1 } } 7 25 32
Condition100%2/2
$val // ""
l!l
311
MC/DC100%1/1
$val // ""
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
2250 25 ▶ return () unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 4 21 25
2251 21 ▶ return map { $_ // '' } grep { !ref } @{$list};
Condition100%2/2
$_ // ""
l!l
332
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
2252 }
2253  
2254 # _between_bounds($val) -> ($lo, $hi) | ()
2255 #
2256 # Purpose: Split a between value, "lo:hi", into its two bounds. The
2257 # separator is a colon, and a timestamp holds colons of its own, so
2258 # the split is made where both sides read as dates or timestamps if
2259 # there is such a place, and at the first colon otherwise.
2260 # Input: $val - the filter value.
2261 # Output: The two bounds, both non-empty, in the order written; the empty
2262 # list when there is no colon or either side is empty.
2263 26 ▶sub _between_bounds {
Subroutine _between_bounds: called CC=9 SCAR=22.0
2264 26 my ($val) = @_;
2265 26 ▶ return () unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 25 26
2266 25 my @at;
2267 25 my $pos = -1;
2268 25 ▶ push @at, $pos while ($pos = index($val, ':', $pos + 1)) != -1;
Branch: 1 branch
BranchTrueFalseTotal
($pos = CORE::index($val, ":", $pos + 1)) != -1 and do { push @at, $pos } 35 25 60
2269 25 ▶ return () unless @at;
Branch: 1 branch
BranchTrueFalseTotal
unless @at 3 22 25
2270 22 for my $i (@at) {
2271 30 my ($lo, $hi) = (substr($val, 0, $i), substr($val, $i + 1));
2272 30 ▶ return ($lo, $hi) if _is_iso_date($lo) && _is_iso_date($hi);
Branch: 1 branch
BranchTrueFalseTotal
if _is_iso_date($lo) and _is_iso_date($hi) 10 20 30
Condition100%3/3
_is_iso_date($lo) and _is_iso_date($hi)
!ll&&!rl&&r
17310
MC/DC100%2/2
_is_iso_date($lo) and _is_iso_date($hi)
_is_iso_date($lo) _is_iso_date($hi)
Truth table100%3/3
A and B
A= _is_iso_date($lo)B= _is_iso_date($hi)
ABresult
0X0
100
111
2273 }
2274 12 my ($lo, $hi) = (substr($val, 0, $at[0]), substr($val, $at[0] + 1));
2275 12 ▶ return () unless length $lo && length $hi;
Branch: 1 branch
BranchTrueFalseTotal
unless length $lo and length $hi 3 9 12
Condition100%3/3
length $lo and length $hi
!ll&&!rl&&r
219
MC/DC100%2/2
length $lo and length $hi
length $lo length $hi
Truth table100%3/3
A and B
A= length $loB= length $hi
ABresult
0X0
100
111
2276 9 return ($lo, $hi);
2277 }
2278  
2279 111 ▶sub _apply_filter_spec {
Subroutine _apply_filter_spec: called CC=63 SCAR=42.6
2280 111 my ($records, $spec) = @_;
2281 111 my ($col, $op, $val) = split /:/, $spec, 3;
2282 111 ▶ return $records unless length($col // '') && defined $op && length $op;
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and defined $op and length $op 13 98 111
Condition62%5/8
length($col // "") and defined $op and length $op
!ll&&!rl&&r
13098
length($col // "") and defined $op
!ll&&!rl&&r
01398
$col // ""
l!l
1110
MC/DC33%1/3
length($col // "") and defined $op and length $op
$col defined $op length $op
Truth table50%2/4
A and B and C
A= $colB= defined $opC= length $op
ABCresult
0XX0
10X0
1100
1111
2283 98 ▶ $val //= '';
Condition50%1/2
$val //= ""
l!l
980
MC/DC0%0/1
$val //= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
2284 98 my $lval = lc $val; # precompute once; avoids O(N) redundant lc() inside grep
2285 98 my $between = $op eq 'between';
2286 98 ▶ my ($lo, $hi) = $between ? _between_bounds($val) : ();
Branch: 1 branch
BranchTrueFalseTotal
$between ? : 21 77 98
2287 # A between is about times only when both its bounds are.
2288 98 ▶ my $is_date = $between
Branch: 1 branch
BranchTrueFalseTotal
$between ? : 21 77 98
Condition83%5/6
defined $lo and _is_iso_date($lo) and _is_iso_date($hi)
!ll&&!rl&&r
1209
defined $lo and _is_iso_date($lo)
!ll&&!rl&&r
489
MC/DC66%2/3
defined $lo and _is_iso_date($lo) and _is_iso_date($hi)
defined $lo _is_iso_date($lo) _is_iso_date($hi)
Truth table75%3/4
A and B and C
A= defined $loB= _is_iso_date($lo)C= _is_iso_date($hi)
ABCresult
0XX0
10X0
1100
1111
2289 ? (defined $lo && _is_iso_date($lo) && _is_iso_date($hi))
2290 : _is_iso_date($val);
2291 # Compare at the precision the bound names: a bound with a time in it asks
2292 # about the second, a bare date asks about the day. Without this, a date
2293 # bound compared against a column of timestamps drops every row on the
2294 # boundary day, because the cell string carries more characters than the
2295 # bound and "2026-10-27T09:15:00" sorts after "2026-10-27".
2296 98 ▶ my $prec = ($is_date && (grep { defined((Database::BI::Util::iso_datetime($_))[1]) }
Branch: 2 branches
BranchTrueFalseTotal
$is_date && grep({defined((Database::BI::Util::iso_datetime($_))[1]);} $between ? ($lo, $hi) : $val) ? : 3 95 98
$between ? : 9 2 11
Condition100%3/3
$is_date and grep {defined((Database::BI::Util::iso_datetime($_))[1]);} $between ? ($lo, $hi) : $val
!ll&&!rl&&r
8783
MC/DC100%2/2
$is_date and grep {defined((Database::BI::Util::iso_datetime($_))[1]);} $between ? ($lo, $hi) : $val
$is_date grep {defined((Database::BI::Util::iso_datetime($_))[1]);} $between ? ($lo, $hi) : $val
Truth table100%3/3
A and B
A= $is_dateB= grep {defined((Database::BI::Util::iso_datetime($_))[1]);} $between ? ($lo, $hi) : $val
ABresult
0X0
100
111
2297 $between ? ($lo, $hi) : ($val))) ? 19 : 10;
2298 98 ▶ my $nval = $is_date && !$between ? (_date_key($val, $prec) // $val) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$is_date && !$between ? : 2 96 98
Condition66%4/6
$is_date and not $between
!ll&&!rl&&r
8792
_date_key($val, $prec) // $val
l!l&&r!l&&!r
200
MC/DC100%2/2
$is_date and not $between
$is_date not $between
MC/DC0%0/2
_date_key($val, $prec) // $val
_date_key($val, $prec) $val
Truth table100%3/3
A and not(B)
A= $is_dateB= not $between
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= _date_key($val, $prec)B= $val
ABresult
1X1
011
000
2299 # An order is a statement about numbers. A blank or a word has no place in
2300 # one, so it matches none of the four, rather than counting as zero.
2301 98 ▶ my $by_number = !$is_date && $op =~ /\A(?:lt|le|gt|ge)\z/;
Condition100%3/3
!$is_date && $op =~ /\A(?:lt|le|gt|ge)\z/u
!ll&&!rl&&r
117413
MC/DC100%2/2
!$is_date && $op =~ /\A(?:lt|le|gt|ge)\z/u
!$is_date $op =~ /\A(?:lt|le|gt|ge)\z/u
Truth table100%3/3
not(A) && B
A= !$is_dateB= $op =~ /\A(?:lt|le|gt|ge)\z/u
ABresult
0X0
100
111
2302 98 ▶ my $num_val = $by_number ? _numeric_cell($val) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$by_number ? : 13 85 98
2303 # A between of two numbers (a range of years, of prices) compares as
2304 # numbers, inclusive at both ends and whichever bound is written first;
2305 # "9" sorts after "10" as text. Anything else keeps the text comparison.
2306 # Dates and text likewise: the bounds are put in order before they are
2307 # used, so "2026-12-31:2026-01-01" is the year, not nothing.
2308 98 my ($num_lo, $num_hi);
2309 98 ▶ if ($between && defined $lo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($between and defined $lo) 17 81 98
Condition100%3/3
$between and defined $lo
!ll&&!rl&&r
77417
MC/DC100%2/2
$between and defined $lo
$between defined $lo
Truth table100%3/3
A and B
A= $betweenB= defined $lo
ABresult
0X0
100
111
2310 17 ▶ if ($is_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_date) { } 9 8 17
2311 9 ▶ ($lo, $hi) = map { _date_key($_, $prec) // $_ } $lo, $hi;
Condition33%1/3
_date_key($_, $prec) // $_
l!l&&r!l&&!r
1800
MC/DC0%0/2
_date_key($_, $prec) // $_
_date_key($_, $prec) $_
Truth table33%1/3
A // B
A= _date_key($_, $prec)B= $_
ABresult
1X1
011
000
2312 } else {
2313 8 ($num_lo, $num_hi) = (_numeric_cell($lo), _numeric_cell($hi));
2314 8 ▶ ($num_lo, $num_hi) = (undef, undef) unless defined $num_lo && defined $num_hi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num_lo and defined $num_hi 4 4 8
Condition66%2/3
defined $num_lo and defined $num_hi
!ll&&!rl&&r
404
MC/DC50%1/2
defined $num_lo and defined $num_hi
defined $num_lo defined $num_hi
Truth table66%2/3
A and B
A= defined $num_loB= defined $num_hi
ABresult
0X0
100
111
2315 8 ($lo, $hi) = (lc $lo, lc $hi);
2316 }
2317 17 ▶ ($num_lo, $num_hi) = ($num_hi, $num_lo) if defined $num_lo && $num_lo > $num_hi;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num_lo and $num_lo > $num_hi 1 16 17
Condition100%3/3
defined $num_lo and $num_lo > $num_hi
!ll&&!rl&&r
1331
MC/DC100%2/2
defined $num_lo and $num_lo > $num_hi
defined $num_lo $num_lo > $num_hi
Truth table100%3/3
A and B
A= defined $num_loB= $num_lo > $num_hi
ABresult
0X0
100
111
2318 17 ▶ ($lo, $hi) = ($hi, $lo) if $lo gt $hi;
Branch: 1 branch
BranchTrueFalseTotal
if $lo gt $hi 8 9 17
2319 }
2320 98 ▶ my %in_set = $op eq 'in' ? (map { lc($_) => 1 } _filter_in_values($val)) : ();
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'in' ? : 20 78 98
2321 return [grep {
2322 98 ▶ my $cell = $_->{$col} // '';
Condition100%2/2
$_->{$col} // ""
l!l
39812
MC/DC100%1/1
$_->{$col} // ""
$_->{$col}
Truth table100%2/2
A
A= $_->{$col}
Aresult
11
00
2323 410 ▶ my $num = $by_number ? _numeric_cell($cell) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$by_number ? : 36 374 410
2324 410 ▶ my $both = defined $num && defined $num_val;
Condition66%2/3
defined $num && defined $num_val
!ll&&!rl&&r
374036
MC/DC50%1/2
defined $num && defined $num_val
defined $num defined $num_val
Truth table66%2/3
A && B
A= defined $numB= defined $num_val
ABresult
0X0
100
111
2325 # Normalise M/D/YYYY cells to ISO for date comparisons so that the
2326 # ISO values produced by <input type="date"> match against either format.
2327 410 ▶ my $ncell = $is_date
Branch: 1 branch
BranchTrueFalseTotal
$is_date ? : 43 367 410
Condition33%2/6
_date_key(_to_iso_date($cell) // $cell, $prec) // $cell
l!l&&r!l&&!r
4300
_to_iso_date($cell) // $cell
l!l&&r!l&&!r
4300
MC/DC0%0/2
_date_key(_to_iso_date($cell) // $cell, $prec) // $cell
_date_key(_to_iso_date($cell) // $cell, $prec) $cell
MC/DC0%0/2
_to_iso_date($cell) // $cell
_to_iso_date($cell) $cell
Truth table33%1/3
A // B
A= _date_key(_to_iso_date($cell) // $cell, $prec)B= $cell
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= _to_iso_date($cell)B= $cell
ABresult
1X1
011
000
2328 ? (_date_key(_to_iso_date($cell) // $cell, $prec) // $cell)
2329 : $cell;
2330 $op eq 'eq' ? ($is_date ? $ncell eq $nval : lc($cell) eq $lval) :
2331 $op eq 'ne' ? ($is_date ? $ncell ne $nval : lc($cell) ne $lval) :
2332 $op eq 'contains' ? index(lc($cell), $lval) != -1 :
2333 $op eq 'starts' ? index(lc($cell), $lval) == 0 :
2334 $op eq 'lt' ? ($is_date ? $ncell lt $nval : $both && $num < $num_val) :
2335 $op eq 'le' ? ($is_date ? $ncell le $nval : $both && $num <= $num_val) :
2336 $op eq 'gt' ? ($is_date ? $ncell gt $nval : $both && $num > $num_val) :
2337 $op eq 'ge' ? ($is_date ? $ncell ge $nval : $both && $num >= $num_val) :
2338 $op eq 'empty' ? $cell eq '' :
2339 $op eq 'notempty' ? $cell ne '' :
2340 $op eq 'in' ? $in_set{ lc($cell) } :
2341 $op eq 'between' && defined $num_lo ? do {
2342 40 my $n = _numeric_cell($cell);
2343 40 ▶ defined $n && $n >= $num_lo && $n <= $num_hi
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'between' ? : 82 5 87
Condition100%6/6
defined $n && $n >= $num_lo && $n <= $num_hi
!ll&&!rl&&r
20128
defined $n and $n >= $num_lo
!ll&&!rl&&r
81220
MC/DC100%3/3
defined $n && $n >= $num_lo && $n <= $num_hi
defined $n $n >= $num_lo $n <= $num_hi
Truth table100%4/4
A and B && C
A= defined $nB= $n >= $num_loC= $n <= $num_hi
ABCresult
0XX0
10X0
1100
1111
2344 } :
2345 410 ▶ $op eq 'between' ? do {
Branch: 18 branches
BranchTrueFalseTotal
$op eq 'eq' ? : 139 271 410
$is_date ? : 0 139 139
$op eq 'ne' ? : 2 269 271
$is_date ? : 0 2 2
$op eq 'contains' ? : 4 265 269
$op eq 'starts' ? : 4 261 265
$op eq 'lt' ? : 3 258 261
$is_date ? : 0 3 3
$op eq 'le' ? : 6 252 258
$is_date ? : 1 5 6
$op eq 'gt' ? : 18 234 252
$is_date ? : 0 18 18
$op eq 'ge' ? : 12 222 234
$is_date ? : 2 10 12
$op eq 'empty' ? : 3 219 222
$op eq 'notempty' ? : 3 216 219
$op eq 'in' ? : 89 127 216
$op eq 'between' && defined $num_lo ? : 40 87 127
Condition73%11/15
$both and $num < $num_val
!ll&&!rl&&r
021
$both and $num <= $num_val
!ll&&!rl&&r
023
$both and $num > $num_val
!ll&&!rl&&r
0810
$both and $num >= $num_val
!ll&&!rl&&r
055
$op eq "between" and defined $num_lo
!ll&&!rl&&r
58240
MC/DC50%1/2
$both and $num < $num_val
$both $num < $num_val
MC/DC50%1/2
$both and $num <= $num_val
$both $num <= $num_val
MC/DC50%1/2
$both and $num > $num_val
$both $num > $num_val
MC/DC50%1/2
$both and $num >= $num_val
$both $num >= $num_val
MC/DC100%2/2
$op eq "between" and defined $num_lo
$op eq "between" defined $num_lo
Truth table66%2/3
A and B
A= $bothB= $num < $num_val
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $bothB= $num <= $num_val
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $bothB= $num > $num_val
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $bothB= $num >= $num_val
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $op eq "between"B= defined $num_lo
ABresult
0X0
100
111
2346 82 ▶ my $key = $is_date ? $ncell : lc $cell;
Branch: 1 branch
BranchTrueFalseTotal
$is_date ? : 40 42 82
2347 82 ▶ defined $lo && $key ge $lo && $key le $hi
Condition100%6/6
defined $lo && $key ge $lo && $key le $hi
!ll&&!rl&&r
361432
defined $lo and $key ge $lo
!ll&&!rl&&r
241246
MC/DC100%3/3
defined $lo && $key ge $lo && $key le $hi
defined $lo $key ge $lo $key le $hi
Truth table100%4/4
A and B && C
A= defined $loB= $key ge $loC= $key le $hi
ABCresult
0XX0
10X0
1100
1111
2348 } :
2349 1
2350 } @$records];
2351 }
2352  
2353 # ---------------------------------------------------------------------------
2354 # The cross-row pass (BI-Phase-7.md item 7.2)
2355 # ---------------------------------------------------------------------------
2356 #
2357 # A measure folds a group into one number and a calculated field computes one
2358 # row from that row. This is the third shape: one row from many rows, written
2359 # back onto every row -- a running total, a moving average, a rank, a share of
2360 # the whole. None of them can be a measure (the answer is per row, not per
2361 # group) and none can be a calc field (it needs its neighbours), which is why
2362 # they are a pass of their own.
2363 #
2364 # The functions follow Splunk's split, because it is also the natural one:
2365 # the eventstats family is order-free (pct_of_total, rank, zscore), the
2366 # streamstats family is incremental and therefore needs an order (running_*,
2367 # window_*, lag, delta, pct_change).
2368  
2369 # The same vocabulary as a menu, for the panel that adds a cross-row column.
2370 # It is data rather than a list written into the template so the function
2371 # names, their labels and the two facts the form needs -- whether a function
2372 # takes a window, and whether it needs an order -- cannot drift from the
2373 # validator above.
2374 5 Readonly my @CROSS_ROW_MENU => (
2375 { name => 'running_total', label => 'Running total', arg => 0, order => 1 },
2376 { name => 'running_count', label => 'Running count', arg => 0, order => 1 },
2377 { name => 'running_pct', label => 'Running percent', arg => 0, order => 1 },
2378 { name => 'window_sum', label => 'Moving sum', arg => 1, order => 1 },
2379 { name => 'window_avg', label => 'Moving average', arg => 1, order => 1 },
2380 { name => 'lag', label => 'Previous row', arg => 0, order => 1 },
2381 { name => 'delta', label => 'Change from previous', arg => 0, order => 1 },
2382 { name => 'pct_change', label => 'Percent change', arg => 0, order => 1 },
2383 { name => 'pct_of_total', label => 'Percent of total', arg => 0, order => 0 },
2384 { name => 'rank', label => 'Rank, largest first', arg => 0, order => 0 },
2385 { name => 'zscore', label => 'Z-score', arg => 0, order => 0 },
2386 );
2387  
2388 5 Readonly my %CROSS_ROW_FNS => (
2389 running_total => { family => 'stream' },
2390 running_count => { family => 'stream' },
2391 running_pct => { family => 'stream' },
2392 window_sum => { family => 'stream', arg => 'count' },
2393 window_avg => { family => 'stream', arg => 'count' },
2394 lag => { family => 'stream' },
2395 delta => { family => 'stream' },
2396 pct_change => { family => 'stream' },
2397 pct_of_total => { family => 'event' },
2398 rank => { family => 'event' },
2399 zscore => { family => 'event' },
2400 );
2401  
2402 # _cross_row_specs($self, \@columns, $params) -> \%plan | { error => $message } | undef
2403 #
2404 # Purpose: Read cs=/by=/order= and turn them into a plan the pass can run:
2405 # which function writes which column, partitioned by what, ordered by
2406 # what. Nothing is evaluated here, so a request that names a column
2407 # or a function that does not exist is refused before a single row is
2408 # touched, and the refusal is a 400 rather than a chart of something
2409 # else.
2410 # Entry: \@columns -- the columns of this view, so a cs=/by=/order= column is
2411 # checked against what the reader can actually see.
2412 # $params -- where the three parameters are read from, or undef for
2413 # this request. Anything with param()/every_param() will
2414 # do: the canvas reads them from the canvas page's own query
2415 # string, because its cards are drawn against the canvas's
2416 # state rather than against whatever URL fetched them.
2417 # Exit: undef when the request names no cross-row column. Otherwise
2418 # { specs => [ { col, fn, as, arg } ], by => [col ...],
2419 # order => [ { col, dir } ] }, or { error => $message } for a
2420 # request that cannot be honoured.
2421 # Notes: The streamstats family reads its rows in order, so order= is
2422 # required for it and refused as missing by name. The eventstats
2423 # family does not care and does not require it.
2424 60 ▶sub _cross_row_specs :Protected ($self, $columns, $params = undef) {
Subroutine _cross_row_specs: called CC=6 SCAR=18.5
2425 60 ▶ my $p = $params // $self;
Condition66%2/3
$params // $self
l!l&&r!l&&!r
24360
MC/DC0%0/2
$params // $self
$params $self
Truth table66%2/3
A // B
A= $paramsB= $self
ABresult
1X1
011
000
2426  
2427 # The three values are the URL's spelling of a plan, read once here so the
2428 # parser below serves a canvas card too - a card stores its own plan as
2429 # { cs, by, order } rather than as a query string.
2430 return $self->_cross_row_specs_from($columns, {
2431 60 ▶ cs => [ @{ $p->every_param('cs') // [] } ],
Condition75%3/4
$p->every_param("cs") // []
l!l
600
$p->param("by") // ""
l!l
159
MC/DC0%0/1
$p->every_param("cs") // []
$p->every_param("cs")
MC/DC100%1/1
$p->param("by") // ""
$p->param("by")
Truth table50%1/2
A
A= $p->every_param("cs")
Aresult
11
00
Truth table100%2/2
A
A= $p->param("by")
Aresult
11
00
2432 by => ($p->param('by') // ''),
2433 60 ▶ order => [ @{ $p->every_param('order') // [] } ],
Condition50%1/2
$p->every_param("order") // []
l!l
600
MC/DC0%0/1
$p->every_param("order") // []
$p->every_param("order")
Truth table50%1/2
A
A= $p->every_param("order")
Aresult
11
00
2434 });
2435 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2436  
2437 # _cross_row_specs_from($self, \@columns, \%raw) -> \%plan | { error } | undef
2438 #
2439 # Purpose: The same validation, from a plan as a saved canvas card stores it.
2440 # Entry: \%raw -- { cs => [spec ...], by => 'a,b', order => ['col:dir' ...] },
2441 # in the spelling cs=/by=/order= use.
2442 # Exit: As _cross_row_specs.
2443 77 ▶sub _cross_row_specs_from :Protected ($self, $columns, $raw) {
Subroutine _cross_row_specs_from: called CC=44 SCAR=38.8
2444 77 ▶ my @texts = grep { defined $_ && !ref $_ && length } @{ $raw->{cs} // [] };
Condition100%8/8
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
2134
defined $_ and not ref $_
!ll&&!rl&&r
1135
$raw->{"cs"} // []
l!l
761
MC/DC100%3/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
MC/DC100%1/1
$raw->{"cs"} // []
$raw->{"cs"}
Truth table100%4/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $raw->{"cs"}
Aresult
11
00
2445 77 ▶ return undef unless @texts;
Branch: 1 branch
BranchTrueFalseTotal
unless @texts 44 33 77
2446  
2447 33 ▶ my %col_set = map { $_ => 1 } @{ $columns // [] };
Condition50%1/2
$columns // []
l!l
330
MC/DC0%0/1
$columns // []
$columns
Truth table50%1/2
A
A= $columns
Aresult
11
00
2448 33 my %taken;
2449 my @specs;
2450  
2451 33 for my $text (@texts) {
2452 34 my ($col, $fn, $as, $arg) = split /:/, $text, 4;
2453 34 ▶ return { error => $self->_i18n('error_cs_spec', $text) }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and length $col and defined $fn and length $fn and defined $as and length $as 15 19 34
Condition80%12/15
defined $col and length $col and defined $fn and length $fn and defined $as and length $as
!ll&&!rl&&r
15019
defined $col and length $col and defined $fn and length $fn and defined $as
!ll&&!rl&&r
12319
defined $col and length $col and defined $fn and length $fn
!ll&&!rl&&r
12022
defined $col and length $col and defined $fn
!ll&&!rl&&r
11122
defined $col and length $col
!ll&&!rl&&r
0133
MC/DC50%3/6
defined $col and length $col and defined $fn and length $fn and defined $as and length $as
defined $col length $col defined $fn length $fn defined $as length $as
Truth table57%4/7
A and B and C and D and E and F
A= defined $colB= length $colC= defined $fnD= length $fnE= defined $asF= length $as
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
2454 unless defined $col && length $col
2455 && defined $fn && length $fn
2456 && defined $as && length $as;
2457  
2458 return { error => $self->_i18n('error_cs_column', $text, $col) }
2459 19 ▶ unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 2 17 19
2460  
2461 return { error => $self->_i18n('error_cs_fn', $fn, join(' ', sort keys %CROSS_ROW_FNS)) }
2462 17 ▶ unless $CROSS_ROW_FNS{$fn};
Branch: 1 branch
BranchTrueFalseTotal
unless $CROSS_ROW_FNS{$fn} 1 16 17
2463  
2464 16 ▶ return { error => $self->_i18n('error_cs_alias', $as) }
Branch: 1 branch
BranchTrueFalseTotal
unless $as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/u 1 15 16
2465 unless $as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
2466  
2467 return { error => $self->_i18n(q{error_cs_conflict}, $as) }
2468 15 ▶ if $col_set{$as} || $taken{$as};
Branch: 1 branch
BranchTrueFalseTotal
if $col_set{$as} or $taken{$as} 2 13 15
Condition100%3/3
$col_set{$as} or $taken{$as}
l!l&&r!l&&!r
1113
MC/DC100%2/2
$col_set{$as} or $taken{$as}
$col_set{$as} $taken{$as}
Truth table100%3/3
A or B
A= $col_set{$as}B= $taken{$as}
ABresult
1X1
011
000
2469 13 $taken{$as} = 1;
2470  
2471 13 my $decl = $CROSS_ROW_FNS{$fn};
2472 13 ▶ if ($decl->{arg}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($decl->{'arg'}) { } 3 10 13
elsif (defined $arg and length $arg) { } 1 18 19
2473 3 ▶ return { error => $self->_i18n('error_cs_arg', $text, $fn, $col, $fn) }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg and $arg =~ /\A\d+\z/u and $arg > 0 3 0 3
Condition83%5/6
defined $arg and $arg =~ /\A\d+\z/u and $arg > 0
!ll&&!rl&&r
210
defined $arg and $arg =~ /\A\d+\z/u
!ll&&!rl&&r
111
MC/DC0%0/3
defined $arg and $arg =~ /\A\d+\z/u and $arg > 0
defined $arg $arg =~ /\A\d+\z/u $arg > 0
Truth table75%3/4
A and B and C
A= defined $argB= $arg =~ /\A\d+\z/uC= $arg > 0
ABCresult
0XX0
10X0
1100
1111
2474 unless defined $arg && $arg =~ /\A\d+\z/ && $arg > 0;
2475 }
2476 ▶ elsif (defined $arg && length $arg) {
Condition66%2/3
defined $arg and length $arg
!ll&&!rl&&r
901
MC/DC50%1/2
defined $arg and length $arg
defined $arg length $arg
Truth table66%2/3
A and B
A= defined $argB= length $arg
ABresult
0X0
100
111
2477 1 return { error => $self->_i18n('error_cs_no_arg', $text, $fn) };
2478 }
2479  
2480 9 ▶ push @specs, {
Branch: 1 branch
BranchTrueFalseTotal
defined $arg && length $arg ? : 0 9 9
Condition33%1/3
defined $arg and length $arg
!ll&&!rl&&r
900
MC/DC0%0/2
defined $arg and length $arg
defined $arg length $arg
Truth table33%1/3
A and B
A= defined $argB= length $arg
ABresult
0X0
100
111
2481 col => $col,
2482 fn => $fn,
2483 as => $as,
2484 arg => (defined $arg && length $arg) ? $arg + 0 : undef,
2485 };
2486 }
2487  
2488 # by= is the partition. An empty value is its own partition rather than a
2489 # dropped row: a windowed column has to cover every displayed row, which is
2490 # not the rule the pivot uses for a cell -- see the POD.
2491 8 ▶ my $by_text = (!ref $raw->{by} && defined $raw->{by}) ? "$raw->{by}" : '';
Branch: 1 branch
BranchTrueFalseTotal
!ref($raw->{'by'}) && defined $raw->{'by'} ? : 5 3 8
Condition66%2/3
not ref $raw->{"by"} and defined $raw->{"by"}
!ll&&!rl&&r
035
MC/DC50%1/2
not ref $raw->{"by"} and defined $raw->{"by"}
not ref $raw->{"by"} defined $raw->{"by"}
Truth table66%2/3
not(A) and B
A= not ref $raw->{"by"}B= defined $raw->{"by"}
ABresult
0X0
100
111
2492 8 my @by = grep { length } map { _trim($_) } split /,/, $by_text, -1;
2493 8 for my $c (@by) {
2494 3 ▶ return { error => $self->_i18n('error_cs_by', $by_text, $c) } unless $col_set{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$c} 1 2 3
2495 }
2496  
2497 7 my @order;
2498 7 ▶ for my $text (@{ $raw->{order} // [] }) {
Condition100%2/2
$raw->{"order"} // []
l!l
61
MC/DC100%1/1
$raw->{"order"} // []
$raw->{"order"}
Truth table100%2/2
A
A= $raw->{"order"}
Aresult
11
00
2499 5 ▶ my $spec = _trim((!ref $text && defined $text) ? $text : '');
Branch: 1 branch
BranchTrueFalseTotal
!ref($text) && defined $text ? : 5 0 5
Condition33%1/3
not ref $text and defined $text
!ll&&!rl&&r
005
MC/DC0%0/2
not ref $text and defined $text
not ref $text defined $text
Truth table33%1/3
not(A) and B
A= not ref $textB= defined $text
ABresult
0X0
100
111
2500 5 ▶ next unless length $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 0 5 5
2501 5 my ($c, $dir) = split /:/, $spec, 2;
2502 5 ▶ $c = _trim($c // '');
Condition50%1/2
$c // ""
l!l
50
MC/DC0%0/1
$c // ""
$c
Truth table50%1/2
A
A= $c
Aresult
11
00
2503 5 ▶ $dir = defined $dir && length $dir ? lc _trim($dir) : 'asc';
Branch: 1 branch
BranchTrueFalseTotal
defined $dir && length $dir ? : 2 3 5
Condition66%2/3
defined $dir and length $dir
!ll&&!rl&&r
302
MC/DC50%1/2
defined $dir and length $dir
defined $dir length $dir
Truth table66%2/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
2504 5 ▶ return { error => $self->_i18n('error_cs_order', $spec) }
Branch: 1 branch
BranchTrueFalseTotal
unless length $c and $dir =~ /\A(?:asc|desc)\z/u 1 4 5
Condition66%2/3
length $c and $dir =~ /\A(?:asc|desc)\z/u
!ll&&!rl&&r
014
MC/DC50%1/2
length $c and $dir =~ /\A(?:asc|desc)\z/u
length $c $dir =~ /\A(?:asc|desc)\z/u
Truth table66%2/3
A and B
A= length $cB= $dir =~ /\A(?:asc|desc)\z/u
ABresult
0X0
100
111
2505 unless length $c && $dir =~ /\A(?:asc|desc)\z/;
2506 return { error => $self->_i18n('error_cs_order_col', $spec, $c) }
2507 4 ▶ unless $col_set{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$c} 1 3 4
2508 3 push @order, { col => $c, dir => $dir };
2509 }
2510  
2511 5 for my $spec (@specs) {
2512 5 ▶ next unless $CROSS_ROW_FNS{ $spec->{fn} }{family} eq 'stream';
Branch: 1 branch
BranchTrueFalseTotal
unless $CROSS_ROW_FNS{$spec->{"fn"}}{"family"} eq "stream" 2 3 5
2513 return {
2514 error => $self->_i18n('error_cs_order_need',
2515 join(':', $spec->{col}, $spec->{fn}, $spec->{as}), $spec->{fn})
2516 3 ▶ } unless @order;
Branch: 1 branch
BranchTrueFalseTotal
unless @order 1 2 3
2517 }
2518  
2519 4 return { specs => \@specs, by => \@by, order => \@order };
2520 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2521  
2522 # _card_plan($raw) -> \%plan | { error }
2523 #
2524 # Purpose: The cross-row plan a saved canvas card carries, in the spelling
2525 # cs=/by=/order= use, so a card added and a card repaired store the
2526 # same shape and the one parser above reads them both.
2527 # Entry: $raw -- the request's cross_row value. A hash that names no cs is
2528 # the card saying it runs no plan of its own; _cross_row_specs_from
2529 # reads that as no pass at all, and so does the renderer.
2530 # Exit: { cs => [...], by => '', order => [...] } -- only the shape is
2531 # checked here. Which columns exist is a fact about the data file and
2532 # is checked again on every render, so a plan written today is read
2533 # against the columns the canvas has today.
2534 # Notes: A part of the plan that is the wrong kind is an error rather than a
2535 # silently empty list: a repair that stores nothing still answers 200,
2536 # and the caller would have no way to know the card had not changed.
2537 7 ▶sub _card_plan {
Subroutine _card_plan: called CC=11 SCAR=24.0
2538 7 my ($raw) = @_;
2539  
2540 7 for my $key (qw(cs order)) {
2541 11 ▶ next unless defined $raw->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw->{$key} 5 6 11
2542 return { error => "cross_row.$key must be a list" }
2543 6 ▶ if ref $raw->{$key} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw->{$key} ne "ARRAY" 3 3 6
2544 return { error => "cross_row.$key must be a list of strings" }
2545 3 ▶ if grep { ref } @{ $raw->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} @{$$raw{$key};} 1 2 3
2546 }
2547 3 ▶ return { error => 'cross_row.by must be a column name' } if ref $raw->{by};
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw->{"by"} 1 2 3
2548  
2549 return {
2550 3 ▶ cs => [ grep { defined $_ && length } @{ $raw->{cs} // [] } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $raw->{'by'} ? : 1 1 2
Condition100%5/5
defined $_ && length $_
!ll&&!rl&&r
111
$raw->{"cs"} // []
l!l
11
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
MC/DC100%1/1
$raw->{"cs"} // []
$raw->{"cs"}
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table100%2/2
A
A= $raw->{"cs"}
Aresult
11
00
2551 by => (defined $raw->{by} ? "$raw->{by}" : ''),
2552 2 ▶ order => [ grep { defined $_ && length } @{ $raw->{order} // [] } ],
Condition60%3/5
defined $_ && length $_
!ll&&!rl&&r
001
$raw->{"order"} // []
l!l
11
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC100%1/1
$raw->{"order"} // []
$raw->{"order"}
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table100%2/2
A
A= $raw->{"order"}
Aresult
11
00
2553 };
2554 }
2555  
2556 # _cross_row_ui_state($c) -> %stash_pairs
2557 #
2558 # Purpose: The cross-row state the table page shows: the specs it puts in
2559 # chips, the partition and order its chart panels carry, and the
2560 # panel's own menu. Also the rest of the query string, because the
2561 # panel edits three parameters and must submit the others unchanged.
2562 # Entry: $c -- the controller, for its query string only.
2563 # Exit: A list suitable for stash(): cross_row_specs, cross_row_by,
2564 # cross_row_order, cross_row_menu, cross_row_hidden.
2565 # Notes: cs=/by=/order= are deliberately absent from cross_row_hidden. The
2566 # panel writes them from its own controls, and a hidden by= as well
2567 # would send two values; param('by') reads the first, so the reader's
2568 # choice would be silently ignored in favour of the old one.
2569 # Only the query string is read -- never the body -- because this runs
2570 # before dispatch on every request, including JSON posts.
2571 2186 ▶sub _cross_row_ui_state {
Subroutine _cross_row_ui_state: called CC=9 SCAR=22.0
2572 2186 my ($c) = @_;
2573  
2574 2186 my $query = $c->req->query_params;
2575 2186 ▶ my @specs = grep { length } @{ $query->every_param('cs') // [] };
Condition50%1/2
$query->every_param("cs") // []
l!l
21860
MC/DC0%0/1
$query->every_param("cs") // []
$query->every_param("cs")
Truth table50%1/2
A
A= $query->every_param("cs")
Aresult
11
00
2576 2186 ▶ my @orders = grep { length } @{ $query->every_param('order') // [] };
Condition50%1/2
$query->every_param("order") // []
l!l
21860
MC/DC0%0/1
$query->every_param("order") // []
$query->every_param("order")
Truth table50%1/2
A
A= $query->every_param("order")
Aresult
11
00
2577  
2578 2186 my @hidden;
2579 # names() is a list in some Mojo releases and an arrayref in others; taking
2580 # the arrayref is what both accept.
2581 2186 for my $name (@{ $query->names }) {
2582 17410 ▶ next if $name eq 'cs' || $name eq 'by' || $name eq 'order';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "cs" or $name eq "by" or $name eq "order" 769 16641 17410
Condition100%6/6
$name eq "cs" or $name eq "by" or $name eq "order"
l!l&&r!l&&!r
51325616641
$name eq "cs" or $name eq "by"
l!l&&r!l&&!r
25725616897
MC/DC100%3/3
$name eq "cs" or $name eq "by" or $name eq "order"
$name eq "cs" $name eq "by" $name eq "order"
Truth table100%4/4
A or B or C
A= $name eq "cs"B= $name eq "by"C= $name eq "order"
ABCresult
1XX1
01X1
0011
0000
2583 16780 push @hidden, map { { name => $name, value => $_ } }
2584 16641 ▶ @{ $query->every_param($name) // [] };
Condition50%1/2
$query->every_param($name) // []
l!l
166410
MC/DC0%0/1
$query->every_param($name) // []
$query->every_param($name)
Truth table50%1/2
A
A= $query->every_param($name)
Aresult
11
00
2585 }
2586  
2587 return (
2588 2186 ▶ cross_row_specs => \@specs,
Condition100%2/2
$query->param("by") // ""
l!l
2561930
MC/DC100%1/1
$query->param("by") // ""
$query->param("by")
Truth table100%2/2
A
A= $query->param("by")
Aresult
11
00
2589 cross_row_by => ($query->param('by') // ''),
2590 cross_row_order => \@orders,
2591 cross_row_menu => \@CROSS_ROW_MENU,
2592 cross_row_hidden => \@hidden,
2593 );
2594 }
2595  
2596 # _apply_cross_row($self, $records, \@columns, $plan) -> ($records, \@columns)
2597 #
2598 # Purpose: Stage 6 of the pipeline: run every cs= spec over the rows and write
2599 # the answer onto each row as a column of its own. Called from
2600 # _reduce_records (the routes that reduce there) and from _fetch_page
2601 # after its own filtering (the routes that paginate), always before
2602 # anything slices a page, so page 2 continues page 1's running total.
2603 # Entry: $records -- arrayref of hashrefs, already filtered.
2604 # \@columns -- the columns list, or undef where the caller has none in
2605 # scope; then it is derived from the first row.
2606 # $plan -- an already validated plan from _cross_row_specs($columns,
2607 # $params), or undef to read this request's cs=. A canvas
2608 # validates once and runs the plan once per card, on each
2609 # card's own rows; re-reading cs= there would work today only
2610 # because the canvas page carries it, and would silently stop
2611 # working for a card redrawn by an API call.
2612 # Exit: ($records, \@columns) with the produced columns appended, or the
2613 # arguments untouched when the request names no cs=. Croaks with an
2614 # i18n message when a spec cannot be honoured; the caller renders 400.
2615 # Notes: Rows are grouped into partitions by by=, each partition is sorted
2616 # once (stable, ties in arrival order, so two runs over the same file
2617 # agree), and each spec is then one pass over each partition. A
2618 # partition total is accumulated in that pass, never per row.
2619 #
2620 # The rows keep the order they arrived in: order= says which order the
2621 # window is read along, not which order the table is shown in. The
2622 # table has its own sort controls, and a reader may well want them to
2623 # disagree (a ledger sorted by amount, running totals following the
2624 # date), so the pass writes values in place rather than reordering.
2625 #
2626 # A cell that is not numeric yields undef in the produced column rather
2627 # than a placeholder: there is no previous row for the first row of a
2628 # partition, and a chart skips undef while the measure fold does too,
2629 # so an undef window cannot silently become a zero.
2630 19 ▶sub _apply_cross_row :Protected ($self, $records, $columns, $plan = undef) {
Subroutine _apply_cross_row: called CC=23 SCAR=31.7
2631 19 my $cols = $columns;
2632 19 ▶ unless (defined $cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cols) 1 18 19
2633 1 ▶ $cols = [ sort keys %{ $records->[0] // {} } ];
Condition50%1/2
$records->[0] // {}
l!l
10
MC/DC0%0/1
$records->[0] // {}
$records->[0]
Truth table50%1/2
A
A= $records->[0]
Aresult
11
00
2634 }
2635  
2636 19 ▶ $plan //= $self->_cross_row_specs($cols);
Condition66%2/3
$plan //= $self->_cross_row_specs($cols)
l!l&&r!l&&!r
0181
MC/DC50%1/2
$plan //= $self->_cross_row_specs($cols)
$plan $self->_cross_row_specs($cols)
Truth table66%2/3
A //= B
A= $planB= $self->_cross_row_specs($cols)
ABresult
1X1
011
000
2637 19 ▶ return ($records, $columns) unless $plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $plan 1 18 19
2638 18 ▶ croak $plan->{error} if $plan->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{"error"} 15 3 18
2639  
2640 # Partitions, in first-appearance order so a report reads in file order.
2641 3 my (@part_keys, %parts);
2642 3 for my $i (0 .. $#$records) {
2643 5 ▶ my $row = $records->[$i] // {};
Condition50%1/2
$records->[$i] // {}
l!l
50
MC/DC0%0/1
$records->[$i] // {}
$records->[$i]
Truth table50%1/2
A
A= $records->[$i]
Aresult
11
00
2644 5 ▶ my $key = join("\x00", map { defined $row->{$_} ? "$row->{$_}" : '' } @{ $plan->{by} });
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{$_} ? : 3 0 3
2645 5 ▶ push @part_keys, $key unless exists $parts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $parts{$key} 4 1 5
2646 5 push @{ $parts{$key} }, $i;
2647 }
2648  
2649 # One sort per partition, whatever the number of specs. The index is the
2650 # tie-break, so the sort is stable even where the keys are equal.
2651 3 ▶ if (my @order = @{ $plan->{order} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@order) = @{$plan->{"order"};}) 1 2 3
2652 1 for my $key (@part_keys) {
2653 2 my $idx = $parts{$key};
2654 2 ▶ next unless @$idx > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$idx > 1 1 1 2
2655 $parts{$key} = [ sort {
2656 1 my $cmp = 0;
2657 1 for my $o (@order) {
2658 1 $cmp = _cross_row_cmp($records->[$a]{ $o->{col} }, $records->[$b]{ $o->{col} });
2659 1 ▶ $cmp = -$cmp if $o->{dir} eq 'desc';
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"dir"} eq "desc" 0 1 1
2660 1 ▶ last if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 1 0 1
2661 }
2662 1 ▶ $cmp || $a <=> $b;
Condition33%1/3
$cmp || $a <=> $b
l!l&&r!l&&!r
100
MC/DC0%0/2
$cmp || $a <=> $b
$cmp $a <=> $b
Truth table33%1/3
A || B
A= $cmpB= $a <=> $b
ABresult
1X1
011
000
2663 } @$idx ];
2664 }
2665 }
2666  
2667 3 my @names;
2668 3 for my $spec (@{ $plan->{specs} }) {
2669 3 my ($fn, $as, $col) = @{$spec}{qw(fn as col)};
2670 3 for my $key (@part_keys) {
2671 4 my $idx = $parts{$key};
2672 4 my @vals = map { _numeric_cell($records->[$_]{$col}) } @$idx;
2673 4 my @out = _cross_row_values($fn, \@vals, $spec->{arg});
2674 4 for my $j (0 .. $#$idx) {
2675 5 my $i = $idx->[$j];
2676 5 $records->[$i] = _extend_row($records->[$i], $as, $out[$j]);
2677 }
2678 }
2679 3 push @names, $as;
2680 }
2681  
2682 3 my @extended = (@$cols, @names);
2683 3 ▶ $self->stash('_virtual_columns' => {}) unless $self->stash('_virtual_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("_virtual_columns") 3 0 3
2684 3 $self->stash('_virtual_columns')->{$_} = 1 for @names;
2685 # The routes that paginate ask for their columns separately, after the page
2686 # is sliced, so the names are left where they can be picked up.
2687 3 $self->stash('_cross_row_columns' => \@names);
2688  
2689 3 return ($records, \@extended);
2690 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2691  
2692 # _cross_row_values($fn, \@values, $arg) -> @values
2693 #
2694 # The arithmetic of one spec over one partition: @values is the source column
2695 # of each row in order, numeric or undef, and the answer is aligned with it.
2696 # A plain function, so the pass has one place where the functions are defined
2697 # and the tests can drive them without a controller.
2698 24 ▶sub _cross_row_values {
Subroutine _cross_row_values: called CC=44 SCAR=37.8
2699 24 my ($fn, $vals, $arg) = @_;
2700 24 my @out;
2701 24 my $n = scalar @$vals;
2702 24 ▶ return @out unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 23 24
2703  
2704 23 ▶ if ($fn eq 'running_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "running_count") 1 22 23
2705 1 return map { $_ + 1 } 0 .. $n - 1;
2706 }
2707  
2708 22 ▶ if ($fn eq 'running_total') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "running_total") 4 18 22
2709 4 my $run = 0;
2710 4 for my $v (@$vals) {
2711 9 ▶ $run += $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 8 1 9
2712 9 ▶ push @out, defined $v ? $run : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 8 1 9
2713 }
2714 4 return @out;
2715 }
2716  
2717 18 ▶ if ($fn eq 'running_pct') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "running_pct") 2 16 18
2718 2 my $total = 0;
2719 2 $total += $_ for grep { defined } @$vals;
2720 2 my $run = 0;
2721 2 for my $v (@$vals) {
2722 4 ▶ $run += $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 4 0 4
2723 4 ▶ push @out, ($total && defined $v) ? $run / $total : undef;
Branch: 1 branch
BranchTrueFalseTotal
$total && defined $v ? : 2 2 4
Condition66%2/3
$total and defined $v
!ll&&!rl&&r
202
MC/DC50%1/2
$total and defined $v
$total defined $v
Truth table66%2/3
A and B
A= $totalB= defined $v
ABresult
0X0
100
111
2724 }
2725 2 return @out;
2726 }
2727  
2728 16 ▶ if ($fn eq 'window_sum' || $fn eq 'window_avg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "window_sum" or $fn eq "window_avg") 4 12 16
Condition100%3/3
$fn eq "window_sum" or $fn eq "window_avg"
l!l&&r!l&&!r
3112
MC/DC100%2/2
$fn eq "window_sum" or $fn eq "window_avg"
$fn eq "window_sum" $fn eq "window_avg"
Truth table100%3/3
A or B
A= $fn eq "window_sum"B= $fn eq "window_avg"
ABresult
1X1
011
000
2729 4 for my $i (0 .. $n - 1) {
2730 10 my $lo = $i - $arg + 1;
2731 10 ▶ $lo = 0 if $lo < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lo < 0 5 5 10
2732 10 my @win = grep { defined } @{$vals}[$lo .. $i];
2733 10 ▶ unless (@win) { push @out, undef; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (@win) 2 8 10
2734 8 my $sum = 0;
2735 8 $sum += $_ for @win;
2736 8 ▶ push @out, $fn eq 'window_sum' ? $sum : $sum / scalar @win;
Branch: 1 branch
BranchTrueFalseTotal
$fn eq 'window_sum' ? : 5 3 8
2737 }
2738 4 return @out;
2739 }
2740  
2741 12 ▶ if ($fn eq 'lag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "lag") 1 11 12
2742 1 my $prev;
2743 1 for my $v (@$vals) {
2744 3 push @out, $prev;
2745 3 $prev = $v;
2746 }
2747 1 return @out;
2748 }
2749  
2750 11 ▶ if ($fn eq 'delta' || $fn eq 'pct_change') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "delta" or $fn eq "pct_change") 2 9 11
Condition100%3/3
$fn eq "delta" or $fn eq "pct_change"
l!l&&r!l&&!r
119
MC/DC100%2/2
$fn eq "delta" or $fn eq "pct_change"
$fn eq "delta" $fn eq "pct_change"
Truth table100%3/3
A or B
A= $fn eq "delta"B= $fn eq "pct_change"
ABresult
1X1
011
000
2751 2 my $prev;
2752 2 for my $v (@$vals) {
2753 8 my $d;
2754 8 ▶ if (defined $v && defined $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and defined $prev) 4 4 8
Condition100%3/3
defined $v and defined $prev
!ll&&!rl&&r
134
MC/DC100%2/2
defined $v and defined $prev
defined $v defined $prev
Truth table100%3/3
A and B
A= defined $vB= defined $prev
ABresult
0X0
100
111
2755 4 ▶ $d = $fn eq 'delta' ? $v - $prev : ($prev ? ($v - $prev) / $prev : undef);
Branch: 2 branches
BranchTrueFalseTotal
$fn eq 'delta' ? : 1 3 4
$prev ? : 2 1 3
2756 }
2757 8 push @out, $d;
2758 8 $prev = $v;
2759 }
2760 2 return @out;
2761 }
2762  
2763 9 my @nums = grep { defined } @$vals;
2764  
2765 9 ▶ if ($fn eq 'pct_of_total') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "pct_of_total") 2 7 9
2766 2 my $total = 0;
2767 2 $total += $_ for @nums;
2768 2 for my $v (@$vals) {
2769 5 ▶ push @out, ($total && defined $v) ? $v / $total : undef;
Branch: 1 branch
BranchTrueFalseTotal
$total && defined $v ? : 2 3 5
Condition100%3/3
$total and defined $v
!ll&&!rl&&r
212
MC/DC100%2/2
$total and defined $v
$total defined $v
Truth table100%3/3
A and B
A= $totalB= defined $v
ABresult
0X0
100
111
2770 }
2771 2 return @out;
2772 }
2773  
2774 7 ▶ if ($fn eq 'rank') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "rank") 3 4 7
2775 # SQL RANK(): ties share a rank and the next rank skips, which counting
2776 # the values strictly greater gives for free.
2777 3 for my $v (@$vals) {
2778 7 ▶ push @out, defined $v ? 1 + scalar(grep { $_ > $v } @nums) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 6 1 7
2779 }
2780 3 return @out;
2781 }
2782  
2783 4 ▶ if ($fn eq 'zscore') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "zscore") 3 1 4
2784 3 my ($mean, $sd);
2785 3 ▶ if (@nums >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nums >= 2) 2 1 3
2786 2 $mean = 0;
2787 2 $mean += $_ for @nums;
2788 2 $mean /= scalar @nums;
2789 2 $sd = _stdev_of(\@nums);
2790 }
2791 3 for my $v (@$vals) {
2792 5 ▶ push @out, (defined $v && defined $sd && $sd > 0) ? ($v - $mean) / $sd : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && defined $sd && $sd > 0 ? : 2 3 5
Condition83%5/6
defined $v and defined $sd and $sd > 0
!ll&&!rl&&r
122
defined $v and defined $sd
!ll&&!rl&&r
014
MC/DC66%2/3
defined $v and defined $sd and $sd > 0
defined $v defined $sd $sd > 0
Truth table75%3/4
A and B and C
A= defined $vB= defined $sdC= $sd > 0
ABCresult
0XX0
10X0
1100
1111
2793 }
2794 3 return @out;
2795 }
2796  
2797 1 croak "Unknown cross-row function '$fn'"; # unreachable: validated above
2798 }
2799  
2800 # _cross_row_cmp($x, $y) -> -1 | 0 | 1
2801 #
2802 # The order= comparator: numeric when both cells are numbers, case-sensitive
2803 # string otherwise. Numbers sort as numbers because an order= on an amount is
2804 # meant to put 100 after 20, and a mixed column still has to be total.
2805 8 ▶sub _cross_row_cmp {
Subroutine _cross_row_cmp: called CC=5 SCAR=16.1
2806 8 my ($x, $y) = @_;
2807 8 my ($nx, $ny) = (_numeric_cell($x), _numeric_cell($y));
2808 8 ▶ return $nx <=> $ny if defined $nx && defined $ny;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nx and defined $ny 3 5 8
Condition100%3/3
defined $nx and defined $ny
!ll&&!rl&&r
413
MC/DC100%2/2
defined $nx and defined $ny
defined $nx defined $ny
Truth table100%3/3
A and B
A= defined $nxB= defined $ny
ABresult
0X0
100
111
2809 5 ▶ return ($x // '') cmp($y // '');
Condition100%4/4
$x // ""
l!l
32
$y // ""
l!l
41
MC/DC100%1/1
$x // ""
$x
MC/DC100%1/1
$y // ""
$y
Truth table100%2/2
A
A= $x
Aresult
11
00
Truth table100%2/2
A
A= $y
Aresult
11
00
2810 }
2811  
2812 # _trim($text) -> $text
2813 20 ▶sub _trim {
Subroutine _trim: called CC=2 SCAR=6.9
2814 20 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 19 1 20
2815 20 $s =~ s/\A\s+//;
2816 20 $s =~ s/\s+\z//;
2817 20 return $s;
2818 }
2819  
2820 # _reduce_records($self, $records, $columns, $opts) -> ($records, $columns)
2821 #
2822 # Purpose: The reduce half of every data pipeline, in one place: the request's
2823 # filters -- the reader's own f= params, then the two bounds of the
2824 # relative time range -- run over $records in order, with the columns
2825 # list threaded through so a later stage can add columns and hand back
2826 # the extended list. Every route that reads rows and then filters
2827 # them calls this rather than writing the loop out again, so a stage
2828 # added here reaches all of them at once.
2829 # Entry: $records -- arrayref of hashrefs, after the calculated-field pass.
2830 # $columns -- arrayref of column names, or undef where the caller has
2831 # no list in scope; a stage that adds columns needs one,
2832 # so undef is only for the filters-only callers.
2833 # $opts -- optional hashref. chips_only => 1 says the caller's rows
2834 # have already been reduced and it is here only for the
2835 # chip list _apply_filters returns: the stage is skipped
2836 # and both arguments come back as they arrived.
2837 # no_cross_row => 1 runs the filters only, for a caller
2838 # that wants the rows without stage 6 (_apply_filters,
2839 # whose callers are after the chip list).
2840 # Exit: ($records, $columns), reduced -- or untouched, when chips_only is
2841 # set. A malformed f= spec leaves the records alone rather than
2842 # croaking, as _apply_filter_spec always has.
2843 # Notes: This is stage 5 of the stage order in BI-Phase-7.md section 3, and
2844 # it is where that document's stages 6 and 7 are meant to land: the
2845 # cross-row pass and the second calculated-field pass. Both belong
2846 # here rather than at a call site, because they have to see *filtered*
2847 # rows and have to run before pagination (an exported or second page
2848 # then carries the same numbers as the first).
2849 #
2850 # Three kinds of row-reading route deliberately do not reduce here,
2851 # and each is a different case: the canvas builds its own per-card
2852 # pipeline and runs stage 6 per card, after that card's filters
2853 # (_canvas_render_context, _render_visual_html), /view's table action
2854 # filters inside _fetch_page before it paginates, and /api/ask samples
2855 # rows without filtering them at all.
2856 #
2857 # chips_only exists for the routes that filter somewhere else and then
2858 # call _apply_filters for its chip list: /view, the three /open
2859 # branches and /join reduce inside _fetch_page, before they paginate,
2860 # and /combine reduces through this function before slicing its page.
2861 # Re-running the stage on that page would still give the right answer
2862 # today, because every stage in it is idempotent -- filtering a
2863 # filtered set changes nothing. It stops being harmless as soon as a
2864 # stage is not: a cross-row pass (running totals, differences) or a
2865 # time-fill that adds rows would double-apply to a page that has
2866 # already been through it, and would do so only on non-first pages.
2867 # So the option is not a micro-optimisation; it is what keeps those
2868 # six call sites correct once such a stage lands here.
2869 #
2870 # A plain function rather than a method, like _filter_specs and
2871 # _apply_filter_spec beside it: the tests drive the reduce stage with a
2872 # controller-shaped mock (build_controller), and only every_param and
2873 # stash are ever wanted of $self.
2874 843 ▶sub _reduce_records {
Subroutine _reduce_records: called CC=15 SCAR=28.0
2875 843 my ($self, $records, $columns, $opts) = @_;
2876  
2877 # Already reduced: hand the rows and the columns back exactly as they came,
2878 # ahead of the first stage, so a stage that is not idempotent cannot see
2879 # these rows a second time.
2880 843 ▶ return ($records, $columns) if $opts && $opts->{chips_only};
Branch: 1 branch
BranchTrueFalseTotal
if $opts and $opts->{"chips_only"} 204 639 843
Condition100%3/3
$opts and $opts->{"chips_only"}
!ll&&!rl&&r
6318204
MC/DC100%2/2
$opts and $opts->{"chips_only"}
$opts $opts->{"chips_only"}
Truth table100%3/3
A and B
A= $optsB= $opts->{"chips_only"}
ABresult
0X0
100
111
2881  
2882 # A filter on a column stage 6 or 7 will write has to wait for it: f=ytd:gt:50
2883 # selects rows by their running total, and must not change those totals. So
2884 # every filter that can run first does, and the rest run last.
2885 639 ▶ my $crossing = (!$opts || !$opts->{no_cross_row}) && length($self->param('cs') // '');
Condition87%7/8
!($opts && $opts->{'no_cross_row'}) && length($self->param('cs') // '')
!ll&&!rl&&r
861912
$opts && $opts->{'no_cross_row'}
!ll&&!rl&&r
63108
$self->param("cs") // ""
l!l
13618
MC/DC66%2/3
!($opts && $opts->{'no_cross_row'}) && length($self->param('cs') // '')
$opts $opts->{'no_cross_row'} $self->param("cs")
Truth table60%3/5
not(A && B) && C
A= $optsB= $opts->{'no_cross_row'}C= $self->param("cs")
ABCresult
11X0
0X00
1000
0X11
1011
2886 639 ▶ my %written = $crossing ? map { $_ => 1 } _cross_row_written($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
$crossing ? : 12 627 639
2887 639 ▶ if (defined(my $why = _unknown_filter_column($self, $columns, \%written))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $why = _unknown_filter_column($self, $columns, \%written))) 0 639 639
2888 0 croak $why;
2889 }
2890 639 my @late;
2891 639 for my $s (@{ _filter_specs($self) }) {
2892 36 ▶ if ($written{ (split /:/, $s, 2)[0] // '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written{(split(/:/u, $s, 2))[0] // ""}) 0 36 36
Condition50%1/2
(split(/:/u, $s, 2))[0] // ""
l!l
360
MC/DC0%0/1
(split(/:/u, $s, 2))[0] // ""
(split(/:/u, $s, 2))[0]
Truth table50%1/2
A
A= (split(/:/u, $s, 2))[0]
Aresult
11
00
2893 0 push @late, $s;
2894 0 next;
2895 }
2896 36 $records = _apply_filter_spec($records, $s);
2897 }
2898  
2899 # d=1 removes duplicate rows after the filters and before the cross-row
2900 # pass, the order _fetch_page uses for the table view, so a chart of a
2901 # de-duplicated view counts the rows the table shows. Only callers with a
2902 # columns list run the whole stage; the filters-only entry point does not.
2903 639 ▶ $records = _dedup_records($records, $columns)
Branch: 1 branch
BranchTrueFalseTotal
if $columns and _dedup_requested($self) 3 636 639
Condition100%3/3
$columns and _dedup_requested($self)
!ll&&!rl&&r
96273
MC/DC100%2/2
$columns and _dedup_requested($self)
$columns _dedup_requested($self)
Truth table100%3/3
A and B
A= $columnsB= _dedup_requested($self)
ABresult
0X0
100
111
2904 if $columns && _dedup_requested($self);
2905  
2906 # Stage 6: the cross-row pass, and only when the request asks for one. It
2907 # has to see filtered rows -- a window is over what the filters left -- and it
2908 # has to run before anything paginates, which is why the routes that slice
2909 # their own page call it again (once) inside _fetch_page.
2910 639 ▶ if ($crossing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crossing) 12 627 639
2911 12 ($records, $columns) = $self->_apply_cross_row($records, $columns);
2912 # Stage 7: the calculated fields stage 4 held back for these columns.
2913 0 ($records, $columns) = $self->_apply_held_calc_fields($records, $columns);
2914 0 $records = _apply_filter_spec($records, $_) for @late;
2915 }
2916  
2917 627 return ($records, $columns);
2918 }
2919  
2920 # _reduce_or_render($self, \$records, \$columns) -> $ok
2921 #
2922 # Purpose: Run _reduce_records and turn a cross-row spec that cannot be
2923 # honoured into the 400 it deserves, in the one shape every route
2924 # already uses for a calculated field that will not compile.
2925 # Entry: References to the route's own lexicals: on success they are updated
2926 # in place, so a call site is one line.
2927 # Exit: True when the records are ready. False after rendering a 400 -- the
2928 # caller returns straight away, because nothing has been produced.
2929 # Notes: A method, unlike _reduce_records itself, because the failure needs
2930 # a controller to render through. The error text is the i18n message
2931 # the pass croaked with, with the "at File line N" tail removed so a
2932 # reader sees the fault rather than the interpreter's location.
2933 626 ▶sub _reduce_or_render :Protected ($self, $recs_ref, $cols_ref) {
Subroutine _reduce_or_render: called CC=2 SCAR=6.9
2934 626 my @reduced = eval { _reduce_records($self, $$recs_ref, $$cols_ref) };
2935 626 ▶ if (@reduced) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reduced) 614 12 626
2936 614 ($$recs_ref, $$cols_ref) = @reduced;
2937 614 return 1;
2938 }
2939  
2940 12 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
2941 12 $self->render(text => $e, status => 400);
2942 # Recorded because the callers that answer a missing table with a 404 get
2943 # their rows back empty here, and the 400 has already been rendered.
2944 12 $self->stash('_reduce_error' => 1);
2945 12 return 0;
2946 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2947  
2948 # _apply_filters($self, $records, $opts) -> ($filtered_records, \@raw_specs, $json_string)
2949 #
2950 # Purpose: Read all "f=" query params, apply them in order via _apply_filter_spec,
2951 # and return: the filtered arrayref, the raw spec strings (for building
2952 # export URLs), and a script-safe JSON string for pre-populating the UI.
2953 # Entry: $records is an arrayref of hashrefs. $opts is passed straight to
2954 # _reduce_records: chips_only => 1 for the routes whose rows are
2955 # already reduced and which are here only for the chips.
2956 # Exit: Three-element list; never croaks. $json_string has '</' escaped to
2957 # '<\/' so it is safe to embed directly in a <script> block.
2958 #
2959 # Reduction: the original code had two sequential loops over @specs -- one to
2960 # apply filters, one to parse them for JSON. By Modus Ponens, _apply_filter_spec
2961 # returns $records unchanged for a malformed spec (same guard condition), so
2962 # skipping the call for malformed specs is equivalent. Merging into one loop
2963 # eliminates a full O(n) pass and the redundant split() on every spec.
2964 211 ▶sub _apply_filters :Protected ($self, $records, $opts = undef) {
Subroutine _apply_filters: called CC=10 SCAR=23.1
2965 211 ▶ my @specs = @{ $self->every_param('f') // [] };
Condition50%1/2
$self->every_param("f") // []
l!l
2110
MC/DC0%0/1
$self->every_param("f") // []
$self->every_param("f")
Truth table50%1/2
A
A= $self->every_param("f")
Aresult
11
00
2966 211 my @parsed;
2967 211 for my $s (@specs) {
2968 22 my ($col, $op, $val) = split /:/, $s, 3;
2969 22 ▶ next unless length($col // '') && defined $op && length $op;
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and defined $op and length $op 4 18 22
Condition87%7/8
length($col // "") and defined $op and length $op
!ll&&!rl&&r
4018
length($col // "") and defined $op
!ll&&!rl&&r
1318
$col // ""
l!l
211
MC/DC66%2/3
length($col // "") and defined $op and length $op
$col defined $op length $op
Truth table75%3/4
A and B and C
A= $colB= defined $opC= length $op
ABCresult
0XX0
10X0
1100
1111
2970 18 ▶ push @parsed, { col => $col, op => $op, val => $val // '' };
Condition50%1/2
$val // ""
l!l
180
MC/DC0%0/1
$val // ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
2971 }
2972  
2973 # The range joins the request's own filters here, but it is not one of them:
2974 # @specs -- which the page shows as chips, keeps in its panel forms and
2975 # offers to its filter UI -- stays the reader's own list, so a relative range
2976 # cannot be submitted as a frozen pair of dates and a reopened link cannot
2977 # narrow twice. The application itself is _reduce_records, so this entry
2978 # point and the routes that call it directly cannot drift apart; the columns
2979 # list is not in scope here and every caller holds its own, so none is
2980 # threaded through. $opts travels with the records for the same reason:
2981 # a caller that has already reduced asks for the chips alone here, and the
2982 # seam, not this wrapper, is where that is decided.
2983 # no_cross_row: this entry point is the chip list, and its callers either
2984 # skip the stage entirely (chips_only) or want the rows without stage 6.
2985 211 ▶ ($records) = _reduce_records($self, $records, undef, { %{ $opts // {} }, no_cross_row => 1 });
Condition100%2/2
$opts // {}
l!l
2038
MC/DC100%1/1
$opts // {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
2986  
2987 211 my $json = encode_json(\@parsed);
2988 211 $json =~ s{</}{<\\/}g;
2989 211 return ($records, \@specs, $json);
2990 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2991  
2992 # _filter_specs($self) -> \@specs
2993 #
2994 # Purpose: Every filter that applies to this request: the f= params the reader
2995 # wrote, followed by the two bounds of the relative time range the
2996 # request names, if any. This is the list the data pipelines fold over
2997 # -- charts, pivots, stats, insights and the exports as well as the
2998 # table view -- so a range narrows all of them through one call site
2999 # each, and a route that never heard of ranges still honours one.
3000 # Entry: $self -- the controller; it needs only every_param() and stash().
3001 # Exit: Returns an arrayref of "col:op:val" strings; possibly empty.
3002 1276 ▶sub _filter_specs {
Subroutine _filter_specs: called CC=3 SCAR=11.0
3003 1276 my ($self) = @_;
3004 return [
3005 1276 ▶ @{ $self->every_param('f') // [] },
Condition50%1/2
$self->every_param("f") // []
l!l
12760
MC/DC0%0/1
$self->every_param("f") // []
$self->every_param("f")
Truth table50%1/2
A
A= $self->every_param("f")
Aresult
11
00
3006 1276 ▶ @{ $self->stash('range_specs') // [] },
Condition100%2/2
$self->stash("range_specs") // []
l!l
126214
MC/DC100%1/1
$self->stash("range_specs") // []
$self->stash("range_specs")
Truth table100%2/2
A
A= $self->stash("range_specs")
Aresult
11
00
3007 ];
3008 }
3009  
3010 # _dedup_requested($self) -> $bool
3011 #
3012 # Purpose: True when the request asks for duplicate rows to be removed (d=1).
3013 # The SQL aggregate push paths check it too, because the backend
3014 # would count the duplicates the Perl fold has already dropped.
3015 1100 ▶sub _dedup_requested {
Subroutine _dedup_requested: called CC=2 SCAR=6.9
3016 1100 my ($self) = @_;
3017 1100 ▶ return $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 4 1096 1100
3018 }
3019  
3020 # _dedup_records(\@records, \@columns) -> \@unique_records
3021 #
3022 # Purpose: Remove duplicate rows from $records. Two rows are duplicates when
3023 # every column value is identical (string comparison, undef treated as '').
3024 # Preserves the order of first occurrence.
3025 # Entry: $records is an arrayref of hashrefs; $columns is an arrayref of names.
3026 # Exit: Returns a new arrayref; the input is not modified.
3027 10 ▶sub _dedup_records {
Subroutine _dedup_records: called CC=4 SCAR=13.9
3028 10 my ($records, $columns) = @_;
3029 10 my (%seen, @out);
3030 10 for my $row (@$records) {
3031 34 ▶ my $key = join "\x00", map { $row->{$_} // '' } @$columns;
Condition100%2/2
$row->{$_} // ""
l!l
791
MC/DC100%1/1
$row->{$_} // ""
$row->{$_}
Truth table100%2/2
A
A= $row->{$_}
Aresult
11
00
3032 34 ▶ push @out, $row unless $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$key}++ 20 14 34
3033 }
3034 10 return \@out;
3035 }
3036  
3037 # _filter_specs_to_criteria( \@specs, $source ) -> ( \%criteria, \@residual_specs )
3038 #
3039 # Purpose: Map the f= filter vocabulary onto Database::Abstraction / Database::Join
3040 # criteria hashrefs so the backend can apply pushable filters before
3041 # paginating. Specs that cannot be safely pushed are returned as
3042 # @residual_specs for subsequent in-memory application.
3043 #
3044 # Pushdown is only performed when $source->dbi_source() confirms a SQL (SQLite)
3045 # backend. For CSV (slurp path), in-memory, XLSX, BerkeleyDB and URL sources
3046 # everything is placed in @residual_specs so the existing _apply_filter_spec
3047 # Perl path handles them.
3048 #
3049 # Pushable mapping (SQLite only):
3050 # eq / ne -- LIKE / NOT LIKE without wildcards. SQLite LIKE is
3051 # case-insensitive for ASCII, matching the existing Perl
3052 # lc() semantics. Values containing % or _ are pushed to
3053 # residual to avoid LIKE metacharacter conflicts.
3054 # contains -- LIKE '%val%'. Residual when value contains % or _.
3055 # starts -- LIKE 'val%'. Residual when value contains % or _.
3056 # lt / le / gt / ge -- SQL numeric comparison. Residual when the value is
3057 # not a plain number (avoids column-affinity surprises
3058 # with currency or comma-formatted values).
3059 # in -- Always residual (a list of values, matched as eq is).
3060 # empty / notempty -- Always residual. Must match both SQL NULL and the
3061 # literal empty string; cannot be expressed as a single
3062 # D::A criterion without an -or grouping.
3063 #
3064 # Column collision: if a column already has a pushable criterion, the second
3065 # spec for that column is placed in @residual_specs. This keeps the AND logic
3066 # simple and safe; callers with multiple conditions on one column (e.g. a range)
3067 # still get correct results via the in-memory fallback.
3068 35 ▶sub _filter_specs_to_criteria {
Subroutine _filter_specs_to_criteria: called CC=28 SCAR=33.3
3069 35 my ($specs, $source) = @_;
3070  
3071 # Only push when the source has a live SQL (SQLite) backend.
3072 35 ▶ my $is_sql = $source
Condition100%6/6
$source && $source->can('dbi_source') && $source->dbi_source
!ll&&!rl&&r
21023
$source and $source->can("dbi_source")
!ll&&!rl&&r
1133
MC/DC100%3/3
$source && $source->can('dbi_source') && $source->dbi_source
$source $source->can("dbi_source") $source->dbi_source
Truth table100%4/4
A and B && C
A= $sourceB= $source->can("dbi_source")C= $source->dbi_source
ABCresult
0XX0
10X0
1100
1111
3073 && $source->can('dbi_source')
3074 && $source->dbi_source();
3075 35 ▶ return ({}, [@$specs]) unless $is_sql;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_sql 12 23 35
3076  
3077 23 require Scalar::Util;
3078 23 my (%criteria, @residual);
3079  
3080 23 for my $spec (@$specs) {
3081 27 my ($col, $op, $val) = split /:/, $spec, 3;
3082 27 ▶ next unless length($col // '') && defined $op && length $op;
Branch: 1 branch
BranchTrueFalseTotal
unless length($col // "") and defined $op and length $op 4 23 27
Condition100%8/8
length($col // "") and defined $op and length $op
!ll&&!rl&&r
3123
length($col // "") and defined $op
!ll&&!rl&&r
2124
$col // ""
l!l
261
MC/DC100%3/3
length($col // "") and defined $op and length $op
$col defined $op length $op
Truth table100%4/4
A and B and C
A= $colB= defined $opC= length $op
ABCresult
0XX0
10X0
1100
1111
3083 23 ▶ $val //= '';
Condition100%2/2
$val //= ""
l!l
212
MC/DC100%1/1
$val //= ""
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
3084  
3085 # A second spec on the same column goes to residual.
3086 23 ▶ if (exists $criteria{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $criteria{$col}) 1 22 23
3087 1 push @residual, $spec;
3088 1 next;
3089 }
3090  
3091 22 ▶ if ($op eq 'eq' || $op eq 'ne') {
Branch: 7 branches
BranchTrueFalseTotal
if ($op eq 'eq' or $op eq 'ne') { } 5 17 22
elsif ($op eq 'contains') { } 2 15 17
elsif ($op eq 'starts') { } 2 13 15
elsif ($op eq 'lt') { } 2 11 13
elsif ($op eq 'le') { } 2 9 11
elsif ($op eq 'gt') { } 3 6 9
elsif ($op eq 'ge') { } 2 4 6
Condition100%3/3
$op eq "eq" or $op eq "ne"
l!l&&r!l&&!r
3217
MC/DC100%2/2
$op eq "eq" or $op eq "ne"
$op eq "eq" $op eq "ne"
Truth table100%3/3
A or B
A= $op eq "eq"B= $op eq "ne"
ABresult
1X1
011
000
3092 # A date compares by the day in Perl (a timestamp cell is on the
3093 # day it names); LIKE would want the cell to be the date exactly.
3094 5 ▶ if ($val =~ /[%_]/ || _is_iso_date($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[%_]/u or _is_iso_date($val)) { } 3 2 5
Condition66%2/3
$val =~ /[%_]/u or _is_iso_date($val)
l!l&&r!l&&!r
302
MC/DC50%1/2
$val =~ /[%_]/u or _is_iso_date($val)
$val =~ /[%_]/u _is_iso_date($val)
Truth table66%2/3
A or B
A= $val =~ /[%_]/uB= _is_iso_date($val)
ABresult
1X1
011
000
3095 3 push @residual, $spec;
3096 } else {
3097 2 ▶ $criteria{$col} = ($op eq 'eq')
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'eq' ? : 1 1 2
3098 ? { -like => $val }
3099 : { -not_like => $val };
3100 }
3101 } elsif ($op eq 'contains') {
3102 2 ▶ if ($val =~ /[%_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[%_]/u) { } 1 1 2
3103 1 push @residual, $spec;
3104 } else {
3105 1 $criteria{$col} = { -like => "%${val}%" };
3106 }
3107 } elsif ($op eq 'starts') {
3108 2 ▶ if ($val =~ /[%_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[%_]/u) { } 1 1 2
3109 1 push @residual, $spec;
3110 } else {
3111 1 $criteria{$col} = { -like => "${val}%" };
3112 }
3113 } elsif ($op eq 'lt') {
3114 Scalar::Util::looks_like_number($val)
3115 2 ▶ ? ($criteria{$col} = { '<' => $val })
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($val) ? : 1 1 2
3116 : push @residual, $spec;
3117 } elsif ($op eq 'le') {
3118 Scalar::Util::looks_like_number($val)
3119 2 ▶ ? ($criteria{$col} = { '<=' => $val })
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($val) ? : 1 1 2
3120 : push @residual, $spec;
3121 } elsif ($op eq 'gt') {
3122 Scalar::Util::looks_like_number($val)
3123 3 ▶ ? ($criteria{$col} = { '>' => $val })
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($val) ? : 2 1 3
3124 : push @residual, $spec;
3125 } elsif ($op eq 'ge') {
3126 Scalar::Util::looks_like_number($val)
3127 2 ▶ ? ($criteria{$col} = { '>=' => $val })
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($val) ? : 1 1 2
3128 : push @residual, $spec;
3129 } else {
3130 # empty, notempty, between, unknown operator: always residual.
3131 4 push @residual, $spec;
3132 }
3133 }
3134  
3135 23 return (\%criteria, \@residual);
3136 }
3137  
3138 # _measure($fn, $col, $as) -> \%measure
3139 #
3140 # Build one measure descriptor in the vocabulary shared by Database::Abstraction
3141 # and Database::Join (sum|avg|min|max|count|count_distinct). $col is the
3142 # source column name, or undef for COUNT(*). $as is the result-row key.
3143 # The returned hashref is consumed directly by aggregate(measures => [...]).
3144 588 ▶sub _measure {
Subroutine _measure: called CC=2 SCAR=6.9
3145 588 my ($fn, $col, $as) = @_;
3146 588 ▶ return { fn => $fn, (defined $col ? (col => $col) : ()), as => $as };
Branch: 1 branch
BranchTrueFalseTotal
defined $col ? : 240 348 588
3147 }
3148  
3149 # _numeric_cell($v) -> $number | undef
3150 #
3151 # Extract a numeric value from a raw cell, handling currency prefixes
3152 # (e.g. $1,234.56) and accounting-notation negatives ((1,234.56) -> -1234.56).
3153 # Returns undef for empty, undef, or non-numeric input.
3154 # Used by the Perl fold path of _grouped_totals so all four chart routes
3155 # share one extraction function.
3156 3070 ▶sub _numeric_cell {
Subroutine _numeric_cell: called CC=1 SCAR=0.0
3157 3070 return Database::BI::Util::parse_number($_[0]);
3158 }
3159  
3160 # _percentile_of(\@sorted, $level) -> $number | undef
3161 #
3162 # The value at the given percentile of an already sorted list of numbers, by
3163 # linear interpolation between closest ranks (R's type 7, Excel's
3164 # PERCENTILE.INC). $level is 1..100. The empty list has no percentile, so it
3165 # returns undef rather than a made-up 0.
3166 #
3167 # At level 50 this is the median the column-stats view has always shown: the
3168 # middle value when the count is odd, the mean of the two middle values when it
3169 # is even. That is the point of it, and the reason median is defined in terms
3170 # of it below -- one arithmetic, so a p50 measure, a median measure and the
3171 # median column of the stats page cannot disagree.
3172 191 ▶sub _percentile_of {
Subroutine _percentile_of: called CC=4 SCAR=13.9
3173 191 my ($sorted, $level) = @_;
3174 191 my $n = scalar @$sorted;
3175 191 ▶ return undef unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 190 191
3176 190 my $h = ($n - 1) * $level / 100;
3177 190 my $lo = int $h;
3178 190 ▶ return $sorted->[$lo] if $h == $lo || $lo >= $n - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $h == $lo or $lo >= $n - 1 11 179 190
Condition66%2/3
$h == $lo or $lo >= $n - 1
l!l&&r!l&&!r
110179
MC/DC50%1/2
$h == $lo or $lo >= $n - 1
$h == $lo $lo >= $n - 1
Truth table66%2/3
A or B
A= $h == $loB= $lo >= $n - 1
ABresult
1X1
011
000
3179 179 return $sorted->[$lo] + ($h - $lo) * ($sorted->[ $lo + 1 ] - $sorted->[$lo]);
3180 }
3181  
3182 # _median_of(\@sorted) -> $number | undef
3183 89 ▶sub _median_of {
Subroutine _median_of: called CC=1 SCAR=0.0
3184 89 return _percentile_of($_[0], 50);
3185 }
3186  
3187 # _stdev_of(\@nums) -> $number | undef
3188 #
3189 # Sample standard deviation, the n-1 denominator: what Splunk's stdev() and
3190 # Excel's STDEV.S report. One value has no spread to measure, so fewer than
3191 # two values give undef rather than a reassuring 0.
3192 88 ▶sub _stdev_of {
Subroutine _stdev_of: called CC=4 SCAR=13.9
3193 88 my $nums = shift;
3194 88 my $n = scalar @$nums;
3195 88 ▶ return undef unless $n >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $n >= 2 2 86 88
3196 86 my $mean = 0;
3197 86 $mean += $_ for @$nums;
3198 86 $mean /= $n;
3199 86 my $sum_sq = 0;
3200 86 $sum_sq += ($_ - $mean) ** 2 for @$nums;
3201 86 return sqrt($sum_sq / ($n - 1));
3202 }
3203  
3204 # _range_of(\@sorted) -> $number | undef
3205 #
3206 # Largest minus smallest, on the already sorted list: the spread a reader means
3207 # by "range", which is not min/max in the sense the SQL backend could push.
3208 85 ▶sub _range_of {
Subroutine _range_of: called CC=2 SCAR=6.9
3209 85 my $sorted = shift;
3210 85 ▶ return undef unless @$sorted;
Branch: 1 branch
BranchTrueFalseTotal
unless @$sorted 1 84 85
3211 84 return $sorted->[-1] - $sorted->[0];
3212 }
3213  
3214 # _measure_value($fn, \@sorted_values) -> $number | undef
3215 #
3216 # The function from @PERL_MEASURE_FNS (or a percentile) applied to the sorted
3217 # values of one group. It takes the values rather than a group key so that the
3218 # caller decides what a group is: the pivot's fold has one list per group, and
3219 # the stats page measures a whole column with the same four functions.
3220 6 ▶sub _measure_value {
Subroutine _measure_value: called CC=5 SCAR=16.1
3221 6 my ($fn, $sorted) = @_;
3222 6 my $level = _measure_percentile($fn);
3223 6 ▶ return _percentile_of($sorted, $level) if defined $level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level 1 5 6
3224 5 ▶ return _median_of($sorted) if $fn eq 'median';
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "median" 1 4 5
3225 4 ▶ return _stdev_of($sorted) if $fn eq 'stdev';
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "stdev" 1 3 4
3226 3 ▶ return _range_of($sorted) if $fn eq 'range';
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "range" 1 2 3
3227 2 return undef;
3228 }
3229  
3230 # _column_is_numeric($records, $col) -> bool
3231 #
3232 # Says whether a column can be used as a chart measure. It samples the first
3233 # non-empty cells rather than the whole table: the canvas needs a quick answer
3234 # for the column picker, and a column with a non-numeric value in its first 20
3235 # rows is not a safe measure anyway.
3236 108 ▶sub _column_is_numeric {
Subroutine _column_is_numeric: called CC=8 SCAR=20.8
3237 108 my ($records, $col) = @_;
3238 108 my $seen = 0;
3239 108 ▶ for my $row (@{ $records // [] }) {
Condition100%2/2
$records // []
l!l
1071
MC/DC100%1/1
$records // []
$records
Truth table100%2/2
A
A= $records
Aresult
11
00
3240 296 my $v = $row->{$col};
3241 296 ▶ next unless defined $v && length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 5 291 296
Condition100%3/3
defined $v and length $v
!ll&&!rl&&r
32291
MC/DC100%2/2
defined $v and length $v
defined $v length $v
Truth table100%3/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
3242 291 ▶ return 0 unless defined _numeric_cell($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined _numeric_cell($v) 68 223 291
3243 223 ▶ last if ++$seen >= 20;
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen >= 20 1 222 223
3244 }
3245 40 ▶ return $seen ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seen ? : 37 3 40
3246 }
3247  
3248 # _measure_pushable($source, $fn, $col, \@records) -> bool
3249 #
3250 # Returns true when the measure can be computed by aggregate() on $source and
3251 # the result will be identical to the Perl fold. False means use the Perl fold.
3252 #
3253 # Decision table:
3254 # Not in the pushed vocabulary -- never pushable. None of median, a
3255 # percentile, stdev, first, last or range is a sum
3256 # of its parts, and the backend would either refuse
3257 # the spec or quietly answer with its own idea of
3258 # the statistic (it has one for median and stddev).
3259 # count / count_distinct -- always pushable (no numeric coercion).
3260 # sum / avg / min / max -- only when every sampled value in $col is either
3261 # a plain number (looks_like_number) or empty/undef.
3262 # Currency symbols or accounting parentheses in any
3263 # value force the Perl fold, which strips them
3264 # correctly; SQLite CAST would coerce them to 0.
3265 # Missing $source -- never pushable.
3266 # Missing $col -- sum/avg/min/max are not pushable without a column.
3267 464 ▶sub _measure_pushable {
Subroutine _measure_pushable: called CC=13 SCAR=25.9
3268 464 my ($source, $fn, $col, $records) = @_;
3269 464 ▶ return 0 unless $source && $source->can('aggregate');
Branch: 1 branch
BranchTrueFalseTotal
unless $source and $source->can("aggregate") 1 463 464
Condition66%2/3
$source and $source->can("aggregate")
!ll&&!rl&&r
01463
MC/DC50%1/2
$source and $source->can("aggregate")
$source $source->can("aggregate")
Truth table66%2/3
A and B
A= $sourceB= $source->can("aggregate")
ABresult
0X0
100
111
3270 463 ▶ return 0 unless defined $fn && $PUSHED_MEASURE_FN_OK{$fn};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and $PUSHED_MEASURE_FN_OK{$fn} 6 457 463
Condition66%2/3
defined $fn and $PUSHED_MEASURE_FN_OK{$fn}
!ll&&!rl&&r
06457
MC/DC50%1/2
defined $fn and $PUSHED_MEASURE_FN_OK{$fn}
defined $fn $PUSHED_MEASURE_FN_OK{$fn}
Truth table66%2/3
A and B
A= defined $fnB= $PUSHED_MEASURE_FN_OK{$fn}
ABresult
0X0
100
111
3271 457 ▶ return 1 if $fn eq 'count' || $fn eq 'count_distinct';
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "count" or $fn eq "count_distinct" 348 109 457
Condition100%3/3
$fn eq "count" or $fn eq "count_distinct"
l!l&&r!l&&!r
3471109
MC/DC100%2/2
$fn eq "count" or $fn eq "count_distinct"
$fn eq "count" $fn eq "count_distinct"
Truth table100%3/3
A or B
A= $fn eq "count"B= $fn eq "count_distinct"
ABresult
1X1
011
000
3272 109 ▶ return 0 unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 109 109
3273 # For sum/avg/min/max, only push when at least one value is a plain number
3274 # and every non-null/non-blank value passes looks_like_number.
3275 #
3276 # Two cases where both paths must agree:
3277 # - All values undef/blank: Perl fold produces no rows; SQL SUM(NULL)
3278 # produces NULL, normalised to 0 by D::A -> discrepancy. Return 0 so
3279 # the Perl fold runs and yields the expected "No plottable data" result.
3280 # - Currency/accounting notation (e.g. ($1,234)): _numeric_cell strips
3281 # these correctly; SQL sees non-numeric text -> SUM = NULL -> 0. Return 0
3282 # so _numeric_cell is used on every value.
3283 109 require Scalar::Util;
3284 109 my $found = 0;
3285 109 ▶ for my $row (@{ $records // [] }) {
Condition50%1/2
$records // []
l!l
1090
MC/DC0%0/1
$records // []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
3286 587 my $v = $row->{$col};
3287 587 ▶ next unless defined $v && length($v); # undef/blank: same effect as NULL
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 2 585 587
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
20585
MC/DC50%1/2
defined $v and length $v
defined $v length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
3288 585 ▶ return 0 unless Scalar::Util::looks_like_number($v);
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($v) 7 578 585
3289 578 $found = 1;
3290 }
3291 102 return $found; # 0 (false) when every sampled value was undef or blank
3292 }
3293  
3294 # _parse_measure_param($spec_str) -> \%measure | undef
3295 #
3296 # Parse 'col:fn:as' into { col => ..., fn => ..., as => ... }.
3297 # Shared dialect for /pivot so it does not invent a second format.
3298 # $fn is lower-cased first, so SUM and P90 are the same measures as sum and
3299 # p90. Returns undef for an invalid or unrecognised spec.
3300 22 ▶sub _parse_measure_param {
Subroutine _parse_measure_param: called CC=8 SCAR=21.4
3301 22 my $str = shift;
3302 22 ▶ return undef unless defined $str && length($str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length $str 0 22 22
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
3303 22 my ($col, $fn, $as) = split /:/, $str, 3;
3304 22 ▶ return undef unless defined $fn && defined $as && length($fn) && length($as);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and defined $as and length $fn and length $as 2 20 22
Condition77%7/9
defined $fn and defined $as and length $fn and length $as
!ll&&!rl&&r
2020
defined $fn and defined $as and length $fn
!ll&&!rl&&r
1120
defined $fn and defined $as
!ll&&!rl&&r
0121
MC/DC50%2/4
defined $fn and defined $as and length $fn and length $as
defined $fn defined $as length $fn length $as
Truth table60%3/5
A and B and C and D
A= defined $fnB= defined $asC= length $fnD= length $as
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3305 20 $fn = lc $fn;
3306 20 ▶ return undef unless _measure_fn_ok($fn);
Branch: 1 branch
BranchTrueFalseTotal
unless _measure_fn_ok($fn) 5 15 20
3307 15 return { col => $col, fn => $fn, as => $as };
3308 }
3309  
3310 # _grouped_totals($self, \%pipeline, \%spec) -> \@rows
3311 #
3312 # Single GROUP BY entry point for all four chart routes. Returns an arrayref
3313 # of hashrefs, each containing the 'by' column values and the aggregate result
3314 # under $spec->{as}.
3315 #
3316 # $pipeline = { source => $src_or_undef, records => \@rows }
3317 # source -- a DataSource or Database::Join for the push path; undef when
3318 # the pipeline is a /combine union (must fold in Perl).
3319 # records -- the already-fetched rows (pre-filtered by the caller).
3320 #
3321 # $spec = { by => [...], fn => ..., val => ..., as => ..., [sort_by, having, limit, offset] }
3322 #
3323 # Dispatch:
3324 # Push path -- $source is defined, measure is pushable (checked by
3325 # _measure_pushable), and either no active f= filters or all
3326 # active filters are SQL-pushable (via _filter_specs_to_criteria).
3327 # Calls $source->aggregate() with pushable criteria when present.
3328 # Perl fold -- Iterates $pipeline->{records} (pre-filtered by the caller)
3329 # when the push path is unavailable (combine union, non-SQL
3330 # source, non-pushable measure, or residual filters that cannot
3331 # be expressed as SQL criteria).
3332 #
3333 # $ENV{BI_FORCE_PERL_AGG}: set to a true value in tests to force the Perl fold
3334 # regardless of source type, enabling cross-backend equivalence assertions.
3335 394 ▶sub _grouped_totals :Protected ($self, $pipeline, $spec) {
Subroutine _grouped_totals: called CC=79 SCAR=62.9
3336 394 my $source = $pipeline->{source};
3337 394 ▶ my $records = $pipeline->{records} // [];
Condition50%1/2
$pipeline->{"records"} // []
l!l
3940
MC/DC0%0/1
$pipeline->{"records"} // []
$pipeline->{"records"}
Truth table50%1/2
A
A= $pipeline->{"records"}
Aresult
11
00
3338 394 ▶ my $by = $spec->{by} // [];
Condition50%1/2
$spec->{"by"} // []
l!l
3940
MC/DC0%0/1
$spec->{"by"} // []
$spec->{"by"}
Truth table50%1/2
A
A= $spec->{"by"}
Aresult
11
00
3339 394 ▶ my $fn = lc($spec->{fn} // 'count');
Condition50%1/2
$spec->{"fn"} // "count"
l!l
3940
MC/DC0%0/1
$spec->{"fn"} // "count"
$spec->{"fn"}
Truth table50%1/2
A
A= $spec->{"fn"}
Aresult
11
00
3340 394 my $val_col = $spec->{val};
3341 394 ▶ my $as = $spec->{as} // 'value';
Condition50%1/2
$spec->{"as"} // "value"
l!l
3940
MC/DC0%0/1
$spec->{"as"} // "value"
$spec->{"as"}
Truth table50%1/2
A
A= $spec->{"as"}
Aresult
11
00
3342  
3343 # Push path: use backend aggregate() when safe.
3344 # When f= filters are active, _filter_specs_to_criteria translates as many
3345 # specs as possible to SQL criteria. The push path is used only when ALL
3346 # specs are SQL-pushable (no residual); otherwise the Perl fold handles the
3347 # pre-filtered records the caller already passed in.
3348 394 my @f_specs = grep { /\S/ } @{ _filter_specs($self) };
3349 394 my $has_filter = !!@f_specs;
3350  
3351 # SQL::Statement 1.414 (the SQL engine under DBD::CSV) uses
3352 # "use warnings FATAL => 'all'" and calls join("\0", @row[@keycols])
3353 # inside getAffectedResult without guarding against undef. When a GROUP BY
3354 # column contains blank CSV cells (empty_is_undef => 1 converts them to
3355 # undef), the join triggers a fatal "uninitialized value" exception and the
3356 # execute() call dies. The correct fix is to use
3357 # map { $_ // '' } @row[@keycols]
3358 # in SQL::Statement::getAffectedResult, but the module has been unmaintained
3359 # since 2020 (21 open RT tickets as of 2026-10).
3360 # See: https://rt.cpan.org/Ticket/Display.html?id=181946
3361 # Skip the push path whenever any 'by' column has a blank or undef value so
3362 # DBD::CSV/SQL::Statement is never asked to GROUP BY a null key.
3363 # TODO: remove this guard once SQL-Statement RT#181946 is fixed (last checked: 1.414, still the latest release, 2026-11-06).
3364 394 ▶ my $has_null_by_key = @$by && do {
Condition66%2/3
@$by && do { my $found; KEY_SCAN: foreach my $row (@$records) { foreach my $col (@$by) { my $v = $row->{$col}; unless (defined $v and length $v) { $found = 1; last KEY_SCAN; } ; } ; } ; $found }
!ll&&!rl&&r
13930
MC/DC0%0/2
@$by && do { my $found; KEY_SCAN: foreach my $row (@$records) { foreach my $col (@$by) { my $v = $row->{$col}; unless (defined $v and length $v) { $found = 1; last KEY_SCAN; } ; } ; } ; $found }
@$by do { my $found; KEY_SCAN: foreach my $row (@$records) { foreach my $col (@$by) { my $v = $row->{$col}; unless (defined $v and length $v) { $found = 1; last KEY_SCAN; } ; } ; } ; $found }
Truth table66%2/3
A && B
A= @$byB= do { my $found; KEY_SCAN: foreach my $row (@$records) { foreach my $col (@$by) { my $v = $row->{$col}; unless (defined $v and length $v) { $found = 1; last KEY_SCAN; } ; } ; } ; $found }
ABresult
0X0
100
111
3365 393 my $found;
3366 393 KEY_SCAN: for my $row (@$records) {
3367 2295 for my $col (@$by) {
3368 2541 my $v = $row->{$col};
3369 0 ▶ if (!defined($v) || !length($v)) { $found = 1; last KEY_SCAN; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v and length $v) 0 2541 2541
Condition33%1/3
defined $v and length $v
!ll&&!rl&&r
002541
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table33%1/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
3370 }
3371 }
3372 393 $found;
3373 };
3374  
3375 # When filters are active, translate them to SQL criteria for the push path.
3376 # If any spec has no SQL equivalent (@residual is non-empty), the push path
3377 # is skipped and the Perl fold uses the already-filtered $records.
3378 394 ▶ my ($push_crit, $push_residual) = ($has_filter && $source)
Branch: 1 branch
BranchTrueFalseTotal
$has_filter && $source ? : 9 385 394
Condition66%2/3
$has_filter and $source
!ll&&!rl&&r
38509
MC/DC50%1/2
$has_filter and $source
$has_filter $source
Truth table66%2/3
A and B
A= $has_filterB= $source
ABresult
0X0
100
111
3379 ? _filter_specs_to_criteria(\@f_specs, $source)
3380 : ({}, []);
3381 394 ▶ my $can_push_filters = !$has_filter || (!@$push_residual && %$push_crit);
Condition50%3/6
!$has_filter || !@$push_residual && %$push_crit
l!l&&r!l&&!r
38509
not @$push_residual and %$push_crit
!ll&&!rl&&r
900
MC/DC33%1/3
!$has_filter || !@$push_residual && %$push_crit
!$has_filter not @$push_residual %$push_crit
Truth table50%2/4
not(A) || not(B) and C
A= !$has_filterB= not @$push_residualC= %$push_crit
ABCresult
1XX1
0111
00X0
0100
3382  
3383 # Skip the push path when any 'by' or 'val' column is virtual (added by
3384 # _apply_calc_fields or named-group loader). Virtual columns do not exist in
3385 # the backend and would cause a "No such column" SQL error before the fold fallback.
3386 394 ▶ my $virt = $self->stash('_virtual_columns') // {};
Condition50%1/2
$self->stash("_virtual_columns") // {}
l!l
0394
MC/DC0%0/1
$self->stash("_virtual_columns") // {}
$self->stash("_virtual_columns")
Truth table50%1/2
A
A= $self->stash("_virtual_columns")
Aresult
11
00
3387 482 ▶ my $has_virtual = grep { $virt->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
defined $val_col ? : 38 356 394
3388 394 ▶ (@$by, (defined $val_col ? $val_col : ()), map { (split /:/, $_, 2)[0] // () } @f_specs);
Condition33%1/3
(split(/:/u, $_, 2))[0] // ()
l!l&&r!l&&!r
900
MC/DC0%0/2
(split(/:/u, $_, 2))[0] // ()
(split(/:/u, $_, 2))[0] ()
Truth table33%1/3
A // B
A= (split(/:/u, $_, 2))[0]B= ()
ABresult
1X1
011
000
3389  
3390 394 ▶ unless ($ENV{BI_FORCE_PERL_AGG} || _dedup_requested($self) || $has_virtual || !$can_push_filters || $has_null_by_key || !$source
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters or $has_null_by_key or not $source or not _measure_pushable($source, $fn, $val_col, $records)) 376 18 394
Condition83%15/18
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters or $has_null_by_key or not $source or not _measure_pushable($source, $fn, $val_col, $records)
l!l&&r!l&&!r
144376
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters or $has_null_by_key or not $source
l!l&&r!l&&!r
104380
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters or $has_null_by_key
l!l&&r!l&&!r
100384
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters
l!l&&r!l&&!r
19384
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual
l!l&&r!l&&!r
10393
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self)
l!l&&r!l&&!r
10393
MC/DC57%4/7
$ENV{"BI_FORCE_PERL_AGG"} or _dedup_requested($self) or $has_virtual or not $can_push_filters or $has_null_by_key or not $source or not _measure_pushable($source, $fn, $val_col, $records)
$ENV{"BI_FORCE_PERL_AGG"} _dedup_requested($self) $has_virtual not $can_push_filters $has_null_by_key not $source not _measure_pushable($source, $fn, $val_col, $records)
Truth table62%5/8
A or B or C or not(D) or E or not(F) or not(G)
A= $ENV{"BI_FORCE_PERL_AGG"}B= _dedup_requested($self)C= $has_virtualD= not $can_push_filtersE= $has_null_by_keyF= not $sourceG= not _measure_pushable($source, $fn, $val_col, $records)
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
3391 || !_measure_pushable($source, $fn, $val_col, $records)) {
3392 376 my $m = _measure($fn, $val_col, $as);
3393 376 my $agg_rows = eval {
3394 $source->aggregate(
3395 by => $by,
3396 measures => [$m],
3397 ($has_filter && %$push_crit ? (%$push_crit) : ()),
3398 ($spec->{sort_by} ? (sort_by => $spec->{sort_by}) : ()),
3399 ($spec->{having} ? (having => $spec->{having}) : ()),
3400 ($spec->{limit} ? (limit => $spec->{limit}) : ()),
3401 376 ▶ ($spec->{offset} ? (offset => $spec->{offset}) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$has_filter && %$push_crit ? : 0 376 376
$spec->{'sort_by'} ? : 0 376 376
$spec->{'having'} ? : 0 376 376
$spec->{'limit'} ? : 0 376 376
$spec->{'offset'} ? : 0 376 376
Condition33%1/3
$has_filter and %$push_crit
!ll&&!rl&&r
37600
MC/DC0%0/2
$has_filter and %$push_crit
$has_filter %$push_crit
Truth table33%1/3
A and B
A= $has_filterB= %$push_crit
ABresult
0X0
100
111
3402 )
3403 };
3404 376 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 376 0 376
3405 376 ▶ return $agg_rows // [];
Condition50%1/2
$agg_rows // []
l!l
3760
MC/DC0%0/1
$agg_rows // []
$agg_rows
Truth table50%1/2
A
A= $agg_rows
Aresult
11
00
3406 }
3407 0 $self->app->log->debug("aggregate() push failed: $@; falling back to Perl fold");
3408 }
3409  
3410 # Perl fold over pre-filtered records.
3411 #
3412 # The fold keeps a running total for the functions that are sums of their
3413 # parts (sum, avg, min, max) and the values themselves for the ones that are
3414 # not (median, a percentile, stdev, range), which are resolved once the group
3415 # is complete. first and last are order-dependent, so they are taken as the
3416 # rows arrive and need no list at all.
3417 18 my (%seen_warned, %seen_key, @key_order);
3418 18 my (%totals, %count_map, %distinct_vals, %values);
3419  
3420 18 for my $row (@$records) {
3421 # Skip rows where any 'by' column is empty/undef (mirrors SQL GROUP BY NULL exclusion).
3422 # Note: calc field columns store '(unparseable)' (not undef) for failed expressions,
3423 # so those rows pass this guard and appear in the (unparseable) group instead.
3424 75 ▶ my @key_parts = map { $row->{$_} // '' } @$by;
Condition50%1/2
$row->{$_} // ""
l!l
780
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
3425 75 ▶ next if @$by && grep { !length($_) } @key_parts;
Branch: 1 branch
BranchTrueFalseTotal
if @$by and grep {not length $_;} @key_parts 0 75 75
Condition66%2/3
@$by and grep {not length $_;} @key_parts
!ll&&!rl&&r
3720
MC/DC0%0/2
@$by and grep {not length $_;} @key_parts
@$by grep {not length $_;} @key_parts
Truth table66%2/3
A and B
A= @$byB= grep {not length $_;} @key_parts
ABresult
0X0
100
111
3426 75 my $key = join("\x00", @key_parts);
3427  
3428 75 ▶ if ($fn eq 'count') {
Branch: 3 branches
BranchTrueFalseTotal
if ($fn eq 'count') { } 54 21 75
elsif ($fn eq 'count_distinct') { } 0 21 21
elsif ($fn eq 'first' or $fn eq 'last') { } 0 21 21
3429 54 ▶ push @key_order, $key unless $seen_key{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_key{$key}++ 43 11 54
3430 54 $totals{$key}++;
3431 } elsif ($fn eq 'count_distinct') {
3432 0 ▶ push @key_order, $key unless $seen_key{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_key{$key}++ 0 0 0
3433 0 ▶ my $dv = defined $val_col ? ($row->{$val_col} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val_col ? : 0 0 0
Condition0%0/2
$row->{$val_col} // ""
l!l
00
MC/DC0%0/1
$row->{$val_col} // ""
$row->{$val_col}
Truth table0%0/2
A
A= $row->{$val_col}
Aresult
11
00
3434 0 $distinct_vals{$key}{$dv}++;
3435 ▶ } elsif ($fn eq 'first' || $fn eq 'last') {
Condition33%1/3
$fn eq "first" or $fn eq "last"
l!l&&r!l&&!r
0021
MC/DC0%0/2
$fn eq "first" or $fn eq "last"
$fn eq "first" $fn eq "last"
Truth table33%1/3
A or B
A= $fn eq "first"B= $fn eq "last"
ABresult
1X1
011
000
3436 # first/last read any column, not only a numeric one, and they
3437 # report the value as it appears: the number a cell holds when it
3438 # holds a number, the text it holds otherwise. A blank cell is
3439 # not a value, so it is passed over -- the first row that has
3440 # something in the column is the first value.
3441 0 ▶ my $v = defined $val_col ? $row->{$val_col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val_col ? : 0 0 0
3442 0 ▶ next unless defined $v && length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 0 0 0
Condition0%0/3
defined $v and length $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table0%0/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
3443 0 ▶ push @key_order, $key unless $seen_key{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_key{$key}++ 0 0 0
3444 0 ▶ $totals{$key} = $v if $fn eq 'last' || !exists $totals{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "last" or not exists $totals{$key} 0 0 0
Condition0%0/3
$fn eq "last" or not exists $totals{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn eq "last" or not exists $totals{$key}
$fn eq "last" not exists $totals{$key}
Truth table0%0/3
A or not(B)
A= $fn eq "last"B= not exists $totals{$key}
ABresult
1X1
011
000
3445 } else {
3446 21 ▶ my $v = defined $val_col ? ($row->{$val_col} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val_col ? : 21 0 21
Condition100%2/2
$row->{$val_col} // ""
l!l
192
MC/DC100%1/1
$row->{$val_col} // ""
$row->{$val_col}
Truth table100%2/2
A
A= $row->{$val_col}
Aresult
11
00
3447 21 my $n = _numeric_cell($v);
3448 21 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 3 18 21
3449 3 ▶ unless ($seen_warned{"$fn\x01" . ($val_col // '')}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_warned{"$fn\cA" . ($val_col // "")}++) 2 1 3
Condition50%1/2
$val_col // ""
l!l
30
MC/DC0%0/1
$val_col // ""
$val_col
Truth table50%1/2
A
A= $val_col
Aresult
11
00
3450 2 ▶ carp $self->_i18n('warn_measure_fallback', $fn, $val_col // '(none)');
Condition50%1/2
$val_col // "(none)"
l!l
20
MC/DC0%0/1
$val_col // "(none)"
$val_col
Truth table50%1/2
A
A= $val_col
Aresult
11
00
3451 }
3452 3 next; # skip: do NOT add key to order for non-numeric rows
3453 }
3454 18 ▶ push @key_order, $key unless $seen_key{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_key{$key}++ 13 5 18
3455 18 ▶ if ($fn eq 'sum') { $totals{$key} += $n }
Branch: 4 branches
BranchTrueFalseTotal
if ($fn eq 'sum') { } 15 3 18
elsif ($fn eq 'avg') { } 3 0 3
elsif ($fn eq 'min') { } 0 0 0
elsif ($fn eq 'max') { } 0 0 0
3456 3 elsif ($fn eq 'avg') { $totals{$key} += $n; $count_map{$key}++ }
3457 0 ▶ elsif ($fn eq 'min') { $totals{$key} = (!exists $totals{$key} || $n < $totals{$key}) ? $n : $totals{$key} }
Branch: 1 branch
BranchTrueFalseTotal
!exists $totals{$key} || $n < $totals{$key} ? : 0 0 0
Condition0%0/3
not exists $totals{$key} or $n < $totals{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $totals{$key} or $n < $totals{$key}
not exists $totals{$key} $n < $totals{$key}
Truth table0%0/3
not(A) or B
A= not exists $totals{$key}B= $n < $totals{$key}
ABresult
1X1
011
000
3458 0 ▶ elsif ($fn eq 'max') { $totals{$key} = (!exists $totals{$key} || $n > $totals{$key}) ? $n : $totals{$key} }
Branch: 1 branch
BranchTrueFalseTotal
!exists $totals{$key} || $n > $totals{$key} ? : 0 0 0
Condition0%0/3
not exists $totals{$key} or $n > $totals{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $totals{$key} or $n > $totals{$key}
not exists $totals{$key} $n > $totals{$key}
Truth table0%0/3
not(A) or B
A= not exists $totals{$key}B= $n > $totals{$key}
ABresult
1X1
011
000
3459 0 else { push @{ $values{$key} }, $n }
3460 }
3461 }
3462  
3463 18 ▶ if ($fn eq 'avg') {
Branch: 3 branches
BranchTrueFalseTotal
if ($fn eq 'avg') { } 2 16 18
elsif ($fn eq 'count_distinct') { } 0 16 16
elsif (%values) { } 0 16 16
3464 2 for my $k (keys %totals) {
3465 2 ▶ $totals{$k} = $count_map{$k} ? $totals{$k} / $count_map{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$count_map{$k} ? : 2 0 2
3466 }
3467 } elsif ($fn eq 'count_distinct') {
3468 0 for my $k (@key_order) {
3469 0 ▶ $totals{$k} = scalar keys %{ $distinct_vals{$k} // {} };
Condition0%0/2
$distinct_vals{$k} // {}
l!l
00
MC/DC0%0/1
$distinct_vals{$k} // {}
$distinct_vals{$k}
Truth table0%0/2
A
A= $distinct_vals{$k}
Aresult
11
00
3470 }
3471 } elsif (%values) {
3472 # median / p<N> / stdev / range: resolved from the group's values, and
3473 # through the same arithmetic the stats view uses for a whole column.
3474 0 for my $k (keys %values) {
3475 0 my @sorted = sort { $a <=> $b } @{ $values{$k} };
3476 0 $totals{$k} = _measure_value($fn, \@sorted);
3477 }
3478 }
3479  
3480 # Assemble result rows in ascending key order (matching the SQL push path)
3481 # unless the caller requested an explicit sort.
3482 18 ▶ unless ($spec->{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{"sort_by"}) 18 0 18
3483 18 @key_order = sort @key_order;
3484 }
3485 18 my @result;
3486 18 for my $key (@key_order) {
3487 56 ▶ my @parts = @$by ? (split /\x00/, $key, scalar @$by) : ();
Branch: 1 branch
BranchTrueFalseTotal
@$by ? : 55 1 56
3488 56 my %row;
3489 56 for my $i (0 .. $#$by) {
3490 61 $row{$by->[$i]} = $parts[$i];
3491 }
3492 56 ▶ $row{$as} = $totals{$key} // ($fn =~ /\A(?:sum|count|count_distinct)\z/ ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ /\A(?:sum|count|count_distinct)\z/u ? : 0 0 0
Condition50%1/2
$totals{$key} // ($fn =~ /\A(?:sum|count|count_distinct)\z/u ? 0 : undef)
l!l
560
MC/DC0%0/1
$totals{$key} // ($fn =~ /\A(?:sum|count|count_distinct)\z/u ? 0 : undef)
$totals{$key}
Truth table50%1/2
A
A= $totals{$key}
Aresult
11
00
3493 56 push @result, \%row;
3494 }
3495  
3496 18 return \@result;
3497 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
3498  
3499 # _rows_of_page(\@rows, $offset, $per_page) -> \@page
3500 #
3501 # One page of rows, in an array of its own. The rows may be a backend's own
3502 # read-only array, as they are from a DBI handle that is not SQLite, so they are
3503 # copied and never spliced.
3504 198 ▶sub _rows_of_page {
Subroutine _rows_of_page: called CC=3 SCAR=11.0
3505 198 my ($rows, $offset, $per_page) = @_;
3506 # A negative offset would count from the end of the rows.
3507 198 ▶ $offset = 0 if $offset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 1 197 198
3508 198 my $last = $offset + $per_page - 1;
3509 198 ▶ $last = $#$rows if $last > $#$rows;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $rows->$#* 195 3 198
3510 198 return [ @{$rows}[$offset .. $last] ];
3511 }
3512  
3513 # _page_count($total, $per_page) -> $pages
3514 #
3515 # How many pages $total rows make, for the table's "Page N of M". No rows is
3516 # still one page, the empty one the table shows.
3517 189 ▶sub _page_count {
Subroutine _page_count: called CC=4 SCAR=17.1
3518 189 my ($total, $per_page) = @_;
3519 189 ▶ return 1 unless $total && $per_page && $per_page > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $total and $per_page and $per_page > 0 0 189 189
Condition33%2/6
$total and $per_page and $per_page > 0
!ll&&!rl&&r
00189
$total and $per_page
!ll&&!rl&&r
00189
MC/DC0%0/3
$total and $per_page and $per_page > 0
$total $per_page $per_page > 0
Truth table25%1/4
A and B and C
A= $totalB= $per_pageC= $per_page > 0
ABCresult
0XX0
10X0
1100
1111
3520 189 return int(($total + $per_page - 1) / $per_page);
3521 }
3522  
3523 # Where a cell with no date or number sorts: first, ascending, as -Infinity
3524 # does in the browser.
3525 5 Readonly my $NO_NUMBER => -9**9**9;
3526  
3527 # _sort_records(\@rows, $column, $dir) -> \@sorted
3528 #
3529 # Every row, in the order the table's own header sort gives a page of them
3530 # (biValueComparator in dashboard.html.tt), so the first page of a table too
3531 # long for one page is the first page of the sorted rows rather than the first
3532 # rows sorted. The column sorts as dates when every non-empty cell is one
3533 # (YYYY-MM-DD, or M/D/YYYY unless a cell shows the day comes first), else as
3534 # numbers when every non-empty cell holds one (Util::parse_number, which the
3535 # browser's parseAccounting copies: "$1,000" and "(5)" are numbers, "T27" is
3536 # text), else as text the way localeCompare(..., { numeric: true, sensitivity:
3537 # 'base' }) does: case and accents ignored, a run of digits compared as a
3538 # number. An empty cell sorts first, ascending. Rows that compare equal keep
3539 # their order, in either direction. A new array: the rows may be read-only.
3540 1 ▶sub _sort_records ($rows, $column, $dir) {
Subroutine _sort_records: called CC=14 SCAR=39.8
3541 my @text = map {
3542 1 ▶ my $v = $_->{$column} // '';
Condition50%1/2
$_->{$column} // ""
l!l
20
MC/DC0%0/1
$_->{$column} // ""
$_->{$column}
Truth table50%1/2
A
A= $_->{$column}
Aresult
11
00
3543 2 $v =~ s/\A\s+|\s+\z//g;
3544 2 $v;
3545 } @$rows;
3546 1 my @filled = grep { length } @text;
3547  
3548 1 my $key_of; # cell text -> number; text sorts when there is none
3549 1 ▶ if (@filled && !(grep { !m{\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z} } @filled)) {
Branch: 2 branches
BranchTrueFalseTotal
if (@filled and not grep({not m[\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z]u;} @filled)) { } 1 0 1
elsif (@filled and not grep({not defined Database::BI::Util::parse_number($_);} @filled)) { } 0 0 0
Condition33%1/3
@filled and not grep({not m[\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z]u;} @filled)
!ll&&!rl&&r
001
MC/DC0%0/2
@filled and not grep({not m[\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z]u;} @filled)
@filled not grep({not m[\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z]u;} @filled)
Truth table33%1/3
A and not(B)
A= @filledB= not grep({not m[\A(?:\d{4}-\d{2}-\d{2}|\d{1,2}/\d{1,2}/\d{4})\z]u;} @filled)
ABresult
0X0
100
111
3550 1 my $month_first = 1;
3551 1 for (@filled) {
3552 2 ▶ my ($p1, $p2) = m{\A(\d{1,2})/(\d{1,2})/} or next;
Branch: 1 branch
BranchTrueFalseTotal
() = m[\A(\d{1,2})/(\d{1,2})/]u or next 0 2 2
3553 0 ▶ if ($p1 > 12) { $month_first = 0; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($p1 > 12) 0 0 0
3554 0 ▶ if ($p2 > 12) { last }
Branch: 1 branch
BranchTrueFalseTotal
if ($p2 > 12) 0 0 0
3555 }
3556 2 ▶ $key_of = sub ($v) {
Subroutine __ANON__: called CC=4 SCAR=25.0
3557 2 ▶ return $1 * 10_000 + $2 * 100 + $3 if $v =~ /\A(\d{4})-(\d{2})-(\d{2})\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A(\d{4})-(\d{2})-(\d{2})\z/u 2 0 2
3558 0 ▶ if ($v =~ m{\A(\d{1,2})/(\d{1,2})/(\d{4})\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ m[\A(\d{1,2})/(\d{1,2})/(\d{4})\z]u) 0 0 0
3559 0 ▶ return $month_first ? $3 * 10_000 + $1 * 100 + $2 : $3 * 10_000 + $2 * 100 + $1;
Branch: 1 branch
BranchTrueFalseTotal
$month_first ? : 0 0 0
3560 }
3561 0 return $NO_NUMBER;
3562 1 };
3563 0 ▶ } elsif (@filled && !(grep { !defined Database::BI::Util::parse_number($_) } @filled)) {
Condition0%0/3
@filled and not grep({not defined Database::BI::Util::parse_number($_);} @filled)
!ll&&!rl&&r
000
MC/DC0%0/2
@filled and not grep({not defined Database::BI::Util::parse_number($_);} @filled)
@filled not grep({not defined Database::BI::Util::parse_number($_);} @filled)
Truth table0%0/3
A and not(B)
A= @filledB= not grep({not defined Database::BI::Util::parse_number($_);} @filled)
ABresult
0X0
100
111
3564 0 ▶ $key_of = sub ($v) { Database::BI::Util::parse_number($v) // $NO_NUMBER };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
Database::BI::Util::parse_number($v) // $NO_NUMBER
l!l&&r!l&&!r
000
MC/DC0%0/2
Database::BI::Util::parse_number($v) // $NO_NUMBER
Database::BI::Util::parse_number($v) $NO_NUMBER
Truth table0%0/3
A // B
A= Database::BI::Util::parse_number($v)B= $NO_NUMBER
ABresult
1X1
011
000
3565 }
3566  
3567 # Equal rows keep their order in either direction, as the browser's stable
3568 # sort of -cmp does: the direction turns the comparison, not the result.
3569 1 ▶ my $sign = $dir eq 'desc' ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'desc' ? : 1 0 1
3570 1 my @order;
3571 1 ▶ if ($key_of) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_of) { } 1 0 1
3572 1 my @key = map { $key_of->($_) } @text;
3573 1 ▶ @order = sort { $sign * ($key[$a] <=> $key[$b]) || $a <=> $b } 0 .. $#$rows;
Condition33%1/3
$sign * ($key[$a] <=> $key[$b]) || $a <=> $b
l!l&&r!l&&!r
100
MC/DC0%0/2
$sign * ($key[$a] <=> $key[$b]) || $a <=> $b
$sign * ($key[$a] <=> $key[$b]) $a <=> $b
Truth table33%1/3
A || B
A= $sign * ($key[$a] <=> $key[$b])B= $a <=> $b
ABresult
1X1
011
000
3574 } else {
3575 0 my @key = map { _collation_key($_) } @text;
3576 0 ▶ @order = sort { $sign * _collate($key[$a], $key[$b]) || $a <=> $b } 0 .. $#$rows;
Condition0%0/3
$sign * _collate($key[$a], $key[$b]) || $a <=> $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$sign * _collate($key[$a], $key[$b]) || $a <=> $b
$sign * _collate($key[$a], $key[$b]) $a <=> $b
Truth table0%0/3
A || B
A= $sign * _collate($key[$a], $key[$b])B= $a <=> $b
ABresult
1X1
011
000
3577 }
3578 1 return [ @{$rows}[@order] ];
3579 }
3580  
3581 # _collation_key($text) -> \@tokens
3582 #
3583 # The text as localeCompare with sensitivity 'base' and numeric sees it: case
3584 # and accents folded away, each run of digits one number, every other
3585 # character ranked as ICU's root collation does -- spaces, then punctuation and
3586 # symbols, then digits, then letters.
3587 0 ▶sub _collation_key ($text) {
Subroutine _collation_key: not called CC=5 SCAR=34.0
3588 0 my $folded = CORE::fc(Unicode::Normalize::NFD($text));
3589 0 $folded =~ s/\p{Mn}+//g;
3590 0 my @tokens;
3591 0 ▶ while ($folded =~ /(\d+)|(.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folded =~ /(\d+)|(.)/gsu) 0 0 0
3592 0 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
3593 0 push @tokens, [2, $1 + 0];
3594 } else {
3595 0 my $c = $2;
3596 0 ▶ push @tokens, [ $c =~ /\s/ ? 0 : $c =~ /\p{L}/ ? 3 : 1, $c ];
Branch: 2 branches
BranchTrueFalseTotal
$c =~ /\s/u ? : 0 0 0
$c =~ /\p{L}/u ? : 0 0 0
3597 }
3598 }
3599 0 return \@tokens;
3600 }
3601  
3602 # _collate(\@a, \@b) -> -1 | 0 | 1
3603 0 ▶sub _collate ($x, $y) {
Subroutine _collate: not called CC=6 SCAR=37.4
3604 0 ▶ my $n = @$x < @$y ? @$x : @$y;
Branch: 1 branch
BranchTrueFalseTotal
@$x < @$y ? : 0 0 0
3605 0 for my $i (0 .. $n - 1) {
3606 0 my ($p, $q) = ($x->[$i], $y->[$i]);
3607 0 ▶ my $c = $p->[0] <=> $q->[0]
Branch: 1 branch
BranchTrueFalseTotal
$p->[0] == 2 ? : 0 0 0
Condition0%0/3
$p->[0] <=> $q->[0] || ($p->[0] == 2 ? $p->[1] <=> $q->[1] : $p->[1] cmp $q->[1])
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->[0] <=> $q->[0] || ($p->[0] == 2 ? $p->[1] <=> $q->[1] : $p->[1] cmp $q->[1])
$p->[0] <=> $q->[0] ($p->[0] == 2 ? $p->[1] <=> $q->[1] : $p->[1] cmp $q->[1])
Truth table0%0/3
A || B
A= $p->[0] <=> $q->[0]B= ($p->[0] == 2 ? $p->[1] <=> $q->[1] : $p->[1] cmp $q->[1])
ABresult
1X1
011
000
3608 || ($p->[0] == 2 ? $p->[1] <=> $q->[1] : $p->[1] cmp $q->[1]);
3609 0 ▶ return $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
3610 }
3611 0 return @$x <=> @$y;
3612 }
3613  
3614 # _fetch_page( $self, $source, \%params ) -> ( $records, $has_next )
3615 # (also stashes page_count, the number of pages: from the rows read, or on
3616 # the SQL fast path, which reads one page and a row, from the source's count)
3617 # params: per_page -- page size (required)
3618 # offset -- 0-based row offset (required)
3619 # dedup -- truthy when ?d=1 dedup is requested
3620 # sort_col -- column name to sort by (optional; default the request's
3621 # sort= parameter)
3622 # sort_dir -- 'asc' or 'desc' (default the request's dir=, else 'asc')
3623 #
3624 # Purpose: Fetch and reduce one page of records from $source, honouring the
3625 # reduce-before-paginate ordering contract. Two modes:
3626 #
3627 # Fast path (no f= filters, no dedup): uses the N+1 trick
3628 # selectall_arrayref(limit => per_page+1, offset => offset). has_next is
3629 # determined by the extra row before it is popped.
3630 #
3631 # Reduction path (f= filters or dedup present): materialises the full result
3632 # in memory, applies all f= filters with _apply_filter_spec (case-insensitive),
3633 # applies dedup if requested, then paginates in Perl. This guarantees that
3634 # the Nth page contains the Nth page of the *reduced* set, not the Nth page
3635 # of the raw set reduced to fewer rows. Memory cost: the full reduced set.
3636 #
3637 # Sort: a sort column takes the reduction path, and every row is sorted by
3638 # _sort_records -- after the fields, filters and cross-row columns, so any
3639 # column the table shows can be sorted on -- before the page is cut. The
3640 # table's header sort works on the rows it was given, so on a table longer
3641 # than a page it sorted only those, and the page was not the first of the
3642 # sorted table; the page asks for sort= instead whenever there is more than
3643 # one page. The backend's sort_by is not used: it compares as text, and
3644 # ignores a column whose name is not a safe identifier. When the sort is
3645 # applied, stash sorted_by => { col, dir } tells the page what it shows.
3646 #
3647 # Returns (undef, 0) when the underlying fetch throws; the caller must handle
3648 # $records being undef.
3649 164 ▶sub _fetch_page :Protected ($self, $source, $params) {
Subroutine _fetch_page: called CC=57 SCAR=49.8
3650 164 my $per_page = $params->{per_page};
3651 164 my $offset = $params->{offset};
3652 164 my $dedup = $params->{dedup};
3653 164 ▶ my $sort_col = $params->{sort_col} // $self->param('sort');
Condition100%3/3
$params->{"sort_col"} // $self->param("sort")
l!l&&r!l&&!r
22160
MC/DC100%2/2
$params->{"sort_col"} // $self->param("sort")
$params->{"sort_col"} $self->param("sort")
Truth table100%3/3
A // B
A= $params->{"sort_col"}B= $self->param("sort")
ABresult
1X1
011
000
3654 164 ▶ $sort_col = undef unless defined $sort_col && length $sort_col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sort_col and length $sort_col 161 3 164
Condition100%3/3
defined $sort_col and length $sort_col
!ll&&!rl&&r
16013
MC/DC100%2/2
defined $sort_col and length $sort_col
defined $sort_col length $sort_col
Truth table100%3/3
A and B
A= defined $sort_colB= length $sort_col
ABresult
0X0
100
111
3655 164 ▶ my $sort_dir = ($params->{sort_dir} // $self->param('dir') // 'asc') =~ /\Adesc\z/i ? 'desc' : 'asc';
Branch: 1 branch
BranchTrueFalseTotal
(($params->{'sort_dir'} // $self->param('dir')) // 'asc') =~ /\Adesc\z/iu ? : 1 163 164
Condition80%4/5
($params->{"sort_dir"} // $self->param("dir")) // "asc"
l!l
50114
$params->{"sort_dir"} // $self->param("dir")
l!l&&r!l&&!r
500114
MC/DC50%1/2
($params->{"sort_dir"} // $self->param("dir")) // "asc"
$params->{"sort_dir"} $self->param("dir")
Truth table66%2/3
A // B
A= $params->{"sort_dir"}B= $self->param("dir")
ABresult
1X1
011
000
3656  
3657 # Collect and validate the filters, the range's bounds included.
3658 my @f_specs = grep {
3659 12 my ($c, $o) = split /:/, $_, 3;
3660 12 ▶ length($c // '') && defined $o && length $o;
Condition87%7/8
length($c // '') && defined $o && length $o
!ll&&!rl&&r
2010
length($c // "") and defined $o
!ll&&!rl&&r
1110
$c // ""
l!l
111
MC/DC66%2/3
length($c // '') && defined $o && length $o
$c defined $o length $o
Truth table75%3/4
A and B && C
A= $cB= defined $oC= length $o
ABCresult
0XX0
10X0
1100
1111
3661 164 } @{ _filter_specs($self) };
3662  
3663 # Determine whether the source has a SQL backend that supports LIMIT/OFFSET.
3664 # DBD::CSV / SQL::Statement do not implement LIMIT/OFFSET; passing them emits
3665 # a "Bad limit clause" error at the D::A layer before falling back to a Perl
3666 # approach. Avoid the noise by only using the N+1 fast path for SQL-backed
3667 # sources. For non-SQL sources (CSV, XLSX, in-memory) the reduction path
3668 # below materialises the full set and paginates in Perl.
3669 164 ▶ my $is_sql = ($source->can('dbi_source') && $source->dbi_source)
Condition55%5/9
$source->can('dbi_source') && $source->dbi_source || $source->can('pages_on_server') && $source->pages_on_server
l!l&&r!l&&!r
190145
$source->can("dbi_source") and $source->dbi_source
!ll&&!rl&&r
014519
$source->can("pages_on_server") and $source->pages_on_server
!ll&&!rl&&r
01450
MC/DC25%1/4
$source->can('dbi_source') && $source->dbi_source || $source->can('pages_on_server') && $source->pages_on_server
$source->can("dbi_source") $source->dbi_source $source->can("pages_on_server") $source->pages_on_server
Truth table28%2/7
A and B || C and D
A= $source->can("dbi_source")B= $source->dbi_sourceC= $source->can("pages_on_server")D= $source->pages_on_server
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3670 || ($source->can('pages_on_server') && $source->pages_on_server);
3671  
3672 # A cross-row column is a column of the whole filtered set, so a request that
3673 # names one cannot take the N+1 fast path: it would see one page of rows and
3674 # restart every running total on page 2.
3675 164 ▶ my $cross = length($self->param('cs') // '') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length($self->param('cs') // '') ? : 2 162 164
Condition100%2/2
$self->param("cs") // ""
l!l
3161
MC/DC100%1/1
$self->param("cs") // ""
$self->param("cs")
Truth table100%2/2
A
A= $self->param("cs")
Aresult
11
00
3676  
3677 164 ▶ unless (@f_specs || $dedup || $cross || defined $sort_col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@f_specs or $dedup or $cross or defined $sort_col) 149 15 164
Condition100%9/9
@f_specs or $dedup or $cross or defined $sort_col
l!l&&r!l&&!r
123149
@f_specs or $dedup or $cross
l!l&&r!l&&!r
102152
@f_specs or $dedup
l!l&&r!l&&!r
91154
MC/DC100%4/4
@f_specs or $dedup or $cross or defined $sort_col
@f_specs $dedup $cross defined $sort_col
Truth table100%5/5
A or B or C or D
A= @f_specsB= $dedupC= $crossD= defined $sort_col
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3678 149 ▶ if ($is_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_sql) 16 133 149
3679 # Fast path (SQL-backed): delegate LIMIT/OFFSET to the backend N+1 trick.
3680 # Copy before pop: D::A may return a read-only ref to its internal cache.
3681 16 ▶ my @records = @{ $source->selectall_arrayref(
Condition50%1/2
$source->selectall_arrayref("limit", $per_page + 1, "offset", $offset) // []
l!l
160
MC/DC0%0/1
$source->selectall_arrayref("limit", $per_page + 1, "offset", $offset) // []
$source->selectall_arrayref("limit", $per_page + 1, "offset", $offset)
Truth table50%1/2
A
A= $source->selectall_arrayref("limit", $per_page + 1, "offset", $offset)
Aresult
11
00
3682 limit => $per_page + 1, offset => $offset,
3683 ) // [] };
3684 16 ▶ my $has_next = (@records > $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@records > $per_page ? : 0 16 16
3685 16 ▶ pop @records if $has_next;
Branch: 1 branch
BranchTrueFalseTotal
if $has_next 0 16 16
3686 # The number of pages needs the number of rows. count() remembers a
3687 # file's until it changes, so only the first page of a big SQLite
3688 # file pays for the COUNT(*). Only when there is more than one page:
3689 # a short table shows no pagination. A count that fails costs the
3690 # page its "of M", never the page.
3691 16 ▶ if ($has_next || $offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_next or $offset > 0) 0 16 16
Condition33%1/3
$has_next or $offset > 0
l!l&&r!l&&!r
0016
MC/DC0%0/2
$has_next or $offset > 0
$has_next $offset > 0
Truth table33%1/3
A or B
A= $has_nextB= $offset > 0
ABresult
1X1
011
000
3692 0 my $total;
3693 0 my $ok = eval { $total = $source->count; 1 };
3694 0 ▶ $self->stash(page_count => _page_count($total, $per_page))
Branch: 1 branch
BranchTrueFalseTotal
if $ok and defined $total and $total =~ /\A\d+\z/u 0 0 0
Condition0%0/6
$ok and defined $total and $total =~ /\A\d+\z/u
!ll&&!rl&&r
000
$ok and defined $total
!ll&&!rl&&r
000
MC/DC0%0/3
$ok and defined $total and $total =~ /\A\d+\z/u
$ok defined $total $total =~ /\A\d+\z/u
Truth table0%0/4
A and B and C
A= $okB= defined $totalC= $total =~ /\A\d+\z/u
ABCresult
0XX0
10X0
1100
1111
3695 if $ok && defined $total && $total =~ /\A\d+\z/;
3696 }
3697 16 return (\@records, $has_next);
3698 }
3699 # Non-SQL fast path: fetch all, then paginate. Every row is in hand, so
3700 # the number of pages costs nothing.
3701 133 ▶ my $all = $source->selectall_arrayref() // [];
Condition50%1/2
$source->selectall_arrayref // []
l!l
1330
MC/DC0%0/1
$source->selectall_arrayref // []
$source->selectall_arrayref
Truth table50%1/2
A
A= $source->selectall_arrayref
Aresult
11
00
3702 133 ▶ my $has_next = (@$all > $offset + $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$all > $offset + $per_page ? : 0 133 133
3703 133 $self->stash(page_count => _page_count(scalar(@$all), $per_page));
3704 133 return (_rows_of_page($all, $offset, $per_page), $has_next);
3705 }
3706  
3707 # Always fetch all rows in Perl and filter with _apply_filter_spec.
3708 # SQL criteria pushdown (passing %criteria to selectall_arrayref) is
3709 # intentionally avoided: on some Perl/D::A version combinations the WHERE
3710 # clause is silently dropped, returning the full unfiltered set. The Perl
3711 # path is correct on every supported version. Memory cost: bounded by the
3712 # source file size (the same as the dedup path already accepted).
3713 15 ▶ my $all = $source->selectall_arrayref() // [];
Condition50%1/2
$source->selectall_arrayref // []
l!l
150
MC/DC0%0/1
$source->selectall_arrayref // []
$source->selectall_arrayref
Truth table50%1/2
A
A= $source->selectall_arrayref
Aresult
11
00
3714  
3715 # A stored date or time pattern is what makes a patterned column comparable
3716 # with the ISO bounds a relative time range carries, and it belongs to stage 2
3717 # of the pipeline -- before the filters of stage 5. The rest of the
3718 # application reaches the filters through that order already; this path, which
3719 # filters before it paginates, is the one call site that would otherwise
3720 # compare the reader's format, so it normalises first, on exactly the rows the
3721 # specs are about to be applied to.
3722 15 $self->_apply_date_normalization($all, [_get_columns($source, $all)],
3723 $self->_calc_spec_key);
3724  
3725 # A failure below is a 400 about the request. It is rendered here, and the
3726 # caller sees the stash and returns rather than drawing a page over it.
3727 2 ▶ my $refuse = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
3728 2 my ($why) = @_;
3729 2 $why =~ s/ at \S+ line \d+\.?\n?$//;
3730 2 $self->render(text => $why, status => 400);
3731 2 $self->stash('_cross_row_error' => $why);
3732 2 return;
3733 15 };
3734  
3735 # Stage 4. The page's own fields run on the sliced page (_page_columns), which
3736 # is enough to show them, but a filter or a window that reads one needs it on
3737 # every row, and before them. That pass then only repeats these, on one page.
3738 15 $all = [ @$all ]; # the backend's own array can be read-only
3739 15 my @cols_so_far;
3740 15 ▶ if (defined $params->{calc_spec_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"calc_spec_key"}) 15 0 15
3741 15 my @before = _get_columns($source, $all);
3742 15 ▶ my $span = @before ? $self->_span_calc_spec(\@before, ()) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@before ? : 15 0 15
3743 15 ▶ if ($span && $span->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span and $span->{"error"}) 0 15 15
Condition33%1/3
$span and $span->{"error"}
!ll&&!rl&&r
1500
MC/DC0%0/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table33%1/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
3744 0 $refuse->($span->{error});
3745 0 return ();
3746 }
3747 15 my ($r, $c) = eval {
3748 $self->_calc_for_page($all, \@before, {
3749 spec_key => $params->{calc_spec_key},
3750 15 ▶ ($span ? (calc => $span->{calc}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 15 15
3751 })
3752 };
3753 15 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 15 15
3754 0 $refuse->($@);
3755 0 return ();
3756 }
3757 15 ($all, @cols_so_far) = ($r, @$c);
3758 }
3759  
3760 # A filter on a column stages 6 and 7 write waits for them. See _reduce_records.
3761 0 ▶ my %written = $cross ? map { $_ => 1 } _cross_row_written($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
$cross ? : 2 13 15
3762 15 ▶ my @filter_cols = @cols_so_far ? @cols_so_far : _get_columns($source, $all);
Branch: 1 branch
BranchTrueFalseTotal
@cols_so_far ? : 15 0 15
3763 15 ▶ if (defined(my $why = _unknown_filter_column($self, \@filter_cols, \%written))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $why = _unknown_filter_column($self, \@filter_cols, \%written))) 0 15 15
3764 0 $refuse->($why);
3765 0 return ();
3766 }
3767 15 my @late = grep { $written{ (split /:/, $_, 2)[0] } } @f_specs;
3768 15 $all = _apply_filter_spec($all, $_) for grep { !$written{ (split /:/, $_, 2)[0] } } @f_specs;
3769  
3770 15 ▶ if ($dedup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dedup) 1 14 15
3771 1 my $cols = $source->columns;
3772 0 ▶ my @dcols = $cols ? @$cols : (keys %{ $all->[0] // {} });
Branch: 1 branch
BranchTrueFalseTotal
$cols ? : 1 0 1
Condition0%0/2
$all->[0] // {}
l!l
00
MC/DC0%0/1
$all->[0] // {}
$all->[0]
Truth table0%0/2
A
A= $all->[0]
Aresult
11
00
3773 1 $all = _dedup_records($all, \@dcols);
3774 }
3775  
3776 # Stages 6 and 7, before the slice: a running total has to see every filtered
3777 # row, or page 2 would start again from zero.
3778 15 ▶ if ($cross) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cross) 2 13 15
3779 2 my $cols = $source->columns;
3780 my @xcols = @cols_so_far ? @cols_so_far
3781 0 ▶ : $cols ? @$cols : (sort keys %{ $all->[0] // {} });
Branch: 2 branches
BranchTrueFalseTotal
@cols_so_far ? : 2 0 2
$cols ? : 0 0 0
Condition0%0/2
$all->[0] // {}
l!l
00
MC/DC0%0/1
$all->[0] // {}
$all->[0]
Truth table0%0/2
A
A= $all->[0]
Aresult
11
00
3782 2 my $done = eval {
3783 2 my ($r, $c) = $self->_apply_cross_row($all, \@xcols);
3784 0 ($all) = $self->_apply_held_calc_fields($r, $c);
3785 0 1;
3786 };
3787 2 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 2 0 2
3788 2 $refuse->($@);
3789 2 return ();
3790 }
3791 0 $all = _apply_filter_spec($all, $_) for @late;
3792 }
3793  
3794 # The whole reduced set, sorted, before the page is cut. A column the view
3795 # does not have is left alone (the order is the file's), as the header sort
3796 # ignores one it cannot find.
3797 13 ▶ if (defined $sort_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_col) 3 10 13
3798 3 ▶ my %known = map { $_ => 1 } (@cols_so_far ? @cols_so_far : _get_columns($source, $all)), keys %written;
Branch: 1 branch
BranchTrueFalseTotal
@cols_so_far ? : 3 0 3
3799 3 ▶ $known{$sort_col} = 1 if $cross && @$all && exists $all->[0]{$sort_col};
Branch: 1 branch
BranchTrueFalseTotal
if $cross and @$all and exists $all->[0]{$sort_col} 0 3 3
Condition33%2/6
$cross and @$all and exists $all->[0]{$sort_col}
!ll&&!rl&&r
300
$cross and @$all
!ll&&!rl&&r
300
MC/DC0%0/3
$cross and @$all and exists $all->[0]{$sort_col}
$cross @$all exists $all->[0]{$sort_col}
Truth table25%1/4
A and B and C
A= $crossB= @$allC= exists $all->[0]{$sort_col}
ABCresult
0XX0
10X0
1100
1111
3800 3 ▶ if ($known{$sort_col}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known{$sort_col}) 1 2 3
3801 1 $all = _sort_records($all, $sort_col, $sort_dir);
3802 1 $self->stash(sorted_by => { col => $sort_col, dir => $sort_dir });
3803 }
3804 }
3805  
3806 13 my $total = scalar @$all;
3807 13 ▶ my $has_next = ($total > $offset + $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$total > $offset + $per_page ? : 0 13 13
3808 13 my @page = @{ _rows_of_page($all, $offset, $per_page) };
3809 13 $self->stash(page_count => _page_count($total, $per_page));
3810  
3811 13 return (\@page, $has_next);
3812 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
3813  
3814 # _combine_tables(\@sources) -> (\@merged_records, \@merged_columns)
3815 #
3816 # Purpose: Perform a vertical stack (UNION ALL equivalent) of two or more
3817 # record sets. The merged column list is the union of all source
3818 # column lists: the first source's columns appear first (in their
3819 # original order), then each subsequent source contributes any new
3820 # column names it introduces, in order. Where a source does not
3821 # have a particular column, its rows receive an empty string for
3822 # that column.
3823 # Entry: $sources is an arrayref of [$records_aref, $columns_aref] pairs.
3824 # At least one element is required; a single-element call returns
3825 # that element's data unchanged (no allocation beyond the column copy).
3826 # Exit: Returns (\@merged_records, \@merged_columns).
3827 # Side Effects: None; allocates new record hashrefs (original rows are not mutated).
3828 12 ▶sub _combine_tables {
Subroutine _combine_tables: called CC=9 SCAR=22.0
3829 12 my ($sources) = @_;
3830  
3831 # Build the unified column order: first source's columns first, then any
3832 # new columns introduced by each subsequent source in their file order.
3833 12 my (@all_cols, %seen);
3834 12 for my $src (@$sources) {
3835 23 for my $col (@{ $src->[1] }) {
3836 95 ▶ push @all_cols, $col unless $seen{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$col}++ 57 38 95
3837 }
3838 }
3839  
3840 12 my @merged;
3841 12 for my $src (@$sources) {
3842 23 my ($recs, $cols) = @$src;
3843 23 my %has_col = map { $_ => 1 } @$cols;
3844 23 for my $row (@$recs) {
3845 133 my %new_row;
3846 $new_row{$_} = $has_col{$_} ? ($row->{$_} // '') : ''
3847 133 ▶ for @all_cols;
Branch: 1 branch
BranchTrueFalseTotal
$has_col{$_} ? : 790 150 940
Condition50%1/2
$row->{$_} // ""
l!l
7900
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
3848 133 push @merged, \%new_row;
3849 }
3850 }
3851  
3852 12 return (\@merged, \@all_cols);
3853 }
3854  
3855 # _csv_row(@fields) -> $csv_line_with_crlf
3856 #
3857 # Purpose: Format one row as an RFC 4180 CSV line. Fields containing commas,
3858 # double-quotes, or newlines are enclosed in double-quotes; embedded
3859 # double-quotes are doubled. The line ends with CRLF.
3860 # Entry: @fields may contain undef (treated as empty string).
3861 # Exit: Returns a string ending with "\r\n".
3862 521 ▶sub _csv_row {
Subroutine _csv_row: called CC=3 SCAR=11.0
3863 return join(',', map {
3864 521 ▶ my $f = $_ // '';
Condition100%2/2
$_ // ""
l!l
30462
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
3865 3048 ▶ $f =~ /[,"\r\n]/ ? do { (my $q = $f) =~ s/"/""/g; qq{"$q"} } : $f;
Branch: 1 branch
BranchTrueFalseTotal
$f =~ /[,"\r\n]/u ? : 6 3042 3048
3866 } @_) . "\r\n";
3867 }
3868  
3869 # _build_export_url($self, $left_spec, \@join_specs, \@filter_specs) -> $url_string
3870 #
3871 # Purpose: Assemble a /export?l=...&j=...&f=... URL string for the current
3872 # logical view. The caller must apply | html in TT to escape & as &amp;.
3873 # Entry: All args non-undef; spec arrays may be empty.
3874 # Exit: Returns a relative URL string beginning with '/export'.
3875 207 ▶sub _build_export_url :Protected ($self, $left_spec, $join_specs, $filter_specs, $combine_specs = undef, $dedup = 0, $extra = undef) {
Subroutine _build_export_url: called CC=13 SCAR=25.6
3876 207 my $u = '/export?l=' . url_escape($left_spec);
3877 207 $u .= '&j=' . url_escape($_) for @$join_specs;
3878 207 ▶ $u .= '&c=' . url_escape($_) for @{ $combine_specs // [] };
Condition100%2/2
$combine_specs // []
l!l
43164
MC/DC100%1/1
$combine_specs // []
$combine_specs
Truth table100%2/2
A
A= $combine_specs
Aresult
11
00
3879 207 $u .= '&f=' . url_escape($_) for @$filter_specs;
3880 207 ▶ $u .= '&d=1' if $dedup;
Branch: 1 branch
BranchTrueFalseTotal
if $dedup 2 205 207
3881 # Parameters that shape the columns rather than the rows travel with the
3882 # export, so the file holds the column the page shows -- a time bucket, for
3883 # one -- and not the raw values behind it.
3884 207 ▶ for my $key (sort keys %{ $extra // {} }) {
Condition100%2/2
$extra // {}
l!l
76131
MC/DC100%1/1
$extra // {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
3885 228 my $val = $extra->{$key};
3886 228 ▶ next unless defined $val && length $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and length $val 227 1 228
Condition100%3/3
defined $val and length $val
!ll&&!rl&&r
22431
MC/DC100%2/2
defined $val and length $val
defined $val length $val
Truth table100%3/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
3887 1 $u .= "&$key=" . url_escape($val);
3888 }
3889 207 return $u;
3890 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
3891  
3892 # _list_dir($self, $dir, $want_files) -> { dirs => [...], files => [...] }
3893 #
3894 # Purpose: Shared directory-listing kernel used by browse (which needs files)
3895 # and dirs_api (which needs only subdirectories). Hidden entries
3896 # (names starting with ".") are excluded. Entries are sorted
3897 # case-insensitively.
3898 # Entry: $dir is a Mojo::File pointing to an existing readable directory.
3899 # $want_files: if true, populate 'files' with entries matching $EXT_RE.
3900 # Exit: Returns a hashref with 'dirs' and 'files' arrayrefs (files is always
3901 # present but empty when $want_files is false).
3902 # Side Effects: Filesystem directory read.
3903 57 ▶sub _list_dir :Protected ($self, $dir, $want_files) {
Subroutine _list_dir: called CC=9 SCAR=22.0
3904 57 my (@dirs, @files);
3905 57 ▶ if (opendir my $dh, $dir->to_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $dir->to_string) 57 0 57
3906 57 ▶ while (my $entry = readdir $dh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 429 57 486
3907 429 ▶ next if $entry eq '.' || $entry eq '..' || $entry =~ /\A\./;
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." or $entry =~ /\A\./u 295 134 429
Condition100%6/6
$entry eq "." or $entry eq ".." or $entry =~ /\A\./u
l!l&&r!l&&!r
114181134
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
5757315
MC/DC100%3/3
$entry eq "." or $entry eq ".." or $entry =~ /\A\./u
$entry eq "." $entry eq ".." $entry =~ /\A\./u
Truth table100%4/4
A or B or C
A= $entry eq "."B= $entry eq ".."C= $entry =~ /\A\./u
ABCresult
1XX1
01X1
0011
0000
3908 134 my $f = $dir->child($entry);
3909 134 ▶ if (-d $f) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $f) { } 75 59 134
elsif ($want_files and $entry =~ /$EXT_RE/u) { } 21 52 73
3910 75 push @dirs, { name => $entry, path => $f->to_string };
3911 }
3912 ▶ elsif ($want_files && $entry =~ $EXT_RE) {
Condition100%3/3
$want_files and $entry =~ /$EXT_RE/u
!ll&&!rl&&r
142421
MC/DC100%2/2
$want_files and $entry =~ /$EXT_RE/u
$want_files $entry =~ /$EXT_RE/u
Truth table100%3/3
A and B
A= $want_filesB= $entry =~ /$EXT_RE/u
ABresult
0X0
100
111
3913 21 push @files, { name => $entry, path => $f->to_string };
3914 }
3915 }
3916 57 closedir $dh;
3917 }
3918 return {
3919 48 dirs => [ sort { lc($a->{name}) cmp lc($b->{name}) } @dirs ],
3920 57 files => [ sort { lc($a->{name}) cmp lc($b->{name}) } @files ],
3921 };
3922 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
3923  
3924 # _write_sqlite_db($self, $records, $columns, $table) -> $raw_bytes
3925 #
3926 # Purpose: $records as a SQLite database of one table, every column TEXT, as
3927 # the file's bytes. Used by both _render_sqlite (HTTP download) and
3928 # export_write (filesystem write); Database::Join::Catalog writes it.
3929 # Entry: $records is an arrayref of hashrefs; $columns is an arrayref of names;
3930 # $table names the table, "data" unless given. An extract passes
3931 # "extract", which Database::Abstraction before 0.54 needed (it could
3932 # not read a table called "data"); extracts already built use it.
3933 # Exit: Returns scalar binary string. Croaks when it cannot be written.
3934 # Side Effects: Catalog makes and removes one temporary file under tmpdir.
3935 8 ▶sub _write_sqlite_db :Protected ($self, $records, $columns, $table = 'data') {
Subroutine _write_sqlite_db: called CC=3 SCAR=11.0
3936 # D~ fix: an empty column list would be "CREATE TABLE data ()", which is
3937 # invalid SQLite syntax; this message is the one readers have seen.
3938 8 ▶ croak 'Cannot export SQLite: result set has no columns'
Branch: 1 branch
BranchTrueFalseTotal
unless @$columns 1 7 8
3939 unless @$columns;
3940  
3941 7 require Database::Join::Catalog;
3942 7 return Database::Join::Catalog->sqlite_bytes(table => $table, columns => $columns, rows => $records);
3943 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
3944  
3945 # _run_pipeline_source($self) -> ($src, $records, \@columns, $left_label) or ()
3946 #
3947 # Purpose: Inner pipeline: opens the left spec, builds the join chain, fetches
3948 # all rows, and runs any c= combine step. Does NOT apply f= filters,
3949 # dedup, or pagination -- that is the caller's responsibility.
3950 # Chart routes call this directly so they can access $src for the
3951 # aggregate() push path. Row-viewing routes and export routes
3952 # call the thin _run_export_pipeline wrapper instead.
3953 # Exit: On success: ($src_or_undef, $records_aref, $columns_aref, $label).
3954 # $src is undef when a c= combine makes the result non-aggregatable.
3955 # On failure: empty list (left table not found / fetch error).
3956 749 ▶sub _run_pipeline_source :Protected ($self) {
Subroutine _run_pipeline_source: called CC=27 SCAR=33.9
3957 749 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
7472
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
3958 749 my ($left_src, $left_label) = $self->_open_spec($left_spec);
3959 749 ▶ return () unless $left_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $left_src 34 715 749
3960  
3961 # Build the join chain lazily.
3962 715 ▶ my $max_rows = $self->app->config('join_max_rows') // $DEFAULT_JOIN_MAX_ROWS;
Condition66%2/3
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
l!l&&r!l&&!r
07132
MC/DC50%1/2
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
$self->app->config("join_max_rows") $DEFAULT_JOIN_MAX_ROWS
Truth table66%2/3
A // B
A= $self->app->config("join_max_rows")B= $DEFAULT_JOIN_MAX_ROWS
ABresult
1X1
011
000
3963 715 ▶ my $join_type = $self->param('jtype') // 'left';
Condition100%2/2
$self->param("jtype") // "left"
l!l
15700
MC/DC100%1/1
$self->param("jtype") // "left"
$self->param("jtype")
Truth table100%2/2
A
A= $self->param("jtype")
Aresult
11
00
3964 715 ▶ $join_type = 'left' unless $join_type =~ /\A(?:left|inner|outer)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $join_type =~ /\A(?:left|inner|outer)\z/u 14 701 715
3965 715 my $src = $left_src;
3966 715 my (@schema_warnings, @joined, @combined);
3967 715 for my $jspec (@{ $self->every_param('j') }) {
3968 20 my ($right_spec, $left_key, $right_key) = split /\|/, $jspec, 3;
3969 20 ▶ next unless defined $right_spec && defined $left_key && defined $right_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $right_spec and defined $left_key and defined $right_key 14 6 20
Condition66%4/6
defined $right_spec and defined $left_key and defined $right_key
!ll&&!rl&&r
1406
defined $right_spec and defined $left_key
!ll&&!rl&&r
0146
MC/DC33%1/3
defined $right_spec and defined $left_key and defined $right_key
defined $right_spec defined $left_key defined $right_key
Truth table50%2/4
A and B and C
A= defined $right_specB= defined $left_keyC= defined $right_key
ABCresult
0XX0
10X0
1100
1111
3970  
3971 6 my $cur_cols = $src->columns;
3972 6 ▶ next unless $cur_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur_cols 0 6 6
3973 6 ▶ next unless +{ map { $_ => 1 } @$cur_cols }->{$left_key};
Branch: 1 branch
BranchTrueFalseTotal
unless {map({$_, 1;} @$cur_cols)}->{$left_key} 0 6 6
3974  
3975 6 my ($right_src, $right_label) = $self->_open_spec($right_spec);
3976 6 ▶ next unless $right_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_src 1 5 6
3977  
3978 5 my $right_cols = $right_src->columns;
3979 5 ▶ next unless $right_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_cols 0 5 5
3980 5 ▶ next unless +{ map { $_ => 1 } @$right_cols }->{$right_key};
Branch: 1 branch
BranchTrueFalseTotal
unless {map({$_, 1;} @$right_cols)}->{$right_key} 0 5 5
3981  
3982 # Types are compared while the two sides are still separate sources;
3983 # after the join is built the mismatched columns are already published
3984 # under different names. See _schema_type_warnings.
3985 5 push @schema_warnings, $self->_schema_type_warnings(
3986 $src, $right_src, $left_label, $right_label, $left_key, $right_key);
3987  
3988 5 require Database::Join;
3989 0 ▶ local $SIG{__WARN__} = sub { push @schema_warnings, $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
3990 5 ▶ $src = Database::Join->new(
Branch: 2 branches
BranchTrueFalseTotal
$left_key ne $right_key ? : 0 5 5
$right_label ? : 5 0 5
3991 databases => [$src, $right_src],
3992 join_column => $left_key,
3993 backend => 'auto',
3994 max_array_rows => $max_rows,
3995 join_type => $join_type,
3996 ($left_key ne $right_key ? (join_map => {1 => $right_key}) : ()),
3997 ($right_label ? (collision_prefix => {1 => $right_label}) : ()),
3998 );
3999 5 push @joined, { label => $right_label, left_key => $left_key, right_key => $right_key };
4000 }
4001  
4002 715 ▶ $self->stash(schema_warnings => \@schema_warnings) if @schema_warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @schema_warnings 0 715 715
4003  
4004 715 my $records = eval { $src->selectall_arrayref };
4005 715 ▶ return () if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 715 715
4006 715 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
7150
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
4007 715 my @columns = _get_columns($src, $records);
4008  
4009 # Combine (vertical stack): sets $src = undef since the result is a union
4010 # with no single aggregatable object.
4011 715 ▶ my @cspecs = @{ $self->every_param('c') // [] };
Condition50%1/2
$self->every_param("c") // []
l!l
7150
MC/DC0%0/1
$self->every_param("c") // []
$self->every_param("c")
Truth table50%1/2
A
A= $self->every_param("c")
Aresult
11
00
4012 715 ▶ if (@cspecs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cspecs) 24 691 715
4013 24 my @sources = ([$records, \@columns]);
4014 24 for my $cspec (@cspecs) {
4015 24 my ($csrc, $clabel) = $self->_open_spec($cspec);
4016 24 ▶ next unless $csrc;
Branch: 1 branch
BranchTrueFalseTotal
unless $csrc 17 7 24
4017 7 ▶ my $crecs = eval { $csrc->fetch_all } // [];
Condition50%1/2
eval { do { $csrc->fetch_all } } // []
l!l
70
MC/DC0%0/1
eval { do { $csrc->fetch_all } } // []
eval { do { $csrc->fetch_all } }
Truth table50%1/2
A
A= eval { do { $csrc->fetch_all } }
Aresult
11
00
4018 7 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
4019 7 my @ccols = _get_columns($csrc, $crecs);
4020 7 push @sources, [$crecs, \@ccols];
4021 7 push @combined, { label => $clabel };
4022 }
4023 24 ▶ if (@sources > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sources > 1) 7 17 24
4024 7 ($records, my $new_cols) = _combine_tables(\@sources);
4025 7 @columns = @$new_cols;
4026 7 $src = undef; # union: no single aggregatable source
4027 }
4028 }
4029  
4030 # What was actually joined and stacked, for the page that shows the rows.
4031 715 $self->stash(pipeline_joined => \@joined, pipeline_combined => \@combined);
4032  
4033 715 return ($src, $records, \@columns, $left_label);
4034 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4035  
4036 # _calc_spec_key($self) -> $spec
4037 #
4038 # Purpose: The key the stored calc fields, groups and date patterns are filed
4039 # under for this request: the l= spec, or the table a /view route is
4040 # addressed by, which names its spec explicitly because it has no l=.
4041 23 ▶sub _calc_spec_key :Protected ($self) {
Subroutine _calc_spec_key: called CC=5 SCAR=16.1
4042 23 ▶ my $l = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
716
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
4043 23 ▶ return canonical_spec($l) if length $l;
Branch: 1 branch
BranchTrueFalseTotal
if length $l 7 16 23
4044 16 my $table = $self->stash('table');
4045 16 ▶ return (defined $table && $table =~ /$TABLE_NAME_RE/) ? "table:$table" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $table && $table =~ /$TABLE_NAME_RE/u ? : 6 10 16
Condition100%3/3
defined $table and $table =~ /$TABLE_NAME_RE/u
!ll&&!rl&&r
916
MC/DC100%2/2
defined $table and $table =~ /$TABLE_NAME_RE/u
defined $table $table =~ /$TABLE_NAME_RE/u
Truth table100%3/3
A and B
A= defined $tableB= $table =~ /$TABLE_NAME_RE/u
ABresult
0X0
100
111
4046 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4047  
4048 # _span_calc_spec($self, \@columns, @dimension_params) -> \%span | undef
4049 #
4050 # Purpose: Turn span=/tcol=/as= into the c= calculated field a reader could have
4051 # typed, so bucketing a dimension is one code path with one set of
4052 # semantics (Calc.pm's span()) rather than a second pipeline of its
4053 # own, and so a bucket works on every route at once.
4054 # Entry: \@columns -- the source's columns, so the column to bucket and the
4055 # name of the bucket are both checked before anything is
4056 # evaluated.
4057 # @dimension_params -- the params that name this route's dimension
4058 # (cat, x, rows, ...), in the order they should be tried.
4059 # Exit: undef when the request says nothing about bucketing. Otherwise
4060 # { calc => [ '<col>_span:span(<col>,<interval>)' ],
4061 # src => <column to bucket>, col => '<bucket column>' },
4062 # or { error => <message> } for a request that cannot be honoured,
4063 # which the caller renders as a 400 rather than drawing a chart of
4064 # something the reader did not ask for.
4065 # Notes: An ambiguous dimension is refused, not guessed: with rows=at,region
4066 # only tcol= knows which column the reader meant, and bucketing the
4067 # wrong one silently is worse than an error that says so. A name that
4068 # is already a column, or already a calculated field, is refused too,
4069 # because two definitions of one name is the bug that rule prevents.
4070 634 ▶sub _span_calc_spec :Protected ($self, $columns, @dimension_params) {
Subroutine _span_calc_spec: called CC=25 SCAR=32.5
4071 634 ▶ my $interval = $self->param('span') // '';
Condition100%2/2
$self->param("span") // ""
l!l
30604
MC/DC100%1/1
$self->param("span") // ""
$self->param("span")
Truth table100%2/2
A
A= $self->param("span")
Aresult
11
00
4072 634 ▶ my $tcol = $self->param('tcol') // '';
Condition100%2/2
$self->param("tcol") // ""
l!l
23611
MC/DC100%1/1
$self->param("tcol") // ""
$self->param("tcol")
Truth table100%2/2
A
A= $self->param("tcol")
Aresult
11
00
4073 634 ▶ my $as = $self->param('as') // '';
Condition100%2/2
$self->param("as") // ""
l!l
19615
MC/DC100%1/1
$self->param("as") // ""
$self->param("as")
Truth table100%2/2
A
A= $self->param("as")
Aresult
11
00
4074 634 ▶ return undef unless length($interval) || length($tcol);
Branch: 1 branch
BranchTrueFalseTotal
unless length $interval or length $tcol 595 39 634
Condition100%3/3
length $interval or length $tcol
l!l&&r!l&&!r
2811595
MC/DC100%2/2
length $interval or length $tcol
length $interval length $tcol
Truth table100%3/3
A or B
A= length $intervalB= length $tcol
ABresult
1X1
011
000
4075  
4076 39 ▶ return { error => $self->_i18n('error_span_tcol_orphan', $tcol) }
Branch: 1 branch
BranchTrueFalseTotal
if length $tcol and not length $interval 11 28 39
Condition100%3/3
length $tcol and not length $interval
!ll&&!rl&&r
171111
MC/DC100%2/2
length $tcol and not length $interval
length $tcol not length $interval
Truth table100%3/3
A and not(B)
A= length $tcolB= not length $interval
ABresult
0X0
100
111
4077 if length($tcol) && !length($interval);
4078  
4079 28 require Database::BI::Calc;
4080 28 ▶ return { error => $self->_i18n('error_span_interval', $interval) }
Branch: 1 branch
BranchTrueFalseTotal
unless Database::BI::Calc::span_interval($interval) 15 13 28
4081 unless Database::BI::Calc::span_interval($interval);
4082  
4083 13 my %col_set = map { $_ => 1 } @$columns;
4084  
4085 13 my $col;
4086 13 ▶ if (length $tcol) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tcol) { } 10 3 13
4087 return { error => $self->_i18n('error_span_tcol_unknown', $tcol) }
4088 10 ▶ unless $col_set{$tcol};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$tcol} 1 9 10
4089 9 $col = $tcol;
4090 } else {
4091 3 my @named = grep { length }
4092 3 ▶ map { split /,/, ($self->param($_) // ''), -1 } @dimension_params;
Condition100%2/2
$self->param($_) // ""
l!l
21
MC/DC100%1/1
$self->param($_) // ""
$self->param($_)
Truth table100%2/2
A
A= $self->param($_)
Aresult
11
00
4093 3 ▶ return { error => $self->_i18n('error_span_no_column') } unless @named == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @named == 1 2 1 3
4094 1 $col = $named[0];
4095 }
4096  
4097 # A bucket column is written in the calc grammar, so spaces in the column's
4098 # name arrive as underscores, exactly as a reader typing the expression
4099 # would have to write them.
4100 10 (my $expr_col = $col) =~ s/ /_/g;
4101 10 ▶ my $name = length($as) ? $as : "${expr_col}_span";
Branch: 1 branch
BranchTrueFalseTotal
length $as ? : 8 2 10
4102 10 ▶ return { error => $self->_i18n('error_span_name', $name) }
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/u 5 5 10
4103 unless $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
4104  
4105 5 ▶ my $taken = $col_set{$name} || do {
Condition100%3/3
$col_set{$name} || do { my $found = 0; my $sk = $self->_calc_spec_key; my $stored = length $sk ? $self->_read_models->{'fields'}{$sk} // {} : {}; $found = 1 if $stored->{$name}; foreach my $spec (@{$self->every_param('c') // [];}) { next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu; my($n) = $spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):/u; $found = 1 if defined $n and $n eq $name; } ; $found }
l!l&&r!l&&!r
113
MC/DC100%2/2
$col_set{$name} || do { my $found = 0; my $sk = $self->_calc_spec_key; my $stored = length $sk ? $self->_read_models->{'fields'}{$sk} // {} : {}; $found = 1 if $stored->{$name}; foreach my $spec (@{$self->every_param('c') // [];}) { next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu; my($n) = $spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):/u; $found = 1 if defined $n and $n eq $name; } ; $found }
$col_set{$name} do { my $found = 0; my $sk = $self->_calc_spec_key; my $stored = length $sk ? $self->_read_models->{'fields'}{$sk} // {} : {}; $found = 1 if $stored->{$name}; foreach my $spec (@{$self->every_param('c') // [];}) { next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu; my($n) = $spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):/u; $found = 1 if defined $n and $n eq $name; } ; $found }
Truth table100%3/3
A || B
A= $col_set{$name}B= do { my $found = 0; my $sk = $self->_calc_spec_key; my $stored = length $sk ? $self->_read_models->{'fields'}{$sk} // {} : {}; $found = 1 if $stored->{$name}; foreach my $spec (@{$self->every_param('c') // [];}) { next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu; my($n) = $spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):/u; $found = 1 if defined $n and $n eq $name; } ; $found }
ABresult
1X1
011
000
4106 4 my $found = 0;
4107 4 my $sk = $self->_calc_spec_key;
4108 4 ▶ my $stored = length($sk) ? (($self->_read_models)->{fields}{$sk} // {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
length $sk ? : 0 4 4
Condition0%0/2
$self->_read_models->{"fields"}{$sk} // {}
l!l
00
MC/DC0%0/1
$self->_read_models->{"fields"}{$sk} // {}
$self->_read_models->{"fields"}{$sk}
Truth table0%0/2
A
A= $self->_read_models->{"fields"}{$sk}
Aresult
11
00
4109 4 ▶ $found = 1 if $stored->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $stored->{$name} 0 4 4
4110 4 ▶ for my $spec (@{ $self->every_param('c') // [] }) {
Condition50%1/2
$self->every_param("c") // []
l!l
40
MC/DC0%0/1
$self->every_param("c") // []
$self->every_param("c")
Truth table50%1/2
A
A= $self->every_param("c")
Aresult
11
00
4111 1 ▶ next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu 0 1 1
4112 1 my ($n) = $spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):/;
4113 1 ▶ $found = 1 if defined $n && $n eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n and $n eq $name 1 0 1
Condition33%1/3
defined $n and $n eq $name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $n and $n eq $name
defined $n $n eq $name
Truth table33%1/3
A and B
A= defined $nB= $n eq $name
ABresult
0X0
100
111
4114 }
4115 4 $found;
4116 };
4117 5 ▶ return { error => $self->_i18n('error_span_conflict', $name) } if $taken;
Branch: 1 branch
BranchTrueFalseTotal
if $taken 2 3 5
4118  
4119 return {
4120 3 src => $col,
4121 col => $name,
4122 calc => [ "$name:span($expr_col,'$interval')" ],
4123 };
4124 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4125  
4126 # _apply_date_normalization($self, $records, \@columns, $spec_key) -> void
4127 #
4128 # Reads date_patterns from .bi/models.json for $spec_key and normalises each
4129 # configured column's values to ISO-8601 in-place. A value that does not match
4130 # the stored pattern is left unchanged so YEAR/MONTH/etc. return undef (and the
4131 # row falls into the '(unparseable)' bucket in _grouped_totals).
4132 # Called at the top of _apply_calc_fields so registered patterns are applied
4133 # before any calc expression is evaluated.
4134 896 ▶sub _apply_date_normalization :Protected ($self, $records, $columns, $spec_key) {
Subroutine _apply_date_normalization: called CC=11 SCAR=24.0
4135 896 ▶ return unless defined $spec_key && length $spec_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec_key and length $spec_key 13 883 896
Condition100%3/3
defined $spec_key and length $spec_key
!ll&&!rl&&r
112883
MC/DC100%2/2
defined $spec_key and length $spec_key
defined $spec_key length $spec_key
Truth table100%3/3
A and B
A= defined $spec_keyB= length $spec_key
ABresult
0X0
100
111
4136 883 my $models = $self->_read_models;
4137 883 my $patterns = $models->{date_patterns}{$spec_key};
4138 883 ▶ return unless $patterns && %{$patterns};
Branch: 1 branch
BranchTrueFalseTotal
unless $patterns and %{$patterns;} 880 3 883
Condition66%2/3
$patterns and %{$patterns;}
!ll&&!rl&&r
88003
MC/DC50%1/2
$patterns and %{$patterns;}
$patterns %{$patterns;}
Truth table66%2/3
A and B
A= $patternsB= %{$patterns;}
ABresult
0X0
100
111
4139 3 my %col_set = map { $_ => 1 } @{$columns};
4140 3 require Database::BI::Calc;
4141 3 for my $col (keys %{$patterns}) {
4142 3 ▶ next unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 1 2 3
4143 2 my $pat = $patterns->{$col};
4144 2 for my $row (@{$records}) {
4145 6 my $v = $row->{$col};
4146 6 ▶ next unless defined $v && length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 2 4 6
Condition100%3/3
defined $v and length $v
!ll&&!rl&&r
114
MC/DC100%2/2
defined $v and length $v
defined $v length $v
Truth table100%3/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
4147 # Only overwrite when normalisation succeeds; leave unchanged on
4148 # mismatch so the row lands in the '(unparseable)' group.
4149 4 my $iso = Database::BI::Calc::date_normalise($v, $pat);
4150 4 ▶ $row->{$col} = $iso if defined $iso;
Branch: 1 branch
BranchTrueFalseTotal
if defined $iso 1 3 4
4151 }
4152 }
4153 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4154  
4155 # _lookups_of_request($self, $spec_key) -> @lookups
4156 #
4157 # Purpose: The lookups this request applies, in order: those stored for
4158 # $spec_key, by name, then each lk=<name> stored for another dataset,
4159 # less every nolk=<name>.
4160 # Entry: $spec_key files the stored lookups (the l= spec, or a canvas's).
4161 # Exit: A list of { name => $name, %definition }. Croaks error_lookup_unknown
4162 # for an lk= that names no stored lookup, and error_lookup_name for one
4163 # that is not an identifier.
4164 881 ▶sub _lookups_of_request :Protected ($self, $spec_key) {
Subroutine _lookups_of_request: called CC=14 SCAR=26.4
4165 881 my $all = $self->_read_models->{lookups};
4166 881 ▶ my %skip = map { $_ => 1 } @{ $self->every_param('nolk') // [] };
Condition50%1/2
$self->every_param("nolk") // []
l!l
8810
MC/DC0%0/1
$self->every_param("nolk") // []
$self->every_param("nolk")
Truth table50%1/2
A
A= $self->every_param("nolk")
Aresult
11
00
4167 881 ▶ my $own = (length($spec_key // '') && ref $all->{$spec_key} eq 'HASH') ? $all->{$spec_key} : {};
Branch: 1 branch
BranchTrueFalseTotal
length($spec_key // '') && ref $all->{$spec_key} eq 'HASH' ? : 8 873 881
Condition100%5/5
length($spec_key // "") and ref $all->{$spec_key} eq "HASH"
!ll&&!rl&&r
88658
$spec_key // ""
l!l
8801
MC/DC100%2/2
length($spec_key // "") and ref $all->{$spec_key} eq "HASH"
$spec_key ref $all->{$spec_key} eq "HASH"
Truth table100%3/3
A and B
A= $spec_keyB= ref $all->{$spec_key} eq "HASH"
ABresult
0X0
100
111
4168  
4169 881 my (@lookups, %seen);
4170 881 for my $name (sort keys %$own) {
4171 12 ▶ next if $skip{$name} || ref $own->{$name} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$name} or ref $own->{$name} ne "HASH" 5 7 12
Condition100%3/3
$skip{$name} or ref $own->{$name} ne "HASH"
l!l&&r!l&&!r
147
MC/DC100%2/2
$skip{$name} or ref $own->{$name} ne "HASH"
$skip{$name} ref $own->{$name} ne "HASH"
Truth table100%3/3
A or B
A= $skip{$name}B= ref $own->{$name} ne "HASH"
ABresult
1X1
011
000
4172 7 push @lookups, { %{ $own->{$name} }, name => $name };
4173 7 $seen{$name} = 1;
4174 }
4175 881 ▶ for my $name (@{ $self->every_param('lk') // [] }) {
Condition50%1/2
$self->every_param("lk") // []
l!l
8810
MC/DC0%0/1
$self->every_param("lk") // []
$self->every_param("lk")
Truth table50%1/2
A
A= $self->every_param("lk")
Aresult
11
00
4176 21 ▶ next if $seen{$name}++ || $skip{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ or $skip{$name} 2 19 21
Condition66%2/3
$seen{$name}++ or $skip{$name}
l!l&&r!l&&!r
0219
MC/DC50%1/2
$seen{$name}++ or $skip{$name}
$seen{$name}++ $skip{$name}
Truth table66%2/3
A or B
A= $seen{$name}++B= $skip{$name}
ABresult
1X1
011
000
4177 19 ▶ croak $self->_i18n('error_lookup_name', $name) unless $name =~ $LOOKUP_NAME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$LOOKUP_NAME_RE/u 15 4 19
4178 # Another dataset's lookup, the first by the order of the specs, so the
4179 # choice does not depend on how the store happens to be read.
4180 6 my ($def) = grep { ref $_ eq 'HASH' }
4181 4 map { $all->{$_}{$name} } sort keys %$all;
4182 4 ▶ croak $self->_i18n('error_lookup_unknown', $name) unless $def;
Branch: 1 branch
BranchTrueFalseTotal
unless $def 2 2 4
4183 2 push @lookups, { %$def, name => $name };
4184 }
4185 864 return @lookups;
4186 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4187  
4188 # _lookup_file($self, $name, $file) -> $path
4189 #
4190 # Purpose: Resolve a lookup's file= to the file it reads. A relative path is
4191 # under data_dir; an absolute one must be, after symlinks are resolved,
4192 # so a link inside data_dir cannot reach outside it.
4193 # Entry: $name names the lookup in the messages.
4194 # Exit: The resolved path as a string. Croaks error_lookup_remote (a URL or
4195 # another source spec, or a remote /../host/ path), error_lookup_outside,
4196 # error_lookup_not_found (missing, or not a supported extension) or
4197 # error_lookup_too_large (over $LOOKUP_MAX_BYTES).
4198 26 ▶sub _lookup_file :Protected ($self, $name, $file) {
Subroutine _lookup_file: called CC=10 SCAR=23.0
4199 26 ▶ $file //= '';
Condition100%2/2
$file //= ""
l!l
251
MC/DC100%1/1
$file //= ""
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
4200 # Two letters at least before the colon, so C:\data is still a path.
4201 26 ▶ croak $self->_i18n('error_lookup_remote', $name, $file)
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[\A(?:[A-Za-z][A-Za-z0-9+.-]+:|/\.\./)]u 5 21 26
4202 if $file =~ m{\A(?:[A-Za-z][A-Za-z0-9+.-]+:|/\.\./)};
4203 21 ▶ croak $self->_i18n('error_lookup_not_found', $name, $file) unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 2 19 21
4204  
4205 19 my $data_dir = _data_dir_for($self->app);
4206 19 my $given = Mojo::File->new($file);
4207 19 ▶ my $path = $given->is_abs ? $given : $data_dir->child($file);
Branch: 1 branch
BranchTrueFalseTotal
$given->is_abs ? : 2 17 19
4208 19 ▶ croak $self->_i18n('error_lookup_outside', $name, $file)
Branch: 1 branch
BranchTrueFalseTotal
unless _inside_dir(_canonical_path($path), _canonical_path($data_dir)) 4 15 19
4209 unless _inside_dir(_canonical_path($path), _canonical_path($data_dir));
4210 15 ▶ croak $self->_i18n('error_lookup_not_found', $name, $file)
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path and $path->basename =~ /$EXT_RE/u 3 12 15
Condition100%3/3
-f $path and $path->basename =~ /$EXT_RE/u
!ll&&!rl&&r
2112
MC/DC100%2/2
-f $path and $path->basename =~ /$EXT_RE/u
-f $path $path->basename =~ /$EXT_RE/u
Truth table100%3/3
A and B
A= -f $pathB= $path->basename =~ /$EXT_RE/u
ABresult
0X0
100
111
4211 unless -f $path && $path->basename =~ $EXT_RE;
4212 12 ▶ my $size = -s $path // 0;
Condition50%1/2
-s $path // 0
l!l
120
MC/DC0%0/1
-s $path // 0
-s $path
Truth table50%1/2
A
A= -s $path
Aresult
11
00
4213 12 ▶ croak $self->_i18n('error_lookup_too_large', $name, $file, $size, $LOOKUP_MAX_BYTES)
Branch: 1 branch
BranchTrueFalseTotal
if $size > $LOOKUP_MAX_BYTES 1 11 12
4214 if $size > $LOOKUP_MAX_BYTES;
4215 11 return _canonical_path($path);
4216 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4217  
4218 # _load_lookup($self, $lookup) -> \%entry
4219 #
4220 # Purpose: Read a lookup's file into a map from key to row, once per request.
4221 # Entry: $lookup is one entry of _lookups_of_request.
4222 # Exit: { map => { key => \%row }, fields => \@names, file => $basename }.
4223 # Keys are compared as strings after trimming, case-sensitively; a
4224 # duplicate key keeps its first row and is logged once. Croaks one of
4225 # _lookup_file's errors, error_lookup_unreadable, error_lookup_key_unknown
4226 # or error_lookup_field_unknown.
4227 # Side Effects: Caches the entry in the stash, keyed by name, path and mtime.
4228 11 ▶sub _load_lookup :Protected ($self, $lookup) {
Subroutine _load_lookup: called CC=18 SCAR=29.2
4229 11 my $name = $lookup->{name};
4230 11 my $path = $self->_lookup_file($name, $lookup->{file});
4231 9 my $file = Mojo::File->new($path)->basename;
4232 9 ▶ my $memo = $self->stash('_lookup_maps') // {};
Condition100%2/2
$self->stash("_lookup_maps") // {}
l!l
36
MC/DC100%1/1
$self->stash("_lookup_maps") // {}
$self->stash("_lookup_maps")
Truth table100%2/2
A
A= $self->stash("_lookup_maps")
Aresult
11
00
4233 my $tag = join "\0", $name, $path, (stat $path)[9] // 0,
4234 9 ▶ $lookup->{key} // '', @{ ref $lookup->{fields} eq 'ARRAY' ? $lookup->{fields} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $lookup->{'fields'} eq 'ARRAY' ? : 5 4 9
Condition50%2/4
(stat $path)[9] // 0
l!l
90
$lookup->{"key"} // ""
l!l
90
MC/DC0%0/1
(stat $path)[9] // 0
(stat $path)[9]
MC/DC0%0/1
$lookup->{"key"} // ""
$lookup->{"key"}
Truth table50%1/2
A
A= (stat $path)[9]
Aresult
11
00
Truth table50%1/2
A
A= $lookup->{"key"}
Aresult
11
00
4235 9 ▶ return $memo->{$tag} if $memo->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $memo->{$tag} 1 8 9
4236  
4237 8 (my $table = $file) =~ s/\.[^.]+\z//;
4238 8 my ($src, $rows);
4239 8 my $ok = eval {
4240 8 $src = $self->open_table($table, directory => Mojo::File->new($path)->dirname->to_string);
4241 8 $rows = $src->fetch_all;
4242 8 1;
4243 };
4244 8 ▶ croak $self->_i18n('error_lookup_unreadable', $name, $file, _tidy_error($@)) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 8 8
4245  
4246 8 my @columns = _get_columns($src, $rows);
4247 8 my %has = map { $_ => 1 } @columns;
4248 8 ▶ my $key = $lookup->{key} // '';
Condition50%1/2
$lookup->{"key"} // ""
l!l
80
MC/DC0%0/1
$lookup->{"key"} // ""
$lookup->{"key"}
Truth table50%1/2
A
A= $lookup->{"key"}
Aresult
11
00
4249 8 ▶ croak $self->_i18n('error_lookup_key_unknown', $name, $key, $file) unless $has{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $has{$key} 1 7 8
4250  
4251 5 my @fields = ref $lookup->{fields} eq 'ARRAY' && @{ $lookup->{fields} }
4252 5 ? @{ $lookup->{fields} }
4253 7 ▶ : grep { $_ ne $key } @columns;
Branch: 1 branch
BranchTrueFalseTotal
ref $lookup->{'fields'} eq 'ARRAY' && @{$lookup->{'fields'};} ? : 5 2 7
Condition66%2/3
ref $lookup->{"fields"} eq "ARRAY" and @{$lookup->{"fields"};}
!ll&&!rl&&r
205
MC/DC50%1/2
ref $lookup->{"fields"} eq "ARRAY" and @{$lookup->{"fields"};}
ref $lookup->{"fields"} eq "ARRAY" @{$lookup->{"fields"};}
Truth table66%2/3
A and B
A= ref $lookup->{"fields"} eq "ARRAY"B= @{$lookup->{"fields"};}
ABresult
0X0
100
111
4254 7 for my $f (@fields) {
4255 9 ▶ croak $self->_i18n('error_lookup_field_unknown', $name, $f, $file) unless $has{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $has{$f} 1 8 9
4256 }
4257  
4258 6 my (%map, $duplicates);
4259 6 for my $row (@$rows) {
4260 17 my $k = _lookup_key($row->{$key});
4261 17 ▶ next unless defined $k && length $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and length $k 0 17 17
Condition33%1/3
defined $k and length $k
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $k and length $k
defined $k length $k
Truth table33%1/3
A and B
A= defined $kB= length $k
ABresult
0X0
100
111
4262 17 ▶ if (exists $map{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map{$k}) 5 12 17
4263 5 $duplicates++;
4264 5 next;
4265 }
4266 12 $map{$k} = { map { $_ => $row->{$_} } @fields };
4267 }
4268 6 ▶ $self->app->log->warn($self->_i18n('warn_lookup_duplicate_key', $name, $duplicates, $file))
Branch: 1 branch
BranchTrueFalseTotal
if $duplicates 5 1 6
4269 if $duplicates;
4270  
4271 6 $memo->{$tag} = { map => \%map, fields => \@fields, file => $file };
4272 6 $self->stash('_lookup_maps' => $memo);
4273 6 return $memo->{$tag};
4274 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4275  
4276 # _lookup_key($value) -> $key | undef
4277 #
4278 # Purpose: The form in which a lookup compares a key: the string, trimmed. Case
4279 # and everything else are left as they are, so "ab" and "AB" are two keys.
4280 30 ▶sub _lookup_key {
Subroutine _lookup_key: called CC=3 SCAR=11.0
4281 30 my ($v) = @_;
4282 30 ▶ return undef unless defined $v && !ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v 2 28 30
Condition100%3/3
defined $v and not ref $v
!ll&&!rl&&r
1128
MC/DC100%2/2
defined $v and not ref $v
defined $v not ref $v
Truth table100%3/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
4283 28 (my $k = $v) =~ s/\A\s+|\s+\z//g;
4284 28 return $k;
4285 }
4286  
4287 # _apply_lookups($self, $records, \@cols, $spec_key) -> void
4288 #
4289 # Purpose: Stage 3 of the pipeline: each lookup the request applies adds its
4290 # fields to every row, read from the lookup file's row whose key equals
4291 # the row's own (col=, which defaults to the key's name). A row whose
4292 # key is missing gets the lookup's default, or undef.
4293 # Entry: \@cols is the column list so far; it gains the new columns.
4294 # Exit: Rows are replaced through _extend_row where they must be. The new
4295 # columns are virtual, so an aggregate that groups by one is folded in
4296 # Perl. Croaks with a message for a bad lookup or a missing column.
4297 # Notes: A field lands under its own name unless a column has it, and then
4298 # under <lookup>_<field>; a source column is never overwritten. The
4299 # columns this pass wrote do not count as taken when it runs again in
4300 # the same request, so the names a page gets do not depend on how
4301 # many times the stage has run over it.
4302 876 ▶sub _apply_lookups :Protected ($self, $records, $cols, $spec_key) {
Subroutine _apply_lookups: called CC=17 SCAR=28.4
4303 876 my @lookups = $self->_lookups_of_request($spec_key);
4304 861 ▶ return unless @lookups;
Branch: 1 branch
BranchTrueFalseTotal
unless @lookups 857 4 861
4305  
4306 4 ▶ my $written = $self->stash('_lookup_written') // {};
Condition50%1/2
$self->stash("_lookup_written") // {}
l!l
04
MC/DC0%0/1
$self->stash("_lookup_written") // {}
$self->stash("_lookup_written")
Truth table50%1/2
A
A= $self->stash("_lookup_written")
Aresult
11
00
4307 4 my %taken = map { $_ => 1 } grep { !$written->{$_} } @$cols;
4308 4 for my $lookup (@lookups) {
4309 4 my $name = $lookup->{name};
4310 4 my $entry = $self->_load_lookup($lookup);
4311 4 ▶ my $col = length($lookup->{col} // '') ? $lookup->{col} : $lookup->{key};
Branch: 1 branch
BranchTrueFalseTotal
length($lookup->{'col'} // '') ? : 1 3 4
Condition100%2/2
$lookup->{"col"} // ""
l!l
13
MC/DC100%1/1
$lookup->{"col"} // ""
$lookup->{"col"}
Truth table100%2/2
A
A= $lookup->{"col"}
Aresult
11
00
4312 croak $self->_i18n('error_lookup_col_unknown', $name, $col, $name)
4313 4 ▶ unless $taken{$col} || $written->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $taken{$col} or $written->{$col} 1 3 4
Condition66%2/3
$taken{$col} or $written->{$col}
l!l&&r!l&&!r
301
MC/DC50%1/2
$taken{$col} or $written->{$col}
$taken{$col} $written->{$col}
Truth table66%2/3
A or B
A= $taken{$col}B= $written->{$col}
ABresult
1X1
011
000
4314  
4315 3 my @out;
4316 3 for my $f (@{ $entry->{fields} }) {
4317 3 ▶ my $as = $taken{$f} ? "${name}_$f" : $f;
Branch: 1 branch
BranchTrueFalseTotal
$taken{$f} ? : 2 1 3
4318 3 ▶ croak $self->_i18n('error_lookup_name_taken', $name, $f, $as) if $taken{$as};
Branch: 1 branch
BranchTrueFalseTotal
if $taken{$as} 1 2 3
4319 2 $taken{$as} = 1;
4320 2 push @out, [ $f, $as ];
4321 }
4322  
4323 2 my $default = $lookup->{default};
4324 2 my $map = $entry->{map};
4325 2 for my $i (0 .. $#$records) {
4326 6 my $k = _lookup_key($records->[$i]{$col});
4327 6 ▶ my $hit = defined $k ? $map->{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $k ? : 6 0 6
4328 6 for my $pair (@out) {
4329 $records->[$i] = _extend_row($records->[$i], $pair->[1],
4330 6 ▶ $hit ? $hit->{ $pair->[0] } : $default);
Branch: 1 branch
BranchTrueFalseTotal
$hit ? : 4 2 6
4331 }
4332 }
4333  
4334 2 my %have = map { $_ => 1 } @$cols;
4335 2 push @$cols, grep { !$have{$_} } map { $_->[1] } @out;
4336 2 ▶ $self->stash('_virtual_columns' => {}) unless $self->stash('_virtual_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("_virtual_columns") 2 0 2
4337 2 for my $pair (@out) {
4338 2 $self->stash('_virtual_columns')->{ $pair->[1] } = 1;
4339 2 $written->{ $pair->[1] } = 1;
4340 }
4341 }
4342 2 $self->stash('_lookup_written' => $written);
4343 2 return;
4344 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4345  
4346 # _compile_extraction($self, $name, \%definition) -> (\%compiled, \@fields)
4347 #
4348 # Purpose: Check a stored extraction and compile its pattern, the same way on
4349 # save and on every request.
4350 # Exit: The compiled pattern (Calc::compile_pattern) and the named groups it
4351 # writes: fields= when it was given, else every named group, in order.
4352 # Croaks error_extract_pattern (wrapping Calc's reason),
4353 # error_extract_no_names or error_extract_field.
4354 13 ▶sub _compile_extraction :Protected ($self, $name, $def) {
Subroutine _compile_extraction: called CC=7 SCAR=19.5
4355 13 require Database::BI::Calc;
4356 13 my $compiled = eval { Database::BI::Calc::compile_pattern($def->{pattern}) };
4357 13 ▶ croak $self->_i18n('error_extract_pattern', $name, _tidy_error($@)) unless $compiled;
Branch: 1 branch
BranchTrueFalseTotal
unless $compiled 4 9 13
4358 9 my @names = @{ $compiled->{names} };
4359 9 ▶ croak $self->_i18n('error_extract_no_names', $name) unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 2 7 9
4360 7 my %named = map { $_ => 1 } @names;
4361 7 ▶ my @fields = ref $def->{fields} eq 'ARRAY' && @{ $def->{fields} } ? @{ $def->{fields} } : @names;
Branch: 1 branch
BranchTrueFalseTotal
ref $def->{'fields'} eq 'ARRAY' && @{$def->{'fields'};} ? : 2 5 7
Condition66%2/3
ref $def->{"fields"} eq "ARRAY" and @{$def->{"fields"};}
!ll&&!rl&&r
502
MC/DC50%1/2
ref $def->{"fields"} eq "ARRAY" and @{$def->{"fields"};}
ref $def->{"fields"} eq "ARRAY" @{$def->{"fields"};}
Truth table66%2/3
A and B
A= ref $def->{"fields"} eq "ARRAY"B= @{$def->{"fields"};}
ABresult
0X0
100
111
4362 7 for my $f (@fields) {
4363 8 ▶ croak $self->_i18n('error_extract_field', $name, $f) unless $named{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $named{$f} 1 7 8
4364 }
4365 6 return ($compiled, \@fields);
4366 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4367  
4368 # _apply_extractions($self, $records, \@cols, $spec_key) -> void
4369 #
4370 # Purpose: The stored extractions of $spec_key, after the lookups in stage 3:
4371 # each named group of a pattern becomes a column, read from the column
4372 # the extraction names. A row the pattern does not match, or whose
4373 # cell is empty, gets '(unparseable)', so it groups visibly rather than
4374 # vanishing; a group that took no part in a match is ''.
4375 # Entry: \@cols is the column list so far; it gains the new columns.
4376 # Exit: Croaks with a message for a bad pattern or a missing column.
4377 # Notes: Columns are named as a lookup's are: the group's own name unless a
4378 # column has it, then <extraction>_<group>. A capture is cut to 200
4379 # characters (Calc::capture_of).
4380 861 ▶sub _apply_extractions :Protected ($self, $records, $cols, $spec_key) {
Subroutine _apply_extractions: called CC=19 SCAR=29.5
4381 861 ▶ return unless length($spec_key // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($spec_key // "") 8 853 861
Condition100%2/2
$spec_key // ""
l!l
8601
MC/DC100%1/1
$spec_key // ""
$spec_key
Truth table100%2/2
A
A= $spec_key
Aresult
11
00
4382 853 my $stored = $self->_read_models->{extractions}{$spec_key};
4383 853 ▶ return unless ref $stored eq 'HASH' && %$stored;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stored eq "HASH" and %$stored 849 4 853
Condition66%2/3
ref $stored eq "HASH" and %$stored
!ll&&!rl&&r
84904
MC/DC50%1/2
ref $stored eq "HASH" and %$stored
ref $stored eq "HASH" %$stored
Truth table66%2/3
A and B
A= ref $stored eq "HASH"B= %$stored
ABresult
0X0
100
111
4384  
4385 4 ▶ my $written = $self->stash('_extract_written') // {};
Condition50%1/2
$self->stash("_extract_written") // {}
l!l
04
MC/DC0%0/1
$self->stash("_extract_written") // {}
$self->stash("_extract_written")
Truth table50%1/2
A
A= $self->stash("_extract_written")
Aresult
11
00
4386 4 my %taken = map { $_ => 1 } grep { !$written->{$_} } @$cols;
4387 4 for my $name (sort keys %$stored) {
4388 4 my $def = $stored->{$name};
4389 4 ▶ next unless ref $def eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def eq "HASH" 1 3 4
4390 3 my ($compiled, $fields) = $self->_compile_extraction($name, $def);
4391 3 ▶ my $col = $def->{col} // '';
Condition50%1/2
$def->{"col"} // ""
l!l
30
MC/DC0%0/1
$def->{"col"} // ""
$def->{"col"}
Truth table50%1/2
A
A= $def->{"col"}
Aresult
11
00
4392 croak $self->_i18n('error_extract_col_unknown', $name, $col)
4393 3 ▶ unless $taken{$col} || $written->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $taken{$col} or $written->{$col} 1 2 3
Condition66%2/3
$taken{$col} or $written->{$col}
l!l&&r!l&&!r
201
MC/DC50%1/2
$taken{$col} or $written->{$col}
$taken{$col} $written->{$col}
Truth table66%2/3
A or B
A= $taken{$col}B= $written->{$col}
ABresult
1X1
011
000
4394  
4395 2 my @out;
4396 2 for my $f (@$fields) {
4397 2 ▶ my $as = $taken{$f} ? "${name}_$f" : $f;
Branch: 1 branch
BranchTrueFalseTotal
$taken{$f} ? : 1 1 2
4398 2 ▶ croak $self->_i18n('error_extract_name_taken', $name, $f, $as) if $taken{$as};
Branch: 1 branch
BranchTrueFalseTotal
if $taken{$as} 1 1 2
4399 1 $taken{$as} = 1;
4400 1 push @out, [ $f, $as ];
4401 }
4402  
4403 # The groups were checked against the pattern, so undef is a row it
4404 # does not match, and a group that took no part is ''.
4405 1 for my $i (0 .. $#$records) {
4406 3 my $v = $records->[$i]{$col};
4407 3 for my $pair (@out) {
4408 3 my $got = Database::BI::Calc::capture_of($compiled, $v, $pair->[0]);
4409 3 ▶ $records->[$i] = _extend_row($records->[$i], $pair->[1], $got // '(unparseable)');
Condition100%2/2
$got // "(unparseable)"
l!l
21
MC/DC100%1/1
$got // "(unparseable)"
$got
Truth table100%2/2
A
A= $got
Aresult
11
00
4410 }
4411 }
4412  
4413 1 my %have = map { $_ => 1 } @$cols;
4414 1 push @$cols, grep { !$have{$_} } map { $_->[1] } @out;
4415 1 ▶ $self->stash('_virtual_columns' => {}) unless $self->stash('_virtual_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("_virtual_columns") 1 0 1
4416 1 for my $pair (@out) {
4417 1 $self->stash('_virtual_columns')->{ $pair->[1] } = 1;
4418 1 $written->{ $pair->[1] } = 1;
4419 }
4420 }
4421 2 $self->stash('_extract_written' => $written);
4422 2 return;
4423 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4424  
4425 # _extend_row($row, $name, $value) -> $row | \%copy
4426 #
4427 # Purpose: Write a virtual column into a row that came out of a source. A row
4428 # from a SQL-backed source arrives with its key set locked (the backend
4429 # locks it so that reading a column that is not there fails loudly),
4430 # and a locked row cannot take a new key in place: the assignment raises
4431 # "Attempt to access disallowed key". Such a row is replaced by an
4432 # unlocked copy of itself carrying the new cell, which the rest of the
4433 # pipeline reads exactly as it reads any other row.
4434 # Entry: $row -- the row hashref; $name -- the column to add; $value -- its value.
4435 # Exit: $row itself when it can take the key (already present, or writable),
4436 # otherwise a new hashref.
4437 # Notes: Only a *new* key is a problem -- writing a key a locked row already
4438 # has is allowed -- so the common case (a CSV-backed source, and any
4439 # repeated evaluation) copies nothing.
4440 46 ▶sub _extend_row {
Subroutine _extend_row: called CC=3 SCAR=11.0
4441 46 my ($row, $name, $value) = @_;
4442 46 ▶ return $row if ref $row ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "HASH" 2 44 46
4443 44 require Hash::Util;
4444 44 ▶ unless (Hash::Util::hashref_locked($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Hash::Util::hashref_locked($row)) 41 3 44
4445 41 $row->{$name} = $value;
4446 41 return $row;
4447 }
4448 3 return { %$row, $name => $value };
4449 }
4450  
4451 # _write_calc_column($self, $records, \@cols, $name, $calc)
4452 #
4453 # Purpose: Evaluate one compiled calculated field over the rows and write it as
4454 # a column, recording it as virtual so _grouped_totals skips the SQL
4455 # push path. \@cols gains the name if it is new.
4456 13 ▶sub _write_calc_column :Protected ($self, $records, $cols, $name, $calc) {
Subroutine _write_calc_column: called CC=7 SCAR=19.5
4457 13 my $undef_count = 0;
4458 13 for my $i (0 .. $#$records) {
4459 26 my $row = $records->[$i];
4460 26 my $val = $calc->evaluate($row);
4461 26 ▶ $undef_count++ unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 22 26
4462 # Store '(unparseable)' rather than undef so that pivot/bar
4463 # grouping (which skips empty-string keys) keeps these rows in a
4464 # visible '(unparseable)' bucket rather than silently dropping them.
4465 # See _extend_row for why the row may have to be replaced.
4466 26 ▶ $records->[$i] = _extend_row($row, $name,
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 22 4 26
4467 defined $val ? $val : '(unparseable)');
4468 }
4469 13 ▶ if ($undef_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef_count) 2 11 13
4470 2 $self->app->log->warn($self->_i18n('warn_calc_undef', $name, $undef_count));
4471 }
4472 13 ▶ push @$cols, $name unless grep { $_ eq $name } @$cols;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @$cols 12 1 13
4473 13 ▶ $self->stash('_virtual_columns' => {}) unless $self->stash('_virtual_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("_virtual_columns") 9 4 13
4474 13 $self->stash('_virtual_columns')->{$name} = 1;
4475 13 return;
4476 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4477  
4478 # _apply_held_calc_fields($self, $records, \@columns, $card) -> ($records, \@columns)
4479 #
4480 # Purpose: Stage 7 of the pipeline: the calculated fields stage 4 held back,
4481 # now that the cross-row columns they name exist. Each was checked
4482 # against those names when it was held, so a failure here is reported
4483 # as any other compile failure is.
4484 # Entry: $card is true for a canvas card. A canvas holds a field for any
4485 # card's plan, so a field that does not compile against this card's
4486 # columns belongs to another card, and this card goes without it.
4487 # Exit: The rows and columns, extended. The list is a new array, and both
4488 # come back as they arrived when nothing is held.
4489 3 ▶sub _apply_held_calc_fields :Protected ($self, $records, $columns, $card = 0) {
Subroutine _apply_held_calc_fields: called CC=9 SCAR=22.5
4490 3 my $held = $self->stash('_held_calc_fields');
4491 3 ▶ return ($records, $columns) unless $held && @$held;
Branch: 1 branch
BranchTrueFalseTotal
unless $held and @$held 1 2 3
Condition66%2/3
$held and @$held
!ll&&!rl&&r
102
MC/DC50%1/2
$held and @$held
$held @$held
Truth table66%2/3
A and B
A= $heldB= @$held
ABresult
0X0
100
111
4492  
4493 2 require Database::BI::Calc;
4494 0 ▶ my @cols = @{ $columns // [ sort keys %{ $records->[0] // {} } ] };
Condition25%1/4
$columns // [sort(keys %{$records->[0] // {};})]
l!l
20
$records->[0] // {}
l!l
00
MC/DC0%0/1
$columns // [sort(keys %{$records->[0] // {};})]
$columns
MC/DC0%0/1
$records->[0] // {}
$records->[0]
Truth table50%1/2
A
A= $columns
Aresult
11
00
Truth table0%0/2
A
A= $records->[0]
Aresult
11
00
4495 2 for my $cs (@$held) {
4496 my $calc = Database::BI::Calc->new(
4497 expr => $cs->{expr},
4498 columns => \@cols,
4499 params => $cs->{params},
4500 4 );
4501 4 ▶ if ($calc->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc->has_error) 2 2 4
4502 2 ▶ next if $card;
Branch: 1 branch
BranchTrueFalseTotal
if $card 1 1 2
4503 1 croak $self->_i18n('error_calc_compile', $cs->{name}, $calc->error);
4504 }
4505 2 $self->_write_calc_column($records, \@cols, $cs->{name}, $calc);
4506 }
4507 1 return ($records, \@cols);
4508 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4509  
4510 # _cross_row_aliases(@specs) -> @names
4511 #
4512 # Purpose: The columns a list of cs= specs will add: the third field of each.
4513 # Only names are wanted, so a malformed spec simply has none; refusing
4514 # it is the work of _cross_row_specs_from.
4515 878 ▶sub _cross_row_aliases {
Subroutine _cross_row_aliases: called CC=4 SCAR=13.9
4516 878 ▶ return map { (split /:/, $_, 4)[2] // () } grep { defined && !ref && length } @_;
Condition88%8/9
(split(/:/u, $_, 4))[2] // ()
l!l&&r!l&&!r
8300
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
2338
defined $_ and not ref $_
!ll&&!rl&&r
1141
MC/DC0%0/2
(split(/:/u, $_, 4))[2] // ()
(split(/:/u, $_, 4))[2] ()
MC/DC100%3/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
Truth table66%2/3
A // B
A= (split(/:/u, $_, 4))[2]B= ()
ABresult
1X1
011
000
Truth table100%4/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
4517 }
4518  
4519 # _cross_row_written($self) -> @names
4520 #
4521 # Purpose: The columns this request writes at stages 6 and 7: the cs= aliases and
4522 # the calculated fields held back for them. A filter on one of these
4523 # has to run after them.
4524 16 ▶sub _cross_row_written {
Subroutine _cross_row_written: called CC=3 SCAR=11.0
4525 16 my ($self) = @_;
4526 return (
4527 16 ▶ _cross_row_aliases(@{ $self->every_param('cs') // [] }),
Condition50%1/2
$self->every_param("cs") // []
l!l
160
MC/DC0%0/1
$self->every_param("cs") // []
$self->every_param("cs")
Truth table50%1/2
A
A= $self->every_param("cs")
Aresult
11
00
4528 16 ▶ map { $_->{name} } @{ $self->stash('_held_calc_fields') // [] },
Condition100%2/2
$self->stash("_held_calc_fields") // []
l!l
151
MC/DC100%1/1
$self->stash("_held_calc_fields") // []
$self->stash("_held_calc_fields")
Truth table100%2/2
A
A= $self->stash("_held_calc_fields")
Aresult
11
00
4529 );
4530 }
4531  
4532 # _canvas_window_columns($analysis, $params, \@more) -> \@names
4533 #
4534 # Purpose: Every column a cross-row plan may add on this canvas: the page's cs=,
4535 # read from $params, and each card's own plan. \@more holds cards that
4536 # are not on the canvas yet: one being added, or being given a new plan.
4537 44 ▶sub _canvas_window_columns {
Subroutine _canvas_window_columns: called CC=8 SCAR=20.8
4538 44 my ($analysis, $params, $more) = @_;
4539 44 ▶ my @specs = @{ $params->every_param('cs') // [] };
Condition50%1/2
$params->every_param("cs") // []
l!l
440
MC/DC0%0/1
$params->every_param("cs") // []
$params->every_param("cs")
Truth table50%1/2
A
A= $params->every_param("cs")
Aresult
11
00
4540 44 ▶ for my $vis (@{ $analysis->{visuals} // [] }, @{ $more // [] }) {
Condition100%4/4
$analysis->{"visuals"} // []
l!l
422
$more // []
l!l
539
MC/DC100%1/1
$analysis->{"visuals"} // []
$analysis->{"visuals"}
MC/DC100%1/1
$more // []
$more
Truth table100%2/2
A
A= $analysis->{"visuals"}
Aresult
11
00
Truth table100%2/2
A
A= $more
Aresult
11
00
4541 next unless ref $vis eq 'HASH' && ref $vis->{cross_row} eq 'HASH'
4542 50 ▶ && ref $vis->{cross_row}{cs} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vis eq "HASH" and ref $vis->{"cross_row"} eq "HASH" and ref $vis->{"cross_row"}{"cs"} eq "ARRAY" 48 2 50
Condition100%6/6
ref $vis eq "HASH" and ref $vis->{"cross_row"} eq "HASH" and ref $vis->{"cross_row"}{"cs"} eq "ARRAY"
!ll&&!rl&&r
4712
ref $vis eq "HASH" and ref $vis->{"cross_row"} eq "HASH"
!ll&&!rl&&r
1463
MC/DC100%3/3
ref $vis eq "HASH" and ref $vis->{"cross_row"} eq "HASH" and ref $vis->{"cross_row"}{"cs"} eq "ARRAY"
ref $vis eq "HASH" ref $vis->{"cross_row"} eq "HASH" ref $vis->{"cross_row"}{"cs"} eq "ARRAY"
Truth table100%4/4
A and B and C
A= ref $vis eq "HASH"B= ref $vis->{"cross_row"} eq "HASH"C= ref $vis->{"cross_row"}{"cs"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
4543 2 push @specs, @{ $vis->{cross_row}{cs} };
4544 }
4545 44 return [ _cross_row_aliases(@specs) ];
4546 }
4547  
4548 # _canvas_cross_filter($self, $records, \@columns, \@specs) -> ($records, \@late)
4549 #
4550 # Purpose: Apply a canvas's cross-filter to a card's rows, except any spec on a
4551 # column a cross-row plan writes. That column does not exist yet, so
4552 # the spec comes back for _render_visual_html to apply after the pass.
4553 7 ▶sub _canvas_cross_filter :Protected ($self, $records, $columns, $specs) {
Subroutine _canvas_cross_filter: called CC=6 SCAR=17.9
4554 3 my %written = map { $_ => 1 }
4555 7 ▶ @{ $self->stash('_canvas_window_names') // [] },
Condition100%2/2
$self->stash("_canvas_window_names") // []
l!l
61
MC/DC100%1/1
$self->stash("_canvas_window_names") // []
$self->stash("_canvas_window_names")
Truth table100%2/2
A
A= $self->stash("_canvas_window_names")
Aresult
11
00
4556 7 ▶ map { $_->{name} } @{ $self->stash('_held_calc_fields') // [] };
Condition100%2/2
$self->stash("_held_calc_fields") // []
l!l
61
MC/DC100%1/1
$self->stash("_held_calc_fields") // []
$self->stash("_held_calc_fields")
Truth table100%2/2
A
A= $self->stash("_held_calc_fields")
Aresult
11
00
4557 7 delete @written{ @$columns };
4558 7 my @late;
4559 7 for my $spec (@$specs) {
4560 6 ▶ if ($written{ (split /:/, $spec, 2)[0] // '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written{(split(/:/u, $spec, 2))[0] // ""}) 2 4 6
Condition50%1/2
(split(/:/u, $spec, 2))[0] // ""
l!l
60
MC/DC0%0/1
(split(/:/u, $spec, 2))[0] // ""
(split(/:/u, $spec, 2))[0]
Truth table50%1/2
A
A= (split(/:/u, $spec, 2))[0]
Aresult
11
00
4561 2 push @late, $spec;
4562 2 next;
4563 }
4564 4 $records = _apply_filter_spec($records, $spec);
4565 }
4566 7 return ($records, \@late);
4567 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4568  
4569 # _calc_specs_of_request($self) -> @specs
4570 #
4571 # Purpose: The calculated fields among the request's c= params, leaving out the
4572 # files it stacks, which share that parameter.
4573 213 ▶sub _calc_specs_of_request :Protected ($self) {
Subroutine _calc_specs_of_request: called CC=2 SCAR=7.1
4574 213 ▶ return grep { !/\A(?:path|table|url|redis|mariadb):/i } @{ $self->every_param('c') // [] };
Condition50%1/2
$self->every_param("c") // []
l!l
2130
MC/DC0%0/1
$self->every_param("c") // []
$self->every_param("c")
Truth table50%1/2
A
A= $self->every_param("c")
Aresult
11
00
4575 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4576  
4577 # _page_columns($self, $source, $records, $spec_key) -> ($records, \@columns) | ()
4578 #
4579 # Purpose: The columns of a table page from _fetch_page, with the calculated
4580 # fields and any span= bucket drawn on its rows. A page is one slice,
4581 # so _fetch_page has already run them over the whole set if a filter or
4582 # a window needed them; this pass is what puts them on the page.
4583 # Entry: $spec_key files the stored fields: the page's left_spec.
4584 # Exit: The rows and columns; or the empty list after rendering a 400, and the
4585 # caller returns.
4586 166 ▶sub _page_columns :Protected ($self, $source, $records, $spec_key) {
Subroutine _page_columns: called CC=7 SCAR=19.6
4587 166 my @columns = _get_columns($source, $records);
4588 166 my %seen_col = map { $_ => 1 } @columns;
4589 # Stage 6 wrote its columns onto the rows before the page was sliced, so the
4590 # list is what makes the table show them, and lets a field below read one.
4591 0 ▶ push @columns, grep { !$seen_col{$_}++ } @{ $self->stash('_cross_row_columns') // [] };
Condition50%1/2
$self->stash("_cross_row_columns") // []
l!l
0166
MC/DC0%0/1
$self->stash("_cross_row_columns") // []
$self->stash("_cross_row_columns")
Truth table50%1/2
A
A= $self->stash("_cross_row_columns")
Aresult
11
00
4592 # Nothing to draw a field from: a source with no declared columns and no rows.
4593 166 ▶ return ($records, \@columns) unless @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 1 165 166
4594  
4595 # span= adds a bucket column to the table rather than grouping it: a table has
4596 # no dimension to bucket *into*, so the reader gets the column and the raw
4597 # value beside it. See _span_calc_spec.
4598 165 my $span = $self->_span_calc_spec(\@columns, ());
4599 165 ▶ if ($span && $span->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span and $span->{"error"}) 5 160 165
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
16005
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
4600 5 $self->render(text => $span->{error}, status => 400);
4601 5 return ();
4602 }
4603  
4604 160 my ($calc_records, $calc_columns) = eval {
4605 $self->_calc_for_page($records, \@columns, {
4606 spec_key => $spec_key,
4607 160 ▶ ($span ? (calc => $span->{calc}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 160 160
4608 })
4609 };
4610 160 ▶ unless ($calc_records) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calc_records) 1 159 160
4611 # Every location croak added on the way up, not only the last.
4612 1 $self->render(text => _tidy_error($@), status => 400);
4613 1 return ();
4614 }
4615 159 return ($calc_records, $calc_columns);
4616 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4617  
4618 # _calc_for_page($self, $records, \@columns, \%extra) -> ($records, \@columns)
4619 #
4620 # Purpose: _apply_calc_fields for a table page, which must still be drawn when a
4621 # definition saved for its data cannot be applied -- a lookup file that
4622 # is gone, a column an extraction or a field reads that the file no
4623 # longer has. Every chart and export of that data refuses with the
4624 # reason, and the panels that mend a definition are on the table page,
4625 # so a page that refused too would leave the reader no way back.
4626 # Entry: As _apply_calc_fields; $extra->{spec_key} files the definitions.
4627 # Exit: As _apply_calc_fields. When the first pass croaks and a second one
4628 # without the saved fields, lookups and extractions succeeds, returns
4629 # that, with stash stored_error holding the first pass's reason and
4630 # _skip_stored set for the rest of the request. Otherwise croaks with
4631 # the first pass's error, so a mistake in the request's own c= is the
4632 # 400 it always was.
4633 # Side Effects: The first pass runs on a copy of the rows when anything is
4634 # saved, so a pass that failed half way leaves nothing on them.
4635 177 ▶sub _calc_for_page :Protected ($self, $records, $columns, $extra) {
Subroutine _calc_for_page: called CC=9 SCAR=23.0
4636 177 ▶ my $stored = !$self->stash('_skip_stored') && $self->_has_stored_columns($extra->{spec_key});
Condition66%2/3
!$self->stash('_skip_stored') && $self->_has_stored_columns($extra->{'spec_key'})
!ll&&!rl&&r
01734
MC/DC50%1/2
!$self->stash('_skip_stored') && $self->_has_stored_columns($extra->{'spec_key'})
!$self->stash('_skip_stored') $self->_has_stored_columns($extra->{'spec_key'})
Truth table66%2/3
not(A) && B
A= !$self->stash('_skip_stored')B= $self->_has_stored_columns($extra->{'spec_key'})
ABresult
0X0
100
111
4637 177 ▶ my $rows = $stored ? [ map { ref $_ eq 'HASH' ? { %$_ } : $_ } @$records ] : $records;
Branch: 2 branches
BranchTrueFalseTotal
$stored ? : 4 173 177
ref $_ eq 'HASH' ? : 21 0 21
4638 177 my @got = eval { $self->_apply_calc_fields($rows, $columns, $extra) };
4639 177 ▶ return @got if @got && $got[0];
Branch: 1 branch
BranchTrueFalseTotal
if @got and $got[0] 172 5 177
Condition66%2/3
@got and $got[0]
!ll&&!rl&&r
50172
MC/DC50%1/2
@got and $got[0]
@got $got[0]
Truth table66%2/3
A and B
A= @gotB= $got[0]
ABresult
0X0
100
111
4640 5 my $error = $@;
4641 5 ▶ croak $error unless $stored;
Branch: 1 branch
BranchTrueFalseTotal
unless $stored 2 3 5
4642  
4643 3 $self->stash('_skip_stored' => 1);
4644 3 @got = eval { $self->_apply_calc_fields($records, $columns, $extra) };
4645 3 ▶ unless (@got && $got[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@got and $got[0]) 0 3 3
Condition33%1/3
@got and $got[0]
!ll&&!rl&&r
003
MC/DC0%0/2
@got and $got[0]
@got $got[0]
Truth table33%1/3
A and B
A= @gotB= $got[0]
ABresult
0X0
100
111
4646 0 $self->stash('_skip_stored' => 0);
4647 0 croak $error;
4648 }
4649 3 $self->stash(stored_error => _tidy_error($error));
4650 3 return @got;
4651 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4652  
4653 # _has_stored_columns($self, $spec_key) -> $bool
4654 #
4655 # Purpose: Whether anything saved for $spec_key adds columns that can fail: a
4656 # calculated field, a lookup (its own, or another's through lk=) or an
4657 # extraction.
4658 # Entry: $spec_key may be undef, when the request's l= is the key.
4659 # Exit: True or false.
4660 182 ▶sub _has_stored_columns :Protected ($self, $spec_key) {
Subroutine _has_stored_columns: called CC=9 SCAR=22.3
4661 182 ▶ $spec_key = canonical_spec($spec_key // $self->param('l') // '');
Condition60%3/5
($spec_key // $self->param("l")) // ""
l!l
1820
$spec_key // $self->param("l")
l!l&&r!l&&!r
18110
MC/DC0%0/2
($spec_key // $self->param("l")) // ""
$spec_key $self->param("l")
Truth table66%2/3
A // B
A= $spec_keyB= $self->param("l")
ABresult
1X1
011
000
4662 182 ▶ return 1 if @{ $self->every_param('lk') // [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->every_param("lk") // [];} 4 178 182
Condition50%1/2
$self->every_param("lk") // []
l!l
1820
MC/DC0%0/1
$self->every_param("lk") // []
$self->every_param("lk")
Truth table50%1/2
A
A= $self->every_param("lk")
Aresult
11
00
4663 178 ▶ return 0 unless length $spec_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec_key 1 177 178
4664 177 my $models = $self->_read_models;
4665 177 for my $kind (qw(fields lookups extractions)) {
4666 525 my $set = $models->{$kind}{$spec_key};
4667 525 ▶ return 1 if ref $set eq 'HASH' && %$set;
Branch: 1 branch
BranchTrueFalseTotal
if ref $set eq "HASH" and %$set 3 522 525
Condition66%2/3
ref $set eq "HASH" and %$set
!ll&&!rl&&r
52203
MC/DC50%1/2
ref $set eq "HASH" and %$set
ref $set eq "HASH" %$set
Truth table66%2/3
A and B
A= ref $set eq "HASH"B= %$set
ABresult
0X0
100
111
4668 }
4669 174 return 0;
4670 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4671  
4672 # _apply_calc_fields($self, $records, \@columns, \%extra) -> ($records, \@columns)
4673 #
4674 # Reads ?c=<name>:<expression> params (those NOT matching a path:/table:/url:/redis:/mariadb:
4675 # source spec) and compiles each via Database::BI::Calc. Evaluates per row,
4676 # appending the virtual column to each row hashref. Croaks on compile error
4677 # (caller renders 400); runtime undef values are logged at warn level.
4678 # Later ?c= specs may reference names defined by earlier ones (evaluated in order).
4679 #
4680 # Optional \%extra keys:
4681 # params => \%hash -- named parameter values bound from the URL (?p.name=val)
4682 # passed to Calc->new so expressions may reference them.
4683 # spec_key => $str -- override the l= spec key used to look up stored calc
4684 # fields (canvas context where ?l= is absent).
4685 # calc => \@specs -- additional ?c=-style specs, for a canvas whose query
4686 # string arrived with the request rather than as the
4687 # request's own parameters.
4688 # window_columns => \@names -- the columns the cross-row pass will add, for a
4689 # canvas, whose plans are not the request's own cs=.
4690 #
4691 # A field that needs a cross-row column cannot run yet: stage 6 comes after the
4692 # filters this stage feeds. So a field that fails to compile, and would compile
4693 # once those columns exist, is held back for _apply_held_calc_fields. Any other
4694 # failure is still the error it always was, a mistyped name included, so holding
4695 # cannot hide a mistake, and with no cs= nothing is ever held.
4696 874 ▶sub _apply_calc_fields :Protected ($self, $records, $columns, $extra = undef) {
Subroutine _apply_calc_fields: called CC=41 SCAR=37.7
4697 874 ▶ my $bound_params = ($extra // {})->{params} // {};
Condition100%4/4
+($extra // {})->{"params"} // {}
l!l
43831
$extra // {}
l!l
223651
MC/DC100%1/1
+($extra // {})->{"params"} // {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
4698 874 ▶ my $spec_key_override = ($extra // {})->{spec_key};
Condition100%2/2
$extra // {}
l!l
223651
MC/DC100%1/1
$extra // {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
4699 874 ▶ my $extra_calc = ($extra // {})->{calc} // [];
Condition100%4/4
+($extra // {})->{"calc"} // []
l!l
26848
$extra // {}
l!l
223651
MC/DC100%1/1
+($extra // {})->{"calc"} // []
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
4700  
4701 # Normalise date columns before evaluating expressions so that date
4702 # functions (YEAR, MONTH, etc.) see ISO-8601 values even when the source
4703 # file uses a non-ISO format (e.g. M/D/YYYY).
4704 874 ▶ my $date_spec = defined $spec_key_override
Branch: 1 branch
BranchTrueFalseTotal
defined $spec_key_override ? : 222 652 874
Condition100%2/2
$self->param("l") // ""
l!l
6457
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
4705 ? $spec_key_override : ($self->param('l') // '');
4706 # A canvas dataset's spec comes from the stored canvas, not the request,
4707 # so the hook that spells the request's specs one way has not seen it.
4708 874 $date_spec = canonical_spec($date_spec);
4709 874 $self->_apply_date_normalization($records, $columns, $date_spec);
4710  
4711 # Stage 3: the stored lookups, so that a field, a filter, a bucket or a window
4712 # can read a looked-up column. They join the column list from here on.
4713 # _skip_stored is set by _calc_for_page when one of them cannot be applied:
4714 # the table page is drawn without them, so its panels can mend them.
4715 874 my $skip_stored = $self->stash('_skip_stored');
4716 874 my @cols = @{$columns};
4717 874 ▶ unless ($skip_stored) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_stored) 871 3 874
4718 871 $self->_apply_lookups($records, \@cols, $date_spec);
4719 856 $self->_apply_extractions($records, \@cols, $date_spec);
4720 }
4721  
4722 # Build calc spec list: stored fields from models.json first (lower priority),
4723 # then URL ?c= params (higher priority -- they override stored fields of the
4724 # same name so the user can test a new expression without permanently saving).
4725 859 my (@calc_specs, %seen_calc);
4726 {
4727 859 my $sk = $date_spec;
4728 859 ▶ if (length $sk && !$skip_stored) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sk and not $skip_stored) 849 10 859
Condition100%3/3
length $sk and not $skip_stored
!ll&&!rl&&r
73849
MC/DC100%2/2
length $sk and not $skip_stored
length $sk not $skip_stored
Truth table100%3/3
A and not(B)
A= length $skB= not $skip_stored
ABresult
0X0
100
111
4729 849 ▶ my $stored = ($self->_read_models)->{fields}{$sk} // {};
Condition100%2/2
$self->_read_models->{"fields"}{$sk} // {}
l!l
2847
MC/DC100%1/1
$self->_read_models->{"fields"}{$sk} // {}
$self->_read_models->{"fields"}{$sk}
Truth table100%2/2
A
A= $self->_read_models->{"fields"}{$sk}
Aresult
11
00
4730 849 for my $n (sort keys %{$stored}) {
4731 3 push @calc_specs, { name => $n, expr => $stored->{$n} };
4732 3 $seen_calc{$n} = $#calc_specs;
4733 }
4734 }
4735 }
4736 859 ▶ for my $spec (@{ $self->every_param('c') // [] }, @$extra_calc) {
Condition50%1/2
$self->every_param("c") // []
l!l
8590
MC/DC0%0/1
$self->every_param("c") // []
$self->every_param("c")
Truth table50%1/2
A
A= $self->every_param("c")
Aresult
11
00
4737 # Skip source specs handled by _run_pipeline_source.
4738 40 ▶ next if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /\A(?:path:|table:|url:|redis:|mariadb:)/iu 11 29 40
4739 29 my ($name, $expr) = ($spec =~ /\A([A-Za-z_][A-Za-z0-9_]*):(.+)\z/s);
4740 29 ▶ next unless defined $name && defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $expr 15 14 29
Condition66%2/3
defined $name and defined $expr
!ll&&!rl&&r
15014
MC/DC50%1/2
defined $name and defined $expr
defined $name defined $expr
Truth table66%2/3
A and B
A= defined $nameB= defined $expr
ABresult
0X0
100
111
4741 14 ▶ if (exists $seen_calc{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen_calc{$name}) { } 1 13 14
4742 1 $calc_specs[$seen_calc{$name}]{expr} = $expr; # URL overrides stored
4743 } else {
4744 13 push @calc_specs, { name => $name, expr => $expr };
4745 13 $seen_calc{$name} = $#calc_specs;
4746 }
4747 }
4748  
4749 my @window = $extra && $extra->{window_columns}
4750 42 ? @{ $extra->{window_columns} }
4751 859 ▶ : _cross_row_aliases(@{ $self->every_param('cs') // [] });
Branch: 1 branch
BranchTrueFalseTotal
$extra && $extra->{'window_columns'} ? : 42 817 859
Condition80%4/5
$extra and $extra->{"window_columns"}
!ll&&!rl&&r
63817942
$self->every_param("cs") // []
l!l
8170
MC/DC100%2/2
$extra and $extra->{"window_columns"}
$extra $extra->{"window_columns"}
MC/DC0%0/1
$self->every_param("cs") // []
$self->every_param("cs")
Truth table100%3/3
A and B
A= $extraB= $extra->{"window_columns"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->every_param("cs")
Aresult
11
00
4752 859 my @held;
4753  
4754 859 ▶ if (@calc_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@calc_specs) 14 845 859
4755 14 require Database::BI::Calc;
4756 14 for my $cs (@calc_specs) {
4757 16 my $name = $cs->{name};
4758 my $calc = Database::BI::Calc->new(
4759 expr => $cs->{expr},
4760 16 columns => \@cols,
4761 params => $bound_params,
4762 );
4763 16 ▶ if ($calc->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc->has_error) 8 8 16
4764 # The names of fields already held count too, so that a field
4765 # can build on one that is waiting.
4766 my $later = @window && Database::BI::Calc->new(
4767 expr => $cs->{expr},
4768 0 ▶ columns => [ @cols, @window, map { $_->{name} } @held ],
Condition33%1/3
@window && 'Database::BI::Calc'->new('expr', $cs->{'expr'}, 'columns', [@cols, @window, map({$_->{'name'};} @held)], 'params', $bound_params)
!ll&&!rl&&r
800
MC/DC0%0/2
@window && 'Database::BI::Calc'->new('expr', $cs->{'expr'}, 'columns', [@cols, @window, map({$_->{'name'};} @held)], 'params', $bound_params)
@window 'Database::BI::Calc'->new('expr', $cs->{'expr'}, 'columns', [@cols, @window, map({$_->{'name'};} @held)], 'params', $bound_params)
Truth table33%1/3
A && B
A= @windowB= 'Database::BI::Calc'->new('expr', $cs->{'expr'}, 'columns', [@cols, @window, map({$_->{'name'};} @held)], 'params', $bound_params)
ABresult
0X0
100
111
4769 params => $bound_params,
4770 );
4771 8 ▶ if ($later && !$later->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($later and not $later->has_error) 0 8 8
Condition33%1/3
$later and not $later->has_error
!ll&&!rl&&r
800
MC/DC0%0/2
$later and not $later->has_error
$later not $later->has_error
Truth table33%1/3
A and not(B)
A= $laterB= not $later->has_error
ABresult
0X0
100
111
4772 0 push @held, { %$cs, params => $bound_params };
4773 0 next;
4774 }
4775 8 croak $self->_i18n('error_calc_compile', $name, $calc->error);
4776 }
4777  
4778 8 $self->_write_calc_column($records, \@cols, $name, $calc);
4779 }
4780 }
4781 851 $self->stash('_held_calc_fields' => \@held);
4782  
4783 # Apply named value groups from models.json.
4784 # For each source column that has groups registered under the current l=
4785 # spec, add a virtual "${col}_group" column to every row. Each row's value
4786 # is mapped to its group label; values not in any group become '(other)'.
4787 # The spec is the one the stored fields use, so a table page, which names
4788 # its spec rather than taking l=, shows the column its charts can draw.
4789 {
4790 851 my $spec_key = $date_spec;
4791 851 ▶ if (length $spec_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec_key) 845 6 851
4792 845 my $models = $self->_read_models;
4793 845 my $grps = $models->{groups}{$spec_key};
4794 845 ▶ if ($grps && %{$grps}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grps and %{$grps;}) 1 844 845
Condition66%2/3
$grps and %{$grps;}
!ll&&!rl&&r
84401
MC/DC50%1/2
$grps and %{$grps;}
$grps %{$grps;}
Truth table66%2/3
A and B
A= $grpsB= %{$grps;}
ABresult
0X0
100
111
4795 1 my %col_set = map { $_ => 1 } @cols;
4796 1 for my $col (sort keys %{$grps}) {
4797 1 ▶ next unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 0 1 1
4798 # Build reverse map: value -> group label.
4799 1 my %val2group;
4800 1 for my $gname (keys %{$grps->{$col}}) {
4801 2 for my $v (@{$grps->{$col}{$gname}}) {
4802 2 $val2group{$v} = $gname;
4803 }
4804 }
4805 1 my $vcol = "${col}_group";
4806 1 for my $i (0 .. $#$records) {
4807 2 my $row = $records->[$i];
4808 2 ▶ my $v = $row->{$col} // '';
Condition50%1/2
$row->{$col} // ""
l!l
20
MC/DC0%0/1
$row->{$col} // ""
$row->{$col}
Truth table50%1/2
A
A= $row->{$col}
Aresult
11
00
4809 $records->[$i] = _extend_row(
4810 2 ▶ $row, $vcol, $val2group{$v} // '(other)');
Condition100%2/2
$val2group{$v} // "(other)"
l!l
11
MC/DC100%1/1
$val2group{$v} // "(other)"
$val2group{$v}
Truth table100%2/2
A
A= $val2group{$v}
Aresult
11
00
4811 }
4812 1 ▶ push @cols, $vcol unless grep { $_ eq $vcol } @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $vcol;} @cols 1 0 1
4813 # Record group column as virtual.
4814 1 ▶ $self->stash('_virtual_columns' => {}) unless $self->stash('_virtual_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("_virtual_columns") 1 0 1
4815 1 $self->stash('_virtual_columns')->{$vcol} = 1;
4816 }
4817 }
4818 }
4819 }
4820  
4821 851 return ($records, \@cols);
4822 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4823  
4824 # ---------------------------------------------------------------------------
4825 # Calculated-field model storage (.bi/models.json)
4826 # ---------------------------------------------------------------------------
4827  
4828 # _canonical_path($path) -> $string
4829 #
4830 # The absolute, symlink-resolved form of a path, for the data_dir guard. Two
4831 # spellings of one directory have to compare equal, or the guard can be walked
4832 # around: a model_dir reached through a symlink is still inside the directory it
4833 # points at, and a data_dir that is itself a symlink is still where the user
4834 # keeps their data. Cwd::realpath resolves the whole path when it exists; a
4835 # store directory usually does not exist yet -- deciding whether to create it is
4836 # what the guard is for -- so the deepest ancestor that does exist is resolved
4837 # and the names below it are re-attached.
4838 12232 ▶sub _canonical_path {
Subroutine _canonical_path: called CC=9 SCAR=22.4
4839 12232 my ($path) = @_;
4840  
4841 12232 ▶ my $abs = eval { $path->to_abs } // $path;
Condition33%1/3
eval { do { $path->to_abs } } // $path
l!l&&r!l&&!r
1223200
MC/DC0%0/2
eval { do { $path->to_abs } } // $path
eval { do { $path->to_abs } } $path
Truth table33%1/3
A // B
A= eval { do { $path->to_abs } }B= $path
ABresult
1X1
011
000
4842 # Cwd::realpath on Windows does not follow symlinks: it gives undef for a
4843 # path through one, which would send the loop below to the link's parent
4844 # and keep the link's own name. So the links are followed first.
4845 12232 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 3 12229 12232
4846 3 my $followed = _follow_links($abs->to_string);
4847 3 ▶ $abs = Mojo::File->new($followed) if defined $followed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $followed 3 0 3
4848 }
4849 12232 my @tail;
4850 12232 my $probe = $abs;
4851 12232 while (1) {
4852 12245 my $string = $probe->to_string;
4853 12245 ▶ my $real = -e $string ? realpath($string) : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $string ? : 12232 13 12245
4854 12245 ▶ if (defined $real && length $real) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $real and length $real) 12232 13 12245
Condition66%2/3
defined $real and length $real
!ll&&!rl&&r
13012232
MC/DC50%1/2
defined $real and length $real
defined $real length $real
Truth table66%2/3
A and B
A= defined $realB= length $real
ABresult
0X0
100
111
4855 12232 ▶ return File::Spec->canonpath(
Branch: 1 branch
BranchTrueFalseTotal
@tail ? : 10 12222 12232
4856 @tail ? Mojo::File->new($real, @tail)->to_string : $real
4857 );
4858 }
4859 13 my $parent = $probe->dirname;
4860 13 ▶ last if $parent->to_string eq $string; # the root: nothing left to resolve
Branch: 1 branch
BranchTrueFalseTotal
if $parent->to_string eq $string 0 13 13
4861 13 unshift @tail, $probe->basename;
4862 13 $probe = $parent;
4863 }
4864  
4865 # Nothing on the path exists at all, so the lexical absolute form is all
4866 # there is to compare.
4867 0 return File::Spec->canonpath($abs->to_string);
4868 }
4869  
4870 # _follow_links($path) -> $path | undef
4871 #
4872 # Purpose: $path with every symlink in it replaced by what it points at, one
4873 # component at a time, as realpath does where it can. For Windows,
4874 # whose realpath leaves links alone; it works the same anywhere, which
4875 # is how it is tested.
4876 # Entry: An absolute path.
4877 # Exit: The resolved absolute path; undef after 40 links, which is a loop.
4878 21 ▶sub _follow_links {
Subroutine _follow_links: called CC=10 SCAR=23.2
4879 21 my ($path) = @_;
4880 21 my ($vol, $dirs, $file) = File::Spec->splitpath($path);
4881 21 my @todo = grep { length } File::Spec->splitdir($dirs), $file;
4882 21 my @done;
4883 21 my $hops = 0;
4884 21 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 219 19 238
4885 219 my $part = shift @todo;
4886 219 ▶ next if $part eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "." 0 219 219
4887 219 ▶ if ($part eq '..') { pop @done; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq "..") 3 216 219
4888 216 my $here = File::Spec->catpath($vol, File::Spec->catdir(File::Spec->rootdir, @done, $part), '');
4889 216 ▶ if (-l $here) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $here) 102 114 216
4890 102 ▶ return undef if ++$hops > 40;
Branch: 1 branch
BranchTrueFalseTotal
if ++$hops > 40 2 100 102
4891 100 my $target = readlink $here;
4892 100 ▶ return undef unless defined $target && length $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and length $target 0 100 100
Condition33%1/3
defined $target and length $target
!ll&&!rl&&r
00100
MC/DC0%0/2
defined $target and length $target
defined $target length $target
Truth table33%1/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
4893 # Windows perls can give the NT form of an absolute target,
4894 # \??\C:\dir, or the long-path form \\?\C:\dir.
4895 100 $target =~ s{\A(?:\\\?\?\\|\\\\\?\\)}{};
4896 100 my ($tvol, $tdirs, $tfile) = File::Spec->splitpath($target);
4897 100 ▶ if (File::Spec->file_name_is_absolute($target)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($target)) 12 88 100
4898 12 ▶ $vol = $tvol if length $tvol;
Branch: 1 branch
BranchTrueFalseTotal
if length $tvol 0 12 12
4899 12 @done = ();
4900 }
4901 100 unshift @todo, grep { length } File::Spec->splitdir($tdirs), $tfile;
4902 100 next;
4903 }
4904 114 push @done, $part;
4905 }
4906 19 return File::Spec->catpath($vol, File::Spec->catdir(File::Spec->rootdir, @done), '');
4907 }
4908  
4909 # canonical_spec($spec) -> $spec
4910 #
4911 # Purpose: The one spelling of a path: spec that the model store files things
4912 # under. /open names its file by its realpath, so a definition saved,
4913 # or an extract built, under a path reached through a symlink (macOS's
4914 # /var is /private/var) would never be found by the page it is for.
4915 # Entry: Any spec, or undef.
4916 # Exit: path:<realpath> when the spec is path:<file> and the file exists
4917 # locally; anything else -- another kind of spec, a remote /../host
4918 # path, a file that is not there, a name with a newline -- unchanged.
4919 2287 ▶sub canonical_spec {
Subroutine canonical_spec: called CC=9 SCAR=22.2
Pod documented
4920 2287 my ($spec) = @_;
4921 2287 ▶ return $spec unless defined $spec && $spec =~ m{\Apath:(?!/\.\./)([^\n]+)\z};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and $spec =~ m[\Apath:(?!/\.\./)([^\n]+)\z]u 1963 324 2287
Condition100%3/3
defined $spec and $spec =~ m[\Apath:(?!/\.\./)([^\n]+)\z]u
!ll&&!rl&&r
11962324
MC/DC100%2/2
defined $spec and $spec =~ m[\Apath:(?!/\.\./)([^\n]+)\z]u
defined $spec $spec =~ m[\Apath:(?!/\.\./)([^\n]+)\z]u
Truth table100%3/3
A and B
A= defined $specB= $spec =~ m[\Apath:(?!/\.\./)([^\n]+)\z]u
ABresult
0X0
100
111
4922 324 my ($path, $internal) = _split_path($1);
4923 324 ▶ my $tail = defined $internal ? "#$internal" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 2 322 324
4924 # Windows' realpath leaves a symlink as it is, so a file reached through
4925 # one would be filed under a second key; follow the links first, as
4926 # _canonical_path does.
4927 324 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 1 323 324
4928 1 my $followed = eval { _follow_links(Mojo::File->new($path)->to_abs->to_string) };
4929 1 ▶ $path = $followed if defined $followed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $followed 1 0 1
4930 }
4931 324 ▶ return $spec unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 4 320 324
4932 320 my $real = eval { Mojo::File->new($path)->realpath->to_string };
4933 320 ▶ return (defined $real && length $real) ? "path:$real$tail" : $spec;
Branch: 1 branch
BranchTrueFalseTotal
defined $real && length $real ? : 320 0 320
Condition33%1/3
defined $real and length $real
!ll&&!rl&&r
00320
MC/DC0%0/2
defined $real and length $real
defined $real length $real
Truth table33%1/3
A and B
A= defined $realB= length $real
ABresult
0X0
100
111
4934 }
4935  
4936 # _inside_dir($path, $dir) -> $bool
4937 #
4938 # True when $path names $dir itself or something below it. abs2rel asks the
4939 # file system what the relationship between the two is, so a textual prefix
4940 # cannot make ".../data-archive" look like it is inside ".../data", and the
4941 # comparison is made with the platform's separator rather than a hard-coded
4942 # slash.
4943 6116 ▶sub _inside_dir {
Subroutine _inside_dir: called CC=9 SCAR=22.3
4944 6116 my ($path, $dir) = @_;
4945 6116 ▶ return 0 unless length $path && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path and length $dir 2 6114 6116
Condition100%3/3
length $path and length $dir
!ll&&!rl&&r
116114
MC/DC100%2/2
length $path and length $dir
length $path length $dir
Truth table100%3/3
A and B
A= length $pathB= length $dir
ABresult
0X0
100
111
4946 6114 ▶ return 1 if $path eq $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $dir 2 6112 6114
4947  
4948 6112 my $rel = eval { File::Spec->abs2rel($path, $dir) };
4949 6112 ▶ return 0 unless defined $rel && length $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel and length $rel 0 6112 6112
Condition33%1/3
defined $rel and length $rel
!ll&&!rl&&r
006112
MC/DC0%0/2
defined $rel and length $rel
defined $rel length $rel
Truth table33%1/3
A and B
A= defined $relB= length $rel
ABresult
0X0
100
111
4950 6112 ▶ return 0 if File::Spec->file_name_is_absolute($rel);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($rel) 0 6112 6112
4951 6112 ▶ return $rel eq '..' || $rel =~ m{\A\.\.[\\/]} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$rel eq '..' || $rel =~ m[\A\.\.[\\/]]u ? : 6093 19 6112
Condition100%3/3
$rel eq ".." or $rel =~ m[\A\.\.[\\/]]u
l!l&&r!l&&!r
1609219
MC/DC100%2/2
$rel eq ".." or $rel =~ m[\A\.\.[\\/]]u
$rel eq ".." $rel =~ m[\A\.\.[\\/]]u
Truth table100%3/3
A or B
A= $rel eq ".."B= $rel =~ m[\A\.\.[\\/]]u
ABresult
1X1
011
000
4952 }
4953  
4954 # _calc_models_path($self) -> $path_string_or_undef
4955 #
4956 # Returns the path to .bi/models.json (creating .bi/ on first call), or undef
4957 # when the write must be skipped (data_dir safety guard, mkdir failure).
4958 5692 ▶sub _calc_models_path :Protected ($self) {
Subroutine _calc_models_path: called CC=6 SCAR=18.0
4959 5692 my $app = $self->app;
4960 5692 my $home = $app->home;
4961 5692 ▶ my $cfg_dir = $app->config('model_dir') // '.bi';
Condition50%1/2
$app->config("model_dir") // ".bi"
l!l
56920
MC/DC0%0/1
$app->config("model_dir") // ".bi"
$app->config("model_dir")
Truth table50%1/2
A
A= $app->config("model_dir")
Aresult
11
00
4962 # Support absolute paths in model_dir (e.g. when tests redirect to a temp dir).
4963 5692 ▶ my $bi_dir = Mojo::File->new($cfg_dir)->is_abs
Branch: 1 branch
BranchTrueFalseTotal
'Mojo::File'->new($cfg_dir)->is_abs ? : 5692 0 5692
4964 ? Mojo::File->new($cfg_dir)
4965 : $home->child($cfg_dir);
4966 5692 my $data_dir = _data_dir_for($app);
4967  
4968 # A store inside data_dir is refused, not created: the file would sit beside
4969 # the user's data, in a directory they chose for something else. The test is
4970 # on canonical paths, so it holds however the two are spelled -- an absolute
4971 # path, a relative one, or either of them reached through a symlink.
4972 5692 ▶ if (_inside_dir(_canonical_path($bi_dir), _canonical_path($data_dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inside_dir(_canonical_path($bi_dir), _canonical_path($data_dir))) 2 5690 5692
4973 2 carp 'model_dir is inside data_dir; skipping model file write';
4974 2 return undef;
4975 }
4976 5690 ▶ unless (-d $bi_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $bi_dir) 2 5688 5690
4977 mkdir $bi_dir->to_string
4978 2 ▶ or do { $app->log->warn("Cannot create $bi_dir: $!"); return undef };
Branch: 1 branch
BranchTrueFalseTotal
mkdir $bi_dir->to_string or do { $app->log->warn("Cannot create ${bi_dir}: $!"); return undef } 1 1 2
4979 }
4980 5689 return $bi_dir->child('models.json')->to_string;
4981 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
4982  
4983 # _read_models($self) -> \%models
4984 #
4985 # Returns the parsed models hashref from .bi/models.json, or an empty scaffold.
4986 5627 ▶sub _read_models :Protected ($self) {
Subroutine _read_models: called CC=9 SCAR=22.0
4987 5627 my $path = $self->_calc_models_path;
4988 5627 my $scaffold = { version => 1, fields => {}, groups => {}, date_patterns => {}, lookups => {}, extractions => {}, accelerated => {} };
4989 5627 ▶ return $scaffold unless defined $path && -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and -f $path 5526 101 5627
Condition66%2/3
defined $path and -f $path
!ll&&!rl&&r
05526101
MC/DC50%1/2
defined $path and -f $path
defined $path -f $path
Truth table66%2/3
A and B
A= defined $pathB= -f $path
ABresult
0X0
100
111
4990 101 my $json = eval { Mojo::File->new($path)->slurp };
4991 101 ▶ return $scaffold unless $json && !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $json and not $@ 1 100 101
Condition66%2/3
$json and not $@
!ll&&!rl&&r
10100
MC/DC50%1/2
$json and not $@
$json not $@
Truth table66%2/3
A and not(B)
A= $jsonB= not $@
ABresult
0X0
100
111
4992 100 my $data = eval { decode_json($json) };
4993 100 ▶ return $scaffold unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 4 96 100
4994 # Callers go straight to ->{fields}{$spec}{$name}, so a hand-edited section of
4995 # the wrong shape would otherwise be a strict-refs crash on every page.
4996 96 for my $section (qw(fields groups date_patterns lookups extractions accelerated)) {
4997 576 ▶ $data->{$section} = {} unless ref $data->{$section} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{$section} eq "HASH" 246 330 576
4998 576 my $by_spec = $data->{$section};
4999 576 delete @{$by_spec}{ grep { ref $by_spec->{$_} ne 'HASH' } keys %$by_spec };
5000 }
5001 96 _canonical_store_keys($data->{$_}) for grep { ref $data->{$_} eq 'HASH' }
5002 qw(fields groups date_patterns lookups extractions accelerated geo);
5003 96 return $data;
5004 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5005  
5006 # _canonical_store_keys(\%by_spec) -> void
5007 #
5008 # Purpose: File every path: entry of one section of the model store under
5009 # canonical_spec of its key, so that a reader asking with the resolved
5010 # spec finds an entry written under a symlinked spelling, by a version
5011 # before specs were resolved or by hand. The next write persists it.
5012 # Notes: A geo key is <spec>|loc=<column>; only the spec part is resolved.
5013 # When both spellings are present the resolved one's names win.
5014 587 ▶sub _canonical_store_keys {
Subroutine _canonical_store_keys: called CC=7 SCAR=19.6
5015 587 my ($by_spec) = @_;
5016 587 for my $key (grep { /\Apath:/ } keys %$by_spec) {
5017 43 my ($spec, $rest) = split /\|/, $key, 2;
5018 43 ▶ my $new = canonical_spec($spec) . (defined $rest ? "|$rest" : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $rest ? : 1 42 43
5019 43 ▶ next if $new eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $new eq $key 41 2 43
5020 2 my $old = delete $by_spec->{$key};
5021 2 my $cur = $by_spec->{$new};
5022 2 ▶ $by_spec->{$new} = (ref $cur eq 'HASH' && ref $old eq 'HASH') ? { %$old, %$cur } : ($cur // $old);
Branch: 1 branch
BranchTrueFalseTotal
ref $cur eq 'HASH' && ref $old eq 'HASH' ? : 1 1 2
Condition50%3/6
ref $cur eq "HASH" and ref $old eq "HASH"
!ll&&!rl&&r
101
$cur // $old
l!l&&r!l&&!r
010
MC/DC50%1/2
ref $cur eq "HASH" and ref $old eq "HASH"
ref $cur eq "HASH" ref $old eq "HASH"
MC/DC0%0/2
$cur // $old
$cur $old
Truth table66%2/3
A and B
A= ref $cur eq "HASH"B= ref $old eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $curB= $old
ABresult
1X1
011
000
5023 }
5024 587 return;
5025 }
5026  
5027 # _write_models($self, \%models) -> $bool
5028 #
5029 # Persists models to .bi/models.json. Returns false when the store could not be
5030 # written - because the data_dir guard refused it, or because the write failed -
5031 # so a caller whose result is only an optimisation (the geocode cache: it
5032 # degrades to a per-request cache) can say so rather than pretend.
5033 46 ▶sub _write_models :Protected ($self, $models) {
Subroutine _write_models: called CC=5 SCAR=16.2
5034 46 my $path = $self->_calc_models_path;
5035 46 ▶ return 0 unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 46 46
5036 46 $self->_set_aside_corrupt_store($path);
5037 46 my $ok = eval { Mojo::File->new($path)->spurt(encode_json($models)); 1 };
5038 46 ▶ $self->app->log->warn("Cannot write $path: $@") if !$ok && $@;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $@ 3 43 46
Condition66%2/3
not $ok and $@
!ll&&!rl&&r
4303
MC/DC50%1/2
not $ok and $@
not $ok $@
Truth table66%2/3
not(A) and B
A= not $okB= $@
ABresult
0X0
100
111
5039 46 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 43 3 46
5040 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5041  
5042 # _set_aside_corrupt_store($self, $path) -> void
5043 #
5044 # Purpose: Keep a store that cannot be parsed from being overwritten. A reader
5045 # treats such a file as empty, so without this the next save would
5046 # replace every saved canvas or model with the one change it carries.
5047 # Entry: $path is the store about to be written; it need not exist.
5048 # Exit: A file holding anything but a JSON object is renamed to
5049 # <path>.corrupt.<time>, for its owner to repair.
5050 # Only a plain file: a folder (or anything else) of that name is left
5051 # where it is, and the write then fails as it should. Linux will not
5052 # read a folder, but the BSDs read its raw entries, which never parse,
5053 # so a folder was moved aside there and the write went ahead.
5054 # Side Effects: Renames the file and logs where it went.
5055 107 ▶sub _set_aside_corrupt_store :Protected ($self, $path) {
Subroutine _set_aside_corrupt_store: called CC=6 SCAR=18.1
5056 107 ▶ return unless -f $path && -s _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path and -s _ 47 60 107
Condition100%3/3
-f $path and -s _
!ll&&!rl&&r
46160
MC/DC100%2/2
-f $path and -s _
-f $path -s _
Truth table100%3/3
A and B
A= -f $pathB= -s _
ABresult
0X0
100
111
5057  
5058 60 my $json = eval { Mojo::File->new($path)->slurp };
5059 60 ▶ return unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 0 60 60
5060 60 ▶ return if ref(eval { decode_json($json) }) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref eval { do { decode_json($json) } } eq "HASH" 58 2 60
5061  
5062 2 my $aside = "$path.corrupt." . time();
5063 2 ▶ if (rename $path, $aside) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $path, $aside) { } 2 0 2
5064 2 $self->app->log->error("$path could not be parsed; kept it as $aside");
5065 } else {
5066 0 $self->app->log->error("$path could not be parsed, and could not be kept as $aside: $!");
5067 }
5068 2 return;
5069 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5070  
5071 # _analyses_path($self) -> $path_string_or_undef
5072 #
5073 # Returns the path to model_dir/analyses.json (creating model_dir on first call),
5074 # or undef when the write must be skipped (data_dir safety guard, mkdir failure).
5075 392 ▶sub _analyses_path :Protected ($self) {
Subroutine _analyses_path: called CC=7 SCAR=23.0
5076 392 my $app = $self->app;
5077 392 my $home = $app->home;
5078 392 ▶ my $cfg_dir = $app->config('model_dir') // '.bi';
Condition50%1/2
$app->config("model_dir") // ".bi"
l!l
3920
MC/DC0%0/1
$app->config("model_dir") // ".bi"
$app->config("model_dir")
Truth table50%1/2
A
A= $app->config("model_dir")
Aresult
11
00
5079 392 ▶ my $bi_dir = Mojo::File->new($cfg_dir)->is_abs
Branch: 1 branch
BranchTrueFalseTotal
'Mojo::File'->new($cfg_dir)->is_abs ? : 392 0 392
5080 ? Mojo::File->new($cfg_dir)
5081 : $home->child($cfg_dir);
5082 392 my $data_dir = _data_dir_for($app);
5083  
5084 # The same refusal as the model store, with its own message: see
5085 # _canonical_path for why the comparison is on resolved paths.
5086 392 ▶ if (_inside_dir(_canonical_path($bi_dir), _canonical_path($data_dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inside_dir(_canonical_path($bi_dir), _canonical_path($data_dir))) 1 391 392
5087 1 carp 'model_dir is inside data_dir; skipping analyses file write';
5088 1 return undef;
5089 }
5090 391 ▶ unless (-d $bi_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $bi_dir) 0 391 391
5091 mkdir $bi_dir->to_string
5092 0 ▶ or do { $app->log->warn("Cannot create $bi_dir: $!"); return undef };
Branch: 1 branch
BranchTrueFalseTotal
mkdir $bi_dir->to_string or do { $app->log->warn("Cannot create ${bi_dir}: $!"); return undef } 0 0 0
5093 }
5094 391 ▶ unless (-w $bi_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $bi_dir) 0 391 391
5095 0 carp 'model_dir is not writable; analysis storage disabled';
5096 0 return undef;
5097 }
5098 391 return $bi_dir->child('analyses.json')->to_string;
5099 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5100  
5101 # _read_analyses($self) -> \%analyses (keyed by id)
5102 306 ▶sub _read_analyses :Protected ($self) {
Subroutine _read_analyses: called CC=6 SCAR=18.0
5103 306 my $path = $self->_analyses_path;
5104 306 ▶ return {} unless defined $path && -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and -f $path 32 274 306
Condition66%2/3
defined $path and -f $path
!ll&&!rl&&r
032274
MC/DC50%1/2
defined $path and -f $path
defined $path -f $path
Truth table66%2/3
A and B
A= defined $pathB= -f $path
ABresult
0X0
100
111
5105 274 my $json = eval { Mojo::File->new($path)->slurp };
5106 274 ▶ return {} unless $json && !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $json and not $@ 1 273 274
Condition66%2/3
$json and not $@
!ll&&!rl&&r
10273
MC/DC50%1/2
$json and not $@
$json not $@
Truth table66%2/3
A and not(B)
A= $jsonB= not $@
ABresult
0X0
100
111
5107 273 my $data = eval { decode_json($json) };
5108 273 ▶ return {} unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 271 273
5109 271 return { map { $_ => _tidy_analysis($data->{$_}) } grep { ref $data->{$_} eq 'HASH' } keys %$data };
5110 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5111  
5112 # _tidy_analysis(\%doc) -> \%doc
5113 #
5114 # A stored canvas can hold anything: it may be hand-edited or older than a rule.
5115 # Every page that reads one goes straight to ->{visuals}[0]{encode}{cat}, so the
5116 # shape is put right here, once, and a part of the wrong shape is treated as
5117 # absent: a canvas with one bad part still shows the rest, not a 500. A visual
5118 # of the wrong shape becomes an empty one rather than going away, because a
5119 # visual's index is its identity in the layout and in /a/:id/visual/:n.
5120 262 ▶sub _tidy_analysis {
Subroutine _tidy_analysis: called CC=16 SCAR=27.7
5121 262 my $doc = $_[0];
5122  
5123 262 delete $doc->{$_} for grep { ref $doc->{$_} ne 'ARRAY' } qw(datasets visuals joins measures combines bookmarks layout hierarchies);
5124 262 delete $doc->{$_} for grep { ref $doc->{$_} ne 'HASH' } qw(fields params);
5125 262 delete $doc->{$_} for grep { ref $doc->{$_} } qw(name updated);
5126  
5127 262 ▶ if ($doc->{datasets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"datasets"}) 224 38 262
5128 224 ▶ $doc->{datasets} = [ grep { ref $_ eq 'HASH' && defined $_->{spec} && !ref $_->{spec} } @{ $doc->{datasets} } ];
Condition100%6/6
ref $_ eq 'HASH' && defined $_->{'spec'} && !ref($_->{'spec'})
!ll&&!rl&&r
21224
ref $_ eq "HASH" and defined $_->{"spec"}
!ll&&!rl&&r
11225
MC/DC100%3/3
ref $_ eq 'HASH' && defined $_->{'spec'} && !ref($_->{'spec'})
ref $_ eq "HASH" defined $_->{"spec"} !ref($_->{'spec'})
Truth table100%4/4
A and B && not(C)
A= ref $_ eq "HASH"B= defined $_->{"spec"}C= !ref($_->{'spec'})
ABCresult
0XX0
10X0
1100
1111
5129 224 for my $ds (@{ $doc->{datasets} }) {
5130 224 ▶ delete $ds->{filters} if ref $ds->{filters} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ds->{"filters"} ne "ARRAY" 223 1 224
5131 }
5132 }
5133 262 for my $key (grep { $doc->{$_} } qw(joins layout)) {
5134 20 $doc->{$key} = [ grep { ref $_ eq 'HASH' } @{ $doc->{$key} } ];
5135 }
5136 262 ▶ if ($doc->{visuals}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"visuals"}) 167 95 262
5137 167 ▶ $doc->{visuals} = [ map { ref $_ eq 'HASH' ? $_ : {} } @{ $doc->{visuals} } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 207 13 220
5138 167 _tidy_visual($_) for @{ $doc->{visuals} };
5139 }
5140 262 ▶ if ($doc->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"params"}) 13 249 262
5141 13 for my $def (values %{ $doc->{params} }) {
5142 14 ▶ $def = {} unless ref $def eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def eq "HASH" 12 2 14
5143 }
5144 }
5145 262 return $doc;
5146 }
5147  
5148 # _tidy_visual(\%visual) -> \%visual
5149 #
5150 # The same for one card, whose parts are read as blindly as the document's.
5151 220 ▶sub _tidy_visual {
Subroutine _tidy_visual: called CC=4 SCAR=13.9
5152 220 my $vis = $_[0];
5153  
5154 220 delete $vis->{$_} for grep { ref $vis->{$_} ne 'HASH' } qw(encode options);
5155 220 ▶ delete $vis->{filters} if ref $vis->{filters} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $vis->{"filters"} ne "ARRAY" 219 1 220
5156 220 delete $vis->{$_} for grep { ref $vis->{$_} } qw(kind title);
5157 220 return $vis;
5158 }
5159  
5160 # _write_analyses($self, \%analyses) -> 1 | 0 | nothing -- atomic write (temp + rename)
5161 # Nothing means the store is switched off (see _analyses_path).
5162 56 ▶sub _write_analyses :Protected ($self, $analyses) {
Subroutine _write_analyses: called CC=9 SCAR=23.9
5163 56 my $path = $self->_analyses_path;
5164 56 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 56 56
5165 56 $self->_set_aside_corrupt_store($path);
5166 56 my $dir = Mojo::File->new($path)->dirname->to_string;
5167 56 my ($fh, $tmpfile) = eval { tempfile(DIR => $dir, UNLINK => 0) };
5168 56 ▶ if ($@ || !$fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $fh) 0 56 56
Condition33%1/3
$@ or not $fh
l!l&&r!l&&!r
0056
MC/DC0%0/2
$@ or not $fh
$@ not $fh
Truth table33%1/3
A or not(B)
A= $@B= not $fh
ABresult
1X1
011
000
5169 0 ▶ $self->app->log->warn("Cannot create temp file in $dir: " . ($@ // $!));
Condition0%0/3
$@ // $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ // $!
$@ $!
Truth table0%0/3
A // B
A= $@B= $!
ABresult
1X1
011
000
5170 0 return;
5171 }
5172 56 my $ok = eval {
5173 56 ▶ print {$fh} encode_json($analyses) or croak "print: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} encode_json($analyses) or croak("print: $!") 56 0 56
5174 56 ▶ close $fh or croak "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("close: $!") 56 0 56
5175 56 ▶ rename($tmpfile, $path) or croak "rename: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfile, $path or croak("rename: $!") 54 2 56
5176 54 1;
5177 };
5178 56 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 54 56
5179 2 $self->app->log->warn("Cannot write $path: $@");
5180 2 unlink $tmpfile;
5181 2 return 0;
5182 }
5183 54 return 1;
5184 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5185  
5186 # _gen_analysis_id($self) -> $id_string
5187 #
5188 # Returns a 10-character URL-safe alphanumeric random ID.
5189 5 Readonly my $_ID_CHARS => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
5190  
5191 228 ▶sub _gen_analysis_id :Protected ($self) {
Subroutine _gen_analysis_id: called CC=2 SCAR=6.9
5192 228 my $id = '';
5193 228 $id .= substr($_ID_CHARS, int(rand(62)), 1) for 1..10;
5194 228 return $id;
5195 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5196  
5197 # The canvas is a 12-column CSS grid; a layout entry addresses cells in it.
5198 # Shared by _validate_layout and by the layout precompute in canvas_render.
5199 5 Readonly my $GRID_COLUMNS => 12;
5200  
5201 # How many grid rows one visual may span. A row is at least 15rem tall (see
5202 # grid-auto-rows in templates/web/en/canvas.html.tt), so this is also a cap on
5203 # how tall a single visual can be made from the canvas.
5204 5 Readonly my $MAX_ROW_SPAN => 6;
5205  
5206 # _save_analyses_or_503($self, \%analyses) -> $bool
5207 #
5208 # Writes the store, or answers 503 and returns false, so that no route tells a
5209 # client something was saved when it was not.
5210 42 ▶sub _save_analyses_or_503 :Protected ($self, $analyses) {
Subroutine _save_analyses_or_503: called CC=2 SCAR=6.9
5211 42 ▶ return 1 if $self->_write_analyses($analyses);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_write_analyses($analyses) 39 3 42
5212  
5213 3 $self->render(json => { error => _message_text('error_analysis_storage') }, status => 503);
5214 3 return 0;
5215 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5216  
5217 # _validate_layout($self, $layout_or_undef) -> $error_string_or_undef
5218 #
5219 # A layout entry is { visual, x, y, w, h } -- the grid position of one visual
5220 # on the canvas. The canvas PATCHes these straight out of a drag, so they are
5221 # untrusted input like every other field: reject anything that is not a whole
5222 # number of columns or rows inside the grid, rather than store a position that
5223 # renders as "span 0" or runs off the right-hand edge.
5224 60 ▶sub _validate_layout :Protected ($self, $layout) {
Subroutine _validate_layout: called CC=22 SCAR=31.0
5225 60 ▶ return undef unless defined $layout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $layout 39 21 60
5226 21 ▶ return 'layout must be an array' unless ref $layout eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $layout eq "ARRAY" 1 20 21
5227  
5228 20 my $n = 0;
5229 20 my @placed; # the cells already spoken for, to reject an overlap
5230 20 for my $lp (@$layout) {
5231 23 $n++;
5232 23 ▶ return "layout entry $n must be a hash" unless ref $lp eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lp eq "HASH" 2 21 23
5233 21 for my $f (qw( visual x y w h )) {
5234 94 ▶ return "layout entry $n is missing $f" unless defined $lp->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lp->{$f} 2 92 94
5235 return "layout entry $n: $f must be a whole number"
5236 92 ▶ if ref $lp->{$f} || $lp->{$f} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $lp->{$f} or not $lp->{$f} =~ /\A\d+\z/u 4 88 92
Condition100%3/3
ref $lp->{$f} or not $lp->{$f} =~ /\A\d+\z/u
l!l&&r!l&&!r
1388
MC/DC100%2/2
ref $lp->{$f} or not $lp->{$f} =~ /\A\d+\z/u
ref $lp->{$f} not $lp->{$f} =~ /\A\d+\z/u
Truth table100%3/3
A or not(B)
A= ref $lp->{$f}B= not $lp->{$f} =~ /\A\d+\z/u
ABresult
1X1
011
000
5237 }
5238 return "layout entry $n: x must be between 1 and $GRID_COLUMNS"
5239 15 ▶ unless $lp->{x} >= 1 && $lp->{x} <= $GRID_COLUMNS;
Branch: 1 branch
BranchTrueFalseTotal
unless $lp->{"x"} >= 1 and $lp->{"x"} <= $GRID_COLUMNS 2 13 15
Condition100%3/3
$lp->{"x"} >= 1 and $lp->{"x"} <= $GRID_COLUMNS
!ll&&!rl&&r
1113
MC/DC100%2/2
$lp->{"x"} >= 1 and $lp->{"x"} <= $GRID_COLUMNS
$lp->{"x"} >= 1 $lp->{"x"} <= $GRID_COLUMNS
Truth table100%3/3
A and B
A= $lp->{"x"} >= 1B= $lp->{"x"} <= $GRID_COLUMNS
ABresult
0X0
100
111
5240 return "layout entry $n: y must be at least 1"
5241 13 ▶ unless $lp->{y} >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $lp->{"y"} >= 1 1 12 13
5242 return "layout entry $n: w must be between 1 and $GRID_COLUMNS"
5243 12 ▶ unless $lp->{w} >= 1 && $lp->{w} <= $GRID_COLUMNS;
Branch: 1 branch
BranchTrueFalseTotal
unless $lp->{"w"} >= 1 and $lp->{"w"} <= $GRID_COLUMNS 1 11 12
Condition66%2/3
$lp->{"w"} >= 1 and $lp->{"w"} <= $GRID_COLUMNS
!ll&&!rl&&r
1011
MC/DC50%1/2
$lp->{"w"} >= 1 and $lp->{"w"} <= $GRID_COLUMNS
$lp->{"w"} >= 1 $lp->{"w"} <= $GRID_COLUMNS
Truth table66%2/3
A and B
A= $lp->{"w"} >= 1B= $lp->{"w"} <= $GRID_COLUMNS
ABresult
0X0
100
111
5244 return "layout entry $n: h must be between 1 and $MAX_ROW_SPAN"
5245 11 ▶ unless $lp->{h} >= 1 && $lp->{h} <= $MAX_ROW_SPAN;
Branch: 1 branch
BranchTrueFalseTotal
unless $lp->{"h"} >= 1 and $lp->{"h"} <= $MAX_ROW_SPAN 2 9 11
Condition100%3/3
$lp->{"h"} >= 1 and $lp->{"h"} <= $MAX_ROW_SPAN
!ll&&!rl&&r
119
MC/DC100%2/2
$lp->{"h"} >= 1 and $lp->{"h"} <= $MAX_ROW_SPAN
$lp->{"h"} >= 1 $lp->{"h"} <= $MAX_ROW_SPAN
Truth table100%3/3
A and B
A= $lp->{"h"} >= 1B= $lp->{"h"} <= $MAX_ROW_SPAN
ABresult
0X0
100
111
5246 return "layout entry $n: x + w - 1 must not exceed $GRID_COLUMNS"
5247 9 ▶ if $lp->{x} + $lp->{w} - 1 > $GRID_COLUMNS;
Branch: 1 branch
BranchTrueFalseTotal
if $lp->{"x"} + $lp->{"w"} - 1 > $GRID_COLUMNS 1 8 9
5248  
5249 # Two visuals cannot hold the same cell: the canvas is a grid of positions,
5250 # and an overlapping layout renders one visual on top of another. The
5251 # canvas pushes the cards a drop lands on out of the way before it PATCHes,
5252 # so this rejects a hand-written or stale layout rather than a drag.
5253 8 for my $p (@placed) {
5254 4 ▶ next unless $p->{x} < $lp->{x} + $lp->{w} && $lp->{x} < $p->{x} + $p->{w};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"x"} < $lp->{"x"} + $lp->{"w"} and $lp->{"x"} < $p->{"x"} + $p->{"w"} 2 2 4
Condition66%2/3
$p->{"x"} < $lp->{"x"} + $lp->{"w"} and $lp->{"x"} < $p->{"x"} + $p->{"w"}
!ll&&!rl&&r
022
MC/DC50%1/2
$p->{"x"} < $lp->{"x"} + $lp->{"w"} and $lp->{"x"} < $p->{"x"} + $p->{"w"}
$p->{"x"} < $lp->{"x"} + $lp->{"w"} $lp->{"x"} < $p->{"x"} + $p->{"w"}
Truth table66%2/3
A and B
A= $p->{"x"} < $lp->{"x"} + $lp->{"w"}B= $lp->{"x"} < $p->{"x"} + $p->{"w"}
ABresult
0X0
100
111
5255 return "layout entry $n overlaps an earlier entry"
5256 2 ▶ if $p->{y} < $lp->{y} + $lp->{h} && $lp->{y} < $p->{y} + $p->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"y"} < $lp->{"y"} + $lp->{"h"} and $lp->{"y"} < $p->{"y"} + $p->{"h"} 2 0 2
Condition33%1/3
$p->{"y"} < $lp->{"y"} + $lp->{"h"} and $lp->{"y"} < $p->{"y"} + $p->{"h"}
!ll&&!rl&&r
002
MC/DC0%0/2
$p->{"y"} < $lp->{"y"} + $lp->{"h"} and $lp->{"y"} < $p->{"y"} + $p->{"h"}
$p->{"y"} < $lp->{"y"} + $lp->{"h"} $lp->{"y"} < $p->{"y"} + $p->{"h"}
Truth table33%1/3
A and B
A= $p->{"y"} < $lp->{"y"} + $lp->{"h"}B= $lp->{"y"} < $p->{"y"} + $p->{"h"}
ABresult
0X0
100
111
5257 }
5258 6 push @placed, { x => $lp->{x}, y => $lp->{y}, w => $lp->{w}, h => $lp->{h} };
5259 }
5260  
5261 3 return undef;
5262 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5263  
5264 # _validate_analysis($self, \%doc) -> $error_string_or_undef
5265 #
5266 # Structural validation of a saved-analysis document. Called on create, on
5267 # replace and on import; analyses_patch validates the keys it is given instead,
5268 # so that a PATCH of one field cannot fail because of another field it never
5269 # read.
5270 72 ▶sub _validate_analysis :Protected ($self, $doc) {
Subroutine _validate_analysis: called CC=3 SCAR=11.0
5271 72 for my $required (qw(name datasets)) {
5272 143 ▶ return "Missing required field: $required" unless defined $doc->{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$required} 3 140 143
5273 }
5274 69 return $self->_validate_analysis_parts($doc);
5275 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5276  
5277 # _drop_unknown_keys(\%doc) -> void
5278 #
5279 # Only these keys are ever read back, so anything else a client sends is not kept
5280 # on any write: the POD promises this, and a store should not grow what nothing uses.
5281 39 ▶sub _drop_unknown_keys {
Subroutine _drop_unknown_keys: called CC=1 SCAR=0.0
5282 39 my $doc = $_[0];
5283  
5284 39 delete @{$doc}{ grep { !$ANALYSIS_KEYS{$_} } keys %$doc };
5285 }
5286  
5287 # _validate_analysis_parts($self, \%doc) -> $error_string_or_undef
5288 #
5289 # Checks only the keys present, which is all a PATCH may be judged on. This body
5290 # is whatever a client sent, so nothing about the shape of a value is assumed.
5291 84 ▶sub _validate_analysis_parts :Protected ($self, $doc) {
Subroutine _validate_analysis_parts: called CC=30 SCAR=34.1
5292 84 ▶ if (exists $doc->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"name"}) 72 12 84
5293 return 'Missing required field: name'
5294 72 ▶ unless defined $doc->{name} && !ref $doc->{name} && length $doc->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{"name"} and not ref $doc->{"name"} and length $doc->{"name"} 7 65 72
Condition83%5/6
defined $doc->{"name"} and not ref $doc->{"name"} and length $doc->{"name"}
!ll&&!rl&&r
5265
defined $doc->{"name"} and not ref $doc->{"name"}
!ll&&!rl&&r
0567
MC/DC66%2/3
defined $doc->{"name"} and not ref $doc->{"name"} and length $doc->{"name"}
defined $doc->{"name"} not ref $doc->{"name"} length $doc->{"name"}
Truth table75%3/4
A and not(B) and C
A= defined $doc->{"name"}B= not ref $doc->{"name"}C= length $doc->{"name"}
ABCresult
0XX0
10X0
1100
1111
5295 }
5296 77 ▶ if (exists $doc->{datasets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"datasets"}) 64 13 77
5297 return 'Missing required field: datasets'
5298 64 ▶ unless ref $doc->{datasets} eq 'ARRAY' && @{ $doc->{datasets} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc->{"datasets"} eq "ARRAY" and @{$doc->{"datasets"};} 3 61 64
Condition100%3/3
ref $doc->{"datasets"} eq "ARRAY" and @{$doc->{"datasets"};}
!ll&&!rl&&r
1261
MC/DC100%2/2
ref $doc->{"datasets"} eq "ARRAY" and @{$doc->{"datasets"};}
ref $doc->{"datasets"} eq "ARRAY" @{$doc->{"datasets"};}
Truth table100%3/3
A and B
A= ref $doc->{"datasets"} eq "ARRAY"B= @{$doc->{"datasets"};}
ABresult
0X0
100
111
5299 61 for my $ds (@{ $doc->{datasets} }) {
5300 return 'Each dataset entry must have a spec field'
5301 61 ▶ unless ref $ds eq 'HASH' && defined $ds->{spec} && !ref $ds->{spec} && length $ds->{spec};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ds eq "HASH" and defined $ds->{"spec"} and not ref $ds->{"spec"} and length $ds->{"spec"} 15 46 61
Condition88%8/9
ref $ds eq "HASH" and defined $ds->{"spec"} and not ref $ds->{"spec"} and length $ds->{"spec"}
!ll&&!rl&&r
15046
ref $ds eq "HASH" and defined $ds->{"spec"} and not ref $ds->{"spec"}
!ll&&!rl&&r
11446
ref $ds eq "HASH" and defined $ds->{"spec"}
!ll&&!rl&&r
10150
MC/DC75%3/4
ref $ds eq "HASH" and defined $ds->{"spec"} and not ref $ds->{"spec"} and length $ds->{"spec"}
ref $ds eq "HASH" defined $ds->{"spec"} not ref $ds->{"spec"} length $ds->{"spec"}
Truth table80%4/5
A and B and not(C) and D
A= ref $ds eq "HASH"B= defined $ds->{"spec"}C= not ref $ds->{"spec"}D= length $ds->{"spec"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
5302 return qq{"$ds->{spec}" is not a valid spec (must start with table:, path:, url:, or redis:, or be mariadb:server/table)}
5303 46 ▶ unless $ds->{spec} =~ m{\A(?:table:|path:|url:|redis:)} || $ds->{spec} =~ $MARIADB_SPEC_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $ds->{"spec"} =~ /\A(?:table:|path:|url:|redis:)/u or $ds->{"spec"} =~ /$MARIADB_SPEC_RE/u 1 45 46
Condition66%2/3
$ds->{"spec"} =~ /\A(?:table:|path:|url:|redis:)/u or $ds->{"spec"} =~ /$MARIADB_SPEC_RE/u
l!l&&r!l&&!r
4501
MC/DC50%1/2
$ds->{"spec"} =~ /\A(?:table:|path:|url:|redis:)/u or $ds->{"spec"} =~ /$MARIADB_SPEC_RE/u
$ds->{"spec"} =~ /\A(?:table:|path:|url:|redis:)/u $ds->{"spec"} =~ /$MARIADB_SPEC_RE/u
Truth table66%2/3
A or B
A= $ds->{"spec"} =~ /\A(?:table:|path:|url:|redis:)/uB= $ds->{"spec"} =~ /$MARIADB_SPEC_RE/u
ABresult
1X1
011
000
5304 }
5305 }
5306 58 ▶ if (defined $doc->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{"version"}) 5 53 58
5307 return sprintf(_message_text('error_analysis_version'), $doc->{version})
5308 5 ▶ unless !ref $doc->{version} && $doc->{version} =~ /\A\d+\z/ && $doc->{version} <= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $doc->{"version"} and $doc->{"version"} =~ /\A\d+\z/u and $doc->{"version"} <= 1 3 2 5
Condition100%6/6
not ref $doc->{"version"} and $doc->{"version"} =~ /\A\d+\z/u and $doc->{"version"} <= 1
!ll&&!rl&&r
212
not ref $doc->{"version"} and $doc->{"version"} =~ /\A\d+\z/u
!ll&&!rl&&r
113
MC/DC100%3/3
not ref $doc->{"version"} and $doc->{"version"} =~ /\A\d+\z/u and $doc->{"version"} <= 1
not ref $doc->{"version"} $doc->{"version"} =~ /\A\d+\z/u $doc->{"version"} <= 1
Truth table100%4/4
not(A) and B and C
A= not ref $doc->{"version"}B= $doc->{"version"} =~ /\A\d+\z/uC= $doc->{"version"} <= 1
ABCresult
0XX0
10X0
1100
1111
5309 }
5310 55 for my $key (qw( visuals joins measures combines bookmarks layout hierarchies )) {
5311 345 ▶ if (defined $doc->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{$key}) 46 299 345
5312 return "$key must be an array"
5313 46 ▶ unless ref $doc->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc->{$key} eq "ARRAY" 11 35 46
5314 }
5315 }
5316 44 for my $key (qw( visuals joins )) {
5317 86 ▶ next unless defined $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} 56 30 86
5318 return "Each entry in $key must be an object"
5319 30 ▶ if grep { ref $_ ne 'HASH' } @{ $doc->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "HASH";} @{$$doc{$key};} 3 27 30
5320 }
5321 41 my $lerr = $self->_validate_layout($doc->{layout});
5322 41 ▶ return $lerr if $lerr;
Branch: 1 branch
BranchTrueFalseTotal
if $lerr 2 39 41
5323 39 ▶ if (defined $doc->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{"fields"}) 1 38 39
5324 return 'fields must be a hash'
5325 1 ▶ unless ref $doc->{fields} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc->{"fields"} eq "HASH" 1 0 1
5326 }
5327 38 ▶ if (defined $doc->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{"params"}) 2 36 38
5328 return 'params must be a hash'
5329 2 ▶ unless ref $doc->{params} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc->{"params"} eq "HASH" 2 0 2
5330 }
5331 36 return undef;
5332 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5333  
5334 # _analysis_to_url($self, \%analysis) -> $url_string_or_undef
5335 #
5336 # Reconstructs a /join?l=...&j=...&f=... URL from a saved analysis.
5337 # Used by canvas_render to redirect when no visuals exist yet.
5338 30 ▶sub _analysis_to_url :Protected ($self, $a) {
Subroutine _analysis_to_url: called CC=18 SCAR=29.0
5339 30 ▶ my @datasets = @{ $a->{datasets} // [] };
Condition100%2/2
$a->{"datasets"} // []
l!l
264
MC/DC100%1/1
$a->{"datasets"} // []
$a->{"datasets"}
Truth table100%2/2
A
A= $a->{"datasets"}
Aresult
11
00
5340 30 ▶ return undef unless @datasets;
Branch: 1 branch
BranchTrueFalseTotal
unless @datasets 4 26 30
5341  
5342 26 ▶ my $left_spec = $datasets[0]{spec} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $left_spec = $datasets[0]{"spec"} or return undef 25 1 26
5343 25 my @params = ('l=' . url_escape($left_spec));
5344  
5345 # Build an alias->spec lookup for join resolution
5346 25 my %spec_by_alias;
5347 25 for my $ds (@datasets) {
5348 26 ▶ $spec_by_alias{ $ds->{alias} // '' } = $ds->{spec} // '';
Condition75%3/4
$ds->{"spec"} // ""
l!l
260
$ds->{"alias"} // ""
l!l
323
MC/DC0%0/1
$ds->{"spec"} // ""
$ds->{"spec"}
MC/DC100%1/1
$ds->{"alias"} // ""
$ds->{"alias"}
Truth table50%1/2
A
A= $ds->{"spec"}
Aresult
11
00
Truth table100%2/2
A
A= $ds->{"alias"}
Aresult
11
00
5349 }
5350  
5351 25 ▶ for my $j (@{ $a->{joins} // [] }) {
Condition100%2/2
$a->{"joins"} // []
l!l
322
MC/DC100%1/1
$a->{"joins"} // []
$a->{"joins"}
Truth table100%2/2
A
A= $a->{"joins"}
Aresult
11
00
5352 3 ▶ next unless ref $j eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $j eq "HASH" 1 2 3
5353 2 ▶ my $right_spec = $spec_by_alias{ $j->{right} // '' } // '';
Condition75%3/4
$spec_by_alias{$j->{"right"} // ""} // ""
l!l
11
$j->{"right"} // ""
l!l
20
MC/DC100%1/1
$spec_by_alias{$j->{"right"} // ""} // ""
$spec_by_alias{$j->{"right"} // ""}
MC/DC0%0/1
$j->{"right"} // ""
$j->{"right"}
Truth table100%2/2
A
A= $spec_by_alias{$j->{"right"} // ""}
Aresult
11
00
Truth table50%1/2
A
A= $j->{"right"}
Aresult
11
00
5354 2 ▶ next unless $right_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_spec 1 1 2
5355 push @params, 'j=' . url_escape(
5356 1 ▶ join('|', $right_spec, $j->{left_key}//'', $j->{right_key}//'')
Condition50%2/4
$j->{"left_key"} // ""
l!l
10
$j->{"right_key"} // ""
l!l
10
MC/DC0%0/1
$j->{"left_key"} // ""
$j->{"left_key"}
MC/DC0%0/1
$j->{"right_key"} // ""
$j->{"right_key"}
Truth table50%1/2
A
A= $j->{"left_key"}
Aresult
11
00
Truth table50%1/2
A
A= $j->{"right_key"}
Aresult
11
00
5357 );
5358 }
5359  
5360 # Filters from all datasets
5361 25 for my $ds (@datasets) {
5362 26 ▶ next unless ref $ds->{filters} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ds->{"filters"} eq "ARRAY" 25 1 26
5363 1 for my $f (@{ $ds->{filters} }) {
5364 1 push @params, 'f=' . url_escape(encode('UTF-8', $f));
5365 }
5366 }
5367  
5368 25 return '/join?' . join('&', @params);
5369 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
5370  
5371 # GET /api/analyses
5372 21 ▶sub analyses_list ($self) {
Subroutine analyses_list: called CC=1 SCAR=0.0
Pod documented
5373 21 my $analyses = $self->_read_analyses;
5374 my @list = map {
5375 21 my $a = $analyses->{$_};
5376 { id => $_, name => $a->{name}, datasets => $a->{datasets},
5377 updated => $a->{updated} }
5378 16 } sort keys %$analyses;
5379 21 $self->render(json => { analyses => \@list });
5380 }
5381  
5382 # POST /api/analyses
5383 40 ▶sub analyses_create ($self) {
Subroutine analyses_create: called CC=8 SCAR=21.1
Pod documented
5384 40 my $body = eval { decode_json($self->req->body) };
5385 40 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 8 32 40
5386 unless ref $body eq 'HASH';
5387  
5388 32 my $err = $self->_validate_analysis($body);
5389 32 ▶ return $self->render(json => { error => sprintf(_message_text('error_analysis_invalid'), $err) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if $err 6 26 32
5390 if $err;
5391  
5392 26 my $analyses = $self->_read_analyses;
5393 26 ▶ unless ($self->_analyses_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_analyses_path) 0 26 26
5394 0 return $self->render(json => { error => _message_text('error_analysis_storage') }, status => 503);
5395 }
5396  
5397 26 my $id;
5398 26 my $tries = 0;
5399 26 do { $id = $self->_gen_analysis_id; $tries++ }
5400 26 ▶ while (exists $analyses->{$id} && $tries < 100);
Branch: 1 branch
BranchTrueFalseTotal
exists $analyses->{$id} and $tries < 100 and do { do { $id = $self->_gen_analysis_id; ++$tries } } 0 26 26
Condition33%1/3
exists $analyses->{$id} and $tries < 100
!ll&&!rl&&r
2600
MC/DC0%0/2
exists $analyses->{$id} and $tries < 100
exists $analyses->{$id} $tries < 100
Truth table33%1/3
A and B
A= exists $analyses->{$id}B= $tries < 100
ABresult
0X0
100
111
5401  
5402 26 $body->{id} = $id;
5403 26 ▶ $body->{version} //= 1;
Condition50%1/2
$body->{"version"} //= 1
l!l
026
MC/DC0%0/1
$body->{"version"} //= 1
$body->{"version"}
Truth table50%1/2
A
A= $body->{"version"}
Aresult
11
00
5404 26 $body->{created} = time();
5405 26 $body->{updated} = time();
5406  
5407 # Strip caller-supplied immutable fields except id/version/created/updated
5408 26 _drop_unknown_keys($body);
5409  
5410 26 $analyses->{$id} = $body;
5411 26 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 25 1 26
5412  
5413 25 $self->res->headers->location("/api/analyses/$id");
5414 25 $self->render(json => { id => $id, created => 1 }, status => 201);
5415 }
5416  
5417 # GET /api/analyses/:id
5418 40 ▶sub analyses_get ($self) {
Subroutine analyses_get: called CC=4 SCAR=13.9
Pod documented
5419 40 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
400
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5420 40 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 3 37 40
5421 unless $id =~ /\A[A-Za-z0-9]+\z/;
5422  
5423 37 my $analyses = $self->_read_analyses;
5424 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5425 37 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 5 32 37
5426  
5427 32 $self->render(json => $analyses->{$id});
5428 }
5429  
5430 # PUT /api/analyses/:id (full replace)
5431 17 ▶sub analyses_update ($self) {
Subroutine analyses_update: called CC=9 SCAR=22.1
Pod documented
5432 17 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
170
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5433 17 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 2 15 17
5434 unless $id =~ /\A[A-Za-z0-9]+\z/;
5435  
5436 15 my $analyses = $self->_read_analyses;
5437 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5438 15 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 2 13 15
5439  
5440 13 my $body = eval { decode_json($self->req->body) };
5441 13 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 7 6 13
5442 unless ref $body eq 'HASH';
5443  
5444 6 my $err = $self->_validate_analysis($body);
5445 6 ▶ return $self->render(json => { error => sprintf(_message_text('error_analysis_invalid'), $err) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if $err 4 2 6
5446 if $err;
5447  
5448 2 _drop_unknown_keys($body);
5449 2 $body->{id} = $id;
5450 2 ▶ $body->{version} //= 1;
Condition50%1/2
$body->{"version"} //= 1
l!l
02
MC/DC0%0/1
$body->{"version"} //= 1
$body->{"version"}
Truth table50%1/2
A
A= $body->{"version"}
Aresult
11
00
5451 2 ▶ $body->{created} = $analyses->{$id}{created} // time();
Condition50%1/2
$analyses->{$id}{"created"} // time
l!l
20
MC/DC0%0/1
$analyses->{$id}{"created"} // time
$analyses->{$id}{"created"}
Truth table50%1/2
A
A= $analyses->{$id}{"created"}
Aresult
11
00
5452 2 $body->{updated} = time();
5453  
5454 2 $analyses->{$id} = $body;
5455 2 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 2 0 2
5456  
5457 2 $self->render(json => { id => $id, updated => 1 });
5458 }
5459  
5460 # PATCH /api/analyses/:id (partial update -- merges supplied keys)
5461 13 ▶sub analyses_patch ($self) {
Subroutine analyses_patch: called CC=10 SCAR=23.0
Pod documented
5462 13 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
130
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5463 13 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 2 11 13
5464 unless $id =~ /\A[A-Za-z0-9]+\z/;
5465  
5466 11 my $analyses = $self->_read_analyses;
5467 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5468 11 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 2 9 11
5469  
5470 9 my $body = eval { decode_json($self->req->body) };
5471 9 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 2 7 9
5472 unless ref $body eq 'HASH';
5473  
5474 # Only the keys actually supplied are validated here: the canvas PATCHes
5475 # nothing but the layout (a drag) or the bookmarks, and a document that
5476 # fails on some other field it never mentioned is not this request's fault.
5477 7 my $perr = $self->_validate_analysis_parts($body);
5478 7 ▶ return $self->render(json => { error => sprintf(_message_text('error_analysis_invalid'), $perr) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if $perr 4 3 7
5479 if $perr;
5480  
5481 3 _drop_unknown_keys($body);
5482 3 my $existing = { %{ $analyses->{$id} } };
5483 3 for my $key (keys %$body) {
5484 6 ▶ next if $key eq 'id' || $key eq 'created'; # immutable
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "id" or $key eq "created" 2 4 6
Condition100%3/3
$key eq "id" or $key eq "created"
l!l&&r!l&&!r
114
MC/DC100%2/2
$key eq "id" or $key eq "created"
$key eq "id" $key eq "created"
Truth table100%3/3
A or B
A= $key eq "id"B= $key eq "created"
ABresult
1X1
011
000
5485 4 $existing->{$key} = $body->{$key};
5486 }
5487 3 $existing->{updated} = time();
5488  
5489 3 $analyses->{$id} = $existing;
5490 3 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 3 0 3
5491  
5492 3 $self->render(json => { id => $id, updated => 1 });
5493 }
5494  
5495 # DELETE /api/analyses/:id
5496 9 ▶sub analyses_delete ($self) {
Subroutine analyses_delete: called CC=5 SCAR=16.1
Pod documented
5497 9 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
90
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5498 9 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 3 6 9
5499 unless $id =~ /\A[A-Za-z0-9]+\z/;
5500  
5501 6 my $analyses = $self->_read_analyses;
5502 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5503 6 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 3 3 6
5504  
5505 3 delete $analyses->{$id};
5506 3 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 2 1 3
5507  
5508 2 $self->render(json => { deleted => 1 });
5509 }
5510  
5511 # POST /api/analyses/:id/visuals (append one table or chart)
5512 #
5513 # Adds a visual to a saved canvas without sending the reader back to the table
5514 # or chart page it came from. The request is { kind, encode, options, title }
5515 # and the visual is rendered here, with the same pipeline and renderer the
5516 # canvas itself uses, before it is stored: a column that does not exist is a
5517 # 400 rather than a broken card the reader only discovers after a reload.
5518 #
5519 # The new card is deliberately left out of the saved layout. An auto-placed
5520 # card is ordered after the saved ones and stays there without pinning a
5521 # position that the next drag would have to fight; the canvas writes a layout
5522 # entry once the reader moves or resizes it.
5523 17 ▶sub analyses_add_visual ($self) {
Subroutine analyses_add_visual: called CC=35 SCAR=43.9
Pod documented
5524 17 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
170
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5525 17 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 2 15 17
5526 unless $id =~ /\A[A-Za-z0-9]+\z/;
5527  
5528 15 my $analyses = $self->_read_analyses;
5529 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5530 15 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 2 13 15
5531 13 my $a = $analyses->{$id};
5532  
5533 13 my $body = eval { decode_json($self->req->body) };
5534 13 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 2 11 13
5535 unless ref $body eq 'HASH';
5536  
5537 11 ▶ my $kind = $body->{kind} // '';
Condition50%1/2
$body->{"kind"} // ""
l!l
110
MC/DC0%0/1
$body->{"kind"} // ""
$body->{"kind"}
Truth table50%1/2
A
A= $body->{"kind"}
Aresult
11
00
5538 11 my %supported = map { $_ => 1 }
5539 qw( table bar pie line graph scatter heatmap stacked combo treemap histogram map wordcloud text );
5540 return $self->render(json => { error => 'Unsupported visual kind' }, status => 400)
5541 11 ▶ unless $supported{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $supported{$kind} 1 10 11
5542  
5543 10 for my $key (qw( encode options cross_row )) {
5544 return $self->render(json => { error => "$key must be an object" }, status => 400)
5545 30 ▶ if defined $body->{$key} && ref $body->{$key} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{$key} and ref $body->{$key} ne "HASH" 1 29 30
Condition100%3/3
defined $body->{$key} and ref $body->{$key} ne "HASH"
!ll&&!rl&&r
2091
MC/DC100%2/2
defined $body->{$key} and ref $body->{$key} ne "HASH"
defined $body->{$key} ref $body->{$key} ne "HASH"
Truth table100%3/3
A and B
A= defined $body->{$key}B= ref $body->{$key} ne "HASH"
ABresult
0X0
100
111
5546 }
5547  
5548 # A text card reads no data, so it can be added whatever state the canvas's
5549 # source is in.
5550 9 ▶ if ($kind eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "text") 5 4 9
5551 5 my ($options, $errors) = _text_card_options($body->{options});
5552 5 ▶ return $self->render(json => { error => $errors->[0] }, status => 400) if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 5 0 5
5553  
5554 0 my $vis = { kind => 'text', encode => {}, options => $options };
5555 $vis->{title} = $body->{title}
5556 0 ▶ if defined $body->{title} && !ref $body->{title} && length $body->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"} 0 0 0
Condition0%0/6
defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"}
!ll&&!rl&&r
000
defined $body->{"title"} and not ref $body->{"title"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"}
defined $body->{"title"} not ref $body->{"title"} length $body->{"title"}
Truth table0%0/4
A and not(B) and C
A= defined $body->{"title"}B= not ref $body->{"title"}C= length $body->{"title"}
ABCresult
0XX0
10X0
1100
1111
5557  
5558 0 ▶ my $index = scalar @{ $a->{visuals} // [] };
Condition0%0/2
$a->{"visuals"} // []
l!l
00
MC/DC0%0/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table0%0/2
A
A= $a->{"visuals"}
Aresult
11
00
5559 0 ▶ push @{ $a->{visuals} //= [] }, $vis;
Condition0%0/2
$a->{"visuals"} //= []
l!l
00
MC/DC0%0/1
$a->{"visuals"} //= []
$a->{"visuals"}
Truth table0%0/2
A
A= $a->{"visuals"}
Aresult
11
00
5560 0 $a->{updated} = time();
5561 0 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 0 0 0
5562  
5563 0 return $self->render(json => _text_card_reply($id, $index, $vis), status => 201);
5564 }
5565  
5566 my $vis = {
5567 kind => $kind,
5568 encode => ($body->{encode} // {}),
5569 4 ▶ options => ($body->{options} // {}),
Condition75%3/4
$body->{"encode"} // {}
l!l
31
$body->{"options"} // {}
l!l
04
MC/DC100%1/1
$body->{"encode"} // {}
$body->{"encode"}
MC/DC0%0/1
$body->{"options"} // {}
$body->{"options"}
Truth table100%2/2
A
A= $body->{"encode"}
Aresult
11
00
Truth table50%1/2
A
A= $body->{"options"}
Aresult
11
00
5570 };
5571  
5572 # A card may bring a cross-row plan of its own; it is stored in the same
5573 # spelling cs=/by=/order= use -- { cs => [...], by => '...', order => [...] }
5574 # -- so the pass reads it through the one parser, with the page's plan left
5575 # out of it. Only the shape is checked here: which columns exist is a fact
5576 # about the data file, and it is checked again on every render for the same
5577 # reason a saved canvas is.
5578 4 ▶ if (my $raw = $body->{cross_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $raw = $body->{"cross_row"}) 1 3 4
5579 1 my $plan = _card_plan($raw);
5580 return $self->render(json => { error => $plan->{error} }, status => 400)
5581 1 ▶ if $plan->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{"error"} 1 0 1
5582 0 $vis->{cross_row} = $plan;
5583 }
5584  
5585 $vis->{title} = $body->{title}
5586 3 ▶ if defined $body->{title} && !ref $body->{title} && length $body->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"} 2 1 3
Condition66%4/6
defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"}
!ll&&!rl&&r
102
defined $body->{"title"} and not ref $body->{"title"}
!ll&&!rl&&r
102
MC/DC33%1/3
defined $body->{"title"} and not ref $body->{"title"} and length $body->{"title"}
defined $body->{"title"} not ref $body->{"title"} length $body->{"title"}
Truth table50%2/4
A and not(B) and C
A= defined $body->{"title"}B= not ref $body->{"title"}C= length $body->{"title"}
ABCresult
0XX0
10X0
1100
1111
5587  
5588 # The card is added without a page reload, so the canvas's own URL state has
5589 # to come with the request: a reader who adds a card while a chart is
5590 # cross-filtering the canvas, with calculated columns on the URL, or with a
5591 # what-if parameter set, gets a card drawn like the ones beside it rather
5592 # than against the unfiltered table.
5593 3 my $ctx = $self->_canvas_render_context($a, $body->{query}, [$vis]);
5594 return $self->render(json => { error => $ctx->{error} }, status => $ctx->{status})
5595 3 ▶ if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"error"} 0 3 3
5596  
5597 # The new card is never the cross-filter source: it did not exist when the
5598 # filter was made, so every cf applies to it, as it does to every card but
5599 # the source.
5600 3 my ($records, $late_cf) = $self->_canvas_cross_filter($ctx->{records}, $ctx->{columns}, $ctx->{cf_specs});
5601  
5602 # The index is known before the render, so the card's SVG and Leaflet ids
5603 # match the ones a later page load will give it.
5604 3 ▶ my $index = scalar @{ $a->{visuals} // [] };
Condition100%2/2
$a->{"visuals"} // []
l!l
21
MC/DC100%1/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
5605  
5606 my $rendered = $self->_render_visual_html(
5607 $vis, $records, $ctx->{columns}, $ctx->{src}, $index, $ctx->{spec_key},
5608 3 (@{ $ctx->{cf_specs} } ? 1 : 0),
5609 $ctx->{cross_row},
5610 3 ▶ $late_cf,
Branch: 1 branch
BranchTrueFalseTotal
@{$ctx->{'cf_specs'};} ? : 0 3 3
5611 );
5612 return $self->render(json => { error => $rendered->{error} }, status => 400)
5613 3 ▶ if $rendered->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $rendered->{"error"} 1 2 3
5614  
5615 2 ▶ push @{ $a->{visuals} //= [] }, $vis;
Condition100%2/2
$a->{"visuals"} //= []
l!l
11
MC/DC100%1/1
$a->{"visuals"} //= []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
5616 2 $a->{updated} = time();
5617 2 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 2 0 2
5618  
5619 return $self->render(
5620 json => {
5621 id => $id,
5622 visual => $index,
5623 kind => $kind,
5624 title => ($vis->{title} // $rendered->{title} // 'Visual ' . ($index + 1)),
5625 html => $rendered->{html},
5626 2 ▶ head => ($rendered->{head} // ''),
Condition44%4/9
($vis->{"title"} // $rendered->{"title"}) // 'Visual ' . ($index + 1)
l!l
20
$vis->{"title"} // $rendered->{"title"}
l!l&&r!l&&!r
200
$rendered->{"head"} // ""
l!l
02
_vis_cf_col($vis) // ""
l!l
20
MC/DC0%0/2
($vis->{"title"} // $rendered->{"title"}) // 'Visual ' . ($index + 1)
$vis->{"title"} $rendered->{"title"}
MC/DC0%0/1
$rendered->{"head"} // ""
$rendered->{"head"}
MC/DC0%0/1
_vis_cf_col($vis) // ""
_vis_cf_col($vis)
Truth table33%1/3
A // B
A= $vis->{"title"}B= $rendered->{"title"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $rendered->{"head"}
Aresult
11
00
Truth table50%1/2
A
A= _vis_cf_col($vis)
Aresult
11
00
5627 # The nonce the card's scripts were written with: the page runs
5628 # only those that carry it (runCardScripts in canvas.html.tt).
5629 nonce => $self->csp_nonce,
5630 # The column a chart's marks filter by, so the card appended in
5631 # place is a cross-filter control like the cards around it.
5632 cf_col => (_vis_cf_col($vis) // ''),
5633 },
5634 status => 201,
5635 );
5636 }
5637  
5638 # PATCH /api/analyses/:id/visuals/:visual (change a saved visual's columns)
5639 #
5640 # A map card that plotted the wrong column should not have to be deleted and
5641 # added again: the visual keeps its index - and with it its place in the
5642 # layout, its size, its title and the Leaflet ids a re-render reuses - while
5643 # its C<encode> is replaced. Like the append above, the replacement is drawn
5644 # before it is stored, so an unknown column is a 400 and the saved visual is
5645 # left alone. The optional C<query> is the canvas page's URL state, so a card
5646 # edited while the canvas is cross-filtered is redrawn with the others.
5647 #
5648 # C<cross_row> is repaired the same way, and on the same request: a card's plan
5649 # is stored data like its columns, so a card that was saved with a plan this
5650 # canvas cannot honour - or with none where one is now wanted - is fixed in
5651 # place, keeping its layout slot, its size and its title. The two keys are
5652 # independent, so a repair that renames the column the pass produces writes
5653 # both in one request rather than leaving the card drawn against a column that
5654 # no longer exists. A JSON C<null> is the card giving up its plan and taking
5655 # the page's again, which is the state a card added without one is in; an
5656 # empty plan (C<{ cs => [] }>) is the card running no pass even where the page
5657 # runs one, and leaving the key out is no request to change it at all.
5658 4 ▶sub analyses_update_visual ($self) {
Subroutine analyses_update_visual: called CC=34 SCAR=50.9
Pod documented
5659 4 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
40
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5660 4 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 0 4 4
5661 unless $id =~ /\A[A-Za-z0-9]+\z/;
5662  
5663 4 my $analyses = $self->_read_analyses;
5664 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5665 4 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 0 4 4
5666 4 my $a = $analyses->{$id};
5667  
5668 4 ▶ my $index = $self->stash('visual') // '';
Condition50%1/2
$self->stash("visual") // ""
l!l
40
MC/DC0%0/1
$self->stash("visual") // ""
$self->stash("visual")
Truth table50%1/2
A
A= $self->stash("visual")
Aresult
11
00
5669 4 ▶ return $self->render(json => { error => 'Visual index must be a number' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $index =~ /\A\d+\z/u 1 3 4
5670 unless $index =~ /\A\d+\z/;
5671 3 $index += 0;
5672 return $self->render(json => { error => "No visual at index $index" }, status => 404)
5673 3 ▶ unless $index < scalar @{ $a->{visuals} // [] };
Branch: 1 branch
BranchTrueFalseTotal
unless $index < scalar @{$a->{"visuals"} // [];} 1 2 3
Condition50%1/2
$a->{"visuals"} // []
l!l
30
MC/DC0%0/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table50%1/2
A
A= $a->{"visuals"}
Aresult
11
00
5674  
5675 2 my $body = eval { decode_json($self->req->body) };
5676 2 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 1 1 2
5677 unless ref $body eq 'HASH';
5678  
5679 # A text card's only settings are its options, and it reads no data.
5680 1 ▶ if (($a->{visuals}[$index]{kind} // '') eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if (($a->{"visuals"}[$index]{"kind"} // "") eq "text") 0 1 1
Condition50%1/2
$a->{"visuals"}[$index]{"kind"} // ""
l!l
10
MC/DC0%0/1
$a->{"visuals"}[$index]{"kind"} // ""
$a->{"visuals"}[$index]{"kind"}
Truth table50%1/2
A
A= $a->{"visuals"}[$index]{"kind"}
Aresult
11
00
5681 return $self->render(json => { error => _message_text('error_text_card_columns') }, status => 400)
5682 0 ▶ if exists $body->{encode} || exists $body->{cross_row};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{"encode"} or exists $body->{"cross_row"} 0 0 0
Condition0%0/3
exists $body->{"encode"} or exists $body->{"cross_row"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $body->{"encode"} or exists $body->{"cross_row"}
exists $body->{"encode"} exists $body->{"cross_row"}
Truth table0%0/3
A or B
A= exists $body->{"encode"}B= exists $body->{"cross_row"}
ABresult
1X1
011
000
5683 return $self->render(json => { error => _message_text('error_text_card_options') }, status => 400)
5684 0 ▶ unless ref $body->{options} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body->{"options"} eq "HASH" 0 0 0
5685  
5686 0 my ($options, $errors) = _text_card_options($body->{options});
5687 0 ▶ return $self->render(json => { error => $errors->[0] }, status => 400) if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 0 0 0
5688  
5689 0 my $vis = { %{ $a->{visuals}[$index] }, options => $options };
5690 0 $a->{visuals}[$index] = $vis;
5691 0 $a->{updated} = time();
5692 0 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 0 0 0
5693  
5694 0 return $self->render(json => _text_card_reply($id, $index, $vis));
5695 }
5696  
5697 1 my $has_encode = exists $body->{encode};
5698 1 my $has_plan = exists $body->{cross_row};
5699 1 ▶ return $self->render(json => { error => 'encode or cross_row must be given' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $has_encode or $has_plan 0 1 1
Condition33%1/3
$has_encode or $has_plan
l!l&&r!l&&!r
100
MC/DC0%0/2
$has_encode or $has_plan
$has_encode $has_plan
Truth table33%1/3
A or B
A= $has_encodeB= $has_plan
ABresult
1X1
011
000
5700 unless $has_encode || $has_plan;
5701  
5702 1 my $vis = { %{ $a->{visuals}[$index] } };
5703  
5704 1 ▶ if ($has_encode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_encode) 1 0 1
5705 return $self->render(json => { error => 'encode must be an object' }, status => 400)
5706 1 ▶ unless ref $body->{encode} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body->{"encode"} eq "HASH" 0 1 1
5707 1 $vis->{encode} = { %{ $body->{encode} } };
5708 }
5709  
5710 1 ▶ if ($has_plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_plan) 0 1 1
5711 0 ▶ if (defined $body->{cross_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body->{'cross_row'}) { } 0 0 0
5712 return $self->render(json => { error => 'cross_row must be an object or null' }, status => 400)
5713 0 ▶ unless ref $body->{cross_row} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body->{"cross_row"} eq "HASH" 0 0 0
5714 0 my $plan = _card_plan($body->{cross_row});
5715 return $self->render(json => { error => $plan->{error} }, status => 400)
5716 0 ▶ if $plan->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{"error"} 0 0 0
5717 0 $vis->{cross_row} = $plan;
5718 }
5719 else {
5720 0 delete $vis->{cross_row};
5721 }
5722 }
5723  
5724 1 my $ctx = $self->_canvas_render_context($a, $body->{query}, [$vis]);
5725 return $self->render(json => { error => $ctx->{error} }, status => $ctx->{status})
5726 1 ▶ if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"error"} 0 1 1
5727  
5728 # The card is drawn as it is on this page: a cross-filter applies to every
5729 # card but the one that made it, and this card may be that one.
5730 1 my $records = $ctx->{records};
5731 1 my $prefiltered = 0;
5732 1 my $late_cf = [];
5733 1 ▶ if (@{ $ctx->{cf_specs} } && $ctx->{cf_source} != $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ctx->{"cf_specs"};} and $ctx->{"cf_source"} != $index) 0 1 1
Condition33%1/3
@{$ctx->{"cf_specs"};} and $ctx->{"cf_source"} != $index
!ll&&!rl&&r
100
MC/DC0%0/2
@{$ctx->{"cf_specs"};} and $ctx->{"cf_source"} != $index
@{$ctx->{"cf_specs"};} $ctx->{"cf_source"} != $index
Truth table33%1/3
A and B
A= @{$ctx->{"cf_specs"};}B= $ctx->{"cf_source"} != $index
ABresult
0X0
100
111
5734 0 ($records, $late_cf) = $self->_canvas_cross_filter($records, $ctx->{columns}, $ctx->{cf_specs});
5735 0 $prefiltered = 1;
5736 }
5737  
5738 my $rendered = $self->_render_visual_html(
5739 $vis, $records, $ctx->{columns}, $ctx->{src}, $index, $ctx->{spec_key},
5740 $prefiltered,
5741 $ctx->{cross_row},
5742 1 $late_cf,
5743 );
5744 return $self->render(json => { error => $rendered->{error} }, status => 400)
5745 1 ▶ if $rendered->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $rendered->{"error"} 0 1 1
5746  
5747 1 $a->{visuals}[$index] = $vis;
5748 1 $a->{updated} = time();
5749 1 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 1 0 1
5750  
5751 return $self->render(
5752 json => {
5753 id => $id,
5754 visual => $index,
5755 kind => ($vis->{kind} // ''),
5756 title => ($vis->{title} // $rendered->{title} // 'Visual ' . ($index + 1)),
5757 html => $rendered->{html},
5758 1 ▶ head => ($rendered->{head} // ''),
Condition45%5/11
$vis->{"kind"} // ""
l!l
10
($vis->{"title"} // $rendered->{"title"}) // 'Visual ' . ($index + 1)
l!l
10
$vis->{"title"} // $rendered->{"title"}
l!l&&r!l&&!r
100
$rendered->{"head"} // ""
l!l
01
_vis_cf_col($vis) // ""
l!l
10
MC/DC0%0/1
$vis->{"kind"} // ""
$vis->{"kind"}
MC/DC0%0/2
($vis->{"title"} // $rendered->{"title"}) // 'Visual ' . ($index + 1)
$vis->{"title"} $rendered->{"title"}
MC/DC0%0/1
$rendered->{"head"} // ""
$rendered->{"head"}
MC/DC0%0/1
_vis_cf_col($vis) // ""
_vis_cf_col($vis)
Truth table50%1/2
A
A= $vis->{"kind"}
Aresult
11
00
Truth table33%1/3
A // B
A= $vis->{"title"}B= $rendered->{"title"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $rendered->{"head"}
Aresult
11
00
Truth table50%1/2
A
A= _vis_cf_col($vis)
Aresult
11
00
5759 # The nonce the card's scripts were written with: the page runs
5760 # only those that carry it (runCardScripts in canvas.html.tt).
5761 nonce => $self->csp_nonce,
5762 cf_col => (_vis_cf_col($vis) // ''),
5763 },
5764 );
5765 }
5766  
5767 # DELETE /api/analyses/:id/visuals/:visual (remove a text card)
5768 #
5769 # A visual's index is its identity in the layout and in a cross-filter's cv=, so
5770 # everything that names a later card moves down by one with it. The page reloads
5771 # afterwards, because the cards on screen carry the old numbers.
5772 5 ▶sub analyses_delete_visual ($self) {
Subroutine analyses_delete_visual: called CC=22 SCAR=33.2
Pod documented
5773 5 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
50
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5774 5 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 0 5 5
5775 unless $id =~ /\A[A-Za-z0-9]+\z/;
5776  
5777 5 my $analyses = $self->_read_analyses;
5778 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5779 5 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 1 4 5
5780 4 my $a = $analyses->{$id};
5781  
5782 4 ▶ my $index = $self->stash('visual') // '';
Condition50%1/2
$self->stash("visual") // ""
l!l
40
MC/DC0%0/1
$self->stash("visual") // ""
$self->stash("visual")
Truth table50%1/2
A
A= $self->stash("visual")
Aresult
11
00
5783 4 ▶ return $self->render(json => { error => 'Visual index must be a number' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $index =~ /\A\d+\z/u 1 3 4
5784 unless $index =~ /\A\d+\z/;
5785 3 $index += 0;
5786 return $self->render(json => { error => "No visual at index $index" }, status => 404)
5787 3 ▶ unless $index < scalar @{ $a->{visuals} // [] };
Branch: 1 branch
BranchTrueFalseTotal
unless $index < scalar @{$a->{"visuals"} // [];} 1 2 3
Condition50%1/2
$a->{"visuals"} // []
l!l
30
MC/DC0%0/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table50%1/2
A
A= $a->{"visuals"}
Aresult
11
00
5788 return $self->render(json => { error => _message_text('error_visual_not_text') }, status => 400)
5789 2 ▶ unless ($a->{visuals}[$index]{kind} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless ($a->{"visuals"}[$index]{"kind"} // "") eq "text" 1 1 2
Condition50%1/2
$a->{"visuals"}[$index]{"kind"} // ""
l!l
20
MC/DC0%0/1
$a->{"visuals"}[$index]{"kind"} // ""
$a->{"visuals"}[$index]{"kind"}
Truth table50%1/2
A
A= $a->{"visuals"}[$index]{"kind"}
Aresult
11
00
5790  
5791 1 $a->{visuals} = [ @{ $a->{visuals} }[ grep { $_ != $index } 0 .. $#{ $a->{visuals} } ] ];
5792  
5793 1 my @layout;
5794 1 ▶ for my $entry (@{ $a->{layout} // [] }) {
Condition50%1/2
$a->{"layout"} // []
l!l
10
MC/DC0%0/1
$a->{"layout"} // []
$a->{"layout"}
Truth table50%1/2
A
A= $a->{"layout"}
Aresult
11
00
5795 2 ▶ my $n = $entry->{visual} // '';
Condition50%1/2
$entry->{"visual"} // ""
l!l
20
MC/DC0%0/1
$entry->{"visual"} // ""
$entry->{"visual"}
Truth table50%1/2
A
A= $entry->{"visual"}
Aresult
11
00
5796 2 ▶ if ($n =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\A\d+\z/u) 2 0 2
5797 2 ▶ next if $n == $index;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $index 1 1 2
5798 1 ▶ $entry = { %$entry, visual => $n - 1 } if $n > $index;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $index 0 1 1
5799 }
5800 1 push @layout, $entry;
5801 }
5802 1 ▶ $a->{layout} = \@layout if $a->{layout};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"layout"} 1 0 1
5803  
5804 1 ▶ for my $bm (grep { ref $_ eq 'HASH' && defined $_->{state} && !ref $_->{state} } @{ $a->{bookmarks} // [] }) {
Condition37%3/8
ref $_ eq 'HASH' && defined $_->{'state'} && !ref($_->{'state'})
!ll&&!rl&&r
001
ref $_ eq "HASH" and defined $_->{"state"}
!ll&&!rl&&r
001
$a->{"bookmarks"} // []
l!l
10
MC/DC0%0/3
ref $_ eq 'HASH' && defined $_->{'state'} && !ref($_->{'state'})
ref $_ eq "HASH" defined $_->{"state"} !ref($_->{'state'})
MC/DC0%0/1
$a->{"bookmarks"} // []
$a->{"bookmarks"}
Truth table25%1/4
A and B && not(C)
A= ref $_ eq "HASH"B= defined $_->{"state"}C= !ref($_->{'state'})
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $a->{"bookmarks"}
Aresult
11
00
5805 1 $bm->{state} = _shift_card_param($bm->{state}, $index);
5806 }
5807  
5808 1 $a->{updated} = time();
5809 1 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 1 0 1
5810  
5811 1 return $self->render(json => { deleted => 1 });
5812 }
5813  
5814 # _shift_card_param($search, $removed) -> $search
5815 #
5816 # A saved query string with its cv= moved down if the card it names came after
5817 # the one removed. The canvas never makes a text card a cross-filter's source,
5818 # but a bookmark is stored as PATCHed, so a cv= naming the removed card is
5819 # dropped: kept, it would name the card that moved into its place. A cv= of
5820 # more than nine digits names no card (_cross_filter_source), so it is left
5821 # as it is: arithmetic on it came back as cv=1e+20. A search or index that is
5822 # not one is returned unchanged.
5823 10 ▶sub _shift_card_param {
Subroutine _shift_card_param: called CC=9 SCAR=26.1
5824 10 my ($search, $removed) = @_;
5825 10 ▶ return $search unless defined($search) && !ref($search)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search and not ref $search and defined $removed and not ref $removed and $removed =~ /\A[0-9]{1,9}\z/u 0 10 10
Condition33%4/12
defined $search and not ref $search and defined $removed and not ref $removed and $removed =~ /\A[0-9]{1,9}\z/u
!ll&&!rl&&r
0010
defined $search and not ref $search and defined $removed and not ref $removed
!ll&&!rl&&r
0010
defined $search and not ref $search and defined $removed
!ll&&!rl&&r
0010
defined $search and not ref $search
!ll&&!rl&&r
0010
MC/DC0%0/5
defined $search and not ref $search and defined $removed and not ref $removed and $removed =~ /\A[0-9]{1,9}\z/u
defined $search not ref $search defined $removed not ref $removed $removed =~ /\A[0-9]{1,9}\z/u
Truth table16%1/6
A and not(B) and C and not(D) and E
A= defined $searchB= not ref $searchC= defined $removedD= not ref $removedE= $removed =~ /\A[0-9]{1,9}\z/u
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
5826 && defined($removed) && !ref($removed) && $removed =~ /\A[0-9]{1,9}\z/;
5827 10 $search =~ s{(?<![^?&])cv=(\d+)(&?)}{
5828 10 ▶ length($1) > 9 ? "cv=$1$2"
Branch: 3 branches
BranchTrueFalseTotal
length $1 > 9 ? : 0 10 10
$1 == $removed ? : 5 5 10
$1 > $removed ? : 3 2 5
5829 : $1 == $removed ? ''
5830 : ('cv=' . ($1 > $removed ? $1 - 1 : $1) . $2)
5831 }ge;
5832 10 $search =~ s{&\z}{};
5833 10 return $search;
5834 }
5835  
5836 # _text_card_options($raw) -> (\%options, \@errors)
5837 #
5838 # Purpose: The one reader of a text card's settings.
5839 # Entry: Whatever is stored or was sent; it may be any shape.
5840 # Exit: The settings that are allowed, each in its one spelling, and a message
5841 # for each that was not. Settings not asked for are absent.
5842 # Notes: A write refuses a card with errors. A render ignores them and draws
5843 # what is left, because PUT, PATCH and import can store a card that
5844 # never came through here, and nothing else stands between a stored
5845 # value and a style attribute.
5846 37 ▶sub _text_card_options {
Subroutine _text_card_options: called CC=27 SCAR=33.0
5847 37 my ($raw) = @_;
5848 37 ▶ $raw = {} unless ref $raw eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 5 32 37
5849  
5850 37 my (%clean, @errors);
5851  
5852 37 ▶ my $text = (defined $raw->{text} && !ref $raw->{text}) ? $raw->{text} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw->{'text'} && !ref($raw->{'text'}) ? : 31 6 37
Condition100%3/3
defined $raw->{"text"} and not ref $raw->{"text"}
!ll&&!rl&&r
5131
MC/DC100%2/2
defined $raw->{"text"} and not ref $raw->{"text"}
defined $raw->{"text"} not ref $raw->{"text"}
Truth table100%3/3
A and not(B)
A= defined $raw->{"text"}B= not ref $raw->{"text"}
ABresult
0X0
100
111
5853 37 $text =~ s/\r\n?/\n/g;
5854 37 ▶ if ($text !~ /\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $text =~ /\S/u) { } 8 29 37
elsif (length $text > $MAX_TEXT_CHARS) { } 1 28 29
5855 8 push @errors, _message_text('error_text_card_empty');
5856 }
5857 elsif (length($text) > $MAX_TEXT_CHARS) {
5858 1 push @errors, sprintf(_message_text('error_text_card_long'), $MAX_TEXT_CHARS);
5859 }
5860 37 $clean{text} = $text;
5861  
5862 37 ▶ if (length(my $font = $raw->{font} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $font = $raw->{"font"} // "")) 5 32 37
Condition100%2/2
$raw->{"font"} // ""
l!l
532
MC/DC100%1/1
$raw->{"font"} // ""
$raw->{"font"}
Truth table100%2/2
A
A= $raw->{"font"}
Aresult
11
00
5863 5 ▶ if (!ref $font && exists $TEXT_FONT_CSS{$font}) { $clean{font} = $font }
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $font and exists $TEXT_FONT_CSS{$font}) { } 2 3 5
Condition100%3/3
not ref $font and exists $TEXT_FONT_CSS{$font}
!ll&&!rl&&r
122
MC/DC100%2/2
not ref $font and exists $TEXT_FONT_CSS{$font}
not ref $font exists $TEXT_FONT_CSS{$font}
Truth table100%3/3
not(A) and B
A= not ref $fontB= exists $TEXT_FONT_CSS{$font}
ABresult
0X0
100
111
5864 3 else { push @errors, _message_text('error_text_card_font') }
5865 }
5866  
5867 37 ▶ if (length(my $size = $raw->{size} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $size = $raw->{"size"} // "")) 8 29 37
Condition100%2/2
$raw->{"size"} // ""
l!l
829
MC/DC100%1/1
$raw->{"size"} // ""
$raw->{"size"}
Truth table100%2/2
A
A= $raw->{"size"}
Aresult
11
00
5868 8 ▶ if (!ref $size && $size =~ /\A\d{1,3}\z/ && $size >= $MIN_TEXT_POINTS && $size <= $MAX_TEXT_POINTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $size and $size =~ /\A\d{1,3}\z/u and $size >= $MIN_TEXT_POINTS and $size <= $MAX_TEXT_POINTS) { } 1 7 8
Condition100%9/9
not ref $size and $size =~ /\A\d{1,3}\z/u and $size >= $MIN_TEXT_POINTS and $size <= $MAX_TEXT_POINTS
!ll&&!rl&&r
611
not ref $size and $size =~ /\A\d{1,3}\z/u and $size >= $MIN_TEXT_POINTS
!ll&&!rl&&r
512
not ref $size and $size =~ /\A\d{1,3}\z/u
!ll&&!rl&&r
143
MC/DC100%4/4
not ref $size and $size =~ /\A\d{1,3}\z/u and $size >= $MIN_TEXT_POINTS and $size <= $MAX_TEXT_POINTS
not ref $size $size =~ /\A\d{1,3}\z/u $size >= $MIN_TEXT_POINTS $size <= $MAX_TEXT_POINTS
Truth table100%5/5
not(A) and B and C and D
A= not ref $sizeB= $size =~ /\A\d{1,3}\z/uC= $size >= $MIN_TEXT_POINTSD= $size <= $MAX_TEXT_POINTS
ABCDresult
0XXX0
10XX0
110X0
11100
11111
5869 1 $clean{size} = $size + 0;
5870 }
5871 else {
5872 7 push @errors, sprintf(_message_text('error_text_card_size'), $MIN_TEXT_POINTS, $MAX_TEXT_POINTS);
5873 }
5874 }
5875  
5876 37 for my $key (qw(colour background)) {
5877 74 ▶ my $value = $raw->{$key} // '';
Condition100%2/2
$raw->{$key} // ""
l!l
1163
MC/DC100%1/1
$raw->{$key} // ""
$raw->{$key}
Truth table100%2/2
A
A= $raw->{$key}
Aresult
11
00
5878 74 ▶ next unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 63 11 74
5879 11 ▶ if (!ref $value && $value =~ /\A#[0-9a-fA-F]{6}\z/) { $clean{$key} = lc $value }
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and $value =~ /\A#[0-9a-fA-F]{6}\z/u) { } 2 9 11
Condition100%3/3
not ref $value and $value =~ /\A#[0-9a-fA-F]{6}\z/u
!ll&&!rl&&r
182
MC/DC100%2/2
not ref $value and $value =~ /\A#[0-9a-fA-F]{6}\z/u
not ref $value $value =~ /\A#[0-9a-fA-F]{6}\z/u
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /\A#[0-9a-fA-F]{6}\z/u
ABresult
0X0
100
111
5880 9 else { push @errors, _message_text('error_text_card_colour') }
5881 }
5882  
5883 37 ▶ if (length(my $align = $raw->{align} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $align = $raw->{"align"} // "")) 2 35 37
Condition100%2/2
$raw->{"align"} // ""
l!l
235
MC/DC100%1/1
$raw->{"align"} // ""
$raw->{"align"}
Truth table100%2/2
A
A= $raw->{"align"}
Aresult
11
00
5884 2 ▶ if (!ref $align && $TEXT_ALIGNS{$align}) { $clean{align} = $align }
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $align and $TEXT_ALIGNS{$align}) { } 1 1 2
Condition66%2/3
not ref $align and $TEXT_ALIGNS{$align}
!ll&&!rl&&r
011
MC/DC50%1/2
not ref $align and $TEXT_ALIGNS{$align}
not ref $align $TEXT_ALIGNS{$align}
Truth table66%2/3
not(A) and B
A= not ref $alignB= $TEXT_ALIGNS{$align}
ABresult
0X0
100
111
5885 1 else { push @errors, _message_text('error_text_card_align') }
5886 }
5887  
5888 37 for my $key (qw(bold italic plain)) {
5889 111 ▶ $clean{$key} = 1 if $raw->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $raw->{$key} 4 107 111
5890 }
5891  
5892 37 return (\%clean, \@errors);
5893 }
5894  
5895 # _text_card_markup(\%visual) -> $html
5896 #
5897 # The body of a text card. The text is escaped and the style holds only values
5898 # _text_card_options has checked. Spaces and line ends are written as entities
5899 # and <br>, not left to CSS, because the response minifier collapses whitespace
5900 # and the embed page has none of this page's styles.
5901 7 ▶sub _text_card_markup {
Subroutine _text_card_markup: called CC=9 SCAR=22.3
5902 7 my ($vis) = @_;
5903 7 my ($o) = _text_card_options($vis->{options});
5904  
5905 7 my @style;
5906 7 ▶ push @style, "font-family:$TEXT_FONT_CSS{$o->{font}}" if $o->{font};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"font"} 1 6 7
5907 7 ▶ push @style, "font-size:$o->{size}pt" if $o->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"size"} 0 7 7
5908 7 ▶ push @style, "color:$o->{colour}" if $o->{colour};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"colour"} 0 7 7
5909 7 ▶ push @style, "background-color:$o->{background}" if $o->{background};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"background"} 0 7 7
5910 7 ▶ push @style, 'font-weight:bold' if $o->{bold};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"bold"} 1 6 7
5911 7 ▶ push @style, 'font-style:italic' if $o->{italic};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"italic"} 0 7 7
5912 7 ▶ push @style, "text-align:$o->{align}" if $o->{align};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"align"} 0 7 7
5913 7 ▶ my $style = @style ? ' style="' . xml_escape(join ';', @style) . '"' : '';
Branch: 1 branch
BranchTrueFalseTotal
@style ? : 1 6 7
5914  
5915 7 my $text = xml_escape($o->{text});
5916 7 $text =~ s/\t/ /g;
5917 7 $text =~ s/^ /&#160;/mg;
5918 7 $text =~ s/ (?= )/&#160;/g;
5919 7 $text =~ s/\n/<br>/g;
5920  
5921 7 return qq{<div class="text-card-text"$style>$text</div>};
5922 }
5923  
5924 # _text_card_title(\%visual) -> $title
5925 #
5926 # What names a text card to the controls on it and to a screen reader: the start
5927 # of its first line that has anything on it.
5928 7 ▶sub _text_card_title {
Subroutine _text_card_title: called CC=3 SCAR=11.0
5929 7 my ($vis) = @_;
5930 7 my ($o) = _text_card_options($vis->{options});
5931 7 my ($line) = $o->{text} =~ /^[ \t]*(\S[^\n]*)/m;
5932 7 ▶ return 'Text' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 5 7
5933 5 $line =~ s/\s+\z//;
5934 5 ▶ return length($line) > $TEXT_TITLE_CHARS ? substr($line, 0, $TEXT_TITLE_CHARS) . '...' : $line;
Branch: 1 branch
BranchTrueFalseTotal
length $line > $TEXT_TITLE_CHARS ? : 1 4 5
5935 }
5936  
5937 # _text_card_plain(\%visual) -> $bool
5938 #
5939 # Whether the card hides its frame and header until it is pointed at.
5940 32 ▶sub _text_card_plain {
Subroutine _text_card_plain: called CC=4 SCAR=13.9
5941 32 my ($vis) = @_;
5942 32 ▶ return 0 unless ($vis->{kind} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless ($vis->{"kind"} // "") eq "text" 29 3 32
Condition100%2/2
$vis->{"kind"} // ""
l!l
248
MC/DC100%1/1
$vis->{"kind"} // ""
$vis->{"kind"}
Truth table100%2/2
A
A= $vis->{"kind"}
Aresult
11
00
5943 3 my ($o) = _text_card_options($vis->{options});
5944 3 ▶ return $o->{plain} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o->{'plain'} ? : 1 2 3
5945 }
5946  
5947 # _text_card_reply($id, $index, \%visual) -> \%json
5948 2 ▶sub _text_card_reply {
Subroutine _text_card_reply: called CC=2 SCAR=7.1
5949 2 my ($id, $index, $vis) = @_;
5950 return {
5951 id => $id,
5952 visual => $index,
5953 kind => 'text',
5954 2 ▶ title => ($vis->{title} // _text_card_title($vis)),
Condition66%2/3
$vis->{"title"} // _text_card_title($vis)
l!l&&r!l&&!r
110
MC/DC0%0/2
$vis->{"title"} // _text_card_title($vis)
$vis->{"title"} _text_card_title($vis)
Truth table66%2/3
A // B
A= $vis->{"title"}B= _text_card_title($vis)
ABresult
1X1
011
000
5955 html => _text_card_markup($vis),
5956 head => '',
5957 cf_col => '',
5958 plain => _text_card_plain($vis),
5959 };
5960 }
5961  
5962 # GET /api/analyses/:id/export
5963 21 ▶sub analyses_export ($self) {
Subroutine analyses_export: called CC=4 SCAR=13.9
Pod documented
5964 21 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
210
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
5965 21 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 2 19 21
5966 unless $id =~ /\A[A-Za-z0-9]+\z/;
5967  
5968 19 my $analyses = $self->_read_analyses;
5969 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
5970 19 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 4 15 19
5971  
5972 15 my $json = encode_json($analyses->{$id});
5973 15 $self->res->headers->content_disposition("attachment; filename=\"analysis-$id.json\"");
5974 15 $self->render(data => $json, format => 'json');
5975 }
5976  
5977 # POST /api/analyses/import (create from uploaded JSON or raw body)
5978 13 ▶sub analyses_import ($self) {
Subroutine analyses_import: called CC=9 SCAR=22.5
Pod documented
5979 13 my $upload = $self->req->upload('file');
5980 13 ▶ my $body_json = $upload ? $upload->slurp : $self->req->body;
Branch: 1 branch
BranchTrueFalseTotal
$upload ? : 0 13 13
5981  
5982 13 my $body = eval { decode_json($body_json) };
5983 13 ▶ return $self->render(json => { error => _analysis_json_error($@) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 7 6 13
5984 unless ref $body eq 'HASH';
5985  
5986 # Strip any id/timestamps from imported document; assign fresh ones below
5987 6 delete @{$body}{qw( id created updated )};
5988 6 _drop_unknown_keys($body);
5989  
5990 6 my $err = $self->_validate_analysis($body);
5991 6 ▶ return $self->render(json => { error => sprintf(_message_text('error_analysis_invalid'), $err) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if $err 4 2 6
5992 if $err;
5993  
5994 2 ▶ unless ($self->_analyses_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_analyses_path) 0 2 2
5995 0 return $self->render(json => { error => _message_text('error_analysis_storage') }, status => 503);
5996 }
5997  
5998 2 my $analyses = $self->_read_analyses;
5999  
6000 2 my $id;
6001 2 my $tries = 0;
6002 2 do { $id = $self->_gen_analysis_id; $tries++ }
6003 2 ▶ while (exists $analyses->{$id} && $tries < 100);
Branch: 1 branch
BranchTrueFalseTotal
exists $analyses->{$id} and $tries < 100 and do { do { $id = $self->_gen_analysis_id; ++$tries } } 0 2 2
Condition33%1/3
exists $analyses->{$id} and $tries < 100
!ll&&!rl&&r
200
MC/DC0%0/2
exists $analyses->{$id} and $tries < 100
exists $analyses->{$id} $tries < 100
Truth table33%1/3
A and B
A= exists $analyses->{$id}B= $tries < 100
ABresult
0X0
100
111
6004  
6005 2 $body->{id} = $id;
6006 2 ▶ $body->{version} //= 1;
Condition50%1/2
$body->{"version"} //= 1
l!l
20
MC/DC0%0/1
$body->{"version"} //= 1
$body->{"version"}
Truth table50%1/2
A
A= $body->{"version"}
Aresult
11
00
6007 2 $body->{created} = time();
6008 2 $body->{updated} = time();
6009  
6010 2 $analyses->{$id} = $body;
6011 2 ▶ $self->_save_analyses_or_503($analyses) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_save_analyses_or_503($analyses) or return 2 0 2
6012  
6013 2 $self->res->headers->location("/api/analyses/$id");
6014 2 $self->render(json => { id => $id, imported => 1 }, status => 201);
6015 }
6016  
6017 # _cross_filter_source($cv) -> Int
6018 #
6019 # The index of the card a cross-filter came from (cv=), or -1 for none. Only
6020 # a whole number names a card: int('abc') is 0, which would draw the first
6021 # card unfiltered as if it were the source.
6022 52 ▶sub _cross_filter_source {
Subroutine _cross_filter_source: called CC=3 SCAR=11.0
6023 52 my $cv = shift;
6024 52 ▶ return (defined($cv) && $cv =~ /\A[0-9]{1,9}\z/) ? int($cv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cv && $cv =~ /\A[0-9]{1,9}\z/u ? : 3 49 52
Condition100%3/3
defined $cv and $cv =~ /\A[0-9]{1,9}\z/u
!ll&&!rl&&r
39103
MC/DC100%2/2
defined $cv and $cv =~ /\A[0-9]{1,9}\z/u
defined $cv $cv =~ /\A[0-9]{1,9}\z/u
Truth table100%3/3
A and B
A= defined $cvB= $cv =~ /\A[0-9]{1,9}\z/u
ABresult
0X0
100
111
6025 }
6026  
6027 # _build_canvas_pipeline($self, \%analysis) -> ($src, $records, \@cols) or ()
6028 #
6029 # Opens the first dataset from a saved analysis and chains any declared joins.
6030 # Returns the base pipeline triple for _render_visual_html.
6031 # Returns an empty list on any error so canvas_render can respond 422.
6032 # _canvas_render_context($self, \%analysis, $query) -> \%ctx | { error, status }
6033 #
6034 # The state a card is drawn through when it is rendered outside canvas_render:
6035 # the base pipeline, calculated columns, bound parameters and the cross-filters
6036 # named by a canvas page's query string. C<cf_specs> are returned unapplied so
6037 # the caller can decide which card, if any, is their source; C<cf_source> is
6038 # the C<cv> index, or -1 when the page names none. C<cross_row> is the
6039 # validated cs= plan, undef when the page names none, which the caller passes to
6040 # _render_visual_html so each card runs it over its own rows. Only C<cf>,
6041 # C<cv>, C<c>, C<cs>, C<by>, C<order>, C<range>, C<rcol> and C<p.<name>> are read
6042 # from C<$query> - the same keys canvas_render reads from its own URL - so a
6043 # page's state is not a way to reach anything else.
6044 28 ▶sub _canvas_render_context :Protected ($self, $a, $query, $more_cards = undef) {
Subroutine _canvas_render_context: called CC=21 SCAR=31.2
6045 28 my $params;
6046 28 ▶ if (defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) 23 5 28
6047 23 ▶ return { error => 'query must be a string', status => 400 }
Branch: 1 branch
BranchTrueFalseTotal
if ref $query or length $query > 4096 2 21 23
Condition100%3/3
ref $query or length $query > 4096
l!l&&r!l&&!r
1121
MC/DC100%2/2
ref $query or length $query > 4096
ref $query length $query > 4096
Truth table100%3/3
A or B
A= ref $queryB= length $query > 4096
ABresult
1X1
011
000
6048 if ref $query || length($query) > 4096;
6049 21 (my $qs = $query) =~ s/\A\?//;
6050 21 $params = Mojo::Parameters->new($qs);
6051 }
6052  
6053 26 my ($src, $records, $columns) = $self->_build_canvas_pipeline($a);
6054 26 ▶ return { error => 'Cannot open data source for this canvas', status => 422 }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 25 26
6055 unless defined $records;
6056  
6057 my ($bound_ref, $p_errors) = $self->_bound_canvas_params(
6058 2 ▶ $a, sub { $params ? $params->param($_[0]) : undef },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 2 0 2
6059 25 );
6060 25 ▶ return { error => join('; ', @$p_errors), status => 400 }
Branch: 1 branch
BranchTrueFalseTotal
if @$p_errors 0 25 25
6061 if @$p_errors;
6062 25 my %param_values = %$bound_ref;
6063  
6064 25 ▶ my $window_names = _canvas_window_columns($a, $params || Mojo::Parameters->new, $more_cards);
Condition66%2/3
$params || 'Mojo::Parameters'->new
l!l&&r!l&&!r
2050
MC/DC0%0/2
$params || 'Mojo::Parameters'->new
$params 'Mojo::Parameters'->new
Truth table66%2/3
A || B
A= $paramsB= 'Mojo::Parameters'->new
ABresult
1X1
011
000
6065 25 $self->stash('_canvas_window_names' => $window_names);
6066  
6067 25 eval {
6068 ($records, $columns) = $self->_apply_calc_fields(
6069 $records, $columns,
6070 {
6071 params => \%param_values,
6072 spec_key => ($a->{datasets}[0]{spec} // ''),
6073 25 ▶ calc => [ $params ? @{ $params->every_param('c') // [] } : () ],
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 20 5 25
Condition50%2/4
$a->{"datasets"}[0]{"spec"} // ""
l!l
250
$params->every_param("c") // []
l!l
200
MC/DC0%0/1
$a->{"datasets"}[0]{"spec"} // ""
$a->{"datasets"}[0]{"spec"}
MC/DC0%0/1
$params->every_param("c") // []
$params->every_param("c")
Truth table50%1/2
A
A= $a->{"datasets"}[0]{"spec"}
Aresult
11
00
Truth table50%1/2
A
A= $params->every_param("c")
Aresult
11
00
6074 window_columns => $window_names,
6075 }
6076 );
6077 };
6078 25 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 24 25
6079 1 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
6080 1 return { error => $e, status => 400 };
6081 }
6082  
6083 # The canvas names cs= once and every card runs the same plan over its own
6084 # rows (see _render_visual_html). Validated here, against the columns the
6085 # cards will be drawn from, so a card added or edited in place fails with
6086 # the same 400 the page itself would give.
6087 24 ▶ my $cross_row = $self->_cross_row_specs($columns, $params || Mojo::Parameters->new);
Condition66%2/3
$params || 'Mojo::Parameters'->new
l!l&&r!l&&!r
1950
MC/DC0%0/2
$params || 'Mojo::Parameters'->new
$params 'Mojo::Parameters'->new
Truth table66%2/3
A || B
A= $paramsB= 'Mojo::Parameters'->new
ABresult
1X1
011
000
6088 return { error => $cross_row->{error}, status => 400 }
6089 24 ▶ if $cross_row && $cross_row->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $cross_row and $cross_row->{"error"} 0 24 24
Condition33%1/3
$cross_row and $cross_row->{"error"}
!ll&&!rl&&r
2400
MC/DC0%0/2
$cross_row and $cross_row->{"error"}
$cross_row $cross_row->{"error"}
Truth table33%1/3
A and B
A= $cross_rowB= $cross_row->{"error"}
ABresult
0X0
100
111
6090  
6091 24 ▶ my @cf_specs = $params ? grep { /\S/ } @{ $params->every_param('cf') // [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 19 5 24
Condition50%1/2
$params->every_param("cf") // []
l!l
190
MC/DC0%0/1
$params->every_param("cf") // []
$params->every_param("cf")
Truth table50%1/2
A
A= $params->every_param("cf")
Aresult
11
00
6092 24 ▶ my $cf_source = _cross_filter_source($params ? $params->param('cv') : undef);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 19 5 24
6093  
6094 # The relative time range the canvas page names in its query string is part
6095 # of the state a card is drawn through, like cf= and the bound parameters,
6096 # so a visual added while a range is active shows the same rows as the
6097 # cards already on screen.
6098 24 ▶ $records = _apply_time_range(
Branch: 2 branches
BranchTrueFalseTotal
$params ? : 19 5 24
$params ? : 19 5 24
6099 $records,
6100 $params ? $params->param('range') : undef,
6101 $params ? $params->param('rcol') : undef,
6102 );
6103  
6104 return {
6105 src => $src,
6106 records => $records,
6107 columns => $columns,
6108 24 ▶ spec_key => ($a->{datasets}[0]{spec} // ''),
Condition50%1/2
$a->{"datasets"}[0]{"spec"} // ""
l!l
240
MC/DC0%0/1
$a->{"datasets"}[0]{"spec"} // ""
$a->{"datasets"}[0]{"spec"}
Truth table50%1/2
A
A= $a->{"datasets"}[0]{"spec"}
Aresult
11
00
6109 cf_specs => \@cf_specs,
6110 cf_source => $cf_source,
6111 cross_row => $cross_row,
6112 };
6113 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
6114  
6115 49 ▶sub _build_canvas_pipeline :Protected ($self, $analysis) {
Subroutine _build_canvas_pipeline: called CC=21 SCAR=33.5
6116 49 ▶ my @datasets = @{ $analysis->{datasets} // [] };
Condition100%2/2
$analysis->{"datasets"} // []
l!l
481
MC/DC100%1/1
$analysis->{"datasets"} // []
$analysis->{"datasets"}
Truth table100%2/2
A
A= $analysis->{"datasets"}
Aresult
11
00
6117 49 ▶ return () unless @datasets;
Branch: 1 branch
BranchTrueFalseTotal
unless @datasets 2 47 49
6118  
6119 47 ▶ my $left_spec = $datasets[0]{spec} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $left_spec = $datasets[0]{"spec"} or return () 46 1 47
6120 46 my ($left_src) = $self->_open_spec($left_spec);
6121 46 ▶ return () unless $left_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $left_src 1 45 46
6122  
6123 45 ▶ my $max_rows = $self->app->config('join_max_rows') // $DEFAULT_JOIN_MAX_ROWS;
Condition33%1/3
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
l!l&&r!l&&!r
0450
MC/DC0%0/2
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
$self->app->config("join_max_rows") $DEFAULT_JOIN_MAX_ROWS
Truth table33%1/3
A // B
A= $self->app->config("join_max_rows")B= $DEFAULT_JOIN_MAX_ROWS
ABresult
1X1
011
000
6124 45 my $src = $left_src;
6125  
6126 # Build alias -> spec lookup for join resolution.
6127 45 my %spec_by_alias;
6128 45 for my $ds (@datasets) {
6129 46 ▶ $spec_by_alias{ $ds->{alias} // '' } = $ds->{spec} // '';
Condition75%3/4
$ds->{"spec"} // ""
l!l
460
$ds->{"alias"} // ""
l!l
640
MC/DC0%0/1
$ds->{"spec"} // ""
$ds->{"spec"}
MC/DC100%1/1
$ds->{"alias"} // ""
$ds->{"alias"}
Truth table50%1/2
A
A= $ds->{"spec"}
Aresult
11
00
Truth table100%2/2
A
A= $ds->{"alias"}
Aresult
11
00
6130 }
6131  
6132 45 ▶ for my $j (@{ $analysis->{joins} // [] }) {
Condition100%2/2
$analysis->{"joins"} // []
l!l
144
MC/DC100%1/1
$analysis->{"joins"} // []
$analysis->{"joins"}
Truth table100%2/2
A
A= $analysis->{"joins"}
Aresult
11
00
6133 2 ▶ my $right_spec = $spec_by_alias{ $j->{right} // '' } // '';
Condition100%4/4
$spec_by_alias{$j->{"right"} // ""} // ""
l!l
11
$j->{"right"} // ""
l!l
11
MC/DC100%1/1
$spec_by_alias{$j->{"right"} // ""} // ""
$spec_by_alias{$j->{"right"} // ""}
MC/DC100%1/1
$j->{"right"} // ""
$j->{"right"}
Truth table100%2/2
A
A= $spec_by_alias{$j->{"right"} // ""}
Aresult
11
00
Truth table100%2/2
A
A= $j->{"right"}
Aresult
11
00
6134 2 ▶ next unless length $right_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless length $right_spec 1 1 2
6135 1 ▶ my $left_key = $j->{left_key} // '';
Condition50%1/2
$j->{"left_key"} // ""
l!l
01
MC/DC0%0/1
$j->{"left_key"} // ""
$j->{"left_key"}
Truth table50%1/2
A
A= $j->{"left_key"}
Aresult
11
00
6136 1 ▶ my $right_key = $j->{right_key} // $left_key;
Condition33%1/3
$j->{"right_key"} // $left_key
l!l&&r!l&&!r
010
MC/DC0%0/2
$j->{"right_key"} // $left_key
$j->{"right_key"} $left_key
Truth table33%1/3
A // B
A= $j->{"right_key"}B= $left_key
ABresult
1X1
011
000
6137 1 ▶ next unless length $left_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_key 1 0 1
6138  
6139 0 my ($right_src) = $self->_open_spec($right_spec);
6140 0 ▶ next unless $right_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_src 0 0 0
6141  
6142 0 require Database::Join;
6143 0 ▶ $src = Database::Join->new(
Branch: 1 branch
BranchTrueFalseTotal
$left_key ne $right_key ? : 0 0 0
6144 databases => [$src, $right_src],
6145 join_column => $left_key,
6146 backend => 'auto',
6147 max_array_rows => $max_rows,
6148 ($left_key ne $right_key ? (join_map => {1 => $right_key}) : ()),
6149 );
6150 }
6151  
6152 45 my $records = eval { $src->selectall_arrayref };
6153 45 ▶ return () if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 45 45
6154 45 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
450
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
6155 45 my @columns = _get_columns($src, $records);
6156  
6157 45 return ($src, $records, \@columns);
6158 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
6159  
6160 # _chart_json($self, \%chart_spec) -> $json_for_a_data_attribute
6161 #
6162 # The chart templates put this in the New canvas panel's data-chart attribute
6163 # so the canvas buttons can save the chart exactly as it is drawn, including
6164 # options the URL does not carry. Escape '<' as well, so the value is inert
6165 # even if a title or column name contains markup.
6166 298 ▶sub _chart_json :Protected ($self, $chart) {
Subroutine _chart_json: called CC=1 SCAR=0.0
6167 298 my $json = encode_json($chart);
6168 298 $json =~ s{<}{\\u003c}g;
6169 298 return $json;
6170 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
6171  
6172 # _chart_orientation(\%options) -> 'horizontal' | 'vertical'
6173 #
6174 # The chart pages store a bar's direction as options.orientation, the spelling
6175 # their URL uses, while the canvas used to read options.orient; both are read
6176 # here. The values the pages emit are v/h and vertical/horizontal.
6177 21 ▶sub _chart_orientation {
Subroutine _chart_orientation: called CC=4 SCAR=13.9
6178 21 my ($options) = @_;
6179  
6180 21 ▶ my $orient = $options->{orientation} // $options->{orient} // '';
Condition100%5/5
($options->{"orientation"} // $options->{"orient"}) // ""
l!l
714
$options->{"orientation"} // $options->{"orient"}
l!l&&r!l&&!r
6114
MC/DC100%2/2
($options->{"orientation"} // $options->{"orient"}) // ""
$options->{"orientation"} $options->{"orient"}
Truth table100%3/3
A // B
A= $options->{"orientation"}B= $options->{"orient"}
ABresult
1X1
011
000
6181 21 ▶ return ($orient =~ /\A(?:h|horizontal)\z/i) ? 'horizontal' : 'vertical';
Branch: 1 branch
BranchTrueFalseTotal
$orient =~ /\A(?:h|horizontal)\z/iu ? : 4 17 21
6182 }
6183  
6184 # _visual_measure($agg_fn, $val_col) -> \%measure
6185 #
6186 # The measure a canvas card aggregates a value column with: C<{ fn, val,
6187 # label }> when the card can be drawn, or C<{ error }> when it cannot, which
6188 # the card reports in place of its chart.
6189 #
6190 # A card that names no function keeps the default it has always had: count the
6191 # rows when it names no value column, sum it when it does. A named function is
6192 # the measure vocabulary the pivot and the stats page share, with a percentile
6193 # riding in the name as p<N> -- p90, p50 -- exactly as a pivot's measure spells
6194 # it, so a card needs no second key to keep in step. 'count' counts rows, so it
6195 # ignores the value column and needs none.
6196 #
6197 # label is what the card's title calls the result: 'Count', the column's own
6198 # name for a plain sum, and "fn(col)" for anything else, so two cards over one
6199 # column do not read alike when one of them is a median. An explicit sum keeps
6200 # the plain name, because naming the default should not change a card.
6201 #
6202 # The name itself is not checked here: _render_visual_html validates it once,
6203 # before the dispatch, so that a card of any kind reports a function it cannot
6204 # compute -- including the kinds that never call this.
6205 47 ▶sub _visual_measure {
Subroutine _visual_measure: called CC=14 SCAR=26.4
6206 47 my ($agg_fn, $val_col) = @_;
6207 47 ▶ my $count_col = (!defined $val_col || !length $val_col || $val_col eq '__count__');
Condition100%6/6
!(defined $val_col && length $val_col) || $val_col eq '__count__'
l!l&&r!l&&!r
10235
defined $val_col && length $val_col
!ll&&!rl&&r
4637
MC/DC100%3/3
!(defined $val_col && length $val_col) || $val_col eq '__count__'
defined $val_col length $val_col $val_col eq '__count__'
Truth table100%4/4
not(A && B) || C
A= defined $val_colB= length $val_colC= $val_col eq '__count__'
ABCresult
0XX1
10X1
1111
1100
6208 # An absent name and an empty one are the same non-choice, so the default is
6209 # one branch rather than two.
6210 47 ▶ my $named = (defined $agg_fn && !ref $agg_fn && length $agg_fn) ? $agg_fn : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $agg_fn && !ref($agg_fn) && length $agg_fn ? : 27 20 47
Condition83%5/6
defined $agg_fn and not ref $agg_fn and length $agg_fn
!ll&&!rl&&r
19127
defined $agg_fn and not ref $agg_fn
!ll&&!rl&&r
19028
MC/DC66%2/3
defined $agg_fn and not ref $agg_fn and length $agg_fn
defined $agg_fn not ref $agg_fn length $agg_fn
Truth table75%3/4
A and not(B) and C
A= defined $agg_fnB= not ref $agg_fnC= length $agg_fn
ABCresult
0XX0
10X0
1100
1111
6211  
6212 47 ▶ return { fn => 'count', val => undef, label => 'Count' }
Branch: 1 branch
BranchTrueFalseTotal
if defined $named and $named eq "count" 2 45 47
Condition100%3/3
defined $named and $named eq "count"
!ll&&!rl&&r
20252
MC/DC100%2/2
defined $named and $named eq "count"
defined $named $named eq "count"
Truth table100%3/3
A and B
A= defined $namedB= $named eq "count"
ABresult
0X0
100
111
6213 if defined $named && $named eq 'count';
6214 45 ▶ return { fn => 'count', val => undef, label => 'Count' }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $named and $count_col 6 39 45
Condition100%3/3
not defined $named and $count_col
!ll&&!rl&&r
25146
MC/DC100%2/2
not defined $named and $count_col
not defined $named $count_col
Truth table100%3/3
not(A) and B
A= not defined $namedB= $count_col
ABresult
0X0
100
111
6215 if !defined $named && $count_col;
6216 39 ▶ return { error => "Aggregate '$named' needs a value column" }
Branch: 1 branch
BranchTrueFalseTotal
if defined $named and $count_col 5 34 39
Condition100%3/3
defined $named and $count_col
!ll&&!rl&&r
14205
MC/DC100%2/2
defined $named and $count_col
defined $named $count_col
Truth table100%3/3
A and B
A= defined $namedB= $count_col
ABresult
0X0
100
111
6217 if defined $named && $count_col;
6218  
6219 34 ▶ my $fn = defined $named ? $named : 'sum';
Branch: 1 branch
BranchTrueFalseTotal
defined $named ? : 20 14 34
6220 return {
6221 34 ▶ fn => $fn,
Branch: 1 branch
BranchTrueFalseTotal
$fn eq 'sum' ? : 17 17 34
6222 val => $val_col,
6223 label => ($fn eq 'sum' ? $val_col : "$fn($val_col)"),
6224 };
6225 }
6226  
6227 # _render_visual_html($self, \%vis, $records, \@columns, $src, $vis_id, $spec_key,
6228 # $prefiltered, $cross_row)
6229 # -> { html => Str, title => Str, head => Str } | { error => Str }
6230 #
6231 # Renders one canvas visual spec to an HTML snippet without touching
6232 # $self->render(), with each of the card's inline scripts closed over a
6233 # function of its own (see _scope_card_scripts). The scoping is done here
6234 # rather than by the callers because every card passes through here and every
6235 # caller wants the same thing: a card's script and the page's are separate
6236 # programs, whether the card is drawn on a canvas of its own kind or returned
6237 # to a page that is about to insert it.
6238 37 ▶sub _render_visual_html :Protected ($self, $vis, $records, $columns, $src, $vis_id, $spec_key, $prefiltered, $cross_row = undef, $late_cf = undef) {
Subroutine _render_visual_html: called CC=4 SCAR=13.9
6239 37 my $rendered = $self->_render_visual_markup(
6240 $vis, $records, $columns, $src, $vis_id, $spec_key, $prefiltered, $cross_row, $late_cf,
6241 );
6242 37 ▶ return $rendered unless defined $rendered->{html};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rendered->{"html"} 19 18 37
6243  
6244 18 $rendered->{html} = _scope_card_scripts($rendered->{html});
6245 18 return $rendered;
6246 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
6247  
6248 # _render_visual_markup($self, \%vis, $records, \@columns, $src, $vis_id,
6249 # $spec_key, $prefiltered, $cross_row)
6250 # -> { html => Str, title => Str, head => Str } | { error => Str }
6251 #
6252 # The card itself, without the scope _render_visual_html puts around its
6253 # scripts. Applies any per-visual filters, then dispatches by
6254 # kind to the appropriate renderer. $vis_id gives a map's Leaflet element
6255 # ids a page-unique suffix; $spec_key is the dataset whose older per-dataset
6256 # geocode entry may still be read while the shared cache is migrated.
6257 # $cross_row is the canvas's validated cs= plan, run here so that each card's
6258 # window is over that card's own rows: see the note at the pass below. A card
6259 # that saved a plan of its own uses that one instead. $late_cf holds the
6260 # cross-filter specs on columns a plan writes (_canvas_cross_filter); a card
6261 # whose plan does not write the column has nothing to filter by, and ignores it.
6262 37 ▶sub _render_visual_markup :Protected ($self, $vis, $records, $columns, $src, $vis_id, $spec_key, $prefiltered, $cross_row = undef, $late_cf = undef) {
Subroutine _render_visual_markup: called CC=290 SCAR=105.7
6263 37 ▶ my $kind = $vis->{kind} // '';
Condition100%2/2
$vis->{"kind"} // ""
l!l
316
MC/DC100%1/1
$vis->{"kind"} // ""
$vis->{"kind"}
Truth table100%2/2
A
A= $vis->{"kind"}
Aresult
11
00
6264 37 ▶ return { html => _text_card_markup($vis), title => _text_card_title($vis) }
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "text" 2 35 37
6265 if $kind eq 'text';
6266  
6267 # A stored card is not checked part by part (PUT and import keep a visual
6268 # as sent), so a part of the wrong shape is read as absent here rather than
6269 # dereferenced.
6270 35 ▶ my $encode = ref($vis->{encode}) eq 'HASH' ? $vis->{encode} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $vis->{'encode'} eq 'HASH' ? : 21 14 35
6271 35 ▶ my $options = ref($vis->{options}) eq 'HASH' ? $vis->{options} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $vis->{'options'} eq 'HASH' ? : 7 28 35
6272 0 ▶ my $filters = ref($vis->{filters}) eq 'ARRAY' ? [ grep { defined && !ref } @{ $vis->{filters} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $vis->{'filters'} eq 'ARRAY' ? : 0 35 35
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
6273 35 ▶ my $title = $vis->{title} // '';
Condition100%2/2
$vis->{"title"} // ""
l!l
431
MC/DC100%1/1
$vis->{"title"} // ""
$vis->{"title"}
Truth table100%2/2
A
A= $vis->{"title"}
Aresult
11
00
6274  
6275 # Every chart on a canvas needs an element id of its own: HTML::D3 names the
6276 # SVG after the chart kind by default, so two bar cards both write #bar_chart
6277 # and the second card's script draws into the first. The id carries the
6278 # visual's index, which is stable across renders, so a saved canvas and a
6279 # card appended in place agree; the chart pages render one chart and keep
6280 # HTML::D3's default.
6281 35 ▶ my $svg_id = 'vis-' . (defined($vis_id) ? $vis_id : 'chart');
Branch: 1 branch
BranchTrueFalseTotal
defined $vis_id ? : 35 0 35
6282  
6283 # A card that carries a cross-row plan of its own (visual.cross_row) runs that
6284 # plan instead of the page's: putting a running total on one card is not a
6285 # request to put one on every card, and a card whose plan is empty has none
6286 # at all rather than the page's. Its plan is validated here rather than
6287 # when it was saved, because the columns a saved canvas has are the columns
6288 # its data file has today. A card's own plan is its own fault, so a bad one
6289 # is that card's error while the page's bad plan is the page's 400.
6290 35 my $plan = $cross_row;
6291 35 ▶ if (ref $vis->{cross_row} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vis->{"cross_row"} eq "HASH") 0 35 35
6292 0 $plan = $self->_cross_row_specs_from($columns, $vis->{cross_row});
6293 return { error => $plan->{error} }
6294 0 ▶ if $plan && $plan->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $plan and $plan->{"error"} 0 0 0
Condition0%0/3
$plan and $plan->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
$plan and $plan->{"error"}
$plan $plan->{"error"}
Truth table0%0/3
A and B
A= $planB= $plan->{"error"}
ABresult
0X0
100
111
6295 }
6296  
6297 # Apply per-visual filters on top of the base pipeline records. One on a column
6298 # this card's plan writes waits for the pass, as it does on a table page.
6299 my %written = $plan
6300 0 ? (map({ $_->{as} => 1 } @{ $plan->{specs} }),
6301 0 ▶ map({ $_->{name} => 1 } @{ $self->stash('_held_calc_fields') // [] }))
Branch: 1 branch
BranchTrueFalseTotal
$plan ? : 0 35 35
Condition0%0/2
$self->stash("_held_calc_fields") // []
l!l
00
MC/DC0%0/1
$self->stash("_held_calc_fields") // []
$self->stash("_held_calc_fields")
Truth table0%0/2
A
A= $self->stash("_held_calc_fields")
Aresult
11
00
6302 : ();
6303 35 my $vis_recs = $records;
6304 35 my @late_filters;
6305 35 for my $f (@$filters) {
6306 0 ▶ if ($written{ (split /:/, $f, 2)[0] // '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written{(split(/:/u, $f, 2))[0] // ""}) 0 0 0
Condition0%0/2
(split(/:/u, $f, 2))[0] // ""
l!l
00
MC/DC0%0/1
(split(/:/u, $f, 2))[0] // ""
(split(/:/u, $f, 2))[0]
Truth table0%0/2
A
A= (split(/:/u, $f, 2))[0]
Aresult
11
00
6307 0 push @late_filters, $f;
6308 0 next;
6309 }
6310 0 $vis_recs = _apply_filter_spec($vis_recs, $f);
6311 }
6312  
6313 # Stage 6, per card. A canvas names cs= once, but by the time the pass runs
6314 # the card has already been narrowed by its own filters and by the page's
6315 # cross-filter, so a running total is over the rows that card is drawn from:
6316 # two cards showing different regions show different running totals. The
6317 # pass therefore cannot be hoisted above the card loop -- one shared pass
6318 # would give every card the same numbers, and those are the numbers of
6319 # neither card. A fault is this card's error, not the page's: the plan was
6320 # validated once, and only the arithmetic can still fail here.
6321 35 ▶ if ($plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan) 0 35 35
6322 # The pass writes its column onto the rows it is handed, and a card is
6323 # handed the canvas's rows -- the same hashrefs every card draws from. The
6324 # numbers a pass produces are that card's, so it runs on copies: without
6325 # them one card's running total is written into its neighbour's rows, and a
6326 # card that asked for no plan at all reads a column it never asked for.
6327 0 ▶ my $own = [ map { ref $_ eq 'HASH' ? { %$_ } : $_ } @$vis_recs ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 0 0
6328 0 my ($crossed, $crossed_cols) = eval {
6329 0 my ($r, $c) = $self->_apply_cross_row($own, $columns, $plan);
6330 0 $self->_apply_held_calc_fields($r, $c, 1);
6331 };
6332 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
6333 0 $e =~ s/ at \S+ line \d+\.?\n?$//;
6334 0 return { error => $e };
6335 }
6336 0 ($vis_recs, $columns) = ($crossed, $crossed_cols);
6337 }
6338  
6339 35 my %col_set = map { $_ => 1 } @$columns;
6340  
6341 $vis_recs = _apply_filter_spec($vis_recs, $_)
6342 0 ▶ for @late_filters, grep { $col_set{ (split /:/, $_, 2)[0] // '' } } @{ $late_cf // [] };
Condition50%2/4
(split(/:/u, $_, 2))[0] // ""
l!l
00
$late_cf // []
l!l
2213
MC/DC0%0/1
(split(/:/u, $_, 2))[0] // ""
(split(/:/u, $_, 2))[0]
MC/DC100%1/1
$late_cf // []
$late_cf
Truth table0%0/2
A
A= (split(/:/u, $_, 2))[0]
Aresult
11
00
Truth table100%2/2
A
A= $late_cf
Aresult
11
00
6343  
6344 # The aggregate function this card picked for itself, if any (encode.agg).
6345 # It is read and checked here, before the dispatch, so a misspelt function
6346 # is this card's error whatever kind it is, rather than a chart quietly
6347 # drawn some other way; the kinds that aggregate then honour it below. A
6348 # percentile rides in the name (p90, p50), as it does in a pivot measure, so
6349 # there is no second key to keep in step.
6350 35 my $agg_fn = $encode->{agg};
6351 35 ▶ if (defined $agg_fn && !ref $agg_fn && length $agg_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $agg_fn and not ref $agg_fn and length $agg_fn) { } 0 35 35
Condition33%2/6
defined $agg_fn and not ref $agg_fn and length $agg_fn
!ll&&!rl&&r
3500
defined $agg_fn and not ref $agg_fn
!ll&&!rl&&r
3500
MC/DC0%0/3
defined $agg_fn and not ref $agg_fn and length $agg_fn
defined $agg_fn not ref $agg_fn length $agg_fn
Truth table25%1/4
A and not(B) and C
A= defined $agg_fnB= not ref $agg_fnC= length $agg_fn
ABCresult
0XX0
10X0
1100
1111
6352 0 ▶ return { error => "Unknown aggregate function '$agg_fn'; expected one of: "
Branch: 1 branch
BranchTrueFalseTotal
unless _measure_fn_ok($agg_fn) 0 0 0
6353 . join(' ', @MEASURE_FNS) . ', or p1 to p100 for a percentile' }
6354 unless _measure_fn_ok($agg_fn);
6355 } else {
6356 35 $agg_fn = undef;
6357 }
6358  
6359 # A chart aggregates the rows it is given. The SQL push path in
6360 # _grouped_totals re-reads the source, which has none of the filters the
6361 # caller already applied (a cross-filter, or this visual's own filters), so
6362 # those rows have to be folded in Perl. Passing no source is how the
6363 # pipeline says so.
6364 35 ▶ my $agg_src = (@$filters || $prefiltered) ? undef : $src;
Branch: 1 branch
BranchTrueFalseTotal
@$filters || $prefiltered ? : 1 34 35
Condition66%2/3
@$filters or $prefiltered
l!l&&r!l&&!r
0134
MC/DC50%1/2
@$filters or $prefiltered
@$filters $prefiltered
Truth table66%2/3
A or B
A= @$filtersB= $prefiltered
ABresult
1X1
011
000
6365  
6366 35 require HTML::D3;
6367  
6368 # ------------------------------------------------------------------ bar --
6369 35 ▶ if ($kind eq 'bar') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "bar") 22 13 35
6370 22 ▶ my $cat_col = $encode->{cat} // '';
Condition100%2/2
$encode->{"cat"} // ""
l!l
184
MC/DC100%1/1
$encode->{"cat"} // ""
$encode->{"cat"}
Truth table100%2/2
A
A= $encode->{"cat"}
Aresult
11
00
6371 22 ▶ my $val_col = $encode->{value} // '';
Condition100%2/2
$encode->{"value"} // ""
l!l
1210
MC/DC100%1/1
$encode->{"value"} // ""
$encode->{"value"}
Truth table100%2/2
A
A= $encode->{"value"}
Aresult
11
00
6372 22 ▶ return { error => 'Missing cat column for bar visual' } unless length $cat_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col 4 18 22
6373 18 ▶ return { error => "Column not found: $cat_col" } unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 5 13 18
6374 13 ▶ my $count_mode = ($val_col eq '__count__' || !length $val_col);
Condition66%2/3
$val_col eq '__count__' || !length($val_col)
l!l&&r!l&&!r
0310
MC/DC50%1/2
$val_col eq '__count__' || !length($val_col)
$val_col eq '__count__' !length($val_col)
Truth table66%2/3
A || not(B)
A= $val_col eq '__count__'B= !length($val_col)
ABresult
1X1
011
000
6375 return { error => "Column not found: $val_col" }
6376 13 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 0 13 13
Condition66%2/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
3100
MC/DC0%0/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table66%2/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
6377  
6378 13 my $m = _visual_measure($agg_fn, $val_col);
6379 13 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 13 13
6380  
6381 my $agg = $self->_grouped_totals(
6382 { source => $agg_src, records => $vis_recs },
6383 { by => [$cat_col], fn => $m->{fn},
6384 13 val => $m->{val}, as => 'value' },
6385 );
6386 13 ▶ return { error => 'No plottable data' } unless @$agg;
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg 0 13 13
6387  
6388 13 ▶ $title ||= "$m->{label} by $cat_col";
Condition100%2/2
$title ||= qq[$m->{"label"} by $cat_col]
l!l
49
MC/DC100%1/1
$title ||= qq[$m->{"label"} by $cat_col]
$title
Truth table100%2/2
A
A= $title
Aresult
11
00
6389 13 my @bars = map { [_decode_cell($_->{$cat_col}), $_->{value}] } @$agg;
6390 13 my $orient = _chart_orientation($options);
6391 # The bar page caps its bars as max_bars and stores the cap as
6392 # max_items; the seed has to carry it or a seeded canvas draws every
6393 # category where the page drew the top few and Other.
6394 13 ▶ my $max_bars = int($options->{max_items} // $options->{max_bars} // 0);
Condition40%2/5
($options->{"max_items"} // $options->{"max_bars"}) // 0
l!l
013
$options->{"max_items"} // $options->{"max_bars"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
($options->{"max_items"} // $options->{"max_bars"}) // 0
$options->{"max_items"} $options->{"max_bars"}
Truth table33%1/3
A // B
A= $options->{"max_items"}B= $options->{"max_bars"}
ABresult
1X1
011
000
6395 13 ▶ my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
Branch: 1 branch
BranchTrueFalseTotal
$max_bars > 0 ? : 0 13 13
6396 ->render_bar_chart_snippet(\@bars, {
6397 orientation => $orient,
6398 x_label => $cat_col,
6399 id => $svg_id,
6400 ($max_bars > 0 ? (max_bars => $max_bars) : ()),
6401 });
6402 13 my $html = $sn->{html};
6403 13 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 13 13
6404 13 return { html => $html, title => $title };
6405 }
6406  
6407 # ------------------------------------------------------------------ pie --
6408 13 ▶ if ($kind eq 'pie') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "pie") 1 12 13
6409 1 ▶ my $cat_col = $encode->{cat} // '';
Condition50%1/2
$encode->{"cat"} // ""
l!l
10
MC/DC0%0/1
$encode->{"cat"} // ""
$encode->{"cat"}
Truth table50%1/2
A
A= $encode->{"cat"}
Aresult
11
00
6410 1 ▶ my $val_col = $encode->{value} // '';
Condition50%1/2
$encode->{"value"} // ""
l!l
10
MC/DC0%0/1
$encode->{"value"} // ""
$encode->{"value"}
Truth table50%1/2
A
A= $encode->{"value"}
Aresult
11
00
6411 1 ▶ return { error => 'Missing cat or val column for pie visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col and length $val_col 0 1 1
Condition33%1/3
length $cat_col and length $val_col
!ll&&!rl&&r
001
MC/DC0%0/2
length $cat_col and length $val_col
length $cat_col length $val_col
Truth table33%1/3
A and B
A= length $cat_colB= length $val_col
ABresult
0X0
100
111
6412 unless length($cat_col) && length($val_col);
6413 1 ▶ return { error => "Column not found: $cat_col" } unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 0 1 1
6414 1 my $count_mode = ($val_col eq '__count__');
6415 return { error => "Column not found: $val_col" }
6416 1 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 0 1 1
Condition33%1/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
010
MC/DC0%0/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table33%1/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
6417  
6418 1 my $m = _visual_measure($agg_fn, $val_col);
6419 1 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 1 1
6420  
6421 my $agg = $self->_grouped_totals(
6422 { source => $agg_src, records => $vis_recs },
6423 { by => [$cat_col], fn => $m->{fn},
6424 1 val => $m->{val}, as => 'value' },
6425 );
6426 1 ▶ return { error => 'No plottable data' } unless @$agg;
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg 0 1 1
6427  
6428 1 ▶ $title ||= "$m->{label} by $cat_col";
Condition50%1/2
$title ||= qq[$m->{"label"} by $cat_col]
l!l
01
MC/DC0%0/1
$title ||= qq[$m->{"label"} by $cat_col]
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
6429 1 my @slices = map { [_decode_cell($_->{$cat_col}), $_->{value}] } @$agg;
6430 1 my $scheme = _categorical_scheme($options->{scheme});
6431 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6432 ->render_pie_chart_snippet(\@slices, {
6433 1 ▶ donut => $options->{donut} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$options->{'donut'} ? : 0 1 1
6434 legend => 1,
6435 color_scheme => $scheme,
6436 id => $svg_id,
6437 });
6438 1 my $html = $sn->{html};
6439 1 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 1 1
6440 1 return { html => $html, title => $title };
6441 }
6442  
6443 # -------------------------------------------------------------- line/graph --
6444 12 ▶ if ($kind eq 'line' || $kind eq 'graph') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "line" or $kind eq "graph") 0 12 12
Condition33%1/3
$kind eq "line" or $kind eq "graph"
l!l&&r!l&&!r
0012
MC/DC0%0/2
$kind eq "line" or $kind eq "graph"
$kind eq "line" $kind eq "graph"
Truth table33%1/3
A or B
A= $kind eq "line"B= $kind eq "graph"
ABresult
1X1
011
000
6445 0 ▶ my $x_col = $encode->{x} // '';
Condition0%0/2
$encode->{"x"} // ""
l!l
00
MC/DC0%0/1
$encode->{"x"} // ""
$encode->{"x"}
Truth table0%0/2
A
A= $encode->{"x"}
Aresult
11
00
6446 0 ▶ my $y_col = $encode->{y} // '';
Condition0%0/2
$encode->{"y"} // ""
l!l
00
MC/DC0%0/1
$encode->{"y"} // ""
$encode->{"y"}
Truth table0%0/2
A
A= $encode->{"y"}
Aresult
11
00
6447 0 ▶ return { error => 'Missing x or y column for line visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $y_col 0 0 0
Condition0%0/3
length $x_col and length $y_col
!ll&&!rl&&r
000
MC/DC0%0/2
length $x_col and length $y_col
length $x_col length $y_col
Truth table0%0/3
A and B
A= length $x_colB= length $y_col
ABresult
0X0
100
111
6448 unless length($x_col) && length($y_col);
6449 0 ▶ return { error => "Column not found: $x_col" } unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 0 0 0
6450 0 ▶ return { error => "Column not found: $y_col" } unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 0 0 0
6451  
6452 # encode.series is what /graph's "Add to canvas" saves from series=: one
6453 # line per value of that column, as on the page. Without it every row
6454 # is one line, which runs through all the groups' points in turn.
6455 0 ▶ my $series_col = $encode->{series} // '';
Condition0%0/2
$encode->{"series"} // ""
l!l
00
MC/DC0%0/1
$encode->{"series"} // ""
$encode->{"series"}
Truth table0%0/2
A
A= $encode->{"series"}
Aresult
11
00
6456 0 ▶ $series_col = '' if ref $series_col;
Branch: 1 branch
BranchTrueFalseTotal
if ref $series_col 0 0 0
6457 return { error => "Column not found: $series_col" }
6458 0 ▶ if length $series_col && !$col_set{$series_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col and not $col_set{$series_col} 0 0 0
Condition0%0/3
length $series_col and not $col_set{$series_col}
!ll&&!rl&&r
000
MC/DC0%0/2
length $series_col and not $col_set{$series_col}
length $series_col not $col_set{$series_col}
Truth table0%0/3
A and not(B)
A= length $series_colB= not $col_set{$series_col}
ABresult
0X0
100
111
6459  
6460 # A card whose spec names a measure plots one point per x value rather
6461 # than one per row: the shape /graph?agg= draws, and the only shape that
6462 # makes sense when the x axis is a bucket. The fold writes its result
6463 # back under the y column's own name, so the loop below is the same one
6464 # an unaggregated card runs, and the groups arrive in label order, which
6465 # for an ISO bucket is chronological order. With a series the fold is
6466 # per x value and series value, so each line has its own totals.
6467 0 my $rows = $vis_recs;
6468 0 my $m;
6469 0 ▶ if (defined $agg_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $agg_fn) 0 0 0
6470 0 $m = _visual_measure($agg_fn, $y_col);
6471 0 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 0 0
6472 my $agg = $self->_grouped_totals(
6473 { source => undef, records => $vis_recs },
6474 { by => [ $x_col, (length $series_col ? $series_col : ()) ],
6475 0 ▶ fn => $m->{fn}, val => $m->{val}, as => 'value' },
Branch: 1 branch
BranchTrueFalseTotal
length $series_col ? : 0 0 0
6476 );
6477 0 $rows = [ map { { %$_, $y_col => $_->{value} } } @$agg ];
6478 }
6479  
6480 0 my $sn;
6481 0 ▶ if (length $series_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col) { } 0 0 0
6482 # The order /graph draws, so each line has the page's colour:
6483 # aggregated, lines by name and points by label; unaggregated, lines
6484 # as first met and each keeping its rows' order.
6485 0 my (%points_of, @names);
6486 0 for my $row (@$rows) {
6487 0 ▶ my $xv = $row->{$x_col} // ''; next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 0 0
Condition0%0/2
$row->{$x_col} // ""
l!l
00
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table0%0/2
A
A= $row->{$x_col}
Aresult
11
00
6488 0 ▶ my $name = $row->{$series_col} // ''; next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
Condition0%0/2
$row->{$series_col} // ""
l!l
00
MC/DC0%0/1
$row->{$series_col} // ""
$row->{$series_col}
Truth table0%0/2
A
A= $row->{$series_col}
Aresult
11
00
6489 0 ▶ my $yn = _numeric_cell($row->{$y_col} // ''); next unless defined $yn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yn 0 0 0
Condition0%0/2
$row->{$y_col} // ""
l!l
00
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table0%0/2
A
A= $row->{$y_col}
Aresult
11
00
6490 0 $name = _decode_cell($name);
6491 0 ▶ push @names, $name unless $points_of{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $points_of{$name} 0 0 0
6492 0 push @{ $points_of{$name} }, { label => _decode_cell($xv), value => $yn };
6493 }
6494 0 ▶ return { error => 'No plottable data' } unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
6495 my @series_data = map {
6496 0 my $points = $points_of{$_};
6497 0 ▶ $points = [ sort { $a->{label} cmp $b->{label} } @$points ] if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 0 0 0
6498 0 ▶ { name => $_, data => $points };
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
6499 } ($m ? sort @names : @names);
6500  
6501 0 ▶ $title ||= $m ? "$m->{label} by $x_col and $series_col" : "$y_col vs $x_col by $series_col";
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
Condition0%0/3
$title ||= $m ? qq[$m->{"label"} by $x_col and $series_col] : "$y_col vs $x_col by $series_col"
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= $m ? qq[$m->{"label"} by $x_col and $series_col] : "$y_col vs $x_col by $series_col"
$title $m ? qq[$m->{"label"} by $x_col and $series_col] : "$y_col vs $x_col by $series_col"
Truth table0%0/3
A ||= B
A= $titleB= $m ? qq[$m->{"label"} by $x_col and $series_col] : "$y_col vs $x_col by $series_col"
ABresult
1X1
011
000
6502 0 $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6503 ->render_multi_series_line_chart_snippet(\@series_data, { id => $svg_id });
6504 } else {
6505 0 my @pairs;
6506 0 for my $row (@$rows) {
6507 0 ▶ my $xv = $row->{$x_col} // ''; next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 0 0
Condition0%0/2
$row->{$x_col} // ""
l!l
00
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table0%0/2
A
A= $row->{$x_col}
Aresult
11
00
6508 0 ▶ my $yn = _numeric_cell($row->{$y_col} // ''); next unless defined $yn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yn 0 0 0
Condition0%0/2
$row->{$y_col} // ""
l!l
00
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table0%0/2
A
A= $row->{$y_col}
Aresult
11
00
6509 0 push @pairs, [_decode_cell($xv), $yn];
6510 }
6511 0 ▶ return { error => 'No plottable data' } unless @pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 0 0 0
6512  
6513 0 ▶ $title ||= $m ? "$m->{label} by $x_col" : "$y_col vs $x_col";
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
Condition0%0/3
$title ||= $m ? qq[$m->{"label"} by $x_col] : "$y_col vs $x_col"
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= $m ? qq[$m->{"label"} by $x_col] : "$y_col vs $x_col"
$title $m ? qq[$m->{"label"} by $x_col] : "$y_col vs $x_col"
Truth table0%0/3
A ||= B
A= $titleB= $m ? qq[$m->{"label"} by $x_col] : "$y_col vs $x_col"
ABresult
1X1
011
000
6514 0 $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6515 ->render_zoomable_line_chart_snippet(\@pairs, { id => $svg_id });
6516 }
6517 0 my $html = $sn->{html};
6518 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6519 0 return { html => $html, title => $title };
6520 }
6521  
6522 # --------------------------------------------------------------- scatter --
6523 12 ▶ if ($kind eq 'scatter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "scatter") 0 12 12
6524 0 ▶ my $x_col = $encode->{x} // '';
Condition0%0/2
$encode->{"x"} // ""
l!l
00
MC/DC0%0/1
$encode->{"x"} // ""
$encode->{"x"}
Truth table0%0/2
A
A= $encode->{"x"}
Aresult
11
00
6525 0 ▶ my $y_col = $encode->{y} // '';
Condition0%0/2
$encode->{"y"} // ""
l!l
00
MC/DC0%0/1
$encode->{"y"} // ""
$encode->{"y"}
Truth table0%0/2
A
A= $encode->{"y"}
Aresult
11
00
6526 0 ▶ return { error => 'Missing x or y column for scatter visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $y_col 0 0 0
Condition0%0/3
length $x_col and length $y_col
!ll&&!rl&&r
000
MC/DC0%0/2
length $x_col and length $y_col
length $x_col length $y_col
Truth table0%0/3
A and B
A= length $x_colB= length $y_col
ABresult
0X0
100
111
6527 unless length($x_col) && length($y_col);
6528 0 ▶ return { error => "Column not found: $x_col" } unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 0 0 0
6529 0 ▶ return { error => "Column not found: $y_col" } unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 0 0 0
6530  
6531 # series and size are what /scatter's "Add to canvas" saves from series=
6532 # and size=; scheme, min_r and max_r come with them in options. A
6533 # canvas stores a visual unchecked (PUT, PATCH, import), so the scheme
6534 # goes through _categorical_scheme -- HTML::D3 writes it into the script
6535 # -- and an unknown one draws the default colours, as on a pie card.
6536 0 ▶ my $series_col = $encode->{series} // '';
Condition0%0/2
$encode->{"series"} // ""
l!l
00
MC/DC0%0/1
$encode->{"series"} // ""
$encode->{"series"}
Truth table0%0/2
A
A= $encode->{"series"}
Aresult
11
00
6537 0 ▶ my $size_col = $encode->{size} // '';
Condition0%0/2
$encode->{"size"} // ""
l!l
00
MC/DC0%0/1
$encode->{"size"} // ""
$encode->{"size"}
Truth table0%0/2
A
A= $encode->{"size"}
Aresult
11
00
6538 0 ▶ $series_col = '' if ref $series_col;
Branch: 1 branch
BranchTrueFalseTotal
if ref $series_col 0 0 0
6539 0 ▶ $size_col = '' if ref $size_col;
Branch: 1 branch
BranchTrueFalseTotal
if ref $size_col 0 0 0
6540 return { error => "Column not found: $series_col" }
6541 0 ▶ if length $series_col && !$col_set{$series_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col and not $col_set{$series_col} 0 0 0
Condition0%0/3
length $series_col and not $col_set{$series_col}
!ll&&!rl&&r
000
MC/DC0%0/2
length $series_col and not $col_set{$series_col}
length $series_col not $col_set{$series_col}
Truth table0%0/3
A and not(B)
A= length $series_colB= not $col_set{$series_col}
ABresult
0X0
100
111
6542 return { error => "Column not found: $size_col" }
6543 0 ▶ if length $size_col && !$col_set{$size_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $size_col and not $col_set{$size_col} 0 0 0
Condition0%0/3
length $size_col and not $col_set{$size_col}
!ll&&!rl&&r
000
MC/DC0%0/2
length $size_col and not $col_set{$size_col}
length $size_col not $col_set{$size_col}
Truth table0%0/3
A and not(B)
A= length $size_colB= not $col_set{$size_col}
ABresult
0X0
100
111
6544  
6545 0 my @points;
6546 0 for my $row (@$vis_recs) {
6547 0 ▶ my $xv = _numeric_cell($row->{$x_col} // ''); next unless defined $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xv 0 0 0
Condition0%0/2
$row->{$x_col} // ""
l!l
00
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table0%0/2
A
A= $row->{$x_col}
Aresult
11
00
6548 0 ▶ my $yv = _numeric_cell($row->{$y_col} // ''); next unless defined $yv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yv 0 0 0
Condition0%0/2
$row->{$y_col} // ""
l!l
00
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table0%0/2
A
A= $row->{$y_col}
Aresult
11
00
6549 0 my %meta;
6550 0 ▶ $meta{series} = _decode_cell($row->{$series_col} // '') if length $series_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col 0 0 0
Condition0%0/2
$row->{$series_col} // ""
l!l
00
MC/DC0%0/1
$row->{$series_col} // ""
$row->{$series_col}
Truth table0%0/2
A
A= $row->{$series_col}
Aresult
11
00
6551 0 ▶ if (length $size_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $size_col) 0 0 0
6552 0 ▶ my $sv = _numeric_cell($row->{$size_col} // '');
Condition0%0/2
$row->{$size_col} // ""
l!l
00
MC/DC0%0/1
$row->{$size_col} // ""
$row->{$size_col}
Truth table0%0/2
A
A= $row->{$size_col}
Aresult
11
00
6553 0 ▶ $meta{size} = $sv if defined $sv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sv 0 0 0
6554 }
6555 0 ▶ push @points, [$xv, $yv, {}, (%meta ? \%meta : ())];
Branch: 1 branch
BranchTrueFalseTotal
%meta ? : 0 0 0
6556 }
6557 0 ▶ return { error => 'No plottable data' } unless @points;
Branch: 1 branch
BranchTrueFalseTotal
unless @points 0 0 0
6558  
6559 0 ▶ $title ||= "$y_col vs $x_col";
Condition0%0/2
$title ||= "$y_col vs $x_col"
l!l
00
MC/DC0%0/1
$title ||= "$y_col vs $x_col"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
6560 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6561 ->render_scatter_chart_snippet(\@points, {
6562 x_label => $x_col,
6563 y_label => $y_col,
6564 id => $svg_id,
6565 (length $series_col
6566 ? (colour_by_series => 1, series_col => $series_col,
6567 scheme => _categorical_scheme($options->{scheme}))
6568 : ()),
6569 (length $size_col
6570 ? (size_col => $size_col,
6571 0 ▶ radius_range => [ _scatter_radii($options->{min_r}, $options->{max_r}) ])
Branch: 2 branches
BranchTrueFalseTotal
length $series_col ? : 0 0 0
length $size_col ? : 0 0 0
6572 : ()),
6573 });
6574 0 my $html = $sn->{html};
6575 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6576 0 return { html => $html, title => $title };
6577 }
6578  
6579 # -------------------------------------------------------------- heatmap --
6580 12 ▶ if ($kind eq 'heatmap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "heatmap") 1 11 12
6581 1 ▶ my $x_col = $encode->{x} // '';
Condition50%1/2
$encode->{"x"} // ""
l!l
10
MC/DC0%0/1
$encode->{"x"} // ""
$encode->{"x"}
Truth table50%1/2
A
A= $encode->{"x"}
Aresult
11
00
6582 1 ▶ my $y_col = $encode->{y} // '';
Condition50%1/2
$encode->{"y"} // ""
l!l
10
MC/DC0%0/1
$encode->{"y"} // ""
$encode->{"y"}
Truth table50%1/2
A
A= $encode->{"y"}
Aresult
11
00
6583 1 ▶ my $val_col = $encode->{value} // '';
Condition50%1/2
$encode->{"value"} // ""
l!l
10
MC/DC0%0/1
$encode->{"value"} // ""
$encode->{"value"}
Truth table50%1/2
A
A= $encode->{"value"}
Aresult
11
00
6584 1 ▶ return { error => 'Missing x or y column for heatmap visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $y_col 0 1 1
Condition33%1/3
length $x_col and length $y_col
!ll&&!rl&&r
001
MC/DC0%0/2
length $x_col and length $y_col
length $x_col length $y_col
Truth table33%1/3
A and B
A= length $x_colB= length $y_col
ABresult
0X0
100
111
6585 unless length($x_col) && length($y_col);
6586 1 ▶ return { error => "Column not found: $x_col" } unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 0 1 1
6587 1 ▶ return { error => "Column not found: $y_col" } unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 0 1 1
6588 return { error => "Column not found: $val_col" }
6589 1 ▶ if length($val_col) && !$col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $val_col and not $col_set{$val_col} 0 1 1
Condition33%1/3
length $val_col and not $col_set{$val_col}
!ll&&!rl&&r
010
MC/DC0%0/2
length $val_col and not $col_set{$val_col}
length $val_col not $col_set{$val_col}
Truth table33%1/3
A and not(B)
A= length $val_colB= not $col_set{$val_col}
ABresult
0X0
100
111
6590  
6591 1 my (@x_order, @y_order, %seen_x, %seen_y);
6592 1 for my $row (@$vis_recs) {
6593 6 ▶ my $xv = $row->{$x_col} // ''; next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 6 6
Condition50%1/2
$row->{$x_col} // ""
l!l
60
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table50%1/2
A
A= $row->{$x_col}
Aresult
11
00
6594 6 ▶ my $yv = $row->{$y_col} // ''; next unless length $yv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $yv 0 6 6
Condition50%1/2
$row->{$y_col} // ""
l!l
60
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table50%1/2
A
A= $row->{$y_col}
Aresult
11
00
6595 6 ▶ push @x_order, $xv unless $seen_x{$xv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_x{$xv}++ 4 2 6
6596 6 ▶ push @y_order, $yv unless $seen_y{$yv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_y{$yv}++ 3 3 6
6597 }
6598 1 my $m = _visual_measure($agg_fn, $val_col);
6599 1 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 1 1
6600  
6601 my $agg = $self->_grouped_totals(
6602 { source => $agg_src, records => $vis_recs },
6603 { by => [$x_col, $y_col],
6604 fn => $m->{fn},
6605 val => $m->{val},
6606 1 as => 'value' },
6607 );
6608 1 my %grid;
6609 1 for my $r (@$agg) {
6610 6 $grid{$r->{$x_col}}{$r->{$y_col}} = $r->{value};
6611 }
6612 my @triples = map {
6613 1 my ($raw_x, $xv) = ($_, _decode_cell($_));
6614 4 ▶ map { [$xv, _decode_cell($_), $grid{$raw_x}{$_} // 0] } @y_order
Condition100%2/2
$grid{$raw_x}{$_} // 0
l!l
66
MC/DC100%1/1
$grid{$raw_x}{$_} // 0
$grid{$raw_x}{$_}
Truth table100%2/2
A
A= $grid{$raw_x}{$_}
Aresult
11
00
6615 } @x_order;
6616 1 ▶ return { error => 'No plottable data' } unless @triples;
Branch: 1 branch
BranchTrueFalseTotal
unless @triples 0 1 1
6617  
6618 1 ▶ $title ||= "$m->{label} by $x_col and $y_col";
Condition50%1/2
$title ||= qq[$m->{"label"} by $x_col and $y_col]
l!l
01
MC/DC0%0/1
$title ||= qq[$m->{"label"} by $x_col and $y_col]
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
6619 1 my $scheme = _sequential_scheme($options->{scheme});
6620 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6621 ->render_heatmap_snippet(\@triples, {
6622 x_label => $x_col,
6623 y_label => $y_col,
6624 val_label => $m->{label},
6625 color_scheme => $scheme,
6626 1 ▶ show_values => ($options->{show_values} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$options->{'show_values'} ? : 0 1 1
6627 id => $svg_id,
6628 });
6629 1 my $html = $sn->{html};
6630 1 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 1 1
6631 1 return { html => $html, title => $title };
6632 }
6633  
6634 # --------------------------------------------------------------- stacked --
6635 11 ▶ if ($kind eq 'stacked') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "stacked") 0 11 11
6636 0 ▶ my $cat_col = $encode->{cat} // '';
Condition0%0/2
$encode->{"cat"} // ""
l!l
00
MC/DC0%0/1
$encode->{"cat"} // ""
$encode->{"cat"}
Truth table0%0/2
A
A= $encode->{"cat"}
Aresult
11
00
6637 0 ▶ my $seg_col = $encode->{series} // '';
Condition0%0/2
$encode->{"series"} // ""
l!l
00
MC/DC0%0/1
$encode->{"series"} // ""
$encode->{"series"}
Truth table0%0/2
A
A= $encode->{"series"}
Aresult
11
00
6638 0 ▶ my $val_col = $encode->{value} // '';
Condition0%0/2
$encode->{"value"} // ""
l!l
00
MC/DC0%0/1
$encode->{"value"} // ""
$encode->{"value"}
Truth table0%0/2
A
A= $encode->{"value"}
Aresult
11
00
6639 0 ▶ return { error => 'Missing cat or series column for stacked visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col and length $seg_col 0 0 0
Condition0%0/3
length $cat_col and length $seg_col
!ll&&!rl&&r
000
MC/DC0%0/2
length $cat_col and length $seg_col
length $cat_col length $seg_col
Truth table0%0/3
A and B
A= length $cat_colB= length $seg_col
ABresult
0X0
100
111
6640 unless length($cat_col) && length($seg_col);
6641 0 ▶ return { error => "Column not found: $cat_col" } unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 0 0 0
6642 0 ▶ return { error => "Column not found: $seg_col" } unless $col_set{$seg_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$seg_col} 0 0 0
6643 0 ▶ my $count_mode = (!length $val_col || $val_col eq '__count__');
Condition0%0/3
!length($val_col) || $val_col eq '__count__'
l!l&&r!l&&!r
000
MC/DC0%0/2
!length($val_col) || $val_col eq '__count__'
!length($val_col) $val_col eq '__count__'
Truth table0%0/3
not(A) || B
A= !length($val_col)B= $val_col eq '__count__'
ABresult
1X1
011
000
6644 return { error => "Column not found: $val_col" }
6645 0 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 0 0 0
Condition0%0/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
000
MC/DC0%0/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table0%0/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
6646  
6647 0 my $m = _visual_measure($agg_fn, $val_col);
6648 0 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 0 0
6649  
6650 my $agg = $self->_grouped_totals(
6651 { source => $agg_src, records => $vis_recs },
6652 { by => [$cat_col, $seg_col],
6653 fn => $m->{fn},
6654 val => $m->{val},
6655 0 as => 'value' },
6656 );
6657 my @data = map {
6658 0 ▶ my ($c, $s) = ($_->{$cat_col}//'', $_->{$seg_col}//'');
Condition0%0/4
$_->{$cat_col} // ""
l!l
00
$_->{$seg_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
MC/DC0%0/1
$_->{$seg_col} // ""
$_->{$seg_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
Truth table0%0/2
A
A= $_->{$seg_col}
Aresult
11
00
6659 (length $c && length $s)
6660 ? { cat => $c, seg => $s, val => $_->{value} }
6661 : ()
6662 0 ▶ } @$agg;
Branch: 1 branch
BranchTrueFalseTotal
length $c && length $s ? : 0 0 0
Condition0%0/3
length $c and length $s
!ll&&!rl&&r
000
MC/DC0%0/2
length $c and length $s
length $c length $s
Truth table0%0/3
A and B
A= length $cB= length $s
ABresult
0X0
100
111
6663 0 ▶ return { error => 'No plottable data' } unless @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 0 0 0
6664  
6665 0 ▶ $title ||= "$m->{label} by $cat_col and $seg_col";
Condition0%0/2
$title ||= qq[$m->{"label"} by $cat_col and $seg_col]
l!l
00
MC/DC0%0/1
$title ||= qq[$m->{"label"} by $cat_col and $seg_col]
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
6666 0 my $scheme = _categorical_scheme($options->{scheme});
6667 0 ▶ my $orient = (_chart_orientation($options) eq 'horizontal') ? 'h' : 'v';
Branch: 1 branch
BranchTrueFalseTotal
_chart_orientation($options) eq 'horizontal' ? : 0 0 0
6668 0 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6669 ->render_stacked_bar_snippet(\@data, {
6670 scheme => $scheme,
6671 x_label => $cat_col,
6672 orient => $orient,
6673 id => $svg_id,
6674 });
6675 0 my $html = $sn->{html};
6676 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6677 0 return { html => $html, title => $title };
6678 }
6679  
6680 # ---------------------------------------------------------------- combo --
6681 11 ▶ if ($kind eq 'combo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "combo") 0 11 11
6682 0 ▶ my $x_col = $encode->{cat} // $encode->{x} // '';
Condition0%0/5
($encode->{"cat"} // $encode->{"x"}) // ""
l!l
00
$encode->{"cat"} // $encode->{"x"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"cat"} // $encode->{"x"}) // ""
$encode->{"cat"} $encode->{"x"}
Truth table0%0/3
A // B
A= $encode->{"cat"}B= $encode->{"x"}
ABresult
1X1
011
000
6683 0 ▶ my $bar_col = $encode->{bar_val} // $encode->{bar} // '';
Condition0%0/5
($encode->{"bar_val"} // $encode->{"bar"}) // ""
l!l
00
$encode->{"bar_val"} // $encode->{"bar"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"bar_val"} // $encode->{"bar"}) // ""
$encode->{"bar_val"} $encode->{"bar"}
Truth table0%0/3
A // B
A= $encode->{"bar_val"}B= $encode->{"bar"}
ABresult
1X1
011
000
6684 0 ▶ my $line_col = $encode->{line_val} // $encode->{line} // '';
Condition0%0/5
($encode->{"line_val"} // $encode->{"line"}) // ""
l!l
00
$encode->{"line_val"} // $encode->{"line"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"line_val"} // $encode->{"line"}) // ""
$encode->{"line_val"} $encode->{"line"}
Truth table0%0/3
A // B
A= $encode->{"line_val"}B= $encode->{"line"}
ABresult
1X1
011
000
6685 0 ▶ return { error => 'Missing cat, bar_val, or line_val for combo visual' }
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $bar_col and length $line_col 0 0 0
Condition0%0/6
length $x_col and length $bar_col and length $line_col
!ll&&!rl&&r
000
length $x_col and length $bar_col
!ll&&!rl&&r
000
MC/DC0%0/3
length $x_col and length $bar_col and length $line_col
length $x_col length $bar_col length $line_col
Truth table0%0/4
A and B and C
A= length $x_colB= length $bar_colC= length $line_col
ABCresult
0XX0
10X0
1100
1111
6686 unless length($x_col) && length($bar_col) && length($line_col);
6687 0 ▶ return { error => "Column not found: $x_col" } unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 0 0 0
6688 0 ▶ return { error => "Column not found: $bar_col" } unless $col_set{$bar_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$bar_col} 0 0 0
6689 0 ▶ return { error => "Column not found: $line_col" } unless $col_set{$line_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$line_col} 0 0 0
6690  
6691 # One card, one function: a combo aggregates both its series the same
6692 # way, so the two cannot be read against different arithmetic by
6693 # accident.
6694 0 my $bar_m = _visual_measure($agg_fn, $bar_col);
6695 0 my $line_m = _visual_measure($agg_fn, $line_col);
6696 0 ▶ return { error => $bar_m->{error} } if $bar_m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $bar_m->{"error"} 0 0 0
6697 0 ▶ return { error => $line_m->{error} } if $line_m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $line_m->{"error"} 0 0 0
6698  
6699 my $bar_rows = $self->_grouped_totals(
6700 { source => $agg_src, records => $vis_recs },
6701 0 { by => [$x_col], fn => $bar_m->{fn}, val => $bar_m->{val}, as => 'bar_val' },
6702 );
6703 my $line_rows = $self->_grouped_totals(
6704 { source => $agg_src, records => $vis_recs },
6705 0 { by => [$x_col], fn => $line_m->{fn}, val => $line_m->{val}, as => 'line_val' },
6706 );
6707 0 my (%bar_map, %line_map, @x_order, %x_seen);
6708 0 for my $r (@$bar_rows) {
6709 0 ▶ my $xv = _decode_cell($r->{$x_col} // ''); next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 0 0
Condition0%0/2
$r->{$x_col} // ""
l!l
00
MC/DC0%0/1
$r->{$x_col} // ""
$r->{$x_col}
Truth table0%0/2
A
A= $r->{$x_col}
Aresult
11
00
6710 0 ▶ push @x_order, $xv unless $x_seen{$xv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $x_seen{$xv}++ 0 0 0
6711 0 $bar_map{$xv} = $r->{bar_val};
6712 }
6713 0 for my $r (@$line_rows) {
6714 0 ▶ my $xv = _decode_cell($r->{$x_col} // ''); next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 0 0
Condition0%0/2
$r->{$x_col} // ""
l!l
00
MC/DC0%0/1
$r->{$x_col} // ""
$r->{$x_col}
Truth table0%0/2
A
A= $r->{$x_col}
Aresult
11
00
6715 0 ▶ push @x_order, $xv unless $x_seen{$xv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $x_seen{$xv}++ 0 0 0
6716 0 $line_map{$xv} = $r->{line_val};
6717 }
6718 0 ▶ return { error => 'No plottable data' } unless @x_order;
Branch: 1 branch
BranchTrueFalseTotal
unless @x_order 0 0 0
6719  
6720 0 my @data = map { { x => $_, bar_val => $bar_map{$_}, line_val => $line_map{$_} } }
6721 @x_order;
6722 # Sum keeps the columns in the title because that is what the card has
6723 # always said; count needs no column, so it reads as the tally it is.
6724 $title ||= ($bar_m->{fn} eq 'sum')
6725 ? "$bar_col and $line_col by $x_col"
6726 0 ▶ : ($bar_m->{fn} eq 'count')
Branch: 2 branches
BranchTrueFalseTotal
$bar_m->{'fn'} eq 'sum' ? : 0 0 0
$bar_m->{'fn'} eq 'count' ? : 0 0 0
Condition0%0/3
$title ||= $bar_m->{"fn"} eq "sum" ? "$bar_col and $line_col by $x_col" : ($bar_m->{"fn"} eq "count" ? "Count by $x_col" : qq[$bar_m->{"fn"} of $bar_col and $line_col by $x_col])
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= $bar_m->{"fn"} eq "sum" ? "$bar_col and $line_col by $x_col" : ($bar_m->{"fn"} eq "count" ? "Count by $x_col" : qq[$bar_m->{"fn"} of $bar_col and $line_col by $x_col])
$title $bar_m->{"fn"} eq "sum" ? "$bar_col and $line_col by $x_col" : ($bar_m->{"fn"} eq "count" ? "Count by $x_col" : qq[$bar_m->{"fn"} of $bar_col and $line_col by $x_col])
Truth table0%0/3
A ||= B
A= $titleB= $bar_m->{"fn"} eq "sum" ? "$bar_col and $line_col by $x_col" : ($bar_m->{"fn"} eq "count" ? "Count by $x_col" : qq[$bar_m->{"fn"} of $bar_col and $line_col by $x_col])
ABresult
1X1
011
000
6727 ? "Count by $x_col"
6728 : "$bar_m->{fn} of $bar_col and $line_col by $x_col";
6729 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6730 ->render_combo_snippet(\@data, {
6731 bar_label => $bar_col,
6732 line_label => $line_col,
6733 id => $svg_id,
6734 # What /combo saves from scheme=, checked again: a canvas stores it
6735 # unchecked.
6736 0 ▶ (_optional_scheme($options->{scheme}) ? (scheme => $options->{scheme}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
_optional_scheme($options->{'scheme'}) ? : 0 0 0
6737 });
6738 0 my $html = $sn->{html};
6739 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6740 0 return { html => $html, title => $title };
6741 }
6742  
6743 # --------------------------------------------------------------- treemap --
6744 11 ▶ if ($kind eq 'treemap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "treemap") 0 11 11
6745 0 ▶ my $cat_col = $encode->{group} // $encode->{cat} // '';
Condition0%0/5
($encode->{"group"} // $encode->{"cat"}) // ""
l!l
00
$encode->{"group"} // $encode->{"cat"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"group"} // $encode->{"cat"}) // ""
$encode->{"group"} $encode->{"cat"}
Truth table0%0/3
A // B
A= $encode->{"group"}B= $encode->{"cat"}
ABresult
1X1
011
000
6746 0 ▶ my $val_col = $encode->{size} // $encode->{value} // '';
Condition0%0/5
($encode->{"size"} // $encode->{"value"}) // ""
l!l
00
$encode->{"size"} // $encode->{"value"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"size"} // $encode->{"value"}) // ""
$encode->{"size"} $encode->{"value"}
Truth table0%0/3
A // B
A= $encode->{"size"}B= $encode->{"value"}
ABresult
1X1
011
000
6747 0 ▶ return { error => 'Missing group column for treemap visual' } unless length $cat_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col 0 0 0
6748 0 ▶ return { error => "Column not found: $cat_col" } unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 0 0 0
6749 0 ▶ my $count_mode = (!length $val_col || $val_col eq '__count__');
Condition0%0/3
!length($val_col) || $val_col eq '__count__'
l!l&&r!l&&!r
000
MC/DC0%0/2
!length($val_col) || $val_col eq '__count__'
!length($val_col) $val_col eq '__count__'
Truth table0%0/3
not(A) || B
A= !length($val_col)B= $val_col eq '__count__'
ABresult
1X1
011
000
6750 return { error => "Column not found: $val_col" }
6751 0 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 0 0 0
Condition0%0/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
000
MC/DC0%0/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table0%0/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
6752  
6753 0 my $m = _visual_measure($agg_fn, $val_col);
6754 0 ▶ return { error => $m->{error} } if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 0 0
6755  
6756 # encode.color and options.color_agg / color_scheme are what /treemap
6757 # saves from color=, color_agg= and color_scheme=. The function is
6758 # checked by _tile_colour_measure, as the page's is; the scheme falls back as the
6759 # heatmap's does, since HTML::D3 refuses a name it does not know.
6760 0 ▶ my $color_col = $encode->{color} // '';
Condition0%0/2
$encode->{"color"} // ""
l!l
00
MC/DC0%0/1
$encode->{"color"} // ""
$encode->{"color"}
Truth table0%0/2
A
A= $encode->{"color"}
Aresult
11
00
6761 0 ▶ $color_col = '' if ref $color_col;
Branch: 1 branch
BranchTrueFalseTotal
if ref $color_col 0 0 0
6762 0 my $color_m;
6763 0 ▶ if (length $color_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $color_col) 0 0 0
6764 0 ▶ return { error => "Column not found: $color_col" } unless $col_set{$color_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$color_col} 0 0 0
6765 0 $color_m = $self->_tile_colour_measure($options->{color_agg}, $color_col);
6766 0 ▶ return { error => $color_m->{error} } if $color_m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $color_m->{"error"} 0 0 0
6767 }
6768  
6769 my $agg = $self->_grouped_totals(
6770 { source => $agg_src, records => $vis_recs },
6771 { by => [$cat_col],
6772 fn => $m->{fn},
6773 val => $m->{val},
6774 0 as => 'value' },
6775 );
6776 0 ▶ return { error => 'No plottable data' } unless @$agg;
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg 0 0 0
6777 0 ▶ my $colour_of = $color_m
Branch: 1 branch
BranchTrueFalseTotal
$color_m ? : 0 0 0
6778 ? $self->_tile_colours($agg_src, $vis_recs, $cat_col, $color_m)
6779 : {};
6780  
6781 0 ▶ $title ||= "$m->{label} by $cat_col" . ($color_m ? " coloured by $color_m->{label}" : q{});
Branch: 1 branch
BranchTrueFalseTotal
$color_m ? : 0 0 0
Condition0%0/3
$title ||= qq[$m->{"label"} by $cat_col] . ($color_m ? qq[ coloured by $color_m->{"label"}] : "")
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= qq[$m->{"label"} by $cat_col] . ($color_m ? qq[ coloured by $color_m->{"label"}] : "")
$title qq[$m->{"label"} by $cat_col] . ($color_m ? qq[ coloured by $color_m->{"label"}] : "")
Truth table0%0/3
A ||= B
A= $titleB= qq[$m->{"label"} by $cat_col] . ($color_m ? qq[ coloured by $color_m->{"label"}] : "")
ABresult
1X1
011
000
6782 0 my $scheme = _categorical_scheme($options->{scheme});
6783 # The snippet takes one row per tile as { path, value }, the shape the
6784 # treemap page builds; a [label, value] pair is not a path and croaks.
6785 my @tiles = map {
6786 0 ▶ my $cv = $colour_of->{ $_->{$cat_col} // '' };
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
6787 +{ path => 'Root/' . _decode_cell($_->{$cat_col}), value => $_->{value},
6788 0 ▶ (defined $cv ? (color_value => $cv) : ()) }
Branch: 1 branch
BranchTrueFalseTotal
defined $cv ? : 0 0 0
6789 } @$agg;
6790 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6791 ->render_treemap_snippet(\@tiles, {
6792 scheme => $scheme,
6793 id => $svg_id,
6794 ($color_m
6795 ? (color_scheme => _sequential_scheme($options->{color_scheme}),
6796 color_label => $color_m->{label})
6797 0 ▶ : ()),
Branch: 1 branch
BranchTrueFalseTotal
$color_m ? : 0 0 0
6798 });
6799 0 my $html = $sn->{html};
6800 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6801 0 return { html => $html, title => $title };
6802 }
6803  
6804 # ------------------------------------------------------------ histogram --
6805 11 ▶ if ($kind eq 'histogram') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "histogram") 0 11 11
6806 0 ▶ my $col = $encode->{value} // $encode->{col} // '';
Condition0%0/5
($encode->{"value"} // $encode->{"col"}) // ""
l!l
00
$encode->{"value"} // $encode->{"col"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($encode->{"value"} // $encode->{"col"}) // ""
$encode->{"value"} $encode->{"col"}
Truth table0%0/3
A // B
A= $encode->{"value"}B= $encode->{"col"}
ABresult
1X1
011
000
6807 0 ▶ return { error => 'Missing col for histogram visual' } unless length $col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 0 0 0
6808 0 ▶ return { error => "Column not found: $col" } unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 0 0 0
6809  
6810 0 my @values;
6811 0 for my $row (@$vis_recs) {
6812 0 ▶ my $v = _numeric_cell($row->{$col} // '');
Condition0%0/2
$row->{$col} // ""
l!l
00
MC/DC0%0/1
$row->{$col} // ""
$row->{$col}
Truth table0%0/2
A
A= $row->{$col}
Aresult
11
00
6813 0 ▶ push @values, $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
6814 }
6815 0 ▶ return { error => 'No plottable data' } unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 0 0
6816  
6817 0 ▶ my $bins = _whole_number($options->{bins}) // 10;
Condition0%0/2
_whole_number($options->{"bins"}) // 10
l!l
00
MC/DC0%0/1
_whole_number($options->{"bins"}) // 10
_whole_number($options->{"bins"})
Truth table0%0/2
A
A= _whole_number($options->{"bins"})
Aresult
11
00
6818 0 ▶ $bins = 1 if $bins < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bins < 1 0 0 0
6819 0 ▶ $bins = $MAX_HISTOGRAM_BINS if $bins > $MAX_HISTOGRAM_BINS;
Branch: 1 branch
BranchTrueFalseTotal
if $bins > $MAX_HISTOGRAM_BINS 0 0 0
6820 0 my $min_v = min(@values);
6821 0 my $max_v = max(@values);
6822 0 my @bin_data;
6823 0 ▶ if ($min_v == $max_v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_v == $max_v) { } 0 0 0
6824 0 push @bin_data, { label => "$min_v", count => scalar @values,
6825 lo => $min_v, hi => $min_v };
6826 } else {
6827 0 my $raw_w = ($max_v - $min_v) / $bins;
6828 0 my $width = sprintf('%.10g', $raw_w) + 0;
6829 0 ▶ $width = $raw_w if $width <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 0 0 0
6830 0 ▶ my $fmt = ($width == int($width)) ? '%d' : '%g';
Branch: 1 branch
BranchTrueFalseTotal
$width == int $width ? : 0 0 0
6831 0 my (@bin_counts, @bin_los);
6832 0 for my $i (0 .. $bins - 1) {
6833 0 push @bin_los, $min_v + $i * $width;
6834 0 push @bin_counts, 0;
6835 }
6836 0 for my $v (@values) {
6837 0 my $idx = POSIX::floor(sprintf('%.10g', ($v - $min_v) / $width) + 0);
6838 0 ▶ $idx = $bins - 1 if $idx >= $bins;
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= $bins 0 0 0
6839 0 ▶ $idx = 0 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 0 0
6840 0 $bin_counts[$idx]++;
6841 }
6842 0 for my $i (0 .. $bins - 1) {
6843 0 ▶ next unless $bin_counts[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $bin_counts[$i] 0 0 0
6844 0 my $lo = $bin_los[$i];
6845 0 my $hi = $lo + $width;
6846 0 push @bin_data, {
6847 label => sprintf("[${fmt},${fmt})", $lo, $hi),
6848 count => $bin_counts[$i],
6849 lo => $lo,
6850 hi => $hi,
6851 };
6852 }
6853 }
6854  
6855 0 ▶ $title ||= "Distribution of $col";
Condition0%0/2
$title ||= "Distribution of $col"
l!l
00
MC/DC0%0/1
$title ||= "Distribution of $col"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
6856 my $sn = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 480, height => 320, responsive => 1)
6857 ->render_histogram_snippet(\@bin_data, {
6858 binned => 1,
6859 x_label => $col,
6860 id => $svg_id,
6861 # What /histogram saves from scheme=. Stored unchecked, so it
6862 # goes through the same check as the page's.
6863 0 ▶ (_optional_scheme($options->{scheme}) ? (scheme => $options->{scheme}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
_optional_scheme($options->{'scheme'}) ? : 0 0 0
6864 });
6865 0 my $html = $sn->{html};
6866 0 ▶ utf8::decode($html) unless utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($html) 0 0 0
6867 0 return { html => $html, title => $title };
6868 }
6869  
6870 # ------------------------------------------------------------------- map --
6871 11 ▶ if ($kind eq 'map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "map") 1 10 11
6872 1 my $map = $self->_map_data(
6873 $vis_recs, $columns, $encode, $options, $spec_key, $vis_id,
6874 );
6875 1 ▶ return { error => $map->{error} } if $map->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $map->{"error"} 0 1 1
6876  
6877 1 my $html = '';
6878 1 ▶ if ($map->{map_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map->{"map_body"}) 0 1 1
6879 0 $html .= '<div class="canvas-map-wrap">' . $map->{map_body} . '</div>';
6880 }
6881 1 ▶ if ($map->{marker_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map->{"marker_count"}) 0 1 1
6882 $html .= '<p class="map-summary">Plotting '
6883 . $map->{marker_count} . ' marker' . ($map->{marker_count} == 1 ? '' : 's')
6884 . ' for ' . $map->{row_count} . ' of ' . $map->{source_rows}
6885 0 ▶ . ' row' . ($map->{source_rows} == 1 ? '' : 's') . '.</p>';
Branch: 2 branches
BranchTrueFalseTotal
$map->{'marker_count'} == 1 ? : 0 0 0
$map->{'source_rows'} == 1 ? : 0 0 0
6886 }
6887 1 ▶ if (@{ $map->{messages} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$map->{"messages"};}) 1 0 1
6888 $html .= '<div class="map-notes" role="status"><ul>'
6889 . join('', map {
6890 '<li class="map-note" data-key="' . xml_escape($_->{key} // '') . '">'
6891 3 ▶ . xml_escape($_->{text} // '') . '</li>'
Condition50%2/4
$_->{"key"} // ""
l!l
30
$_->{"text"} // ""
l!l
30
MC/DC0%0/1
$_->{"key"} // ""
$_->{"key"}
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"key"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
6892 1 } @{ $map->{messages} })
6893 . '</ul></div>';
6894 }
6895  
6896 return {
6897 html => $html,
6898 title => $title || $map->{title},
6899 1 ▶ head => ($map->{map_head} // ''),
Condition40%2/5
$title || $map->{'title'}
l!l&&r!l&&!r
010
$map->{"map_head"} // ""
l!l
01
MC/DC0%0/2
$title || $map->{'title'}
$title $map->{'title'}
MC/DC0%0/1
$map->{"map_head"} // ""
$map->{"map_head"}
Truth table33%1/3
A || B
A= $titleB= $map->{'title'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $map->{"map_head"}
Aresult
11
00
6900 };
6901 }
6902  
6903 # ------------------------------------------------------------ word cloud --
6904 # A card that draws the words of a text column, the way GET /wordcloud does,
6905 # from the same counted and ranked words (see _wordcloud_counts). It is the
6906 # one kind with no value column and no measure: the size of a word is how
6907 # many of the card's own rows contain it, so a card the reader has filtered
6908 # shows the words of those rows.
6909 10 ▶ if ($kind eq 'wordcloud') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "wordcloud") 0 10 10
6910 0 ▶ my $col = $encode->{col} // '';
Condition0%0/2
$encode->{"col"} // ""
l!l
00
MC/DC0%0/1
$encode->{"col"} // ""
$encode->{"col"}
Truth table0%0/2
A
A= $encode->{"col"}
Aresult
11
00
6911 0 ▶ return { error => 'Missing col for word cloud visual' } unless length $col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 0 0 0
6912 0 ▶ return { error => "Column not found: $col" } unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 0 0 0
6913  
6914 0 my $topn = _wordcloud_option($options->{topn}, 50, 1, 500);
6915 0 my $minlen = _wordcloud_option($options->{minlen}, 3, 1, 64);
6916  
6917 my $cloud = _wordcloud_counts(
6918 $vis_recs, $col, $topn, $minlen, $options->{stop},
6919 0 );
6920 0 my @tags = @{ $cloud->{words} };
6921  
6922 0 ▶ $title ||= "Words in $col";
Condition0%0/2
$title ||= "Words in $col"
l!l
00
MC/DC0%0/1
$title ||= "Words in $col"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
6923 my $summary = $cloud->{distinct}
6924 ? scalar(@tags) . ' of ' . $cloud->{distinct} . ' different word'
6925 . ($cloud->{distinct} == 1 ? '' : 's')
6926 . ' &middot; ' . $cloud->{total} . ' word'
6927 . ($cloud->{total} == 1 ? '' : 's')
6928 . ' in ' . $cloud->{rows} . ' row'
6929 0 ▶ . ($cloud->{rows} == 1 ? '' : 's')
Branch: 4 branches
BranchTrueFalseTotal
$cloud->{'distinct'} ? : 0 0 0
$cloud->{'distinct'} == 1 ? : 0 0 0
$cloud->{'total'} == 1 ? : 0 0 0
$cloud->{'rows'} == 1 ? : 0 0 0
6930 : '';
6931  
6932 0 ▶ if (!@tags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tags) 0 0 0
6933 return {
6934 0 html => '<div class="wordcloud-card"><p class="wordcloud-empty">'
6935 . 'No words: no row in this view has text in '
6936 . xml_escape($col) . ' at least ' . $minlen
6937 . ' characters long.</p></div>',
6938 title => $title,
6939 };
6940 }
6941  
6942 # HTML::TagCloud ranks the words and works out each one's level; the size
6943 # it would give that level is 12px plus the level. The size is written on
6944 # the word here instead of using the module's style sheet, because that
6945 # sheet scopes itself through #htmltagcloud -- an id only one element on a
6946 # page may carry, and a canvas may hold two word-cloud cards. The level,
6947 # the part that is actually arithmetic, stays the module's. The module is
6948 # handed the word as it is and the escaping happens on the way out, because
6949 # the same word is both what the reader reads and, unescaped, the filter a
6950 # click on it asks for.
6951 0 require HTML::TagCloud;
6952 0 my $ranked = HTML::TagCloud->new(levels => 18);
6953 0 $ranked->add_static($_->{word}, $_->{count}) for @tags;
6954  
6955 # A word is a control, not a label. Clicking one cross-filters the canvas
6956 # the way a chart mark does -- ?cf=...&cv=<card>, so every other card is
6957 # narrowed and this one stays drawn in full -- but with C<contains> rather
6958 # than C<eq>: a word is a token inside a cell, not the cell's whole value,
6959 # so no row equals it. The href carries the whole URL the click asks for,
6960 # so a word filters even before the page's script runs; the script takes
6961 # the click over only to keep the rest of the query string (a bound
6962 # parameter, a drill path) and to record the undo step, as it does for a
6963 # mark. A card drawn with no index -- by anything that is not a canvas
6964 # render -- names no source, so the filter applies to every card.
6965 0 ▶ my $cv = (defined $vis_id && $vis_id =~ /\A\d+\z/) ? "&cv=$vis_id" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $vis_id && $vis_id =~ /\A\d+\z/u ? : 0 0 0
Condition0%0/3
defined $vis_id and $vis_id =~ /\A\d+\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $vis_id and $vis_id =~ /\A\d+\z/u
defined $vis_id $vis_id =~ /\A\d+\z/u
Truth table0%0/3
A and B
A= defined $vis_idB= $vis_id =~ /\A\d+\z/u
ABresult
0X0
100
111
6966 my $spans = join(' ', map {
6967 0 my $word = $_->{name};
6968 0 my $label = xml_escape("Filter by $col: $word");
6969 # aria-describedby names the canvas's cross-filter help, which covers
6970 # a word as well as a chart mark: the label says which word this is,
6971 # the help says what pressing Enter on it does.
6972 '<a class="wordcloud-word" href="?cf='
6973 . _percent_encode("$col:contains:$word") . $cv . '"'
6974 . ' data-cf-value="' . xml_escape($word) . '"'
6975 0 . ' style="font-size:' . (12 + $_->{level}) . 'px"'
6976 . ' aria-label="' . $label . '" title="' . $label . '"'
6977 . ' aria-describedby="cf-help">'
6978 . xml_escape($word) . '</a>'
6979 } $ranked->tags(scalar @tags));
6980  
6981 return {
6982 0 html => '<div class="wordcloud-card" data-col="' . xml_escape($col) . '">'
6983 . '<p class="wordcloud-cloud">' . $spans . '</p>'
6984 . '<p class="wordcloud-summary">' . $summary . '</p></div>',
6985 title => $title,
6986 };
6987 }
6988  
6989 # ----------------------------------------------------------------- table --
6990 10 ▶ if ($kind eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "table") 0 10 10
6991 0 ▶ return { error => 'No data to display' } unless @$vis_recs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$vis_recs 0 0 0
6992 0 my @cols = @$columns;
6993 0 ▶ if (ref $encode->{columns} eq 'ARRAY' && @{ $encode->{columns} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $encode->{"columns"} eq "ARRAY" and @{$encode->{"columns"};}) 0 0 0
Condition0%0/3
ref $encode->{"columns"} eq "ARRAY" and @{$encode->{"columns"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $encode->{"columns"} eq "ARRAY" and @{$encode->{"columns"};}
ref $encode->{"columns"} eq "ARRAY" @{$encode->{"columns"};}
Truth table0%0/3
A and B
A= ref $encode->{"columns"} eq "ARRAY"B= @{$encode->{"columns"};}
ABresult
0X0
100
111
6994 # A table names the columns it shows, and it is the one kind that
6995 # could name a column nothing produces: the heading printed and
6996 # every cell under it was empty, which reads as missing data rather
6997 # than as a card drawn against a column that is not there. Whether
6998 # a plan produces a name is not something a reader can see from the
6999 # card, so the name is refused here as the charts below refuse it.
7000 0 for my $c (@{ $encode->{columns} }) {
7001 0 ▶ next if ref $c || !length $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c or not length $c 0 0 0
Condition0%0/3
ref $c or not length $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c or not length $c
ref $c not length $c
Truth table0%0/3
A or not(B)
A= ref $cB= not length $c
ABresult
1X1
011
000
7002 0 ▶ return { error => "Column not found: $c" } unless $col_set{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$c} 0 0 0
7003 }
7004 0 @cols = @{ $encode->{columns} };
7005 }
7006  
7007 0 ▶ $title ||= 'Table';
Condition0%0/2
$title ||= "Table"
l!l
00
MC/DC0%0/1
$title ||= "Table"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
7008 0 ▶ my $max_rows_t = int($options->{rows} // 50);
Condition0%0/2
$options->{"rows"} // 50
l!l
00
MC/DC0%0/1
$options->{"rows"} // 50
$options->{"rows"}
Truth table0%0/2
A
A= $options->{"rows"}
Aresult
11
00
7009 0 ▶ $max_rows_t = 200 if $max_rows_t > 200;
Branch: 1 branch
BranchTrueFalseTotal
if $max_rows_t > 200 0 0 0
7010 0 ▶ $max_rows_t = 1 if $max_rows_t < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max_rows_t < 1 0 0 0
7011  
7012 0 my $html = '<div class="visual-table-wrap"><table class="data-table">'
7013 . '<thead><tr>';
7014 0 for my $c (@cols) {
7015 0 (my $ch = $c) =~ s/&/&amp;/g; $ch =~ s/</&lt;/g; $ch =~ s/>/&gt;/g;
7016 0 $html .= "<th>$ch</th>";
7017 }
7018 0 $html .= '</tr></thead><tbody>';
7019 0 my $count = 0;
7020 0 for my $row (@$vis_recs) {
7021 0 ▶ last if $count++ >= $max_rows_t;
Branch: 1 branch
BranchTrueFalseTotal
if $count++ >= $max_rows_t 0 0 0
7022 0 $html .= '<tr>';
7023 0 for my $c (@cols) {
7024 0 ▶ my $v = $row->{$c} // '';
Condition0%0/2
$row->{$c} // ""
l!l
00
MC/DC0%0/1
$row->{$c} // ""
$row->{$c}
Truth table0%0/2
A
A= $row->{$c}
Aresult
11
00
7025 0 $v =~ s/&/&amp;/g; $v =~ s/</&lt;/g; $v =~ s/>/&gt;/g;
7026 0 $html .= "<td>$v</td>";
7027 }
7028 0 $html .= '</tr>';
7029 }
7030 0 $html .= '</tbody></table></div>';
7031 0 return { html => $html, title => $title };
7032 }
7033  
7034 10 return { error => "Unknown visual kind: $kind" };
7035 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7036  
7037 # _vis_cf_col(\%vis) -> $col_name | undef
7038 #
7039 # Returns the primary categorical dimension of a visual -- the column that
7040 # makes sense to cross-filter on when the user clicks a mark.
7041 # Priority: cat > group > x > col (histogram). Series/size/val are measures,
7042 # not dimensions, so they are intentionally excluded.
7043 27 ▶sub _vis_cf_col {
Subroutine _vis_cf_col: called CC=5 SCAR=16.1
7044 27 my ($vis) = @_;
7045 27 ▶ my $enc = $vis->{encode} // {};
Condition100%2/2
$vis->{"encode"} // {}
l!l
1710
MC/DC100%1/1
$vis->{"encode"} // {}
$vis->{"encode"}
Truth table100%2/2
A
A= $vis->{"encode"}
Aresult
11
00
7046 27 for my $key (qw(cat group x col)) {
7047 66 ▶ return $enc->{$key} if length($enc->{$key} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($enc->{$key} // "") 16 50 66
Condition100%2/2
$enc->{$key} // ""
l!l
1749
MC/DC100%1/1
$enc->{$key} // ""
$enc->{$key}
Truth table100%2/2
A
A= $enc->{$key}
Aresult
11
00
7048 }
7049 11 return undef;
7050 }
7051  
7052 # GET /a/:id -- render a saved canvas
7053 #
7054 # redirects to the reconstructed data-view URL when the
7055 # canvas has no visuals array.
7056 # renders N visuals in a CSS grid when visuals are present.
7057 # _bound_canvas_params($self, $a, $lookup) -> (\%bound, \@errors)
7058 #
7059 # Resolve the canvas's declared parameters against URL values. $lookup takes a
7060 # parameter name ("p.<name>") and returns the raw string from the request or
7061 # undef; the default is used when it returns undef. Numbers are coerced and
7062 # range-checked, and an error is reported rather than a value bound, so the
7063 # canvas page and a card appended to it draw the same parameter values.
7064 58 ▶sub _bound_canvas_params :Protected ($self, $a, $lookup) {
Subroutine _bound_canvas_params: called CC=16 SCAR=27.7
7065 58 ▶ my %param_defs = %{ $a->{params} // {} };
Condition100%2/2
$a->{"params"} // {}
l!l
1939
MC/DC100%1/1
$a->{"params"} // {}
$a->{"params"}
Truth table100%2/2
A
A= $a->{"params"}
Aresult
11
00
7066 58 my (%bound_params, @errors);
7067  
7068 58 for my $pname (sort keys %param_defs) {
7069 20 ▶ my $def = ref $param_defs{$pname} eq 'HASH' ? $param_defs{$pname} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $param_defs{$pname} eq 'HASH' ? : 17 3 20
7070 20 ▶ my $type = $def->{type} // 'string';
Condition100%2/2
$def->{"type"} // "string"
l!l
119
MC/DC100%1/1
$def->{"type"} // "string"
$def->{"type"}
Truth table100%2/2
A
A= $def->{"type"}
Aresult
11
00
7071 20 ▶ my $default = $def->{default} // '';
Condition100%2/2
$def->{"default"} // ""
l!l
218
MC/DC100%1/1
$def->{"default"} // ""
$def->{"default"}
Truth table100%2/2
A
A= $def->{"default"}
Aresult
11
00
7072 20 my $raw = $lookup->("p.$pname");
7073 20 ▶ my $val = defined $raw ? $raw : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 11 9 20
7074  
7075 20 ▶ if ($type eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 11 9 20
7076 11 require Scalar::Util;
7077 # looks_like_number accepts these, and every comparison with NaN is
7078 # false, so it would walk straight past min and max below.
7079 11 ▶ unless (Scalar::Util::looks_like_number($val) && $val !~ /\A\s*[-+]?(?:nan|inf)/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Scalar::Util::looks_like_number($val) and not $val =~ /\A\s*[-+]?(?:nan|inf)/iu) 6 5 11
Condition100%3/3
&Scalar::Util::looks_like_number($val) and not $val =~ /\A\s*[-+]?(?:nan|inf)/iu
!ll&&!rl&&r
155
MC/DC100%2/2
&Scalar::Util::looks_like_number($val) and not $val =~ /\A\s*[-+]?(?:nan|inf)/iu
&Scalar::Util::looks_like_number($val) not $val =~ /\A\s*[-+]?(?:nan|inf)/iu
Truth table100%3/3
A and not(B)
A= &Scalar::Util::looks_like_number($val)B= not $val =~ /\A\s*[-+]?(?:nan|inf)/iu
ABresult
0X0
100
111
7080 6 push @errors, sprintf(_message_text('error_param_type'), $pname);
7081 6 next;
7082 }
7083 5 $val = $val + 0;
7084 5 my ($lo, $hi) = ($def->{min}, $def->{max});
7085 5 ▶ if (defined $lo && $val < $lo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lo and $val < $lo) 1 4 5
Condition100%3/3
defined $lo and $val < $lo
!ll&&!rl&&r
311
MC/DC100%2/2
defined $lo and $val < $lo
defined $lo $val < $lo
Truth table100%3/3
A and B
A= defined $loB= $val < $lo
ABresult
0X0
100
111
7086 1 ▶ push @errors, sprintf(_message_text('error_param_range'),
Branch: 1 branch
BranchTrueFalseTotal
defined $hi ? : 0 1 1
7087 $pname, $lo, defined $hi ? $hi : '+inf');
7088 1 next;
7089 }
7090 4 ▶ if (defined $hi && $val > $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hi and $val > $hi) 2 2 4
Condition66%2/3
defined $hi and $val > $hi
!ll&&!rl&&r
202
MC/DC50%1/2
defined $hi and $val > $hi
defined $hi $val > $hi
Truth table66%2/3
A and B
A= defined $hiB= $val > $hi
ABresult
0X0
100
111
7091 2 ▶ push @errors, sprintf(_message_text('error_param_range'),
Branch: 1 branch
BranchTrueFalseTotal
defined $lo ? : 1 1 2
7092 $pname, defined $lo ? $lo : '-inf', $hi);
7093 2 next;
7094 }
7095 }
7096 11 $bound_params{$pname} = $val;
7097 }
7098  
7099 58 return (\%bound_params, \@errors);
7100 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7101  
7102 27 ▶sub canvas_render ($self) {
Subroutine canvas_render: called CC=125 SCAR=74.1
Pod documented
7103 27 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
270
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
7104 27 ▶ return $self->render(text => _message_text('error_analysis_id_invalid'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 0 27 27
7105 unless $id =~ /\A[A-Za-z0-9]+\z/;
7106  
7107 27 my $analyses = $self->_read_analyses;
7108 27 ▶ unless (exists $analyses->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $analyses->{$id}) 4 23 27
7109 # An HTML page, not plain text, so the user keeps the breadcrumb and
7110 # the site navigation. No canvas_id: there is nothing to delete.
7111 4 my ($platform, $language) = $self->_resolve_template;
7112 4 return $self->render(
7113 handler => 'tt',
7114 template => "$platform/$language/canvas_error",
7115 format => 'html',
7116 status => 404,
7117 title => 'Canvas not found',
7118 canvas_name => $id,
7119 error_msg => sprintf(_message_text('error_analysis_not_found'), $id),
7120 error_specs => [],
7121 language => $language,
7122 );
7123 }
7124 23 my $a = $analyses->{$id};
7125  
7126 # freshness: compare each dataset's current mtime against
7127 # the analysis's saved updated timestamp. Flag stale when any source
7128 # is newer. Non-fatal: the canvas renders with a stale banner.
7129 23 my ($freshness_stale, $freshness_msg, $freshness_source_mtime) = (0, '', '');
7130 {
7131 23 ▶ my $analysis_updated = $a->{updated} // 0;
Condition100%2/2
$a->{"updated"} // 0
l!l
320
MC/DC100%1/1
$a->{"updated"} // 0
$a->{"updated"}
Truth table100%2/2
A
A= $a->{"updated"}
Aresult
11
00
7132 # Parse ISO-8601 to epoch for comparison if needed.
7133 23 ▶ if ($analysis_updated && $analysis_updated =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis_updated and $analysis_updated =~ / \A (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day T (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec Z? \z /ux) 0 23 23
Condition66%2/3
$analysis_updated and $analysis_updated =~ / \A (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day T (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec Z? \z /ux
!ll&&!rl&&r
2030
MC/DC0%0/2
$analysis_updated and $analysis_updated =~ / \A (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day T (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec Z? \z /ux
$analysis_updated $analysis_updated =~ / \A (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day T (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec Z? \z /ux
Truth table66%2/3
A and B
A= $analysis_updatedB= $analysis_updated =~ / \A (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day T (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec Z? \z /ux
ABresult
0X0
100
111
7134 \A
7135 (\d{4}) - (\d{2}) - (\d{2}) # $1 yr $2 mon $3 day
7136 T
7137 (\d{2}) : (\d{2}) : (\d{2}) # $4 hr $5 min $6 sec
7138 Z? \z
7139 /x) {
7140 0 require POSIX;
7141 0 $analysis_updated = POSIX::mktime($6, $5, $4, $3, $2 - 1, $1 - 1900);
7142 }
7143 23 my $newest_src_mtime = 0;
7144 23 ▶ for my $ds (@{ $a->{datasets} // [] }) {
Condition100%2/2
$a->{"datasets"} // []
l!l
203
MC/DC100%1/1
$a->{"datasets"} // []
$a->{"datasets"}
Truth table100%2/2
A
A= $a->{"datasets"}
Aresult
11
00
7145 20 ▶ my $spec = $ds->{spec} // '';
Condition50%1/2
$ds->{"spec"} // ""
l!l
200
MC/DC0%0/1
$ds->{"spec"} // ""
$ds->{"spec"}
Truth table50%1/2
A
A= $ds->{"spec"}
Aresult
11
00
7146 20 ▶ next unless $spec =~ /\A(?:table|path):/;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /\A(?:table|path):/u 0 20 20
7147 20 my ($src) = eval { $self->_open_spec($spec) };
7148 20 ▶ next unless $src && $src->can('updated');
Branch: 1 branch
BranchTrueFalseTotal
unless $src and $src->can("updated") 0 20 20
Condition33%1/3
$src and $src->can("updated")
!ll&&!rl&&r
0020
MC/DC0%0/2
$src and $src->can("updated")
$src $src->can("updated")
Truth table33%1/3
A and B
A= $srcB= $src->can("updated")
ABresult
0X0
100
111
7149 20 ▶ my $mt = eval { $src->updated } // 0;
Condition50%1/2
eval { do { $src->updated } } // 0
l!l
200
MC/DC0%0/1
eval { do { $src->updated } } // 0
eval { do { $src->updated } }
Truth table50%1/2
A
A= eval { do { $src->updated } }
Aresult
11
00
7150 20 ▶ $newest_src_mtime = $mt if $mt > $newest_src_mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $mt > $newest_src_mtime 20 0 20
7151 }
7152 23 ▶ if ($newest_src_mtime && $analysis_updated && $newest_src_mtime > $analysis_updated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newest_src_mtime and $analysis_updated and $newest_src_mtime > $analysis_updated) 0 23 23
Condition83%5/6
$newest_src_mtime and $analysis_updated and $newest_src_mtime > $analysis_updated
!ll&&!rl&&r
2030
$newest_src_mtime and $analysis_updated
!ll&&!rl&&r
3173
MC/DC0%0/3
$newest_src_mtime and $analysis_updated and $newest_src_mtime > $analysis_updated
$newest_src_mtime $analysis_updated $newest_src_mtime > $analysis_updated
Truth table75%3/4
A and B and C
A= $newest_src_mtimeB= $analysis_updatedC= $newest_src_mtime > $analysis_updated
ABCresult
0XX0
10X0
1100
1111
7153 0 $freshness_stale = 1;
7154 0 $freshness_msg = _message_text('freshness_stale_msg');
7155 0 require POSIX;
7156 0 $freshness_source_mtime = Database::BI::Util::minute_stamp(gmtime($newest_src_mtime)) . ' UTC';
7157 }
7158 }
7159  
7160 # if visuals array is non-empty, render the canvas.
7161 23 ▶ my @visuals = @{ $a->{visuals} // [] };
Condition100%2/2
$a->{"visuals"} // []
l!l
176
MC/DC100%1/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
7162 23 ▶ if (@visuals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@visuals) 17 6 23
7163  
7164 # bound parameters from URL ?p.<name>=<value>.
7165 # Parameter definitions live in the analysis: params => { name => { type, default, min, max } }.
7166 # URL values are type-coerced and range-checked; out-of-range returns 400.
7167 my ($bound_ref, $p_errors) = $self->_bound_canvas_params(
7168 2 ▶ $a, sub { $self->param($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
7169 17 );
7170 17 my %bound_params = %$bound_ref;
7171 17 ▶ if (@$p_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$p_errors) 0 17 17
7172 0 return $self->render(
7173 text => join('; ', @$p_errors),
7174 status => 400,
7175 );
7176 }
7177  
7178 # Build stash list for parameter bar: sorted by name.
7179 17 ▶ my %param_defs = %{ $a->{params} // {} };
Condition100%2/2
$a->{"params"} // {}
l!l
215
MC/DC100%1/1
$a->{"params"} // {}
$a->{"params"}
Truth table100%2/2
A
A= $a->{"params"}
Aresult
11
00
7180 my @canvas_params = map {
7181 17 my $def = $param_defs{$_};
7182 {
7183 name => $_,
7184 type => ($def->{type} // 'string'),
7185 value => $bound_params{$_},
7186 default => ($def->{default} // ''),
7187 min => (defined $def->{min} ? $def->{min} : ''),
7188 2 ▶ max => (defined $def->{max} ? $def->{max} : ''),
Branch: 2 branches
BranchTrueFalseTotal
defined $def->{'min'} ? : 0 2 2
defined $def->{'max'} ? : 0 2 2
Condition50%2/4
$def->{"type"} // "string"
l!l
02
$def->{"default"} // ""
l!l
02
MC/DC0%0/1
$def->{"type"} // "string"
$def->{"type"}
MC/DC0%0/1
$def->{"default"} // ""
$def->{"default"}
Truth table50%1/2
A
A= $def->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $def->{"default"}
Aresult
11
00
7189 }
7190 } sort keys %param_defs;
7191  
7192 # Everything else the canvas is showing has to survive a change of time
7193 # range, so the control carries the rest of this page's query string: the
7194 # cross-filter (cf/vc), the drill path (dd/dl), the calculated columns
7195 # (c) and the what-if parameter values (p.<name>).
7196 17 my @canvas_range_hidden;
7197 {
7198 17 my $q = $self->req->query_params;
7199 17 for my $name ($q->names) {
7200 17 ▶ next if $name eq 'range' || $name eq 'rcol';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "range" or $name eq "rcol" 0 17 17
Condition33%1/3
$name eq "range" or $name eq "rcol"
l!l&&r!l&&!r
0017
MC/DC0%0/2
$name eq "range" or $name eq "rcol"
$name eq "range" $name eq "rcol"
Truth table33%1/3
A or B
A= $name eq "range"B= $name eq "rcol"
ABresult
1X1
011
000
7201 push @canvas_range_hidden,
7202 0 ▶ map { { name => $name, value => $_ } } @{ $q->every_param($name) // [] };
Condition50%1/2
$q->every_param($name) // []
l!l
170
MC/DC0%0/1
$q->every_param($name) // []
$q->every_param($name)
Truth table50%1/2
A
A= $q->every_param($name)
Aresult
11
00
7203 }
7204 }
7205  
7206 17 my ($src, $records, $columns) = $self->_build_canvas_pipeline($a);
7207 17 ▶ unless (defined $records) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $records) 0 17 17
7208 0 my ($platform, $language) = $self->_resolve_template;
7209 0 ▶ my @specs = map { $_->{spec} // '' } @{ $a->{datasets} // [] };
Condition0%0/4
$_->{"spec"} // ""
l!l
00
$a->{"datasets"} // []
l!l
00
MC/DC0%0/1
$_->{"spec"} // ""
$_->{"spec"}
MC/DC0%0/1
$a->{"datasets"} // []
$a->{"datasets"}
Truth table0%0/2
A
A= $_->{"spec"}
Aresult
11
00
Truth table0%0/2
A
A= $a->{"datasets"}
Aresult
11
00
7210 return $self->render(
7211 handler => 'tt',
7212 template => "$platform/$language/canvas_error",
7213 format => 'html',
7214 status => 422,
7215 title => 'Canvas unavailable',
7216 canvas_id => $id,
7217 0 ▶ canvas_name => ($a->{name} // $id),
Condition0%0/3
$a->{"name"} // $id
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->{"name"} // $id
$a->{"name"} $id
Truth table0%0/3
A // B
A= $a->{"name"}B= $id
ABresult
1X1
011
000
7218 error_msg => 'The data source for this canvas could not be opened.',
7219 error_specs => \@specs,
7220 language => $language,
7221 );
7222 }
7223  
7224 # Apply calc fields with bound params so expressions may reference them.
7225 17 ▶ my $canvas_spec_key = ($a->{datasets}[0]{spec} // '');
Condition50%1/2
$a->{"datasets"}[0]{"spec"} // ""
l!l
170
MC/DC0%0/1
$a->{"datasets"}[0]{"spec"} // ""
$a->{"datasets"}[0]{"spec"}
Truth table50%1/2
A
A= $a->{"datasets"}[0]{"spec"}
Aresult
11
00
7226 17 my $window_names = _canvas_window_columns($a, $self);
7227 17 $self->stash('_canvas_window_names' => $window_names);
7228 17 eval {
7229 17 ($records, $columns) = $self->_apply_calc_fields(
7230 $records, $columns,
7231 {
7232 params => \%bound_params,
7233 spec_key => $canvas_spec_key,
7234 window_columns => $window_names,
7235 }
7236 );
7237 };
7238 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
7239 0 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
7240 0 return $self->render(text => $e, status => 400);
7241 }
7242  
7243 # cs= belongs to the page, not to a card: one plan, run per card on that
7244 # card's own rows (see _render_visual_html). A fault is the page's 400 --
7245 # the same message repeated on every card would be noise, not a report.
7246 17 my $cross_row = $self->_cross_row_specs($columns);
7247 17 ▶ if ($cross_row && $cross_row->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cross_row and $cross_row->{"error"}) 0 17 17
Condition33%1/3
$cross_row and $cross_row->{"error"}
!ll&&!rl&&r
1700
MC/DC0%0/2
$cross_row and $cross_row->{"error"}
$cross_row $cross_row->{"error"}
Truth table33%1/3
A and B
A= $cross_rowB= $cross_row->{"error"}
ABresult
0X0
100
111
7248 0 return $self->render(text => $cross_row->{error}, status => 400);
7249 }
7250  
7251 # The relative time range the page names (?range=/?rcol=) narrows every
7252 # card, before the per-card cross-filter. The before_dispatch hook has
7253 # already resolved it into the f= params the routes apply, but the canvas
7254 # builds its own pipeline, so it applies the range itself from the same
7255 # two specs.
7256 17 ▶ if (my $range = $self->stash('time_range')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $self->stash("time_range")) 0 17 17
7257 0 ▶ for my $spec (@{ $self->stash('range_specs') // [] }) {
Condition0%0/2
$self->stash("range_specs") // []
l!l
00
MC/DC0%0/1
$self->stash("range_specs") // []
$self->stash("range_specs")
Truth table0%0/2
A
A= $self->stash("range_specs")
Aresult
11
00
7258 0 $records = _apply_filter_spec($records, $spec);
7259 }
7260 }
7261  
7262 # cross-filter state from URL params.
7263 # ?cf=col:op:val (repeatable) filters all visuals except the source.
7264 # ?cv=N identifies the source visual (shown unfiltered but highlighted).
7265 # Semantics: self-highlighting (source shown in full; others filtered).
7266 # Multiple simultaneous selections are not supported in this version.
7267 17 ▶ my @cf_specs = grep { /\S/ } @{ $self->every_param('cf') // [] };
Condition50%1/2
$self->every_param("cf") // []
l!l
170
MC/DC0%0/1
$self->every_param("cf") // []
$self->every_param("cf")
Truth table50%1/2
A
A= $self->every_param("cf")
Aresult
11
00
7268 17 my $cf_source = _cross_filter_source($self->param('cv'));
7269 17 my $cf_active = !!@cf_specs;
7270  
7271 # Build the "clear cross-filter" URL for the chip close button.
7272 17 my $cf_clear_url = $self->req->url->clone;
7273 17 $cf_clear_url->query->remove('cf')->remove('cv');
7274 17 $cf_clear_url = $cf_clear_url->to_string;
7275  
7276 # Human-readable summary for the cross-filter chip, e.g. "region = North".
7277 17 my $cf_display = '';
7278 17 ▶ if ($cf_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf_active) 1 16 17
7279 1 my @parts;
7280 1 for my $spec (@cf_specs) {
7281 1 my ($col, $op, $val) = split /:/, $spec, 3;
7282 1 ▶ $val //= '';
Condition50%1/2
$val //= ""
l!l
10
MC/DC0%0/1
$val //= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
7283 1 ▶ my $sym = ($op eq 'eq') ? '=' : ($op eq 'ne') ? '!='
Branch: 8 branches
BranchTrueFalseTotal
$op eq 'eq' ? : 1 0 1
$op eq 'ne' ? : 0 0 0
$op eq 'lt' ? : 0 0 0
$op eq 'le' ? : 0 0 0
$op eq 'gt' ? : 0 0 0
$op eq 'ge' ? : 0 0 0
$op eq 'contains' ? : 0 0 0
$op eq 'starts' ? : 0 0 0
7284 : ($op eq 'lt') ? '<' : ($op eq 'le') ? '<='
7285 : ($op eq 'gt') ? '>' : ($op eq 'ge') ? '>='
7286 : ($op eq 'contains') ? 'contains'
7287 : ($op eq 'starts') ? 'starts with'
7288 : $op;
7289 1 push @parts, "$col $sym $val";
7290 }
7291 1 $cf_display = join(' AND ', @parts);
7292 }
7293  
7294 # drill-through and drill-down.
7295 #
7296 # Drill-through: when cross-filters are active, build a URL to the
7297 # primary data source with those filters applied as ?f= params so the
7298 # user can inspect the raw rows behind the selection.
7299 17 my $drillthrough_url = '';
7300 17 ▶ if (@cf_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cf_specs) 1 16 17
7301 1 ▶ my $ds0 = ($a->{datasets} // [])->[0];
Condition50%1/2
$a->{"datasets"} // []
l!l
10
MC/DC0%0/1
$a->{"datasets"} // []
$a->{"datasets"}
Truth table50%1/2
A
A= $a->{"datasets"}
Aresult
11
00
7302 1 ▶ my $spec0 = ($ds0 // {})->{spec} // '';
Condition50%2/4
+($ds0 // {})->{"spec"} // ""
l!l
10
$ds0 // {}
l!l
10
MC/DC0%0/1
+($ds0 // {})->{"spec"} // ""
$ds0
Truth table50%1/2
A
A= $ds0
Aresult
11
00
7303 1 my $base = $self->_spec_to_url($spec0);
7304 1 ▶ if ($base ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($base ne "/") 1 0 1
7305 1 ▶ my $sep = ($base =~ /\?/) ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$base =~ /\?/u ? : 1 0 1
7306 1 for my $cf (@cf_specs) {
7307 1 $base .= $sep . 'f=' . url_escape(encode('UTF-8', $cf));
7308 1 $sep = '&';
7309 }
7310 1 $drillthrough_url = $base;
7311 }
7312 }
7313  
7314 # Drill-down: navigate a named hierarchy one level at a time.
7315 # ?dd=<name> names the active hierarchy; ?dl=<N> is the current level
7316 # (0-based); accumulated ?cf= params carry the drill path.
7317 17 ▶ my $dd_name = $self->param('dd') // '';
Condition50%1/2
$self->param("dd") // ""
l!l
017
MC/DC0%0/1
$self->param("dd") // ""
$self->param("dd")
Truth table50%1/2
A
A= $self->param("dd")
Aresult
11
00
7318 17 my $dl_raw = $self->param('dl');
7319 17 ▶ my $dd_level = (defined $dl_raw && length $dl_raw) ? int($dl_raw) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $dl_raw && length $dl_raw ? : 0 17 17
Condition33%1/3
defined $dl_raw and length $dl_raw
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $dl_raw and length $dl_raw
defined $dl_raw length $dl_raw
Truth table33%1/3
A and B
A= defined $dl_rawB= length $dl_raw
ABresult
0X0
100
111
7320 17 ▶ $dd_level = 0 if $dd_level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dd_level < 0 0 17 17
7321  
7322 0 ▶ my ($dd_hier) = grep { ($_->{name} // '') eq $dd_name }
Condition0%0/2
$_->{"name"} // ""
l!l
00
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table0%0/2
A
A= $_->{"name"}
Aresult
11
00
7323 17 ▶ @{ $a->{hierarchies} // [] };
Condition50%1/2
$a->{"hierarchies"} // []
l!l
017
MC/DC0%0/1
$a->{"hierarchies"} // []
$a->{"hierarchies"}
Truth table50%1/2
A
A= $a->{"hierarchies"}
Aresult
11
00
7324  
7325 # A drill needs a first level to name: a hierarchy with none answers no
7326 # press, and #cf-help has no column it could call the one that drills.
7327 my $drill_active = ($dd_hier && length $dd_name
7328 0 ▶ && @{ $dd_hier->{levels} // [] }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dd_hier && length $dd_name && @{$dd_hier->{'levels'} // [];} ? : 0 17 17
Condition25%2/8
$dd_hier and length $dd_name and @{$dd_hier->{"levels"} // [];}
!ll&&!rl&&r
1700
$dd_hier and length $dd_name
!ll&&!rl&&r
1700
$dd_hier->{"levels"} // []
l!l
00
MC/DC0%0/3
$dd_hier and length $dd_name and @{$dd_hier->{"levels"} // [];}
$dd_hier length $dd_name @{$dd_hier->{"levels"} // [];}
MC/DC0%0/1
$dd_hier->{"levels"} // []
$dd_hier->{"levels"}
Truth table25%1/4
A and B and C
A= $dd_hierB= length $dd_nameC= @{$dd_hier->{"levels"} // [];}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $dd_hier->{"levels"}
Aresult
11
00
7329 17 my $drill_col = '';
7330 17 my @drill_crumb = ();
7331 17 my $drill_max = 0;
7332  
7333 17 ▶ if ($drill_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drill_active) 0 17 17
7334 0 ▶ my @levels = @{ $dd_hier->{levels} // [] };
Condition0%0/2
$dd_hier->{"levels"} // []
l!l
00
MC/DC0%0/1
$dd_hier->{"levels"} // []
$dd_hier->{"levels"}
Truth table0%0/2
A
A= $dd_hier->{"levels"}
Aresult
11
00
7335 0 $drill_max = $#levels;
7336 # Allow dl up to scalar(@levels) -- one past the last valid level
7337 # means "all levels drilled through" (drill-through mode).
7338 0 my $dl_cap = scalar @levels;
7339 0 ▶ $dd_level = $dl_cap if $dd_level > $dl_cap;
Branch: 1 branch
BranchTrueFalseTotal
if $dd_level > $dl_cap 0 0 0
7340 0 ▶ $drill_col = $levels[$dd_level] // '';
Condition0%0/2
$levels[$dd_level] // ""
l!l
00
MC/DC0%0/1
$levels[$dd_level] // ""
$levels[$dd_level]
Truth table0%0/2
A
A= $levels[$dd_level]
Aresult
11
00
7341  
7342 # Extract drilled values: each crossed level contributes a
7343 # cf=<col>:eq:<val> param in the URL.
7344 0 my %cf_by_col;
7345 0 for my $cf (@cf_specs) {
7346 0 my ($col, $op, $val) = split /:/, $cf, 3;
7347 0 ▶ $cf_by_col{$col} = ($val // '')
Branch: 1 branch
BranchTrueFalseTotal
if length($col // "") and ($op // "") eq "eq" 0 0 0
Condition0%0/9
length($col // "") and ($op // "") eq "eq"
!ll&&!rl&&r
000
$col // ""
l!l
00
$op // ""
l!l
00
$val // ""
l!l
00
MC/DC0%0/2
length($col // "") and ($op // "") eq "eq"
$col $op
MC/DC0%0/1
$val // ""
$val
Truth table0%0/3
A and B
A= $colB= $op
ABresult
0X0
100
111
Truth table0%0/2
A
A= $val
Aresult
11
00
7348 if length($col // '') && ($op // '') eq 'eq';
7349 }
7350  
7351 # "All" crumb -- keep ?dd=, remove dl/cf/cv.
7352 0 my $all_url = $self->req->url->clone;
7353 0 $all_url->query->remove('dl')->remove('cf')->remove('cv');
7354 0 ▶ push @drill_crumb, {
Branch: 2 branches
BranchTrueFalseTotal
$dd_level > 0 ? : 0 0 0
$dd_level == 0 ? : 0 0 0
7355 label => 'All',
7356 url => ($dd_level > 0 ? $all_url->to_string : ''),
7357 cur => ($dd_level == 0 ? 1 : 0),
7358 };
7359  
7360 # One crumb per drilled value (levels 0 .. dd_level-1).
7361 0 for my $i (0 .. $dd_level - 1) {
7362 0 my $col_i = $levels[$i];
7363 0 ▶ my $val_i = $cf_by_col{$col_i} // '';
Condition0%0/2
$cf_by_col{$col_i} // ""
l!l
00
MC/DC0%0/1
$cf_by_col{$col_i} // ""
$cf_by_col{$col_i}
Truth table0%0/2
A
A= $cf_by_col{$col_i}
Aresult
11
00
7364 0 my $is_last = ($i == $dd_level - 1);
7365  
7366 # Back-link: keep ?dd=, cf for levels 0..i, dl=i+1.
7367 0 my $back_url = '';
7368 0 ▶ unless ($is_last) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_last) 0 0 0
7369 0 my $u = $self->req->url->clone;
7370 0 $u->query->remove('dl')->remove('cf')->remove('cv');
7371 0 $u->query->append('dl' => $i + 1);
7372 0 for my $j (0 .. $i) {
7373 0 my $val_j = $cf_by_col{ $levels[$j] };
7374 0 ▶ $u->query->append('cf' => "$levels[$j]:eq:$val_j")
Branch: 1 branch
BranchTrueFalseTotal
if defined $val_j 0 0 0
7375 if defined $val_j;
7376 }
7377 0 $back_url = $u->to_string;
7378 }
7379  
7380 0 ▶ push @drill_crumb, {
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 0 0 0
7381 label => $val_i,
7382 url => $back_url,
7383 cur => $is_last ? 1 : 0,
7384 };
7385 }
7386 }
7387  
7388 # Pre-build CSS grid position strings to avoid TT hash-key lookups, and
7389 # keep the two spans of a saved card so the page can print the card's size
7390 # on the card before the script has measured anything.
7391 17 my %layout_pos;
7392 my %layout_size;
7393 17 ▶ for my $lp (@{ $a->{layout} // [] }) {
Condition50%1/2
$a->{"layout"} // []
l!l
017
MC/DC0%0/1
$a->{"layout"} // []
$a->{"layout"}
Truth table50%1/2
A
A= $a->{"layout"}
Aresult
11
00
7394 0 my ($x, $y, $w, $h) = @{$lp}{qw(x y w h)};
7395 0 ▶ next unless defined $x && defined $y && defined $w && defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y and defined $w and defined $h 0 0 0
Condition0%0/9
defined $x and defined $y and defined $w and defined $h
!ll&&!rl&&r
000
defined $x and defined $y and defined $w
!ll&&!rl&&r
000
defined $x and defined $y
!ll&&!rl&&r
000
MC/DC0%0/4
defined $x and defined $y and defined $w and defined $h
defined $x defined $y defined $w defined $h
Truth table0%0/5
A and B and C and D
A= defined $xB= defined $yC= defined $wD= defined $h
ABCDresult
0XXX0
10XX0
110X0
11100
11111
7396 0 ▶ $layout_pos{ $lp->{visual} // '' } =
Condition0%0/2
$lp->{"visual"} // ""
l!l
00
MC/DC0%0/1
$lp->{"visual"} // ""
$lp->{"visual"}
Truth table0%0/2
A
A= $lp->{"visual"}
Aresult
11
00
7397 "grid-column:${x}/span ${w};grid-row:${y}/span ${h}";
7398 0 ▶ $layout_size{ $lp->{visual} // '' } = [$w, $h];
Condition0%0/2
$lp->{"visual"} // ""
l!l
00
MC/DC0%0/1
$lp->{"visual"} // ""
$lp->{"visual"}
Truth table0%0/2
A
A= $lp->{"visual"}
Aresult
11
00
7399 }
7400  
7401 17 my @rendered;
7402 17 my $canvas_map_head = '';
7403 17 for my $i (0 .. $#visuals) {
7404 19 my $vis = $visuals[$i];
7405  
7406 # Apply cross-filter to every visual except the source (self-highlighting).
7407 19 my $vis_recs = $records;
7408 19 my $late_cf = [];
7409 19 ▶ if ($cf_active && $i != $cf_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf_active and $i != $cf_source) 2 17 19
Condition66%2/3
$cf_active and $i != $cf_source
!ll&&!rl&&r
1702
MC/DC50%1/2
$cf_active and $i != $cf_source
$cf_active $i != $cf_source
Truth table66%2/3
A and B
A= $cf_activeB= $i != $cf_source
ABresult
0X0
100
111
7410 2 ($vis_recs, $late_cf) = $self->_canvas_cross_filter($records, $columns, \@cf_specs);
7411 }
7412  
7413 19 ▶ my $result = $self->_render_visual_html(
Branch: 1 branch
BranchTrueFalseTotal
$cf_active && $i != $cf_source ? : 2 17 19
Condition66%2/3
$cf_active and $i != $cf_source
!ll&&!rl&&r
1702
MC/DC50%1/2
$cf_active and $i != $cf_source
$cf_active $i != $cf_source
Truth table66%2/3
A and B
A= $cf_activeB= $i != $cf_source
ABresult
0X0
100
111
7414 $vis, $vis_recs, $columns, $src, $i, $canvas_spec_key,
7415 ($cf_active && $i != $cf_source ? 1 : 0),
7416 $cross_row,
7417 $late_cf,
7418 );
7419 19 ▶ $canvas_map_head ||= $result->{head} // '';
Condition40%2/5
$canvas_map_head ||= $result->{"head"} // ""
l!l&&r!l&&!r
0019
$result->{"head"} // ""
l!l
019
MC/DC0%0/2
$canvas_map_head ||= $result->{"head"} // ""
$canvas_map_head $result->{"head"}
Truth table33%1/3
A ||= B
A= $canvas_map_headB= $result->{"head"}
ABresult
1X1
011
000
7420 push @rendered, {
7421 id => $i,
7422 title => ($vis->{title} // $result->{title} // ('Visual ' . ($i + 1))),
7423 html => $result->{html} // '',
7424 error => $result->{error} // '',
7425 kind => $vis->{kind} // '',
7426 plain => _text_card_plain($vis),
7427 style => $layout_pos{$i} // '',
7428 # The saved spans, or nothing: a card with no saved position is
7429 # placed by the browser, and only the script can measure that.
7430 w => (defined $layout_size{$i} ? $layout_size{$i}[0] : undef),
7431 19 ▶ h => (defined $layout_size{$i} ? $layout_size{$i}[1] : undef),
Branch: 3 branches
BranchTrueFalseTotal
defined $layout_size{$i} ? : 0 19 19
defined $layout_size{$i} ? : 0 19 19
$i == $cf_source ? : 0 19 19
Condition93%14/15
($vis->{"title"} // $result->{"title"}) // 'Visual ' . ($i + 1)
l!l
910
$vis->{"title"} // $result->{"title"}
l!l&&r!l&&!r
1810
$result->{"html"} // ""
l!l
910
$result->{"error"} // ""
l!l
109
$vis->{"kind"} // ""
l!l
154
$layout_pos{$i} // ""
l!l
019
_vis_cf_col($vis) // ""
l!l
109
MC/DC100%2/2
($vis->{"title"} // $result->{"title"}) // 'Visual ' . ($i + 1)
$vis->{"title"} $result->{"title"}
MC/DC100%1/1
$result->{"html"} // ""
$result->{"html"}
MC/DC100%1/1
$result->{"error"} // ""
$result->{"error"}
MC/DC100%1/1
$vis->{"kind"} // ""
$vis->{"kind"}
MC/DC0%0/1
$layout_pos{$i} // ""
$layout_pos{$i}
MC/DC100%1/1
_vis_cf_col($vis) // ""
_vis_cf_col($vis)
Truth table100%3/3
A // B
A= $vis->{"title"}B= $result->{"title"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $result->{"html"}
Aresult
11
00
Truth table100%2/2
A
A= $result->{"error"}
Aresult
11
00
Truth table100%2/2
A
A= $vis->{"kind"}
Aresult
11
00
Truth table50%1/2
A
A= $layout_pos{$i}
Aresult
11
00
Truth table100%2/2
A
A= _vis_cf_col($vis)
Aresult
11
00
7432 cf_col => (_vis_cf_col($vis) // ''),
7433 cf_source => ($i == $cf_source ? 1 : 0),
7434 };
7435 }
7436  
7437 # bookmarks list for the toolbar panel.
7438 17 ▶ my @canvas_bookmarks = @{ $a->{bookmarks} // [] };
Condition50%1/2
$a->{"bookmarks"} // []
l!l
017
MC/DC0%0/1
$a->{"bookmarks"} // []
$a->{"bookmarks"}
Truth table50%1/2
A
A= $a->{"bookmarks"}
Aresult
11
00
7439 # Pre-encode as JSON for the JS canvasBookmarks variable.
7440 # Escape </ so the array literal cannot prematurely close a <script> tag.
7441 17 (my $bm_json = encode_json(\@canvas_bookmarks)) =~ s{</}{<\\/}g;
7442  
7443 my $canvas_columns_json = encode_json([
7444 17 ▶ map { { name => $_, numeric => _column_is_numeric($records, $_) ? 1 : 0 } }
Branch: 1 branch
BranchTrueFalseTotal
_column_is_numeric($records, $_) ? : 34 65 99
7445 @$columns
7446 ]);
7447 # The JSON is written into a <script> block. Escape '<' so a column
7448 # name containing markup cannot close the tag, as the chart spec is.
7449 17 $canvas_columns_json =~ s{<}{\\u003c}g;
7450  
7451 # The columns the page's own plan produces, for the panel that repairs a
7452 # card's: a card that takes the page's plan plots whatever that plan makes,
7453 # so the panel can only tell a reader what a change would cost it if it
7454 # knows the page's columns too.
7455 my $page_aliases = ($cross_row && ref $cross_row eq 'HASH')
7456 0 ▶ ? [ map { $_->{as} } @{ $cross_row->{specs} // [] } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
$cross_row && ref $cross_row eq 'HASH' ? : 0 17 17
Condition20%1/5
$cross_row and ref $cross_row eq "HASH"
!ll&&!rl&&r
1700
$cross_row->{"specs"} // []
l!l
00
MC/DC0%0/2
$cross_row and ref $cross_row eq "HASH"
$cross_row ref $cross_row eq "HASH"
MC/DC0%0/1
$cross_row->{"specs"} // []
$cross_row->{"specs"}
Truth table33%1/3
A and B
A= $cross_rowB= ref $cross_row eq "HASH"
ABresult
0X0
100
111
Truth table0%0/2
A
A= $cross_row->{"specs"}
Aresult
11
00
7457 17 my $canvas_page_aliases_json = encode_json($page_aliases);
7458 17 $canvas_page_aliases_json =~ s{<}{\\u003c}g;
7459  
7460 17 my ($platform, $language) = $self->_resolve_template;
7461 return $self->render(
7462 handler => 'tt',
7463 template => "$platform/$language/canvas",
7464 format => 'html',
7465 title => $a->{name},
7466 canvas_id => $id,
7467 canvas_name => $a->{name},
7468 # The other sites the Share panel's embed code works on.
7469 embed_origins => [ Database::BI::_embed_frame_ancestors($self->app->config) ],
7470 canvas_data_url => ($self->_analysis_to_url($a) // ''),
7471 canvas_columns_json => $canvas_columns_json,
7472 canvas_page_aliases_json => $canvas_page_aliases_json,
7473 # The same menu the table page's cross-row panel renders, so a card
7474 # can offer the functions the pass actually knows.
7475 cross_row_menu => \@CROSS_ROW_MENU,
7476 text_font_menu => \@TEXT_FONTS,
7477 text_min_points => $MIN_TEXT_POINTS,
7478 text_max_points => $MAX_TEXT_POINTS,
7479 text_max_chars => $MAX_TEXT_CHARS,
7480 visuals => \@rendered,
7481 canvas_params => \@canvas_params,
7482 canvas_bookmarks => \@canvas_bookmarks,
7483 canvas_bookmarks_json => $bm_json,
7484 canvas_time_range => $self->stash('time_range'),
7485 canvas_time_range_col => ($self->stash('time_range') ? $self->stash('time_range')->{col} : ''),
7486 17 ▶ canvas_time_range_clear_url => (do {
Branch: 1 branch
BranchTrueFalseTotal
$self->stash('time_range') ? : 0 17 17
Condition50%1/2
$self->_analysis_to_url($a) // ""
l!l
170
MC/DC0%0/1
$self->_analysis_to_url($a) // ""
$self->_analysis_to_url($a)
Truth table50%1/2
A
A= $self->_analysis_to_url($a)
Aresult
11
00
7487 17 my $u = $self->req->url->clone;
7488 17 $u->query->remove('range')->remove('rcol');
7489 17 ▶ $self->stash('time_range') ? $u->to_string : '';
Branch: 4 branches
BranchTrueFalseTotal
$self->stash('time_range') ? : 0 17 17
$cf_active ? : 1 16 17
$drill_active ? : 0 17 17
$drill_active ? : 0 17 17
7490 }),
7491 canvas_range_cols => [ @$columns ],
7492 canvas_range_hidden => \@canvas_range_hidden,
7493 cf_active => $cf_active ? 1 : 0,
7494 cf_display => $cf_display,
7495 cf_clear_url => $cf_clear_url,
7496 drillthrough_url => $drillthrough_url,
7497 drill_active => $drill_active ? 1 : 0,
7498 # A drill is a hierarchy that exists, not a name in the query string:
7499 # a bookmark can outlive the hierarchy it was saved on, and a page
7500 # that said it was drilled when nothing answered would stop making
7501 # its marks controls and have no column to name in #cf-help.
7502 drill_name => ($drill_active ? $dd_name : ''),
7503 drill_level => $dd_level,
7504 drill_col => $drill_col,
7505 drill_crumb => \@drill_crumb,
7506 drill_max => $drill_max,
7507 extra_head => $canvas_map_head,
7508 back_url => '/',
7509 back_label => 'Home',
7510 freshness_stale => $freshness_stale,
7511 freshness_msg => $freshness_msg,
7512 freshness_source_mtime => $freshness_source_mtime,
7513 language => $language,
7514 );
7515 }
7516  
7517 # no visuals yet -- reconstruct the data-view URL and redirect.
7518 6 my $url = $self->_analysis_to_url($a);
7519 6 ▶ return $self->render(text => 'Cannot reconstruct URL from this analysis', status => 422)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 3 3 6
7520 unless defined $url;
7521  
7522 3 $self->redirect_to($url);
7523 }
7524  
7525 # _run_export_pipeline($self) -> ($records, \@columns, $left_label) or ()
7526 #
7527 # Purpose: Thin wrapper over _run_pipeline_source that additionally applies
7528 # f= filters, d=1 dedup, and page/per_page pagination. Used by
7529 # export_data, export_write, folder_view, and import_url (row-viewing
7530 # routes that do not need the source object for aggregation).
7531 # Entry: Reads "l=", "j=" (repeatable), "c=" (repeatable), "f=" (repeatable),
7532 # "d=", "page=", "per_page=" query/body params.
7533 # Exit: On success: ($filtered_arrayref, \@column_names, $left_label_string).
7534 # On failure: empty list (left table not found / fetch error).
7535 145 ▶sub _run_export_pipeline :Protected ($self) {
Subroutine _run_export_pipeline: called CC=16 SCAR=28.1
7536 145 my (undef, $records, $columns, $left_label) = $self->_run_pipeline_source;
7537 145 ▶ return () unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 10 135 145
7538  
7539 # The export honours span= like the page it was linked from, and accepts the
7540 # dimension params of the aggregating routes so a chart's own URL can be
7541 # pasted into /export and mean the same thing.
7542 135 my $span = $self->_span_calc_spec($columns, 'cat', 'x', 'rows', 'group');
7543 # The stash says the reply has gone. Without it the caller takes the empty list
7544 # for "no such table" and renders a second time, and the client is left hanging
7545 # on a reply that never finishes.
7546 135 ▶ if ($span && $span->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span and $span->{"error"}) 10 125 135
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
125010
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
7547 10 $self->stash('_reduce_error' => 1);
7548 10 $self->render(text => $span->{error}, status => 400);
7549 10 return ();
7550 }
7551  
7552 125 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
7553 125 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 125 125
7554 125 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 5 120 125
7555 5 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
7556 5 $self->stash('_reduce_error' => 1);
7557 5 $self->render(text => $e, status => 400);
7558 5 return ();
7559 }
7560  
7561 120 ▶ return () unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 119 120
7562  
7563 119 ▶ my @columns = @{ $columns // [] };
Condition50%1/2
$columns // []
l!l
1190
MC/DC0%0/1
$columns // []
$columns
Truth table50%1/2
A
A= $columns
Aresult
11
00
7564  
7565 # d=1 has been applied by _reduce_or_render, before the cross-row pass.
7566  
7567 # Sort support for /export: applied in memory after filtering/dedup.
7568 119 my $sort = $self->param('sort');
7569 119 ▶ my $dir = ($self->param('dir') // 'asc') =~ /\Adesc\z/i ? 'desc' : 'asc';
Branch: 1 branch
BranchTrueFalseTotal
($self->param('dir') // 'asc') =~ /\Adesc\z/iu ? : 0 119 119
Condition100%2/2
$self->param("dir") // "asc"
l!l
12107
MC/DC100%1/1
$self->param("dir") // "asc"
$self->param("dir")
Truth table100%2/2
A
A= $self->param("dir")
Aresult
11
00
7570 119 ▶ if (defined $sort && grep { $_ eq $sort } @columns) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $sort and grep {$_ eq $sort;} @columns) { } 0 119 119
elsif (defined $sort) { } 2 117 119
Condition66%2/3
defined $sort and grep {$_ eq $sort;} @columns
!ll&&!rl&&r
11720
MC/DC0%0/2
defined $sort and grep {$_ eq $sort;} @columns
defined $sort grep {$_ eq $sort;} @columns
Truth table66%2/3
A and B
A= defined $sortB= grep {$_ eq $sort;} @columns
ABresult
0X0
100
111
7571 # By the same rule as the table pages (_sort_records returns a new
7572 # array: the backend's own can be read-only, as a SQLite one is).
7573 0 $records = _sort_records($records, $sort, $dir);
7574 0 $self->stash(sorted_by => { col => $sort, dir => $dir });
7575 } elsif (defined $sort) {
7576 2 carp $self->_i18n('error_sort_column', $sort);
7577 }
7578  
7579 # Server-side pagination for /export.
7580 119 ▶ if (defined $self->param('page') || defined $self->param('per_page')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->param("page") or defined $self->param("per_page")) 4 115 119
Condition100%3/3
defined $self->param("page") or defined $self->param("per_page")
l!l&&r!l&&!r
22115
MC/DC100%2/2
defined $self->param("page") or defined $self->param("per_page")
defined $self->param("page") defined $self->param("per_page")
Truth table100%3/3
A or B
A= defined $self->param("page")B= defined $self->param("per_page")
ABresult
1X1
011
000
7581 4 my ($page, $per_page, $offset) = $self->_pagination;
7582 4 my $total = scalar @$records;
7583 4 ▶ my $has_next = ($total > $offset + $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$total > $offset + $per_page ? : 0 4 4
7584 4 my @page_recs = @{ _rows_of_page($records, $offset, $per_page) };
7585 4 $records = \@page_recs;
7586 4 $self->stash(export_has_next => $has_next, export_page => $page, export_per_page => $per_page);
7587 }
7588  
7589 119 return ($records, \@columns, $left_label);
7590 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7591  
7592 # _decode_cell($s) -> $character_string
7593 #
7594 # Purpose: Ensure $s is a Perl character string, not raw UTF-8 bytes.
7595 # URL-fetched data (via LWP::UserAgent::Cached + D::A HTML parsing)
7596 # arrives as byte strings without the UTF-8 flag set. encode_json
7597 # then treats each byte as a Latin-1 code point and re-encodes it,
7598 # producing mojibake (e.g. an o-umlaut character displayed as two garbage bytes). This helper
7599 # upgrades the flag in-place on a copy so the original hash key is
7600 # unchanged but the value passed to HTML::D3 is a proper character.
7601 # Entry: $s -- any defined scalar (undef returned as-is).
7602 # Exit: The same string value, but with the UTF-8 flag set if it was absent.
7603 4011 ▶sub _decode_cell {
Subroutine _decode_cell: called CC=3 SCAR=11.0
7604 4011 my $s = shift;
7605 4011 ▶ return $s unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 4010 4011
7606 4010 ▶ utf8::decode($s) unless utf8::is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($s) 4009 1 4010
7607 4010 $s;
7608 }
7609  
7610 # _serialize_csv($records, \@columns) -> $utf8_string
7611 #
7612 # Purpose: Build a complete RFC 4180 CSV document (header + data rows) from
7613 # $records. Shared by _render_csv (HTTP download) and the csv branch
7614 # of export_write (filesystem write) to eliminate duplicated logic.
7615 # Entry: $records is an arrayref of hashrefs; $columns is an arrayref of names.
7616 # Exit: Returns a UTF-8 Perl string ending with CRLF.
7617 #
7618 # Optimisation: rows are collected in @lines and joined in one operation.
7619 # The previous pattern ($out .= _csv_row(...) per row) triggers O(N) string
7620 # reallocations; joining a pre-built array is a single O(total_bytes) allocation.
7621 # For exports of thousands of rows the difference is measurable.
7622 #
7623 # XS note: for very large exports, replacing _csv_row with Text::CSV_XS
7624 # (available from CPAN) would further reduce serialisation time -- its C-level
7625 # implementation is roughly 5-10x faster than the pure-Perl version here.
7626 80 ▶sub _serialize_csv {
Subroutine _serialize_csv: called CC=2 SCAR=6.9
7627 80 my ($records, $columns) = @_;
7628 80 my @lines = _csv_row(@$columns);
7629 80 for my $row (@$records) {
7630 436 push @lines, _csv_row(map { $row->{$_} } @$columns);
7631 }
7632 80 return encode('UTF-8', join('', @lines));
7633 }
7634  
7635 # _render_csv($self, $records, \@columns, $name) -> void (renders response)
7636 #
7637 # Purpose: Serialise $records to RFC 4180 CSV and emit as a UTF-8 download.
7638 # Entry: $name is a safe filename stem (alphanumeric/underscore).
7639 # Exit: Renders the Mojolicious response; does not return a meaningful value.
7640 # Side Effects: Writes HTTP response headers and body.
7641 58 ▶sub _render_csv :Protected ($self, $records, $columns, $name) {
Subroutine _render_csv: called CC=1 SCAR=0.0
7642 58 $self->stash(buffer => 0);
7643 58 $self->res->headers->content_type('text/csv; charset=UTF-8');
7644 58 $self->res->headers->content_disposition(qq{attachment; filename="${name}.csv"});
7645 58 $self->render(data => _serialize_csv($records, $columns));
7646 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7647  
7648 # _render_sqlite($self, $records, \@columns, $name) -> void (renders response)
7649 #
7650 # Purpose: Write $records to a SQLite database and emit it as a binary download.
7651 # Entry: $name is a safe filename stem.
7652 # Exit: Renders the Mojolicious response.
7653 # Side Effects: Creates and unlinks a temporary file; writes HTTP response.
7654 5 ▶sub _render_sqlite :Protected ($self, $records, $columns, $name) {
Subroutine _render_sqlite: called CC=2 SCAR=6.9
7655 5 $self->stash(buffer => 0);
7656 5 my $data = eval { $self->_write_sqlite_db($records, $columns) };
7657 5 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 4 5
7658 1 $self->app->log->error('SQLite export failed: ' . _tidy_error($@));
7659 1 return $self->render(text => $self->_i18n('error_export_sqlite'), status => 500);
7660 }
7661 4 $self->res->headers->content_type('application/vnd.sqlite3');
7662 4 $self->res->headers->content_disposition(qq{attachment; filename="${name}.db"});
7663 4 $self->render(data => $data);
7664 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7665  
7666 # _render_json($self, $records, \@columns, $name) -> void (renders response)
7667 #
7668 # Purpose: Emit $records as a JSON array download.
7669 # Entry: $records is an arrayref of hashrefs; $name is a safe filename stem.
7670 # Exit: Renders the Mojolicious response.
7671 # Side Effects: Writes HTTP response headers and body.
7672 4 ▶sub _render_json :Protected ($self, $records, $columns, $name) {
Subroutine _render_json: called CC=1 SCAR=0.0
7673 4 $self->stash(buffer => 0);
7674 4 $self->res->headers->content_disposition(qq{attachment; filename="${name}.json"});
7675 4 $self->render(json => $records);
7676 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
7677  
7678 # ---------------------------------------------------------------------------
7679 # Public actions
7680 # ---------------------------------------------------------------------------
7681  
7682 =encoding utf8
7683  
7684 =head1 ACTIONS
7685  
7686 =head2 index
7687  
7688 C<GET /> -- Scan C<data_dir> and present a card grid of available tables.
7689  
7690 =head3 API SPECIFICATION
7691  
7692 =head4 INPUT
7693  
7694 None (reads C<data_dir> from application config, and C<BI_OPEN> and the
7695 C<Referer> header).
7696  
7697 =head4 OUTPUT
7698  
7699 When C<BI_OPEN> is set and the request has no C<Referer> from this server,
7700 redirects to C</open> for that path. Otherwise renders C<home.html.tt> with:
7701  
7702 tables ARRAYREF of { name => $stem, file => $basename }
7703 title 'Choose a Database'
7704  
7705 =head3 MESSAGES
7706  
7707 None produced by this action; any file-scan errors are silently ignored
7708 (empty directory yields an empty card grid).
7709  
7710 =head3 FORMAL SPECIFICATION
7711  
7712 index == lambda self .
7713 let dir = resolve self.app.config.data_dir in
7714 let tbls = { b | b in dir /\ basename(b) =~ EXT_RE } in
7715 post render(home, tables: map(stem, tbls))
7716  
7717 =head3 EXAMPLE
7718  
7719 GET / -> 200 text/html containing a card for each file in data/
7720  
7721 With BI_OPEN=/data/store.db and no Referer from this server:
7722 GET / -> 302 /open?path=%2Fdata%2Fstore.db
7723  
7724 =cut
7725  
7726 58 ▶sub index ($self) {
Subroutine index: called CC=6 SCAR=29.7
Pod documented
7727 # A database named when the server was started is what a reader arriving
7728 # from outside sees first. A Home link within the application carries this
7729 # server as its Referer and gets the list, so that stays reachable. The
7730 # check needs no state, so it survives morbo restarting the application.
7731 58 ▶ if (defined $ENV{BI_OPEN} && length $ENV{BI_OPEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"BI_OPEN"} and length $ENV{"BI_OPEN"}) 0 58 58
Condition33%1/3
defined $ENV{"BI_OPEN"} and length $ENV{"BI_OPEN"}
!ll&&!rl&&r
5800
MC/DC0%0/2
defined $ENV{"BI_OPEN"} and length $ENV{"BI_OPEN"}
defined $ENV{"BI_OPEN"} length $ENV{"BI_OPEN"}
Truth table33%1/3
A and B
A= defined $ENV{"BI_OPEN"}B= length $ENV{"BI_OPEN"}
ABresult
0X0
100
111
7732 0 my $from = $self->req->headers->referrer;
7733 0 my $here = $self->req->url->to_abs->host_port;
7734 0 ▶ return $self->redirect_to('/open?path=' . url_escape($ENV{BI_OPEN}))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and ("Mojo::URL"->new($from)->host_port // "") eq $here 0 0 0
Condition0%0/5
defined $from and ("Mojo::URL"->new($from)->host_port // "") eq $here
!ll&&!rl&&r
000
"Mojo::URL"->new($from)->host_port // ""
l!l
00
MC/DC0%0/2
defined $from and ("Mojo::URL"->new($from)->host_port // "") eq $here
defined $from "Mojo::URL"->new($from)->host_port
Truth table0%0/3
A and B
A= defined $fromB= "Mojo::URL"->new($from)->host_port
ABresult
0X0
100
111
7735 unless defined $from && (Mojo::URL->new($from)->host_port // '') eq $here;
7736 }
7737  
7738 58 my ($platform, $language) = $self->_resolve_template;
7739  
7740 # The layout writes the resolved language into <html lang>, so every action
7741 # that renders a template from $platform/$language hands it over; t/html-lang.t
7742 # holds the controller to that. A screen reader, a translation tool and
7743 # anything that indexes the page read the attribute.
7744 58 $self->render(
7745 template => "$platform/$language/home",
7746 handler => 'tt',
7747 format => 'html',
7748 tables => $self->_scan_data_dir,
7749 title => 'Choose a Database',
7750 language => $language,
7751 );
7752 }
7753  
7754 =head2 is_supported_file
7755  
7756 Say whether a file's name has an extension C</open> will read. C<script/database-bi>
7757 asks this to refuse a file before the server starts, rather than show a 404.
7758  
7759 =head3 API SPECIFICATION
7760  
7761 =head4 INPUT
7762  
7763 $path string, a file name or path; undef is allowed
7764  
7765 =head4 OUTPUT
7766  
7767 1 when the name ends in a supported extension (any case), otherwise 0
7768  
7769 =head3 MESSAGES
7770  
7771 None.
7772  
7773 =head3 FORMAL SPECIFICATION
7774  
7775 is_supported_file == lambda path . path =~ EXT_RE
7776  
7777 =head3 EXAMPLE
7778  
7779 is_supported_file('data/store.db') # 1
7780 is_supported_file('LICENSE') # 0
7781  
7782 =cut
7783  
7784 19 ▶sub is_supported_file ($path) {
Subroutine is_supported_file: called CC=3 SCAR=11.0
Pod documented
7785 19 ▶ return (defined $path && $path =~ $EXT_RE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $path && $path =~ /$EXT_RE/u ? : 11 8 19
Condition100%3/3
defined $path and $path =~ /$EXT_RE/u
!ll&&!rl&&r
1711
MC/DC100%2/2
defined $path and $path =~ /$EXT_RE/u
defined $path $path =~ /$EXT_RE/u
Truth table100%3/3
A and B
A= defined $pathB= $path =~ /$EXT_RE/u
ABresult
0X0
100
111
7786 }
7787  
7788 =head2 require_login / login_form / login / logout
7789  
7790 The login, on only when config C<password_hash> is set (see
7791 L<Database::BI/Logging in>). C<require_login> is the C<under> every route but
7792 C</login>, C</logout>, C</share/:id> and C</embed/:id> passes through; the
7793 others are C<GET /login>, C<POST /login> and C<POST /logout>.
7794  
7795 A session is logged in when its C<login> value is this password's mark (an
7796 HMAC of the hash, keyed by the first secret), so a new password logs every
7797 session out. Wrong passwords are counted per remote address, in each server
7798 process: the tenth within 15 minutes locks that address out for the rest of
7799 the 15 minutes (C<429>), whether or not the next one is right.
7800  
7801 =head3 API SPECIFICATION
7802  
7803 =head4 INPUT
7804  
7805 require_login: any request
7806 GET /login: return= string, optional; a path on this site (default /)
7807 POST /login: password= string; return= as above
7808 POST /logout: none
7809  
7810 =head4 OUTPUT
7811  
7812 require_login: true when login is off or the session is logged in. Otherwise
7813 a browser's GET for a page is redirected (302) to
7814 /login?return=<the page>, and anything else gets
7815 401 application/json { "error": error_login_required }.
7816 GET /login: 200 text/html, the form; 302 to return= when logged in; with
7817 login off, 200 and a page that says no password is set.
7818 POST /login: 302 to return= when the password is right; 401 text/html, the
7819 form with error_login_failed, when not; 429 with
7820 error_login_locked after too many.
7821 POST /logout: 302 to /login (to / when login is off), the session ended.
7822  
7823 return= is followed only when it is a path on this site (one /, not // or /\);
7824 anything else goes to /.
7825  
7826 =cut
7827  
7828 # Wrong passwords per application (a test runs several) and remote address:
7829 # epoch seconds of each, newest last.
7830 5 my %LOGIN_FAILURES;
7831 5 Readonly my $LOGIN_WINDOW => 15 * 60;
7832 5 Readonly my $LOGIN_ATTEMPTS => 10;
7833  
7834 # _login_mark($self, $hash) -> $mark
7835 #
7836 # What a logged-in session holds: not the hash, which the readable cookie
7837 # would hand to anyone who saw it, but an HMAC of it keyed by the secret.
7838 0 ▶sub _login_mark ($self, $hash) {
Subroutine _login_mark: not called CC=1 SCAR=6.9
7839 0 return Digest::SHA::hmac_sha256_hex($hash, $self->app->secrets->[0]);
7840 }
7841  
7842 # _login_return($value) -> $path
7843 #
7844 # Where to go after logging in: a path on this site, never another site
7845 # (//evil.example and /\evil.example are other sites to a browser).
7846 0 ▶sub _login_return {
Subroutine _login_return: not called CC=5 SCAR=34.0
7847 0 my ($value) = @_;
7848 0 ▶ return '/' unless defined $value && $value =~ m{\A/(?![/\\])} && $value !~ /[\x00-\x20\x7f\\]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ m(\A/(?![/\\]))u and not $value =~ /[\x00-\x20\x7f\\]/u 0 0 0
Condition0%0/6
defined $value and $value =~ m(\A/(?![/\\]))u and not $value =~ /[\x00-\x20\x7f\\]/u
!ll&&!rl&&r
000
defined $value and $value =~ m(\A/(?![/\\]))u
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value and $value =~ m(\A/(?![/\\]))u and not $value =~ /[\x00-\x20\x7f\\]/u
defined $value $value =~ m(\A/(?![/\\]))u not $value =~ /[\x00-\x20\x7f\\]/u
Truth table0%0/4
A and B and not(C)
A= defined $valueB= $value =~ m(\A/(?![/\\]))uC= not $value =~ /[\x00-\x20\x7f\\]/u
ABCresult
0XX0
10X0
1100
1111
7849 0 ▶ return '/' if $value =~ m{\A/log(?:in|out)\b};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\A/log(?:in|out)\b]u 0 0 0
7850 0 return $value;
7851 }
7852  
7853 # _login_hash($self) -> $hash | undef
7854 1921 ▶sub _login_hash ($self) {
Subroutine _login_hash: called CC=3 SCAR=13.1
7855 1921 my $hash = $self->app->config('password_hash');
7856 1921 ▶ return (defined $hash && length $hash) ? $hash : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash && length $hash ? : 0 1921 1921
Condition33%1/3
defined $hash and length $hash
!ll&&!rl&&r
192100
MC/DC0%0/2
defined $hash and length $hash
defined $hash length $hash
Truth table33%1/3
A and B
A= defined $hashB= length $hash
ABresult
0X0
100
111
7857 }
7858  
7859 0 ▶sub _logged_in ($self) {
Subroutine _logged_in: not called CC=4 SCAR=30.0
7860 0 ▶ my $hash = $self->_login_hash // return 1;
Condition0%0/2
$self->_login_hash // (return 1)
l!l
00
MC/DC0%0/1
$self->_login_hash // (return 1)
$self->_login_hash
Truth table0%0/2
A
A= $self->_login_hash
Aresult
11
00
7861 0 ▶ return secure_compare($self->session('login') // '', $self->_login_mark($hash)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
secure_compare($self->session('login') // '', $self->_login_mark($hash)) ? : 0 0 0
Condition0%0/2
$self->session("login") // ""
l!l
00
MC/DC0%0/1
$self->session("login") // ""
$self->session("login")
Truth table0%0/2
A
A= $self->session("login")
Aresult
11
00
7862 }
7863  
7864 1921 ▶sub require_login ($self) {
Subroutine require_login: called CC=7 SCAR=38.2
Pod documented
7865 1921 ▶ return 1 unless defined $self->_login_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_login_hash 1921 0 1921
7866 0 ▶ if ($self->_logged_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_logged_in) 0 0 0
7867 0 $self->stash(logged_in => 1);
7868 0 return 1;
7869 }
7870  
7871 0 my $req = $self->req;
7872 0 $self->stash(buffer => 0);
7873 0 ▶ if ($req->method =~ /\A(?:GET|HEAD)\z/ && ($req->headers->accept // '') =~ m{text/html}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method =~ /\A(?:GET|HEAD)\z/u and ($req->headers->accept // "") =~ m[text/html]u and not $req->url->path->to_string =~ m[\A/api/]u) 0 0 0
Condition0%0/8
$req->method =~ /\A(?:GET|HEAD)\z/u and ($req->headers->accept // "") =~ m[text/html]u and not $req->url->path->to_string =~ m[\A/api/]u
!ll&&!rl&&r
000
$req->method =~ /\A(?:GET|HEAD)\z/u and ($req->headers->accept // "") =~ m[text/html]u
!ll&&!rl&&r
000
$req->headers->accept // ""
l!l
00
MC/DC0%0/3
$req->method =~ /\A(?:GET|HEAD)\z/u and ($req->headers->accept // "") =~ m[text/html]u and not $req->url->path->to_string =~ m[\A/api/]u
$req->method =~ /\A(?:GET|HEAD)\z/u $req->headers->accept not $req->url->path->to_string =~ m[\A/api/]u
Truth table0%0/4
A and B and not(C)
A= $req->method =~ /\A(?:GET|HEAD)\z/uB= $req->headers->acceptC= not $req->url->path->to_string =~ m[\A/api/]u
ABCresult
0XX0
10X0
1100
1111
7874 && $req->url->path->to_string !~ m{\A/api/}) {
7875 0 my $url = Mojo::URL->new('/login')->query(return => $req->url->path_query);
7876 0 $self->redirect_to($url);
7877 0 return undef;
7878 }
7879 0 $self->render(json => { error => _message_text('error_login_required') }, status => 401);
7880 0 return undef;
7881 }
7882  
7883 0 ▶sub _render_login ($self, %args) {
Subroutine _render_login: not called CC=4 SCAR=30.0
7884 0 my ($platform, $language) = $self->_resolve_template;
7885 0 $self->stash(buffer => 0);
7886 return $self->render(
7887 template => "$platform/$language/login",
7888 handler => 'tt',
7889 format => 'html',
7890 title => 'Log in',
7891 language => $language,
7892 return_to => _login_return(scalar $self->param('return')),
7893 login_error => $args{error} // '',
7894 login_off => $args{login_off} ? 1 : 0,
7895 0 ▶ status => $args{status} // 200,
Branch: 1 branch
BranchTrueFalseTotal
$args{'login_off'} ? : 0 0 0
Condition0%0/4
$args{"error"} // ""
l!l
00
$args{"status"} // 200
l!l
00
MC/DC0%0/1
$args{"error"} // ""
$args{"error"}
MC/DC0%0/1
$args{"status"} // 200
$args{"status"}
Truth table0%0/2
A
A= $args{"error"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"status"}
Aresult
11
00
7896 );
7897 }
7898  
7899 0 ▶sub login_form ($self) {
Subroutine login_form: not called CC=3 SCAR=24.8
Pod documented
7900 # With no password set the page says so, rather than offering a form that
7901 # could never succeed.
7902 0 ▶ return $self->_render_login(login_off => 1) unless defined $self->_login_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_login_hash 0 0 0
7903 0 ▶ return $self->redirect_to(_login_return(scalar $self->param('return'))) if $self->_logged_in;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_logged_in 0 0 0
7904 0 return $self->_render_login;
7905 }
7906  
7907 0 ▶sub login ($self) {
Subroutine login: not called CC=12 SCAR=50.5
Pod documented
7908 0 my $hash = $self->_login_hash;
7909 0 ▶ return $self->redirect_to('/') unless defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 0 0
7910  
7911 0 ▶ my $who = $self->tx->remote_address // '';
Condition0%0/2
$self->tx->remote_address // ""
l!l
00
MC/DC0%0/1
$self->tx->remote_address // ""
$self->tx->remote_address
Truth table0%0/2
A
A= $self->tx->remote_address
Aresult
11
00
7912 0 my $now = time;
7913 0 ▶ my $failures = $LOGIN_FAILURES{ Scalar::Util::refaddr($self->app) } //= {};
Condition0%0/2
$LOGIN_FAILURES{builtin::refaddr($self->app)} //= {}
l!l
00
MC/DC0%0/1
$LOGIN_FAILURES{builtin::refaddr($self->app)} //= {}
$LOGIN_FAILURES{builtin::refaddr($self->app)}
Truth table0%0/2
A
A= $LOGIN_FAILURES{builtin::refaddr($self->app)}
Aresult
11
00
7914 0 for my $addr (keys %{$failures}) {
7915 0 $failures->{$addr} = [ grep { $_ > $now - $LOGIN_WINDOW } @{ $failures->{$addr} } ];
7916 0 ▶ delete $failures->{$addr} unless @{ $failures->{$addr} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$failures->{$addr};} 0 0 0
7917 }
7918 0 ▶ my $seen = $failures->{$who} // [];
Condition0%0/2
$failures->{$who} // []
l!l
00
MC/DC0%0/1
$failures->{$who} // []
$failures->{$who}
Truth table0%0/2
A
A= $failures->{$who}
Aresult
11
00
7919 0 ▶ if (@{$seen} >= $LOGIN_ATTEMPTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$seen;} >= $LOGIN_ATTEMPTS) 0 0 0
7920 0 ▶ my $minutes = int(($seen->[0] + $LOGIN_WINDOW - $now + 59) / 60) || 1;
Condition0%0/2
int(($seen->[0] + $LOGIN_WINDOW - $now + 59) / 60) || 1
l!l
00
MC/DC0%0/1
int(($seen->[0] + $LOGIN_WINDOW - $now + 59) / 60) || 1
int(($seen->[0] + $LOGIN_WINDOW - $now + 59) / 60)
Truth table0%0/2
A
A= int(($seen->[0] + $LOGIN_WINDOW - $now + 59) / 60)
Aresult
11
00
7921 0 return $self->_render_login(error => sprintf(_message_text('error_login_locked'), $minutes), status => 429);
7922 }
7923  
7924 0 ▶ unless (Database::BI::Password::verify_password(scalar($self->param('password')) // '', $hash)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Database::BI::Password::verify_password(scalar $self->param("password") // "", $hash)) 0 0 0
Condition0%0/2
scalar $self->param("password") // ""
l!l
00
MC/DC0%0/1
scalar $self->param("password") // ""
scalar $self->param("password")
Truth table0%0/2
A
A= scalar $self->param("password")
Aresult
11
00
7925 0 ▶ push @{ $failures->{$who} //= [] }, $now;
Condition0%0/2
$failures->{$who} //= []
l!l
00
MC/DC0%0/1
$failures->{$who} //= []
$failures->{$who}
Truth table0%0/2
A
A= $failures->{$who}
Aresult
11
00
7926 0 $self->app->log->warn("Wrong password from $who");
7927 0 return $self->_render_login(error => _message_text('error_login_failed'), status => 401);
7928 }
7929  
7930 0 delete $failures->{$who};
7931 0 $self->session(login => $self->_login_mark($hash));
7932 0 return $self->redirect_to(_login_return(scalar $self->param('return')));
7933 }
7934  
7935 0 ▶sub logout ($self) {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
7936 0 $self->session(expires => 1);
7937 0 ▶ return $self->redirect_to(defined $self->_login_hash ? '/login' : '/');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_login_hash ? : 0 0 0
7938 }
7939  
7940 =head2 preferences
7941  
7942 C<GET /preferences> -- Render the page where a reader sets their preferences:
7943 the light or dark theme, and how many rows a table shows on a page.
7944  
7945 Preferences belong to the browser, not to the server, so this action only
7946 renders the form. The theme is kept in C<localStorage>; the script that
7947 stores and applies it is in C<layouts/theme.html.tt>, which every page loads.
7948 The page size is kept in the cookie C<bi_per_page> (25, 50, 100, 200, or 0 for
7949 every row), because the server needs it before it reads the page; this action
7950 reads it to show the current choice.
7951  
7952 =head3 API SPECIFICATION
7953  
7954 =head4 INPUT
7955  
7956 cookie bi_per_page optional; the saved page size, shown as chosen
7957  
7958 =head4 OUTPUT
7959  
7960 200 text/html The preferences page.
7961  
7962 =head3 FORMAL SPECIFICATION
7963  
7964 preferences
7965 response! : HTTP
7966 ──────────
7967 response!.status = 200
7968  
7969 =cut
7970  
7971 1 ▶sub preferences ($self) {
Subroutine preferences: called CC=2 SCAR=8.1
Pod documented
7972 1 my ($platform, $language) = $self->_resolve_template;
7973  
7974 1 ▶ $self->render(
Condition33%1/3
_page_size_preference($self) // $DEFAULT_PER_PAGE
l!l&&r!l&&!r
010
MC/DC0%0/2
_page_size_preference($self) // $DEFAULT_PER_PAGE
_page_size_preference($self) $DEFAULT_PER_PAGE
Truth table33%1/3
A // B
A= _page_size_preference($self)B= $DEFAULT_PER_PAGE
ABresult
1X1
011
000
7975 template => "$platform/$language/preferences",
7976 handler => 'tt',
7977 format => 'html',
7978 title => 'Preferences',
7979 language => $language,
7980 page_size => _page_size_preference($self) // $DEFAULT_PER_PAGE,
7981 page_size_choices => [ @PAGE_SIZES ],
7982 page_size_cookie => $PAGE_SIZE_COOKIE,
7983 );
7984 }
7985  
7986 =head2 tutorial
7987  
7988 C<GET /tutorial> -- Render the tutorial that is part of the application.
7989  
7990 The chapters, questions and glossary are data in L<Database::BI::Tutorial>.
7991 They are not in this module's POD, so adding a feature means adding a chapter
7992 there and no change to this action or to the template.
7993  
7994 =head3 API SPECIFICATION
7995  
7996 =head4 INPUT
7997  
7998 None.
7999  
8000 =head4 OUTPUT
8001  
8002 200 text/html The tutorial page. It is cached when page_cache_dir is set.
8003  
8004 =head3 FORMAL SPECIFICATION
8005  
8006 tutorial
8007 response! : HTTP
8008 ──────────
8009 response!.status = 200
8010  
8011 =cut
8012  
8013 3 ▶sub tutorial ($self) {
Subroutine tutorial: called CC=2 SCAR=7.1
Pod documented
8014 3 ▶ return if $self->buffer_is_cached;
Branch: 1 branch
BranchTrueFalseTotal
if $self->buffer_is_cached 0 3 3
8015 3 my ($platform, $language) = $self->_resolve_template;
8016  
8017 3 $self->render(
8018 template => "$platform/$language/tutorial",
8019 handler => 'tt',
8020 format => 'html',
8021 title => 'Tutorial',
8022 language => $language,
8023 intro => Database::BI::Tutorial->intro,
8024 parts => Database::BI::Tutorial->parts,
8025 faq => Database::BI::Tutorial->faq,
8026 glossary => Database::BI::Tutorial->glossary,
8027 version => $self->app->VERSION,
8028 );
8029 }
8030  
8031 =head2 view
8032  
8033 C<GET /view/:table> -- Open and display the chosen table from C<data_dir>.
8034  
8035 =head3 API SPECIFICATION
8036  
8037 =head4 INPUT
8038  
8039 :table string Table name (alphanumeric + underscore). Returns 404 on
8040 other characters.
8041 ?f= string (repeatable) Filter spec: "col:op:val".
8042  
8043 =head4 DOMAIN CONSTRAINTS: :table
8044  
8045 C<:table> is validated against C<\A[A-Za-z_][A-Za-z0-9_]*\z> before C<open_table>
8046 is ever called. The first character must be a letter (A-Z, a-z) or an
8047 underscore; subsequent characters may also include digits (0-9).
8048  
8049 =over 4
8050  
8051 =item Valid partition
8052  
8053 C<sales> (letters only), C<_temp> (underscore-start), C<report_2024>
8054 (mixed). A name that is valid but has no backing file returns 200 with
8055 an error message -- NOT 404.
8056  
8057 =item Invalid partition
8058  
8059 C<1sales> (digit-start, 404), C<my.data> (dot, 404), C<my-data>
8060 (hyphen, 404), non-ASCII characters (404).
8061  
8062 =item Boundary values
8063  
8064 C<a> (single letter, valid), C<_> (single underscore, valid), C<1>
8065 (single digit, 404), C<a1> (letter then digit, valid), C<1a> (digit
8066 then letter, 404).
8067  
8068 =back
8069  
8070 =head4 OUTPUT
8071  
8072 On success renders C<dashboard.html.tt> with table data.
8073 On error re-renders C<home.html.tt> with an C<error> stash variable.
8074  
8075 =head3 MESSAGES
8076  
8077 error_table_open -- DataSource initialisation or fetch_all threw.
8078  
8079 =head3 FORMAL SPECIFICATION
8080  
8081 view == lambda self .
8082 pre self.stash('table') =~ TABLE_NAME_RE
8083 let src = open_table(table) in
8084 let records = src.fetch_all() in
8085 let cols = get_columns(src, records) in
8086 post render(dashboard, records: filter(records, f_params))
8087  
8088 =head3 EXAMPLE
8089  
8090 GET /view/sales -> 200 HTML table of all sales rows
8091 GET /view/sales?f=region:eq:North -> shows only North region rows
8092 GET /view/../etc -> 404
8093  
8094 =cut
8095  
8096 80 ▶sub view ($self) {
Subroutine view: called CC=16 SCAR=33.3
Pod documented
8097 80 my ($platform, $language) = $self->_resolve_template;
8098 80 my $table = $self->stash('table');
8099  
8100 80 ▶ unless (defined $table && $table =~ $TABLE_NAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table and $table =~ /$TABLE_NAME_RE/u) 0 80 80
Condition33%1/3
defined $table and $table =~ /$TABLE_NAME_RE/u
!ll&&!rl&&r
0080
MC/DC0%0/2
defined $table and $table =~ /$TABLE_NAME_RE/u
defined $table $table =~ /$TABLE_NAME_RE/u
Truth table33%1/3
A and B
A= defined $tableB= $table =~ /$TABLE_NAME_RE/u
ABresult
0X0
100
111
8101 0 return $self->reply->not_found;
8102 }
8103  
8104 80 my ($page, $per_page, $offset) = $self->_pagination;
8105  
8106 80 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 80 80
8107 80 my ($source, $records, $has_next, $is_multi);
8108 80 eval {
8109 80 ▶ $source = $self->_accelerated_source("table:$table") // $self->open_table($table);
Condition33%1/3
$self->_accelerated_source("table:$table") // $self->open_table($table)
l!l&&r!l&&!r
0800
MC/DC0%0/2
$self->_accelerated_source("table:$table") // $self->open_table($table)
$self->_accelerated_source("table:$table") $self->open_table($table)
Truth table33%1/3
A // B
A= $self->_accelerated_source("table:$table")B= $self->open_table($table)
ABresult
1X1
011
000
8110 # Multi-table SQLite: flag redirect without fetching data.
8111 80 ▶ if ($source && $source->sqlite_tables && @{ $source->sqlite_tables } > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1) { } 1 79 80
Condition66%4/6
$source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1
!ll&&!rl&&r
7901
$source and $source->sqlite_tables
!ll&&!rl&&r
0791
MC/DC33%1/3
$source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1
$source $source->sqlite_tables @{$source->sqlite_tables;} > 1
Truth table50%2/4
A and B and C
A= $sourceB= $source->sqlite_tablesC= @{$source->sqlite_tables;} > 1
ABCresult
0XX0
10X0
1100
1111
8112 1 $is_multi = 1;
8113 } else {
8114 # _fetch_page applies reduce-before-paginate: full-fetch + filter + dedup
8115 # + Perl paginate when f= or d=1 are set; N+1 trick otherwise.
8116 79 ($records, $has_next) = $self->_fetch_page($source,
8117 { per_page => $per_page, offset => $offset, dedup => $dedup,
8118 calc_spec_key => "table:$table" });
8119 }
8120 };
8121 80 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 80 80
8122 # No file by that name: a 404 that names the table, with the trail
8123 # and the navigation, rather than the home page and the backend's
8124 # message (which gives away the data directory).
8125 # A directory that may not be read has no file by that name to offer either.
8126 0 my $error = $@;
8127 0 ▶ my $stem_of = sub { lc($_[0]->basename =~ s/\.[^.]+\z//r) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
8128 0 my $listing = eval { _data_dir_for($self->app)->list };
8129 0 $@ = $error;
8130 0 ▶ unless ($listing && grep { $stem_of->($_) eq lc $table } $listing->each) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listing and grep {&$stem_of($_) eq lc $table;} $listing->each) 0 0 0
Condition0%0/3
$listing and grep {&$stem_of($_) eq lc $table;} $listing->each
!ll&&!rl&&r
000
MC/DC0%0/2
$listing and grep {&$stem_of($_) eq lc $table;} $listing->each
$listing grep {&$stem_of($_) eq lc $table;} $listing->each
Truth table0%0/3
A and B
A= $listingB= grep {&$stem_of($_) eq lc $table;} $listing->each
ABresult
0X0
100
111
8131 0 return $self->render(
8132 template => "$platform/$language/not_found",
8133 handler => 'tt',
8134 format => 'html',
8135 status => 404,
8136 title => 'Table not found',
8137 error_msg => $self->_i18n('error_table_missing', $table),
8138 language => $language,
8139 );
8140 }
8141 0 return $self->render(
8142 template => "$platform/$language/home",
8143 handler => 'tt',
8144 format => 'html',
8145 tables => [],
8146 title => 'Choose a Database',
8147 error => $self->_i18n('error_table_open', $table, _tidy_error($@)),
8148 language => $language,
8149 );
8150 }
8151  
8152 80 ▶ if ($is_multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multi) 1 79 80
8153 1 return $self->redirect_to('/tables?l=' . url_escape("table:$table"));
8154 }
8155  
8156 # A cross-row spec that could not be honoured has already rendered its 400
8157 # inside _fetch_page; drawing the page over it would hide the reason.
8158 79 ▶ return if $self->stash('_cross_row_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_cross_row_error") 1 78 79
8159  
8160 78 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
780
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
8161 # The page from _fetch_page is already reduced; this call is here for the chip
8162 # list only -- filter_specs (for export URLs) and filters_json (for the UI).
8163 78 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
8164  
8165 # Calculated fields belong to the table, not only to its charts. The table
8166 # action is addressed by :table rather than l=, so name the spec explicitly
8167 # when looking up the fields saved for it; otherwise a saved column would
8168 # appear in every chart and not in the table it was made from.
8169 78 my $page_columns;
8170 78 ($records, $page_columns) = $self->_page_columns($source, $records, "table:$table");
8171 78 ▶ return unless $page_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_columns 2 76 78
8172 76 my @columns = @$page_columns;
8173  
8174 76 my $page_url = $self->req->url->clone;
8175 76 $page_url->query->remove('page')->remove('per_page');
8176 76 my $page_base_url = $page_url->to_string;
8177  
8178 76 ▶ $self->render(
Condition100%2/2
$self->param("back2_label") // "Back"
l!l
373
MC/DC100%1/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table100%2/2
A
A= $self->param("back2_label")
Aresult
11
00
8179 template => "$platform/$language/dashboard",
8180 handler => 'tt',
8181 format => 'html',
8182 records => $records,
8183 columns => \@columns,
8184 table => $table,
8185 title => ucfirst($table),
8186 left_spec => "table:$table",
8187 combine_specs => [ $self->_calc_specs_of_request ],
8188 current_joins => [],
8189 available_tables => $self->_scan_join_tables,
8190 join_summaries => [],
8191 filter_specs => $filter_specs,
8192 filters_json => $filters_json,
8193 dedup => $dedup,
8194 back_url => _safe_back_url($self->param('back2')),
8195 back_label => $self->param('back2_label') // 'Back',
8196 export_url => $self->_build_export_url("table:$table", [], $filter_specs, undef, $dedup, {
8197 span => $self->param('span'), tcol => $self->param('tcol'), as => $self->param('as'),
8198 }),
8199 page => $page,
8200 per_page => $per_page,
8201 has_next => $has_next,
8202 page_base_url => $page_base_url,
8203 language => $language,
8204 );
8205 }
8206  
8207 =head2 browse
8208  
8209 C<GET /browse> -- Navigate the filesystem and pick a data file.
8210  
8211 =head3 API SPECIFICATION
8212  
8213 =head4 INPUT
8214  
8215 ?path= string Absolute filesystem path to browse (default: $HOME).
8216 Returns 404 when the path does not exist or is not a
8217 directory.
8218  
8219 =head4 OUTPUT
8220  
8221 Renders C<browse.html.tt> with:
8222  
8223 current_path string
8224 parent_href string or undef (undef at filesystem root)
8225 crumbs ARRAYREF of { name, href }
8226 dirs ARRAYREF of { name, href } -- subdirectories, sorted
8227 files ARRAYREF of { name, href } -- supported data files, sorted
8228  
8229 =head3 MESSAGES
8230  
8231 None; errors render as 404.
8232  
8233 =head3 FORMAL SPECIFICATION
8234  
8235 browse == lambda self .
8236 let dir = realpath(param('path') // first_root) in
8237 pre inside(dir, file_roots) /\ is_dir(dir)
8238 post render(browse, dirs: subdirs(dir), files: supported_files(dir))
8239  
8240 =head3 EXAMPLE
8241  
8242 GET /browse -> 200, lists $HOME
8243 GET /browse?path=/tmp -> 200, lists /tmp
8244 GET /browse?path=/nonexistent/xyz -> 404
8245  
8246 =cut
8247  
8248 49 ▶sub browse ($self) {
Subroutine browse: called CC=10 SCAR=24.1
Pod documented
8249 49 my ($platform, $language) = $self->_resolve_template;
8250  
8251 49 ▶ my $raw_path = $self->param('path') // _first_root($self);
Condition66%2/3
$self->param("path") // _first_root($self)
l!l&&r!l&&!r
6430
MC/DC0%0/2
$self->param("path") // _first_root($self)
$self->param("path") _first_root($self)
Truth table66%2/3
A // B
A= $self->param("path")B= _first_root($self)
ABresult
1X1
011
000
8252 49 ▶ if (my $refusal = _outside_roots($self, $raw_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = _outside_roots($self, $raw_path)) 0 49 49
8253 0 return $self->render(text => $refusal, status => 403);
8254 }
8255 49 my $dir = eval { Mojo::File->new($raw_path)->realpath };
8256 49 ▶ return $self->reply->not_found unless defined $dir && -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and -d $dir 0 49 49
Condition33%1/3
defined $dir and -d $dir
!ll&&!rl&&r
0049
MC/DC0%0/2
defined $dir and -d $dir
defined $dir -d $dir
Truth table33%1/3
A and B
A= defined $dirB= -d $dir
ABresult
0X0
100
111
8257  
8258 49 my $listing = $self->_list_dir($dir, 1);
8259  
8260 # Add browse hrefs to file entries (dirs already have path; browse needs href).
8261 my @dirs = map {
8262 59 +{ name => $_->{name}, href => '/browse?path=' . url_escape($_->{path}) }
8263 49 } @{ $listing->{dirs} };
8264  
8265 my @files = map {
8266 18 +{ name => $_->{name}, href => '/open?path=' . url_escape($_->{path}) }
8267 49 } @{ $listing->{files} };
8268  
8269 # Build breadcrumb trail from filesystem root down to $dir.
8270 49 my @crumbs;
8271 {
8272 49 my $f = $dir;
8273 49 while (1) {
8274 158 my $name = $f->basename;
8275 158 ▶ $name = '/' unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 158 158
8276 158 unshift @crumbs, { name => $name, href => '/browse?path=' . url_escape($f->to_string) };
8277 158 my $parent = $f->dirname;
8278 158 ▶ last if $parent->to_string eq $f->to_string;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->to_string eq $f->to_string 49 109 158
8279 109 ▶ last if _outside_roots($self, $parent->to_string);
Branch: 1 branch
BranchTrueFalseTotal
if _outside_roots($self, $parent->to_string) 0 109 109
8280 109 $f = $parent;
8281 }
8282 }
8283  
8284 49 my $parent = $dir->dirname;
8285 49 ▶ my $parent_href = $parent->to_string ne $dir->to_string && !_outside_roots($self, $parent->to_string)
Branch: 1 branch
BranchTrueFalseTotal
$parent->to_string ne $dir->to_string && !_outside_roots($self, $parent->to_string) ? : 49 0 49
Condition33%1/3
$parent->to_string ne $dir->to_string and not _outside_roots($self, $parent->to_string)
!ll&&!rl&&r
0049
MC/DC0%0/2
$parent->to_string ne $dir->to_string and not _outside_roots($self, $parent->to_string)
$parent->to_string ne $dir->to_string not _outside_roots($self, $parent->to_string)
Truth table33%1/3
A and not(B)
A= $parent->to_string ne $dir->to_stringB= not _outside_roots($self, $parent->to_string)
ABresult
0X0
100
111
8286 ? '/browse?path=' . url_escape($parent->to_string)
8287 : undef;
8288  
8289 49 $self->render(
8290 template => "$platform/$language/browse",
8291 handler => 'tt',
8292 format => 'html',
8293 title => 'Browse Files',
8294 current_path => $dir->to_string,
8295 parent_href => $parent_href,
8296 crumbs => \@crumbs,
8297 dirs => \@dirs,
8298 files => \@files,
8299 language => $language,
8300 );
8301 }
8302  
8303 =head2 open_file
8304  
8305 C<GET /open> -- Open a supported data file from any absolute filesystem path.
8306  
8307 =head3 API SPECIFICATION
8308  
8309 =head4 INPUT
8310  
8311 ?path= string Absolute path to the data file. Returns 404 when missing,
8312 not a regular file, or the extension is not in SUPPORTED_EXT.
8313 ?f= string (repeatable) Filter spec: "col:op:val".
8314  
8315 =head4 DOMAIN CONSTRAINTS: ?path=
8316  
8317 =over 4
8318  
8319 =item Extension filter (C<EXT_RE>)
8320  
8321 The basename must match C<\.(?:csv|db|sql|xml|tsv|psv)\z> (case-insensitive).
8322 The C<\z> anchor (absolute end-of-string) means a URL-encoded trailing
8323 newline (e.g. C<file.csv%0A> decoded to C<file.csv\n>) does NOT pass --
8324 the C<\n> falls after the C<\z> boundary and the extension check fails.
8325  
8326 =item Valid partition
8327  
8328 C</data/sales.csv> (lowercase extension), C</tmp/REPORT.CSV> (uppercase
8329 extension, /i matches), any C<.db>, C<.sql>, C<.xml>, C<.tsv>, C<.psv> regular
8330 file.
8331  
8332 =item Invalid partition
8333  
8334 Absent C<?path=> (404), non-existent file (404), directory instead of
8335 file (404), unsupported extension such as C<.txt> (404), empty string
8336 (404), path containing a C<%0A> (newline) suffix (404).
8337  
8338 =item Double-extension filenames
8339  
8340 A file like C<file.php.csv> passes C<EXT_RE> (last segment is C<.csv>)
8341 but produces table stem C<file.php> which fails C<DataSource>'s
8342 C<TABLE_NAME_RE>. The C<open_table> call is inside C<eval>, so the
8343 croak is caught and the action returns 200 with a friendly error, not
8344 a 500.
8345  
8346 =back
8347  
8348 =head4 OUTPUT
8349  
8350 On success renders C<dashboard.html.tt> with C<back_url> pointing to the
8351 directory's C</browse> page, and C<file_path> set to the resolved absolute
8352 path (used by the template to record the file in C<localStorage>).
8353 On error re-renders C<home.html.tt> with C<error>, C<back_url>, C<back_label>.
8354  
8355 =head3 MESSAGES
8356  
8357 error_file_open -- DataSource threw during initialisation or fetch_all.
8358  
8359 =head3 FORMAL SPECIFICATION
8360  
8361 open_file == lambda self .
8362 let file = realpath(param('path')) in
8363 pre is_file(file) /\ basename(file) =~ EXT_RE
8364 let src = open_table(stem(file), dir=dirname(file)) in
8365 post render(dashboard, file_path: file.to_string)
8366  
8367 =head3 EXAMPLE
8368  
8369 GET /open?path=/data/archive/sales.csv -> 200, table view
8370 GET /open -> 404
8371 GET /open?path=/etc/passwd -> 404
8372  
8373 =cut
8374  
8375 86 ▶sub open_file ($self) {
Subroutine open_file: called CC=61 SCAR=55.6
Pod documented
8376 86 my ($platform, $language) = $self->_resolve_template;
8377 86 my $file_path = $self->param('path');
8378  
8379 86 ▶ return $self->reply->not_found unless defined $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_path 42 44 86
8380  
8381 44 my ($page, $per_page, $offset) = $self->_pagination;
8382  
8383 # Bare redis:// URL (no table name) -- redirect to key-listing page.
8384 44 ▶ if ($file_path =~ $REDIS_URL_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ /$REDIS_URL_RE/u) 0 44 44
8385 0 return $self->redirect_to('/tables?l=' . url_escape($file_path));
8386 }
8387  
8388 # A MariaDB server with no table named: list its tables.
8389 44 ▶ if ($file_path =~ $MARIADB_SERVER_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ /$MARIADB_SERVER_RE/u) 0 44 44
8390 0 return $self->redirect_to('/tables?l=' . url_escape($file_path));
8391 }
8392  
8393 # Redis spec: redis:tablename|redis://host:port -- delegate to _open_spec.
8394 # MariaDB spec: mariadb:server/table -- likewise.
8395 44 ▶ if ($file_path =~ $REDIS_SPEC_RE || $file_path =~ $MARIADB_SPEC_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ /$REDIS_SPEC_RE/u or $file_path =~ /$MARIADB_SPEC_RE/u) 0 44 44
Condition33%1/3
$file_path =~ /$REDIS_SPEC_RE/u or $file_path =~ /$MARIADB_SPEC_RE/u
l!l&&r!l&&!r
0044
MC/DC0%0/2
$file_path =~ /$REDIS_SPEC_RE/u or $file_path =~ /$MARIADB_SPEC_RE/u
$file_path =~ /$REDIS_SPEC_RE/u $file_path =~ /$MARIADB_SPEC_RE/u
Truth table33%1/3
A or B
A= $file_path =~ /$REDIS_SPEC_RE/uB= $file_path =~ /$MARIADB_SPEC_RE/u
ABresult
1X1
011
000
8396 0 my $lspec = $file_path;
8397 0 my ($source, $label) = $self->_open_spec($lspec);
8398 0 ▶ unless ($source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source) 0 0 0
8399 0 my $why = $self->stash('open_spec_error');
8400 0 ▶ return $self->reply->not_found unless defined $why;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why 0 0 0
8401 0 return $self->render(
8402 template => "$platform/$language/home",
8403 handler => 'tt',
8404 format => 'html',
8405 tables => [],
8406 title => 'Error',
8407 error => $self->_i18n('error_file_open', $lspec, $why),
8408 back_url => '/',
8409 back_label => 'Home',
8410 language => $language,
8411 );
8412 }
8413 0 my $table = $label;
8414 0 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 0 0
8415 0 my ($records, $has_next);
8416 0 eval {
8417 0 ($records, $has_next) = $self->_fetch_page($source,
8418 { per_page => $per_page, offset => $offset, dedup => $dedup, calc_spec_key => $lspec });
8419 };
8420 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
8421 0 return $self->render(
8422 template => "$platform/$language/home",
8423 handler => 'tt',
8424 format => 'html',
8425 tables => [],
8426 title => 'Error',
8427 error => $self->_i18n('error_file_open', $label, _tidy_error($@)),
8428 back_url => '/',
8429 back_label => 'Home',
8430 language => $language,
8431 );
8432 }
8433 # A cross-row spec that could not be honoured has already rendered its 400
8434 # inside _fetch_page; the page must not be drawn over it.
8435 0 ▶ return if $self->stash('_cross_row_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_cross_row_error") 0 0 0
8436  
8437 0 ▶ $records //= [];
Condition0%0/2
$records //= []
l!l
00
MC/DC0%0/1
$records //= []
$records
Truth table0%0/2
A
A= $records
Aresult
11
00
8438 0 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
8439 0 my $page_columns;
8440 0 ($records, $page_columns) = $self->_page_columns($source, $records, $lspec);
8441 0 ▶ return unless $page_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_columns 0 0 0
8442 0 my @columns = @$page_columns;
8443 0 my $rpage_url = $self->req->url->clone;
8444 0 $rpage_url->query->remove('page')->remove('per_page');
8445 0 ▶ return $self->render(
Condition0%0/6
_safe_back_url($self->param("back")) // "/"
l!l
00
$self->param("back_label") // "Home"
l!l
00
$self->param("back2_label") // "Back"
l!l
00
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
MC/DC0%0/1
$self->param("back_label") // "Home"
$self->param("back_label")
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table0%0/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
Truth table0%0/2
A
A= $self->param("back_label")
Aresult
11
00
Truth table0%0/2
A
A= $self->param("back2_label")
Aresult
11
00
8446 template => "$platform/$language/dashboard",
8447 handler => 'tt',
8448 format => 'html',
8449 records => $records,
8450 columns => \@columns,
8451 table => $table,
8452 title => $label,
8453 back_url => _safe_back_url($self->param('back')) // '/',
8454 back_label => $self->param('back_label') // 'Home',
8455 back2_url => _safe_back_url($self->param('back2')),
8456 back2_label => $self->param('back2_label') // 'Back',
8457 file_path => $file_path,
8458 left_spec => $lspec,
8459 combine_specs => [ $self->_calc_specs_of_request ],
8460 current_joins => [],
8461 available_tables => $self->_scan_join_tables,
8462 join_summaries => [],
8463 filter_specs => $filter_specs,
8464 filters_json => $filters_json,
8465 dedup => $dedup,
8466 export_url => $self->_build_export_url($lspec, [], $filter_specs, undef, $dedup),
8467 page => $page,
8468 per_page => $per_page,
8469 has_next => $has_next,
8470 page_base_url => $rpage_url->to_string,
8471 language => $language,
8472 );
8473 }
8474  
8475 # Remote path /../hostname/dir/file.ext: delegate opening to _open_spec
8476 # and render the dashboard directly, bypassing the realpath/local-file path.
8477 44 ▶ if ($file_path =~ m{\A/\.\./}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ m[\A/\.\./]u) 4 40 44
8478 4 my $lspec = 'path:' . $file_path;
8479 4 my ($source, $label) = $self->_open_spec($lspec);
8480 4 ▶ return $self->reply->not_found unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 1 3 4
8481 3 my $filename = $label;
8482 3 (my $table = $filename) =~ s/\.[^.]+\z//;
8483 3 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 3 3
8484 3 my ($records, $has_next);
8485 3 eval {
8486 3 ($records, $has_next) = $self->_fetch_page($source,
8487 { per_page => $per_page, offset => $offset, dedup => $dedup, calc_spec_key => $lspec });
8488 };
8489 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
8490 0 return $self->render(
8491 template => "$platform/$language/home",
8492 handler => 'tt',
8493 format => 'html',
8494 tables => [],
8495 title => 'Error',
8496 error => $self->_i18n('error_file_open', $filename, _tidy_error($@)),
8497 back_url => '/',
8498 back_label => 'Home',
8499 language => $language,
8500 );
8501 }
8502 # A cross-row spec that could not be honoured has already rendered its 400
8503 # inside _fetch_page; the page must not be drawn over it.
8504 3 ▶ return if $self->stash('_cross_row_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_cross_row_error") 0 3 3
8505  
8506 3 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
30
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
8507 3 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
8508 3 my $page_columns;
8509 3 ($records, $page_columns) = $self->_page_columns($source, $records, $lspec);
8510 3 ▶ return unless $page_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_columns 0 3 3
8511 3 my @columns = @$page_columns;
8512 3 my $rpage_url = $self->req->url->clone;
8513 3 $rpage_url->query->remove('page')->remove('per_page');
8514 3 ▶ return $self->render(
Condition50%1/2
$self->param("back2_label") // "Back"
l!l
03
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
8515 template => "$platform/$language/dashboard",
8516 handler => 'tt',
8517 format => 'html',
8518 records => $records,
8519 columns => \@columns,
8520 table => $table,
8521 title => $filename,
8522 back_url => '/',
8523 back_label => 'Home',
8524 back2_url => _safe_back_url($self->param('back2')),
8525 back2_label => $self->param('back2_label') // 'Back',
8526 file_path => $file_path,
8527 left_spec => $lspec,
8528 combine_specs => [ $self->_calc_specs_of_request ],
8529 current_joins => [],
8530 available_tables => $self->_scan_join_tables,
8531 join_summaries => [],
8532 filter_specs => $filter_specs,
8533 filters_json => $filters_json,
8534 dedup => $dedup,
8535 export_url => $self->_build_export_url($lspec, [], $filter_specs, undef, $dedup),
8536 page => $page,
8537 per_page => $per_page,
8538 has_next => $has_next,
8539 page_base_url => $rpage_url->to_string,
8540 language => $language,
8541 );
8542 }
8543  
8544 # A path that names a table after a "#", as a spec does, is that table.
8545 40 my ($named_file, $named_it) = _split_path($file_path);
8546 40 ▶ $file_path = $named_file if defined $named_it;
Branch: 1 branch
BranchTrueFalseTotal
if defined $named_it 0 40 40
8547 40 ▶ if (my $refusal = _outside_roots($self, $file_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = _outside_roots($self, $file_path)) 0 40 40
8548 0 return $self->render(text => $refusal, status => 403);
8549 }
8550  
8551 40 my $file = eval { Mojo::File->new($file_path)->realpath };
8552 40 ▶ return $self->reply->not_found
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -f $file and $file->basename =~ /$EXT_RE/u 7 33 40
Condition100%6/6
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
!ll&&!rl&&r
4333
defined $file and -f $file
!ll&&!rl&&r
1336
MC/DC100%3/3
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
defined $file -f $file $file->basename =~ /$EXT_RE/u
Truth table100%4/4
A and B and C
A= defined $fileB= -f $fileC= $file->basename =~ /$EXT_RE/u
ABCresult
0XX0
10X0
1100
1111
8553 unless defined $file && -f $file && $file->basename =~ $EXT_RE;
8554  
8555 33 my $dir = $file->dirname;
8556 33 (my $table = $file->basename) =~ s/\.[^.]+\z//;
8557 33 my $back = '/browse?path=' . url_escape($dir->to_string);
8558 33 my $filename = $file->basename;
8559 33 my $lspec = 'path:' . $file->to_string;
8560  
8561 33 ▶ my $it = $self->param('it') // $named_it; # optional: select a specific internal table
Condition66%2/3
$self->param("it") // $named_it
l!l&&r!l&&!r
1032
MC/DC50%1/2
$self->param("it") // $named_it
$self->param("it") $named_it
Truth table66%2/3
A // B
A= $self->param("it")B= $named_it
ABresult
1X1
011
000
8562  
8563 # A table opened from the schema browser belongs to a file in data_dir. Name
8564 # it as table:store/sales so the join, filter and export panels keep the
8565 # chosen internal table instead of dropping back to the file's first table.
8566 33 ▶ if (defined $it && $it =~ $TABLE_NAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $it and $it =~ /$TABLE_NAME_RE/u) 1 32 33
Condition66%2/3
defined $it and $it =~ /$TABLE_NAME_RE/u
!ll&&!rl&&r
3201
MC/DC50%1/2
defined $it and $it =~ /$TABLE_NAME_RE/u
defined $it $it =~ /$TABLE_NAME_RE/u
Truth table66%2/3
A and B
A= defined $itB= $it =~ /$TABLE_NAME_RE/u
ABresult
0X0
100
111
8567 1 my $data_dir = eval { _data_dir_for($self->app)->realpath };
8568 1 my $file_dir = eval { $file->dirname->realpath };
8569 1 ▶ $lspec = "table:$table/$it" if $data_dir && $file_dir && "$file_dir" eq "$data_dir";
Branch: 1 branch
BranchTrueFalseTotal
if $data_dir and $file_dir and "$file_dir" eq "$data_dir" 0 1 1
Condition33%2/6
$data_dir and $file_dir and "$file_dir" eq "$data_dir"
!ll&&!rl&&r
010
$data_dir and $file_dir
!ll&&!rl&&r
001
MC/DC0%0/3
$data_dir and $file_dir and "$file_dir" eq "$data_dir"
$data_dir $file_dir "$file_dir" eq "$data_dir"
Truth table25%1/4
A and B and C
A= $data_dirB= $file_dirC= "$file_dir" eq "$data_dir"
ABCresult
0XX0
10X0
1100
1111
8570 }
8571 # Elsewhere the spec names the table after a "#", so the panels and charts
8572 # keep it too.
8573 33 ▶ $lspec .= "#$it" if defined $it && length $it && $it !~ /[#\n]/ && $lspec =~ /\Apath:/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $it and length $it and not $it =~ /[#\n]/u and $lspec =~ /\Apath:/u 1 32 33
Condition66%6/9
defined $it and length $it and not $it =~ /[#\n]/u and $lspec =~ /\Apath:/u
!ll&&!rl&&r
3201
defined $it and length $it and not $it =~ /[#\n]/u
!ll&&!rl&&r
3201
defined $it and length $it
!ll&&!rl&&r
3201
MC/DC25%1/4
defined $it and length $it and not $it =~ /[#\n]/u and $lspec =~ /\Apath:/u
defined $it length $it not $it =~ /[#\n]/u $lspec =~ /\Apath:/u
Truth table40%2/5
A and B and not(C) and D
A= defined $itB= length $itC= not $it =~ /[#\n]/uD= $lspec =~ /\Apath:/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
8574  
8575 # Allow the tables view to override the default "Back to browser" link.
8576 33 my $back_override = _safe_back_url($self->param('back'));
8577 33 ▶ if ($back_override) {
Branch: 1 branch
BranchTrueFalseTotal
if ($back_override) 1 32 33
8578 1 $back = $back_override;
8579 }
8580 33 ▶ my $back_label_default = $back_override ? ($self->param('back_label') // 'Back') : 'Back to browser';
Branch: 1 branch
BranchTrueFalseTotal
$back_override ? : 1 32 33
Condition50%1/2
$self->param("back_label") // "Back"
l!l
10
MC/DC0%0/1
$self->param("back_label") // "Back"
$self->param("back_label")
Truth table50%1/2
A
A= $self->param("back_label")
Aresult
11
00
8581  
8582 33 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 1 32 33
8583 33 my ($source, $records, $has_next, $is_multi_local);
8584 33 eval {
8585 33 ▶ $source = (defined $it ? undef : $self->_accelerated_source($lspec))
Branch: 2 branches
BranchTrueFalseTotal
defined $it ? : 1 32 33
defined $it ? : 1 32 33
Condition33%1/3
(defined $it ? undef : $self->_accelerated_source($lspec)) // $self->open_table($table, "directory", $dir->to_string, defined $it ? ("internal_table", $it) : ())
l!l&&r!l&&!r
0330
MC/DC0%0/2
(defined $it ? undef : $self->_accelerated_source($lspec)) // $self->open_table($table, "directory", $dir->to_string, defined $it ? ("internal_table", $it) : ())
(defined $it ? undef : $self->_accelerated_source($lspec)) $self->open_table($table, "directory", $dir->to_string, defined $it ? ("internal_table", $it) : ())
Truth table33%1/3
A // B
A= (defined $it ? undef : $self->_accelerated_source($lspec))B= $self->open_table($table, "directory", $dir->to_string, defined $it ? ("internal_table", $it) : ())
ABresult
1X1
011
000
8586 // $self->open_table($table, directory => $dir->to_string,
8587 defined $it ? (internal_table => $it) : ());
8588 # Multi-table SQLite without it=: flag redirect without fetching data.
8589 33 ▶ if (!defined $it && $source && $source->sqlite_tables
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $it and $source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1) { } 1 32 33
Condition88%8/9
not defined $it and $source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1
!ll&&!rl&&r
20121
not defined $it and $source and $source->sqlite_tables
!ll&&!rl&&r
11913
not defined $it and $source
!ll&&!rl&&r
1032
MC/DC75%3/4
not defined $it and $source and $source->sqlite_tables and @{$source->sqlite_tables;} > 1
not defined $it $source $source->sqlite_tables @{$source->sqlite_tables;} > 1
Truth table80%4/5
not(A) and B and C and D
A= not defined $itB= $sourceC= $source->sqlite_tablesD= @{$source->sqlite_tables;} > 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
8590 13 && @{ $source->sqlite_tables } > 1) {
8591 1 $is_multi_local = 1;
8592 } else {
8593 32 ($records, $has_next) = $self->_fetch_page($source,
8594 { per_page => $per_page, offset => $offset, dedup => $dedup, calc_spec_key => $lspec });
8595 }
8596 };
8597 33 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 33 33
8598 # Provide a SQLite-specific hint when the error looks like a table-name
8599 # mismatch or a missing-tables failure so the user knows what to do. A
8600 # table whose name cannot be used has its own advice in the message.
8601 0 my $hint;
8602 0 ▶ if ($filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/i && $@ !~ /cannot be opened|can be opened/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iu and not $@ =~ /cannot be opened|can be opened/u) 0 0 0
Condition0%0/3
$filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iu and not $@ =~ /cannot be opened|can be opened/u
!ll&&!rl&&r
000
MC/DC0%0/2
$filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iu and not $@ =~ /cannot be opened|can be opened/u
$filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iu not $@ =~ /cannot be opened|can be opened/u
Truth table0%0/3
A and not(B)
A= $filename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iuB= not $@ =~ /cannot be opened|can be opened/u
ABresult
0X0
100
111
8603 0 $hint = 'SQLite files must contain at least one user-defined table. '
8604 . 'The table name inside the database does not need to match the '
8605 . 'filename -- the application auto-detects and uses the first table it can open.';
8606 }
8607 0 ▶ return $self->render(
Condition0%0/2
$self->param("back2_label") // "Back"
l!l
00
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table0%0/2
A
A= $self->param("back2_label")
Aresult
11
00
8608 template => "$platform/$language/home",
8609 handler => 'tt',
8610 format => 'html',
8611 tables => [],
8612 title => 'Error',
8613 error => $self->_i18n('error_file_open', $filename, _tidy_error($@)),
8614 hint => $hint,
8615 back_url => $back,
8616 back_label => $back_label_default,
8617 back2_url => _safe_back_url($self->param('back2')),
8618 back2_label => $self->param('back2_label') // 'Back',
8619 language => $language,
8620 );
8621 }
8622  
8623 33 ▶ if ($is_multi_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multi_local) 1 32 33
8624 1 return $self->redirect_to('/tables?l=' . url_escape("path:${\$file->to_string}"));
8625 }
8626  
8627 # A cross-row spec that could not be honoured has already rendered its 400
8628 # inside _fetch_page; the page must not be drawn over it.
8629 32 ▶ return if $self->stash('_cross_row_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_cross_row_error") 0 32 32
8630  
8631 32 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
320
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
8632 32 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
8633 32 my $page_columns;
8634 32 ($records, $page_columns) = $self->_page_columns($source, $records, $lspec);
8635 32 ▶ return unless $page_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_columns 0 32 32
8636 32 my @columns = @$page_columns;
8637  
8638 32 my $lpage_url = $self->req->url->clone;
8639 32 $lpage_url->query->remove('page')->remove('per_page');
8640  
8641 32 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
defined $it ? : 1 31 32
defined $it ? : 1 31 32
Condition50%1/2
$self->param("back2_label") // "Back"
l!l
032
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
8642 template => "$platform/$language/dashboard",
8643 handler => 'tt',
8644 format => 'html',
8645 records => $records,
8646 columns => \@columns,
8647 table => defined $it ? $it : $table,
8648 title => defined $it ? "$filename ($it)" : $filename,
8649 back_url => $back,
8650 back_label => $back_label_default,
8651 back2_url => _safe_back_url($self->param('back2')),
8652 back2_label => $self->param('back2_label') // 'Back',
8653 file_path => $file->to_string,
8654 left_spec => $lspec,
8655 combine_specs => [ $self->_calc_specs_of_request ],
8656 current_joins => [],
8657 available_tables => $self->_scan_join_tables,
8658 join_summaries => [],
8659 filter_specs => $filter_specs,
8660 filters_json => $filters_json,
8661 dedup => $dedup,
8662 export_url => $self->_build_export_url($lspec, [], $filter_specs, undef, $dedup),
8663 page => $page,
8664 per_page => $per_page,
8665 has_next => $has_next,
8666 page_base_url => $lpage_url->to_string,
8667 language => $language,
8668 );
8669 }
8670  
8671 =head2 import_url
8672  
8673 C<GET /import> -- Fetch a remote HTML page, extract the first (or selected)
8674 table, and render it as a sortable data grid.
8675  
8676 =head3 API SPECIFICATION
8677  
8678 =head4 INPUT
8679  
8680 ?url= string Required. Full http:// or https:// URL of the page
8681 that contains the HTML table. Returns home with an
8682 error message if the scheme is wrong, the fetch fails,
8683 or no table is found at the specified index.
8684 ?table_index= integer Zero-based index of the C<< <table> >> to use when
8685 the page contains more than one table (default: 0).
8686 ?f= string (repeatable) Filter spec: "col:op:val".
8687  
8688 =head4 OUTPUT
8689  
8690 On success renders C<dashboard.html.tt> with the same stash shape as C<open_file>,
8691 plus C<source_url> (the original URL) for C<localStorage> tracking.
8692  
8693 On error re-renders C<home.html.tt> with an C<error> stash variable.
8694  
8695 =head3 MESSAGES
8696  
8697 error_url_required -- empty or missing C<url> param
8698 error_url_invalid -- URL does not begin with http:// or https://
8699 error_url_fetch -- LWP fetch failure or no table found at the index
8700  
8701 =head3 EXAMPLE
8702  
8703 GET /import?url=https://matrix.perl-magpie.org/dist/Database-Abstraction/
8704 GET /import?url=https://example.com/data.html&table_index=2
8705  
8706 =cut
8707  
8708 49 ▶sub import_url ($self) {
Subroutine import_url: called CC=12 SCAR=25.1
Pod documented
8709 49 my ($platform, $language) = $self->_resolve_template;
8710  
8711 49 ▶ my $url = $self->param('url') // '';
Condition100%2/2
$self->param("url") // ""
l!l
445
MC/DC100%1/1
$self->param("url") // ""
$self->param("url")
Truth table100%2/2
A
A= $self->param("url")
Aresult
11
00
8712 49 ▶ my $idx = $self->param('table_index') // 0;
Condition50%1/2
$self->param("table_index") // 0
l!l
049
MC/DC0%0/1
$self->param("table_index") // 0
$self->param("table_index")
Truth table50%1/2
A
A= $self->param("table_index")
Aresult
11
00
8713 49 ▶ $idx = 0 unless $idx =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $idx =~ /\A[0-9]+\z/u 0 49 49
8714  
8715 47 ▶ my $err_home = sub ($key, @args) {
Subroutine __ANON__: called CC=1 SCAR=0.0
8716 47 $self->render(
8717 template => "$platform/$language/home",
8718 handler => 'tt',
8719 format => 'html',
8720 tables => $self->_scan_data_dir,
8721 title => 'Choose a Database',
8722 error => $self->_i18n($key, @args),
8723 language => $language,
8724 );
8725 49 };
8726  
8727 49 ▶ return $err_home->('error_url_required') unless length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $url 45 4 49
8728 4 ▶ return $err_home->('error_url_invalid', $url)
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[\Ahttps?://]iu 1 3 4
8729 unless $url =~ m{\Ahttps?://}i;
8730 3 ▶ return $err_home->('error_url_ssrf', $url)
Branch: 1 branch
BranchTrueFalseTotal
unless _is_safe_url($url) 1 2 3
8731 unless _is_safe_url($url);
8732  
8733 2 my ($source, $records);
8734 2 my $ok = eval {
8735 2 $source = $self->open_table('', url => $url, html_table_index => $idx);
8736 2 $records = $source->fetch_all;
8737 2 1;
8738 };
8739 2 ▶ return $err_home->('error_url_fetch', $url, _tidy_error($@)) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
8740  
8741 2 my $label = $source->table_name;
8742 2 my $lspec = "url:$url";
8743 2 my @columns = _get_columns($source, $records);
8744 2 my ($filtered, $filter_specs, $filters_json) = $self->_apply_filters($records);
8745 2 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 2 2
8746 2 ▶ $filtered = _dedup_records($filtered, \@columns) if $dedup;
Branch: 1 branch
BranchTrueFalseTotal
if $dedup 0 2 2
8747  
8748 2 my $self_url = '/import?url=' . url_escape($url);
8749 2 ▶ $self_url .= '&table_index=' . $idx if $idx;
Branch: 1 branch
BranchTrueFalseTotal
if $idx 0 2 2
8750  
8751 2 ▶ $self->render(
Condition100%2/2
$self->param("back2_label") // "Back"
l!l
11
MC/DC100%1/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table100%2/2
A
A= $self->param("back2_label")
Aresult
11
00
8752 template => "$platform/$language/dashboard",
8753 handler => 'tt',
8754 format => 'html',
8755 records => $filtered,
8756 columns => \@columns,
8757 table => $label,
8758 title => $label,
8759 source_url => $url,
8760 back_url => $self_url,
8761 back_label => $label,
8762 back2_url => _safe_back_url($self->param('back2')),
8763 back2_label => $self->param('back2_label') // 'Back',
8764 left_spec => $lspec,
8765 combine_specs => [],
8766 current_joins => [],
8767 available_tables => $self->_scan_join_tables,
8768 join_summaries => [],
8769 filter_specs => $filter_specs,
8770 filters_json => $filters_json,
8771 dedup => $dedup,
8772 export_url => $self->_build_export_url($lspec, [], $filter_specs, undef, $dedup),
8773 language => $language,
8774 );
8775 }
8776  
8777 =head2 columns_api
8778  
8779 C<GET /api/columns> -- Return column names for a table or file as JSON.
8780  
8781 Used by the join UI to populate the right-key dropdown without a page reload.
8782  
8783 =head3 API SPECIFICATION
8784  
8785 =head4 INPUT
8786  
8787 ?table= string Table name from C<data_dir>, or C<file/table> for a table
8788 inside a multi-table SQLite file.
8789 ?path= string Absolute path to a data file.
8790 ?spec= string Unified spec: C<"table:name">, C<"table:file/table"> or
8791 C<"path:/abs/path">.
8792 Parsed only when C<table> and C<path> are both absent.
8793  
8794 One of C<table>, C<path>, or C<spec> must be present; C<table> takes
8795 precedence over C<path>, and both take precedence over C<spec>.
8796  
8797 =head4 OUTPUT
8798  
8799 200 application/json { "columns": ["col1", "col2", ...] }
8800 404 application/json { "error": "not found" }
8801  
8802 =head3 MESSAGES
8803  
8804 None produced by this action directly; internal errors yield a 404.
8805  
8806 =head3 FORMAL SPECIFICATION
8807  
8808 columns_api == lambda self .
8809 let src = open_spec(table_param or path_param or parse_spec(spec_param)) in
8810 pre src /= undef
8811 post render_json({ columns: get_columns(src) })
8812  
8813 =head3 EXAMPLE
8814  
8815 GET /api/columns?table=sales -> {"columns":["product","region","amount"]}
8816 GET /api/columns?table=store/customer -> {"columns":["id","name","email"]}
8817 GET /api/columns?spec=table:sales -> {"columns":["product","region","amount"]}
8818 GET /api/columns?spec=path:/data/f.csv -> {"columns":[...]}
8819 GET /api/columns?table=noexist -> 404
8820  
8821 =cut
8822  
8823 55 ▶sub columns_api ($self) {
Subroutine columns_api: called CC=28 SCAR=34.8
Pod documented
8824 55 my $table_name = $self->param('table');
8825 55 my $path = $self->param('path');
8826 55 my $path_internal;
8827  
8828 # Accept the unified spec format used by /join and /graph:
8829 # "table:name" or "path:/abs/path" -- mirrors _open_spec's parsing.
8830 55 ▶ if (!defined($table_name) && !defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table_name or defined $path) 46 9 55
Condition100%3/3
defined $table_name or defined $path
l!l&&r!l&&!r
7246
MC/DC100%2/2
defined $table_name or defined $path
defined $table_name defined $path
Truth table100%3/3
A or B
A= defined $table_nameB= defined $path
ABresult
1X1
011
000
8831 46 ▶ my $spec = $self->param('spec') // '';
Condition100%2/2
$self->param("spec") // ""
l!l
442
MC/DC100%1/1
$self->param("spec") // ""
$self->param("spec")
Truth table100%2/2
A
A= $self->param("spec")
Aresult
11
00
8832 46 ▶ if ($spec =~ m{\Atable:([A-Za-z_][A-Za-z0-9_]*(?:/[A-Za-z_][A-Za-z0-9_]*)?)\z}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ m[\Atable:([A-Za-z_][A-Za-z0-9_]*(?:/[A-Za-z_][A-Za-z0-9_]*)?)\z]u) { } 1 45 46
elsif ($spec =~ /\Apath:(.+)\z/su) { } 3 42 45
8833 1 $table_name = $1;
8834 } elsif ($spec =~ /\Apath:(.+)\z/s) {
8835 3 ($path, $path_internal) = _split_path($1);
8836 }
8837 }
8838  
8839 55 my $source;
8840 55 ▶ if (($self->param('spec') // '') =~ $MARIADB_SPEC_RE) {
Branch: 3 branches
BranchTrueFalseTotal
if (($self->param('spec') // '') =~ /$MARIADB_SPEC_RE/u) { } 0 55 55
elsif (defined $table_name and $table_name =~ m[\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z]u) { } 6 49 55
elsif (defined $path) { } 6 43 49
Condition100%2/2
$self->param("spec") // ""
l!l
451
MC/DC100%1/1
$self->param("spec") // ""
$self->param("spec")
Truth table100%2/2
A
A= $self->param("spec")
Aresult
11
00
8841 0 ($source) = $self->_open_spec($self->param('spec'));
8842 }
8843 ▶ elsif (defined $table_name && $table_name =~ m{\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z}) {
Condition100%3/3
defined $table_name and $table_name =~ m[\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z]u
!ll&&!rl&&r
4726
MC/DC100%2/2
defined $table_name and $table_name =~ m[\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z]u
defined $table_name $table_name =~ m[\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z]u
Truth table100%3/3
A and B
A= defined $table_nameB= $table_name =~ m[\A([A-Za-z_][A-Za-z0-9_]*)(?:/([A-Za-z_][A-Za-z0-9_]*))?\z]u
ABresult
0X0
100
111
8844 6 my ($table, $internal) = ($1, $2);
8845 6 my $data_dir = _data_dir_for($self->app);
8846 6 my $stem = _table_stem($data_dir, $table);
8847 6 ▶ return $self->render(json => { error => 'not found' }, status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stem 1 5 6
8848 unless defined $stem;
8849 5 $source = eval {
8850 5 ▶ $self->open_table($stem, directory => $data_dir->to_string,
Branch: 1 branch
BranchTrueFalseTotal
defined $internal ? : 0 5 5
8851 defined $internal ? (internal_table => $internal) : ())
8852 };
8853 }
8854 elsif (defined $path) {
8855 6 ▶ if ($path =~ m{\A/\.\./([^/]+)((?:/.+)?)/([^/]+)\z}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[\A/\.\./([^/]+)((?:/.+)?)/([^/]+)\z]u) { } 2 4 6
elsif (my $refusal = _outside_roots($self, $path)) { } 0 4 4
8856 2 ▶ my ($host, $dir_part, $file) = ($1, $2 // '', $3);
Condition50%1/2
$2 // ""
l!l
20
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
8857 2 ▶ $dir_part = '/' unless length $dir_part;
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir_part 0 2 2
8858 2 ▶ if ($host =~ $REMOTE_HOST_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /$REMOTE_HOST_RE/u) 2 0 2
8859 2 ▶ my ($tbl, $ext) = $file =~ /\A(.+)\.([^.]+)\z/ ? ($1, $2) : ($file, undef);
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\A(.+)\.([^.]+)\z/u ? : 2 0 2
8860 2 ▶ $source = eval { $self->open_table($tbl, directory => $dir_part, host => $host,
Branch: 2 branches
BranchTrueFalseTotal
defined $ext ? : 2 0 2
defined $path_internal ? : 0 2 2
8861 defined $ext ? (file_ext => $ext) : (),
8862 defined $path_internal ? (internal_table => $path_internal) : ()) };
8863 }
8864 } elsif (my $refusal = _outside_roots($self, $path)) {
8865 0 return $self->render(json => { error => $refusal }, status => 403);
8866 } else {
8867 4 my $file = eval { Mojo::File->new($path)->realpath };
8868 4 ▶ if (defined $file && -f $file && $file->basename =~ $EXT_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file and $file->basename =~ /$EXT_RE/u) 1 3 4
Condition83%5/6
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
!ll&&!rl&&r
301
defined $file and -f $file
!ll&&!rl&&r
211
MC/DC66%2/3
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
defined $file -f $file $file->basename =~ /$EXT_RE/u
Truth table75%3/4
A and B and C
A= defined $fileB= -f $fileC= $file->basename =~ /$EXT_RE/u
ABCresult
0XX0
10X0
1100
1111
8869 1 my $dir = $file->dirname->to_string;
8870 1 (my $tbl = $file->basename) =~ s/\.[^.]+\z//;
8871 1 ▶ $source = eval { $self->open_table($tbl, directory => $dir,
Branch: 1 branch
BranchTrueFalseTotal
defined $path_internal ? : 0 1 1
8872 defined $path_internal ? (internal_table => $path_internal) : ()) };
8873 }
8874 }
8875 }
8876  
8877 54 ▶ return $self->render(json => { error => 'not found' }, status => 404) unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 46 8 54
8878  
8879 # Use _get_columns to ensure consistent ordering with the view action.
8880 0 ▶ my $recs = ($source->columns ? [] : eval { $source->fetch_all } // []);
Branch: 1 branch
BranchTrueFalseTotal
$source->columns ? : 8 0 8
Condition0%0/2
eval { do { $source->fetch_all } } // []
l!l
00
MC/DC0%0/1
eval { do { $source->fetch_all } } // []
eval { do { $source->fetch_all } }
Truth table0%0/2
A
A= eval { do { $source->fetch_all } }
Aresult
11
00
8881 8 my @cols = _get_columns($source, $recs);
8882  
8883 8 $self->render(json => { columns => \@cols });
8884 }
8885  
8886 =head2 range_api
8887  
8888 C<GET /api/range> -- Return the min and max ISO date values for one column of
8889 a view. The view is the left source with any joins and combines applied, so
8890 the column may come from a joined table, and an inner join's dropped rows do not
8891 count. Filters are not applied.
8892  
8893 =head3 API SPECIFICATION
8894  
8895 =head4 INPUT
8896  
8897 ?l= string Required. Data source spec ("table:name", "path:/abs/path", etc.).
8898 ?col= string Required. Column name to compute the range for.
8899 ?j= string Optional, repeatable. Join spec "<spec>|<lkey>|<rkey>".
8900 ?jtype= string Optional. left (default), inner or outer.
8901 ?c= string Optional, repeatable. Source spec to stack under the view.
8902  
8903 =head4 OUTPUT
8904  
8905 200 application/json { "min": "YYYY-MM-DD", "max": "YYYY-MM-DD" }
8906 200 application/json { "min": null, "max": null } (no parseable dates)
8907 400 application/json { "error": "..." }
8908  
8909 =head3 FORMAL SPECIFICATION
8910  
8911 range_api == lambda self .
8912 let recs = _run_pipeline_source().records in
8913 pre param('l') /= '' /\ col /= ''
8914 post render_json({ min: min_date(recs, col), max: max_date(recs, col) })
8915  
8916 =cut
8917  
8918 54 ▶sub range_api ($self) {
Subroutine range_api: called CC=13 SCAR=25.7
Pod documented
8919 54 ▶ my $spec_str = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
531
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
8920 54 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
513
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
8921 54 ▶ return $self->render(json => { error => 'l required' }, status => 400) unless length $spec_str;
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec_str 2 52 54
8922 52 ▶ return $self->render(json => { error => 'col required' }, status => 400) unless length $col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 2 50 52
8923  
8924 # The pipeline, so that the range is of the rows the page shows. A join
8925 # that cannot be built croaks; this route answers "no range" instead.
8926 50 my (undef, $records, $columns) = eval { $self->_run_pipeline_source };
8927 return $self->render(json => { min => undef, max => undef })
8928 50 ▶ unless $records && grep { $_ eq $col } @$columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $records and grep {$_ eq $col;} @$columns 5 45 50
Condition100%3/3
$records and grep {$_ eq $col;} @$columns
!ll&&!rl&&r
2345
MC/DC100%2/2
$records and grep {$_ eq $col;} @$columns
$records grep {$_ eq $col;} @$columns
Truth table100%3/3
A and B
A= $recordsB= grep {$_ eq $col;} @$columns
ABresult
0X0
100
111
8929  
8930 45 my $mdy_re = qr|\A(\d{1,2})/(\d{1,2})/(\d{4})\z|;
8931 45 my @dates;
8932 45 for my $rec (@$records) {
8933 254 ▶ my $v = $rec->{$col} // '';
Condition100%2/2
$rec->{$col} // ""
l!l
2531
MC/DC100%1/1
$rec->{$col} // ""
$rec->{$col}
Truth table100%2/2
A
A= $rec->{$col}
Aresult
11
00
8934 254 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 1 253 254
8935 # A timestamp counts as the day it names, as the filters read it.
8936 253 ▶ if (defined(my $day = (Database::BI::Util::iso_datetime($v))[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $day = (Database::BI::Util::iso_datetime($v))[0])) { } 13 240 253
elsif (my(@m) = $v =~ /$mdy_re/u) { } 0 240 240
8937 13 push @dates, $day;
8938 } elsif (my @m = ($v =~ $mdy_re)) {
8939 0 push @dates, sprintf '%04d-%02d-%02d', $m[2], $m[0], $m[1];
8940 }
8941 }
8942  
8943 45 ▶ return $self->render(json => { min => undef, max => undef }) unless @dates;
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 40 5 45
8944 5 @dates = sort @dates;
8945 5 $self->render(json => { min => $dates[0], max => $dates[-1] });
8946 }
8947  
8948 =head2 join_tables
8949  
8950 C<GET /join> -- Perform one or more left joins and render the merged table.
8951  
8952 =head3 API SPECIFICATION
8953  
8954 =head4 INPUT
8955  
8956 ?l= string Required. Left table spec: "table:name", "table:file/table"
8957 or "path:/abs/path". Returns 404 if unresolvable.
8958 ?j= string (repeatable) Join step: "<right-spec>|<left-key>|<right-key>".
8959 Invalid or non-existent steps are silently skipped.
8960 ?f= string (repeatable) Result filter: "col:op:val".
8961  
8962 =head4 OUTPUT
8963  
8964 Renders C<dashboard.html.tt> with the merged/filtered result set.
8965  
8966 =head3 MESSAGES
8967  
8968 error_table_open -- Left table fetch threw (re-renders home with error).
8969  
8970 =head3 FORMAL SPECIFICATION
8971  
8972 join_tables == lambda self .
8973 let left = open_spec(param('l')) in
8974 pre left /= undef
8975 let joined = fold(_left_join, left, param_list('j')) in
8976 let filtered = fold(_apply_filter_spec, joined, param_list('f')) in
8977 post render(dashboard, records: filtered)
8978  
8979 =head3 PSEUDOCODE
8980  
8981 1. Parse and open left table spec; 404 on failure.
8982 2. For each j= param (in order):
8983 a. Parse right-spec, left-key, right-key from "|"-split.
8984 b. Verify left-key exists in current column list.
8985 c. Open right table; skip step on any error.
8986 d. Verify right-key exists in right column list.
8987 e. Call _left_join; update records and columns.
8988 3. Apply all f= filters via _apply_filter_spec.
8989 4. Build stable table key for localStorage ("join:left:right1:...").
8990 5. Render dashboard.
8991  
8992 =head3 EXAMPLE
8993  
8994 GET /join?l=table:sales&j=table:products|product|name
8995 -> merged left join of sales and products on product/name columns
8996  
8997 =cut
8998  
8999 54 ▶sub join_tables ($self) {
Subroutine join_tables: called CC=30 SCAR=34.9
Pod documented
9000 # Rows stacked from other files cannot be paged from one backend; the
9001 # combine route runs joins and combines together. c= also carries calculated
9002 # fields, which do not stack anything and stay on this page.
9003 return $self->combine_tables
9004 54 ▶ if @{ $self->every_param('c') } > $self->_calc_specs_of_request;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->every_param("c");} > $self->_calc_specs_of_request 0 54 54
9005  
9006 54 my ($platform, $language) = $self->_resolve_template;
9007  
9008 54 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
531
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
9009 54 my ($left_src, $left_label) = $self->_open_spec($left_spec);
9010 54 ▶ return $self->reply->not_found unless $left_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $left_src 4 50 54
9011  
9012 50 my @join_specs = @{ $self->every_param('j') };
9013 50 my @summaries;
9014  
9015 # Build the join chain: each step wraps the previous source in a
9016 # Database::Join. Data is not fetched until after the loop.
9017 50 ▶ my $max_rows = $self->app->config('join_max_rows') // $DEFAULT_JOIN_MAX_ROWS;
Condition33%1/3
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
l!l&&r!l&&!r
0500
MC/DC0%0/2
$self->app->config("join_max_rows") // $DEFAULT_JOIN_MAX_ROWS
$self->app->config("join_max_rows") $DEFAULT_JOIN_MAX_ROWS
Truth table33%1/3
A // B
A= $self->app->config("join_max_rows")B= $DEFAULT_JOIN_MAX_ROWS
ABresult
1X1
011
000
9018 50 ▶ my $join_type = $self->param('jtype') // 'left';
Condition100%2/2
$self->param("jtype") // "left"
l!l
248
MC/DC100%1/1
$self->param("jtype") // "left"
$self->param("jtype")
Truth table100%2/2
A
A= $self->param("jtype")
Aresult
11
00
9019 50 ▶ $join_type = 'left' unless $join_type =~ /\A(?:left|inner|outer)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $join_type =~ /\A(?:left|inner|outer)\z/u 1 49 50
9020 50 my $src = $left_src;
9021 50 my @schema_warnings;
9022 50 for my $jspec (@join_specs) {
9023 9 my ($right_spec, $left_key, $right_key) = split /\|/, $jspec, 3;
9024 9 ▶ next unless defined $right_spec && defined $left_key && defined $right_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $right_spec and defined $left_key and defined $right_key 2 7 9
Condition66%4/6
defined $right_spec and defined $left_key and defined $right_key
!ll&&!rl&&r
207
defined $right_spec and defined $left_key
!ll&&!rl&&r
027
MC/DC33%1/3
defined $right_spec and defined $left_key and defined $right_key
defined $right_spec defined $left_key defined $right_key
Truth table50%2/4
A and B and C
A= defined $right_specB= defined $left_keyC= defined $right_key
ABCresult
0XX0
10X0
1100
1111
9025  
9026 7 my $cur_cols = $src->columns;
9027 7 ▶ next unless $cur_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur_cols 0 7 7
9028 7 ▶ next unless +{ map { $_ => 1 } @$cur_cols }->{$left_key};
Branch: 1 branch
BranchTrueFalseTotal
unless {map({$_, 1;} @$cur_cols)}->{$left_key} 0 7 7
9029  
9030 7 my ($right_src, $right_label) = $self->_open_spec($right_spec);
9031 7 ▶ next unless $right_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_src 1 6 7
9032  
9033 6 my $right_cols = $right_src->columns;
9034 6 ▶ next unless $right_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $right_cols 0 6 6
9035 6 ▶ next unless +{ map { $_ => 1 } @$right_cols }->{$right_key};
Branch: 1 branch
BranchTrueFalseTotal
unless {map({$_, 1;} @$right_cols)}->{$right_key} 0 6 6
9036  
9037 # Types are compared while the two sides are still separate sources;
9038 # after the join is built the mismatched columns are already published
9039 # under different names. See _schema_type_warnings.
9040 6 push @schema_warnings, $self->_schema_type_warnings(
9041 $src, $right_src, $left_label, $right_label, $left_key, $right_key);
9042  
9043 6 require Database::Join;
9044 0 ▶ local $SIG{__WARN__} = sub { push @schema_warnings, $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9045 6 ▶ $src = Database::Join->new(
Branch: 2 branches
BranchTrueFalseTotal
$left_key ne $right_key ? : 0 6 6
$right_label ? : 6 0 6
9046 databases => [$src, $right_src],
9047 join_column => $left_key,
9048 backend => 'auto',
9049 max_array_rows => $max_rows,
9050 join_type => $join_type,
9051 ($left_key ne $right_key ? (join_map => {1 => $right_key}) : ()),
9052 ($right_label ? (collision_prefix => {1 => $right_label}) : ()),
9053 );
9054 6 push @summaries, { label => $right_label, left_key => $left_key, right_key => $right_key };
9055 }
9056  
9057 50 my ($page, $per_page, $offset) = $self->_pagination;
9058 50 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 50 50
9059 50 my $sort = $self->param('sort');
9060 50 ▶ my $dir = ($self->param('dir') // 'asc') =~ /\Adesc\z/i ? 'desc' : 'asc';
Branch: 1 branch
BranchTrueFalseTotal
($self->param('dir') // 'asc') =~ /\Adesc\z/iu ? : 1 49 50
Condition100%2/2
$self->param("dir") // "asc"
l!l
149
MC/DC100%1/1
$self->param("dir") // "asc"
$self->param("dir")
Truth table100%2/2
A
A= $self->param("dir")
Aresult
11
00
9061  
9062 50 my ($records, $has_next);
9063 50 eval {
9064 50 ($records, $has_next) = $self->_fetch_page($src,
9065 { per_page => $per_page, offset => $offset,
9066 dedup => $dedup, sort_col => $sort,
9067 sort_dir => $dir, calc_spec_key => $left_spec });
9068 };
9069 50 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 50 50
9070 0 return $self->render(
9071 template => "$platform/$language/home",
9072 handler => 'tt',
9073 format => 'html',
9074 tables => [],
9075 title => 'Error',
9076 error => $self->_i18n('error_table_open', $left_label, _tidy_error($@)),
9077 language => $language,
9078 );
9079 }
9080 # A cross-row spec that could not be honoured has already rendered its 400
9081 # inside _fetch_page; the page must not be drawn over it.
9082 50 ▶ return if $self->stash('_cross_row_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_cross_row_error") 1 49 50
9083  
9084 49 ▶ $records //= [];
Condition50%1/2
$records //= []
l!l
490
MC/DC0%0/1
$records //= []
$records
Truth table50%1/2
A
A= $records
Aresult
11
00
9085 49 my $page_columns;
9086 49 ($records, $page_columns) = $self->_page_columns($src, $records, $left_spec);
9087 49 ▶ return unless $page_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_columns 2 47 49
9088 47 my @columns = @$page_columns;
9089  
9090 47 ▶ if (defined $sort && !grep { $_ eq $sort } @columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort and not grep({$_ eq $sort;} @columns)) 1 46 47
Condition100%3/3
defined $sort and not grep({$_ eq $sort;} @columns)
!ll&&!rl&&r
4511
MC/DC100%2/2
defined $sort and not grep({$_ eq $sort;} @columns)
defined $sort not grep({$_ eq $sort;} @columns)
Truth table100%3/3
A and not(B)
A= defined $sortB= not grep({$_ eq $sort;} @columns)
ABresult
0X0
100
111
9091 1 carp $self->_i18n('error_sort_column', $sort);
9092 }
9093  
9094 47 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
9095  
9096 47 my $title = $left_label;
9097 47 ▶ $title .= ' + ' . join(' + ', map { $_->{label} } @summaries) if @summaries;
Branch: 1 branch
BranchTrueFalseTotal
if @summaries 6 41 47
9098 47 my $table_key = 'join:' . lc($left_label);
9099 47 $table_key .= ':' . lc($_->{label}) for @summaries;
9100  
9101 47 my $page_url = $self->req->url->clone;
9102 47 $page_url->query->remove('page')->remove('per_page');
9103  
9104 47 ▶ $self->render(
Condition50%1/2
$self->param("back2_label") // "Back"
l!l
047
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
9105 template => "$platform/$language/dashboard",
9106 handler => 'tt',
9107 format => 'html',
9108 records => $records,
9109 columns => \@columns,
9110 table => $table_key,
9111 title => $title,
9112 back_url => $self->_spec_to_url($left_spec),
9113 back_label => "Back to $left_label",
9114 back2_url => _safe_back_url($self->param('back2')),
9115 back2_label => $self->param('back2_label') // 'Back',
9116 left_spec => $left_spec,
9117 combine_specs => [ $self->_calc_specs_of_request ],
9118 current_joins => \@join_specs,
9119 available_tables => $self->_scan_join_tables,
9120 join_summaries => \@summaries,
9121 filter_specs => $filter_specs,
9122 filters_json => $filters_json,
9123 dedup => $dedup,
9124 export_url => $self->_build_export_url($left_spec, \@join_specs, $filter_specs, undef, $dedup),
9125 schema_warnings => \@schema_warnings,
9126 page => $page,
9127 per_page => $per_page,
9128 has_next => $has_next,
9129 page_base_url => $page_url->to_string,
9130 language => $language,
9131 );
9132 }
9133  
9134 =head2 combine_tables
9135  
9136 C<GET /combine> -- Stack rows from two or more tables vertically into a single
9137 unified view.
9138  
9139 Unlike C</join> (which appends columns from a matching row in a second table),
9140 C</combine> appends the I<rows> from a second table beneath the rows of the
9141 first. All columns from all sources appear as headers; where a row has no
9142 value for a particular column (because that column did not exist in its source
9143 file) the cell is left blank.
9144  
9145 This is equivalent to a SQL C<UNION ALL> across heterogeneous schemas.
9146  
9147 =head3 API SPECIFICATION
9148  
9149 =head4 INPUT
9150  
9151 ?l= string (required) Left table spec: "table:name" or "path:/abs/path".
9152 Returns 404 if unresolvable.
9153 ?c= string (repeatable) Additional table spec to stack beneath the
9154 current result. Invalid or non-existent specs are silently
9155 skipped.
9156 ?f= string (repeatable) Result filter: "col:op:val".
9157  
9158 =head4 OUTPUT
9159  
9160 Renders C<dashboard.html.tt> with the combined result set.
9161  
9162 =head3 MESSAGES
9163  
9164 error_table_open -- Left table fetch threw (re-renders home with error).
9165  
9166 =head3 FORMAL SPECIFICATION
9167  
9168 combine_tables == lambda self .
9169 let left = open_spec(param('l')) in
9170 pre left /= undef
9171 let sources = [left] ++ map(open_spec, param_list('c')) in
9172 let combined = _combine_tables(sources) in
9173 let filtered = fold(_apply_filter_spec, combined, param_list('f')) in
9174 post render(dashboard, records: filtered)
9175  
9176 =head3 EXAMPLE
9177  
9178 GET /combine?l=table:cats&c=table:dogs
9179 -> unified view: all cat and dog rows; Species/Name/Color/Breed/Eye Color
9180 appear for both; Environment (cats-only) and Sex/Fixed (dogs-only)
9181 are blank where the source file lacks that column.
9182  
9183 =cut
9184  
9185 50 ▶sub combine_tables ($self) {
Subroutine combine_tables: called CC=26 SCAR=35.6
Pod documented
9186 50 my ($platform, $language) = $self->_resolve_template;
9187  
9188 50 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
491
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
9189 50 my ($left_src, $left_label) = $self->_open_spec($left_spec);
9190 50 ▶ return $self->reply->not_found unless $left_src;
Branch: 1 branch
BranchTrueFalseTotal
unless $left_src 4 46 50
9191  
9192 # The same join-then-stack pipeline the charts of this view use, so the
9193 # table shows the rows they draw.
9194 46 my (undef, $records, $cols_ref) = $self->_run_pipeline_source;
9195 46 ▶ unless (defined $records) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $records) 0 46 46
9196 0 return $self->render(
9197 template => "$platform/$language/home",
9198 handler => 'tt',
9199 format => 'html',
9200 tables => [],
9201 title => 'Error',
9202 error => $self->_i18n('error_table_open', $left_label, ''),
9203 language => $language,
9204 );
9205 }
9206 46 my @join_specs = @{ $self->every_param('j') };
9207 46 my @combine_specs = @{ $self->every_param('c') };
9208 46 ▶ my @summaries = (@{ $self->stash('pipeline_joined') // [] },
Condition50%1/2
$self->stash("pipeline_joined") // []
l!l
460
MC/DC0%0/1
$self->stash("pipeline_joined") // []
$self->stash("pipeline_joined")
Truth table50%1/2
A
A= $self->stash("pipeline_joined")
Aresult
11
00
9209 46 ▶ @{ $self->stash('pipeline_combined') // [] });
Condition50%1/2
$self->stash("pipeline_combined") // []
l!l
460
MC/DC0%0/1
$self->stash("pipeline_combined") // []
$self->stash("pipeline_combined")
Truth table50%1/2
A
A= $self->stash("pipeline_combined")
Aresult
11
00
9210  
9211 # Stage 4, for a table of stacked rows as for any other.
9212 46 my $span = $self->_span_calc_spec($cols_ref, ());
9213 46 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 2 44 46
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
4402
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
9214 44 my ($calc_records, $calc_columns) = eval {
9215 44 ▶ $self->_apply_calc_fields($records, $cols_ref, ($span ? { calc => $span->{calc} } : undef))
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 44 44
9216 };
9217 44 ▶ unless ($calc_records) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calc_records) 1 43 44
9218 1 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
9219 1 return $self->render(text => $e, status => 400);
9220 }
9221 43 ($records, $cols_ref) = ($calc_records, $calc_columns);
9222  
9223 # Apply the filters in memory before paginating (reduce-before-paginate).
9224 43 my @columns;
9225 43 ▶ return unless $self->_reduce_or_render(\$records, \$cols_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$cols_ref) 1 42 43
9226 42 @columns = @$cols_ref;
9227 # _reduce_or_render has removed duplicates for d=1; $dedup is for the page.
9228 42 ▶ my $dedup = $self->param('d') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('d') ? : 0 42 42
9229  
9230 # In-memory sort for /combine (no single backend to delegate to).
9231 42 my $sort = $self->param('sort');
9232 42 ▶ my $dir = ($self->param('dir') // 'asc') =~ /\Adesc\z/i ? 'desc' : 'asc';
Branch: 1 branch
BranchTrueFalseTotal
($self->param('dir') // 'asc') =~ /\Adesc\z/iu ? : 0 42 42
Condition50%1/2
$self->param("dir") // "asc"
l!l
042
MC/DC0%0/1
$self->param("dir") // "asc"
$self->param("dir")
Truth table50%1/2
A
A= $self->param("dir")
Aresult
11
00
9233 42 ▶ if (defined $sort && grep { $_ eq $sort } @columns) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $sort and grep {$_ eq $sort;} @columns) { } 0 42 42
elsif (defined $sort) { } 1 41 42
Condition66%2/3
defined $sort and grep {$_ eq $sort;} @columns
!ll&&!rl&&r
4110
MC/DC0%0/2
defined $sort and grep {$_ eq $sort;} @columns
defined $sort grep {$_ eq $sort;} @columns
Truth table66%2/3
A and B
A= defined $sortB= grep {$_ eq $sort;} @columns
ABresult
0X0
100
111
9234 0 carp $self->_i18n('warn_combine_sort');
9235 # A new array: the backend's own can be read-only, as a SQLite one is.
9236 0 ▶ $records = [ sort { ($a->{$sort} // '') cmp ($b->{$sort} // '') } @$records ];
Condition0%0/4
$Database::BI::Controller::Dashboard::a->{$sort} // ""
l!l
00
$Database::BI::Controller::Dashboard::b->{$sort} // ""
l!l
00
MC/DC0%0/1
$Database::BI::Controller::Dashboard::a->{$sort} // ""
$Database::BI::Controller::Dashboard::a->{$sort}
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{$sort} // ""
$Database::BI::Controller::Dashboard::b->{$sort}
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::a->{$sort}
Aresult
11
00
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::b->{$sort}
Aresult
11
00
9237 0 ▶ $records = [ reverse @$records ] if $dir eq 'desc';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "desc" 0 0 0
9238 } elsif (defined $sort) {
9239 1 carp $self->_i18n('error_sort_column', $sort);
9240 }
9241  
9242 # Server-side pagination: slice the fully-reduced combined set. The page
9243 # below is therefore reduced too, and asks for chips only.
9244 42 my ($page, $per_page, $offset) = $self->_pagination;
9245 42 my $total = scalar @$records;
9246 42 ▶ my $has_next = ($total > $offset + $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$total > $offset + $per_page ? : 0 42 42
9247 42 my @page_recs = @{ _rows_of_page($records, $offset, $per_page) };
9248 42 $records = \@page_recs;
9249 42 $self->stash(page_count => _page_count($total, $per_page));
9250  
9251 42 my (undef, $filter_specs, $filters_json) = $self->_apply_filters($records, { chips_only => 1 });
9252  
9253 42 my $title = $left_label;
9254 42 ▶ $title .= ' + ' . join(' + ', map { $_->{label} } @summaries) if @summaries;
Branch: 1 branch
BranchTrueFalseTotal
if @summaries 4 38 42
9255 42 ▶ my $table_key = (@join_specs ? 'join+combine:' : 'combine:') . lc($left_label);
Branch: 1 branch
BranchTrueFalseTotal
@join_specs ? : 1 41 42
9256 42 $table_key .= ':' . lc($_->{label}) for @summaries;
9257  
9258 42 my $page_url = $self->req->url->clone;
9259 42 $page_url->query->remove('page')->remove('per_page');
9260  
9261 42 ▶ $self->render(
Condition50%2/4
$self->param("back2_label") // "Back"
l!l
042
$self->stash("schema_warnings") // []
l!l
042
MC/DC0%0/1
$self->param("back2_label") // "Back"
$self->param("back2_label")
MC/DC0%0/1
$self->stash("schema_warnings") // []
$self->stash("schema_warnings")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
Truth table50%1/2
A
A= $self->stash("schema_warnings")
Aresult
11
00
9262 template => "$platform/$language/dashboard",
9263 handler => 'tt',
9264 format => 'html',
9265 records => $records,
9266 columns => \@columns,
9267 table => $table_key,
9268 title => $title,
9269 back_url => $self->_spec_to_url($left_spec),
9270 back_label => "Back to $left_label",
9271 back2_url => _safe_back_url($self->param('back2')),
9272 back2_label => $self->param('back2_label') // 'Back',
9273 left_spec => $left_spec,
9274 combine_specs => \@combine_specs,
9275 current_joins => \@join_specs,
9276 available_tables => $self->_scan_join_tables,
9277 join_summaries => \@summaries,
9278 filter_specs => $filter_specs,
9279 filters_json => $filters_json,
9280 dedup => $dedup,
9281 export_url => $self->_build_export_url($left_spec, \@join_specs, $filter_specs, \@combine_specs, $dedup),
9282 schema_warnings => $self->stash('schema_warnings') // [],
9283 page => $page,
9284 per_page => $per_page,
9285 has_next => $has_next,
9286 page_base_url => $page_url->to_string,
9287 language => $language,
9288 );
9289 }
9290  
9291 =head2 export_data
9292  
9293 C<GET /export> -- Stream the current logical view as a browser file download.
9294  
9295 =head3 API SPECIFICATION
9296  
9297 =head4 INPUT
9298  
9299 ?l= string (required) Left table spec.
9300 ?j= string (repeatable) Join steps.
9301 ?f= string (repeatable) Filter specs.
9302 ?format= string "csv" (default), "sqlite", or "json".
9303  
9304 =head4 DOMAIN CONSTRAINTS: ?format=
9305  
9306 Exact lowercase string match.
9307  
9308 =over 4
9309  
9310 =item Valid partitions
9311  
9312 C<csv> (explicit CSV), C<sqlite> (SQLite binary), C<json> (JSON array),
9313 absent/undef (defaults to CSV).
9314  
9315 =item Invalid partitions (all fall back to CSV)
9316  
9317 Any value not in the valid set (e.g. C<SQLITE>, C<Json>).
9318  
9319 =back
9320  
9321 =head4 OUTPUT
9322  
9323 200 text/csv Content-Disposition: attachment; filename=<name>.csv
9324 200 application/vnd.sqlite3 Content-Disposition: attachment; filename=<name>.db
9325 200 application/json Content-Disposition: attachment; filename=<name>.json
9326 404 application/json { "error": "Table not found" }
9327 500 text/plain The SQLite file could not be built (no driver, no
9328 temporary space); the reason goes to the log.
9329  
9330 =head3 MESSAGES
9331  
9332 error_export_sqlite -- the SQLite file could not be built.
9333  
9334 =head3 FORMAL SPECIFICATION
9335  
9336 export_data == lambda self .
9337 let (recs, cols, label) = _run_export_pipeline() in
9338 pre recs /= undef
9339 post if format = 'sqlite' then render_sqlite(recs, cols)
9340 else if format = 'json' then render_json(recs, cols)
9341 else render_csv(recs, cols)
9342  
9343 =head3 EXAMPLE
9344  
9345 GET /export?l=table:sales&format=csv -> downloads sales.csv
9346 GET /export?l=table:sales&format=sqlite -> downloads sales.db
9347 GET /export?l=table:sales&format=json -> downloads sales.json
9348  
9349 =cut
9350  
9351 75 ▶sub export_data ($self) {
Subroutine export_data: called CC=8 SCAR=21.4
Pod documented
9352 75 my ($records, $columns, $left_label) = $self->_run_export_pipeline;
9353 75 ▶ return if $self->stash('_reduce_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_reduce_error") 7 68 75
9354 68 ▶ return $self->reply->not_found unless $records;
Branch: 1 branch
BranchTrueFalseTotal
unless $records 6 62 68
9355  
9356 # Surface schema-type-mismatch warnings in a response header so API
9357 # consumers (and the user via browser dev tools) can see them even
9358 # though exports render no HTML template.
9359 62 ▶ my $warnings = $self->stash('schema_warnings') // [];
Condition50%1/2
$self->stash("schema_warnings") // []
l!l
062
MC/DC0%0/1
$self->stash("schema_warnings") // []
$self->stash("schema_warnings")
Truth table50%1/2
A
A= $self->stash("schema_warnings")
Aresult
11
00
9360 62 ▶ if (@$warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$warnings) 0 62 62
9361 0 my $hdr = join('; ', map { s/\s+/ /gr =~ s/^\s+|\s+$//gr } @$warnings);
9362 0 $self->res->headers->header('X-BI-Warnings' => $hdr);
9363 }
9364  
9365 62 ▶ my $format = $self->param('format') // 'csv';
Condition100%2/2
$self->param("format") // "csv"
l!l
2636
MC/DC100%1/1
$self->param("format") // "csv"
$self->param("format")
Truth table100%2/2
A
A= $self->param("format")
Aresult
11
00
9366 62 (my $safe_name = lc $left_label) =~ s/[^a-z0-9_]+/_/g;
9367  
9368 62 ▶ return $format eq 'sqlite' ? $self->_render_sqlite($records, $columns, $safe_name)
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'sqlite' ? : 3 59 62
$format eq 'json' ? : 3 56 59
9369 : $format eq 'json' ? $self->_render_json($records, $columns, $safe_name)
9370 : $self->_render_csv($records, $columns, $safe_name);
9371 }
9372  
9373 =head2 export_write
9374  
9375 C<POST /export> -- Write the current logical view to a chosen filesystem path.
9376  
9377 =head3 API SPECIFICATION
9378  
9379 =head4 INPUT
9380  
9381 l= string (required) Left table spec.
9382 j= string (repeatable) Join steps.
9383 f= string (repeatable) Filter specs.
9384 dir= string Target directory (must exist; resolved via realpath). Inside
9385 config file_roots and export_dirs when they are set.
9386 filename= string Output filename including extension. Extension determines
9387 format: C<.csv> -> RFC 4180 CSV; C<.sql> -> SQLite;
9388 C<.json> -> JSON array of row objects.
9389  
9390 =head4 DOMAIN CONSTRAINTS: filename=
9391  
9392 The extension check uses C</\.csv\z/i> (CSV), C</\.sql\z/i> (SQLite), or
9393 C</\.json\z/i> (JSON): the C</i> flag makes matching case-insensitive, so
9394 C<.CSV>, C<.SQL>, and C<.JSON> are accepted alongside lowercase forms.
9395 Everything else returns 415.
9396  
9397 Path separator characters (C</> and C<\>) in C<filename> are stripped
9398 first via C<m{([^/\\]+)\z}> -- only the basename is kept, preventing
9399 directory traversal.
9400  
9401 =over 4
9402  
9403 =item Valid partitions
9404  
9405 C<report.csv>, C<report.sql>, C<report.json>, C<REPORT.CSV>, C<report.SQL>.
9406 A single-character stem (C<a.csv>) is also valid.
9407  
9408 =item Invalid partitions (415)
9409  
9410 C<report.txt>, C<report> (no extension), empty string.
9411  
9412 =back
9413  
9414 =head4 OUTPUT
9415  
9416 200 application/json { "saved": "/abs/path/to/file" }
9417 403 application/json { "error": "..." } -- a hidden target; dir outside
9418 file_roots or export_dirs; a name
9419 that is a link, folder or anything
9420 but a plain file; or a request
9421 another site's page made
9422 401 application/json { "error": "..." } -- password_hash set, not logged in
9423 404 application/json { "error": "..." } -- bad dir or table not found
9424 415 application/json { "error": "..." } -- unsupported extension
9425 500 application/json { "error": "..." } -- write failure
9426  
9427 =head3 MESSAGES
9428  
9429 error_dir_not_found -- realpath of dir failed or result is not a directory.
9430 error_ext_required -- filename extension is not .csv, .sql or .json.
9431 error_hidden_target -- the directory, after realpath, or the filename has a
9432 part starting with a dot (~/.config, .vscode/,
9433 .profile.json): those hold settings, and
9434 GET /api/dirs never offers them.
9435 error_outside_roots -- dir is outside config file_roots.
9436 error_outside_export_dirs
9437 -- dir is outside config export_dirs.
9438 error_export_not_file -- the name is already a link, a folder or another
9439 non-file: a link could lead the export anywhere.
9440 error_cross_site, error_host_refused
9441 -- from request_refusal, before this action runs.
9442 error_write_failed -- DBI or filesystem write threw.
9443  
9444 A refusal names the folder as C<dir> spelled it, though the checks are made
9445 on its realpath. The C<saved> path of a success is the realpath: where the
9446 file is.
9447  
9448 The file is written to a temporary file (C<.bi-export-XXXXXXXX>) in C<dir> and
9449 renamed over the name, so a reader never sees half a file and a link made
9450 after the check is replaced, not followed. It gets the replaced file's mode,
9451 or C<0666 & ~umask> for a new one; on failure the temporary file is removed.
9452  
9453 =head3 FORMAL SPECIFICATION
9454  
9455 export_write == lambda self .
9456 let dir = realpath(param('dir')) in
9457 let file = dir / strip_path(param('filename')) in
9458 pre is_dir(dir) /\ ext(file) in {csv, sql}
9459 let (recs, cols, _) = _run_export_pipeline() in
9460 pre recs /= undef
9461 post spurt(file, serialise(recs, cols))
9462 /\ render_json({ saved: file.to_string })
9463  
9464 =head3 EXAMPLE
9465  
9466 POST /export l=table:sales dir=/home/user/exports filename=report.csv
9467 -> { "saved": "/home/user/exports/report.csv" }
9468  
9469 =cut
9470  
9471 26 ▶sub export_write ($self) {
Subroutine export_write: called CC=32 SCAR=37.5
Pod documented
9472 26 ▶ my $dir = $self->param('dir') // '';
Condition50%1/2
$self->param("dir") // ""
l!l
260
MC/DC0%0/1
$self->param("dir") // ""
$self->param("dir")
Truth table50%1/2
A
A= $self->param("dir")
Aresult
11
00
9473 26 ▶ my $filename = $self->param('filename') // '';
Condition50%1/2
$self->param("filename") // ""
l!l
260
MC/DC0%0/1
$self->param("filename") // ""
$self->param("filename")
Truth table50%1/2
A
A= $self->param("filename")
Aresult
11
00
9474  
9475 26 ▶ if (my $refusal = _outside_roots($self, $dir) // _outside_export_dirs($self, $dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = _outside_roots($self, $dir) // _outside_export_dirs($self, $dir)) 0 26 26
Condition33%1/3
_outside_roots($self, $dir) // _outside_export_dirs($self, $dir)
l!l&&r!l&&!r
0026
MC/DC0%0/2
_outside_roots($self, $dir) // _outside_export_dirs($self, $dir)
_outside_roots($self, $dir) _outside_export_dirs($self, $dir)
Truth table33%1/3
A // B
A= _outside_roots($self, $dir)B= _outside_export_dirs($self, $dir)
ABresult
1X1
011
000
9476 0 return $self->render(json => { error => $refusal }, status => 403);
9477 }
9478 26 my $dest_dir = eval { Mojo::File->new($dir)->realpath };
9479 26 ▶ unless (defined $dest_dir && -d $dest_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dest_dir and -d $dest_dir) 2 24 26
Condition100%3/3
defined $dest_dir and -d $dest_dir
!ll&&!rl&&r
1124
MC/DC100%2/2
defined $dest_dir and -d $dest_dir
defined $dest_dir -d $dest_dir
Truth table100%3/3
A and B
A= defined $dest_dirB= -d $dest_dir
ABresult
0X0
100
111
9480 2 return $self->render(
9481 json => { error => $self->_i18n('error_dir_not_found', $dir) },
9482 status => 404,
9483 );
9484 }
9485  
9486 # A NUL is not part of any file name, only a way of hiding the real extension.
9487 24 ▶ $filename = '' if $filename =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /\0/u 2 22 24
9488  
9489 # Strip any path separators the browser might include.
9490 24 ($filename) = $filename =~ m{([^/\\]+)\z};
9491  
9492 # A dot-folder or dot-file holds some program's settings (~/.config, a
9493 # .vscode/settings.json), which a .json export could replace. The folder
9494 # list the panel offers (GET /api/dirs) leaves them out too.
9495 # The checks read the resolved folder; a refusal names the one the reader
9496 # asked for, as every other message does (macOS's tempdir is under /var,
9497 # a link to /private/var, so the two differ there).
9498 24 my $asked = Mojo::File->new($dir);
9499 24 ▶ my @names = (File::Spec->splitdir($dest_dir->to_string), $filename // '');
Condition100%2/2
$filename // ""
l!l
213
MC/DC100%1/1
$filename // ""
$filename
Truth table100%2/2
A
A= $filename
Aresult
11
00
9500 24 ▶ if (grep { /\A\./ && $_ ne '.' && $_ ne '..' } @names) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\A\./u and $_ ne "." and $_ ne "..";} @names) 0 24 24
Condition83%5/6
/\A\./u && $_ ne '.' && $_ ne '..'
!ll&&!rl&&r
9510
/\A\./u and $_ ne "."
!ll&&!rl&&r
9411
MC/DC0%0/3
/\A\./u && $_ ne '.' && $_ ne '..'
/\A\./u $_ ne "." $_ ne '..'
Truth table75%3/4
A and B && C
A= /\A\./uB= $_ ne "."C= $_ ne '..'
ABCresult
0XX0
10X0
1100
1111
9501 0 ▶ return $self->render(
Condition0%0/2
$filename // ""
l!l
00
MC/DC0%0/1
$filename // ""
$filename
Truth table0%0/2
A
A= $filename
Aresult
11
00
9502 json => { error => $self->_i18n('error_hidden_target', $asked->child($filename // '')->to_string) },
9503 status => 403,
9504 );
9505 }
9506 24 my $format;
9507 24 ▶ if ($filename && $filename =~ /\.csv\z/i) { $format = 'csv'; }
Branch: 3 branches
BranchTrueFalseTotal
if ($filename and $filename =~ /\.csv\z/iu) { } 16 8 24
elsif ($filename and $filename =~ /\.sql\z/iu) { } 1 7 8
elsif ($filename and $filename =~ /\.json\z/iu) { } 0 7 7
Condition100%3/3
$filename and $filename =~ /\.csv\z/iu
!ll&&!rl&&r
3516
MC/DC100%2/2
$filename and $filename =~ /\.csv\z/iu
$filename $filename =~ /\.csv\z/iu
Truth table100%3/3
A and B
A= $filenameB= $filename =~ /\.csv\z/iu
ABresult
0X0
100
111
9508 1 ▶ elsif ($filename && $filename =~ /\.sql\z/i) { $format = 'sqlite'; }
Condition100%3/3
$filename and $filename =~ /\.sql\z/iu
!ll&&!rl&&r
341
MC/DC100%2/2
$filename and $filename =~ /\.sql\z/iu
$filename $filename =~ /\.sql\z/iu
Truth table100%3/3
A and B
A= $filenameB= $filename =~ /\.sql\z/iu
ABresult
0X0
100
111
9509 0 ▶ elsif ($filename && $filename =~ /\.json\z/i) { $format = 'json'; }
Condition66%2/3
$filename and $filename =~ /\.json\z/iu
!ll&&!rl&&r
340
MC/DC0%0/2
$filename and $filename =~ /\.json\z/iu
$filename $filename =~ /\.json\z/iu
Truth table66%2/3
A and B
A= $filenameB= $filename =~ /\.json\z/iu
ABresult
0X0
100
111
9510 else {
9511 7 return $self->render(
9512 json => { error => $self->_i18n('error_ext_required') },
9513 status => 415,
9514 );
9515 }
9516  
9517 17 my ($records, $columns, $left_label) = $self->_run_export_pipeline;
9518 17 ▶ return if $self->stash('_reduce_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_reduce_error") 3 14 17
9519 14 ▶ return $self->render(json => { error => 'Table not found' }, status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless $records 2 12 14
9520 unless $records;
9521  
9522 12 my $dest = $dest_dir->child($filename);
9523 # A link (or a folder, a pipe) already at the name is not replaced: written
9524 # through, a link inside the folders would put the export wherever it
9525 # points, outside file_roots or export_dirs, or into a dot-file.
9526 12 ▶ if (-l $dest || (-e $dest && !-f $dest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $dest or -e $dest and not -f $dest) 0 12 12
Condition50%3/6
-l $dest or -e $dest and not -f $dest
l!l&&r!l&&!r
0012
-e $dest and not -f $dest
!ll&&!rl&&r
930
MC/DC0%0/3
-l $dest or -e $dest and not -f $dest
-l $dest -e $dest not -f $dest
Truth table50%2/4
A or B and not(C)
A= -l $destB= -e $destC= not -f $dest
ABCresult
1XX1
0111
00X0
0100
9527 0 return $self->render(json => { error => sprintf(_message_text('error_export_not_file'), $asked->child($filename)->to_string) }, status => 403);
9528 }
9529 12 my $tmp;
9530 12 my $ok = eval {
9531 12 ▶ my $bytes = $format eq 'csv' ? _serialize_csv($records, $columns)
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'csv' ? : 11 1 12
$format eq 'json' ? : 0 1 1
9532 : $format eq 'json' ? encode_json($records)
9533 : $self->_write_sqlite_db($records, $columns);
9534 # Written beside it and renamed over it: a rename replaces whatever
9535 # is at the name, a link made since the check included, and never
9536 # follows it, and a reader never sees half a file.
9537 12 (my $fh, $tmp) = tempfile('.bi-export-XXXXXXXX', DIR => $dest_dir->to_string, UNLINK => 0);
9538 12 binmode $fh;
9539 12 ▶ print {$fh} $bytes or croak "$tmp: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $bytes or croak("${tmp}: $!") 12 0 12
9540 12 ▶ close $fh or croak "$tmp: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("${tmp}: $!") 12 0 12
9541 # tempfile makes it 0600: give it the mode of the file it replaces, as
9542 # writing over that one did, or the mode a new file gets.
9543 12 my @old = stat $dest->to_string;
9544 12 ▶ chmod(@old ? $old[2] & oct('7777') : 0666 & ~umask(), $tmp);
Branch: 1 branch
BranchTrueFalseTotal
@old ? : 3 9 12
9545 12 ▶ rename $tmp, $dest->to_string or croak "$dest: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $dest->to_string or croak("${dest}: $!") 12 0 12
9546 12 undef $tmp;
9547 12 1;
9548 };
9549 12 my $err = $@;
9550 12 ▶ unlink $tmp if defined $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 0 12 12
9551 12 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 12 12
9552 json => { error => $self->_i18n('error_write_failed', _tidy_error($err)) },
9553 status => 500,
9554 ) unless $ok;
9555  
9556 12 $self->render(json => { saved => $dest->to_string });
9557 }
9558  
9559 =head2 dirs_api
9560  
9561 C<GET /api/dirs> -- Return a JSON directory listing for the export panel.
9562  
9563 =head3 API SPECIFICATION
9564  
9565 =head4 INPUT
9566  
9567 ?path= string Directory to list. Default: the first config export_dirs
9568 entry, else $HOME if it is inside file_roots, else the first
9569 root. Returns 404 when not a directory. Hidden entries
9570 (names starting with ".") are excluded.
9571  
9572 =head4 OUTPUT
9573  
9574 200 application/json
9575 {
9576 "path": "/abs/path",
9577 "parent": "/abs/parent" or null (at the filesystem root, or when the
9578 parent is outside file_roots or export_dirs),
9579 "dirs": [ { "name": "alpha", "path": "/abs/path/alpha" }, ... ]
9580 }
9581  
9582 403 application/json { "error": "..." } -- path outside file_roots or
9583 export_dirs, whether or not it
9584 exists
9585 404 application/json { "error": "Not a directory" }
9586  
9587 =head3 MESSAGES
9588  
9589 error_outside_roots, error_outside_export_dirs -- the 403 above.
9590  
9591 =head3 FORMAL SPECIFICATION
9592  
9593 dirs_api == lambda self .
9594 let dir = realpath(param('path') // first(export_dirs) // first_root) in
9595 pre inside(dir, file_roots) /\ inside(dir, export_dirs) /\ is_dir(dir)
9596 post render_json({ path: dir, parent: parent(dir), dirs: subdirs(dir) })
9597  
9598 =head3 EXAMPLE
9599  
9600 GET /api/dirs?path=/home/user -> {"path":"/home/user","parent":"/home","dirs":[...]}
9601  
9602 =cut
9603  
9604 4 ▶sub dirs_api ($self) {
Subroutine dirs_api: called CC=10 SCAR=26.7
Pod documented
9605 4 ▶ my $raw = $self->param('path') // (_export_dirs($self))[0] // _first_root($self);
Condition66%4/6
($self->param("path") // (_export_dirs($self))[0]) // _first_root($self)
l!l&&r!l&&!r
310
$self->param("path") // (_export_dirs($self))[0]
l!l&&r!l&&!r
301
MC/DC0%0/3
($self->param("path") // (_export_dirs($self))[0]) // _first_root($self)
$self->param("path") (_export_dirs($self))[0] _first_root($self)
Truth table50%2/4
A // B // C
A= $self->param("path")B= (_export_dirs($self))[0]C= _first_root($self)
ABCresult
1XX1
01X1
0011
0000
9606 4 ▶ if (my $refusal = _outside_roots($self, $raw) // _outside_export_dirs($self, $raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = _outside_roots($self, $raw) // _outside_export_dirs($self, $raw)) 0 4 4
Condition33%1/3
_outside_roots($self, $raw) // _outside_export_dirs($self, $raw)
l!l&&r!l&&!r
004
MC/DC0%0/2
_outside_roots($self, $raw) // _outside_export_dirs($self, $raw)
_outside_roots($self, $raw) _outside_export_dirs($self, $raw)
Truth table33%1/3
A // B
A= _outside_roots($self, $raw)B= _outside_export_dirs($self, $raw)
ABresult
1X1
011
000
9607 0 return $self->render(json => { error => $refusal }, status => 403);
9608 }
9609 4 my $dir = eval { Mojo::File->new($raw)->realpath };
9610 4 ▶ return $self->render(json => { error => 'Not a directory' }, status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and -d $dir 1 3 4
Condition66%2/3
defined $dir and -d $dir
!ll&&!rl&&r
103
MC/DC50%1/2
defined $dir and -d $dir
defined $dir -d $dir
Truth table66%2/3
A and B
A= defined $dirB= -d $dir
ABresult
0X0
100
111
9611 unless defined $dir && -d $dir;
9612  
9613 3 my $listing = $self->_list_dir($dir, 0);
9614 3 my $parent = $dir->dirname;
9615 3 ▶ my $up = $parent->to_string ne $dir->to_string && !_outside_roots($self, $parent->to_string)
Condition33%2/6
$parent->to_string ne $dir->to_string && !_outside_roots($self, $parent->to_string) && !_outside_export_dirs($self, $parent->to_string)
!ll&&!rl&&r
003
$parent->to_string ne $dir->to_string and not _outside_roots($self, $parent->to_string)
!ll&&!rl&&r
003
MC/DC0%0/3
$parent->to_string ne $dir->to_string && !_outside_roots($self, $parent->to_string) && !_outside_export_dirs($self, $parent->to_string)
$parent->to_string ne $dir->to_string not _outside_roots($self, $parent->to_string) !_outside_export_dirs($self, $parent->to_string)
Truth table25%1/4
A and not(B) && not(C)
A= $parent->to_string ne $dir->to_stringB= not _outside_roots($self, $parent->to_string)C= !_outside_export_dirs($self, $parent->to_string)
ABCresult
0XX0
10X0
1100
1111
9616 && !_outside_export_dirs($self, $parent->to_string);
9617  
9618 $self->render(json => {
9619 path => $dir->to_string,
9620 parent => ($up ? $parent->to_string : undef),
9621 dirs => $listing->{dirs},
9622 3 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$up ? : 3 0 3
9623 }
9624  
9625 =head2 stat_api
9626  
9627 C<GET /api/stat> -- Return filesystem metadata for a file path.
9628  
9629 Used by the home page tooltip to show modification time and size for recently
9630 opened files without a page reload.
9631  
9632 =head3 API SPECIFICATION
9633  
9634 =head4 INPUT
9635  
9636 ?path= string Absolute path to query. Returns HTTP 400 when absent.
9637  
9638 =head4 OUTPUT
9639  
9640 200 application/json (file exists)
9641 { "exists": true, "path": "/resolved/path", "mtime": 1700000000, "size": 4096 }
9642  
9643 200 application/json (file does not exist)
9644 { "exists": false, "path": "/original/path" }
9645  
9646 400 application/json { "error": "\"path\" parameter is required" }
9647  
9648 C<mtime> is Unix epoch seconds. C<size> is bytes. A missing/unresolvable
9649 path returns HTTP 200 with C<exists: false> (not 404) so the UI can
9650 distinguish "file was deleted" from a request error.
9651  
9652 =head3 MESSAGES
9653  
9654 error_path_required -- the "path" query parameter was not supplied.
9655  
9656 =head3 FORMAL SPECIFICATION
9657  
9658 stat_api == lambda self .
9659 let path = param('path') in
9660 pre path /= undef
9661 let file = realpath(path) in
9662 post if is_file(file) then
9663 render_json({ exists: true, mtime: mtime(file), size: size(file) })
9664 else render_json({ exists: false })
9665  
9666 =head3 EXAMPLE
9667  
9668 GET /api/stat?path=/data/sales.csv
9669 -> { "exists": true, "path": "/data/sales.csv", "mtime": 1700000000, "size": 1234 }
9670  
9671 GET /api/stat?path=/deleted.csv
9672 -> { "exists": false, "path": "/deleted.csv" }
9673  
9674 =cut
9675  
9676 12 ▶sub stat_api ($self) {
Subroutine stat_api: called CC=9 SCAR=22.0
Pod documented
9677 12 my $path = $self->param('path');
9678 12 ▶ unless (defined $path && length $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and length $path) 3 9 12
Condition100%3/3
defined $path and length $path
!ll&&!rl&&r
219
MC/DC100%2/2
defined $path and length $path
defined $path length $path
Truth table100%3/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
9679 3 return $self->render(
9680 json => { error => $self->_i18n('error_path_required') },
9681 status => 400,
9682 );
9683 }
9684  
9685 9 my $file = eval { Mojo::File->new($path)->realpath };
9686 # Restrict to files with a supported data extension so stat_api cannot be
9687 # used as a filesystem oracle to probe /etc/shadow, /root/.ssh/, etc.; and,
9688 # with file_roots, to files inside them, said the same way.
9689 9 ▶ return $self->render(json => { exists => \0, path => $path })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -f $file and $file->basename =~ /$EXT_RE/u and not _outside_roots($self, $path) 4 5 9
Condition88%8/9
defined $file and -f $file and $file->basename =~ /$EXT_RE/u and not _outside_roots($self, $path)
!ll&&!rl&&r
405
defined $file and -f $file and $file->basename =~ /$EXT_RE/u
!ll&&!rl&&r
315
defined $file and -f $file
!ll&&!rl&&r
126
MC/DC75%3/4
defined $file and -f $file and $file->basename =~ /$EXT_RE/u and not _outside_roots($self, $path)
defined $file -f $file $file->basename =~ /$EXT_RE/u not _outside_roots($self, $path)
Truth table80%4/5
A and B and C and not(D)
A= defined $fileB= -f $fileC= $file->basename =~ /$EXT_RE/uD= not _outside_roots($self, $path)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
9690 unless defined $file && -f $file && $file->basename =~ $EXT_RE && !_outside_roots($self, $path);
9691  
9692 5 my @s = stat $file->to_string;
9693  
9694 # For SQLite files, count user tables so the home-page tooltip can show
9695 # "SQLite database - N tables". Silently omit the count if the file is
9696 # not actually a SQLite file (e.g. a Berkeley DB .db file).
9697 5 my $table_count;
9698 5 ▶ if ($file->basename =~ /\.(?:sql|sqlite|sqlite3|db)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->basename =~ /\.(?:sql|sqlite|sqlite3|db)\z/iu) 1 4 5
9699 1 eval {
9700 1 require Database::Join::Catalog;
9701 1 $table_count = scalar @{ Database::Join::Catalog->tables(file => $file->to_string) };
9702 };
9703 }
9704  
9705 5 ▶ $self->render(json => {
Branch: 1 branch
BranchTrueFalseTotal
defined $table_count ? : 1 4 5
9706 exists => \1,
9707 path => $file->to_string,
9708 mtime => $s[9],
9709 size => $s[7],
9710 (defined $table_count ? (table_count => $table_count) : ()),
9711 });
9712 }
9713  
9714 =head2 upload_file
9715  
9716 C<POST /upload> -- Accept a drag-and-dropped data file and return a redirect URL.
9717  
9718 The file is saved under its original filename in a managed subdirectory of
9719 C<< <uploads_dir>/ >>. The subdirectory name is randomised so that
9720 concurrent uploads of files with the same name do not collide.
9721  
9722 =head3 API SPECIFICATION
9723  
9724 =head4 INPUT
9725  
9726 Multipart form upload, field name: C<file>.
9727  
9728 file upload Supported extensions: csv, db, sql, xml, tsv, psv.
9729  
9730 =head4 OUTPUT
9731  
9732 200 application/json { "url": "/open?path=/abs/path/file.csv", "path": "/abs/path/file.csv" }
9733 400 application/json { "error": "No file received" }
9734 413 application/json { "error": "File too large to upload (limit: 50 MB)" }
9735 415 application/json { "error": "Unsupported file type. Accepted: ..." }
9736 500 application/json { "error": "Could not save uploaded file" }
9737  
9738 =head3 MESSAGES
9739  
9740 error_upload_none -- no file was received in the multipart upload.
9741 error_upload_ext -- the file's extension is not in the supported list.
9742 error_upload_too_large -- the upload body exceeded MAX_UPLOAD_MIB (50 MiB).
9743 error_upload_save -- move_to failed or the destination file was not
9744 created (I/O error, full disk, permission error).
9745  
9746 =head3 IMPLEMENTATION NOTES
9747  
9748 The C<move_to> call is wrapped with the C<eval { ...; 1 }> idiom (not
9749 C<eval {}> + C<if ($@)>). Mojolicious dispatch and DESTROY-phase evals can
9750 leave C<$@> set before the action runs; checking C<if ($@)> after a new eval
9751 may fire falsely on that stale value. C<eval { move_to(); 1 }> returns 1 on
9752 success and C<undef> on exception, so the result is independent of any prior
9753 C<$@> value.
9754  
9755 =head3 FORMAL SPECIFICATION
9756  
9757 upload_file == lambda self .
9758 let upload = req.upload('file') in
9759 pre upload /= undef /\ basename(upload.filename) =~ EXT_RE
9760 /\ NOT req->is_limit_exceeded
9761 /\ upload.size <= MAX_UPLOAD_BYTES
9762 let dest = uploads_dir/<random>/<filename> in
9763 let ok = eval { upload.move_to(dest); 1 } in
9764 pre ok /\ -f dest
9765 post render_json({ url: '/open?path=' ++ url_escape(dest), path: dest })
9766  
9767 =head3 EXAMPLE
9768  
9769 POST /upload (multipart: file=@sales.csv)
9770 -> { "url": "/open?path=%2F...%2Fsales.csv", "path": "/.../.uploads/abc123/sales.csv" }
9771  
9772 =cut
9773  
9774 15 ▶sub upload_file ($self) {
Subroutine upload_file: called CC=11 SCAR=27.0
Pod documented
9775 15 my $upload = $self->req->upload('file');
9776 15 ▶ unless ($upload && $upload->filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($upload and $upload->filename) 1 14 15
Condition66%2/3
$upload and $upload->filename
!ll&&!rl&&r
1014
MC/DC50%1/2
$upload and $upload->filename
$upload $upload->filename
Truth table66%2/3
A and B
A= $uploadB= $upload->filename
ABresult
0X0
100
111
9777 1 return $self->render(
9778 json => { error => $self->_i18n('error_upload_none') },
9779 status => 400,
9780 );
9781 }
9782  
9783 # Enforce upload size limit. max_request_size in startup() sets the
9784 # transport-layer cap, but Mojolicious still calls the controller when the
9785 # limit fires (with req->is_limit_exceeded true and partial content in the
9786 # upload asset). Check is_limit_exceeded first; fall through to an asset
9787 # size check as a belt-and-braces guard for any bytes that slipped through.
9788 14 ▶ if ($self->req->is_limit_exceeded || $upload->size > $MAX_UPLOAD_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->req->is_limit_exceeded or $upload->size > $MAX_UPLOAD_BYTES) 0 14 14
Condition33%1/3
$self->req->is_limit_exceeded or $upload->size > $MAX_UPLOAD_BYTES
l!l&&r!l&&!r
0014
MC/DC0%0/2
$self->req->is_limit_exceeded or $upload->size > $MAX_UPLOAD_BYTES
$self->req->is_limit_exceeded $upload->size > $MAX_UPLOAD_BYTES
Truth table33%1/3
A or B
A= $self->req->is_limit_exceededB= $upload->size > $MAX_UPLOAD_BYTES
ABresult
1X1
011
000
9789 0 return $self->render(
9790 json => { error => $self->_i18n('error_upload_too_large', $MAX_UPLOAD_MIB) },
9791 status => 413,
9792 );
9793 }
9794  
9795 # /s so .* matches \n; a percent-decoded newline inside a browser filename
9796 # must not silently truncate the directory-strip, leaving "dir\ncomponent" in.
9797 14 (my $filename = $upload->filename) =~ s{.*[/\\]}{}s;
9798 14 ▶ unless ($filename && $filename =~ $EXT_RE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename and $filename =~ /$EXT_RE/u) 1 13 14
Condition66%2/3
$filename and $filename =~ /$EXT_RE/u
!ll&&!rl&&r
0113
MC/DC50%1/2
$filename and $filename =~ /$EXT_RE/u
$filename $filename =~ /$EXT_RE/u
Truth table66%2/3
A and B
A= $filenameB= $filename =~ /$EXT_RE/u
ABresult
0X0
100
111
9799 1 return $self->render(
9800 json => { error => $self->_i18n('error_upload_ext') },
9801 status => 415,
9802 );
9803 }
9804  
9805 # Store uploads in <uploads_dir>/<random_subdir>/<original_filename>. The
9806 # staging directory comes from the app (config uploads_dir, default
9807 # <app_home>/.uploads) so a deployment can point it at a writable disc and
9808 # a test can keep its uploads out of the directory another process clears.
9809 # Using the original filename is essential: Database::Abstraction derives
9810 # the table name from the file stem, so "sales.csv" must stay "sales.csv".
9811 # The random subdirectory prevents collisions from concurrent same-name uploads.
9812 13 my $uploads_base = $self->app->_uploads_dir;
9813 13 my $sub_dir = eval {
9814 13 ▶ $uploads_base->make_path unless -d $uploads_base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $uploads_base 0 13 13
9815 13 tempdir(DIR => $uploads_base->to_string, CLEANUP => 0);
9816 };
9817 13 ▶ unless (defined $sub_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub_dir) 0 13 13
9818 0 $self->app->log->error("Cannot stage an upload in $uploads_base: " . _tidy_error($@));
9819 0 return $self->render(
9820 json => { error => $self->_i18n('error_upload_save') },
9821 status => 500,
9822 );
9823 }
9824 13 my $dest = Mojo::File->new($sub_dir)->child($filename)->to_string;
9825  
9826 # move_to returns the destination Mojo::File on success; croaks on I/O
9827 # failure. Wrap in eval so a disk error becomes a 500 JSON response
9828 # rather than a bare Mojolicious exception page.
9829 # Use the "eval { ...; 1 }" idiom: $ok is undef only when THIS eval
9830 # threw, avoiding false positives from a stale $@ left by Mojo's
9831 # internal dispatch code that runs before the controller action.
9832 13 my $ok = eval { $upload->move_to($dest); 1 };
9833 13 ▶ if (!$ok || !-f $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and -f $dest) 0 13 13
Condition33%1/3
$ok and -f $dest
!ll&&!rl&&r
0013
MC/DC0%0/2
$ok and -f $dest
$ok -f $dest
Truth table33%1/3
A and B
A= $okB= -f $dest
ABresult
0X0
100
111
9834 0 remove_tree($sub_dir);
9835 0 return $self->render(
9836 json => { error => $self->_i18n('error_upload_save') },
9837 status => 500,
9838 );
9839 }
9840  
9841 13 $self->render(json => {
9842 url => '/open?path=' . url_escape($dest),
9843 path => $dest,
9844 });
9845 }
9846  
9847 5 ▶sub clear_uploads ($self) {
Subroutine clear_uploads: called CC=3 SCAR=12.7
Pod documented
9848 5 my $uploads_dir = $self->app->_uploads_dir;
9849  
9850 5 ▶ return $self->render(json => { freed => 0, count => 0 })
Branch: 1 branch
BranchTrueFalseTotal
unless -d $uploads_dir 0 5 5
9851 unless -d $uploads_dir;
9852  
9853 5 my ($freed, $count) = (0, 0);
9854  
9855 # Each upload lands in <uploads_dir>/<random_subdir>/<filename>.
9856 # Iterate the subdirs, tally file sizes, then remove each subdir tree.
9857 5 my $ok = eval {
9858 6 ▶ $uploads_dir->list({ dir => 1 })->each(sub {
Subroutine __ANON__: called CC=4 SCAR=17.5
9859 6 my ($entry) = @_;
9860 6 ▶ if (-d $entry) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $entry) { } 6 0 6
elsif (-f $entry) { } 0 0 0
9861 6 ▶ $entry->list->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
9862 6 my ($file) = @_;
9863 6 ▶ return unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 6 6
9864 6 ▶ $freed += (-s $file) // 0;
Condition50%1/2
-s $file // 0
l!l
60
MC/DC0%0/1
-s $file // 0
-s $file
Truth table50%1/2
A
A= -s $file
Aresult
11
00
9865 6 $count++;
9866 6 });
9867 6 $entry->remove_tree;
9868 } elsif (-f $entry) {
9869 0 ▶ $freed += (-s $entry) // 0;
Condition0%0/2
-s $entry // 0
l!l
00
MC/DC0%0/1
-s $entry // 0
-s $entry
Truth table0%0/2
A
A= -s $entry
Aresult
11
00
9870 0 $count++;
9871 0 unlink $entry->to_string;
9872 }
9873 5 });
9874 5 1;
9875 };
9876 5 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 5 5
9877 0 $self->app->log->error("Cannot clear $uploads_dir: " . _tidy_error($@));
9878 0 return $self->render(json => { error => $self->_i18n('error_uploads_clear') }, status => 500);
9879 }
9880  
9881 5 $self->render(json => { freed => $freed, count => $count });
9882 }
9883  
9884 # graph_view
9885 #
9886 # Purpose: Render a D3.js line chart from the current data pipeline.
9887 # Entry: Query params identical to /join (l=, j=, c=, f=, d=) plus:
9888 # x=<column> -- X-axis column name
9889 # y=<column> -- Y-axis column name (values must be numeric)
9890 # back=<url> -- URL for the "Back to table" link (set by JS)
9891 # Exit: Emits a TT-rendered HTML page with a brush-to-zoom HTML::D3 snippet.
9892 # Each data point carries an "extra" hash of every column not used
9893 # as the X or Y axis; HTML::D3 displays these in the hover tooltip.
9894 # A "Reset zoom" button appears after the user brushes to zoom in.
9895 # Returns HTTP 400 for missing/invalid column names, 404 if the
9896 # data source cannot be opened, 200 with an error message when
9897 # no plottable rows exist.
9898 100 ▶sub graph_view ($self) {
Subroutine graph_view: called CC=86 SCAR=72.7
Pod documented
9899 100 ▶ my $x_col = $self->param('x') // '';
Condition100%2/2
$self->param("x") // ""
l!l
973
MC/DC100%1/1
$self->param("x") // ""
$self->param("x")
Truth table100%2/2
A
A= $self->param("x")
Aresult
11
00
9900 100 ▶ my $y_col = $self->param('y') // '';
Condition100%2/2
$self->param("y") // ""
l!l
973
MC/DC100%1/1
$self->param("y") // ""
$self->param("y")
Truth table100%2/2
A
A= $self->param("y")
Aresult
11
00
9901 100 ▶ my $series_col = $self->param('series') // '';
Condition50%1/2
$self->param("series") // ""
l!l
0100
MC/DC0%0/1
$self->param("series") // ""
$self->param("series")
Truth table50%1/2
A
A= $self->param("series")
Aresult
11
00
9902 100 ▶ my $agg_fn = lc($self->param('agg') // '');
Condition50%1/2
$self->param("agg") // ""
l!l
0100
MC/DC0%0/1
$self->param("agg") // ""
$self->param("agg")
Truth table50%1/2
A
A= $self->param("agg")
Aresult
11
00
9903 100 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
0100
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
9904 100 my $back2 = _safe_back_url($self->param('back2'));
9905 100 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
0100
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
9906  
9907 100 ▶ return $self->render(text => 'Missing x or y column parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $y_col 5 95 100
Condition66%2/3
length $x_col and length $y_col
!ll&&!rl&&r
5095
MC/DC50%1/2
length $x_col and length $y_col
length $x_col length $y_col
Truth table66%2/3
A and B
A= length $x_colB= length $y_col
ABresult
0X0
100
111
9908 unless length($x_col) && length($y_col);
9909  
9910 # ?agg= is the measure vocabulary the pivot and the stats page already use,
9911 # with no new names: a misspelt one is refused here rather than drawn as
9912 # nothing. Absent, this route behaves exactly as it always has.
9913 95 ▶ if (length $agg_fn && !_measure_fn_ok($agg_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $agg_fn and not _measure_fn_ok($agg_fn)) 0 95 95
Condition33%1/3
length $agg_fn and not _measure_fn_ok($agg_fn)
!ll&&!rl&&r
9500
MC/DC0%0/2
length $agg_fn and not _measure_fn_ok($agg_fn)
length $agg_fn not _measure_fn_ok($agg_fn)
Truth table33%1/3
A and not(B)
A= length $agg_fnB= not _measure_fn_ok($agg_fn)
ABresult
0X0
100
111
9914 0 return $self->render(
9915 text => $self->_i18n('error_agg_fn', $agg_fn, join(' ', @MEASURE_FNS)),
9916 status => 400,
9917 );
9918 }
9919  
9920 95 my ($src, $records, $columns) = $self->_run_pipeline_source;
9921 95 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 3 92 95
9922 unless defined $records;
9923  
9924 92 my $span = $self->_span_calc_spec($columns, 'x');
9925 92 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 4 88 92
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
8804
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
9926  
9927 88 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
9928 88 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 88 88
9929 88 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 86 88
9930  
9931 # Pre-filter before building pairs, same as _run_export_pipeline.
9932 86 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 2 84 86
9933  
9934 84 my %col_set = map { $_ => 1 } @{$columns};
9935 return $self->render(text => "Column not found: $x_col", status => 400)
9936 84 ▶ unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 2 82 84
9937 return $self->render(text => "Column not found: $y_col", status => 400)
9938 82 ▶ unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 3 79 82
9939 79 ▶ if (length $series_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col) 0 79 79
9940 return $self->render(text => "Column not found: $series_col", status => 400)
9941 0 ▶ unless $col_set{$series_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$series_col} 0 0 0
9942 }
9943  
9944 # The x axis is the dimension span= buckets.
9945 79 ▶ $x_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 79 79
9946  
9947 # --- Aggregated path: ?agg= plots one point per group of the x axis rather
9948 # than one per row, which is what turns a bucketed x into a timechart. ---
9949 79 ▶ if (length $agg_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $agg_fn) 0 79 79
9950 0 my $m = _visual_measure($agg_fn, $y_col);
9951 0 ▶ return $self->render(text => $m->{error}, status => 400) if $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"error"} 0 0 0
9952  
9953 my $agg_rows = $self->_grouped_totals(
9954 { source => $src, records => $records },
9955 {
9956 by => [ length($series_col) ? ($x_col, $series_col) : ($x_col) ],
9957 fn => $m->{fn},
9958 val => $m->{val},
9959 0 ▶ as => 'value',
Branch: 1 branch
BranchTrueFalseTotal
length $series_col ? : 0 0 0
9960 },
9961 );
9962  
9963 0 my (%value_of, %series_seen, @series_order, %bucket_seen, @bucket_order);
9964 0 for my $row (@$agg_rows) {
9965 0 ▶ my $bucket = _decode_cell($row->{$x_col} // '');
Condition0%0/2
$row->{$x_col} // ""
l!l
00
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table0%0/2
A
A= $row->{$x_col}
Aresult
11
00
9966 0 ▶ next unless length $bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bucket 0 0 0
9967 0 ▶ push @bucket_order, $bucket unless $bucket_seen{$bucket}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $bucket_seen{$bucket}++ 0 0 0
9968 0 ▶ my $name = length($series_col) ? _decode_cell($row->{$series_col} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
length $series_col ? : 0 0 0
Condition0%0/2
$row->{$series_col} // ""
l!l
00
MC/DC0%0/1
$row->{$series_col} // ""
$row->{$series_col}
Truth table0%0/2
A
A= $row->{$series_col}
Aresult
11
00
9969 0 ▶ next if length($series_col) && !length $name;
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col and not length $name 0 0 0
Condition0%0/3
length $series_col and not length $name
!ll&&!rl&&r
000
MC/DC0%0/2
length $series_col and not length $name
length $series_col not length $name
Truth table0%0/3
A and not(B)
A= length $series_colB= not length $name
ABresult
0X0
100
111
9970 0 ▶ push @series_order, $name unless $series_seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $series_seen{$name}++ 0 0 0
9971 0 $value_of{$bucket}{$name} = $row->{value};
9972 }
9973  
9974 # Buckets in label order: the ISO labels a time bucket produces sort
9975 # chronologically as strings, which is why they are ISO.
9976 0 my @buckets = sort @bucket_order;
9977 0 my @series = sort @series_order;
9978 0 ▶ my $title = "$m->{label} by $x_col" . (length($series_col) ? " and $series_col" : '');
Branch: 1 branch
BranchTrueFalseTotal
length $series_col ? : 0 0 0
9979  
9980 0 require HTML::D3;
9981 0 my ($chart_html, $point_count, @series_data);
9982 0 ▶ if (length $series_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col) { } 0 0 0
9983 @series_data = map {
9984 0 my $name = $_;
9985 my @points = map {
9986 0 defined $value_of{$_}{$name}
9987 0 ▶ ? { label => $_, value => $value_of{$_}{$name} } : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $value_of{$_}{$name} ? : 0 0 0
9988 } @buckets;
9989 0 ▶ @points ? { name => $name, data => \@points } : ();
Branch: 1 branch
BranchTrueFalseTotal
@points ? : 0 0 0
9990 } @series;
9991 0 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @series_data 0 0 0
9992 text => 'No plottable data: no rows have valid numeric values in the Y column.',
9993 status => 200,
9994 ) unless @series_data;
9995 0 $point_count = scalar @series_data;
9996 $chart_html = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 1100, height => 580)
9997 0 ->render_multi_series_line_chart_snippet(\@series_data, { animated => 1 })->{html};
9998 } else {
9999 my @pairs = map {
10000 0 ▶ defined $value_of{$_}{''} ? [ $_, $value_of{$_}{''} ] : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $value_of{$_}{''} ? : 0 0 0
10001 } @buckets;
10002 0 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 0 0 0
10003 text => 'No plottable data: no rows have valid numeric values in the Y column.',
10004 status => 200,
10005 ) unless @pairs;
10006 0 $point_count = scalar @pairs;
10007 $chart_html = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 1100, height => 580)
10008 0 ->render_zoomable_line_chart_snippet(\@pairs, { animated => 1 })->{html};
10009 }
10010 0 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 0 0
10011  
10012 my %chart_spec = (
10013 kind => 'graph',
10014 title => $title,
10015 encode => {
10016 x => $x_col, y => $y_col,
10017 (length($series_col) ? (series => $series_col) : ()),
10018 agg => $m->{fn},
10019 },
10020 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
length $series_col ? : 0 0 0
10021 0 my ($platform, $language) = $self->_resolve_template;
10022 0 my ($source_url, $source_accessed) = $self->_url_attribution;
10023 0 ▶ return $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 0 0
$back2 ? : 0 0 0
Condition0%0/5
$back2 // $back
l!l&&r!l&&!r
000
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table0%0/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10024 handler => 'tt',
10025 template => "$platform/$language/graph",
10026 format => 'html',
10027 title => $title,
10028 chart_html => $chart_html,
10029 chart => \%chart_spec,
10030 chart_json => $self->_chart_json(\%chart_spec),
10031 back_url => ($back2 // $back),
10032 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10033 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10034 point_count => $point_count,
10035 source_url => $source_url,
10036 source_accessed => $source_accessed,
10037 language => $language,
10038 );
10039 }
10040  
10041 # --- Multi-series path: ?series=<col> groups records into one line per value ---
10042 79 ▶ if (length $series_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col) 0 79 79
10043 0 my %by_series;
10044 my @series_order;
10045 0 for my $row (@{$records}) {
10046 0 ▶ my $x = $row->{$x_col} // '';
Condition0%0/2
$row->{$x_col} // ""
l!l
00
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table0%0/2
A
A= $row->{$x_col}
Aresult
11
00
10047 0 ▶ my $y = $row->{$y_col} // '';
Condition0%0/2
$row->{$y_col} // ""
l!l
00
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table0%0/2
A
A= $row->{$y_col}
Aresult
11
00
10048 0 ▶ my $grp = $row->{$series_col} // '';
Condition0%0/2
$row->{$series_col} // ""
l!l
00
MC/DC0%0/1
$row->{$series_col} // ""
$row->{$series_col}
Truth table0%0/2
A
A= $row->{$series_col}
Aresult
11
00
10049 0 ▶ next unless length($x) && length($y) && length($grp);
Branch: 1 branch
BranchTrueFalseTotal
unless length $x and length $y and length $grp 0 0 0
Condition0%0/6
length $x and length $y and length $grp
!ll&&!rl&&r
000
length $x and length $y
!ll&&!rl&&r
000
MC/DC0%0/3
length $x and length $y and length $grp
length $x length $y length $grp
Truth table0%0/4
A and B and C
A= length $xB= length $yC= length $grp
ABCresult
0XX0
10X0
1100
1111
10050 0 my $yn = _numeric_cell($y);
10051 0 ▶ next unless defined $yn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yn 0 0 0
10052 0 ▶ unless (exists $by_series{$grp}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $by_series{$grp}) 0 0 0
10053 0 push @series_order, $grp;
10054 0 $by_series{$grp} = [];
10055 }
10056 0 push @{$by_series{$grp}}, { label => _decode_cell($x), value => $yn };
10057 }
10058  
10059 0 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @series_order 0 0 0
10060 text => 'No plottable data: no rows have valid numeric values in the Y column.',
10061 status => 200,
10062 ) unless @series_order;
10063  
10064 0 my @series_data = map { { name => $_, data => $by_series{$_} } } @series_order;
10065  
10066 0 my ($source_url, $source_accessed) = $self->_url_attribution;
10067  
10068 0 require HTML::D3;
10069 0 my $title = "$y_col vs $x_col by $series_col";
10070 0 my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 1100, height => 580)
10071 ->render_multi_series_line_chart_snippet(\@series_data, { animated => 1 });
10072  
10073 0 my $chart_html = $snippet->{html};
10074 0 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 0 0
10075  
10076 0 my %chart_spec = (
10077 kind => 'graph',
10078 title => $title,
10079 encode => { x => $x_col, y => $y_col, series => $series_col },
10080 );
10081 0 my ($platform, $language) = $self->_resolve_template;
10082 0 ▶ return $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 0 0
$back2 ? : 0 0 0
Condition0%0/5
$back2 // $back
l!l&&r!l&&!r
000
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table0%0/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10083 handler => 'tt',
10084 template => "$platform/$language/graph",
10085 format => 'html',
10086 title => $title,
10087 chart_html => $chart_html,
10088 chart => \%chart_spec,
10089 chart_json => $self->_chart_json(\%chart_spec),
10090 back_url => ($back2 // $back),
10091 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10092 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10093 point_count => scalar @series_order,
10094 source_url => $source_url,
10095 source_accessed => $source_accessed,
10096 language => $language,
10097 );
10098 }
10099  
10100 # --- Single-series path (original behaviour when ?series= is absent) ---
10101  
10102 # Extract [label, value] pairs; skip rows where Y is missing or non-numeric.
10103 79 my @pairs;
10104 79 for my $row (@{$records}) {
10105 464 ▶ my $x = $row->{$x_col} // '';
Condition50%1/2
$row->{$x_col} // ""
l!l
4640
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table50%1/2
A
A= $row->{$x_col}
Aresult
11
00
10106 464 ▶ my $y = $row->{$y_col} // '';
Condition50%1/2
$row->{$y_col} // ""
l!l
4640
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table50%1/2
A
A= $row->{$y_col}
Aresult
11
00
10107 464 ▶ next unless length($x) && length($y);
Branch: 1 branch
BranchTrueFalseTotal
unless length $x and length $y 0 464 464
Condition33%1/3
length $x and length $y
!ll&&!rl&&r
00464
MC/DC0%0/2
length $x and length $y
length $x length $y
Truth table33%1/3
A and B
A= length $xB= length $y
ABresult
0X0
100
111
10108 464 my $yn = _numeric_cell($y);
10109 464 ▶ next unless defined $yn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yn 6 458 464
10110 458 ▶ my %extra = map { $_ => _decode_cell($row->{$_}) } grep { $_ ne $x_col && $_ ne $y_col } @{$columns};
Condition100%3/3
$_ ne $x_col && $_ ne $y_col
!ll&&!rl&&r
4584581784
MC/DC100%2/2
$_ ne $x_col && $_ ne $y_col
$_ ne $x_col $_ ne $y_col
Truth table100%3/3
A && B
A= $_ ne $x_colB= $_ ne $y_col
ABresult
0X0
100
111
10111 458 push @pairs, [_decode_cell($x), $yn, \%extra];
10112 }
10113  
10114 79 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 1 78 79
10115 text => 'No plottable data: no rows have valid numeric values in the Y column.',
10116 status => 200,
10117 ) unless @pairs;
10118  
10119 78 my @y_vals = map { $_->[1] } @pairs;
10120 78 my ($min_y, $max_y, $avg_y);
10121  
10122 # Try aggregate() push path for the global min/max/avg reference lines.
10123 # Falls back to List::Util on the already-extracted @y_vals when push is
10124 # not available (no source, filters active, or non-pushable column).
10125 78 my @f_specs = grep { /\S/ } @{ _filter_specs($self) };
10126 78 my $has_filter = !!@f_specs;
10127 78 ▶ if (!$ENV{BI_FORCE_PERL_AGG} && $src && !$has_filter && !_dedup_requested($self)
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"BI_FORCE_PERL_AGG"} and $src and not $has_filter and not _dedup_requested($self) and _measure_pushable($src, "avg", $y_col, $records)) 70 8 78
Condition83%10/12
not $ENV{"BI_FORCE_PERL_AGG"} and $src and not $has_filter and not _dedup_requested($self) and _measure_pushable($src, "avg", $y_col, $records)
!ll&&!rl&&r
4470
not $ENV{"BI_FORCE_PERL_AGG"} and $src and not $has_filter and not _dedup_requested($self)
!ll&&!rl&&r
4074
not $ENV{"BI_FORCE_PERL_AGG"} and $src and not $has_filter
!ll&&!rl&&r
1374
not $ENV{"BI_FORCE_PERL_AGG"} and $src
!ll&&!rl&&r
0177
MC/DC60%3/5
not $ENV{"BI_FORCE_PERL_AGG"} and $src and not $has_filter and not _dedup_requested($self) and _measure_pushable($src, "avg", $y_col, $records)
not $ENV{"BI_FORCE_PERL_AGG"} $src not $has_filter not _dedup_requested($self) _measure_pushable($src, "avg", $y_col, $records)
Truth table66%4/6
not(A) and B and not(C) and not(D) and E
A= not $ENV{"BI_FORCE_PERL_AGG"}B= $srcC= not $has_filterD= not _dedup_requested($self)E= _measure_pushable($src, "avg", $y_col, $records)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
10128 && _measure_pushable($src, 'avg', $y_col, $records)) {
10129 70 my $ref = eval {
10130 70 $src->aggregate(
10131 by => [],
10132 measures => [
10133 _measure('min', $y_col, 'lo'),
10134 _measure('max', $y_col, 'hi'),
10135 _measure('avg', $y_col, 'mean'),
10136 ],
10137 )
10138 };
10139 70 ▶ if (!$@ && $ref && @$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $ref and @$ref) 70 0 70
Condition33%2/6
not $@ and $ref and @$ref
!ll&&!rl&&r
0070
not $@ and $ref
!ll&&!rl&&r
0070
MC/DC0%0/3
not $@ and $ref and @$ref
not $@ $ref @$ref
Truth table25%1/4
not(A) and B and C
A= not $@B= $refC= @$ref
ABCresult
0XX0
10X0
1100
1111
10140 70 $min_y = $ref->[0]{lo};
10141 70 $max_y = $ref->[0]{hi};
10142 70 $avg_y = $ref->[0]{mean};
10143 }
10144 }
10145 # Perl fallback: compute from the already-stripped @y_vals.
10146 78 ▶ unless (defined $min_y) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min_y) 8 70 78
10147 8 require Statistics::Lite;
10148 8 $min_y = Statistics::Lite::min(@y_vals);
10149 8 $max_y = Statistics::Lite::max(@y_vals);
10150 8 $avg_y = Statistics::Lite::mean(@y_vals);
10151 }
10152  
10153 # The trend line: a least-squares fit of Y against the point's place on the
10154 # axis. The axis is a d3.scalePoint, which spaces the points evenly whatever
10155 # their labels say, so the place (0, 1, 2, ...) is what makes the fit a
10156 # straight line on this chart. The page refits the points in view after a
10157 # zoom, the same way; this is the whole chart's.
10158 78 my $trend = _trend_line(\@y_vals);
10159  
10160 78 my ($source_url, $source_accessed) = $self->_url_attribution;
10161  
10162 78 require HTML::D3;
10163 78 my $title = "$y_col vs $x_col";
10164 78 my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 1100, height => 580)
10165 ->render_zoomable_line_chart_snippet(\@pairs, { animated => 1 });
10166  
10167 78 my $chart_html = $snippet->{html};
10168 78 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 78 78
10169  
10170 78 my %chart_spec = (
10171 kind => 'graph',
10172 title => $title,
10173 encode => { x => $x_col, y => $y_col },
10174 );
10175 78 my ($platform, $language) = $self->_resolve_template;
10176 $self->render(
10177 handler => 'tt',
10178 template => "$platform/$language/graph",
10179 format => 'html',
10180 title => $title,
10181 chart_html => $chart_html,
10182 chart => \%chart_spec,
10183 chart_json => $self->_chart_json(\%chart_spec),
10184 back_url => ($back2 // $back),
10185 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10186 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10187 point_count => scalar @pairs,
10188 ref_min_y => $min_y,
10189 ref_max_y => $max_y,
10190 ref_avg_y => sprintf('%.4g', $avg_y),
10191 ($trend ? (
10192 trend_slope => sprintf('%.4g', $trend->{slope}),
10193 trend_intercept => sprintf('%.6g', $trend->{intercept}),
10194 78 ▶ trend_r2 => sprintf('%.2f', $trend->{r_squared}),
Branch: 3 branches
BranchTrueFalseTotal
$back2 ? : 0 78 78
$back2 ? : 0 78 78
$trend ? : 78 0 78
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0780
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10195 ) : ()),
10196 source_url => $source_url,
10197 source_accessed => $source_accessed,
10198 language => $language,
10199 );
10200 }
10201  
10202 # _trend_line(\@y) -> { slope, intercept, r_squared } | undef
10203 #
10204 # The least-squares line through the values, each at its place 0, 1, 2, ...
10205 # (Statistics::LineFit). undef for fewer than two values, or when the fit
10206 # fails. A flat series has slope 0 and an R squared of 1 (LineFit's answer:
10207 # the line passes through every point).
10208 78 ▶sub _trend_line {
Subroutine _trend_line: called CC=7 SCAR=22.0
10209 78 my ($ys) = @_;
10210 78 ▶ return undef unless ref $ys eq 'ARRAY' && @$ys >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ys eq "ARRAY" and @$ys >= 2 0 78 78
Condition33%1/3
ref $ys eq "ARRAY" and @$ys >= 2
!ll&&!rl&&r
0078
MC/DC0%0/2
ref $ys eq "ARRAY" and @$ys >= 2
ref $ys eq "ARRAY" @$ys >= 2
Truth table33%1/3
A and B
A= ref $ys eq "ARRAY"B= @$ys >= 2
ABresult
0X0
100
111
10211 78 require Statistics::LineFit;
10212 78 my $fit = Statistics::LineFit->new;
10213 78 ▶ return undef unless $fit->setData([ 0 .. $#$ys ], $ys);
Branch: 1 branch
BranchTrueFalseTotal
unless $fit->setData([0 .. $ys->$#*], $ys) 0 78 78
10214 78 my ($intercept, $slope) = $fit->coefficients;
10215 78 ▶ return undef unless defined $slope && defined $intercept;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slope and defined $intercept 0 78 78
Condition33%1/3
defined $slope and defined $intercept
!ll&&!rl&&r
0078
MC/DC0%0/2
defined $slope and defined $intercept
defined $slope defined $intercept
Truth table33%1/3
A and B
A= defined $slopeB= defined $intercept
ABresult
0X0
100
111
10216 78 my $r2 = $fit->rSquared;
10217 78 ▶ return { slope => $slope, intercept => $intercept, r_squared => $r2 // 0 };
Condition50%1/2
$r2 // 0
l!l
780
MC/DC0%0/1
$r2 // 0
$r2
Truth table50%1/2
A
A= $r2
Aresult
11
00
10218 }
10219  
10220 54 ▶sub pie_view ($self) {
Subroutine pie_view: called CC=44 SCAR=49.9
Pod documented
10221 54 ▶ my $cat_col = $self->param('cat') // '';
Condition100%2/2
$self->param("cat") // ""
l!l
522
MC/DC100%1/1
$self->param("cat") // ""
$self->param("cat")
Truth table100%2/2
A
A= $self->param("cat")
Aresult
11
00
10222 54 ▶ my $val_col = $self->param('val') // '';
Condition100%2/2
$self->param("val") // ""
l!l
1143
MC/DC100%1/1
$self->param("val") // ""
$self->param("val")
Truth table100%2/2
A
A= $self->param("val")
Aresult
11
00
10223 54 ▶ my $donut = $self->param('donut') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('donut') ? : 0 54 54
10224 54 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition100%2/2
_safe_back_url($self->param("back")) // "/"
l!l
153
MC/DC100%1/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table100%2/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
10225 54 my $back2 = _safe_back_url($self->param('back2'));
10226 54 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
054
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
10227  
10228 54 ▶ return $self->render(text => 'Missing cat or val column parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col and length $val_col 45 9 54
Condition100%3/3
length $cat_col and length $val_col
!ll&&!rl&&r
3429
MC/DC100%2/2
length $cat_col and length $val_col
length $cat_col length $val_col
Truth table100%3/3
A and B
A= length $cat_colB= length $val_col
ABresult
0X0
100
111
10229 unless length($cat_col) && length($val_col);
10230  
10231 9 my ($src, $records, $columns) = $self->_run_pipeline_source;
10232 9 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 0 9 9
10233 unless defined $records;
10234  
10235 9 my $span = $self->_span_calc_spec($columns, 'cat');
10236 return $self->render(text => $span->{error}, status => 400)
10237 9 ▶ if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 0 9 9
Condition33%1/3
$span and $span->{"error"}
!ll&&!rl&&r
900
MC/DC0%0/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table33%1/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
10238  
10239 9 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
10240 9 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 9 9
10241 0 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
10242  
10243 9 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 9 9
10244  
10245 9 my %col_set = map { $_ => 1 } @{$columns};
10246 return $self->render(text => "Column not found: $cat_col", status => 400)
10247 9 ▶ unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 1 8 9
10248  
10249 # '__count__' is the sentinel value sent when the user picks "Count (rows)".
10250 8 my $count_mode = ($val_col eq '__count__');
10251 return $self->render(text => "Column not found: $val_col", status => 400)
10252 8 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 1 7 8
Condition100%3/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
161
MC/DC100%2/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table100%3/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
10253  
10254 7 ▶ $cat_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 7 7
10255  
10256 # Detect the currency symbol from the first non-empty source value.
10257 # This is a display concern; derive from the pre-filtered source rows so the
10258 # $/$GBP/EUR prefix on the legend is unchanged regardless of the push path.
10259 7 my $currency_symbol = '';
10260 7 ▶ if (!$count_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count_mode) 6 1 7
10261 6 for my $row (@$records) {
10262 22 ▶ my $v = $row->{$val_col} // '';
Condition50%1/2
$row->{$val_col} // ""
l!l
220
MC/DC0%0/1
$row->{$val_col} // ""
$row->{$val_col}
Truth table50%1/2
A
A= $row->{$val_col}
Aresult
11
00
10263 22 ▶ next unless length($v);
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 0 22 22
10264 22 ▶ if ($v =~ /\A\s*\(?\s*([^\d\s.,\-\(])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\A\s*\(?\s*([^\d\s.,\-\(])/u) 2 20 22
10265 2 $currency_symbol = $1;
10266 2 last;
10267 }
10268 }
10269 }
10270  
10271 # Aggregate via _grouped_totals (push path or Perl fold).
10272 7 ▶ my $agg_rows = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 1 6 7
$count_mode ? : 1 6 7
10273 { source => $src, records => $records },
10274 {
10275 by => [$cat_col],
10276 fn => $count_mode ? 'count' : 'sum',
10277 val => $count_mode ? undef : $val_col,
10278 as => 'value',
10279 },
10280 );
10281  
10282 # Top-N: keep the highest-N slices, aggregate the rest as "Other".
10283 7 ▶ my $topn_n = _whole_number($self->param('topn')) // 0;
Condition50%1/2
_whole_number($self->param("topn")) // 0
l!l
07
MC/DC0%0/1
_whole_number($self->param("topn")) // 0
_whole_number($self->param("topn"))
Truth table50%1/2
A
A= _whole_number($self->param("topn"))
Aresult
11
00
10284 7 ▶ if ($topn_n > 0 && @$agg_rows > $topn_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topn_n > 0 and @$agg_rows > $topn_n) 0 7 7
Condition33%1/3
$topn_n > 0 and @$agg_rows > $topn_n
!ll&&!rl&&r
700
MC/DC0%0/2
$topn_n > 0 and @$agg_rows > $topn_n
$topn_n > 0 @$agg_rows > $topn_n
Truth table33%1/3
A and B
A= $topn_n > 0B= @$agg_rows > $topn_n
ABresult
0X0
100
111
10285 0 ▶ my @sorted = sort { ($b->{value} // 0) <=> ($a->{value} // 0) } @$agg_rows;
Condition0%0/4
$Database::BI::Controller::Dashboard::b->{"value"} // 0
l!l
00
$Database::BI::Controller::Dashboard::a->{"value"} // 0
l!l
00
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{"value"} // 0
$Database::BI::Controller::Dashboard::b->{"value"}
MC/DC0%0/1
$Database::BI::Controller::Dashboard::a->{"value"} // 0
$Database::BI::Controller::Dashboard::a->{"value"}
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::b->{"value"}
Aresult
11
00
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::a->{"value"}
Aresult
11
00
10286 0 my @top_agg = @sorted[0 .. $topn_n - 1];
10287 0 ▶ my %top_cats = map { ($_->{$cat_col} // '') => 1 } @top_agg;
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
10288 my $other_rec = [
10289 0 ▶ grep { !$top_cats{$_->{$cat_col} // ''} } @$records
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
10290 ];
10291 0 ▶ if (@$other_rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$other_rec) 0 0 0
10292 0 ▶ my $other = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 0 0 0
$count_mode ? : 0 0 0
10293 { source => undef, records => $other_rec },
10294 { by => [], fn => ($count_mode ? 'count' : 'sum'),
10295 val => ($count_mode ? undef : $val_col), as => 'value' },
10296 );
10297 push @top_agg, { $cat_col => 'Other', value => $other->[0]{value} }
10298 0 ▶ if @$other && defined $other->[0]{value};
Branch: 1 branch
BranchTrueFalseTotal
if @$other and defined $other->[0]{"value"} 0 0 0
Condition0%0/3
@$other and defined $other->[0]{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
@$other and defined $other->[0]{"value"}
@$other defined $other->[0]{"value"}
Truth table0%0/3
A and B
A= @$otherB= defined $other->[0]{"value"}
ABresult
0X0
100
111
10299 }
10300 0 $agg_rows = \@top_agg;
10301 }
10302  
10303 7 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg_rows 0 7 7
10304 text => 'No plottable data: no rows have valid data in the selected columns.',
10305 status => 200,
10306 ) unless @$agg_rows;
10307  
10308 7 my @slices = map { [_decode_cell($_->{$cat_col}), $_->{value}] } @$agg_rows;
10309  
10310 7 my ($source_url, $source_accessed) = $self->_url_attribution;
10311  
10312 7 require HTML::D3;
10313 7 my $scheme = _categorical_scheme($self->param('scheme'));
10314 7 ▶ my $title = $count_mode ? "Count by $cat_col" : "$val_col by $cat_col";
Branch: 1 branch
BranchTrueFalseTotal
$count_mode ? : 1 6 7
10315 7 my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 600, height => 500)
10316 ->render_pie_chart_snippet(\@slices, {
10317 animated => 1,
10318 donut => $donut,
10319 sort_slices => 'value',
10320 max_slices => 12,
10321 legend => 1,
10322 color_scheme => $scheme,
10323 });
10324  
10325 7 my $chart_html = $snippet->{html};
10326 7 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 7 7
10327  
10328 7 my %chart_spec = (
10329 kind => 'pie',
10330 title => $title,
10331 encode => { cat => $cat_col, value => $val_col },
10332 options => { scheme => $scheme, donut => $donut },
10333 );
10334 7 my ($platform, $language) = $self->_resolve_template;
10335 7 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 7 7
$back2 ? : 0 7 7
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
070
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10336 handler => 'tt',
10337 template => "$platform/$language/pie",
10338 format => 'html',
10339 title => $title,
10340 chart_html => $chart_html,
10341 chart => \%chart_spec,
10342 chart_json => $self->_chart_json(\%chart_spec),
10343 back_url => ($back2 // $back),
10344 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10345 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10346 drill_back_url => $back,
10347 slice_count => scalar @slices,
10348 cat_col => $cat_col,
10349 val_col => $val_col,
10350 currency_symbol => $currency_symbol,
10351 source_url => $source_url,
10352 source_accessed => $source_accessed,
10353 language => $language,
10354 );
10355 }
10356  
10357 =head2 heatmap_view
10358  
10359 C<GET /heatmap> -- Render a D3.js grid heatmap for two categorical axes.
10360  
10361 Accepts the same C<l=>, C<j=>, C<f=> pipeline params as C</join>, plus:
10362  
10363 x=<col> X-axis (horizontal) categorical column (required)
10364 y=<col> Y-axis (vertical) categorical column (required)
10365 val=<col> Cell value column (optional; when absent, cells show row count)
10366 scheme=<name> D3 sequential colour scheme name (default: YlOrRd)
10367 show_val=1 Overlay the numeric value inside each cell
10368 back=<url> URL for the "Back to table" breadcrumb link
10369  
10370 =head3 API SPECIFICATION
10371  
10372 =head4 INPUT
10373  
10374 l spec Required. Left table spec (table:name or path:/abs/path).
10375 x string Required. Name of the X-axis categorical column.
10376 y string Required. Name of the Y-axis categorical column.
10377 val string Optional. Name of the value column; omit for count mode.
10378 scheme string Optional. D3 sequential colour scheme name.
10379 show_val flag Optional. Set to 1 to render values inside cells.
10380 back url Optional. Safe back-link URL (sanitised by _safe_back_url).
10381  
10382 =head4 OUTPUT
10383  
10384 200 text/html Rendered heatmap page.
10385 200 text/plain "No plottable data: ..." when grid is empty.
10386 400 text/plain Missing x= or y= parameter.
10387 400 text/plain Column not found: <col>.
10388 404 text/plain "Could not open data source" when l= is absent/invalid.
10389  
10390 =head3 MESSAGES
10391  
10392 error_heatmap_no_data -- no (x,y) pair has a non-empty value.
10393  
10394 =head3 FORMAL SPECIFICATION
10395  
10396 heatmap_view == lambda self .
10397 pre length(x_col) > 0 /\ length(y_col) > 0
10398 /\ x_col in columns /\ y_col in columns
10399 let triples = { (row[x_col], row[y_col], row[val_col]) | row in records,
10400 all three fields non-empty } in
10401 post render(heatmap, triples)
10402  
10403 =head3 EXAMPLE
10404  
10405 GET /heatmap?l=table:sales&x=region&y=product&val=amount
10406 -> 200 HTML page containing D3 heatmap SVG
10407  
10408 =cut
10409  
10410 57 ▶sub heatmap_view ($self) {
Subroutine heatmap_view: called CC=39 SCAR=37.8
Pod documented
10411 57 ▶ my $x_col = $self->param('x') // '';
Condition100%2/2
$self->param("x") // ""
l!l
543
MC/DC100%1/1
$self->param("x") // ""
$self->param("x")
Truth table100%2/2
A
A= $self->param("x")
Aresult
11
00
10412 57 ▶ my $y_col = $self->param('y') // '';
Condition100%2/2
$self->param("y") // ""
l!l
543
MC/DC100%1/1
$self->param("y") // ""
$self->param("y")
Truth table100%2/2
A
A= $self->param("y")
Aresult
11
00
10413 57 ▶ my $val_col = $self->param('val') // '';
Condition100%2/2
$self->param("val") // ""
l!l
750
MC/DC100%1/1
$self->param("val") // ""
$self->param("val")
Truth table100%2/2
A
A= $self->param("val")
Aresult
11
00
10414 57 my $scheme = _sequential_scheme($self->param('scheme'));
10415 57 ▶ my $show_v = $self->param('show_val') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('show_val') ? : 0 57 57
10416 57 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
057
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
10417 57 my $back2 = _safe_back_url($self->param('back2'));
10418 57 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
057
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
10419  
10420 57 ▶ return $self->render(text => 'Missing x or y column parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col and length $y_col 5 52 57
Condition100%3/3
length $x_col and length $y_col
!ll&&!rl&&r
4152
MC/DC100%2/2
length $x_col and length $y_col
length $x_col length $y_col
Truth table100%3/3
A and B
A= length $x_colB= length $y_col
ABresult
0X0
100
111
10421 unless length($x_col) && length($y_col);
10422  
10423 52 my ($src, $records, $columns) = $self->_run_pipeline_source;
10424 52 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 2 50 52
10425 unless defined $records;
10426  
10427 50 my $span = $self->_span_calc_spec($columns, 'x');
10428 return $self->render(text => $span->{error}, status => 400)
10429 50 ▶ if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 2 48 50
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
4802
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
10430  
10431 48 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
10432 48 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 48 48
10433 48 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 47 48
10434  
10435 47 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 46 47
10436  
10437 46 my %col_set = map { $_ => 1 } @{$columns};
10438 return $self->render(text => "Column not found: $x_col", status => 400)
10439 46 ▶ unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 3 43 46
10440 return $self->render(text => "Column not found: $y_col", status => 400)
10441 43 ▶ unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 2 41 43
10442 return $self->render(text => "Column not found: $val_col", status => 400)
10443 41 ▶ if length($val_col) && !$col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $val_col and not $col_set{$val_col} 1 40 41
Condition100%3/3
length $val_col and not $col_set{$val_col}
!ll&&!rl&&r
3551
MC/DC100%2/2
length $val_col and not $col_set{$val_col}
length $val_col not $col_set{$val_col}
Truth table100%3/3
A and not(B)
A= length $val_colB= not $col_set{$val_col}
ABresult
0X0
100
111
10444  
10445 # A heatmap's time dimension is its x axis, which is the one span= buckets.
10446 40 ▶ $x_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 40 40
10447  
10448 # Collect first-seen axis order from the filtered source rows.
10449 # The aggregate path returns groups ordered by (x,y) ascending, which
10450 # differs from first-seen. We re-derive the layout order from the source
10451 # so the grid renders in natural data order regardless of backend.
10452 40 my (@x_order, @y_order, %seen_x, %seen_y);
10453 40 for my $row (@$records) {
10454 234 ▶ my $x = $row->{$x_col} // next;
Condition50%1/2
$row->{$x_col} // (next)
l!l
2340
MC/DC0%0/1
$row->{$x_col} // (next)
$row->{$x_col}
Truth table50%1/2
A
A= $row->{$x_col}
Aresult
11
00
10455 234 ▶ my $y = $row->{$y_col} // next;
Condition50%1/2
$row->{$y_col} // (next)
l!l
2340
MC/DC0%0/1
$row->{$y_col} // (next)
$row->{$y_col}
Truth table50%1/2
A
A= $row->{$y_col}
Aresult
11
00
10456 234 ▶ next unless length($x) && length($y);
Branch: 1 branch
BranchTrueFalseTotal
unless length $x and length $y 0 234 234
Condition33%1/3
length $x and length $y
!ll&&!rl&&r
00234
MC/DC0%0/2
length $x and length $y
length $x length $y
Truth table33%1/3
A and B
A= length $xB= length $y
ABresult
0X0
100
111
10457 234 ▶ push @x_order, $x unless $seen_x{$x}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_x{$x}++ 154 80 234
10458 234 ▶ push @y_order, $y unless $seen_y{$y}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_y{$y}++ 219 15 234
10459 }
10460  
10461 # Aggregate with _grouped_totals (push or Perl fold).
10462 40 ▶ my $agg_rows = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
length $val_col ? : 5 35 40
length $val_col ? : 5 35 40
10463 { source => $src, records => $records },
10464 {
10465 by => [$x_col, $y_col],
10466 fn => length($val_col) ? 'sum' : 'count',
10467 val => length($val_col) ? $val_col : undef,
10468 as => 'value',
10469 },
10470 );
10471  
10472 # Index aggregate rows by [x][y] for O(1) lookup.
10473 40 my %grid;
10474 40 for my $row (@$agg_rows) {
10475 234 $grid{$row->{$x_col}}{$row->{$y_col}} = $row->{value};
10476 }
10477  
10478 my @triples = map {
10479 40 my ($raw_x, $xv) = ($_, _decode_cell($_));
10480 154 ▶ map { [$xv, _decode_cell($_), $grid{$raw_x}{$_} // 0] } @y_order
Condition100%2/2
$grid{$raw_x}{$_} // 0
l!l
234630
MC/DC100%1/1
$grid{$raw_x}{$_} // 0
$grid{$raw_x}{$_}
Truth table100%2/2
A
A= $grid{$raw_x}{$_}
Aresult
11
00
10481 } @x_order;
10482  
10483 40 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @triples 0 40 40
10484 text => 'No plottable data: check that x and y columns contain values.',
10485 status => 200,
10486 ) unless @triples;
10487  
10488 40 my ($source_url, $source_accessed) = $self->_url_attribution;
10489  
10490 40 require HTML::D3;
10491 40 ▶ my $title = length($val_col) ? "$val_col by $x_col and $y_col"
Branch: 1 branch
BranchTrueFalseTotal
length $val_col ? : 5 35 40
10492 : "Count by $x_col and $y_col";
10493 40 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 900, height => 500)
Branch: 1 branch
BranchTrueFalseTotal
length $val_col ? : 5 35 40
10494 ->render_heatmap_snippet(\@triples, {
10495 x_label => $x_col,
10496 y_label => $y_col,
10497 val_label => (length($val_col) ? $val_col : 'Count'),
10498 color_scheme => $scheme,
10499 show_values => $show_v,
10500 animated => 1,
10501 });
10502  
10503 40 my $chart_html = $snippet->{html};
10504 40 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 40 40
10505  
10506 40 my %chart_spec = (
10507 kind => 'heatmap',
10508 title => $title,
10509 encode => { x => $x_col, y => $y_col, value => $val_col },
10510 options => { scheme => $scheme, show_values => $show_v },
10511 );
10512 40 my ($platform, $language) = $self->_resolve_template;
10513 40 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 40 40
$back2 ? : 0 40 40
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0400
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10514 handler => 'tt',
10515 template => "$platform/$language/heatmap",
10516 format => 'html',
10517 title => $title,
10518 chart_html => $chart_html,
10519 chart => \%chart_spec,
10520 chart_json => $self->_chart_json(\%chart_spec),
10521 cell_count => scalar @triples,
10522 back_url => ($back2 // $back),
10523 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10524 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10525 source_url => $source_url,
10526 source_accessed => $source_accessed,
10527 language => $language,
10528 );
10529 }
10530  
10531 =head2 bar_view
10532  
10533 C<GET /bar> -- Render a D3.js bar chart grouped by a categorical column.
10534  
10535 Accepts the same C<l=>, C<j=>, C<f=> pipeline params as C</join>, plus:
10536  
10537 cat=<col> Category (X-axis) column (required)
10538 val=<col> Value column to sum per category; omit or use C<__count__>
10539 to count rows instead of summing (optional)
10540 orient=h Horizontal bars (default: vertical)
10541 sort=value|label Sort bars by descending value or ascending label
10542 max=N Collapse bars beyond the top-N into an "Other" bar
10543 back=<url> URL for the "Back to table" breadcrumb link
10544  
10545 =head3 API SPECIFICATION
10546  
10547 =head4 INPUT
10548  
10549 l spec Required. Left table spec (table:name or path:/abs/path).
10550 cat string Required. Name of the category column.
10551 val string Optional. Name of the value column; use __count__ or
10552 omit entirely to count rows per category.
10553 orient string Optional. "h" for horizontal bars; default vertical.
10554 sort string Optional. "value" or "label"; default file order.
10555 max integer Optional. Collapse beyond top-N categories into "Other".
10556 back url Optional. Safe back-link URL (sanitised by _safe_back_url).
10557  
10558 =head4 OUTPUT
10559  
10560 200 text/html Rendered bar chart page (SVG element id="bar_chart").
10561 200 text/plain "No plottable data: ..." when no categories exist.
10562 400 text/plain Missing cat= parameter.
10563 400 text/plain Column not found: <col>.
10564 404 text/plain "Could not open data source" when l= is absent/invalid.
10565  
10566 =head3 MESSAGES
10567  
10568 error_bar_no_data -- no row has a non-empty category value.
10569  
10570 =head3 FORMAL SPECIFICATION
10571  
10572 bar_view == lambda self .
10573 pre length(cat_col) > 0 /\ cat_col in columns
10574 let bars = { (cat, sum(val)) | row in records, row[cat_col] = cat,
10575 val /= undef } in
10576 post render(bar, bars, orientation, sort, max_bars)
10577  
10578 =head3 EXAMPLE
10579  
10580 GET /bar?l=table:sales&cat=region&val=amount&sort=value
10581 -> 200 HTML page with id="bar_chart" SVG element
10582  
10583 =cut
10584  
10585 63 ▶sub bar_view ($self) {
Subroutine bar_view: called CC=47 SCAR=48.2
Pod documented
10586 63 ▶ my $cat_col = $self->param('cat') // '';
Condition100%2/2
$self->param("cat") // ""
l!l
594
MC/DC100%1/1
$self->param("cat") // ""
$self->param("cat")
Truth table100%2/2
A
A= $self->param("cat")
Aresult
11
00
10587 63 ▶ my $val_col = $self->param('val') // '';
Condition100%2/2
$self->param("val") // ""
l!l
1152
MC/DC100%1/1
$self->param("val") // ""
$self->param("val")
Truth table100%2/2
A
A= $self->param("val")
Aresult
11
00
10588 63 ▶ my $orient = $self->param('orient') // 'vertical';
Condition100%2/2
$self->param("orient") // "vertical"
l!l
261
MC/DC100%1/1
$self->param("orient") // "vertical"
$self->param("orient")
Truth table100%2/2
A
A= $self->param("orient")
Aresult
11
00
10589 63 ▶ my $sort = $self->param('sort') // 'none';
Condition100%2/2
$self->param("sort") // "none"
l!l
459
MC/DC100%1/1
$self->param("sort") // "none"
$self->param("sort")
Truth table100%2/2
A
A= $self->param("sort")
Aresult
11
00
10590 63 ▶ my $max_bars = int($self->param('max') || 0);
Condition50%1/2
$self->param('max') || 0
l!l
063
MC/DC0%0/1
$self->param('max') || 0
$self->param('max')
Truth table50%1/2
A
A= $self->param('max')
Aresult
11
00
10591 63 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
063
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
10592 63 my $back2 = _safe_back_url($self->param('back2'));
10593 63 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
063
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
10594  
10595 63 ▶ return $self->render(text => 'Missing cat column parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col 5 58 63
10596 unless length($cat_col);
10597  
10598 58 my ($src, $records, $columns) = $self->_run_pipeline_source;
10599 58 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 57 58
10600 unless defined $records;
10601  
10602 # span= buckets the category column by expanding into the calculated field a
10603 # reader could have typed, so the buckets come from the same arithmetic a
10604 # ?c= definition would use. See _span_calc_spec.
10605 57 my $span = $self->_span_calc_spec($columns, 'cat');
10606 return $self->render(text => $span->{error}, status => 400)
10607 57 ▶ if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 3 54 57
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
5403
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
10608  
10609 54 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
10610 54 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 54 54
10611 54 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 53 54
10612  
10613 53 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 52 53
10614  
10615 52 my %col_set = map { $_ => 1 } @{$columns};
10616 return $self->render(text => "Column not found: $cat_col", status => 400)
10617 52 ▶ unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 2 50 52
10618  
10619 # '__count__' sentinel: count rows per category instead of summing a column.
10620 50 ▶ my $count_mode = ($val_col eq '__count__' || !length($val_col));
Condition100%3/3
$val_col eq '__count__' || !length($val_col)
l!l&&r!l&&!r
1418
MC/DC100%2/2
$val_col eq '__count__' || !length($val_col)
$val_col eq '__count__' !length($val_col)
Truth table100%3/3
A || not(B)
A= $val_col eq '__count__'B= !length($val_col)
ABresult
1X1
011
000
10621 return $self->render(text => "Column not found: $val_col", status => 400)
10622 50 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 2 48 50
Condition100%3/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
4262
MC/DC100%2/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table100%3/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
10623  
10624 # The chart groups by the bucket column the request asked for, if it did.
10625 48 ▶ $cat_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 48 48
10626  
10627 # Aggregate via _grouped_totals (push path or Perl fold).
10628 48 ▶ my $agg_rows = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 42 6 48
$count_mode ? : 42 6 48
10629 { source => $src, records => $records },
10630 {
10631 by => [$cat_col],
10632 fn => $count_mode ? 'count' : 'sum',
10633 val => $count_mode ? undef : $val_col,
10634 as => 'value',
10635 },
10636 );
10637  
10638 # Top-N: keep the highest-N categories, aggregate the rest as "Other".
10639 48 ▶ my $topn_n = _whole_number($self->param('topn')) // 0;
Condition50%1/2
_whole_number($self->param("topn")) // 0
l!l
048
MC/DC0%0/1
_whole_number($self->param("topn")) // 0
_whole_number($self->param("topn"))
Truth table50%1/2
A
A= _whole_number($self->param("topn"))
Aresult
11
00
10640 48 ▶ if ($topn_n > 0 && @$agg_rows > $topn_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topn_n > 0 and @$agg_rows > $topn_n) 0 48 48
Condition33%1/3
$topn_n > 0 and @$agg_rows > $topn_n
!ll&&!rl&&r
4800
MC/DC0%0/2
$topn_n > 0 and @$agg_rows > $topn_n
$topn_n > 0 @$agg_rows > $topn_n
Truth table33%1/3
A and B
A= $topn_n > 0B= @$agg_rows > $topn_n
ABresult
0X0
100
111
10641 0 ▶ my @sorted = sort { ($b->{value} // 0) <=> ($a->{value} // 0) } @$agg_rows;
Condition0%0/4
$Database::BI::Controller::Dashboard::b->{"value"} // 0
l!l
00
$Database::BI::Controller::Dashboard::a->{"value"} // 0
l!l
00
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{"value"} // 0
$Database::BI::Controller::Dashboard::b->{"value"}
MC/DC0%0/1
$Database::BI::Controller::Dashboard::a->{"value"} // 0
$Database::BI::Controller::Dashboard::a->{"value"}
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::b->{"value"}
Aresult
11
00
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::a->{"value"}
Aresult
11
00
10642 0 my @top_agg = @sorted[0 .. $topn_n - 1];
10643 0 ▶ my %top_cats = map { ($_->{$cat_col} // '') => 1 } @top_agg;
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
10644 my $other_rec = [
10645 0 ▶ grep { !$top_cats{$_->{$cat_col} // ''} } @$records
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
10646 ];
10647 0 ▶ if (@$other_rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$other_rec) 0 0 0
10648 0 ▶ my $other = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 0 0 0
$count_mode ? : 0 0 0
10649 { source => undef, records => $other_rec },
10650 { by => [], fn => ($count_mode ? 'count' : 'sum'),
10651 val => ($count_mode ? undef : $val_col), as => 'value' },
10652 );
10653 push @top_agg, { $cat_col => 'Other', value => $other->[0]{value} }
10654 0 ▶ if @$other && defined $other->[0]{value};
Branch: 1 branch
BranchTrueFalseTotal
if @$other and defined $other->[0]{"value"} 0 0 0
Condition0%0/3
@$other and defined $other->[0]{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
@$other and defined $other->[0]{"value"}
@$other defined $other->[0]{"value"}
Truth table0%0/3
A and B
A= @$otherB= defined $other->[0]{"value"}
ABresult
0X0
100
111
10655 }
10656 0 $agg_rows = \@top_agg;
10657 }
10658  
10659 48 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg_rows 1 47 48
10660 text => 'No plottable data: no rows have valid data in the selected columns.',
10661 status => 200,
10662 ) unless @$agg_rows;
10663  
10664 47 my @bars = map { [_decode_cell($_->{$cat_col}), $_->{value}] } @$agg_rows;
10665  
10666 47 ▶ my $orientation = ($orient eq 'h') ? 'horizontal' : 'vertical';
Branch: 1 branch
BranchTrueFalseTotal
$orient eq 'h' ? : 2 45 47
10667 47 ▶ my $sort_bars = ($sort eq 'value' || $sort eq 'label') ? $sort : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$sort eq 'value' || $sort eq 'label' ? : 2 45 47
Condition66%2/3
$sort eq "value" or $sort eq "label"
l!l&&r!l&&!r
2045
MC/DC50%1/2
$sort eq "value" or $sort eq "label"
$sort eq "value" $sort eq "label"
Truth table66%2/3
A or B
A= $sort eq "value"B= $sort eq "label"
ABresult
1X1
011
000
10668  
10669 47 my ($source_url, $source_accessed) = $self->_url_attribution;
10670  
10671 47 require HTML::D3;
10672 47 ▶ my $title = $count_mode ? "Count by $cat_col"
Branch: 1 branch
BranchTrueFalseTotal
$count_mode ? : 42 5 47
10673 : "$val_col by $cat_col";
10674 47 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 800, height => 480)
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 42 5 47
$max_bars > 0 ? : 0 47 47
10675 ->render_bar_chart_snippet(\@bars, {
10676 animated => 1,
10677 orientation => $orientation,
10678 sort_bars => $sort_bars,
10679 color => 'categorical',
10680 show_values => 0,
10681 value_label => ($count_mode ? 'Count' : $val_col),
10682 x_label => $cat_col,
10683 ($max_bars > 0 ? (max_bars => $max_bars) : ()),
10684 });
10685  
10686 47 my $chart_html = $snippet->{html};
10687 47 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 47 47
10688  
10689 47 ▶ my %chart_spec = (
Condition50%1/2
$orient // "v"
l!l
470
MC/DC0%0/1
$orient // "v"
$orient
Truth table50%1/2
A
A= $orient
Aresult
11
00
10690 kind => 'bar',
10691 title => $title,
10692 encode => { cat => $cat_col, value => $val_col },
10693 options => { orientation => ($orient // 'v'), max_items => $max_bars },
10694 );
10695 47 my ($platform, $language) = $self->_resolve_template;
10696 47 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 47 47
$back2 ? : 0 47 47
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0470
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
10697 handler => 'tt',
10698 template => "$platform/$language/bar",
10699 format => 'html',
10700 title => $title,
10701 chart_html => $chart_html,
10702 chart => \%chart_spec,
10703 chart_json => $self->_chart_json(\%chart_spec),
10704 bar_count => scalar @bars,
10705 cat_col => $cat_col,
10706 back_url => ($back2 // $back),
10707 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
10708 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
10709 source_url => $source_url,
10710 source_accessed => $source_accessed,
10711 language => $language,
10712 );
10713 }
10714  
10715 =head2 folder_view
10716  
10717 C<GET /folder> -- Browse a data table grouped by the letter-prefix hierarchy
10718 of an order-number column (e.g. A10, CT4, B3).
10719  
10720 The top level displays one folder icon per initial letter group (A, B, C ...).
10721 Clicking a folder with sub-prefixes (e.g. C contains CA and CB items) shows
10722 the sub-folders; clicking a leaf prefix shows the actual data rows sorted
10723 numerically by the embedded digit sequence.
10724  
10725 Accepts the same C<l=>, C<j=>, C<f=> pipeline params as C</join>, plus:
10726  
10727 col=<col> Column whose values contain order codes (optional; auto-
10728 detected when absent by scanning the first 10 rows for
10729 values matching C<[A-Z]{1,4}\d+[A-Z]?>)
10730 prefix=<str> Current drill-down prefix, e.g. "CA" (optional; top-level
10731 when absent)
10732 back=<url> URL for the "Back to table" breadcrumb link (optional;
10733 derived from C<l=> when absent)
10734  
10735 =head3 API SPECIFICATION
10736  
10737 =head4 INPUT
10738  
10739 l spec Required. Left table spec (table:name or path:/abs/path).
10740 col string Optional. Order-code column name.
10741 prefix string Optional. Current letter prefix; max length MAX_PREFIX_LEN.
10742 back url Optional. Safe back-link URL (sanitised by _safe_back_url).
10743  
10744 =head4 OUTPUT
10745  
10746 200 text/html Rendered folder page with folder grid and/or item table.
10747 400 text/plain "Missing l= parameter" when l= is absent.
10748 400 text/plain "Prefix too long" when prefix exceeds MAX_PREFIX_LEN (20).
10749 404 text/plain "Could not open data source" when the data source is unavailable.
10750  
10751 =head3 MESSAGES
10752  
10753 None -- all failure paths render plain text with an HTTP status code.
10754  
10755 =head3 FORMAL SPECIFICATION
10756  
10757 folder_view == lambda self .
10758 pre length(left_spec) > 0 /\ length(prefix) <= MAX_PREFIX_LEN
10759 let records = pipeline(left_spec, joins, filters)
10760 prefix_map = { p -> [row] | row in records, row[col] =~ /[A-Z]+/ -> p } in
10761 IF prefix = "" THEN
10762 post render(folder, folders = first_letters(prefix_map))
10763 ELSE
10764 post render(folder, folders = sub_prefixes(prefix, prefix_map),
10765 items = sort_numeric(prefix_map[prefix]))
10766  
10767 =head3 EXAMPLE
10768  
10769 GET /folder?l=table:orders&col=order_no
10770 -> 200 HTML folder grid showing A (12 items), B (7 items), C (4 items)
10771  
10772 GET /folder?l=table:orders&col=order_no&prefix=A
10773 -> 200 HTML folder grid for sub-prefixes and items directly under "A"
10774  
10775 =cut
10776  
10777 58 ▶sub folder_view ($self) {
Subroutine folder_view: called CC=38 SCAR=37.7
Pod documented
10778 58 ▶ my $key_col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
508
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
10779 58 ▶ my $prefix = $self->param('prefix') // '';
Condition100%2/2
$self->param("prefix") // ""
l!l
553
MC/DC100%1/1
$self->param("prefix") // ""
$self->param("prefix")
Truth table100%2/2
A
A= $self->param("prefix")
Aresult
11
00
10780 58 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
562
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
10781  
10782 58 ▶ return $self->render(text => 'Missing l= parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 3 55 58
10783 unless length($left_spec);
10784  
10785 # Guard against DoS: a crafted prefix longer than any real order-code would
10786 # generate O(N) breadcrumb entries and a proportionally large HTML response.
10787 55 ▶ return $self->render(text => 'Prefix too long', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix > $MAX_PREFIX_LEN 2 53 55
10788 if length($prefix) > $MAX_PREFIX_LEN;
10789  
10790 # Fall back to the table's own URL when no explicit back= is supplied,
10791 # so the breadcrumb always has a "Back to table" link.
10792 53 ▶ my $back = _safe_back_url($self->param('back'))
Condition33%1/3
_safe_back_url($self->param('back')) || $self->_spec_to_url($left_spec)
l!l&&r!l&&!r
0530
MC/DC0%0/2
_safe_back_url($self->param('back')) || $self->_spec_to_url($left_spec)
_safe_back_url($self->param('back')) $self->_spec_to_url($left_spec)
Truth table33%1/3
A || B
A= _safe_back_url($self->param('back'))B= $self->_spec_to_url($left_spec)
ABresult
1X1
011
000
10793 || $self->_spec_to_url($left_spec);
10794  
10795 53 my ($records, $columns) = $self->_run_export_pipeline;
10796 53 ▶ return if $self->stash('_reduce_error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->stash("_reduce_error") 6 47 53
10797 47 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless $records 2 45 47
10798 unless $records;
10799  
10800 # Auto-detect key column: find the first column whose sampled values look
10801 # like order-number codes (1-4 uppercase letters followed by digits).
10802 45 ▶ if (!length($key_col)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $key_col) 2 43 45
10803 2 ▶ my $sample_end = $#{$records} < 9 ? $#{$records} : 9;
Branch: 1 branch
BranchTrueFalseTotal
$#{$records;} < 9 ? : 2 0 2
10804 2 OUTER: for my $col (@{$columns}) {
10805 12 for my $row (@{$records}[0 .. $sample_end]) {
10806 72 ▶ if (($row->{$col} // '') =~ /\A[A-Z]{1,4}\d+[A-Z]?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($row->{$col} // "") =~ /\A[A-Z]{1,4}\d+[A-Z]?\z/u) 0 72 72
Condition50%1/2
$row->{$col} // ""
l!l
720
MC/DC0%0/1
$row->{$col} // ""
$row->{$col}
Truth table50%1/2
A
A= $row->{$col}
Aresult
11
00
10807 0 $key_col = $col;
10808 0 last OUTER;
10809 }
10810 }
10811 }
10812 2 ▶ $key_col ||= ($columns->[0] // '');
Condition40%2/5
$key_col ||= $columns->[0] // ""
l!l&&r!l&&!r
020
$columns->[0] // ""
l!l
20
MC/DC0%0/2
$key_col ||= $columns->[0] // ""
$key_col $columns->[0]
Truth table33%1/3
A ||= B
A= $key_colB= $columns->[0]
ABresult
1X1
011
000
10813 }
10814  
10815 # Build prefix -> [rows] map. Prefix is the leading letter-run of each
10816 # key value: "A10" -> "A", "CT4" -> "CT", "CC30A" -> "CC".
10817 45 my %prefix_to_rows;
10818 45 for my $row (@{$records}) {
10819 270 ▶ my $val = $row->{$key_col} // '';
Condition50%1/2
$row->{$key_col} // ""
l!l
2700
MC/DC0%0/1
$row->{$key_col} // ""
$row->{$key_col}
Truth table50%1/2
A
A= $row->{$key_col}
Aresult
11
00
10820 270 my ($p) = $val =~ /\A([A-Z]+)/;
10821 270 ▶ next unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 234 36 270
10822 36 push @{ $prefix_to_rows{$p} }, $row;
10823 }
10824  
10825 # Return the sorted list of "one-step" child prefixes of $par: all known
10826 # prefixes that share the parent as a strict prefix and extend it by
10827 # exactly one letter.
10828 3 ▶ my $sub_prefixes = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
10829 3 my ($par) = @_;
10830 3 my $len = length($par);
10831 3 my %seen;
10832 3 for my $p (keys %prefix_to_rows) {
10833 12 ▶ next if length($p) <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $p <= $len 10 2 12
10834 2 ▶ next if substr($p, 0, $len) ne $par;
Branch: 1 branch
BranchTrueFalseTotal
if substr($p, 0, $len) ne $par 2 0 2
10835 0 $seen{ substr($p, 0, $len + 1) } = 1;
10836 }
10837 3 return sort keys %seen;
10838 45 };
10839  
10840 # Count total items underneath a prefix (including all deeper sub-prefixes).
10841 10 ▶ my $deep_count = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
10842 10 my ($par) = @_;
10843 10 my $n = 0;
10844 10 for my $p (keys %prefix_to_rows) {
10845 40 ▶ $n += scalar @{ $prefix_to_rows{$p} }
Branch: 1 branch
BranchTrueFalseTotal
if substr($p, 0, length $par) eq $par 12 28 40
10846 if substr($p, 0, length($par)) eq $par;
10847 }
10848 10 return $n;
10849 45 };
10850  
10851 45 my ($platform, $language) = $self->_resolve_template;
10852  
10853 # Base URL carries the table spec (and optional col=) so every sub-link
10854 # re-uses the same data source without repeating params.
10855 45 my $base_url = '/folder?l=' . url_escape($left_spec);
10856 45 ▶ $base_url .= '&col=' . url_escape($key_col) if length($key_col);
Branch: 1 branch
BranchTrueFalseTotal
if length $key_col 45 0 45
10857 # The table it came from travels too, so a folder level keeps the trail.
10858 45 $base_url .= '&back=' . url_escape($back);
10859  
10860 45 my (@folders, @items, @breadcrumb);
10861  
10862 45 ▶ if (!length($prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $prefix) { } 42 3 45
10863 # Top level: group all prefixes by their first letter.
10864 42 my %first_letter;
10865 42 for my $p (keys %prefix_to_rows) {
10866 12 $first_letter{ substr($p, 0, 1) } = 1;
10867 }
10868 42 for my $fl (sort keys %first_letter) {
10869 10 push @folders, {
10870 name => $fl,
10871 count => $deep_count->($fl),
10872 href => $base_url . '&prefix=' . url_escape($fl),
10873 };
10874 }
10875 } else {
10876 # Build breadcrumb: one entry per letter-prefix layer up to current.
10877 # e.g. prefix="CD" -> [ {name="C", href=...}, {name="CD", href=undef} ]
10878 3 for my $i (1 .. length($prefix)) {
10879 4 my $p = substr($prefix, 0, $i);
10880 4 ▶ push @breadcrumb, {
Branch: 1 branch
BranchTrueFalseTotal
$i < length $prefix ? : 1 3 4
10881 name => $p,
10882 href => ($i < length($prefix))
10883 ? ($base_url . '&prefix=' . url_escape($p))
10884 : undef,
10885 };
10886 }
10887  
10888 # One-step child prefixes of the current prefix (sub-folders).
10889 3 for my $sp ($sub_prefixes->($prefix)) {
10890 0 push @folders, {
10891 name => $sp,
10892 count => $deep_count->($sp),
10893 href => $base_url . '&prefix=' . url_escape($sp),
10894 };
10895 }
10896  
10897 # Items whose prefix matches exactly (e.g. prefix="C" shows C1, C3, ...).
10898 3 ▶ @items = @{ $prefix_to_rows{$prefix} // [] };
Condition50%1/2
$prefix_to_rows{$prefix} // []
l!l
30
MC/DC0%0/1
$prefix_to_rows{$prefix} // []
$prefix_to_rows{$prefix}
Truth table50%1/2
A
A= $prefix_to_rows{$prefix}
Aresult
11
00
10899  
10900 # Sort by the numeric part of the key value so A9 < A10.
10901 @items = sort {
10902 3 ▶ my ($an) = ($a->{$key_col} // '') =~ /(\d+)/;
Condition50%1/2
$Database::BI::Controller::Dashboard::a->{$key_col} // ""
l!l
40
MC/DC0%0/1
$Database::BI::Controller::Dashboard::a->{$key_col} // ""
$Database::BI::Controller::Dashboard::a->{$key_col}
Truth table50%1/2
A
A= $Database::BI::Controller::Dashboard::a->{$key_col}
Aresult
11
00
10903 4 ▶ my ($bn) = ($b->{$key_col} // '') =~ /(\d+)/;
Condition50%1/2
$Database::BI::Controller::Dashboard::b->{$key_col} // ""
l!l
40
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{$key_col} // ""
$Database::BI::Controller::Dashboard::b->{$key_col}
Truth table50%1/2
A
A= $Database::BI::Controller::Dashboard::b->{$key_col}
Aresult
11
00
10904 ($an // 0) <=> ($bn // 0)
10905 4 ▶ || ($a->{$key_col} // '') cmp ($b->{$key_col} // '');
Condition72%8/11
($an // 0) <=> ($bn // 0) || ($Database::BI::Controller::Dashboard::a->{$key_col} // '') cmp ($Database::BI::Controller::Dashboard::b->{$key_col} // '')
l!l&&r!l&&!r
301
$an // 0
l!l
31
$bn // 0
l!l
31
$Database::BI::Controller::Dashboard::a->{$key_col} // ""
l!l
10
$Database::BI::Controller::Dashboard::b->{$key_col} // ""
l!l
10
MC/DC0%0/2
($an // 0) <=> ($bn // 0) || ($Database::BI::Controller::Dashboard::a->{$key_col} // '') cmp ($Database::BI::Controller::Dashboard::b->{$key_col} // '')
$an $Database::BI::Controller::Dashboard::a->{$key_col}
MC/DC100%1/1
$bn // 0
$bn
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{$key_col} // ""
$Database::BI::Controller::Dashboard::b->{$key_col}
Truth table66%2/3
A || B
A= $anB= $Database::BI::Controller::Dashboard::a->{$key_col}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $bn
Aresult
11
00
Truth table50%1/2
A
A= $Database::BI::Controller::Dashboard::b->{$key_col}
Aresult
11
00
10906 } @items;
10907 }
10908  
10909 45 ▶ my $title = length($prefix) ? "Folder: $prefix" : 'Folder view';
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 3 42 45
10910  
10911 $self->render(
10912 handler => 'tt',
10913 template => "$platform/$language/folder",
10914 format => 'html',
10915 title => $title,
10916 folders => \@folders,
10917 items => \@items,
10918 columns => $columns,
10919 key_col => $key_col,
10920 prefix => $prefix,
10921 breadcrumb => \@breadcrumb,
10922 # Inside a folder the trail goes on past this page, so its own crumb
10923 # links to the top of the folder view.
10924 45 ▶ (@breadcrumb ? (trail => do {
Branch: 1 branch
BranchTrueFalseTotal
@breadcrumb ? : 3 42 45
10925 3 my $trail = _breadcrumb_trail($self);
10926 3 ▶ $trail->[-1]{url} = $base_url if @$trail;
Branch: 1 branch
BranchTrueFalseTotal
if @$trail 3 0 3
10927 3 $trail;
10928 }) : ()),
10929 base_url => $base_url,
10930 back_url => $back,
10931 back_label => 'Back to table',
10932 left_spec => $left_spec,
10933 language => $language,
10934 );
10935 }
10936  
10937 =head2 tables_view
10938  
10939 C<GET /tables> -- Schema browser for multi-table databases (SQLite, etc.).
10940 Lists all internal tables with their columns; clicking a table opens it. A
10941 JSON file that is an object holding lists of objects has one table per list
10942 (see L<Database::BI::Model::DataSource/json_lists>).
10943  
10944 =head3 API SPECIFICATION
10945  
10946 =head4 INPUT
10947  
10948 ?l= string Required. Table spec: "table:name" (data_dir) or
10949 "path:/abs/path" (filesystem path). The file must be a
10950 SQLite database or a .json file.
10951  
10952 =head4 OUTPUT
10953  
10954 Renders C<tables.html.tt> with:
10955  
10956 db_label string Short name / stem of the database file
10957 tables ARRAYREF of { name, columns => [...], href }
10958 spec string The raw l= spec (for building back links)
10959 back_url string URL to navigate back one level
10960 back_label string Label for the back link
10961  
10962 =cut
10963  
10964 57 ▶sub tables_view ($self) {
Subroutine tables_view: called CC=41 SCAR=59.6
Pod documented
10965 57 my ($platform, $language) = $self->_resolve_template;
10966 57 my $spec = $self->param('l');
10967 57 ▶ return $self->reply->not_found unless defined $spec && length $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and length $spec 4 53 57
Condition100%3/3
defined $spec and length $spec
!ll&&!rl&&r
2253
MC/DC100%2/2
defined $spec and length $spec
defined $spec length $spec
Truth table100%3/3
A and B
A= defined $specB= length $spec
ABresult
0X0
100
111
10968  
10969 53 my ($file_path, $db_label, $back_url, $back_label);
10970 53 my @tables;
10971  
10972 53 ▶ if ($spec =~ /\Atable:([A-Za-z0-9_]+)\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($spec =~ /\Atable:([A-Za-z0-9_]+)\z/u) { } 44 9 53
elsif ($spec =~ /\Apath:(.+)\z/u) { } 5 4 9
elsif ($spec =~ /$REDIS_URL_RE/u) { } 0 4 4
elsif ($spec =~ /$MARIADB_SERVER_RE/u) { } 0 4 4
10973 44 my $data_dir = _data_dir_for($self->app);
10974 44 my @exts = qw(sql sqlite sqlite3 db json);
10975 44 ▶ my $table_name = _table_stem($data_dir, $1, @exts) // lc $1;
Condition100%2/2
_table_stem($data_dir, $1, @exts) // lc $1
l!l
242
MC/DC100%1/1
_table_stem($data_dir, $1, @exts) // lc $1
_table_stem($data_dir, $1, @exts)
Truth table100%2/2
A
A= _table_stem($data_dir, $1, @exts)
Aresult
11
00
10976 44 for my $ext (@exts) {
10977 218 my $p = $data_dir->child("$table_name.$ext");
10978 218 ▶ if (-f $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $p) 2 216 218
10979 2 $file_path = $p->to_string;
10980 2 last;
10981 }
10982 }
10983 44 ▶ return $self->reply->not_found unless defined $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_path 42 2 44
10984 2 $db_label = $table_name;
10985 2 $back_url = '/';
10986 2 $back_label = 'Home';
10987 } elsif ($spec =~ /\Apath:(.+)\z/) {
10988 5 my ($path_arg) = _split_path($1);
10989 5 ▶ if (my $refusal = _outside_roots($self, $path_arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = _outside_roots($self, $path_arg)) 0 5 5
10990 0 return $self->render(text => $refusal, status => 403);
10991 }
10992 5 my $file = eval { Mojo::File->new($path_arg)->realpath };
10993 5 ▶ return $self->reply->not_found
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -f $file and $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/iu 2 3 5
Condition83%5/6
defined $file and -f $file and $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/iu
!ll&&!rl&&r
113
defined $file and -f $file
!ll&&!rl&&r
104
MC/DC66%2/3
defined $file and -f $file and $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/iu
defined $file -f $file $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/iu
Truth table75%3/4
A and B and C
A= defined $fileB= -f $fileC= $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/iu
ABCresult
0XX0
10X0
1100
1111
10994 unless defined $file && -f $file && $file->basename =~ /\.(?:sql|sqlite|sqlite3|db|json)\z/i;
10995 3 $file_path = $file->to_string;
10996 3 ($db_label = $file->basename) =~ s/\.[^.]+\z//;
10997 3 $back_url = '/browse?path=' . url_escape($file->dirname->to_string);
10998 3 $back_label = 'Back to browser';
10999 } elsif ($spec =~ $REDIS_URL_RE) {
11000 # Bare redis:// URL -- scan keys and present a picker.
11001 # Parse redis://[:password@]host[:port][/db]
11002 0 my ($password, $hostport, $db_idx);
11003 0 ▶ if ($spec =~ m{\Aredis://(?::([^@]*)@)?([^/]+)(?:/(\d+))?\z}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[\Aredis://(?::([^\@]*)@)?([^/]+)(?:/(\d+))?\z]iu) 0 0 0
11004 0 ($password, $hostport, $db_idx) = ($1, $2, $3);
11005 }
11006 0 ▶ $hostport //= 'localhost:6379';
Condition0%0/2
$hostport //= "localhost:6379"
l!l
00
MC/DC0%0/1
$hostport //= "localhost:6379"
$hostport
Truth table0%0/2
A
A= $hostport
Aresult
11
00
11007 0 $db_label = "redis://$hostport";
11008 0 $back_url = '/';
11009 0 $back_label = 'Home';
11010  
11011 0 eval {
11012 # Load Redis::Fast if available, fall back to Redis.
11013 # Use the file-path form ("Redis/Fast.pm") because require with a
11014 # plain string treats it as a filename, not a module name.
11015 0 my $pkg;
11016 0 for my $mod ('Redis::Fast', 'Redis') {
11017 0 (my $file = $mod) =~ s{::}{/}g;
11018 0 ▶ if (eval { require "$file.pm"; 1 }) { $pkg = $mod; last }
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require "$file.pm"; 1 } }) 0 0 0
11019 }
11020 0 ▶ croak 'Redis or Redis::Fast must be installed to browse a Redis server'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 0 0
11021 unless $pkg;
11022  
11023 0 my $redis = $pkg->new(
11024 server => $hostport,
11025 cnx_timeout => 5,
11026 read_timeout => 10,
11027 );
11028 0 ▶ $redis->auth($password) if defined $password && length $password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and length $password 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
11029 0 ▶ $redis->select(int($db_idx)) if defined $db_idx && int($db_idx) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $db_idx and int $db_idx > 0 0 0 0
Condition0%0/3
defined $db_idx and int $db_idx > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $db_idx and int $db_idx > 0
defined $db_idx int $db_idx > 0
Truth table0%0/3
A and B
A= defined $db_idxB= int $db_idx > 0
ABresult
0X0
100
111
11030  
11031 # SCAN in a loop; stop at 200 keys -- enough for a picker.
11032 0 my $cursor = 0;
11033 0 my @all_keys;
11034 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor ne 0 and @all_keys < 200 and do { do { () = $redis->scan($cursor, "COUNT", 100); $cursor = $new_cursor; push @all_keys, @{$batch // [];} } } 0 0 0
Condition0%0/3
$cursor ne 0 and @all_keys < 200
!ll&&!rl&&r
000
MC/DC0%0/2
$cursor ne 0 and @all_keys < 200
$cursor ne 0 @all_keys < 200
Truth table0%0/3
A and B
A= $cursor ne 0B= @all_keys < 200
ABresult
0X0
100
111
11035 0 my ($new_cursor, $batch) = $redis->scan($cursor, 'COUNT', 100);
11036 0 $cursor = $new_cursor;
11037 0 ▶ push @all_keys, @{ $batch // [] };
Condition0%0/2
$batch // []
l!l
00
MC/DC0%0/1
$batch // []
$batch
Truth table0%0/2
A
A= $batch
Aresult
11
00
11038 } while ($cursor ne '0' && @all_keys < 200);
11039  
11040 0 my $tables_url = '/tables?l=' . url_escape($spec);
11041 0 for my $key (sort @all_keys) {
11042 0 my $open_url = '/open?path=' . url_escape("redis:$key|$spec")
11043 . '&back=' . url_escape($tables_url)
11044 . '&back_label=Redis+keys';
11045 0 push @tables, { name => $key, columns => [], href => $open_url };
11046 }
11047 };
11048 0 ▶ return $self->reply->not_found if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
11049 } elsif ($spec =~ $MARIADB_SERVER_RE) {
11050 0 my $server = $1;
11051 0 $db_label = $server;
11052 0 $back_url = '/';
11053 0 $back_label = 'Home';
11054 0 my $found = eval {
11055 Database::BI::Model::DataSource->mariadb_tables($server,
11056 0 ▶ ($self->app->config('mariadb_servers') // {})->{$server});
Condition0%0/2
$self->app->config("mariadb_servers") // {}
l!l
00
MC/DC0%0/1
$self->app->config("mariadb_servers") // {}
$self->app->config("mariadb_servers")
Truth table0%0/2
A
A= $self->app->config("mariadb_servers")
Aresult
11
00
11057 };
11058 0 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
11059 0 return $self->render(
11060 template => "$platform/$language/home",
11061 handler => 'tt',
11062 format => 'html',
11063 tables => [],
11064 title => 'Error',
11065 error => $self->_i18n('error_file_open', $spec, _tidy_error($@)),
11066 back_url => '/',
11067 back_label => 'Home',
11068 language => $language,
11069 );
11070 }
11071 0 for my $found_table (@{$found}) {
11072 0 my ($name, $cols) = @{$found_table}{qw(name columns)};
11073 # Database::Abstraction refuses this name, so the card says so.
11074 0 ▶ if ($name !~ $TABLE_NAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$TABLE_NAME_RE/u) 0 0 0
11075 0 (my $safe = $name) =~ s/[^A-Za-z0-9_]/_/g;
11076 0 ▶ $safe = "_$safe" unless $safe =~ /\A[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $safe =~ /\A[A-Za-z_]/u 0 0 0
11077 0 push @tables, { name => $name, columns => $cols, unusable => 1, safe_name => $safe, on_server => 1 };
11078 0 next;
11079 }
11080 0 my $open_url = '/open?path=' . url_escape("mariadb:$server/$name")
11081 . '&back=' . url_escape('/tables?l=' . $spec)
11082 . '&back_label=' . url_escape(ucfirst($db_label) . ' schema');
11083 0 push @tables, { name => $name, columns => $cols, href => $open_url };
11084 }
11085 } else {
11086 4 return $self->reply->not_found;
11087 }
11088  
11089 5 ▶ if (defined $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_path) 5 0 5
11090 # A JSON file's tables are its lists of objects; a SQLite file's are
11091 # listed by Database::Join.
11092 my $schema = $file_path =~ /\.json\z/i
11093 0 ? eval { Database::BI::Model::DataSource->json_lists($file_path) }
11094 5 ▶ : eval { require Database::Join::Catalog; Database::Join::Catalog->schema(file => $file_path) };
Branch: 1 branch
BranchTrueFalseTotal
$file_path =~ /\.json\z/iu ? : 0 5 5
11095 5 ▶ return $self->reply->not_found unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 5 5
11096 5 for my $table_info (@{$schema}) {
11097 12 my $tname = $table_info->{name};
11098 12 my @cols = @{ $table_info->{columns} };
11099 # Database::Abstraction refuses this name, so the card says so
11100 # rather than linking to an error. A JSON list's name never
11101 # reaches SQL, so any name opens.
11102 12 ▶ if ($tname !~ $TABLE_NAME_RE && $file_path !~ /\.json\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tname =~ /$TABLE_NAME_RE/u or $file_path =~ /\.json\z/iu) 0 12 12
Condition33%1/3
$tname =~ /$TABLE_NAME_RE/u or $file_path =~ /\.json\z/iu
l!l&&r!l&&!r
1200
MC/DC0%0/2
$tname =~ /$TABLE_NAME_RE/u or $file_path =~ /\.json\z/iu
$tname =~ /$TABLE_NAME_RE/u $file_path =~ /\.json\z/iu
Truth table33%1/3
A or B
A= $tname =~ /$TABLE_NAME_RE/uB= $file_path =~ /\.json\z/iu
ABresult
1X1
011
000
11103 0 (my $safe = $tname) =~ s/[^A-Za-z0-9_]/_/g;
11104 0 ▶ $safe = "_$safe" unless $safe =~ /\A[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $safe =~ /\A[A-Za-z_]/u 0 0 0
11105 0 push @tables, { name => $tname, columns => \@cols, unusable => 1, safe_name => $safe };
11106 0 next;
11107 }
11108 12 my $open_url = '/open?path=' . url_escape($file_path)
11109 . '&it=' . url_escape($tname)
11110 . '&back=' . url_escape('/tables?l=' . $spec)
11111 . '&back_label=' . url_escape(ucfirst($db_label) . ' schema');
11112 12 push @tables, { name => $tname, columns => \@cols, href => $open_url };
11113 }
11114 }
11115  
11116 $self->render(
11117 5 template => "$platform/$language/tables",
11118 handler => 'tt',
11119 format => 'html',
11120 title => ucfirst($db_label) . ' -- Tables',
11121 db_label => $db_label,
11122 tables => \@tables,
11123 spec => $spec,
11124 back_url => $back_url,
11125 back_label => $back_label,
11126 language => $language,
11127 );
11128 }
11129  
11130 =head2 pivot_view
11131  
11132 C<GET /pivot> -- Crosstab / pivot table view.
11133  
11134 Accepts the same C<l=>, C<j=>, C<f=> pipeline params as C</join>, plus:
11135  
11136 rows=<col>[,<col>...] Row dimensions (required, comma-separated column names)
11137 cols=<col>[,<col>...] Column dimensions (optional)
11138 m=<col>:<fn>:<as> Measure spec, repeatable (required); col may be empty for count
11139 h=<as>:<op>:<val> Post-aggregation HAVING filter (repeatable, optional)
11140 totals=1 Include row subtotals, column subtotals, and grand total
11141 scheme=<name> Colour scheme for cell shading (optional)
11142 max_cols=<n> Collapse column headers beyond top-N into "Other"
11143 page= / per_page= Pagination over row groups
11144 format=csv|sqlite|json Export the pivot grid
11145  
11146 =head3 API SPECIFICATION
11147  
11148 =head4 INPUT
11149  
11150 The query parameters above, with C<l=> required as well.
11151  
11152 =head4 OUTPUT
11153  
11154 200 text/html (or the export format) The grid.
11155 400 text/plain 'Missing l= parameter', error_pivot_rows_required,
11156 error_pivot_measure_req, error_pivot_measure_invalid,
11157 error_measure_column, error_pivot_dim_not_found or error_topn_col.
11158 404 text/plain The source cannot be opened.
11159  
11160 =head3 FORMAL SPECIFICATION
11161  
11162 pivot_view
11163 l?, rows?, m? : seq Char
11164 response! : HTTP
11165 ──────────
11166 l? = ⟨⟩ ∨ rows? = ⟨⟩ ∨ m? = ⟨⟩ ⇒ response!.status = 400
11167 l? ≠ ⟨⟩ ∧ ¬ opens(l?) ⇒ response!.status = 404
11168  
11169 =cut
11170  
11171 49 ▶sub pivot_view ($self) {
Subroutine pivot_view: called CC=129 SCAR=83.0
Pod documented
11172 49 ▶ my $left_spec = $self->param('l') // '';
Condition50%1/2
$self->param("l") // ""
l!l
490
MC/DC0%0/1
$self->param("l") // ""
$self->param("l")
Truth table50%1/2
A
A= $self->param("l")
Aresult
11
00
11173 49 ▶ return $self->render(text => 'Missing l= parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 1 48 49
11174 unless length $left_spec;
11175  
11176 48 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
048
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
11177 48 my $back2 = _safe_back_url($self->param('back2'));
11178 48 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
048
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
11179  
11180 48 ▶ my $rows_str = $self->param('rows') // '';
Condition100%2/2
$self->param("rows") // ""
l!l
642
MC/DC100%1/1
$self->param("rows") // ""
$self->param("rows")
Truth table100%2/2
A
A= $self->param("rows")
Aresult
11
00
11181 48 ▶ return $self->render(text => $self->_i18n('error_pivot_rows_required'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $rows_str 42 6 48
11182 unless length $rows_str;
11183  
11184 6 ▶ my @measure_strs = @{ $self->every_param('m') // [] };
Condition50%1/2
$self->every_param("m") // []
l!l
60
MC/DC0%0/1
$self->every_param("m") // []
$self->every_param("m")
Truth table50%1/2
A
A= $self->every_param("m")
Aresult
11
00
11185 6 ▶ return $self->render(text => $self->_i18n('error_pivot_measure_req'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless @measure_strs 1 5 6
11186 unless @measure_strs;
11187  
11188 # Split dimensions on comma. A dimension name containing a comma
11189 # would produce an ambiguous split; that must be rejected.
11190 5 my @rows = split /,/, $rows_str, -1;
11191 5 ▶ my @cols = split /,/, ($self->param('cols') // ''), -1;
Condition50%1/2
$self->param("cols") // ""
l!l
05
MC/DC0%0/1
$self->param("cols") // ""
$self->param("cols")
Truth table50%1/2
A
A= $self->param("cols")
Aresult
11
00
11192 0 @cols = grep { length $_ } @cols;
11193  
11194 5 my @measures;
11195 5 for my $ms (@measure_strs) {
11196 5 my $m = _parse_measure_param($ms);
11197 5 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless $m 1 4 5
11198 text => $self->_i18n('error_pivot_measure_invalid', $ms, join(' ', @MEASURE_FNS)),
11199 status => 400,
11200 ) unless $m;
11201 4 push @measures, $m;
11202 }
11203  
11204 4 ▶ my $show_totals = ($self->param('totals') // '') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('totals') // '' ? : 0 4 4
Condition50%1/2
$self->param("totals") // ""
l!l
04
MC/DC0%0/1
$self->param("totals") // ""
$self->param("totals")
Truth table50%1/2
A
A= $self->param("totals")
Aresult
11
00
11205 4 ▶ my $scheme = $self->param('scheme') // '';
Condition50%1/2
$self->param("scheme") // ""
l!l
04
MC/DC0%0/1
$self->param("scheme") // ""
$self->param("scheme")
Truth table50%1/2
A
A= $self->param("scheme")
Aresult
11
00
11206 4 ▶ my $max_cols_n = int($self->param('max_cols') || 0);
Condition50%1/2
$self->param('max_cols') || 0
l!l
04
MC/DC0%0/1
$self->param('max_cols') || 0
$self->param('max_cols')
Truth table50%1/2
A
A= $self->param('max_cols')
Aresult
11
00
11207 4 ▶ my $format = lc($self->param('format') // '');
Condition100%2/2
$self->param("format") // ""
l!l
13
MC/DC100%1/1
$self->param("format") // ""
$self->param("format")
Truth table100%2/2
A
A= $self->param("format")
Aresult
11
00
11208  
11209 # Build the data pipeline.
11210 4 my ($src, $records, $columns) = $self->_run_pipeline_source;
11211 4 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 3 4
11212 unless defined $records;
11213  
11214 # span= buckets one dimension, which for a pivot is one of the rows= columns;
11215 # the dimension itself may be any of them, so with more than one it is tcol=
11216 # that says which. See _span_calc_spec.
11217 3 my $span = $self->_span_calc_spec($columns, 'rows');
11218 3 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 0 3 3
Condition33%1/3
$span and $span->{"error"}
!ll&&!rl&&r
300
MC/DC0%0/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table33%1/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
11219  
11220 # Calculated fields (?c=name:expr) -- evaluated before aggregation so calc
11221 # columns are available as row/col dimensions and measure columns.
11222 3 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
11223 3 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 3 3
11224 0 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
11225  
11226 3 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 3 3
11227  
11228 3 my %col_set = map { $_ => 1 } @{$columns};
11229  
11230 3 for my $dim (@rows, @cols) {
11231 return $self->render(
11232 text => $self->_i18n('error_pivot_dim_not_found', $dim),
11233 status => 400,
11234 3 ▶ ) unless $col_set{$dim};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$dim} 1 2 3
11235 }
11236  
11237 2 ▶ if ($span) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span) 0 2 2
11238 0 ▶ @rows = map { $_ eq $span->{src} ? $span->{col} : $_ } @rows;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $span->{'src'} ? : 0 0 0
11239 0 ▶ @cols = map { $_ eq $span->{src} ? $span->{col} : $_ } @cols;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $span->{'src'} ? : 0 0 0
11240 }
11241  
11242 2 for my $m (@measures) {
11243 2 ▶ next unless defined $m->{col} && length $m->{col};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"col"} and length $m->{"col"} 0 2 2
Condition33%1/3
defined $m->{"col"} and length $m->{"col"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $m->{"col"} and length $m->{"col"}
defined $m->{"col"} length $m->{"col"}
Truth table33%1/3
A and B
A= defined $m->{"col"}B= length $m->{"col"}
ABresult
0X0
100
111
11244 return $self->render(
11245 text => $self->_i18n('error_measure_column', $m->{col}),
11246 status => 400,
11247 2 ▶ ) unless $col_set{$m->{col}};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$m->{"col"}} 0 2 2
11248 }
11249  
11250 2 my $pipeline = { source => $src, records => $records };
11251  
11252 # -----------------------------------------------------------------------
11253 # Aggregate: one _grouped_totals call per measure, grouped by all
11254 # row AND column dimensions together.
11255 # -----------------------------------------------------------------------
11256 2 my (%cells, %row_combos, %col_combos);
11257 2 my (@row_key_order, @col_key_order);
11258 2 my (%row_seen, %col_seen);
11259  
11260 2 for my $m (@measures) {
11261 2 ▶ my $cv = (defined $m->{col} && length $m->{col}) ? $m->{col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{'col'} && length $m->{'col'} ? : 2 0 2
Condition33%1/3
defined $m->{"col"} and length $m->{"col"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $m->{"col"} and length $m->{"col"}
defined $m->{"col"} length $m->{"col"}
Truth table33%1/3
A and B
A= defined $m->{"col"}B= length $m->{"col"}
ABresult
0X0
100
111
11262 my $agg = $self->_grouped_totals($pipeline, {
11263 by => [@rows, @cols],
11264 fn => $m->{fn},
11265 val => $cv,
11266 as => $m->{as},
11267 2 });
11268 2 for my $row (@$agg) {
11269 8 ▶ my $rkey = join("\x00", map { $row->{$_} // '' } @rows);
Condition50%1/2
$row->{$_} // ""
l!l
80
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
11270 0 ▶ my $ckey = join("\x00", map { $row->{$_} // '' } @cols);
Condition0%0/2
$row->{$_} // ""
l!l
00
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
Aresult
11
00
11271 8 ▶ unless ($row_seen{$rkey}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row_seen{$rkey}++) 8 0 8
11272 8 push @row_key_order, $rkey;
11273 8 ▶ $row_combos{$rkey} = { map { $_ => ($row->{$_} // '') } @rows };
Condition50%1/2
$row->{$_} // ""
l!l
80
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
11274 }
11275 8 ▶ unless ($col_seen{$ckey}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col_seen{$ckey}++) 2 6 8
11276 2 push @col_key_order, $ckey;
11277 0 ▶ $col_combos{$ckey} = { map { $_ => ($row->{$_} // '') } @cols };
Condition0%0/2
$row->{$_} // ""
l!l
00
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
Aresult
11
00
11278 }
11279 8 $cells{$rkey}{$ckey}{$m->{as}} = $row->{$m->{as}};
11280 }
11281 }
11282  
11283 # -----------------------------------------------------------------------
11284 # Post-aggregation HAVING filters (h=<as>:<op>:<val>) applied row-by-row.
11285 # Each filter operates on a virtual row whose keys are the measure aliases.
11286 # -----------------------------------------------------------------------
11287 2 ▶ my @h_specs = @{ $self->every_param('h') // [] };
Condition50%1/2
$self->every_param("h") // []
l!l
20
MC/DC0%0/1
$self->every_param("h") // []
$self->every_param("h")
Truth table50%1/2
A
A= $self->every_param("h")
Aresult
11
00
11288 2 ▶ if (@h_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@h_specs) 0 2 2
11289 0 my @keep;
11290 0 for my $rkey (@row_key_order) {
11291 # Build a virtual row containing each measure's cell value
11292 # (using first col-combo as representative, or grand-total style).
11293 # For HAVING, we filter on the *row subtotal* value, not a single cell.
11294 # We'll compute a temp subtotal row for each row key.
11295 0 my %vrow;
11296 0 for my $m (@measures) {
11297 # Sum across cols for this row as representative value.
11298 0 my $v = 0;
11299 0 for my $ck (@col_key_order) {
11300 0 my $cv = $cells{$rkey}{$ck}{$m->{as}};
11301 0 ▶ $v += $cv if defined $cv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cv 0 0 0
11302 }
11303 0 $vrow{$m->{as}} = $v;
11304 }
11305 0 my $ok = 1;
11306 0 for my $hs (@h_specs) {
11307 0 my $passed = _apply_filter_spec([\%vrow], $hs);
11308 0 ▶ $ok = 0 unless @$passed;
Branch: 1 branch
BranchTrueFalseTotal
unless @$passed 0 0 0
11309 }
11310 0 ▶ push @keep, $rkey if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
11311 }
11312 0 @row_key_order = @keep;
11313 }
11314  
11315 # -----------------------------------------------------------------------
11316 # Top-N rows: keep only the top-N row groups (ranked by measure sum
11317 # across all col combos); aggregate the remaining rows as an "Other" row.
11318 # -----------------------------------------------------------------------
11319 {
11320 2 ▶ my $topn_n = _whole_number($self->param('topn')) // 0;
Condition50%1/2
_whole_number($self->param("topn")) // 0
l!l
02
MC/DC0%0/1
_whole_number($self->param("topn")) // 0
_whole_number($self->param("topn"))
Truth table50%1/2
A
A= _whole_number($self->param("topn"))
Aresult
11
00
11321 2 ▶ my $topn_col = $self->param('topn_col') // '';
Condition50%1/2
$self->param("topn_col") // ""
l!l
02
MC/DC0%0/1
$self->param("topn_col") // ""
$self->param("topn_col")
Truth table50%1/2
A
A= $self->param("topn_col")
Aresult
11
00
11322  
11323 2 ▶ if ($topn_n > 0 && @row_key_order > $topn_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topn_n > 0 and @row_key_order > $topn_n) 0 2 2
Condition33%1/3
$topn_n > 0 and @row_key_order > $topn_n
!ll&&!rl&&r
200
MC/DC0%0/2
$topn_n > 0 and @row_key_order > $topn_n
$topn_n > 0 @row_key_order > $topn_n
Truth table33%1/3
A and B
A= $topn_n > 0B= @row_key_order > $topn_n
ABresult
0X0
100
111
11324 0 my %m_aliases = map { $_->{as} => 1 } @measures;
11325 0 ▶ if (length $topn_col && !$m_aliases{$topn_col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $topn_col and not $m_aliases{$topn_col}) 0 0 0
Condition0%0/3
length $topn_col and not $m_aliases{$topn_col}
!ll&&!rl&&r
000
MC/DC0%0/2
length $topn_col and not $m_aliases{$topn_col}
length $topn_col not $m_aliases{$topn_col}
Truth table0%0/3
A and not(B)
A= length $topn_colB= not $m_aliases{$topn_col}
ABresult
0X0
100
111
11326 0 return $self->render(
11327 text => $self->_i18n('error_topn_col', $topn_col),
11328 status => 400,
11329 );
11330 }
11331 0 ▶ my $sort_as = length($topn_col) ? $topn_col : $measures[0]{as};
Branch: 1 branch
BranchTrueFalseTotal
length $topn_col ? : 0 0 0
11332  
11333 # Sort rows by descending sum of sort_as across all column combos.
11334 my @sorted_rkeys = sort {
11335 0 ▶ (do { my $s = 0; $s += ($cells{$b}{$_}{$sort_as} // 0) for @col_key_order; $s })
Condition0%0/2
$cells{$b}{$_}{$sort_as} // 0
l!l
00
MC/DC0%0/1
$cells{$b}{$_}{$sort_as} // 0
$cells{$b}{$_}{$sort_as}
Truth table0%0/2
A
A= $cells{$b}{$_}{$sort_as}
Aresult
11
00
11336 <=>
11337 0 ▶ (do { my $s = 0; $s += ($cells{$a}{$_}{$sort_as} // 0) for @col_key_order; $s })
Condition0%0/2
$cells{$a}{$_}{$sort_as} // 0
l!l
00
MC/DC0%0/1
$cells{$a}{$_}{$sort_as} // 0
$cells{$a}{$_}{$sort_as}
Truth table0%0/2
A
A= $cells{$a}{$_}{$sort_as}
Aresult
11
00
11338 } @row_key_order;
11339  
11340 0 my @top_rkeys = @sorted_rkeys[0 .. $topn_n - 1];
11341 0 my @rest_rkeys = @sorted_rkeys[$topn_n .. $#sorted_rkeys];
11342  
11343 0 ▶ if (@rest_rkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest_rkeys) { } 0 0 0
11344 0 my %top_rkey_set = map { $_ => 1 } @top_rkeys;
11345 my $other_rec = [
11346 grep {
11347 0 my $r = $_;
11348 0 ▶ !$top_rkey_set{ join("\x00", map { $r->{$_} // '' } @rows) }
Condition0%0/2
$r->{$_} // ""
l!l
00
MC/DC0%0/1
$r->{$_} // ""
$r->{$_}
Truth table0%0/2
A
A= $r->{$_}
Aresult
11
00
11349 } @$records
11350 ];
11351 0 ▶ if (@$other_rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$other_rec) { } 0 0 0
11352 0 my $ork = '__topn_other__';
11353 0 $row_combos{$ork} = { map { $_ => 'Other' } @rows };
11354 0 for my $m (@measures) {
11355 0 ▶ my $cv = (defined $m->{col} && length $m->{col}) ? $m->{col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{'col'} && length $m->{'col'} ? : 0 0 0
Condition0%0/3
defined $m->{"col"} and length $m->{"col"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $m->{"col"} and length $m->{"col"}
defined $m->{"col"} length $m->{"col"}
Truth table0%0/3
A and B
A= defined $m->{"col"}B= length $m->{"col"}
ABresult
0X0
100
111
11356 my $oa = $self->_grouped_totals(
11357 { source => undef, records => $other_rec },
11358 { by => [@cols], fn => $m->{fn}, val => $cv, as => $m->{as} },
11359 0 );
11360 0 for my $row (@$oa) {
11361 0 ▶ my $ck = join("\x00", map { $row->{$_} // '' } @cols);
Condition0%0/2
$row->{$_} // ""
l!l
00
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
Aresult
11
00
11362 0 $cells{$ork}{$ck}{$m->{as}} = $row->{$m->{as}};
11363 }
11364 }
11365 0 @row_key_order = (@top_rkeys, $ork);
11366 } else {
11367 0 @row_key_order = @top_rkeys;
11368 }
11369 } else {
11370 0 @row_key_order = @top_rkeys;
11371 }
11372 }
11373 }
11374  
11375 # -----------------------------------------------------------------------
11376 # Subtotals and grand total (three additional _grouped_totals calls
11377 # per measure when totals=1).
11378 # -----------------------------------------------------------------------
11379 2 my (%row_subtotals, %col_subtotals, %grand_total);
11380  
11381 2 ▶ if ($show_totals) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_totals) 0 2 2
11382 0 for my $m (@measures) {
11383 0 ▶ my $cv = (defined $m->{col} && length $m->{col}) ? $m->{col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{'col'} && length $m->{'col'} ? : 0 0 0
Condition0%0/3
defined $m->{"col"} and length $m->{"col"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $m->{"col"} and length $m->{"col"}
defined $m->{"col"} length $m->{"col"}
Truth table0%0/3
A and B
A= defined $m->{"col"}B= length $m->{"col"}
ABresult
0X0
100
111
11384  
11385 # Row subtotals: group by @rows only.
11386 my $rsub = $self->_grouped_totals($pipeline, {
11387 by => [@rows],
11388 fn => $m->{fn},
11389 val => $cv,
11390 as => $m->{as},
11391 0 });
11392 0 for my $r (@$rsub) {
11393 0 ▶ my $rk = join("\x00", map { $r->{$_} // '' } @rows);
Condition0%0/2
$r->{$_} // ""
l!l
00
MC/DC0%0/1
$r->{$_} // ""
$r->{$_}
Truth table0%0/2
A
A= $r->{$_}
Aresult
11
00
11394 0 $row_subtotals{$rk}{$m->{as}} = $r->{$m->{as}};
11395 }
11396  
11397 # Column subtotals: group by @cols only.
11398 0 ▶ if (@cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols) 0 0 0
11399 my $csub = $self->_grouped_totals($pipeline, {
11400 by => [@cols],
11401 fn => $m->{fn},
11402 val => $cv,
11403 as => $m->{as},
11404 0 });
11405 0 for my $c (@$csub) {
11406 0 ▶ my $ck = join("\x00", map { $c->{$_} // '' } @cols);
Condition0%0/2
$c->{$_} // ""
l!l
00
MC/DC0%0/1
$c->{$_} // ""
$c->{$_}
Truth table0%0/2
A
A= $c->{$_}
Aresult
11
00
11407 0 $col_subtotals{$ck}{$m->{as}} = $c->{$m->{as}};
11408 }
11409 }
11410  
11411 # Grand total: group by nothing (all rows -> one result row).
11412 my $gtot = $self->_grouped_totals($pipeline, {
11413 by => [],
11414 fn => $m->{fn},
11415 val => $cv,
11416 as => $m->{as},
11417 0 });
11418 0 ▶ $grand_total{$m->{as}} = $gtot->[0]{$m->{as}} if @$gtot;
Branch: 1 branch
BranchTrueFalseTotal
if @$gtot 0 0 0
11419 }
11420 }
11421  
11422 # -----------------------------------------------------------------------
11423 # max_cols: collapse the tail of column combos beyond top-N into "Other".
11424 # The top-N are ranked by the first measure's column subtotal (or cell sum).
11425 # -----------------------------------------------------------------------
11426 2 ▶ if ($max_cols_n > 0 && @col_key_order > $max_cols_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_cols_n > 0 and @col_key_order > $max_cols_n) 0 2 2
Condition33%1/3
$max_cols_n > 0 and @col_key_order > $max_cols_n
!ll&&!rl&&r
200
MC/DC0%0/2
$max_cols_n > 0 and @col_key_order > $max_cols_n
$max_cols_n > 0 @col_key_order > $max_cols_n
Truth table33%1/3
A and B
A= $max_cols_n > 0B= @col_key_order > $max_cols_n
ABresult
0X0
100
111
11427 0 my $first_as = $measures[0]{as};
11428 my @sorted_ckeys = sort {
11429 0 ( $show_totals
11430 ? ($col_subtotals{$b}{$first_as} // 0)
11431 0 ▶ : (do { my $s = 0; $s += ($cells{$_}{$b}{$first_as} // 0) for @row_key_order; $s })
Branch: 1 branch
BranchTrueFalseTotal
$show_totals ? : 0 0 0
Condition0%0/4
$cells{$_}{$b}{$first_as} // 0
l!l
00
$col_subtotals{$a}{$first_as} // 0
l!l
00
MC/DC0%0/1
$cells{$_}{$b}{$first_as} // 0
$cells{$_}{$b}{$first_as}
MC/DC0%0/1
$col_subtotals{$a}{$first_as} // 0
$col_subtotals{$a}{$first_as}
Truth table0%0/2
A
A= $cells{$_}{$b}{$first_as}
Aresult
11
00
Truth table0%0/2
A
A= $col_subtotals{$a}{$first_as}
Aresult
11
00
11432 )
11433 <=>
11434 ( $show_totals
11435 ? ($col_subtotals{$a}{$first_as} // 0)
11436 0 ▶ : (do { my $s = 0; $s += ($cells{$_}{$a}{$first_as} // 0) for @row_key_order; $s })
Branch: 1 branch
BranchTrueFalseTotal
$show_totals ? : 0 0 0
Condition0%0/4
$col_subtotals{$b}{$first_as} // 0
l!l
00
$cells{$_}{$a}{$first_as} // 0
l!l
00
MC/DC0%0/1
$col_subtotals{$b}{$first_as} // 0
$col_subtotals{$b}{$first_as}
MC/DC0%0/1
$cells{$_}{$a}{$first_as} // 0
$cells{$_}{$a}{$first_as}
Truth table0%0/2
A
A= $col_subtotals{$b}{$first_as}
Aresult
11
00
Truth table0%0/2
A
A= $cells{$_}{$a}{$first_as}
Aresult
11
00
11437 )
11438 } @col_key_order;
11439 0 my @top_ckeys = @sorted_ckeys[0 .. $max_cols_n - 1];
11440 0 my @rest_ckeys = @sorted_ckeys[$max_cols_n .. $#sorted_ckeys];
11441 0 ▶ if (@rest_ckeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest_ckeys) { } 0 0 0
11442 0 my $other_key = '__other__';
11443 0 $col_combos{$other_key} = { map { $_ => 'Other' } @cols };
11444 0 for my $rk (@row_key_order) {
11445 0 for my $m (@measures) {
11446 0 my ($v, $have) = (0, 0);
11447 0 for my $ck (@rest_ckeys) {
11448 0 my $cv = $cells{$rk}{$ck}{$m->{as}};
11449 0 ▶ if (defined $cv) { $v += $cv; $have = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cv) 0 0 0
11450 }
11451 0 ▶ $cells{$rk}{$other_key}{$m->{as}} = $have ? $v : undef;
Branch: 1 branch
BranchTrueFalseTotal
$have ? : 0 0 0
11452 }
11453 }
11454 0 @col_key_order = (@top_ckeys, $other_key);
11455 } else {
11456 0 @col_key_order = @top_ckeys;
11457 }
11458 }
11459  
11460 # -----------------------------------------------------------------------
11461 # Export path: build flat records from the grid.
11462 # -----------------------------------------------------------------------
11463 2 ▶ if ($format && $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format and $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/u) 1 1 2
Condition66%2/3
$format and $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/u
!ll&&!rl&&r
101
MC/DC50%1/2
$format and $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/u
$format $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/u
Truth table66%2/3
A and B
A= $formatB= $format =~ /\A(?:csv|sqlite|sql|json|xlsx)\z/u
ABresult
0X0
100
111
11464 1 my @grid_records;
11465 1 for my $rk (@row_key_order) {
11466 4 for my $ck (@col_key_order) {
11467 4 my %rec;
11468 4 ▶ $rec{$_} = $row_combos{$rk}{$_} // '' for @rows;
Condition50%1/2
$row_combos{$rk}{$_} // ""
l!l
40
MC/DC0%0/1
$row_combos{$rk}{$_} // ""
$row_combos{$rk}{$_}
Truth table50%1/2
A
A= $row_combos{$rk}{$_}
Aresult
11
00
11469 4 ▶ $rec{$_} = $col_combos{$ck}{$_} // '' for @cols;
Condition0%0/2
$col_combos{$ck}{$_} // ""
l!l
00
MC/DC0%0/1
$col_combos{$ck}{$_} // ""
$col_combos{$ck}{$_}
Truth table0%0/2
A
A= $col_combos{$ck}{$_}
Aresult
11
00
11470 4 ▶ $rec{$_->{as}} = $cells{$rk}{$ck}{$_->{as}} // '' for @measures;
Condition50%1/2
$cells{$rk}{$ck}{$_->{"as"}} // ""
l!l
40
MC/DC0%0/1
$cells{$rk}{$ck}{$_->{"as"}} // ""
$cells{$rk}{$ck}{$_->{"as"}}
Truth table50%1/2
A
A= $cells{$rk}{$ck}{$_->{"as"}}
Aresult
11
00
11471 4 push @grid_records, \%rec;
11472 }
11473 }
11474 1 my @export_cols = (@rows, @cols, map { $_->{as} } @measures);
11475 1 ▶ return $self->_render_csv(\@grid_records, \@export_cols, 'pivot')
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "csv" 1 0 1
11476 if $format eq 'csv';
11477 0 ▶ return $self->_render_sqlite(\@grid_records, \@export_cols, 'pivot')
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "sqlite" or $format eq "sql" 0 0 0
Condition0%0/3
$format eq "sqlite" or $format eq "sql"
l!l&&r!l&&!r
000
MC/DC0%0/2
$format eq "sqlite" or $format eq "sql"
$format eq "sqlite" $format eq "sql"
Truth table0%0/3
A or B
A= $format eq "sqlite"B= $format eq "sql"
ABresult
1X1
011
000
11478 if $format eq 'sqlite' || $format eq 'sql';
11479 0 ▶ return $self->_render_json(\@grid_records, \@export_cols, 'pivot')
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "json" 0 0 0
11480 if $format eq 'json';
11481 0 ▶ if ($format eq 'xlsx') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "xlsx") 0 0 0
11482 0 $self->stash(buffer => 0);
11483 0 require Excel::Writer::XLSX;
11484 0 my $out = '';
11485 0 ▶ open my $fh, '>', \$out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \$out or return $self->render("text", "Cannot open XLSX buffer", "status", 500) 0 0 0
11486 or return $self->render(text => 'Cannot open XLSX buffer', status => 500);
11487 0 my $wb = Excel::Writer::XLSX->new($fh);
11488 0 my $ws = $wb->add_worksheet('pivot');
11489 0 $ws->write_row(0, 0, \@export_cols);
11490 0 my $row_idx = 1;
11491 0 for my $rec (@grid_records) {
11492 0 ▶ $ws->write_row($row_idx++, 0, [ map { $rec->{$_} // '' } @export_cols ]);
Condition0%0/2
$rec->{$_} // ""
l!l
00
MC/DC0%0/1
$rec->{$_} // ""
$rec->{$_}
Truth table0%0/2
A
A= $rec->{$_}
Aresult
11
00
11493 }
11494 0 $wb->close;
11495 0 $self->res->headers->content_type(
11496 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
11497 );
11498 0 $self->res->headers->content_disposition(
11499 'attachment; filename="pivot.xlsx"'
11500 );
11501 0 return $self->render(data => $out);
11502 }
11503 }
11504  
11505 # -----------------------------------------------------------------------
11506 # Pagination: slice @row_key_order using the N+1 trick.
11507 # Must come after the export early-return above (exports use the full set)
11508 # and after subtotals/grand total (computed from $pipeline, not @row_key_order).
11509 # -----------------------------------------------------------------------
11510 1 my ($page, $per_page, $offset) = $self->_pagination;
11511 1 my $total_row_groups = scalar @row_key_order;
11512 1 ▶ my $piv_has_next = ($total_row_groups > $offset + $per_page) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$total_row_groups > $offset + $per_page ? : 0 1 1
11513 1 ▶ @row_key_order = $offset < $total_row_groups
Branch: 2 branches
BranchTrueFalseTotal
$offset < $total_row_groups ? : 1 0 1
$piv_has_next ? : 0 1 1
11514 ? @row_key_order[$offset .. ($piv_has_next ? $offset + $per_page - 1 : $#row_key_order)]
11515 : ();
11516  
11517 1 my $piv_page_url = $self->req->url->clone;
11518 1 $piv_page_url->query->remove('page')->remove('per_page');
11519  
11520 # -----------------------------------------------------------------------
11521 # Build the template-ready data structure.
11522 # -----------------------------------------------------------------------
11523 my @col_combo_list = map {
11524 1 my $ck = $_;
11525 {
11526 key => $ck,
11527 dim_vals => $col_combos{$ck},
11528 0 ▶ label => join(' / ', map { $col_combos{$ck}{$_} // '' } @cols),
Condition0%0/2
$col_combos{$ck}{$_} // ""
l!l
00
MC/DC0%0/1
$col_combos{$ck}{$_} // ""
$col_combos{$ck}{$_}
Truth table0%0/2
A
A= $col_combos{$ck}{$_}
Aresult
11
00
11529 }
11530 } @col_key_order;
11531  
11532 1 my @grid_rows;
11533 1 for my $rk (@row_key_order) {
11534 4 my %row_cells;
11535 4 for my $ck (@col_key_order) {
11536 4 ▶ $row_cells{$ck} = $cells{$rk}{$ck} // {};
Condition50%1/2
$cells{$rk}{$ck} // {}
l!l
40
MC/DC0%0/1
$cells{$rk}{$ck} // {}
$cells{$rk}{$ck}
Truth table50%1/2
A
A= $cells{$rk}{$ck}
Aresult
11
00
11537 }
11538 push @grid_rows, {
11539 row_vals => $row_combos{$rk},
11540 cells => \%row_cells,
11541 4 ▶ subtotals => $show_totals ? ($row_subtotals{$rk} // {}) : {},
Branch: 1 branch
BranchTrueFalseTotal
$show_totals ? : 0 4 4
Condition0%0/2
$row_subtotals{$rk} // {}
l!l
00
MC/DC0%0/1
$row_subtotals{$rk} // {}
$row_subtotals{$rk}
Truth table0%0/2
A
A= $row_subtotals{$rk}
Aresult
11
00
11542 };
11543 }
11544  
11545 1 (my $left_label = $left_spec) =~ s/\A(?:table:|path:|url:)//;
11546  
11547 1 my ($platform, $language) = $self->_resolve_template;
11548 1 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 1 1
$back2 ? : 0 1 1
Condition22%2/9
$back2 // $back
l!l&&r!l&&!r
010
$back2_lbl or "Back"
l!l
00
$self->param("back_label") // "Back to table"
l!l
01
$self->param("back_label") // "Back to table"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
Truth table50%1/2
A
A= $self->param("back_label")
Aresult
11
00
Truth table0%0/2
A
A= $self->param("back_label")
Aresult
11
00
11549 handler => 'tt',
11550 template => "$platform/$language/pivot",
11551 format => 'html',
11552 title => 'Pivot: ' . join(', ', @rows),
11553 rows_dims => \@rows,
11554 cols_dims => \@cols,
11555 measures => \@measures,
11556 col_combos => \@col_combo_list,
11557 grid_rows => \@grid_rows,
11558 show_totals => $show_totals,
11559 col_subtotals => \%col_subtotals,
11560 grand_total => \%grand_total,
11561 scheme => $scheme,
11562 left_spec => $left_spec,
11563 left_label => $left_label,
11564 back_url => ($back2 // $back),
11565 back_label => ($back2 ? ($back2_lbl || 'Back') : ($self->param('back_label') // 'Back to table')),
11566 ($back2 ? (back2_url => $back, back2_label => ($self->param('back_label') // 'Back to table')) : ()),
11567 page => $page,
11568 per_page => $per_page,
11569 has_next => $piv_has_next,
11570 page_count => _page_count($total_row_groups, $per_page),
11571 page_base_url => $piv_page_url->to_string,
11572 total_row_groups => $total_row_groups,
11573 language => $language,
11574 );
11575 }
11576  
11577 =head2 stats_view
11578  
11579 C<GET /stats> -- Column statistics panel.
11580  
11581 Accepts the same C<l=>, C<j=>, C<f=> pipeline params as C</join>, plus C<p=N>
11582 to choose the percentile column (1 to 100, default 95), and C<range=name> with
11583 C<rcol=column> for a relative window of rows. Displays per-column statistics:
11584 row_count, null_count, distinct_count, and for numeric columns min, max, range,
11585 mean, median, the chosen percentile and sample standard deviation; first and
11586 last are shown for every column, being the first and last value the column
11587 holds.
11588  
11589 C<distinct_count> is computed through C<_grouped_totals>, so it agrees with the
11590 pivot view. The numeric statistics are computed in Perl by sorting the column's
11591 values, through the same C<_percentile_of>, C<_median_of>, C<_stdev_of> and
11592 C<_range_of> arithmetic the pivot's C<median>, C<pN>, C<stdev> and C<range>
11593 measures use -- so a median here and a median measure there are the same number.
11594  
11595 =head3 API SPECIFICATION
11596  
11597 =head4 INPUT
11598  
11599 The query parameters above, with C<l=> required.
11600  
11601 =head4 OUTPUT
11602  
11603 200 text/html The statistics.
11604 400 text/plain 'Missing l= parameter', or error_stats_percentile when p= is
11605 not a whole number from 1 to 100.
11606 404 text/plain The source cannot be opened.
11607  
11608 =head3 FORMAL SPECIFICATION
11609  
11610 stats_view
11611 l?, p? : seq Char
11612 response! : HTTP
11613 ──────────
11614 l? = ⟨⟩ ⇒ response!.status = 400
11615 p? ≠ ⟨⟩ ∧ p? ∉ { "1" .. "100" } ⇒ response!.status = 400
11616  
11617 =cut
11618  
11619 47 ▶sub stats_view ($self) {
Subroutine stats_view: called CC=28 SCAR=38.3
Pod documented
11620 47 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
461
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11621 47 ▶ return $self->render(text => 'Missing l= parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 2 45 47
11622 unless length $left_spec;
11623  
11624 # The percentile column is the reader's to choose; an unreadable one is a
11625 # 400 rather than a silent 95, so the page never claims a level it did not
11626 # apply.
11627 45 my $pct_arg = $self->param('p');
11628 45 my $percentile = 95;
11629 45 ▶ if (defined $pct_arg && length $pct_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pct_arg and length $pct_arg) 0 45 45
Condition33%1/3
defined $pct_arg and length $pct_arg
!ll&&!rl&&r
4500
MC/DC0%0/2
defined $pct_arg and length $pct_arg
defined $pct_arg length $pct_arg
Truth table33%1/3
A and B
A= defined $pct_argB= length $pct_arg
ABresult
0X0
100
111
11630 0 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless $pct_arg =~ /\A\d{1,3}\z/u and $pct_arg >= 1 and $pct_arg <= 100 0 0 0
Condition0%0/6
$pct_arg =~ /\A\d{1,3}\z/u and $pct_arg >= 1 and $pct_arg <= 100
!ll&&!rl&&r
000
$pct_arg =~ /\A\d{1,3}\z/u and $pct_arg >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$pct_arg =~ /\A\d{1,3}\z/u and $pct_arg >= 1 and $pct_arg <= 100
$pct_arg =~ /\A\d{1,3}\z/u $pct_arg >= 1 $pct_arg <= 100
Truth table0%0/4
A and B and C
A= $pct_arg =~ /\A\d{1,3}\z/uB= $pct_arg >= 1C= $pct_arg <= 100
ABCresult
0XX0
10X0
1100
1111
11631 text => $self->_i18n('error_stats_percentile', $pct_arg),
11632 status => 400,
11633 ) unless $pct_arg =~ /\A\d{1,3}\z/ && $pct_arg >= 1 && $pct_arg <= 100;
11634 0 $percentile = $pct_arg + 0;
11635 }
11636  
11637 45 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
045
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
11638 45 my $back2 = _safe_back_url($self->param('back2'));
11639 45 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
045
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
11640  
11641 45 my ($src, $records, $columns) = $self->_run_pipeline_source;
11642 45 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 3 42 45
11643 unless defined $records;
11644  
11645 42 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
11646 42 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 41 42
11647  
11648 41 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 40 41
11649  
11650 40 my $row_count = scalar @$records;
11651 40 my $pipeline = { source => $src, records => $records };
11652  
11653 40 my @stats;
11654 40 for my $col (@{$columns}) {
11655 # null_count = rows where cell is undef or '' (phase 2 definition).
11656 240 ▶ my @nonnull = grep { defined $_->{$col} && length($_->{$col}) } @$records;
Condition33%1/3
defined $_->{$col} && length $_->{$col}
!ll&&!rl&&r
001440
MC/DC0%0/2
defined $_->{$col} && length $_->{$col}
defined $_->{$col} length $_->{$col}
Truth table33%1/3
A && B
A= defined $_->{$col}B= length $_->{$col}
ABresult
0X0
100
111
11657 240 my $null_count = $row_count - scalar @nonnull;
11658  
11659 # distinct_count via _grouped_totals (count groups by the column).
11660 240 my $dist_rows = $self->_grouped_totals($pipeline, {
11661 by => [$col],
11662 fn => 'count',
11663 as => 'cnt',
11664 });
11665 240 my $distinct_count = scalar @$dist_rows;
11666  
11667 # first/last: the value the column holds in the first and the last row
11668 # that has one. $nonnull is in row order, so no second pass is needed
11669 # and the two agree with the pivot's first() and last() measures.
11670 240 ▶ my $first_val = $nonnull[0] ? $nonnull[0]{$col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$nonnull[0] ? : 240 0 240
11671 240 ▶ my $last_val = $nonnull[-1] ? $nonnull[-1]{$col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$nonnull[-1] ? : 240 0 240
11672  
11673 # Numeric statistics, through the same arithmetic the pivot measures use.
11674 240 my @nums = grep { defined $_ } map { _numeric_cell($_->{$col}) } @nonnull;
11675 240 my ($min_val, $max_val, $avg_val, $median_val, $range_val, $pct_val, $stdev_val);
11676 240 ▶ if (@nums) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nums) 80 160 240
11677 80 my @sorted = sort { $a <=> $b } @nums;
11678 80 $min_val = $sorted[0];
11679 80 $max_val = $sorted[-1];
11680 80 my $sum = 0;
11681 80 $sum += $_ for @nums;
11682 80 $avg_val = $sum / @nums;
11683 # _median_of is _percentile_of at 50; a stdev of one value is undef,
11684 # which the template shows as a dash rather than as a spread of 0.
11685 80 $median_val = _median_of(\@sorted);
11686 80 $range_val = _range_of(\@sorted);
11687 80 $pct_val = _percentile_of(\@sorted, $percentile);
11688 80 $stdev_val = _stdev_of(\@sorted);
11689 }
11690  
11691 240 ▶ push @stats, {
Branch: 2 branches
BranchTrueFalseTotal
defined $min_val ? : 80 160 240
defined $stdev_val ? : 80 0 80
11692 col => $col,
11693 row_count => $row_count,
11694 null_count => $null_count,
11695 distinct_count => $distinct_count,
11696 first => $first_val,
11697 last => $last_val,
11698 (defined $min_val ? (
11699 min => $min_val,
11700 max => $max_val,
11701 range => sprintf('%.4g', $range_val),
11702 avg => sprintf('%.4g', $avg_val),
11703 median => sprintf('%.4g', $median_val),
11704 pct => sprintf('%.4g', $pct_val),
11705 stdev => (defined $stdev_val ? sprintf('%.4g', $stdev_val) : undef),
11706 ) : ()),
11707 };
11708 }
11709  
11710 40 (my $left_label = $left_spec) =~ s/\A(?:table:|path:|url:)//;
11711  
11712 40 my ($platform, $language) = $self->_resolve_template;
11713 40 ▶ $self->render(
Branch: 3 branches
BranchTrueFalseTotal
CORE::index($back, '?') != -1 ? : 0 40 40
$back2 ? : 0 40 40
$back2 ? : 0 40 40
Condition22%2/9
$back2 // $back
l!l&&r!l&&!r
0400
$back2_lbl or "Back"
l!l
00
$self->param("back_label") // "Back to table"
l!l
040
$self->param("back_label") // "Back to table"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
Truth table50%1/2
A
A= $self->param("back_label")
Aresult
11
00
Truth table0%0/2
A
A= $self->param("back_label")
Aresult
11
00
11714 handler => 'tt',
11715 template => "$platform/$language/stats",
11716 format => 'html',
11717 title => 'Column statistics',
11718 stats => \@stats,
11719 stats_percentile => $percentile,
11720 row_count => $row_count,
11721 left_spec => $left_spec,
11722 left_label => $left_label,
11723 drill_back_url => $back,
11724 drill_sep => (CORE::index($back, '?') >= 0 ? '&' : '?'),
11725 stats_url => $self->req->url->path_query,
11726 back_url => ($back2 // $back),
11727 back_label => ($back2 ? ($back2_lbl || 'Back') : ($self->param('back_label') // 'Back to table')),
11728 ($back2 ? (back2_url => $back, back2_label => ($self->param('back_label') // 'Back to table')) : ()),
11729 language => $language,
11730 );
11731 }
11732  
11733 # ---------------------------------------------------------------------------
11734 # Calculated-field CRUD (POST /calc/save, POST /calc/delete, GET /calc/list)
11735 # ---------------------------------------------------------------------------
11736  
11737 6 ▶sub calc_save ($self) {
Subroutine calc_save: called CC=7 SCAR=19.5
Pod documented
11738 6 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
51
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11739 6 ▶ my $name = $self->param('name') // '';
Condition50%1/2
$self->param("name") // ""
l!l
60
MC/DC0%0/1
$self->param("name") // ""
$self->param("name")
Truth table50%1/2
A
A= $self->param("name")
Aresult
11
00
11740 6 ▶ my $expr = $self->param('expr') // '';
Condition100%2/2
$self->param("expr") // ""
l!l
51
MC/DC100%1/1
$self->param("expr") // ""
$self->param("expr")
Truth table100%2/2
A
A= $self->param("expr")
Aresult
11
00
11741  
11742 6 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 5 6
11743 unless length $spec;
11744 5 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/u 2 3 5
11745 json => { error => $self->_i18n('error_calc_name', $name) },
11746 status => 400,
11747 ) unless $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
11748 3 ▶ return $self->render(json => { error => 'expr= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $expr 1 2 3
11749 unless length $expr;
11750  
11751 2 my $models = $self->_read_models;
11752 2 $models->{fields}{$spec}{$name} = $expr;
11753 2 $self->_write_models($models);
11754 2 $self->render(json => { ok => 1 });
11755 }
11756  
11757 2 ▶sub calc_delete ($self) {
Subroutine calc_delete: called CC=4 SCAR=13.9
Pod documented
11758 2 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
11
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11759 2 ▶ my $name = $self->param('name') // '';
Condition50%1/2
$self->param("name") // ""
l!l
20
MC/DC0%0/1
$self->param("name") // ""
$self->param("name")
Truth table50%1/2
A
A= $self->param("name")
Aresult
11
00
11760  
11761 2 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 1 2
11762 unless length $spec;
11763  
11764 1 my $models = $self->_read_models;
11765 1 delete $models->{fields}{$spec}{$name};
11766 1 $self->_write_models($models);
11767 1 $self->render(json => { ok => 1 });
11768 }
11769  
11770 5 ▶sub calc_list ($self) {
Subroutine calc_list: called CC=4 SCAR=13.9
Pod documented
11771 5 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
41
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11772 5 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 3 5
11773 unless length $spec;
11774  
11775 3 my $models = $self->_read_models;
11776 3 ▶ $self->render(json => { fields => $models->{fields}{$spec} // {} });
Condition100%2/2
$models->{"fields"}{$spec} // {}
l!l
21
MC/DC100%1/1
$models->{"fields"}{$spec} // {}
$models->{"fields"}{$spec}
Truth table100%2/2
A
A= $models->{"fields"}{$spec}
Aresult
11
00
11777 }
11778  
11779 # ---------------------------------------------------------------------------
11780 # Named-group CRUD (POST /groups/save, POST /groups/delete, GET /groups/list)
11781 # ---------------------------------------------------------------------------
11782  
11783 5 ▶sub groups_save ($self) {
Subroutine groups_save: called CC=8 SCAR=20.8
Pod documented
11784 5 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
41
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11785 5 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
41
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11786 5 ▶ my $group_name = $self->param('group') // '';
Condition100%2/2
$self->param("group") // ""
l!l
41
MC/DC100%1/1
$self->param("group") // ""
$self->param("group")
Truth table100%2/2
A
A= $self->param("group")
Aresult
11
00
11787  
11788 # The panel sends one values= holding a line for each value; a script may just
11789 # as well repeat the parameter.
11790 5 ▶ my @values = map { split /\r?\n/ } @{ $self->every_param('values') // [] };
Condition50%1/2
$self->every_param("values") // []
l!l
50
MC/DC0%0/1
$self->every_param("values") // []
$self->every_param("values")
Truth table50%1/2
A
A= $self->every_param("values")
Aresult
11
00
11791  
11792 5 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 4 5
11793 unless length $spec;
11794 4 ▶ return $self->render(json => { error => 'col= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 1 3 4
11795 unless length $col;
11796 3 ▶ return $self->render(json => { error => 'group= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $group_name 1 2 3
11797 unless length $group_name;
11798  
11799 2 my $models = $self->_read_models;
11800 2 $models->{groups}{$spec}{$col}{$group_name} = \@values;
11801 2 $self->_write_models($models);
11802 2 $self->render(json => { ok => 1 });
11803 }
11804  
11805 2 ▶sub groups_delete ($self) {
Subroutine groups_delete: called CC=8 SCAR=21.3
Pod documented
11806 2 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition50%1/2
$self->param("l") // ""
l!l
20
MC/DC0%0/1
$self->param("l") // ""
$self->param("l")
Truth table50%1/2
A
A= $self->param("l")
Aresult
11
00
11807 2 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
11
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11808 2 ▶ my $group_name = $self->param('group') // '';
Condition100%2/2
$self->param("group") // ""
l!l
11
MC/DC100%1/1
$self->param("group") // ""
$self->param("group")
Truth table100%2/2
A
A= $self->param("group")
Aresult
11
00
11809  
11810 2 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 0 2 2
11811 unless length $spec;
11812  
11813 2 my $models = $self->_read_models;
11814 2 ▶ if (length $col && length $group_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $col and length $group_name) { } 1 1 2
elsif (length $col) { } 0 1 1
Condition66%2/3
length $col and length $group_name
!ll&&!rl&&r
101
MC/DC50%1/2
length $col and length $group_name
length $col length $group_name
Truth table66%2/3
A and B
A= length $colB= length $group_name
ABresult
0X0
100
111
11815 1 delete $models->{groups}{$spec}{$col}{$group_name};
11816 } elsif (length $col) {
11817 0 delete $models->{groups}{$spec}{$col};
11818 } else {
11819 1 delete $models->{groups}{$spec};
11820 }
11821 2 $self->_write_models($models);
11822 2 $self->render(json => { ok => 1 });
11823 }
11824  
11825 6 ▶sub groups_list ($self) {
Subroutine groups_list: called CC=4 SCAR=13.9
Pod documented
11826 6 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
51
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11827 6 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 4 6
11828 unless length $spec;
11829  
11830 4 my $models = $self->_read_models;
11831 4 ▶ $self->render(json => { groups => $models->{groups}{$spec} // {} });
Condition100%2/2
$models->{"groups"}{$spec} // {}
l!l
22
MC/DC100%1/1
$models->{"groups"}{$spec} // {}
$models->{"groups"}{$spec}
Truth table100%2/2
A
A= $models->{"groups"}{$spec}
Aresult
11
00
11832 }
11833  
11834 # ---------------------------------------------------------------------------
11835 # Date-pattern CRUD (POST /dates/save, POST /dates/delete, GET /dates/list)
11836 # ---------------------------------------------------------------------------
11837  
11838 # dates_save: POST /dates/save?l=<spec>&col=<col>&pattern=<fmt>[&time=<fmt>][&as=timestamp]
11839 # Stores a strptime-style date normalisation pattern for $col in models.json.
11840 # Only the three patterns in %ALLOWED_DATE_PATTERNS are accepted, and only the
11841 # two in %ALLOWED_TIME_PATTERNS beside one of them.
11842 #
11843 # A pattern on its own is stored as the string it has always been, so an
11844 # existing entry keeps its meaning. A time, or as=timestamp, stores the hash
11845 # shape { date =>, time =>, as => } that Calc::date_normalise reads, and which
11846 # normalises the column to 'YYYY-MM-DDTHH:MM:SS' -- the shape the date
11847 # comparisons already read to the second, so a last_24_hours window filters on
11848 # a column that has a pattern of this kind.
11849 9 ▶sub dates_save ($self) {
Subroutine dates_save: called CC=17 SCAR=32.8
Pod documented
11850 9 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
81
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11851 9 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
81
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11852 9 ▶ my $pattern = $self->param('pattern') // '';
Condition50%1/2
$self->param("pattern") // ""
l!l
90
MC/DC0%0/1
$self->param("pattern") // ""
$self->param("pattern")
Truth table50%1/2
A
A= $self->param("pattern")
Aresult
11
00
11853 9 ▶ my $time = $self->param('time') // '';
Condition50%1/2
$self->param("time") // ""
l!l
09
MC/DC0%0/1
$self->param("time") // ""
$self->param("time")
Truth table50%1/2
A
A= $self->param("time")
Aresult
11
00
11854 9 ▶ my $as = $self->param('as') // '';
Condition50%1/2
$self->param("as") // ""
l!l
09
MC/DC0%0/1
$self->param("as") // ""
$self->param("as")
Truth table50%1/2
A
A= $self->param("as")
Aresult
11
00
11855  
11856 9 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 8 9
11857 unless length $spec;
11858 8 ▶ return $self->render(json => { error => 'col= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 1 7 8
11859 unless length $col;
11860 return $self->render(
11861 json => { error => $self->_i18n('error_date_pattern', $pattern) },
11862 status => 400,
11863 7 ▶ ) unless $ALLOWED_DATE_PATTERNS{$pattern};
Branch: 1 branch
BranchTrueFalseTotal
unless $ALLOWED_DATE_PATTERNS{$pattern} 2 5 7
11864 return $self->render(
11865 json => { error => $self->_i18n('error_date_time_pattern', $time) },
11866 status => 400,
11867 5 ▶ ) if length($time) && !$ALLOWED_TIME_PATTERNS{$time};
Branch: 1 branch
BranchTrueFalseTotal
if length $time and not $ALLOWED_TIME_PATTERNS{$time} 0 5 5
Condition33%1/3
length $time and not $ALLOWED_TIME_PATTERNS{$time}
!ll&&!rl&&r
500
MC/DC0%0/2
length $time and not $ALLOWED_TIME_PATTERNS{$time}
length $time not $ALLOWED_TIME_PATTERNS{$time}
Truth table33%1/3
A and not(B)
A= length $timeB= not $ALLOWED_TIME_PATTERNS{$time}
ABresult
0X0
100
111
11868 5 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
if length $as and $as ne "timestamp" 0 5 5
Condition33%1/3
length $as and $as ne "timestamp"
!ll&&!rl&&r
500
MC/DC0%0/2
length $as and $as ne "timestamp"
length $as $as ne "timestamp"
Truth table33%1/3
A and B
A= length $asB= $as ne "timestamp"
ABresult
0X0
100
111
11869 json => { error => $self->_i18n('error_date_as', $as) },
11870 status => 400,
11871 ) if length($as) && $as ne 'timestamp';
11872  
11873 5 ▶ my $stored = (length($time) || length($as))
Branch: 3 branches
BranchTrueFalseTotal
length $time || length $as ? : 0 5 5
length $time ? : 0 0 0
length $as ? : 0 0 0
Condition33%1/3
length $time or length $as
l!l&&r!l&&!r
005
MC/DC0%0/2
length $time or length $as
length $time length $as
Truth table33%1/3
A or B
A= length $timeB= length $as
ABresult
1X1
011
000
11874 ? { date => $pattern, (length($time) ? (time => $time) : ()), (length($as) ? (as => $as) : ()) }
11875 : $pattern;
11876  
11877 5 my $models = $self->_read_models;
11878 5 $models->{date_patterns}{$spec}{$col} = $stored;
11879 5 $self->_write_models($models);
11880 5 $self->render(json => { ok => 1 });
11881 }
11882  
11883 # dates_delete: POST /dates/delete?l=<spec>&col=<col>
11884 # Removes the pattern for $col (or all patterns for $spec when col is absent).
11885 4 ▶sub dates_delete ($self) {
Subroutine dates_delete: called CC=5 SCAR=16.2
Pod documented
11886 4 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition50%1/2
$self->param("l") // ""
l!l
40
MC/DC0%0/1
$self->param("l") // ""
$self->param("l")
Truth table50%1/2
A
A= $self->param("l")
Aresult
11
00
11887 4 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
31
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11888  
11889 4 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 0 4 4
11890 unless length $spec;
11891  
11892 4 my $models = $self->_read_models;
11893 4 ▶ if (length $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $col) { } 3 1 4
11894 3 delete $models->{date_patterns}{$spec}{$col};
11895 } else {
11896 1 delete $models->{date_patterns}{$spec};
11897 }
11898 4 $self->_write_models($models);
11899 4 $self->render(json => { ok => 1 });
11900 }
11901  
11902 # dates_list: GET /dates/list?l=<spec>
11903 # Returns { patterns: { col: pattern, ... } } for $spec, where a pattern is the
11904 # date-format string it has always been or, for a column that also holds a time,
11905 # { date =>, time =>, as => }.
11906 7 ▶sub dates_list ($self) {
Subroutine dates_list: called CC=4 SCAR=13.9
Pod documented
11907 7 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
61
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11908 7 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 5 7
11909 unless length $spec;
11910  
11911 5 my $models = $self->_read_models;
11912 5 ▶ $self->render(json => { patterns => $models->{date_patterns}{$spec} // {} });
Condition100%2/2
$models->{"date_patterns"}{$spec} // {}
l!l
32
MC/DC100%1/1
$models->{"date_patterns"}{$spec} // {}
$models->{"date_patterns"}{$spec}
Truth table100%2/2
A
A= $models->{"date_patterns"}{$spec}
Aresult
11
00
11913 }
11914  
11915 # ---------------------------------------------------------------------------
11916 # Stored-lookup CRUD (POST /lookups/save, POST /lookups/delete, GET /lookups/list)
11917 # ---------------------------------------------------------------------------
11918  
11919 # lookups_save: POST /lookups/save?l=<spec>&name=<ident>&file=<path>&key=<col>
11920 # [&col=<col>][&fields=<col>[,<col>...]][&default=<value>]
11921 # Stores models.json lookups.<spec>.<name>. The file is read now, so a file
11922 # outside data_dir, a missing one, one too large, and a key or field it does not
11923 # have are all 400s here rather than on the next page. fields= may repeat or
11924 # hold a comma-separated list; without it every column but the key is added.
11925 6 ▶sub lookups_save ($self) {
Subroutine lookups_save: called CC=14 SCAR=26.4
Pod documented
11926 6 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
51
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11927 6 ▶ my $name = $self->param('name') // '';
Condition100%2/2
$self->param("name") // ""
l!l
51
MC/DC100%1/1
$self->param("name") // ""
$self->param("name")
Truth table100%2/2
A
A= $self->param("name")
Aresult
11
00
11928 6 ▶ my $key = $self->param('key') // '';
Condition100%2/2
$self->param("key") // ""
l!l
42
MC/DC100%1/1
$self->param("key") // ""
$self->param("key")
Truth table100%2/2
A
A= $self->param("key")
Aresult
11
00
11929 6 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
15
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11930 6 ▶ my @fields = grep { length } map { split /,/ } @{ $self->every_param('fields') // [] };
Condition50%1/2
$self->every_param("fields") // []
l!l
60
MC/DC0%0/1
$self->every_param("fields") // []
$self->every_param("fields")
Truth table50%1/2
A
A= $self->every_param("fields")
Aresult
11
00
11931  
11932 6 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 5 6
11933 unless length $spec;
11934 5 ▶ return $self->render(json => { error => $self->_i18n('error_lookup_name', $name) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$LOOKUP_NAME_RE/u 1 4 5
11935 unless $name =~ $LOOKUP_NAME_RE;
11936 4 ▶ return $self->render(json => { error => 'key= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 1 3 4
11937 unless length $key;
11938  
11939 3 ▶ my %def = (
Branch: 3 branches
BranchTrueFalseTotal
length $col ? : 1 2 3
@fields ? : 1 2 3
defined $self->param('default') ? : 0 3 3
Condition50%1/2
$self->param("file") // ""
l!l
30
MC/DC0%0/1
$self->param("file") // ""
$self->param("file")
Truth table50%1/2
A
A= $self->param("file")
Aresult
11
00
11940 file => $self->param('file') // '',
11941 key => $key,
11942 (length $col ? (col => $col) : ()),
11943 (@fields ? (fields => \@fields) : ()),
11944 (defined $self->param('default') ? (default => $self->param('default')) : ()),
11945 );
11946 3 my $ok = eval { $self->_load_lookup({ %def, name => $name }); 1 };
11947 3 ▶ return $self->render(json => { error => _tidy_error($@) }, status => 400) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 1 3
11948  
11949 1 my $models = $self->_read_models;
11950 1 $models->{lookups}{$spec}{$name} = \%def;
11951 1 $self->_write_models($models);
11952 1 $self->render(json => { ok => 1 });
11953 }
11954  
11955 # lookups_delete: POST /lookups/delete?l=<spec>[&name=<ident>]
11956 # Removes one lookup, or every lookup of $spec when name= is absent.
11957 3 ▶sub lookups_delete ($self) {
Subroutine lookups_delete: called CC=5 SCAR=16.1
Pod documented
11958 3 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
21
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11959 3 ▶ my $name = $self->param('name') // '';
Condition100%2/2
$self->param("name") // ""
l!l
12
MC/DC100%1/1
$self->param("name") // ""
$self->param("name")
Truth table100%2/2
A
A= $self->param("name")
Aresult
11
00
11960  
11961 3 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 2 3
11962 unless length $spec;
11963  
11964 2 my $models = $self->_read_models;
11965 2 ▶ if (length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) { } 1 1 2
11966 1 delete $models->{lookups}{$spec}{$name};
11967 } else {
11968 1 delete $models->{lookups}{$spec};
11969 }
11970 2 $self->_write_models($models);
11971 2 $self->render(json => { ok => 1 });
11972 }
11973  
11974 # lookups_list: GET /lookups/list?l=<spec>
11975 # Returns { lookups: { name: { file, key, col?, fields?, default? }, ... } }.
11976 7 ▶sub lookups_list ($self) {
Subroutine lookups_list: called CC=4 SCAR=13.9
Pod documented
11977 7 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
61
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11978 7 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 5 7
11979 unless length $spec;
11980  
11981 5 my $models = $self->_read_models;
11982 5 ▶ $self->render(json => { lookups => $models->{lookups}{$spec} // {} });
Condition100%2/2
$models->{"lookups"}{$spec} // {}
l!l
23
MC/DC100%1/1
$models->{"lookups"}{$spec} // {}
$models->{"lookups"}{$spec}
Truth table100%2/2
A
A= $models->{"lookups"}{$spec}
Aresult
11
00
11983 }
11984  
11985 # ---------------------------------------------------------------------------
11986 # Stored-extraction CRUD (POST /extract/save, POST /extract/delete, GET /extract/list)
11987 # ---------------------------------------------------------------------------
11988  
11989 # extract_save: POST /extract/save?l=<spec>&name=<ident>&col=<col>&pattern=<re>
11990 # [&fields=<group>[,<group>...]]
11991 # Stores models.json extractions.<spec>.<name> = { col, pattern, fields }. The
11992 # pattern is compiled now, so one that is too long, uses a refused construct,
11993 # does not compile or names no group is a 400 here. fields= picks some of the
11994 # named groups; without it every named group becomes a column.
11995 7 ▶sub extract_save ($self) {
Subroutine extract_save: called CC=12 SCAR=24.9
Pod documented
11996 7 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
61
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
11997 7 ▶ my $name = $self->param('name') // '';
Condition100%2/2
$self->param("name") // ""
l!l
61
MC/DC100%1/1
$self->param("name") // ""
$self->param("name")
Truth table100%2/2
A
A= $self->param("name")
Aresult
11
00
11998 7 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
52
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
11999 7 ▶ my $pattern = $self->param('pattern') // '';
Condition100%2/2
$self->param("pattern") // ""
l!l
52
MC/DC100%1/1
$self->param("pattern") // ""
$self->param("pattern")
Truth table100%2/2
A
A= $self->param("pattern")
Aresult
11
00
12000 0 ▶ my @fields = grep { length } map { split /,/ } @{ $self->every_param('fields') // [] };
Condition50%1/2
$self->every_param("fields") // []
l!l
70
MC/DC0%0/1
$self->every_param("fields") // []
$self->every_param("fields")
Truth table50%1/2
A
A= $self->every_param("fields")
Aresult
11
00
12001  
12002 7 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 6 7
12003 unless length $spec;
12004 6 ▶ return $self->render(json => { error => $self->_i18n('error_extract_name', $name) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$LOOKUP_NAME_RE/u 1 5 6
12005 unless $name =~ $LOOKUP_NAME_RE;
12006 5 ▶ return $self->render(json => { error => 'col= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 1 4 5
12007 unless length $col;
12008 4 ▶ return $self->render(json => { error => 'pattern= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $pattern 1 3 4
12009 unless length $pattern;
12010  
12011 3 ▶ my %def = (col => $col, pattern => $pattern, (@fields ? (fields => \@fields) : ()));
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 0 3 3
12012 3 my $fields;
12013 3 my $ok = eval { (undef, $fields) = $self->_compile_extraction($name, \%def); 1 };
12014 3 ▶ return $self->render(json => { error => _tidy_error($@) }, status => 400) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 1 3
12015 1 $def{fields} = $fields;
12016  
12017 1 my $models = $self->_read_models;
12018 1 $models->{extractions}{$spec}{$name} = \%def;
12019 1 $self->_write_models($models);
12020 1 $self->render(json => { ok => 1, fields => $fields });
12021 }
12022  
12023 # extract_delete: POST /extract/delete?l=<spec>[&name=<ident>]
12024 # Removes one extraction, or every extraction of $spec when name= is absent.
12025 2 ▶sub extract_delete ($self) {
Subroutine extract_delete: called CC=5 SCAR=16.2
Pod documented
12026 2 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
11
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
12027 2 ▶ my $name = $self->param('name') // '';
Condition100%2/2
$self->param("name") // ""
l!l
11
MC/DC100%1/1
$self->param("name") // ""
$self->param("name")
Truth table100%2/2
A
A= $self->param("name")
Aresult
11
00
12028  
12029 2 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 1 2
12030 unless length $spec;
12031  
12032 1 my $models = $self->_read_models;
12033 1 ▶ if (length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) { } 1 0 1
12034 1 delete $models->{extractions}{$spec}{$name};
12035 } else {
12036 0 delete $models->{extractions}{$spec};
12037 }
12038 1 $self->_write_models($models);
12039 1 $self->render(json => { ok => 1 });
12040 }
12041  
12042 # extract_list: GET /extract/list?l=<spec>
12043 # Returns { extractions: { name: { col, pattern, fields }, ... } }.
12044 5 ▶sub extract_list ($self) {
Subroutine extract_list: called CC=4 SCAR=13.9
Pod documented
12045 5 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
41
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
12046 5 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 3 5
12047 unless length $spec;
12048  
12049 3 my $models = $self->_read_models;
12050 3 ▶ $self->render(json => { extractions => $models->{extractions}{$spec} // {} });
Condition100%2/2
$models->{"extractions"}{$spec} // {}
l!l
12
MC/DC100%1/1
$models->{"extractions"}{$spec} // {}
$models->{"extractions"}{$spec}
Truth table100%2/2
A
A= $models->{"extractions"}{$spec}
Aresult
11
00
12051 }
12052  
12053 # ---------------------------------------------------------------------------
12054 # Geo column preferences CRUD (POST /geo/save, POST /geo/delete, GET /geo/list)
12055 #
12056 # Stores the user's choice of lat/lon/loc columns for a given l= spec in
12057 # models.json under geo.<spec>.columns. The geocode cache itself lives under
12058 # geo.<spec>.places and is written by _map_data independently.
12059 # ---------------------------------------------------------------------------
12060  
12061 # geo_save: POST /geo/save?l=<spec>&col=<col>&kind=lat|lon|location|location_suffix[&replace=1][&loc_text=<text>]
12062 # location_suffix is the column whose cells follow the place's, after ", "
12063 # (a country after an address). loc_text= is the text the map adds to the end
12064 # of each place ("Virginia, USA"); it is not a column, so it is kept beside
12065 # them, as geo.<spec>.loc_text, and replace=1 forgets it too.
12066 # col= and kind= may repeat, in pairs, so a latitude and a longitude are saved
12067 # in one request. replace=1 forgets the spec's saved columns first: the map
12068 # panel's "Remember" sends it, so a new choice replaces the old one rather than
12069 # leaving, say, a place-name column beside the latitude chosen after it.
12070 5 ▶sub geo_save ($self) {
Subroutine geo_save: called CC=14 SCAR=27.3
Pod documented
12071 5 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
41
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
12072 5 ▶ my @cols = @{ $self->every_param('col') // [] };
Condition50%1/2
$self->every_param("col") // []
l!l
50
MC/DC0%0/1
$self->every_param("col") // []
$self->every_param("col")
Truth table50%1/2
A
A= $self->every_param("col")
Aresult
11
00
12073 5 ▶ my @kinds = @{ $self->every_param('kind') // [] };
Condition50%1/2
$self->every_param("kind") // []
l!l
50
MC/DC0%0/1
$self->every_param("kind") // []
$self->every_param("kind")
Truth table50%1/2
A
A= $self->every_param("kind")
Aresult
11
00
12074  
12075 5 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 4 5
12076 unless length $spec;
12077 return $self->render(json => { error => 'col= required' }, status => 400)
12078 4 ▶ unless @cols && !grep { !length } @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols and not grep({not length $_;} @cols) 1 3 4
Condition66%2/3
@cols and not grep({not length $_;} @cols)
!ll&&!rl&&r
103
MC/DC50%1/2
@cols and not grep({not length $_;} @cols)
@cols not grep({not length $_;} @cols)
Truth table66%2/3
A and not(B)
A= @colsB= not grep({not length $_;} @cols)
ABresult
0X0
100
111
12079 return $self->render(json => { error => 'kind must be lat, lon, location or location_suffix' }, status => 400)
12080 3 ▶ unless @kinds == @cols && !grep { !/\A(?:lat|lon|location|location_suffix)\z/ } @kinds;
Branch: 1 branch
BranchTrueFalseTotal
unless @kinds == @cols and not grep({not /\A(?:lat|lon|location|location_suffix)\z/u;} @kinds) 2 1 3
Condition100%3/3
@kinds == @cols and not grep({not /\A(?:lat|lon|location|location_suffix)\z/u;} @kinds)
!ll&&!rl&&r
111
MC/DC100%2/2
@kinds == @cols and not grep({not /\A(?:lat|lon|location|location_suffix)\z/u;} @kinds)
@kinds == @cols not grep({not /\A(?:lat|lon|location|location_suffix)\z/u;} @kinds)
Truth table100%3/3
A and not(B)
A= @kinds == @colsB= not grep({not /\A(?:lat|lon|location|location_suffix)\z/u;} @kinds)
ABresult
0X0
100
111
12081 1 ▶ my $loc_text_raw = $self->param('loc_text') // '';
Condition50%1/2
$self->param("loc_text") // ""
l!l
01
MC/DC0%0/1
$self->param("loc_text") // ""
$self->param("loc_text")
Truth table50%1/2
A
A= $self->param("loc_text")
Aresult
11
00
12082 1 my ($loc_text) = _map_loc_text($loc_text_raw);
12083 1 ▶ return $self->render(json => { error => $self->_i18n('error_map_loc_text', $Database::BI::Model::Geocode::PLACE_TEXT_MAX, $loc_text_raw) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loc_text 0 1 1
12084 unless defined $loc_text;
12085  
12086 1 my $models = $self->_read_models;
12087 1 ▶ if ($self->param('replace')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("replace")) 0 1 1
12088 0 delete $models->{geo}{$spec}{columns};
12089 0 delete $models->{geo}{$spec}{loc_text};
12090 }
12091 1 $models->{geo}{$spec}{columns}{ $cols[$_] } = { kind => $kinds[$_] } for 0 .. $#cols;
12092 1 ▶ $models->{geo}{$spec}{loc_text} = $loc_text if length $loc_text;
Branch: 1 branch
BranchTrueFalseTotal
if length $loc_text 0 1 1
12093 1 $self->_write_models($models);
12094 1 $self->render(json => { ok => 1 });
12095 }
12096  
12097 # geo_delete: POST /geo/delete?l=<spec>&col=<col>
12098 # Omit col= to clear all column preferences for the spec, and its loc_text.
12099 3 ▶sub geo_delete ($self) {
Subroutine geo_delete: called CC=5 SCAR=16.3
Pod documented
12100 3 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
21
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
12101 3 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
21
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
12102  
12103 3 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 1 2 3
12104 unless length $spec;
12105  
12106 2 my $models = $self->_read_models;
12107 2 ▶ if (length $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $col) { } 2 0 2
12108 2 delete $models->{geo}{$spec}{columns}{$col};
12109 } else {
12110 0 delete $models->{geo}{$spec}{columns};
12111 0 delete $models->{geo}{$spec}{loc_text};
12112 }
12113 2 $self->_write_models($models);
12114 2 $self->render(json => { ok => 1 });
12115 }
12116  
12117 # geo_list: GET /geo/list?l=<spec>
12118 # Returns { columns: { col: { kind: "lat"|"lon"|"location"|"location_suffix" }, ... } },
12119 # with loc_text: "<text>" when that is remembered too.
12120 5 ▶sub geo_list ($self) {
Subroutine geo_list: called CC=7 SCAR=20.7
Pod documented
12121 5 ▶ my $spec = canonical_spec($self->param('l') // '');
Condition100%2/2
$self->param("l") // ""
l!l
41
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
12122 5 ▶ return $self->render(json => { error => 'l= required' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 2 3 5
12123 unless length $spec;
12124  
12125 3 my $models = $self->_read_models;
12126 3 my $loc_text = $models->{geo}{$spec}{loc_text};
12127 $self->render(json => {
12128 columns => $models->{geo}{$spec}{columns} // {},
12129 3 ▶ (defined($loc_text) && !ref($loc_text) && length($loc_text) ? (loc_text => $loc_text) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $loc_text && !ref($loc_text) && length $loc_text ? : 0 3 3
Condition50%4/8
$models->{"geo"}{$spec}{"columns"} // {}
l!l
21
defined $loc_text and not ref $loc_text and length $loc_text
!ll&&!rl&&r
300
defined $loc_text and not ref $loc_text
!ll&&!rl&&r
300
MC/DC100%1/1
$models->{"geo"}{$spec}{"columns"} // {}
$models->{"geo"}{$spec}{"columns"}
MC/DC0%0/3
defined $loc_text and not ref $loc_text and length $loc_text
defined $loc_text not ref $loc_text length $loc_text
Truth table100%2/2
A
A= $models->{"geo"}{$spec}{"columns"}
Aresult
11
00
Truth table25%1/4
A and not(B) and C
A= defined $loc_textB= not ref $loc_textC= length $loc_text
ABCresult
0XX0
10X0
1100
1111
12130 });
12131 }
12132  
12133 # ---------------------------------------------------------------------------
12134 # Scatter chart (GET /scatter)
12135 # ---------------------------------------------------------------------------
12136  
12137 48 ▶sub scatter_view ($self) {
Subroutine scatter_view: called CC=45 SCAR=42.4
Pod documented
12138 48 ▶ my $x_col = $self->param('x') // '';
Condition100%2/2
$self->param("x") // ""
l!l
462
MC/DC100%1/1
$self->param("x") // ""
$self->param("x")
Truth table100%2/2
A
A= $self->param("x")
Aresult
11
00
12139 48 ▶ my $y_col = $self->param('y') // '';
Condition100%2/2
$self->param("y") // ""
l!l
462
MC/DC100%1/1
$self->param("y") // ""
$self->param("y")
Truth table100%2/2
A
A= $self->param("y")
Aresult
11
00
12140 48 ▶ my $series_col = $self->param('series') // '';
Condition100%2/2
$self->param("series") // ""
l!l
147
MC/DC100%1/1
$self->param("series") // ""
$self->param("series")
Truth table100%2/2
A
A= $self->param("series")
Aresult
11
00
12141 48 ▶ my $size_col = $self->param('size') // '';
Condition100%2/2
$self->param("size") // ""
l!l
246
MC/DC100%1/1
$self->param("size") // ""
$self->param("size")
Truth table100%2/2
A
A= $self->param("size")
Aresult
11
00
12142 48 ▶ my $scheme = $self->param('scheme') // 'tableau10';
Condition100%2/2
$self->param("scheme") // "tableau10"
l!l
345
MC/DC100%1/1
$self->param("scheme") // "tableau10"
$self->param("scheme")
Truth table100%2/2
A
A= $self->param("scheme")
Aresult
11
00
12143 48 my ($min_r, $max_r) = _scatter_radii($self->param('min_r'), $self->param('max_r'));
12144 48 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
048
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
12145 48 my $back2 = _safe_back_url($self->param('back2'));
12146 48 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
048
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12147  
12148 48 ▶ return $self->render(text => 'Missing x= parameter', status => 400) unless length $x_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col 3 45 48
12149 45 ▶ return $self->render(text => 'Missing y= parameter', status => 400) unless length $y_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $y_col 1 44 45
12150  
12151 44 my ($src, $records, $columns) = $self->_run_pipeline_source;
12152 44 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 43 44
12153  
12154 43 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
12155 43 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 42 43
12156  
12157 42 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 41 42
12158  
12159 41 my %col_set = map { $_ => 1 } @{$columns};
12160 41 ▶ return $self->render(text => "Column not found: $x_col", status => 400) unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 2 39 41
12161 39 ▶ return $self->render(text => "Column not found: $y_col", status => 400) unless $col_set{$y_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$y_col} 1 38 39
12162 return $self->render(text => "Column not found: $series_col", status => 400)
12163 38 ▶ if length $series_col && !$col_set{$series_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col and not $col_set{$series_col} 0 38 38
Condition66%2/3
length $series_col and not $col_set{$series_col}
!ll&&!rl&&r
3710
MC/DC0%0/2
length $series_col and not $col_set{$series_col}
length $series_col not $col_set{$series_col}
Truth table66%2/3
A and not(B)
A= length $series_colB= not $col_set{$series_col}
ABresult
0X0
100
111
12164 return $self->render(text => "Column not found: $size_col", status => 400)
12165 38 ▶ if length $size_col && !$col_set{$size_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $size_col and not $col_set{$size_col} 1 37 38
Condition66%2/3
length $size_col and not $col_set{$size_col}
!ll&&!rl&&r
3701
MC/DC50%1/2
length $size_col and not $col_set{$size_col}
length $size_col not $col_set{$size_col}
Truth table66%2/3
A and not(B)
A= length $size_colB= not $col_set{$size_col}
ABresult
0X0
100
111
12166  
12167 37 ▶ if (length $series_col && !$SCATTER_SCHEMES{$scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col and not $SCATTER_SCHEMES{$scheme}) 1 36 37
Condition66%2/3
length $series_col and not $SCATTER_SCHEMES{$scheme}
!ll&&!rl&&r
3601
MC/DC50%1/2
length $series_col and not $SCATTER_SCHEMES{$scheme}
length $series_col not $SCATTER_SCHEMES{$scheme}
Truth table66%2/3
A and not(B)
A= length $series_colB= not $SCATTER_SCHEMES{$scheme}
ABresult
0X0
100
111
12168 1 return $self->render(text => "Unknown colour scheme: $scheme", status => 400);
12169 }
12170  
12171 36 my @points;
12172 36 for my $row (@$records) {
12173 216 ▶ my $xv = _numeric_cell($row->{$x_col} // '');
Condition50%1/2
$row->{$x_col} // ""
l!l
2160
MC/DC0%0/1
$row->{$x_col} // ""
$row->{$x_col}
Truth table50%1/2
A
A= $row->{$x_col}
Aresult
11
00
12174 216 ▶ my $yv = _numeric_cell($row->{$y_col} // '');
Condition50%1/2
$row->{$y_col} // ""
l!l
2160
MC/DC0%0/1
$row->{$y_col} // ""
$row->{$y_col}
Truth table50%1/2
A
A= $row->{$y_col}
Aresult
11
00
12175 216 ▶ next unless defined $xv && defined $yv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xv and defined $yv 210 6 216
Condition66%2/3
defined $xv and defined $yv
!ll&&!rl&&r
21006
MC/DC50%1/2
defined $xv and defined $yv
defined $xv defined $yv
Truth table66%2/3
A and B
A= defined $xvB= defined $yv
ABresult
0X0
100
111
12176 30 ▶ my %extra = map { $_ => _decode_cell($row->{$_} // '') }
Condition50%1/2
$row->{$_} // ""
l!l
300
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
12177 6 ▶ grep { $_ ne $x_col && $_ ne $y_col } @$columns;
Condition66%2/3
$_ ne $x_col && $_ ne $y_col
!ll&&!rl&&r
6030
MC/DC50%1/2
$_ ne $x_col && $_ ne $y_col
$_ ne $x_col $_ ne $y_col
Truth table66%2/3
A && B
A= $_ ne $x_colB= $_ ne $y_col
ABresult
0X0
100
111
12178 6 my %pt_meta;
12179 6 ▶ $pt_meta{series} = _decode_cell($row->{$series_col}) if length $series_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col 0 6 6
12180 6 ▶ if (length $size_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $size_col) 0 6 6
12181 0 ▶ my $sv = _numeric_cell($row->{$size_col} // '');
Condition0%0/2
$row->{$size_col} // ""
l!l
00
MC/DC0%0/1
$row->{$size_col} // ""
$row->{$size_col}
Truth table0%0/2
A
A= $row->{$size_col}
Aresult
11
00
12182 0 ▶ $pt_meta{size} = $sv if defined $sv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sv 0 0 0
12183 }
12184 6 ▶ push @points, [$xv, $yv, \%extra, (%pt_meta ? \%pt_meta : ())];
Branch: 1 branch
BranchTrueFalseTotal
%pt_meta ? : 0 6 6
12185 }
12186  
12187 36 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @points 35 1 36
12188 text => 'No plottable data: no rows have valid numeric values in both selected columns.',
12189 status => 200,
12190 ) unless @points;
12191  
12192 1 my ($source_url, $source_accessed) = $self->_url_attribution;
12193  
12194 1 require HTML::D3;
12195 1 my $title = "$y_col vs $x_col";
12196 1 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 800, height => 480)
Branch: 2 branches
BranchTrueFalseTotal
length $series_col ? : 0 1 1
length $size_col ? : 0 1 1
12197 ->render_scatter_chart_snippet(\@points, {
12198 animated => 1,
12199 x_label => $x_col,
12200 y_label => $y_col,
12201 (length $series_col ? (colour_by_series => 1, series_col => $series_col, scheme => $scheme) : ()),
12202 (length $size_col ? (size_col => $size_col, radius_range => [$min_r, $max_r]) : ()),
12203 });
12204  
12205 1 my $chart_html = $snippet->{html};
12206 1 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 1 1
12207  
12208 1 ▶ my %chart_spec = (
Branch: 4 branches
BranchTrueFalseTotal
length $series_col ? : 0 1 1
length $size_col ? : 0 1 1
length $series_col ? : 0 1 1
length $size_col ? : 0 1 1
12209 kind => 'scatter',
12210 title => $title,
12211 encode => { x => $x_col, y => $y_col,
12212 (length $series_col ? (series => $series_col) : ()),
12213 (length $size_col ? (size => $size_col) : ()),
12214 },
12215 # scheme= means nothing without series=, and is unchecked then, so it
12216 # is not saved: a canvas copy would otherwise carry ?scheme=Blues. The
12217 # radii likewise only with size=.
12218 options => {
12219 (length $series_col ? (scheme => $scheme) : ()),
12220 (length $size_col ? (min_r => $min_r, max_r => $max_r) : ()),
12221 },
12222 );
12223 1 my ($platform, $language) = $self->_resolve_template;
12224 1 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 1 1
$back2 ? : 0 1 1
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
010
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
12225 handler => 'tt',
12226 template => "$platform/$language/scatter",
12227 format => 'html',
12228 title => $title,
12229 chart_html => $chart_html,
12230 chart => \%chart_spec,
12231 chart_json => $self->_chart_json(\%chart_spec),
12232 point_count => scalar @points,
12233 x_col => $x_col,
12234 y_col => $y_col,
12235 back_url => ($back2 // $back),
12236 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
12237 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
12238 source_url => $source_url,
12239 source_accessed => $source_accessed,
12240 language => $language,
12241 );
12242 }
12243  
12244 # ---------------------------------------------------------------------------
12245 # Stacked bar chart (GET /stacked)
12246 # ---------------------------------------------------------------------------
12247  
12248 48 ▶sub stacked_view ($self) {
Subroutine stacked_view: called CC=36 SCAR=42.2
Pod documented
12249 48 ▶ my $cat_col = $self->param('cat') // '';
Condition100%2/2
$self->param("cat") // ""
l!l
462
MC/DC100%1/1
$self->param("cat") // ""
$self->param("cat")
Truth table100%2/2
A
A= $self->param("cat")
Aresult
11
00
12250 # ?series= is the canonical spec name; ?seg= is the legacy alias (keep both).
12251 48 ▶ my $seg_col = $self->param('series') // $self->param('seg') // '';
Condition100%5/5
($self->param("series") // $self->param("seg")) // ""
l!l
543
$self->param("series") // $self->param("seg")
l!l&&r!l&&!r
3243
MC/DC100%2/2
($self->param("series") // $self->param("seg")) // ""
$self->param("series") $self->param("seg")
Truth table100%3/3
A // B
A= $self->param("series")B= $self->param("seg")
ABresult
1X1
011
000
12252 48 ▶ my $val_col = $self->param('val') // '';
Condition100%2/2
$self->param("val") // ""
l!l
246
MC/DC100%1/1
$self->param("val") // ""
$self->param("val")
Truth table100%2/2
A
A= $self->param("val")
Aresult
11
00
12253 48 ▶ my $orient = $self->param('orient') // 'v';
Condition50%1/2
$self->param("orient") // "v"
l!l
048
MC/DC0%0/1
$self->param("orient") // "v"
$self->param("orient")
Truth table50%1/2
A
A= $self->param("orient")
Aresult
11
00
12254 48 my $scheme = _categorical_scheme($self->param('scheme'));
12255 48 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
048
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
12256 48 my $back2 = _safe_back_url($self->param('back2'));
12257 48 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
048
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12258  
12259 48 ▶ return $self->render(text => 'Missing cat= parameter', status => 400) unless length $cat_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col 3 45 48
12260 45 ▶ return $self->render(text => 'Missing series= parameter', status => 400) unless length $seg_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $seg_col 42 3 45
12261  
12262 3 my ($src, $records, $columns) = $self->_run_pipeline_source;
12263 3 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 0 3 3
12264  
12265 3 my $span = $self->_span_calc_spec($columns, 'cat');
12266 3 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 0 3 3
Condition33%1/3
$span and $span->{"error"}
!ll&&!rl&&r
300
MC/DC0%0/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table33%1/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
12267  
12268 3 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
12269 3 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 3 3
12270 0 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
12271  
12272 3 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 3 3
12273  
12274 3 my %col_set = map { $_ => 1 } @{$columns};
12275 3 ▶ return $self->render(text => "Column not found: $cat_col", status => 400) unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 1 2 3
12276 2 ▶ return $self->render(text => "Column not found: $seg_col", status => 400) unless $col_set{$seg_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$seg_col} 1 1 2
12277  
12278 1 ▶ my $count_mode = (!length $val_col || $val_col eq '__count__');
Condition33%1/3
!length($val_col) || $val_col eq '__count__'
l!l&&r!l&&!r
100
MC/DC0%0/2
!length($val_col) || $val_col eq '__count__'
!length($val_col) $val_col eq '__count__'
Truth table33%1/3
not(A) || B
A= !length($val_col)B= $val_col eq '__count__'
ABresult
1X1
011
000
12279 return $self->render(text => "Column not found: $val_col", status => 400)
12280 1 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 0 1 1
Condition33%1/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
100
MC/DC0%0/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table33%1/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
12281  
12282 1 ▶ $cat_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 1 1
12283  
12284 # Aggregate by [cat, seg] via _grouped_totals to respect the governing
12285 # invariant (all numbers from a single path) and to get ascending key order.
12286 1 ▶ my $agg_rows = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 1 0 1
$count_mode ? : 1 0 1
12287 { source => $src, records => $records },
12288 {
12289 by => [$cat_col, $seg_col],
12290 fn => $count_mode ? 'count' : 'sum',
12291 val => $count_mode ? undef : $val_col,
12292 as => 'value',
12293 },
12294 );
12295  
12296 # render_stacked_bar_snippet accepts flat [{cat_col=>,seg_col=>,val_col=>}] rows
12297 # and handles the stacking itself; only include pairs actually returned (no phantom
12298 # zero cells for missing intersections).
12299 my @data = map {
12300 1 ▶ my $cat = _decode_cell($_->{$cat_col} // '');
Condition50%1/2
$_->{$cat_col} // ""
l!l
60
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table50%1/2
A
A= $_->{$cat_col}
Aresult
11
00
12301 6 ▶ my $seg = _decode_cell($_->{$seg_col} // '');
Condition50%1/2
$_->{$seg_col} // ""
l!l
60
MC/DC0%0/1
$_->{$seg_col} // ""
$_->{$seg_col}
Truth table50%1/2
A
A= $_->{$seg_col}
Aresult
11
00
12302 (length $cat && length $seg)
12303 ? { cat => $cat, seg => $seg, val => $_->{value} }
12304 : ()
12305 6 ▶ } @$agg_rows;
Branch: 1 branch
BranchTrueFalseTotal
length $cat && length $seg ? : 6 0 6
Condition33%1/3
length $cat and length $seg
!ll&&!rl&&r
006
MC/DC0%0/2
length $cat and length $seg
length $cat length $seg
Truth table33%1/3
A and B
A= length $catB= length $seg
ABresult
0X0
100
111
12306  
12307 1 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @data 0 1 1
12308 text => 'No plottable data: no rows have valid data in the selected columns.',
12309 status => 200,
12310 ) unless @data;
12311  
12312 1 my ($source_url, $source_accessed) = $self->_url_attribution;
12313  
12314 1 require HTML::D3;
12315 1 ▶ my $title = $count_mode ? "Count by $cat_col and $seg_col"
Branch: 1 branch
BranchTrueFalseTotal
$count_mode ? : 1 0 1
12316 : "$val_col by $cat_col and $seg_col";
12317 1 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 900, height => 500)
Branch: 1 branch
BranchTrueFalseTotal
$orient eq 'h' ? : 0 1 1
12318 ->render_stacked_bar_snippet(\@data, {
12319 orient => ($orient eq 'h') ? 'h' : 'v',
12320 x_label => $cat_col,
12321 scheme => $scheme,
12322 });
12323  
12324 1 my $chart_html = $snippet->{html};
12325 1 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 1 1
12326  
12327 1 my %chart_spec = (
12328 kind => 'stacked',
12329 title => $title,
12330 encode => { cat => $cat_col, series => $seg_col, value => $val_col },
12331 options => { orientation => $orient, scheme => $scheme },
12332 );
12333 1 my ($platform, $language) = $self->_resolve_template;
12334 $self->render(
12335 handler => 'tt',
12336 template => "$platform/$language/stacked",
12337 format => 'html',
12338 title => $title,
12339 chart_html => $chart_html,
12340 chart => \%chart_spec,
12341 chart_json => $self->_chart_json(\%chart_spec),
12342 1 ▶ bar_count => scalar(grep { defined $_->{cat} } @data),
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 1 1
$back2 ? : 0 1 1
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
010
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
12343 back_url => ($back2 // $back),
12344 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
12345 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
12346 source_url => $source_url,
12347 source_accessed => $source_accessed,
12348 language => $language,
12349 );
12350 }
12351  
12352 # ---------------------------------------------------------------------------
12353 # Combo bar+line chart (GET /combo)
12354 # ---------------------------------------------------------------------------
12355  
12356 48 ▶sub combo_view ($self) {
Subroutine combo_view: called CC=42 SCAR=43.4
Pod documented
12357 # ?cat= is the canonical spec name; ?x= is the legacy alias (keep both).
12358 48 ▶ my $x_col = $self->param('cat') // $self->param('x') // '';
Condition80%4/5
($self->param("cat") // $self->param("x")) // ""
l!l
462
$self->param("cat") // $self->param("x")
l!l&&r!l&&!r
4602
MC/DC50%1/2
($self->param("cat") // $self->param("x")) // ""
$self->param("cat") $self->param("x")
Truth table66%2/3
A // B
A= $self->param("cat")B= $self->param("x")
ABresult
1X1
011
000
12359 # ?bar_val= is the canonical spec name; ?bar= is the legacy alias (keep both).
12360 48 ▶ my $bar_col = $self->param('bar_val') // $self->param('bar') // '';
Condition100%5/5
($self->param("bar_val") // $self->param("bar")) // ""
l!l
543
$self->param("bar_val") // $self->param("bar")
l!l&&r!l&&!r
1443
MC/DC100%2/2
($self->param("bar_val") // $self->param("bar")) // ""
$self->param("bar_val") $self->param("bar")
Truth table100%3/3
A // B
A= $self->param("bar_val")B= $self->param("bar")
ABresult
1X1
011
000
12361 # ?line_val= is the canonical spec name; ?line= is the legacy alias (keep both).
12362 48 ▶ my $line_col = $self->param('line_val') // $self->param('line') // '';
Condition100%5/5
($self->param("line_val") // $self->param("line")) // ""
l!l
543
$self->param("line_val") // $self->param("line")
l!l&&r!l&&!r
1443
MC/DC100%2/2
($self->param("line_val") // $self->param("line")) // ""
$self->param("line_val") $self->param("line")
Truth table100%3/3
A // B
A= $self->param("line_val")B= $self->param("line")
ABresult
1X1
011
000
12363 48 ▶ my $bar_label = $self->param('bar_label') // '';
Condition50%1/2
$self->param("bar_label") // ""
l!l
048
MC/DC0%0/1
$self->param("bar_label") // ""
$self->param("bar_label")
Truth table50%1/2
A
A= $self->param("bar_label")
Aresult
11
00
12364 48 ▶ my $line_label = $self->param('line_label') // '';
Condition50%1/2
$self->param("line_label") // ""
l!l
048
MC/DC0%0/1
$self->param("line_label") // ""
$self->param("line_label")
Truth table50%1/2
A
A= $self->param("line_label")
Aresult
11
00
12365 48 my $scheme = _optional_scheme($self->param('scheme'));
12366 48 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
048
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
12367 48 my $back2 = _safe_back_url($self->param('back2'));
12368 48 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
048
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12369  
12370 48 ▶ return $self->render(text => 'Missing cat= parameter', status => 400) unless length $x_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $x_col 3 45 48
12371 45 ▶ return $self->render(text => 'Missing bar_val= parameter', status => 400) unless length $bar_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bar_col 42 3 45
12372 3 ▶ return $self->render(text => 'Missing line_val= parameter', status => 400) unless length $line_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line_col 2 1 3
12373  
12374 1 my ($src, $records, $columns) = $self->_run_pipeline_source;
12375 1 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 0 1 1
12376  
12377 1 my $span = $self->_span_calc_spec($columns, 'cat', 'x');
12378 1 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 0 1 1
Condition33%1/3
$span and $span->{"error"}
!ll&&!rl&&r
100
MC/DC0%0/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table33%1/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
12379  
12380 1 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
12381 1 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 1 1
12382 0 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
12383  
12384 1 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 1 1
12385  
12386 1 my %col_set = map { $_ => 1 } @{$columns};
12387 1 ▶ return $self->render(text => "Column not found: $x_col", status => 400) unless $col_set{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$x_col} 0 1 1
12388 1 ▶ return $self->render(text => "Column not found: $bar_col", status => 400) unless $col_set{$bar_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$bar_col} 0 1 1
12389 1 ▶ return $self->render(text => "Column not found: $line_col", status => 400) unless $col_set{$line_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$line_col} 0 1 1
12390  
12391 1 ▶ $x_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 1 1
12392  
12393 # Aggregate bar and line measures separately via _grouped_totals so both
12394 # use the same path (push or fold) and produce ascending x-axis order.
12395 # A category missing from one result set renders as a gap (undef), not zero.
12396 1 my $bar_rows = $self->_grouped_totals(
12397 { source => $src, records => $records },
12398 { by => [$x_col], fn => 'sum', val => $bar_col, as => 'bar_val' },
12399 );
12400 1 my $line_rows = $self->_grouped_totals(
12401 { source => $src, records => $records },
12402 { by => [$x_col], fn => 'sum', val => $line_col, as => 'line_val' },
12403 );
12404  
12405 # Merge: build a lookup from both result sets and walk the union of categories.
12406 1 my (%bar_map, %line_map, @x_order, %x_seen);
12407 1 for my $r (@$bar_rows) {
12408 4 ▶ my $xv = _decode_cell($r->{$x_col} // '');
Condition50%1/2
$r->{$x_col} // ""
l!l
40
MC/DC0%0/1
$r->{$x_col} // ""
$r->{$x_col}
Truth table50%1/2
A
A= $r->{$x_col}
Aresult
11
00
12409 4 ▶ next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 4 4
12410 4 ▶ push @x_order, $xv unless $x_seen{$xv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $x_seen{$xv}++ 4 0 4
12411 4 $bar_map{$xv} = $r->{bar_val};
12412 }
12413 1 for my $r (@$line_rows) {
12414 4 ▶ my $xv = _decode_cell($r->{$x_col} // '');
Condition50%1/2
$r->{$x_col} // ""
l!l
40
MC/DC0%0/1
$r->{$x_col} // ""
$r->{$x_col}
Truth table50%1/2
A
A= $r->{$x_col}
Aresult
11
00
12415 4 ▶ next unless length $xv;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xv 0 4 4
12416 4 ▶ push @x_order, $xv unless $x_seen{$xv}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $x_seen{$xv}++ 0 4 4
12417 4 $line_map{$xv} = $r->{line_val};
12418 }
12419  
12420 1 my @data = map { {
12421 x => $_,
12422 bar_val => $bar_map{$_}, # undef = gap in bar series
12423 4 line_val => $line_map{$_}, # undef = gap in line series
12424 } } @x_order;
12425  
12426 1 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @data 0 1 1
12427 text => 'No plottable data: no rows have valid data in the selected columns.',
12428 status => 200,
12429 ) unless @data;
12430  
12431 1 my ($source_url, $source_accessed) = $self->_url_attribution;
12432  
12433 1 require HTML::D3;
12434 1 ▶ my $eff_bar_lbl = $bar_label || $bar_col;
Condition33%1/3
$bar_label || $bar_col
l!l&&r!l&&!r
010
MC/DC0%0/2
$bar_label || $bar_col
$bar_label $bar_col
Truth table33%1/3
A || B
A= $bar_labelB= $bar_col
ABresult
1X1
011
000
12435 1 ▶ my $eff_line_lbl = $line_label || $line_col;
Condition33%1/3
$line_label || $line_col
l!l&&r!l&&!r
010
MC/DC0%0/2
$line_label || $line_col
$line_label $line_col
Truth table33%1/3
A || B
A= $line_labelB= $line_col
ABresult
1X1
011
000
12436 1 my $title = "$eff_bar_lbl and $eff_line_lbl by $x_col";
12437 1 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 900, height => 500)
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 0 1 1
12438 ->render_combo_snippet(\@data, {
12439 bar_label => $eff_bar_lbl,
12440 line_label => $eff_line_lbl,
12441 ($scheme ? (scheme => $scheme) : ()),
12442 });
12443  
12444 1 my $chart_html = $snippet->{html};
12445 1 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 1 1
12446  
12447 1 ▶ my %chart_spec = (
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 0 1 1
12448 kind => 'combo',
12449 title => $title,
12450 encode => { x => $x_col, bar => $bar_col, line => $line_col },
12451 ($scheme ? (options => { scheme => $scheme }) : ()),
12452 );
12453 1 my ($platform, $language) = $self->_resolve_template;
12454 1 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 1 1
$back2 ? : 0 1 1
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
010
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
12455 handler => 'tt',
12456 template => "$platform/$language/combo",
12457 format => 'html',
12458 title => $title,
12459 chart_html => $chart_html,
12460 chart => \%chart_spec,
12461 chart_json => $self->_chart_json(\%chart_spec),
12462 point_count => scalar @data,
12463 back_url => ($back2 // $back),
12464 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
12465 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
12466 source_url => $source_url,
12467 source_accessed => $source_accessed,
12468 language => $language,
12469 );
12470 }
12471  
12472 # ---------------------------------------------------------------------------
12473 # Treemap (GET /treemap)
12474 # ---------------------------------------------------------------------------
12475  
12476 # _tile_colour_measure($self, $fn, $col) -> \%measure | { error => $text }
12477 #
12478 # The measure a treemap's tiles are coloured by: $fn (color_agg=, or a card's
12479 # options.color_agg) of $col, average when $fn is absent or empty, since a
12480 # margin or a rating is read as an average rather than a total. A name that
12481 # is not an aggregate function comes back as { error }, worded as the
12482 # function check elsewhere words it. /treemap and a canvas treemap card both
12483 # ask here, so they accept and refuse the same names.
12484 7 ▶sub _tile_colour_measure :Protected ($self, $fn, $col) {
Subroutine _tile_colour_measure: called CC=5 SCAR=16.1
12485 7 ▶ $fn = (defined $fn && !ref $fn) ? lc $fn : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $fn && !ref($fn) ? : 4 3 7
Condition100%3/3
defined $fn and not ref $fn
!ll&&!rl&&r
214
MC/DC100%2/2
defined $fn and not ref $fn
defined $fn not ref $fn
Truth table100%3/3
A and not(B)
A= defined $fnB= not ref $fn
ABresult
0X0
100
111
12486 7 ▶ $fn = 'avg' unless length $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless length $fn 3 4 7
12487 7 ▶ return { error => $self->_i18n('error_agg_fn', $fn, join(' ', @MEASURE_FNS)) }
Branch: 1 branch
BranchTrueFalseTotal
unless _measure_fn_ok($fn) 1 6 7
12488 unless _measure_fn_ok($fn);
12489 6 return _visual_measure($fn, $col);
12490 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
12491  
12492 # _tile_colours($self, $source, \@records, $cat_col, \%measure) -> \%colour_of
12493 #
12494 # The number each treemap tile is coloured by: the measure (from
12495 # _visual_measure) folded per value of $cat_col, keyed by the raw value. With
12496 # $cat_col undef it is one total over every row, under the key ''. A group
12497 # whose result is not a finite number has no entry, and its tile is drawn
12498 # grey. /treemap and a canvas treemap card both fold here, so a card saved
12499 # from the page is coloured by the same numbers.
12500 3 ▶sub _tile_colours :Protected ($self, $source, $records, $cat_col, $m) {
Subroutine _tile_colours: called CC=8 SCAR=22.5
12501 my $rows = $self->_grouped_totals(
12502 { source => $source, records => $records },
12503 { by => [ defined $cat_col ? ($cat_col) : () ],
12504 3 ▶ fn => $m->{fn}, val => $m->{val}, as => 'colour' },
Branch: 1 branch
BranchTrueFalseTotal
defined $cat_col ? : 2 1 3
12505 );
12506 3 my %colour_of;
12507 3 for my $r (@$rows) {
12508 3 my $v = $r->{colour};
12509 3 ▶ next unless defined $v && Scalar::Util::looks_like_number($v) && $v !~ /\A\s*[-+]?(?:nan|inf)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and &Scalar::Util::looks_like_number($v) and not $v =~ /\A\s*[-+]?(?:nan|inf)/iu 0 3 3
Condition33%2/6
defined $v and &Scalar::Util::looks_like_number($v) and not $v =~ /\A\s*[-+]?(?:nan|inf)/iu
!ll&&!rl&&r
003
defined $v and &Scalar::Util::looks_like_number($v)
!ll&&!rl&&r
003
MC/DC0%0/3
defined $v and &Scalar::Util::looks_like_number($v) and not $v =~ /\A\s*[-+]?(?:nan|inf)/iu
defined $v &Scalar::Util::looks_like_number($v) not $v =~ /\A\s*[-+]?(?:nan|inf)/iu
Truth table25%1/4
A and B and not(C)
A= defined $vB= &Scalar::Util::looks_like_number($v)C= not $v =~ /\A\s*[-+]?(?:nan|inf)/iu
ABCresult
0XX0
10X0
1100
1111
12510 3 ▶ $colour_of{ defined $cat_col ? ($r->{$cat_col} // '') : '' } = $v + 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $cat_col ? : 2 1 3
Condition50%1/2
$r->{$cat_col} // ""
l!l
20
MC/DC0%0/1
$r->{$cat_col} // ""
$r->{$cat_col}
Truth table50%1/2
A
A= $r->{$cat_col}
Aresult
11
00
12511 }
12512 3 return \%colour_of;
12513 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
12514  
12515 49 ▶sub treemap_view ($self) {
Subroutine treemap_view: called CC=54 SCAR=52.0
Pod documented
12516 # ?group= is the canonical spec name; ?cat= is the legacy alias (keep both).
12517 49 ▶ my $cat_col = $self->param('group') // $self->param('cat') // '';
Condition100%5/5
($self->param("group") // $self->param("cat")) // ""
l!l
472
$self->param("group") // $self->param("cat")
l!l&&r!l&&!r
6412
MC/DC100%2/2
($self->param("group") // $self->param("cat")) // ""
$self->param("group") $self->param("cat")
Truth table100%3/3
A // B
A= $self->param("group")B= $self->param("cat")
ABresult
1X1
011
000
12518 # ?size= is the canonical spec name; ?val= is the legacy alias (keep both).
12519 49 ▶ my $val_col = $self->param('size') // $self->param('val') // '';
Condition100%5/5
($self->param("size") // $self->param("val")) // ""
l!l
544
$self->param("size") // $self->param("val")
l!l&&r!l&&!r
4144
MC/DC100%2/2
($self->param("size") // $self->param("val")) // ""
$self->param("size") $self->param("val")
Truth table100%3/3
A // B
A= $self->param("size")B= $self->param("val")
ABresult
1X1
011
000
12520 49 my $scheme = _categorical_scheme($self->param('scheme'));
12521 49 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
049
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
12522 49 my $back2 = _safe_back_url($self->param('back2'));
12523 49 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
049
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12524  
12525 # ?color= shades each tile by a second column, folded per tile with
12526 # ?color_agg= (default avg: a margin or a rating is read as an average, not
12527 # a total) on the sequential ?color_scheme=, as the heatmap's cells are.
12528 49 ▶ my $color_col = $self->param('color') // '';
Condition100%2/2
$self->param("color") // ""
l!l
148
MC/DC100%1/1
$self->param("color") // ""
$self->param("color")
Truth table100%2/2
A
A= $self->param("color")
Aresult
11
00
12529 49 ▶ my $color_m = length $color_col
Branch: 1 branch
BranchTrueFalseTotal
length $color_col ? : 1 48 49
12530 ? $self->_tile_colour_measure($self->param('color_agg'), $color_col)
12531 : undef;
12532 49 my $color_scheme = _sequential_scheme($self->param('color_scheme'));
12533  
12534 49 ▶ return $self->render(text => 'Missing group= parameter', status => 400) unless length $cat_col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cat_col 3 46 49
12535 46 ▶ return $self->render(text => $color_m->{error}, status => 400) if $color_m && $color_m->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $color_m and $color_m->{"error"} 0 46 46
Condition66%2/3
$color_m and $color_m->{"error"}
!ll&&!rl&&r
4510
MC/DC0%0/2
$color_m and $color_m->{"error"}
$color_m $color_m->{"error"}
Truth table66%2/3
A and B
A= $color_mB= $color_m->{"error"}
ABresult
0X0
100
111
12536  
12537 46 my ($src, $records, $columns) = $self->_run_pipeline_source;
12538 46 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 45 46
12539  
12540 45 my $span = $self->_span_calc_spec($columns, 'group', 'cat');
12541 45 ▶ return $self->render(text => $span->{error}, status => 400) if $span && $span->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $span and $span->{"error"} 1 44 45
Condition66%2/3
$span and $span->{"error"}
!ll&&!rl&&r
4401
MC/DC50%1/2
$span and $span->{"error"}
$span $span->{"error"}
Truth table66%2/3
A and B
A= $spanB= $span->{"error"}
ABresult
0X0
100
111
12542  
12543 44 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns,
12544 44 ▶ ($span ? { calc => $span->{calc} } : undef)) };
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 0 44 44
12545 44 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 43 44
12546  
12547 43 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 42 43
12548  
12549 42 my %col_set = map { $_ => 1 } @{$columns};
12550 42 ▶ return $self->render(text => "Column not found: $cat_col", status => 400) unless $col_set{$cat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$cat_col} 3 39 42
12551  
12552 39 ▶ my $count_mode = (!length $val_col || $val_col eq '__count__');
Condition66%2/3
!length($val_col) || $val_col eq '__count__'
l!l&&r!l&&!r
3504
MC/DC50%1/2
!length($val_col) || $val_col eq '__count__'
!length($val_col) $val_col eq '__count__'
Truth table66%2/3
not(A) || B
A= !length($val_col)B= $val_col eq '__count__'
ABresult
1X1
011
000
12553 return $self->render(text => "Column not found: $val_col", status => 400)
12554 39 ▶ unless $count_mode || $col_set{$val_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $count_mode or $col_set{$val_col} 2 37 39
Condition100%3/3
$count_mode or $col_set{$val_col}
l!l&&r!l&&!r
3522
MC/DC100%2/2
$count_mode or $col_set{$val_col}
$count_mode $col_set{$val_col}
Truth table100%3/3
A or B
A= $count_modeB= $col_set{$val_col}
ABresult
1X1
011
000
12555 return $self->render(text => "Column not found: $color_col", status => 400)
12556 37 ▶ if length $color_col && !$col_set{$color_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $color_col and not $col_set{$color_col} 1 36 37
Condition66%2/3
length $color_col and not $col_set{$color_col}
!ll&&!rl&&r
3601
MC/DC50%1/2
length $color_col and not $col_set{$color_col}
length $color_col not $col_set{$color_col}
Truth table66%2/3
A and not(B)
A= length $color_colB= not $col_set{$color_col}
ABresult
0X0
100
111
12557  
12558 36 ▶ $cat_col = $span->{col} if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 0 36 36
12559  
12560 36 ▶ my $agg_rows = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 34 2 36
$count_mode ? : 34 2 36
12561 { source => $src, records => $records },
12562 {
12563 by => [$cat_col],
12564 fn => $count_mode ? 'count' : 'sum',
12565 val => $count_mode ? undef : $val_col,
12566 as => 'value',
12567 },
12568 );
12569  
12570 36 ▶ my $colour_of = $color_m
Branch: 1 branch
BranchTrueFalseTotal
$color_m ? : 0 36 36
12571 ? $self->_tile_colours($src, $records, $cat_col, $color_m)
12572 : {};
12573  
12574 # Top-N: keep the highest-N tiles, aggregate the rest as "Other".
12575 36 ▶ my $topn_n = _whole_number($self->param('topn')) // 0;
Condition50%1/2
_whole_number($self->param("topn")) // 0
l!l
036
MC/DC0%0/1
_whole_number($self->param("topn")) // 0
_whole_number($self->param("topn"))
Truth table50%1/2
A
A= _whole_number($self->param("topn"))
Aresult
11
00
12576 36 ▶ if ($topn_n > 0 && @$agg_rows > $topn_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topn_n > 0 and @$agg_rows > $topn_n) 0 36 36
Condition33%1/3
$topn_n > 0 and @$agg_rows > $topn_n
!ll&&!rl&&r
3600
MC/DC0%0/2
$topn_n > 0 and @$agg_rows > $topn_n
$topn_n > 0 @$agg_rows > $topn_n
Truth table33%1/3
A and B
A= $topn_n > 0B= @$agg_rows > $topn_n
ABresult
0X0
100
111
12577 0 ▶ my @sorted = sort { ($b->{value} // 0) <=> ($a->{value} // 0) } @$agg_rows;
Condition0%0/4
$Database::BI::Controller::Dashboard::b->{"value"} // 0
l!l
00
$Database::BI::Controller::Dashboard::a->{"value"} // 0
l!l
00
MC/DC0%0/1
$Database::BI::Controller::Dashboard::b->{"value"} // 0
$Database::BI::Controller::Dashboard::b->{"value"}
MC/DC0%0/1
$Database::BI::Controller::Dashboard::a->{"value"} // 0
$Database::BI::Controller::Dashboard::a->{"value"}
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::b->{"value"}
Aresult
11
00
Truth table0%0/2
A
A= $Database::BI::Controller::Dashboard::a->{"value"}
Aresult
11
00
12578 0 my @top_agg = @sorted[0 .. $topn_n - 1];
12579 0 ▶ my %top_cats = map { ($_->{$cat_col} // '') => 1 } @top_agg;
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
12580 my $other_rec = [
12581 0 ▶ grep { !$top_cats{$_->{$cat_col} // ''} } @$records
Condition0%0/2
$_->{$cat_col} // ""
l!l
00
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table0%0/2
A
A= $_->{$cat_col}
Aresult
11
00
12582 ];
12583 0 ▶ if (@$other_rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$other_rec) 0 0 0
12584 0 ▶ my $other = $self->_grouped_totals(
Branch: 2 branches
BranchTrueFalseTotal
$count_mode ? : 0 0 0
$count_mode ? : 0 0 0
12585 { source => undef, records => $other_rec },
12586 { by => [], fn => ($count_mode ? 'count' : 'sum'),
12587 val => ($count_mode ? undef : $val_col), as => 'value' },
12588 );
12589 0 ▶ if (@$other && defined $other->[0]{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$other and defined $other->[0]{"value"}) 0 0 0
Condition0%0/3
@$other and defined $other->[0]{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
@$other and defined $other->[0]{"value"}
@$other defined $other->[0]{"value"}
Truth table0%0/3
A and B
A= @$otherB= defined $other->[0]{"value"}
ABresult
0X0
100
111
12590 0 push @top_agg, { $cat_col => 'Other', value => $other->[0]{value} };
12591 # Other's colour is its own rows', not an average of averages.
12592 # The key is the tile, so a category itself named Other is
12593 # drawn with this one, as its area already is.
12594 0 ▶ $colour_of->{Other} = $self->_tile_colours(undef, $other_rec, undef, $color_m)->{''}
Branch: 1 branch
BranchTrueFalseTotal
if $color_m 0 0 0
12595 if $color_m;
12596 }
12597 }
12598 0 $agg_rows = \@top_agg;
12599 }
12600  
12601 36 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @$agg_rows 0 36 36
12602 text => 'No plottable data: no rows have valid data in the selected column.',
12603 status => 200,
12604 ) unless @$agg_rows;
12605  
12606 my @data = map {
12607 36 ▶ my $cv = $colour_of->{ $_->{$cat_col} // '' };
Condition50%1/2
$_->{$cat_col} // ""
l!l
1440
MC/DC0%0/1
$_->{$cat_col} // ""
$_->{$cat_col}
Truth table50%1/2
A
A= $_->{$cat_col}
Aresult
11
00
12608 +{ path => 'Root/' . _decode_cell($_->{$cat_col}), value => $_->{value},
12609 144 ▶ (defined $cv ? (color_value => $cv) : ()) }
Branch: 1 branch
BranchTrueFalseTotal
defined $cv ? : 0 144 144
12610 } @$agg_rows;
12611  
12612 36 my ($source_url, $source_accessed) = $self->_url_attribution;
12613  
12614 36 require HTML::D3;
12615 36 ▶ my $title = $count_mode ? "Treemap of $cat_col by count" : "Treemap of $cat_col by $val_col";
Branch: 1 branch
BranchTrueFalseTotal
$count_mode ? : 34 2 36
12616 36 ▶ $title .= " coloured by $color_m->{label}" if $color_m;
Branch: 1 branch
BranchTrueFalseTotal
if $color_m 0 36 36
12617 my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 800, height => 500)
12618 ->render_treemap_snippet(\@data, {
12619 scheme => $scheme,
12620 36 ▶ ($color_m ? (color_scheme => $color_scheme, color_label => $color_m->{label}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$color_m ? : 0 36 36
12621 });
12622  
12623 36 my $chart_html = $snippet->{html};
12624 36 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 36 36
12625  
12626 my %chart_spec = (
12627 kind => 'treemap',
12628 title => $title,
12629 encode => { cat => $cat_col, value => $val_col,
12630 ($color_m ? (color => $color_col) : ()) },
12631 options => { scheme => $scheme,
12632 36 ▶ ($color_m ? (color_agg => $color_m->{fn}, color_scheme => $color_scheme) : ()) },
Branch: 2 branches
BranchTrueFalseTotal
$color_m ? : 0 36 36
$color_m ? : 0 36 36
12633 );
12634 36 my ($platform, $language) = $self->_resolve_template;
12635 36 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 36 36
$back2 ? : 0 36 36
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0360
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
12636 handler => 'tt',
12637 template => "$platform/$language/treemap",
12638 format => 'html',
12639 title => $title,
12640 chart_html => $chart_html,
12641 chart => \%chart_spec,
12642 chart_json => $self->_chart_json(\%chart_spec),
12643 tile_count => scalar @data,
12644 cat_col => $cat_col,
12645 drill_back_url => $back,
12646 back_url => ($back2 // $back),
12647 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
12648 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
12649 source_url => $source_url,
12650 source_accessed => $source_accessed,
12651 language => $language,
12652 );
12653 }
12654  
12655 # ---------------------------------------------------------------------------
12656 # Histogram (GET /histogram)
12657 # ---------------------------------------------------------------------------
12658  
12659 50 ▶sub histogram_view ($self) {
Subroutine histogram_view: called CC=35 SCAR=37.0
Pod documented
12660 50 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
482
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
12661 50 my $_bins_raw = $self->param('bins');
12662 50 ▶ my $bins = (defined $_bins_raw && length $_bins_raw) ? $_bins_raw : 10;
Branch: 1 branch
BranchTrueFalseTotal
defined $_bins_raw && length $_bins_raw ? : 4 46 50
Condition100%3/3
defined $_bins_raw and length $_bins_raw
!ll&&!rl&&r
4514
MC/DC100%2/2
defined $_bins_raw and length $_bins_raw
defined $_bins_raw length $_bins_raw
Truth table100%3/3
A and B
A= defined $_bins_rawB= length $_bins_raw
ABresult
0X0
100
111
12663 50 my $scheme = _optional_scheme($self->param('scheme'));
12664 50 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
050
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
12665 50 my $back2 = _safe_back_url($self->param('back2'));
12666 50 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
050
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12667  
12668 50 ▶ return $self->render(text => 'Missing col= parameter', status => 400) unless length $col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 3 47 50
12669  
12670 47 my ($src, $records, $columns) = $self->_run_pipeline_source;
12671 47 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 46 47
12672  
12673 46 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
12674 46 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 45 46
12675  
12676 45 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 44 45
12677  
12678 44 my %col_set = map { $_ => 1 } @{$columns};
12679 44 ▶ return $self->render(text => "Column not found: $col", status => 400) unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 2 42 44
12680  
12681 42 my @values;
12682 42 for my $row (@$records) {
12683 252 ▶ my $v = _numeric_cell($row->{$col} // '');
Condition50%1/2
$row->{$col} // ""
l!l
2520
MC/DC0%0/1
$row->{$col} // ""
$row->{$col}
Truth table50%1/2
A
A= $row->{$col}
Aresult
11
00
12684 252 ▶ push @values, $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 252 0 252
12685 }
12686  
12687 42 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 42 42
12688 text => 'No plottable data: no rows have valid numeric values in the selected column.',
12689 status => 200,
12690 ) unless @values;
12691  
12692 42 ▶ $bins = _whole_number($bins) // ($bins =~ /\A\d+\z/ ? $MAX_HISTOGRAM_BINS : 1); # more digits than a number holds is a lot of bins
Branch: 1 branch
BranchTrueFalseTotal
$bins =~ /\A\d+\z/u ? : 0 1 1
Condition66%2/3
_whole_number($bins) // ($bins =~ /\A\d+\z/u ? $MAX_HISTOGRAM_BINS : 1)
l!l&&r!l&&!r
4110
MC/DC0%0/2
_whole_number($bins) // ($bins =~ /\A\d+\z/u ? $MAX_HISTOGRAM_BINS : 1)
_whole_number($bins) ($bins =~ /\A\d+\z/u ? $MAX_HISTOGRAM_BINS : 1)
Truth table66%2/3
A // B
A= _whole_number($bins)B= ($bins =~ /\A\d+\z/u ? $MAX_HISTOGRAM_BINS : 1)
ABresult
1X1
011
000
12693 42 ▶ $bins = 1 if $bins < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bins < 1 1 41 42
12694 42 ▶ $bins = $MAX_HISTOGRAM_BINS if $bins > $MAX_HISTOGRAM_BINS;
Branch: 1 branch
BranchTrueFalseTotal
if $bins > $MAX_HISTOGRAM_BINS 1 41 42
12695  
12696 # Server-side binning: compute equal-width bins over the data range.
12697 # Values equal to $max land in the highest bin (no overflow bar).
12698 # Uses the same bin-boundary logic as Calc.pm bin() to stay consistent with
12699 # ?c=band:bin(col,width) computed columns on the same data.
12700 5 ▶ use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
12701 42 my $min_v = min(@values);
12702 42 my $max_v = max(@values);
12703 42 my @bin_data;
12704 42 ▶ if ($min_v == $max_v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_v == $max_v) { } 0 42 42
12705 # All values identical: one bin.
12706 0 push @bin_data, { label => "$min_v", count => scalar @values, lo => $min_v, hi => $min_v };
12707 } else {
12708 42 my $raw_w = ($max_v - $min_v) / $bins;
12709 # Round up to a "nice" width using 10 significant digits (same guard as Calc bin()).
12710 42 my $width = sprintf('%.10g', $raw_w) + 0;
12711 42 ▶ $width = $raw_w if $width <= 0; # safety: never produce zero width
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 0 42 42
12712 42 ▶ my $fmt = ($width == int($width)) ? '%d' : '%g';
Branch: 1 branch
BranchTrueFalseTotal
$width == int $width ? : 0 42 42
12713  
12714 # Build bin boundaries.
12715 42 my @bin_counts;
12716 my @bin_los;
12717 42 for my $i (0 .. $bins - 1) {
12718 587 push @bin_los, $min_v + $i * $width;
12719 587 push @bin_counts, 0;
12720 }
12721  
12722 # Assign each value to a bin.
12723 42 for my $v (@values) {
12724 252 my $idx = floor(sprintf('%.10g', ($v - $min_v) / $width) + 0);
12725 252 ▶ $idx = $bins - 1 if $idx >= $bins; # max value -> last bin
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= $bins 42 210 252
12726 252 ▶ $idx = 0 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 252 252
12727 252 $bin_counts[$idx]++;
12728 }
12729  
12730 42 for my $i (0 .. $bins - 1) {
12731 587 ▶ next unless $bin_counts[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $bin_counts[$i] 386 201 587
12732 201 my $lo = $bin_los[$i];
12733 201 my $hi = $lo + $width;
12734 201 my $lbl = sprintf("[${fmt},${fmt})", $lo, $hi);
12735 201 push @bin_data, { label => $lbl, count => $bin_counts[$i], lo => $lo, hi => $hi };
12736 }
12737 }
12738  
12739 42 my ($source_url, $source_accessed) = $self->_url_attribution;
12740  
12741 42 require HTML::D3;
12742 42 my $title = "Distribution of $col";
12743 42 ▶ my $snippet = HTML::D3->new(nonce => $self->csp_nonce, title => $title, width => 800, height => 480)
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 0 42 42
12744 ->render_histogram_snippet(\@bin_data, {
12745 binned => 1,
12746 x_label => $col,
12747 ($scheme ? (scheme => $scheme) : ()),
12748 });
12749  
12750 42 my $chart_html = $snippet->{html};
12751 42 ▶ utf8::decode($chart_html) unless utf8::is_utf8($chart_html);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($chart_html) 0 42 42
12752  
12753 42 ▶ my %chart_spec = (
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 0 42 42
12754 kind => 'histogram',
12755 title => $title,
12756 encode => { value => $col },
12757 options => { bins => $bins, ($scheme ? (scheme => $scheme) : ()) },
12758 );
12759 42 my ($platform, $language) = $self->_resolve_template;
12760 42 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 42 42
$back2 ? : 0 42 42
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0420
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
12761 handler => 'tt',
12762 template => "$platform/$language/histogram",
12763 format => 'html',
12764 title => $title,
12765 chart_html => $chart_html,
12766 chart => \%chart_spec,
12767 chart_json => $self->_chart_json(\%chart_spec),
12768 value_count => scalar @values,
12769 bins => $bins,
12770 back_url => ($back2 // $back),
12771 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
12772 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
12773 source_url => $source_url,
12774 source_accessed => $source_accessed,
12775 language => $language,
12776 );
12777 }
12778  
12779 # ---------------------------------------------------------------------------
12780 # Word cloud (GET /wordcloud)
12781 # ---------------------------------------------------------------------------
12782  
12783 # The words that say nothing about the data: without them a cloud of a notes
12784 # column is all "the" and "and". English, like the only page language the
12785 # templates have. ?stop= adds to this list for one request rather than
12786 # replacing it, so adding a word never silently brings back the ones this list
12787 # already removes.
12788 5 my %STOP_WORD = map { $_ => 1 } qw(
12789 a about above after again against all also am an and any are as at be
12790 because been before being below between both but by can could did do does
12791 doing down during each few for from further had has have having he her here
12792 hers herself him himself his how i if in into is it its itself just me more
12793 most my myself no nor not now of off on once only or other our ours ourselves
12794 out over own same she should so some such than that the their theirs them
12795 themselves then there these they this those through to too under until up
12796 very was we were what when where which while who whom why will with would
12797 you your yours yourself yourselves
12798 );
12799  
12800 # _percent_encode($text) -> $url_component
12801 #
12802 # Purpose: Percent-encode one value for a query string.
12803 # Notes: url_escape() works on bytes, so a decoded (character) string is
12804 # encoded to UTF-8 first; otherwise "caf\x{e9}" would become "caf%E9" and a
12805 # browser would read that back as Latin-1.
12806 12 ▶sub _percent_encode {
Subroutine _percent_encode: called CC=3 SCAR=11.0
12807 12 my ($text) = @_;
12808 12 ▶ my $bytes = defined $text ? "$text" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 11 1 12
12809 12 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 1 11 12
12810 12 return url_escape($bytes);
12811 }
12812  
12813 # _word_tokens($text, $minlen) -> @words
12814 #
12815 # Purpose: The words in one cell, lower-cased and split on everything that is
12816 # neither a letter, a digit nor an apostrophe.
12817 # Entry: $text is a cell (may be undef); $minlen is the shortest word kept.
12818 # Exit: The words at least $minlen characters long that contain a letter.
12819 # A run of digits alone is kept -- a year or a model number can be the
12820 # point of a column -- while a run of punctuation is not a word at all.
12821 # Notes: A CSV cell arrives as bytes, so it is decoded first (the same
12822 # _decode_cell() every page uses): without that, \p{L} would see the
12823 # bytes of "caf\x{e9}" and split the word in two. The apostrophe stays
12824 # inside a word, so O'Brien is one word rather than "o" and "brien".
12825 284 ▶sub _word_tokens {
Subroutine _word_tokens: called CC=5 SCAR=16.1
12826 284 my ($text, $minlen) = @_;
12827 284 ▶ return () unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 8 276 284
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
71276
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
12828  
12829 276 my @words = split /[^\p{L}\p{N}']+/, lc _decode_cell($text);
12830 276 for my $word (@words) {
12831 554 $word =~ s/\A'+//;
12832 554 $word =~ s/'+\z//;
12833 }
12834  
12835 276 ▶ return grep { length($_) >= $minlen && /\p{L}/ } @words;
Condition100%3/3
length $_ >= $minlen && /\p{L}/u
!ll&&!rl&&r
25322873
MC/DC100%2/2
length $_ >= $minlen && /\p{L}/u
length $_ >= $minlen /\p{L}/u
Truth table100%3/3
A && B
A= length $_ >= $minlenB= /\p{L}/u
ABresult
0X0
100
111
12836 }
12837  
12838 # _wordcloud_option($raw, $default, $min, $max) -> $n
12839 #
12840 # Purpose: One of the two counts a cloud takes -- how many words to show, how
12841 # short a word may be.
12842 # Notes: A value that is absent or unreadable is the default, not the minimum:
12843 # ?topn= with nothing after it must not silently show a single word.
12844 115 ▶sub _wordcloud_option {
Subroutine _wordcloud_option: called CC=6 SCAR=17.9
12845 115 my ($raw, $default, $min, $max) = @_;
12846  
12847 115 ▶ my $n = (defined $raw && !ref $raw && $raw =~ /\A\d+\z/) ? int $raw : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw && !ref($raw) && $raw =~ /\A\d+\z/u ? : 5 110 115
Condition100%6/6
defined $raw and not ref $raw and $raw =~ /\A\d+\z/u
!ll&&!rl&&r
98125
defined $raw and not ref $raw
!ll&&!rl&&r
97117
MC/DC100%3/3
defined $raw and not ref $raw and $raw =~ /\A\d+\z/u
defined $raw not ref $raw $raw =~ /\A\d+\z/u
Truth table100%4/4
A and not(B) and C
A= defined $rawB= not ref $rawC= $raw =~ /\A\d+\z/u
ABCresult
0XX0
10X0
1100
1111
12848 115 ▶ $n = $min if $n < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $n < $min 1 114 115
12849 115 ▶ $n = $max if $n > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max 3 112 115
12850 115 return $n;
12851 }
12852  
12853 # _wordcloud_counts($records, $col, $topn, $minlen, $extra_stop) -> \%cloud
12854 #
12855 # Purpose: The words a text column repeats and how often each appears, ranked,
12856 # which is what both the /wordcloud page and a canvas's word-cloud card
12857 # draw. One function, so the page and the card cannot count differently.
12858 # Entry: $records is a list of rows; $col names the text column; $topn is how
12859 # many words to return; $minlen is the shortest word kept; $extra_stop
12860 # is the reader's own stoplist ("apple,pear"), added to the built-in
12861 # one rather than replacing it.
12862 # Exit: { words => [ { word, count }, ... ] } in cloud order -- count
12863 # descending, then alphabetically -- with distinct (every different word
12864 # seen, before truncation), total (words counted) and rows (rows read).
12865 # Notes: A word counts once per row that contains it, so a long note does not
12866 # outweigh ten short rows that say the same thing.
12867 48 ▶sub _wordcloud_counts {
Subroutine _wordcloud_counts: called CC=10 SCAR=23.1
12868 48 my ($records, $col, $topn, $minlen, $extra_stop) = @_;
12869  
12870 48 my %stop = %STOP_WORD;
12871 48 ▶ for my $extra (split /[\s,]+/, $extra_stop // '') {
Condition100%2/2
$extra_stop // ""
l!l
444
MC/DC100%1/1
$extra_stop // ""
$extra_stop
Truth table100%2/2
A
A= $extra_stop
Aresult
11
00
12872 3 ▶ $stop{ lc $extra } = 1 if length $extra;
Branch: 1 branch
BranchTrueFalseTotal
if length $extra 3 0 3
12873 }
12874  
12875 48 my (%count, $row_count);
12876 48 for my $row (@$records) {
12877 276 $row_count++;
12878 276 my %seen;
12879 276 ▶ $count{$_}++ for grep { !$seen{$_}++ && !$stop{$_} }
Condition100%3/3
$seen{$_}++ || $stop{$_}
l!l&&r!l&&!r
2753
MC/DC100%2/2
$seen{$_}++ || $stop{$_}
$seen{$_}++ $stop{$_}
Truth table100%3/3
A || B
A= $seen{$_}++B= $stop{$_}
ABresult
1X1
011
000
12880 _word_tokens($row->{$col}, $minlen);
12881 }
12882  
12883 48 ▶ my $total = sum(values %count) // 0;
Condition100%2/2
&sum(values %count) // 0
l!l
840
MC/DC100%1/1
&sum(values %count) // 0
&sum(values %count)
Truth table100%2/2
A
A= &sum(values %count)
Aresult
11
00
12884 48 ▶ my @words = sort { $count{$b} <=> $count{$a} || $a cmp $b } keys %count;
Condition66%2/3
$count{$b} <=> $count{$a} || $a cmp $b
l!l&&r!l&&!r
1270
MC/DC0%0/2
$count{$b} <=> $count{$a} || $a cmp $b
$count{$b} <=> $count{$a} $a cmp $b
Truth table66%2/3
A || B
A= $count{$b} <=> $count{$a}B= $a cmp $b
ABresult
1X1
011
000
12885 48 my $distinct = scalar @words;
12886 48 ▶ @words = @words[0 .. $topn - 1] if @words > $topn;
Branch: 1 branch
BranchTrueFalseTotal
if @words > $topn 1 47 48
12887  
12888 return {
12889 48 words => [ map { { word => $_, count => $count{$_} } } @words ],
12890 distinct => $distinct,
12891 total => $total,
12892 rows => $row_count,
12893 };
12894 }
12895  
12896 # _wordcloud_link($back, $col, $word, $cloud_url) -> $url
12897 #
12898 # Purpose: The view URL that shows the rows containing one word, with the cloud
12899 # as that page's breadcrumb, so a click on a word is a drill-down.
12900 # Note: The URL is returned unescaped; the caller escapes it once for the
12901 # href it goes into, because HTML::TagCloud writes its input verbatim.
12902 3 ▶sub _wordcloud_link {
Subroutine _wordcloud_link: called CC=2 SCAR=6.9
12903 3 my ($back, $col, $word, $cloud_url) = @_;
12904 3 ▶ my $sep = $back =~ /\?/ ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$back =~ /\?/u ? : 1 2 3
12905  
12906 3 return $back . $sep . 'f=' . _percent_encode("$col:contains:$word")
12907 . '&back2=' . _percent_encode($cloud_url)
12908 . '&back2_label=' . _percent_encode('Back to word cloud');
12909 }
12910  
12911 52 ▶sub wordcloud_view ($self) {
Subroutine wordcloud_view: called CC=22 SCAR=31.6
Pod documented
12912 52 ▶ my $col = $self->param('col') // '';
Condition100%2/2
$self->param("col") // ""
l!l
502
MC/DC100%1/1
$self->param("col") // ""
$self->param("col")
Truth table100%2/2
A
A= $self->param("col")
Aresult
11
00
12913 52 my $_topn = $self->param('topn');
12914 52 my $_minlen = $self->param('minlen');
12915 52 my $back_raw = $self->param('back');
12916 52 ▶ my $back = _safe_back_url($back_raw) // '/';
Condition50%1/2
_safe_back_url($back_raw) // "/"
l!l
052
MC/DC0%0/1
_safe_back_url($back_raw) // "/"
_safe_back_url($back_raw)
Truth table50%1/2
A
A= _safe_back_url($back_raw)
Aresult
11
00
12917 52 my $back2 = _safe_back_url($self->param('back2'));
12918 52 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
052
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
12919  
12920 # A count that is absent or unreadable is the default, not a clamp of zero:
12921 # ?topn= with nothing after it must not silently show a single word.
12922 52 my $topn = _wordcloud_option($_topn, 50, 1, 500);
12923 52 my $minlen = _wordcloud_option($_minlen, 3, 1, 64);
12924  
12925 52 ▶ return $self->render(text => 'Missing col= parameter', status => 400) unless length $col;
Branch: 1 branch
BranchTrueFalseTotal
unless length $col 3 49 52
12926  
12927 49 my ($src, $records, $columns) = $self->_run_pipeline_source;
12928 49 ▶ return $self->render(text => 'Could not open data source', status => 404) unless defined $records;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 2 47 49
12929  
12930 47 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
12931 47 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 46 47
12932  
12933 46 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 45 46
12934  
12935 45 my %col_set = map { $_ => 1 } @$columns;
12936 45 ▶ return $self->render(text => "Column not found: $col", status => 400) unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 1 44 45
12937  
12938 # One pass over the rows, shared with a canvas's word-cloud card so the page
12939 # and the card count the same way.
12940 44 my $cloud = _wordcloud_counts(
12941 $records, $col, $topn, $minlen, $self->param('stop'),
12942 );
12943 44 my @words = map { $_->{word} } @{ $cloud->{words} };
12944 44 my %count = map { $_->{word} => $_->{count} } @{ $cloud->{words} };
12945 44 my $distinct = $cloud->{distinct};
12946 44 my $word_total = $cloud->{total};
12947 44 my $row_count = $cloud->{rows};
12948  
12949 # A word links back to the rows that contain it, so the cloud is a way into
12950 # the data rather than a picture of it. Only when the request names the page
12951 # it came from: a bare /wordcloud URL has no table to filter, and sending a
12952 # click to "/?f=..." would be a dead end.
12953 44 ▶ my $drill = (defined $back_raw && length $back_raw) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $back_raw && length $back_raw ? : 1 43 44
Condition66%2/3
defined $back_raw and length $back_raw
!ll&&!rl&&r
4301
MC/DC50%1/2
defined $back_raw and length $back_raw
defined $back_raw length $back_raw
Truth table66%2/3
A and B
A= defined $back_rawB= length $back_raw
ABresult
0X0
100
111
12954  
12955 44 my $cloud_url = $self->req->url->path->to_string;
12956 44 my $qs = $self->req->url->query->to_string;
12957 44 ▶ $cloud_url .= "?$qs" if length $qs;
Branch: 1 branch
BranchTrueFalseTotal
if length $qs 44 0 44
12958  
12959 my @tags = map {
12960 44 {
12961 word => _decode_cell($_),
12962 18 ▶ count => $count{$_},
Branch: 1 branch
BranchTrueFalseTotal
$drill ? : 0 18 18
12963 ($drill ? (href => _wordcloud_link($back, $col, $_, $cloud_url)) : ()),
12964 }
12965 } @words;
12966  
12967 # The font size is the module's own logarithm of the count, so a word that
12968 # appears twice as often is one step bigger rather than twice as wide.
12969 44 require HTML::TagCloud;
12970 44 my $ranked = HTML::TagCloud->new(levels => 18);
12971 44 for my $tag (@tags) {
12972 18 my $name = xml_escape($tag->{word});
12973 18 ▶ if (defined $tag->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{'href'}) { } 0 18 18
12974 0 $ranked->add($name, xml_escape($tag->{href}), $tag->{count});
12975 }
12976 else {
12977 18 $ranked->add_static($name, $tag->{count});
12978 }
12979 }
12980  
12981 44 ▶ my $cloud_html = @tags ? $ranked->html_and_css(scalar @tags) : '';
Branch: 1 branch
BranchTrueFalseTotal
@tags ? : 6 38 44
12982  
12983 44 my ($source_url, $source_accessed) = $self->_url_attribution;
12984 44 my $title = "Words in $col";
12985  
12986 # The spec a canvas word-cloud card reads (_render_visual_html), so the
12987 # layout's New canvas / Add to existing canvas buttons save this cloud as
12988 # it is drawn, as they do for every other chart page.
12989 44 my $stop = $self->param('stop');
12990 44 ▶ my %chart_spec = (
Branch: 1 branch
BranchTrueFalseTotal
defined $stop && length $stop ? : 1 43 44
Condition66%2/3
defined $stop and length $stop
!ll&&!rl&&r
4301
MC/DC50%1/2
defined $stop and length $stop
defined $stop length $stop
Truth table66%2/3
A and B
A= defined $stopB= length $stop
ABresult
0X0
100
111
12991 kind => 'wordcloud',
12992 title => $title,
12993 encode => { col => $col },
12994 options => {
12995 topn => $topn,
12996 minlen => $minlen,
12997 ((defined $stop && length $stop) ? (stop => $stop) : ()),
12998 },
12999 );
13000  
13001 44 my ($platform, $language) = $self->_resolve_template;
13002 44 ▶ $self->render(
Branch: 2 branches
BranchTrueFalseTotal
$back2 ? : 0 44 44
$back2 ? : 0 44 44
Condition20%1/5
$back2 // $back
l!l&&r!l&&!r
0440
$back2_lbl or "Back"
l!l
00
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
13003 handler => 'tt',
13004 template => "$platform/$language/wordcloud",
13005 format => 'html',
13006 title => $title,
13007 chart => \%chart_spec,
13008 chart_json => $self->_chart_json(\%chart_spec),
13009 cloud_html => $cloud_html,
13010 words => \@tags,
13011 col => $col,
13012 topn => $topn,
13013 minlen => $minlen,
13014 drill => $drill,
13015 distinct_words => $distinct,
13016 word_total => $word_total,
13017 row_count => $row_count,
13018 back_url => ($back2 // $back),
13019 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
13020 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
13021 source_url => $source_url,
13022 source_accessed => $source_accessed,
13023 language => $language,
13024 );
13025 }
13026  
13027 # ---------------------------------------------------------------------------
13028 # Map (GET /map)
13029 #
13030 # one route, one template, two ways for a row to get a
13031 # position. With lat= and lon= the columns already hold coordinates; with
13032 # loc=<column> every distinct value of that column is geocoded once, by
13033 # Database::BI::Model::Geocode. Answers are remembered in one shared cache in
13034 # the model store, keyed by the place rather than by the file or column that
13035 # named it, so a second visit asks nothing and a second dataset naming the same
13036 # place asks nothing either. Everything before that - the pipeline, calculated
13037 # fields and f= filters - is the same as every other visual, so a map shows the
13038 # filtered, calculated view and not the raw table.
13039 # ---------------------------------------------------------------------------
13040  
13041 # _map_loc_columns($loc) -> @columns
13042 #
13043 # The place-name columns of a map, in order, from loc: one name (a canvas card
13044 # saved before a place could span columns), or a list of them (loc= repeated,
13045 # or a card's encode.loc array). Empty and non-string entries are dropped, and
13046 # a column named twice is used once.
13047 59 ▶sub _map_loc_columns {
Subroutine _map_loc_columns: called CC=5 SCAR=16.1
13048 59 my $loc = shift;
13049 59 ▶ my @names = ref($loc) eq 'ARRAY' ? @{$loc} : ($loc);
Branch: 1 branch
BranchTrueFalseTotal
ref $loc eq 'ARRAY' ? : 49 10 59
13050 59 my %seen;
13051 59 ▶ return grep { defined($_) && !ref($_) && length($_) && !$seen{$_}++ } @names;
Condition100%9/9
defined $_ && !ref($_) && length $_ && !$seen{$_}++
!ll&&!rl&&r
7111
defined $_ and not ref $_ and length $_
!ll&&!rl&&r
5212
defined $_ and not ref $_
!ll&&!rl&&r
3214
MC/DC100%4/4
defined $_ && !ref($_) && length $_ && !$seen{$_}++
defined $_ not ref $_ length $_ !$seen{$_}++
Truth table100%5/5
A and not(B) and C && not(D)
A= defined $_B= not ref $_C= length $_D= !$seen{$_}++
ABCDresult
0XXX0
10XX0
110X0
11100
11111
13052 }
13053  
13054 # _map_loc_text($value) -> ($text) | ()
13055 #
13056 # The text added to the end of each place (loc_text=, a card's
13057 # encode.loc_text): "Virginia, USA" for a column of county names, tidied by
13058 # Geocode->place_text, as bi-geocode's --text is. Nothing is returned for a
13059 # value that is not one short line, which the map page refuses and a card,
13060 # stored unchecked, leaves out.
13061 56 ▶sub _map_loc_text {
Subroutine _map_loc_text: called CC=1 SCAR=0.0
13062 56 my ($value) = @_;
13063 56 require Database::BI::Model::Geocode;
13064 56 return Database::BI::Model::Geocode->place_text($value);
13065 }
13066  
13067 # How many outlines holding several markers the map page lists, the fullest
13068 # first; a map of shops has thousands, and the list is for looking into a few.
13069 5 Readonly my $MAP_OUTLINE_GROUPS_MAX => 50;
13070  
13071 # _map_outline_groups($outlines, \@markers, \@records, \@columns) -> @groups
13072 #
13073 # Purpose: Why a map has fewer outlines than markers, for the reader to look
13074 # into: which outlines hold more than one marker, and which markers
13075 # are in no outline. Geocoded places that land in the wrong county
13076 # show up here, as two places in one outline.
13077 # Entry: $outlines as Boundary's outlines() returns it; each marker has
13078 # lat, lon, row_indexes (or first_row) and, when it was geocoded,
13079 # value (the place); the columns that tell rows apart, for the links.
13080 # Exit: One entry for each outline holding two markers or more, the
13081 # fullest first, at most $MAP_OUTLINE_GROUPS_MAX of them, then one
13082 # with none => 1 for the markers in no outline, when there are any:
13083 # { name, markers, places => [text, ...], rows, filters }. A marker
13084 # still waiting for its outline is in neither. () when every marker
13085 # has an outline to itself.
13086 # Side effects: none.
13087 0 ▶sub _map_outline_groups {
Subroutine _map_outline_groups: not called CC=12 SCAR=50.5
13088 0 my ($outlines, $markers, $records, $columns) = @_;
13089  
13090 0 require Database::BI::Model::Boundary;
13091 0 ▶ my $inside = $outlines->{inside} // {};
Condition0%0/2
$outlines->{"inside"} // {}
l!l
00
MC/DC0%0/1
$outlines->{"inside"} // {}
$outlines->{"inside"}
Truth table0%0/2
A
A= $outlines->{"inside"}
Aresult
11
00
13092 0 ▶ my %waiting = map { $_ => 1 } @{ $outlines->{pending} // [] };
Condition0%0/2
$outlines->{"pending"} // []
l!l
00
MC/DC0%0/1
$outlines->{"pending"} // []
$outlines->{"pending"}
Truth table0%0/2
A
A= $outlines->{"pending"}
Aresult
11
00
13093 0 my (%held, @none);
13094 0 for my $marker (@{$markers}) {
13095 0 my $key = Database::BI::Model::Boundary->point_key($marker->{lat}, $marker->{lon});
13096 0 ▶ next if !defined($key) || $waiting{$key};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $waiting{$key} 0 0 0
Condition0%0/3
not defined $key or $waiting{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $key or $waiting{$key}
not defined $key $waiting{$key}
Truth table0%0/3
not(A) or B
A= not defined $keyB= $waiting{$key}
ABresult
1X1
011
000
13097 0 ▶ if (defined(my $osm = $inside->{$key})) { push @{ $held{$osm} }, $marker }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $osm = $inside->{$key})) { } 0 0 0
13098 0 else { push @none, $marker }
13099 }
13100  
13101 0 ▶ my $group = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
13102 0 my ($name, $in, %more) = @_;
13103 0 ▶ my @rows = map { @{ $_->{row_indexes} // [ $_->{first_row} ] } } @{$in};
Condition0%0/2
$_->{"row_indexes"} // [$_->{"first_row"}]
l!l
00
MC/DC0%0/1
$_->{"row_indexes"} // [$_->{"first_row"}]
$_->{"row_indexes"}
Truth table0%0/2
A
A= $_->{"row_indexes"}
Aresult
11
00
13104 return {
13105 name => $name,
13106 0 markers => scalar(@{$in}),
13107 # A geocoded marker is known by its place, another by where it is
13108 places => [ map {
13109 (defined($_->{value}) && !ref($_->{value}) && length($_->{value}))
13110 0 ▶ ? $_->{value} : "$_->{lat}, $_->{lon}"
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'value'} && !ref($_->{'value'}) && length $_->{'value'} ? : 0 0 0
Condition0%0/6
defined $_->{"value"} and not ref $_->{"value"} and length $_->{"value"}
!ll&&!rl&&r
000
defined $_->{"value"} and not ref $_->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_->{"value"} and not ref $_->{"value"} and length $_->{"value"}
defined $_->{"value"} not ref $_->{"value"} length $_->{"value"}
Truth table0%0/4
A and not(B) and C
A= defined $_->{"value"}B= not ref $_->{"value"}C= length $_->{"value"}
ABCresult
0XX0
10X0
1100
1111
13111 0 } @{$in} ],
13112 rows => scalar(@rows),
13113 filters => [ _map_rows_filters($records, \@rows, $columns) ],
13114 %more,
13115 };
13116 0 };
13117  
13118 0 ▶ my @shared = grep { @{ $held{ $_->{properties}{osm} // '' } // [] } > 1 } @{ $outlines->{features} };
Condition0%0/4
$held{$_->{"properties"}{"osm"} // ""} // []
l!l
00
$_->{"properties"}{"osm"} // ""
l!l
00
MC/DC0%0/1
$held{$_->{"properties"}{"osm"} // ""} // []
$held{$_->{"properties"}{"osm"} // ""}
MC/DC0%0/1
$_->{"properties"}{"osm"} // ""
$_->{"properties"}{"osm"}
Truth table0%0/2
A
A= $held{$_->{"properties"}{"osm"} // ""}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"properties"}{"osm"}
Aresult
11
00
13119 @shared = sort {
13120 0 @{ $held{ $b->{properties}{osm} } } <=> @{ $held{ $a->{properties}{osm} } }
13121 } @shared;
13122 0 ▶ splice(@shared, $MAP_OUTLINE_GROUPS_MAX) if @shared > $MAP_OUTLINE_GROUPS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if @shared > $MAP_OUTLINE_GROUPS_MAX 0 0 0
13123  
13124 return (
13125 0 ▶ (map { $group->($_->{properties}{name} // '', $held{ $_->{properties}{osm} }) } @shared),
Branch: 1 branch
BranchTrueFalseTotal
@none ? : 0 0 0
Condition0%0/2
$_->{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$_->{"properties"}{"name"} // ""
$_->{"properties"}{"name"}
Truth table0%0/2
A
A= $_->{"properties"}{"name"}
Aresult
11
00
13126 (@none ? $group->('', \@none, none => 1) : ()),
13127 );
13128 }
13129  
13130 # _map_rows_filters(\@records, \@row_indexes, \@columns) -> @filters
13131 #
13132 # Purpose: Filters that select exactly the given rows, for a link from the
13133 # map to them in the table.
13134 # Entry: Indexes into @records; the columns that tell the rows apart (the
13135 # place columns, or latitude and longitude).
13136 # Exit: One "col:in:[...]" filter a column, listing the cells those rows
13137 # hold. () when together they would select another row too (two
13138 # columns' lists can pair up into a row that was not asked for), or
13139 # when no row or column is given: a link must not show the wrong rows.
13140 # Side effects: none.
13141 0 ▶sub _map_rows_filters {
Subroutine _map_rows_filters: not called CC=7 SCAR=40.3
13142 0 my ($records, $indexes, $columns) = @_;
13143  
13144 0 my @rows = grep { ref($_) eq 'HASH' } map { $records->[$_] } @{$indexes};
13145 0 ▶ return () unless @rows && @{$columns};
Branch: 1 branch
BranchTrueFalseTotal
unless @rows and @{$columns;} 0 0 0
Condition0%0/3
@rows and @{$columns;}
!ll&&!rl&&r
000
MC/DC0%0/2
@rows and @{$columns;}
@rows @{$columns;}
Truth table0%0/3
A and B
A= @rowsB= @{$columns;}
ABresult
0X0
100
111
13146  
13147 my @filters = map {
13148 0 my $col = $_;
13149 0 ▶ my %cells = map { my $cell = $_->{$col}; ((defined($cell) && !ref($cell)) ? $cell : '') => 1 } @rows;
Branch: 1 branch
BranchTrueFalseTotal
defined $cell && !ref($cell) ? : 0 0 0
Condition0%0/3
defined $cell and not ref $cell
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cell and not ref $cell
defined $cell not ref $cell
Truth table0%0/3
A and not(B)
A= defined $cellB= not ref $cell
ABresult
0X0
100
111
13150 0 "$col:in:" . to_json([ sort keys %cells ]);
13151 0 } @{$columns};
13152  
13153 # Tried on every row: the filters are kept only if they find just these
13154 0 my $hit = [ grep { ref($_) eq 'HASH' } @{$records} ];
13155 0 $hit = _apply_filter_spec($hit, $_) for @filters;
13156 0 ▶ return scalar(@{$hit}) == scalar(@rows) ? @filters : ();
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$hit;} == scalar @rows ? : 0 0 0
13157 }
13158  
13159 # _map_unplotted(\@records, \@places, $loc_col, \@loc_cols, \%resolved)
13160 # -> (\@entries, \@all_filters)
13161 #
13162 # The places a geocoded map could not plot, so that the page can say which
13163 # they are: one entry for each, in the order of the first row that names it,
13164 # as { place, rows, reason, filters }. reason is 'empty' (no place in the
13165 # row), 'refused' (not a place name: a bare number), 'not_found', or 'later'
13166 # (not looked up yet: past a cap, or waiting for OpenStreetMap). filters are
13167 # f= specs that select the place's rows in the table, built from the cells of
13168 # its first row: one for each place column, eq that cell, or empty.
13169 #
13170 # @all_filters select every unplotted row at once: for each place column, in
13171 # the list of its cells in those rows. Columns filtered apart can pair a cell
13172 # of one row with a cell of another, which may name a place that was plotted,
13173 # so the filters are tried on the rows and kept only when they select exactly
13174 # the unplotted ones; otherwise @all_filters is empty.
13175 9 ▶sub _map_unplotted {
Subroutine _map_unplotted: called CC=27 SCAR=33.1
13176 9 my ($records, $places, $loc_col, $loc_cols, $resolved) = @_;
13177  
13178 9 my %reason;
13179 9 ▶ for my $value (@{ $resolved->{skipped} // [] }) {
Condition100%2/2
$resolved->{"skipped"} // []
l!l
63
MC/DC100%1/1
$resolved->{"skipped"} // []
$resolved->{"skipped"}
Truth table100%2/2
A
A= $resolved->{"skipped"}
Aresult
11
00
13180 5 ▶ my $key = (defined($value) && !ref($value)) ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value && !ref($value) ? : 5 0 5
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
005
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
13181 5 ▶ $reason{$key} = length($key) ? 'refused' : 'empty';
Branch: 1 branch
BranchTrueFalseTotal
length $key ? : 1 4 5
13182 }
13183 9 ▶ $reason{$_} = 'not_found' for grep { defined } @{ $resolved->{unresolved} // [] };
Condition100%2/2
$resolved->{"unresolved"} // []
l!l
81
MC/DC100%1/1
$resolved->{"unresolved"} // []
$resolved->{"unresolved"}
Truth table100%2/2
A
A= $resolved->{"unresolved"}
Aresult
11
00
13184 9 ▶ $reason{$_} = 'later' for grep { defined } @{ $resolved->{not_attempted} // [] };
Condition100%2/2
$resolved->{"not_attempted"} // []
l!l
63
MC/DC100%1/1
$resolved->{"not_attempted"} // []
$resolved->{"not_attempted"}
Truth table100%2/2
A
A= $resolved->{"not_attempted"}
Aresult
11
00
13185  
13186 9 my (%rows, %first, %cells);
13187 9 my $total = 0;
13188 9 for my $index (0 .. $#{$places}) {
13189 26 my $place = $places->[$index];
13190 26 ▶ my $value = ref($place) eq 'HASH' ? $place->{$loc_col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $place eq 'HASH' ? : 25 1 26
13191 26 ▶ my $key = (defined($value) && !ref($value)) ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value && !ref($value) ? : 25 1 26
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
1025
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
13192 26 ▶ next unless exists $reason{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $reason{$key} 9 17 26
13193 17 $rows{$key}++;
13194 17 $total++;
13195 17 ▶ $first{$key} //= $index;
Condition66%2/3
$first{$key} //= $index
l!l&&r!l&&!r
2150
MC/DC0%0/2
$first{$key} //= $index
$first{$key} $index
Truth table66%2/3
A //= B
A= $first{$key}B= $index
ABresult
1X1
011
000
13196  
13197 17 my $row = $records->[$index];
13198 17 for my $col (@{$loc_cols}) {
13199 24 ▶ my $cell = ref($row) eq 'HASH' ? $row->{$col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 23 1 24
13200 24 ▶ $cells{$col}{ (defined($cell) && !ref($cell)) ? $cell : '' } = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cell && !ref($cell) ? : 23 1 24
Condition66%2/3
defined $cell and not ref $cell
!ll&&!rl&&r
1023
MC/DC50%1/2
defined $cell and not ref $cell
defined $cell not ref $cell
Truth table66%2/3
A and not(B)
A= defined $cellB= not ref $cell
ABresult
0X0
100
111
13201 }
13202 }
13203  
13204 9 my @all_filters;
13205 9 ▶ if ($total) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total) 7 2 9
13206 7 @all_filters = map { "$_:in:" . to_json([ sort keys %{ $cells{$_} } ]) } @{$loc_cols};
13207 # Only rows can be filtered; one that is not is never selected.
13208 7 my $hit = [ grep { ref($_) eq 'HASH' } @{$records} ];
13209 7 $hit = _apply_filter_spec($hit, $_) for @all_filters;
13210 7 ▶ @all_filters = () unless scalar(@{$hit}) == $total;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$hit;} == $total 2 5 7
13211 }
13212  
13213 my @entries = map {
13214 15 my $row = $records->[ $first{$_} ];
13215 15 ▶ $row = {} unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 1 14 15
13216 +{
13217 place => $_,
13218 rows => $rows{$_},
13219 reason => $reason{$_},
13220 filters => [ map {
13221 21 my $cell = $row->{$_};
13222 21 ▶ (defined($cell) && !ref($cell) && length($cell)) ? "$_:eq:$cell" : "$_:empty";
Branch: 1 branch
BranchTrueFalseTotal
defined $cell && !ref($cell) && length $cell ? : 15 6 21
Condition83%5/6
defined $cell and not ref $cell and length $cell
!ll&&!rl&&r
1515
defined $cell and not ref $cell
!ll&&!rl&&r
1020
MC/DC66%2/3
defined $cell and not ref $cell and length $cell
defined $cell not ref $cell length $cell
Truth table75%3/4
A and not(B) and C
A= defined $cellB= not ref $cellC= length $cell
ABCresult
0XX0
10X0
1100
1111
13223 15 } @{$loc_cols} ],
13224 };
13225 9 } sort { $first{$a} <=> $first{$b} } keys %first;
13226  
13227 9 return (\@entries, \@all_filters);
13228 }
13229  
13230 # _map_region($config) -> $region or undef
13231 #
13232 # The country hint for the local geocoder: config map_region when it is two
13233 # letters, else none.
13234 17 ▶sub _map_region {
Subroutine _map_region: called CC=4 SCAR=13.9
13235 17 my $cfg = shift;
13236 17 my $region = $cfg->{map_region};
13237  
13238 17 ▶ return (defined($region) && !ref($region) && $region =~ /\A[A-Za-z]{2}\z/) ? $region : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $region && !ref($region) && $region =~ /\A[A-Za-z]{2}\z/u ? : 1 16 17
Condition100%6/6
defined $region and not ref $region and $region =~ /\A[A-Za-z]{2}\z/u
!ll&&!rl&&r
1151
defined $region and not ref $region
!ll&&!rl&&r
1016
MC/DC100%3/3
defined $region and not ref $region and $region =~ /\A[A-Za-z]{2}\z/u
defined $region not ref $region $region =~ /\A[A-Za-z]{2}\z/u
Truth table100%4/4
A and not(B) and C
A= defined $regionB= not ref $regionC= $region =~ /\A[A-Za-z]{2}\z/u
ABCresult
0XX0
10X0
1100
1111
13239 }
13240  
13241 # _map_geocoder($self, $online) -> Database::BI::Model::Geocode
13242 #
13243 # The geocoder a map asks, built from the config the same way for the map page,
13244 # a canvas card and /geo/lookup. The geocoder is a config seam first and a
13245 # recommended dependency second: a deployment may hand in an object, and a test
13246 # does exactly that. With neither, the model reports the module as unavailable
13247 # and the map is still the map.
13248 #
13249 # map_geocode_online => 0 keeps every lookup on this machine: a place the local
13250 # data does not know is then not sent to OpenStreetMap. A false $online does
13251 # the same for one request (the progress run, once Nominatim has failed).
13252 # Geo::Coder::List keeps its answers in files, so a place Nominatim found is not
13253 # asked again by another request, worker or restart. Geocode->new chooses the
13254 # directory (Object::Configure, CACHEDIR, CACHE_DIR, cache_dir in this config,
13255 # then <model_dir>/geocode-cache). A model_dir that is refused (inside
13256 # data_dir) gives no default, and with nothing else set the module falls back
13257 # on a cache in this process's memory.
13258 13 ▶sub _map_geocoder :Protected ($self, $online) {
Subroutine _map_geocoder: called CC=10 SCAR=23.6
13259 13 require Database::BI::Model::Geocode;
13260  
13261 13 my $cfg = $self->app->config;
13262 13 my $models_path = $self->_calc_models_path;
13263 13 my $injected = $cfg->{geocoder};
13264  
13265 return Database::BI::Model::Geocode->new(
13266 (defined($models_path)
13267 ? (model_dir => Mojo::File->new($models_path)->dirname->to_string)
13268 : ()),
13269 config => $cfg,
13270 home => $self->app->home->to_string,
13271 (defined($injected) && ref($injected) && $injected->can('geocode_many')
13272 ? (geocoder => $injected) : ()),
13273 (defined($cfg->{openaddr}) && length($cfg->{openaddr})
13274 ? (openaddr => $cfg->{openaddr}) : ()),
13275 online => (($online && ($cfg->{map_geocode_online} // 1)) ? 1 : 0),
13276 13 ▶ online_max => _map_limit($cfg->{map_geocode_online_max}, $DEFAULT_MAP_GEOCODE_ONLINE_MAX),
Branch: 4 branches
BranchTrueFalseTotal
defined $models_path ? : 13 0 13
defined $injected && ref $injected && $injected->can('geocode_many') ? : 10 3 13
defined $cfg->{'openaddr'} && length $cfg->{'openaddr'} ? : 0 13 13
$online && ($cfg->{'map_geocode_online'} // 1) ? : 9 4 13
Condition78%11/14
defined $injected and ref $injected and $injected->can("geocode_many")
!ll&&!rl&&r
3010
defined $injected and ref $injected
!ll&&!rl&&r
1210
defined $cfg->{"openaddr"} and length $cfg->{"openaddr"}
!ll&&!rl&&r
1300
$online and $cfg->{"map_geocode_online"} // 1
!ll&&!rl&&r
139
$cfg->{"map_geocode_online"} // 1
l!l
39
MC/DC66%2/3
defined $injected and ref $injected and $injected->can("geocode_many")
defined $injected ref $injected $injected->can("geocode_many")
MC/DC0%0/2
defined $cfg->{"openaddr"} and length $cfg->{"openaddr"}
defined $cfg->{"openaddr"} length $cfg->{"openaddr"}
MC/DC100%2/2
$online and $cfg->{"map_geocode_online"} // 1
$online $cfg->{"map_geocode_online"}
Truth table75%3/4
A and B and C
A= defined $injectedB= ref $injectedC= $injected->can("geocode_many")
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A and B
A= defined $cfg->{"openaddr"}B= length $cfg->{"openaddr"}
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $onlineB= $cfg->{"map_geocode_online"}
ABresult
0X0
100
111
13277 );
13278 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13279  
13280 # _map_boundary($self, $level, $online) -> Database::BI::Model::Boundary
13281 #
13282 # What finds the outline around each marker, for the map and /geo/boundary
13283 # alike. It asks OpenStreetMap only when the operator allows the geocoder to
13284 # (map_geocode_online), and at most map_geocode_online_max times a request.
13285 # Its answers are kept beside the geocode cache, under model_dir, so they
13286 # outlive the process and are shared by every worker; with no model_dir it
13287 # keeps them in memory. Config map_bounds_ranks adds or changes the place rank
13288 # that is a county in a country (checked at startup).
13289 0 ▶sub _map_boundary :Protected ($self, $level, $online) {
Subroutine _map_boundary: not called CC=7 SCAR=40.3
13290 0 require Database::BI::Model::Boundary;
13291  
13292 0 my $cfg = $self->app->config;
13293 0 my $models_path = $self->_calc_models_path;
13294 0 my $cache;
13295 0 ▶ if (defined $models_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $models_path) 0 0 0
13296 0 my $dir = Mojo::File->new($models_path)->dirname->to_string;
13297 0 $cache = eval { Database::BI::Model::Boundary->file_cache($dir) };
13298 }
13299  
13300 return Database::BI::Model::Boundary->new(
13301 level => $level,
13302 online => (($online && ($cfg->{map_geocode_online} // 1)) ? 1 : 0),
13303 online_max => _map_limit($cfg->{map_geocode_online_max}, $DEFAULT_MAP_GEOCODE_ONLINE_MAX),
13304 (defined($cache) ? (cache => $cache) : ()),
13305 0 ▶ (ref($cfg->{map_bounds_ranks}) eq 'HASH' ? (ranks => $cfg->{map_bounds_ranks}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$online && ($cfg->{'map_geocode_online'} // 1) ? : 0 0 0
defined $cache ? : 0 0 0
ref $cfg->{'map_bounds_ranks'} eq 'HASH' ? : 0 0 0
Condition0%0/5
$online and $cfg->{"map_geocode_online"} // 1
!ll&&!rl&&r
000
$cfg->{"map_geocode_online"} // 1
l!l
00
MC/DC0%0/2
$online and $cfg->{"map_geocode_online"} // 1
$online $cfg->{"map_geocode_online"}
Truth table0%0/3
A and B
A= $onlineB= $cfg->{"map_geocode_online"}
ABresult
0X0
100
111
13306 );
13307 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13308  
13309 # The colours of the outlines on a heat map, low to high: green, amber,
13310 # red, as the top of the heat layer's own gradient (lime, yellow, red). An
13311 # outline's colour is one of $OUTLINE_STEPS steps between them, so outlines of
13312 # about the same total share a layer.
13313 5 Readonly my @OUTLINE_STOPS => ([ 0x1a, 0x98, 0x50 ], [ 0xf5, 0xa6, 0x23 ], [ 0xd7, 0x30, 0x27 ]);
13314 5 Readonly my $OUTLINE_STEPS => 9;
13315 # How an outline is drawn: thin and grey on a plain map, heavier when its
13316 # colour carries a meaning, so the colour can be told apart on a busy map.
13317 5 Readonly my %OUTLINE_PLAIN => (colour => '#555555', weight => 1.5, opacity => 0.8);
13318 5 Readonly my %OUTLINE_HEAT => (weight => 2.5, opacity => 0.9);
13319 # Colour shade (shade=): each outline filled in a shade of one colour, darker
13320 # for a larger total, as a census map is. $SHADE_STEPS shades, the lightest
13321 # $SHADE_LIGHTEST of the way from white to the colour so that it still shows;
13322 # the fill lets the map underneath through.
13323 5 Readonly my $SHADE_STEPS => 5;
13324 5 Readonly my $SHADE_LIGHTEST => 0.15;
13325 5 Readonly my $SHADE_OPACITY => 0.65;
13326 5 Readonly my $DEFAULT_SHADE_COLOR => '#d94801';
13327 # A key's totals are rounded to $KEY_FIGURES significant figures, from
13328 # $KEY_ROUND_FROM up; smaller ones are short enough as they are.
13329 5 Readonly my $KEY_FIGURES => 2;
13330 5 Readonly my $KEY_ROUND_FROM => 100;
13331 # The HTML::OSM whose marker key can leave an entry out while a layer is shown
13332 # (a legend entry's unless_layer).
13333 5 Readonly my $OSM_KEY_WAITS_VERSION => '0.19';
13334  
13335 # _map_outline_layers($self, $outlines, \@markers, $records, $weight_col)
13336 # -> list of { features, colour, weight, opacity }
13337 #
13338 # The county outlines as layers for add_geojson. Without the heat layer
13339 # ($weight_col undef) they are one grey layer. With it, each outline is
13340 # coloured by the total of what is in it: the weight column summed over every
13341 # row of every marker inside it (a cell that is not a number adds nothing), or,
13342 # with no weight column (''), its rows counted. _outline_shares places each
13343 # total between green and red. A lone outline, or totals all alike, stays
13344 # grey with no key, having nothing to be compared with; $noun is what an
13345 # outline is called there, for the key's title. The layers go from low to high, so where two
13346 # outlines share a border the higher is drawn on top; the first carries the
13347 # key to the colours, high to low with the totals each covers. Each popup names the
13348 # outline and its total. Leaflet reads a popup as HTML, and a name from
13349 # OpenStreetMap is text, so it is escaped.
13350 3 ▶sub _map_outline_layers :Protected ($self, $outlines, $markers, $records, $weight_col, $noun) {
Subroutine _map_outline_layers: called CC=13 SCAR=50.7
13351 0 ▶ return () unless $outlines && @{ $outlines->{features} };
Branch: 1 branch
BranchTrueFalseTotal
unless $outlines and @{$outlines->{"features"};} 3 0 3
Condition33%1/3
$outlines and @{$outlines->{"features"};}
!ll&&!rl&&r
300
MC/DC0%0/2
$outlines and @{$outlines->{"features"};}
$outlines @{$outlines->{"features"};}
Truth table33%1/3
A and B
A= $outlinesB= @{$outlines->{"features"};}
ABresult
0X0
100
111
13352  
13353 0 my @features = @{ $outlines->{features} };
13354 0 ▶ my $named = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
13355 0 my ($feature, $text) = @_;
13356 # osm stays, so the fill (shade=) can find the outline's total
13357 0 return { %{$feature}, properties => { name => xml_escape($text), osm => $feature->{properties}{osm} } };
13358 0 };
13359  
13360 # No heat layer: nothing to total, so one grey layer and no key.
13361 0 ▶ unless (defined $weight_col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $weight_col) 0 0 0
13362 return {
13363 0 ▶ features => [ map { $named->($_, $_->{properties}{name} // '') } @features ],
Condition0%0/2
$_->{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$_->{"properties"}{"name"} // ""
$_->{"properties"}{"name"}
Truth table0%0/2
A
A= $_->{"properties"}{"name"}
Aresult
11
00
13364 %OUTLINE_PLAIN,
13365 };
13366 }
13367  
13368 0 ▶ my $total = $self->_map_outline_totals($outlines->{inside} // {}, $markers, $records, $weight_col);
Condition0%0/2
$outlines->{"inside"} // {}
l!l
00
MC/DC0%0/1
$outlines->{"inside"} // {}
$outlines->{"inside"}
Truth table0%0/2
A
A= $outlines->{"inside"}
Aresult
11
00
13369 0 ▶ my @totals = map { $total->{ $_->{properties}{osm} } // 0 } @features;
Condition0%0/2
$$total{$_->{"properties"}{"osm"}} // 0
l!l
00
MC/DC0%0/1
$$total{$_->{"properties"}{"osm"}} // 0
$$total{$_->{"properties"}{"osm"}}
Truth table0%0/2
A
A= $$total{$_->{"properties"}{"osm"}}
Aresult
11
00
13370  
13371 0 my %seen;
13372 0 my @distinct = grep { !$seen{$_}++ } sort { $a <=> $b } @totals;
13373  
13374 # One outline, or totals all alike: there is nothing to compare, so a
13375 # colour would mean nothing. Grey, with no key, but the popups still
13376 # give the totals.
13377 0 ▶ if (@distinct < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@distinct < 2) 0 0 0
13378 return {
13379 features => [ map {
13380 0 $named->($features[$_], $self->_map_outline_popup(
13381 0 ▶ $features[$_]{properties}{name} // '', $totals[$_], $weight_col))
Condition0%0/2
$features[$_]{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$features[$_]{"properties"}{"name"} // ""
$features[$_]{"properties"}{"name"}
Truth table0%0/2
A
A= $features[$_]{"properties"}{"name"}
Aresult
11
00
13382 } 0 .. $#features ],
13383 %OUTLINE_PLAIN,
13384 };
13385 }
13386 0 my $share_of = _outline_shares(\@distinct);
13387  
13388 # Lowest first: a layer is made the first time its step is met, so the
13389 # layers come out in the order they must be drawn.
13390 0 my (%layer_of, @layers);
13391 0 for my $i (sort { $totals[$a] <=> $totals[$b] } 0 .. $#features) {
13392 0 my $share = $share_of->{ $totals[$i] };
13393 0 my $step = int($share * ($OUTLINE_STEPS - 1) + 0.5);
13394 0 ▶ my $layer = $layer_of{$step} //= do {
Condition0%0/3
$layer_of{$step} //= do { push @layers, {"features", [], "totals", [], "colour", _outline_colour($step / ($OUTLINE_STEPS - 1)), %OUTLINE_HEAT}; $layers[-1] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$layer_of{$step} //= do { push @layers, {"features", [], "totals", [], "colour", _outline_colour($step / ($OUTLINE_STEPS - 1)), %OUTLINE_HEAT}; $layers[-1] }
$layer_of{$step} do { push @layers, {"features", [], "totals", [], "colour", _outline_colour($step / ($OUTLINE_STEPS - 1)), %OUTLINE_HEAT}; $layers[-1] }
Truth table0%0/3
A //= B
A= $layer_of{$step}B= do { push @layers, {"features", [], "totals", [], "colour", _outline_colour($step / ($OUTLINE_STEPS - 1)), %OUTLINE_HEAT}; $layers[-1] }
ABresult
1X1
011
000
13395 0 push @layers, { features => [], totals => [],
13396 colour => _outline_colour($step / ($OUTLINE_STEPS - 1)), %OUTLINE_HEAT };
13397 0 $layers[-1];
13398 };
13399 # Sorted already, so each layer's totals are low to high for the key.
13400 0 push @{ $layer->{totals} }, $totals[$i];
13401 0 my $feature = $features[$i];
13402 0 ▶ push @{ $layer->{features} }, $named->($feature,
Condition0%0/2
$feature->{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$feature->{"properties"}{"name"} // ""
$feature->{"properties"}{"name"}
Truth table0%0/2
A
A= $feature->{"properties"}{"name"}
Aresult
11
00
13403 0 $self->_map_outline_popup($feature->{properties}{name} // '', $totals[$i], $weight_col));
13404 # Drawn inside its own edge (HTML::OSM 0.17's bands), so a neighbour of
13405 # another colour is beside it, not over it.
13406 0 $layer->{features}[-1]{properties}{colours} = [ $layer->{colour} ];
13407 0 $layer->{bands} = 1;
13408 }
13409  
13410 # HTML::OSM 0.16 shows one key a layer name and hides it with the layer;
13411 # an older copy ignores it. The totals were only for the key.
13412 0 $layers[0]{key} = $self->_map_outline_key(\@layers, $weight_col, $noun);
13413 0 delete $_->{totals} for @layers;
13414 0 return @layers;
13415 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13416  
13417 # _map_outline_marker_layers($self, $outlines, \@markers)
13418 # -> list of { features, colour, weight, opacity, bands }
13419 #
13420 # Purpose: The outlines coloured as the markers in them, for a map whose
13421 # markers are coloured by a column.
13422 # Entry: $outlines as Boundary's outlines() returns it, or undef; each
13423 # coloured marker has lat, lon and colour (#rgb or #rrggbb).
13424 # Exit: One layer for each distinct list of colours, the outlines of one
13425 # colour first. An outline whose markers differ lists every colour,
13426 # the one most of its markers have first (the earliest on a tie), in
13427 # its feature's "colours" property. A coloured layer has bands set:
13428 # HTML::OSM 0.17 then draws each outline just inside its own edge, a
13429 # line of each colour side by side, the first outermost, so
13430 # neighbours of different colours are beside each other; "colour"
13431 # (the first) is what an older HTML::OSM draws, along the edge. () when there is nothing to draw or no marker has a
13432 # colour, so the caller can fall back.
13433 # Side effects: none.
13434 0 ▶sub _map_outline_marker_layers :Protected ($self, $outlines, $markers) {
Subroutine _map_outline_marker_layers: not called CC=19 SCAR=59.4
13435 0 ▶ return () unless $outlines && @{ $outlines->{features} };
Branch: 1 branch
BranchTrueFalseTotal
unless $outlines and @{$outlines->{"features"};} 0 0 0
Condition0%0/3
$outlines and @{$outlines->{"features"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$outlines and @{$outlines->{"features"};}
$outlines @{$outlines->{"features"};}
Truth table0%0/3
A and B
A= $outlinesB= @{$outlines->{"features"};}
ABresult
0X0
100
111
13436  
13437 0 require Database::BI::Model::Boundary;
13438 0 ▶ my $inside = $outlines->{inside} // {};
Condition0%0/2
$outlines->{"inside"} // {}
l!l
00
MC/DC0%0/1
$outlines->{"inside"} // {}
$outlines->{"inside"}
Truth table0%0/2
A
A= $outlines->{"inside"}
Aresult
11
00
13439 # How many markers of each colour are in each outline, in the order the
13440 # colours were first met there.
13441 0 my (%count, %order);
13442 0 for my $marker (@{$markers}) {
13443 0 ▶ my $colour = $marker->{colour} // next;
Condition0%0/2
$marker->{"colour"} // (next)
l!l
00
MC/DC0%0/1
$marker->{"colour"} // (next)
$marker->{"colour"}
Truth table0%0/2
A
A= $marker->{"colour"}
Aresult
11
00
13444 # The script takes #rrggbb only, so #rgb is written out in full
13445 0 $colour =~ s/\A#(.)(.)(.)\z/#$1$1$2$2$3$3/;
13446 0 my $key = Database::BI::Model::Boundary->point_key($marker->{lat}, $marker->{lon});
13447 0 ▶ my $osm = defined($key) ? $inside->{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
13448 0 ▶ next unless defined $osm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $osm 0 0 0
13449 0 ▶ push @{ $order{$osm} }, $colour unless $count{$osm}{$colour}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $count{$osm}{$colour}++ 0 0 0
13450 }
13451 0 ▶ return () unless %count;
Branch: 1 branch
BranchTrueFalseTotal
unless %count 0 0 0
13452  
13453 0 my (%layer_of, @layers);
13454 0 for my $feature (@{ $outlines->{features} }) {
13455 0 ▶ my $osm = $feature->{properties}{osm} // '';
Condition0%0/2
$feature->{"properties"}{"osm"} // ""
l!l
00
MC/DC0%0/1
$feature->{"properties"}{"osm"} // ""
$feature->{"properties"}{"osm"}
Truth table0%0/2
A
A= $feature->{"properties"}{"osm"}
Aresult
11
00
13456 0 ▶ my $seen = $order{$osm} // [];
Condition0%0/2
$order{$osm} // []
l!l
00
MC/DC0%0/1
$order{$osm} // []
$order{$osm}
Truth table0%0/2
A
A= $order{$osm}
Aresult
11
00
13457 0 my %place = map { $seen->[$_] => $_ } 0 .. $#{$seen};
13458 0 ▶ my @colours = sort { $count{$osm}{$b} <=> $count{$osm}{$a} || $place{$a} <=> $place{$b} } @{$seen};
Condition0%0/3
$count{$osm}{$b} <=> $count{$osm}{$a} || $place{$a} <=> $place{$b}
l!l&&r!l&&!r
000
MC/DC0%0/2
$count{$osm}{$b} <=> $count{$osm}{$a} || $place{$a} <=> $place{$b}
$count{$osm}{$b} <=> $count{$osm}{$a} $place{$a} <=> $place{$b}
Truth table0%0/3
A || B
A= $count{$osm}{$b} <=> $count{$osm}{$a}B= $place{$a} <=> $place{$b}
ABresult
1X1
011
000
13459 # An outline none of whose markers is coloured stays grey
13460 0 ▶ my $style = @colours ? { colour => $colours[0], %OUTLINE_HEAT } : { %OUTLINE_PLAIN };
Branch: 1 branch
BranchTrueFalseTotal
@colours ? : 0 0 0
13461 0 ▶ my $layer = $layer_of{"@colours"} //= do {
Condition0%0/3
$layer_of{join $", @colours} //= do { push @layers, {"features", [], %{$style;}, "bands", @colours ? 1 : 0, "mixed", @colours > 1 ? 1 : 0}; $layers[-1] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$layer_of{join $", @colours} //= do { push @layers, {"features", [], %{$style;}, "bands", @colours ? 1 : 0, "mixed", @colours > 1 ? 1 : 0}; $layers[-1] }
$layer_of{join $", @colours} do { push @layers, {"features", [], %{$style;}, "bands", @colours ? 1 : 0, "mixed", @colours > 1 ? 1 : 0}; $layers[-1] }
Truth table0%0/3
A //= B
A= $layer_of{join $", @colours}B= do { push @layers, {"features", [], %{$style;}, "bands", @colours ? 1 : 0, "mixed", @colours > 1 ? 1 : 0}; $layers[-1] }
ABresult
1X1
011
000
13462 0 ▶ push @layers, { features => [], %{$style}, bands => (@colours ? 1 : 0), mixed => (@colours > 1 ? 1 : 0) };
Branch: 2 branches
BranchTrueFalseTotal
@colours ? : 0 0 0
@colours > 1 ? : 0 0 0
13463 0 $layers[-1];
13464 };
13465 # Leaflet reads a popup as HTML, and a name from OpenStreetMap is text
13466 0 push @{ $layer->{features} }, {
13467 0 ▶ %{$feature},
Condition0%0/2
$feature->{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$feature->{"properties"}{"name"} // ""
$feature->{"properties"}{"name"}
Truth table0%0/2
A
A= $feature->{"properties"}{"name"}
Aresult
11
00
13468 0 properties => { name => xml_escape($feature->{properties}{name} // ''), colours => \@colours, osm => $osm },
13469 };
13470 }
13471 # Those of several colours last: only an older HTML::OSM, which draws every
13472 # outline along its edge, can draw one over another, and then they show.
13473 0 return sort { $a->{mixed} <=> $b->{mixed} } @layers;
13474 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13475  
13476 # _map_outline_shade($self, \@layers, $outlines, \@markers, $records, $col, $colour, $noun, $lower)
13477 # -> list of layers, each with fill
13478 #
13479 # Purpose: Colour shade: fill each outline in a shade of a colour, by the
13480 # total of $col in it.
13481 # Entry: @layers as _map_outline_layers or _map_outline_marker_layers made
13482 # them (each feature keeps its osm and an escaped name); $col is a
13483 # column of $records. $colour is the one colour to shade, as
13484 # _parse_color takes one, or undef for the default. $lower, when a
13485 # hashref, says the markers are coloured and no colour was asked for:
13486 # each outline is then shaded in its own line colour (its markers'),
13487 # and $lower->{<#rrggbb>} is true for a colour whose rule is "less
13488 # than", where the smaller total is the stronger.
13489 # Exit: Each layer split by shade, so every part has one line style and
13490 # one "fill" (#rrggbb), in the order given. One colour: darker for a
13491 # larger total, across every outline. The markers' colours: each
13492 # colour is a scale of its own over the outlines of that colour,
13493 # stronger the further the total is into what the colour stands for
13494 # (larger, or smaller for a "less than" colour), so a county well
13495 # under a "below 20,000" rule is greener than one just under it; a
13496 # colour with one total is its middle shade. The shade follows
13497 # _outline_shares, as the heat colours do. Each popup names the
13498 # outline and its total; the first layer carries the key, every
13499 # shade in use by size, the largest totals first, drawn as boxes (HTML::OSM 0.18; an older copy
13500 # draws lines). () with one colour when fewer than two totals
13501 # differ: nothing to compare, so the caller leaves the outlines
13502 # unfilled.
13503 # Side effects: none.
13504 0 ▶sub _map_outline_shade :Protected ($self, $layers, $outlines, $markers, $records, $col, $colour, $noun, $lower) {
Subroutine _map_outline_shade: not called CC=21 SCAR=61.4
13505 0 ▶ return () unless $outlines && @{$layers};
Branch: 1 branch
BranchTrueFalseTotal
unless $outlines and @{$layers;} 0 0 0
Condition0%0/3
$outlines and @{$layers;}
!ll&&!rl&&r
000
MC/DC0%0/2
$outlines and @{$layers;}
$outlines @{$layers;}
Truth table0%0/3
A and B
A= $outlinesB= @{$layers;}
ABresult
0X0
100
111
13506  
13507 0 ▶ my $total = $self->_map_outline_totals($outlines->{inside} // {}, $markers, $records, $col);
Condition0%0/2
$outlines->{"inside"} // {}
l!l
00
MC/DC0%0/1
$outlines->{"inside"} // {}
$outlines->{"inside"}
Truth table0%0/2
A
A= $outlines->{"inside"}
Aresult
11
00
13508 0 ▶ my $amount = sub { return $total->{ $_[0]{properties}{osm} // '' } // 0 };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/4
$$total{$_[0]{"properties"}{"osm"} // ""} // 0
l!l
00
$_[0]{"properties"}{"osm"} // ""
l!l
00
MC/DC0%0/1
$$total{$_[0]{"properties"}{"osm"} // ""} // 0
$$total{$_[0]{"properties"}{"osm"} // ""}
MC/DC0%0/1
$_[0]{"properties"}{"osm"} // ""
$_[0]{"properties"}{"osm"}
Truth table0%0/2
A
A= $$total{$_[0]{"properties"}{"osm"} // ""}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{"properties"}{"osm"}
Aresult
11
00
13509 0 ▶ my $one = _color_hex(_parse_color($colour // '') // $DEFAULT_SHADE_COLOR);
Condition0%0/5
_parse_color($colour // "") // $DEFAULT_SHADE_COLOR
l!l&&r!l&&!r
000
$colour // ""
l!l
00
MC/DC0%0/2
_parse_color($colour // "") // $DEFAULT_SHADE_COLOR
_parse_color($colour // "") $DEFAULT_SHADE_COLOR
MC/DC0%0/1
$colour // ""
$colour
Truth table0%0/3
A // B
A= _parse_color($colour // "")B= $DEFAULT_SHADE_COLOR
ABresult
1X1
011
000
Truth table0%0/2
A
A= $colour
Aresult
11
00
13510  
13511 # The colour each layer is shaded in: the one colour, or its own line's.
13512 # Written out as #rrggbb, which _shade_colour reads.
13513 0 ▶ my $hue_of = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
13514 0 ▶ my $hue = $lower ? $_[0]{colour} : $one;
Branch: 1 branch
BranchTrueFalseTotal
$lower ? : 0 0 0
13515 0 $hue =~ s/\A#(.)(.)(.)\z/#$1$1$2$2$3$3/;
13516 0 return lc $hue;
13517 0 };
13518  
13519 # Each colour's totals, then where each sits on that colour's scale.
13520 0 my (%totals_of, @hues);
13521 0 for my $layer (@{$layers}) {
13522 0 my $hue = $hue_of->($layer);
13523 0 ▶ push @hues, $hue unless $totals_of{$hue};
Branch: 1 branch
BranchTrueFalseTotal
unless $totals_of{$hue} 0 0 0
13524 0 $totals_of{$hue}{ $amount->($_) } = 1 for @{ $layer->{features} };
13525 }
13526 0 ▶ return () if !$lower && keys(%{ $totals_of{ $hues[0] } }) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if not $lower and keys %{$totals_of{$hues[0]};} < 2 0 0 0
Condition0%0/3
not $lower and keys %{$totals_of{$hues[0]};} < 2
!ll&&!rl&&r
000
MC/DC0%0/2
not $lower and keys %{$totals_of{$hues[0]};} < 2
not $lower keys %{$totals_of{$hues[0]};} < 2
Truth table0%0/3
not(A) and B
A= not $lowerB= keys %{$totals_of{$hues[0]};} < 2
ABresult
0X0
100
111
13527 0 my %share_of;
13528 0 for my $hue (@hues) {
13529 0 my @distinct = sort { $a <=> $b } keys %{ $totals_of{$hue} };
13530 0 ▶ my $shares = @distinct > 1 ? _outline_shares(\@distinct) : { $distinct[0] => 0.5 };
Branch: 1 branch
BranchTrueFalseTotal
@distinct > 1 ? : 0 0 0
13531 # A "less than" colour is the stronger the smaller the total
13532 0 ▶ $shares = { map { $_ => 1 - $shares->{$_} } keys %{$shares} } if $lower && $lower->{$hue};
Branch: 1 branch
BranchTrueFalseTotal
if $lower and $lower->{$hue} 0 0 0
Condition0%0/3
$lower and $lower->{$hue}
!ll&&!rl&&r
000
MC/DC0%0/2
$lower and $lower->{$hue}
$lower $lower->{$hue}
Truth table0%0/3
A and B
A= $lowerB= $lower->{$hue}
ABresult
0X0
100
111
13533 0 $share_of{$hue} = $shares;
13534 }
13535  
13536 0 my (@shaded, %amounts);
13537 0 for my $layer (@{$layers}) {
13538 0 my $hue = $hue_of->($layer);
13539 0 my (%part, @steps);
13540 0 for my $feature (@{ $layer->{features} }) {
13541 0 my $sum = $amount->($feature);
13542 0 my $step = int($share_of{$hue}{$sum} * ($SHADE_STEPS - 1) + 0.5);
13543 0 ▶ push @steps, $step unless $part{$step};
Branch: 1 branch
BranchTrueFalseTotal
unless $part{$step} 0 0 0
13544 0 push @{ $amounts{$hue}{$step} }, $sum;
13545 # The name is escaped already; the column is text of the reader's
13546 0 push @{ $part{$step} }, {
13547 0 %{$feature},
13548 properties => {
13549 0 ▶ %{ $feature->{properties} },
Condition0%0/2
$feature->{"properties"}{"name"} // ""
l!l
00
MC/DC0%0/1
$feature->{"properties"}{"name"} // ""
$feature->{"properties"}{"name"}
Truth table0%0/2
A
A= $feature->{"properties"}{"name"}
Aresult
11
00
13550 name => $self->_i18n('map_bounds_total',
13551 0 $feature->{properties}{name} // '', _map_total_text($sum), xml_escape($col)),
13552 },
13553 };
13554 }
13555 push @shaded, map {
13556 0 +{ %{$layer}, features => $part{$_}, fill => _shade_colour($hue, $_ / ($SHADE_STEPS - 1)) }
13557 0 } sort { $a <=> $b } @steps;
13558 }
13559  
13560 # One key for the fills, in place of any the lines had: for each colour
13561 # the shades in use, strongest first as _map_outline_key lists them,
13562 # drawn as boxes.
13563 0 delete $_->{key} for @shaded;
13564 # They are listed by size whatever their colour, the largest totals at the
13565 # top, so a key of red above 100,000 and green below 20,000 runs from the
13566 # strongest red down to the strongest green.
13567 0 ▶ my @in_use = sort { $a->{totals}[-1] <=> $b->{totals}[-1] || $a->{totals}[0] <=> $b->{totals}[0] } map {
Condition0%0/3
$Database::BI::Controller::Dashboard::a->{'totals'}[-1] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[-1] || $Database::BI::Controller::Dashboard::a->{'totals'}[0] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Database::BI::Controller::Dashboard::a->{'totals'}[-1] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[-1] || $Database::BI::Controller::Dashboard::a->{'totals'}[0] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[0]
$Database::BI::Controller::Dashboard::a->{'totals'}[-1] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[-1] $Database::BI::Controller::Dashboard::a->{'totals'}[0] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[0]
Truth table0%0/3
A || B
A= $Database::BI::Controller::Dashboard::a->{'totals'}[-1] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[-1]B= $Database::BI::Controller::Dashboard::a->{'totals'}[0] <=> $Database::BI::Controller::Dashboard::b->{'totals'}[0]
ABresult
1X1
011
000
13568 0 my $hue = $_;
13569 map {
13570 +{ colour => _shade_colour($hue, $_ / ($SHADE_STEPS - 1)),
13571 0 totals => [ sort { $a <=> $b } @{ $amounts{$hue}{$_} } ] }
13572 0 } keys %{ $amounts{$hue} };
13573 } @hues;
13574 0 $shaded[0]{key} = { %{ $self->_map_outline_key(\@in_use, $col, $noun) }, shape => 'box' };
13575 0 return @shaded;
13576 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13577  
13578 # _shade_colour($hex, $share) -> '#rrggbb': $hex (#rrggbb) at its full
13579 # strength for a share of 1, down to $SHADE_LIGHTEST of the way from white for
13580 # a share of 0.
13581 0 ▶sub _shade_colour {
Subroutine _shade_colour: not called CC=3 SCAR=24.8
13582 0 my ($hex, $share) = @_;
13583 0 ▶ $share = 0 if $share < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $share < 0 0 0 0
13584 0 ▶ $share = 1 if $share > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $share > 1 0 0 0
13585 0 my $strength = $SHADE_LIGHTEST + (1 - $SHADE_LIGHTEST) * $share;
13586 return sprintf('#%02x%02x%02x',
13587 0 map { int(255 + (hex($_) - 255) * $strength + 0.5) } $hex =~ /\A#(..)(..)(..)\z/);
13588 }
13589  
13590 # _map_outline_totals($self, \%inside, \@markers, $records, $weight_col) -> \%total
13591 #
13592 # Purpose: What is in each outline, for its colour.
13593 # Entry: %inside maps a point key to the osm of the outline it is in (from
13594 # Boundary's outlines()); each marker has lat, lon and row_indexes
13595 # (or first_row); $weight_col is a column, or '' to count rows.
13596 # Exit: { osm => total }; an outline no marker is in has no entry.
13597 # Side effects: none. Rows are only read, so locked SQL rows are safe.
13598 0 ▶sub _map_outline_totals :Protected ($self, $inside, $markers, $records, $weight_col) {
Subroutine _map_outline_totals: not called CC=10 SCAR=47.0
13599 0 require Database::BI::Model::Boundary;
13600 0 my %total;
13601 0 for my $marker (@{$markers}) {
13602 0 my $key = Database::BI::Model::Boundary->point_key($marker->{lat}, $marker->{lon});
13603 0 ▶ my $osm = defined($key) ? $inside->{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
13604 0 ▶ next unless defined $osm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $osm 0 0 0
13605 # A marker stands for every row at its place, not only the first.
13606 0 ▶ for my $index (@{ $marker->{row_indexes} // [ $marker->{first_row} ] }) {
Condition0%0/2
$marker->{"row_indexes"} // [$marker->{"first_row"}]
l!l
00
MC/DC0%0/1
$marker->{"row_indexes"} // [$marker->{"first_row"}]
$marker->{"row_indexes"}
Truth table0%0/2
A
A= $marker->{"row_indexes"}
Aresult
11
00
13607 0 my $row = $records->[$index];
13608 0 ▶ next unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 0 0
13609 # A cell that is not a number adds nothing, as on the heat layer.
13610 0 ▶ $total{$osm} += length($weight_col) ? (_numeric_cell($row->{$weight_col} // '') // 0) : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $weight_col ? : 0 0 0
Condition0%0/4
_numeric_cell($row->{$weight_col} // "") // 0
l!l
00
$row->{$weight_col} // ""
l!l
00
MC/DC0%0/1
_numeric_cell($row->{$weight_col} // "") // 0
_numeric_cell($row->{$weight_col} // "")
MC/DC0%0/1
$row->{$weight_col} // ""
$row->{$weight_col}
Truth table0%0/2
A
A= _numeric_cell($row->{$weight_col} // "")
Aresult
11
00
Truth table0%0/2
A
A= $row->{$weight_col}
Aresult
11
00
13611 }
13612 }
13613 0 return \%total;
13614 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13615  
13616 # _map_outline_popup($self, $name, $total, $weight_col) -> $text
13617 #
13618 # Purpose: The text of a coloured outline's popup: its name and its total.
13619 # Entry: $weight_col is the column totalled, or '' when rows were counted.
13620 # Exit: Plain text (the caller escapes it); one row is "1 row", not "1 rows".
13621 # Side effects: none.
13622 0 ▶sub _map_outline_popup :Protected ($self, $name, $total, $weight_col) {
Subroutine _map_outline_popup: not called CC=3 SCAR=24.8
13623 0 ▶ return $self->_i18n('map_bounds_total', $name, _map_total_text($total), $weight_col) if length $weight_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $weight_col 0 0 0
13624 0 ▶ return $total == 1
Branch: 1 branch
BranchTrueFalseTotal
$total == 1 ? : 0 0 0
13625 ? $self->_i18n('map_bounds_row', $name)
13626 : $self->_i18n('map_bounds_rows', $name, _map_total_text($total));
13627 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13628  
13629 # _map_outline_key($self, \@layers, $weight_col) -> { title, entries }
13630 #
13631 # Purpose: The key to the outline colours, for add_geojson's key option.
13632 # Entry: Each layer has its colour and its totals, low to high; the layers
13633 # are low to high too, and there is at least one.
13634 # Exit: The entries are the highest colour first, each labelled with the
13635 # total it covers, or the range when it covers unequal totals.
13636 # Side effects: none.
13637 0 ▶sub _map_outline_key :Protected ($self, $layers, $weight_col, $noun) {
Subroutine _map_outline_key: not called CC=3 SCAR=24.8
13638 my @entries = map {
13639 0 my ($low, $high) = @{ $_->{totals} }[0, -1];
13640 +{
13641 colour => $_->{colour},
13642 # Round figures: a key is read at a glance, and the popups
13643 # give each outline's exact total.
13644 0 ▶ label => (_map_key_number($low) eq _map_key_number($high) ? _map_key_number($low)
Branch: 1 branch
BranchTrueFalseTotal
_map_key_number($low) eq _map_key_number($high) ? : 0 0 0
13645 : $self->_i18n('map_bounds_key_range', _map_key_number($low), _map_key_number($high))),
13646 };
13647 0 } reverse @{$layers};
13648 return {
13649 0 ▶ title => (length($weight_col)
Branch: 1 branch
BranchTrueFalseTotal
length $weight_col ? : 0 0 0
13650 ? $self->_i18n('map_bounds_key_total', $weight_col, $noun)
13651 : $self->_i18n('map_bounds_key_rows', $noun)),
13652 entries => \@entries,
13653 };
13654 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13655  
13656 # _outline_shares(\@distinct) -> { total => share }
13657 #
13658 # Purpose: Where each total sits between the lowest (0) and the highest (1),
13659 # which decides its colour.
13660 # Entry: At least two distinct totals, sorted low to high.
13661 # Exit: When every total is above zero, the share is by size on a log
13662 # scale: each step up the colours is the same multiple, so totals
13663 # close in size share a colour and totals far apart do not. A
13664 # straight scale would leave most outlines green, because such totals
13665 # are skewed (Fairfax County has 500 times the people of Highland).
13666 # A total of zero or less has no logarithm, so then the share is by
13667 # rank: evenly spread in order, whatever the sizes.
13668 # Side effects: none.
13669 0 ▶sub _outline_shares {
Subroutine _outline_shares: not called CC=2 SCAR=17.9
13670 0 my ($distinct) = @_;
13671 0 my %share;
13672 0 ▶ if ($distinct->[0] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distinct->[0] > 0) { } 0 0 0
13673 0 my ($low, $high) = (log $distinct->[0], log $distinct->[-1]);
13674 # The ends are set exactly, so rounding cannot push them off the scale.
13675 0 %share = map { $_ => (log($_) - $low) / ($high - $low) } @{$distinct};
13676 0 @share{ $distinct->[0], $distinct->[-1] } = (0, 1);
13677 } else {
13678 0 %share = map { $distinct->[$_] => $_ / $#{$distinct} } 0 .. $#{$distinct};
13679 }
13680 0 return \%share;
13681 }
13682  
13683 # _map_outline_noun($self, $outlines, $level) -> $word
13684 #
13685 # Purpose: What the outlines drawn are called ("county", "district"), for the
13686 # layer's checkbox, the key, the summary line and the notes about
13687 # outlines not had yet.
13688 # Entry: $outlines as Boundary's outlines() returns it, or undef; $level
13689 # the bounds= level they were asked at, or undef.
13690 # Exit: The word for the one kind drawn; "area" when the map mixes kinds
13691 # (a county and a province); what the level is called by default
13692 # ("state", "town") when nothing is drawn, since no country is known
13693 # then; "county" with no level.
13694 # Side effects: none.
13695 8 ▶sub _map_outline_noun :Protected ($self, $outlines, $level = undef) {
Subroutine _map_outline_noun: called CC=7 SCAR=23.8
13696 8 require Database::BI::Model::Boundary;
13697 8 ▶ my $usual = Database::BI::Model::Boundary->level_name($level) // 'county';
Condition50%1/2
"Database::BI::Model::Boundary"->level_name($level) // "county"
l!l
08
MC/DC0%0/1
"Database::BI::Model::Boundary"->level_name($level) // "county"
"Database::BI::Model::Boundary"->level_name($level)
Truth table50%1/2
A
A= "Database::BI::Model::Boundary"->level_name($level)
Aresult
11
00
13698 0 ▶ my %called = map { ($_->{properties}{called} // $usual) => 1 } @{ $outlines ? $outlines->{features} : [] };
Branch: 1 branch
BranchTrueFalseTotal
$outlines ? : 0 8 8
Condition0%0/3
$_->{"properties"}{"called"} // $usual
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"properties"}{"called"} // $usual
$_->{"properties"}{"called"} $usual
Truth table0%0/3
A // B
A= $_->{"properties"}{"called"}B= $usual
ABresult
1X1
011
000
13699 8 my @kinds = keys %called;
13700 8 ▶ my $kind = @kinds > 1 ? 'area' : ($kinds[0] // $usual);
Branch: 1 branch
BranchTrueFalseTotal
@kinds > 1 ? : 0 8 8
Condition33%1/3
$kinds[0] // $usual
l!l&&r!l&&!r
080
MC/DC0%0/2
$kinds[0] // $usual
$kinds[0] $usual
Truth table33%1/3
A // B
A= $kinds[0]B= $usual
ABresult
1X1
011
000
13701 8 return $self->_i18n("map_bounds_noun_$kind");
13702 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
13703  
13704 # _outline_colour($share) -> '#rrggbb' at $share (0..1) along @OUTLINE_STOPS
13705 0 ▶sub _outline_colour {
Subroutine _outline_colour: not called CC=4 SCAR=30.0
13706 0 my ($share) = @_;
13707 0 ▶ $share = 0 if $share < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $share < 0 0 0 0
13708 0 ▶ $share = 1 if $share > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $share > 1 0 0 0
13709 0 my $at = $share * $#OUTLINE_STOPS;
13710 0 my $from = int($at);
13711 0 ▶ $from = $#OUTLINE_STOPS - 1 if $from >= $#OUTLINE_STOPS;
Branch: 1 branch
BranchTrueFalseTotal
if $from >= $#OUTLINE_STOPS 0 0 0
13712 0 my $part = $at - $from;
13713 0 my ($lo, $hi) = @OUTLINE_STOPS[ $from, $from + 1 ];
13714 0 return sprintf('#%02x%02x%02x', map { int($lo->[$_] + ($hi->[$_] - $lo->[$_]) * $part + 0.5) } 0 .. 2);
13715 }
13716  
13717 # _map_key_number($n) -> a total as a key shows it: rounded to two figures
13718 # ("330,000" for 330,818, "1,200,000" for 1,150,309), since a key gives ranges
13719 # to be read at a glance. A total under 100 is given as it is.
13720 0 ▶sub _map_key_number {
Subroutine _map_key_number: not called CC=2 SCAR=17.9
13721 0 my ($n) = @_;
13722 0 ▶ return _map_total_text($n) if abs($n) < $KEY_ROUND_FROM;
Branch: 1 branch
BranchTrueFalseTotal
if abs $n < $KEY_ROUND_FROM 0 0 0
13723 0 my $unit = 10 ** (POSIX::floor(log(abs($n)) / log(10)) - ($KEY_FIGURES - 1));
13724 0 return _map_total_text(POSIX::floor($n / $unit + 0.5) * $unit);
13725 }
13726  
13727 # _map_total_text($n) -> the total as a popup shows it: thousands separated,
13728 # and at most two decimal places ("1,150,309", "12.5").
13729 0 ▶sub _map_total_text {
Subroutine _map_total_text: not called CC=5 SCAR=34.0
13730 0 my ($n) = @_;
13731 0 my $text = sprintf('%.2f', $n);
13732 0 ▶ $text =~ s/\.?0+\z// if $text =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\./u 0 0 0
13733 # A small negative total rounds to nothing; say 0, not "-0".
13734 0 ▶ $text = '0' if $text eq '-0';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "-0" 0 0 0
13735 0 my ($whole, $fraction) = split /\./, $text, 2;
13736 0 ▶ 1 while $whole =~ s/^(-?\d+)(\d{3})/$1,$2/;
Branch: 1 branch
BranchTrueFalseTotal
$whole =~ s/^(-?\d+)(\d{3})/$1,$2/u and do { '???' } 0 0 0
13737 0 ▶ return defined($fraction) ? "$whole.$fraction" : $whole;
Branch: 1 branch
BranchTrueFalseTotal
defined $fraction ? : 0 0 0
13738 }
13739  
13740 # _map_bounds_level($value) -> the level, or undef when $value names none
13741 #
13742 # bounds= on the map page, and a map card's options.bounds, which is stored
13743 # unchecked.
13744 5 ▶sub _map_bounds_level {
Subroutine _map_bounds_level: called CC=5 SCAR=25.2
13745 5 my ($value) = @_;
13746 5 ▶ return unless defined($value) && !ref($value) && length($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 5 0 5
Condition33%2/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
500
defined $value and not ref $value
!ll&&!rl&&r
500
MC/DC0%0/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table25%1/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
13747 0 require Database::BI::Model::Boundary;
13748 0 ▶ return (grep { $_ eq $value } Database::BI::Model::Boundary->levels) ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $value;} 'Database::BI::Model::Boundary'->levels) ? : 0 0 0
13749 }
13750  
13751 # _map_data($self, \@records, \@columns, \%encode, \%options, $spec_key, $id_suffix)
13752 # -> \%result
13753 #
13754 # Resolve a map's markers and draw its HTML. The map page and a map card on a
13755 # canvas both come through here: a map spec names either loc=<column> (places
13756 # to geocode) or lat=/lon= (coordinates already in the data), and everything
13757 # else - the marker cap, popups, the geocode cache and HTML::OSM - is the same
13758 # in both places. %result has { map_body, map_head, title, messages, ... } on success
13759 # or { error, status } when the spec cannot be drawn. $id_suffix makes the
13760 # Leaflet element ids unique when several maps share one page; undef leaves the
13761 # map page's own ids alone.
13762 8 ▶sub _map_data :Protected ($self, $records, $columns, $encode, $options, $spec_key, $id_suffix) {
Subroutine _map_data: called CC=217 SCAR=90.7
13763 8 $spec_key = canonical_spec($spec_key);
13764 8 ▶ $encode //= {};
Condition50%1/2
$encode //= {}
l!l
80
MC/DC0%0/1
$encode //= {}
$encode
Truth table50%1/2
A
A= $encode
Aresult
11
00
13765 8 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
80
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
13766  
13767 # loc may name several columns, in order: the place is their cells joined
13768 # with ", " (an address and then its country). $loc_col is that list's
13769 # name, which is what the page and the geocoder are told.
13770 8 my @loc_cols = _map_loc_columns($encode->{loc});
13771 8 my $loc_col = join(', ', @loc_cols);
13772 8 ▶ my $lat_col = $encode->{lat} // '';
Condition100%2/2
$encode->{"lat"} // ""
l!l
17
MC/DC100%1/1
$encode->{"lat"} // ""
$encode->{"lat"}
Truth table100%2/2
A
A= $encode->{"lat"}
Aresult
11
00
13773 8 ▶ my $lon_col = $encode->{lon} // '';
Condition100%2/2
$encode->{"lon"} // ""
l!l
17
MC/DC100%1/1
$encode->{"lon"} // ""
$encode->{"lon"}
Truth table100%2/2
A
A= $encode->{"lon"}
Aresult
11
00
13774 8 my $by_location = length $loc_col;
13775 8 my ($loc_text) = _map_loc_text($encode->{loc_text});
13776 8 ▶ $loc_text = '' unless defined($loc_text) && $by_location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loc_text and $by_location 1 7 8
Condition66%2/3
defined $loc_text and $by_location
!ll&&!rl&&r
017
MC/DC50%1/2
defined $loc_text and $by_location
defined $loc_text $by_location
Truth table66%2/3
A and B
A= defined $loc_textB= $by_location
ABresult
0X0
100
111
13777  
13778 8 ▶ return { error => $self->_i18n('error_map_source'), status => 400 }
Branch: 1 branch
BranchTrueFalseTotal
unless $by_location or length $lat_col or length $lon_col 0 8 8
Condition50%3/6
$by_location or length $lat_col or length $lon_col
l!l&&r!l&&!r
800
$by_location or length $lat_col
l!l&&r!l&&!r
710
MC/DC0%0/3
$by_location or length $lat_col or length $lon_col
$by_location length $lat_col length $lon_col
Truth table50%2/4
A or B or C
A= $by_locationB= length $lat_colC= length $lon_col
ABCresult
1XX1
01X1
0011
0000
13779 unless $by_location || length($lat_col) || length($lon_col);
13780 8 ▶ return { error => $self->_i18n('error_map_coordinates'), status => 400 }
Branch: 1 branch
BranchTrueFalseTotal
unless $by_location or length $lat_col and length $lon_col 0 8 8
Condition50%3/6
$by_location or length $lat_col and length $lon_col
l!l&&r!l&&!r
710
length $lat_col and length $lon_col
!ll&&!rl&&r
001
MC/DC0%0/3
$by_location or length $lat_col and length $lon_col
$by_location length $lat_col length $lon_col
Truth table50%2/4
A or B and C
A= $by_locationB= length $lat_colC= length $lon_col
ABCresult
1XX1
0111
00X0
0100
13781 if !$by_location && (!length($lat_col) || !length($lon_col));
13782  
13783 8 my %col_set = map { $_ => 1 } @$columns;
13784 8 my $cfg = $self->app->config;
13785  
13786 8 my %loc_set = map { $_ => 1 } @loc_cols;
13787 my @popup = grep {
13788 0 ▶ defined($_) && !ref($_) && length($_) && !$loc_set{$_} && $col_set{$_}
Condition0%0/12
defined $_ && !ref($_) && length $_ && !$loc_set{$_} && $col_set{$_}
!ll&&!rl&&r
000
defined $_ and not ref $_ and length $_ and not $loc_set{$_}
!ll&&!rl&&r
000
defined $_ and not ref $_ and length $_
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/5
defined $_ && !ref($_) && length $_ && !$loc_set{$_} && $col_set{$_}
defined $_ not ref $_ length $_ not $loc_set{$_} $col_set{$_}
Truth table0%0/6
A and not(B) and C and not(D) && E
A= defined $_B= not ref $_C= length $_D= not $loc_set{$_}E= $col_set{$_}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
13789 8 ▶ } @{ ref($options->{popup}) eq 'ARRAY' ? $options->{popup} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{'popup'} eq 'ARRAY' ? : 0 8 8
13790  
13791 # Default: when no popup columns are requested, show every column that is
13792 # not a coordinate column so the marker tooltip is informative out of the box.
13793 8 ▶ unless (@popup) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@popup) 8 0 8
13794 9 my %coord_set = map { $_ => 1 }
13795 8 ▶ grep { defined($_) && length($_) } (@loc_cols, $lat_col, $lon_col);
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
0149
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
13796 8 @popup = grep { !$coord_set{$_} } @$columns;
13797 }
13798  
13799 8 my ($backend, $plotted_rows) = (undef, 0);
13800 8 my (@markers, @messages, @unplotted, @unplotted_filters, @pending);
13801  
13802 # How many rows a popup lists, and where the rest are: the map page passes
13803 # rows_url, which makes a table link from filters; a canvas card has none.
13804 8 my $popup_rows = _map_limit($cfg->{map_popup_rows}, $DEFAULT_MAP_POPUP_ROWS);
13805 8 ▶ my $rows_url = ref($options->{rows_url}) eq 'CODE' ? $options->{rows_url} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{'rows_url'} eq 'CODE' ? : 2 6 8
13806  
13807 8 ▶ if ($by_location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by_location) { } 7 1 8
13808 7 for my $col (@loc_cols) {
13809 return { error => $self->_i18n('error_map_loc_column', $col), status => 400 }
13810 7 ▶ unless $col_set{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$col} 2 5 7
13811 }
13812  
13813 5 require Database::BI::Model::Geocode;
13814  
13815 # What the geocoder is given: one cell a row, under $loc_col, in the
13816 # rows' own order, so a marker's first_row still indexes $records.
13817 5 my ($places) = Database::BI::Model::Geocode->place_rows($records, \@loc_cols, $loc_text);
13818  
13819 5 my $geocode = $self->_map_geocoder(1);
13820  
13821 # A place name means the same place whichever file's column holds it, so
13822 # the answers live in one shared cache: the second dataset that names a
13823 # place plots it without asking the geocoder. A per-dataset entry from an
13824 # older model store is folded in as well; the shared answer wins if both
13825 # have the name, and places only the old entry knows about are moved into
13826 # the shared cache below, so the next dataset benefits from them too.
13827 5 ▶ my $cache_spec = ($spec_key // '') . '|loc=' . $loc_col;
Condition50%1/2
$spec_key // ""
l!l
50
MC/DC0%0/1
$spec_key // ""
$spec_key
Truth table50%1/2
A
A= $spec_key
Aresult
11
00
13828 5 my $models = $self->_read_models;
13829 5 my $legacy = Database::BI::Model::Geocode->cache_from_models($models, $cache_spec);
13830 5 my $shared = Database::BI::Model::Geocode->shared_cache_from_models($models);
13831 5 my %cache = (%$legacy, %$shared);
13832 0 my $to_share = grep { !exists $shared->{$_} } keys %$legacy;
13833 5 my $region = _map_region($cfg);
13834  
13835 # lookup => 0 (the map page in a browser) asks nothing here: the
13836 # places not cached yet come back as pending, and the page looks them
13837 # up one at a time through /geo/lookup, with a progress bar.
13838 my $result = $geocode->resolve_column(
13839 $places, $loc_col,
13840 cache => \%cache,
13841 (defined($region) ? (region => $region) : ()),
13842 max => _map_limit($cfg->{map_geocode_max}, $DEFAULT_MAP_GEOCODE_MAX),
13843 5 ▶ lookup => ((defined($options->{lookup}) && !$options->{lookup}) ? 0 : 1),
Branch: 2 branches
BranchTrueFalseTotal
defined $region ? : 0 5 5
defined $options->{'lookup'} && !$options->{'lookup'} ? : 1 4 5
Condition66%2/3
defined $options->{"lookup"} and not $options->{"lookup"}
!ll&&!rl&&r
401
MC/DC50%1/2
defined $options->{"lookup"} and not $options->{"lookup"}
defined $options->{"lookup"} not $options->{"lookup"}
Truth table66%2/3
A and not(B)
A= defined $options->{"lookup"}B= not $options->{"lookup"}
ABresult
0X0
100
111
13844 );
13845 5 ▶ @pending = @{ $result->{pending} // [] };
Condition50%1/2
$result->{"pending"} // []
l!l
50
MC/DC0%0/1
$result->{"pending"} // []
$result->{"pending"}
Truth table50%1/2
A
A= $result->{"pending"}
Aresult
11
00
13846  
13847 5 push @messages, @{ $result->{messages} };
13848 5 $backend = $result->{backend};
13849 5 $plotted_rows = $result->{counts}{plotted};
13850 5 my ($entries, $all) = _map_unplotted($records, $places, $loc_col, \@loc_cols, $result);
13851 5 @unplotted = @{$entries};
13852 5 @unplotted_filters = @{$all};
13853  
13854 # Only a request that asked something new, or found old answers to move
13855 # into the shared cache, has something new to remember: rewriting the
13856 # store for a page answered entirely from the shared cache would be a
13857 # write per view. $to_share empties after the migration, so an old store
13858 # is written once per dataset rather than on every view.
13859 5 ▶ if ($result->{counts}{attempted} || $to_share) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"counts"}{"attempted"} or $to_share) 1 4 5
Condition66%2/3
$result->{"counts"}{"attempted"} or $to_share
l!l&&r!l&&!r
104
MC/DC50%1/2
$result->{"counts"}{"attempted"} or $to_share
$result->{"counts"}{"attempted"} $to_share
Truth table66%2/3
A or B
A= $result->{"counts"}{"attempted"}B= $to_share
ABresult
1X1
011
000
13860 my $updated = Database::BI::Model::Geocode->models_with_shared_cache(
13861 $models, $result->{cache},
13862 1 ▶ (defined($backend) ? (backend => $backend) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $backend ? : 1 0 1
13863 built => _iso8601_utc(),
13864 );
13865 1 my $written = $self->_write_models($updated);
13866  
13867 # A failed migration is not worth a page message: the answers are
13868 # still in the old entry, so this dataset is unaffected. A failed
13869 # new lookup is, because the next visit pays for it again.
13870 1 ▶ if (!$written && $result->{counts}{attempted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $written and $result->{"counts"}{"attempted"}) 0 1 1
Condition33%1/3
not $written and $result->{"counts"}{"attempted"}
!ll&&!rl&&r
100
MC/DC0%0/2
not $written and $result->{"counts"}{"attempted"}
not $written $result->{"counts"}{"attempted"}
Truth table33%1/3
not(A) and B
A= not $writtenB= $result->{"counts"}{"attempted"}
ABresult
0X0
100
111
13871 0 push @messages, {
13872 key => 'warn_map_cache_unwritten',
13873 text => $self->_i18n('warn_map_cache_unwritten'),
13874 };
13875 }
13876 }
13877  
13878 # Every row at a place, keyed as the geocoder keyed the place, so its
13879 # popup can list them all and not only the first.
13880 5 my %rows_at;
13881 5 for my $index (0 .. $#{$places}) {
13882 12 my $place = $places->[$index];
13883 12 ▶ my $value = ref($place) eq 'HASH' ? $place->{$loc_col} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $place eq 'HASH' ? : 12 0 12
13884 12 ▶ push @{ $rows_at{ (defined($value) && !ref($value)) ? $value : '' } }, $index;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && !ref($value) ? : 12 0 12
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
13885 }
13886  
13887 5 for my $marker (@{ $result->{markers} }) {
13888 3 ▶ my @at = @{ $rows_at{ $marker->{value} } // [] };
Condition50%1/2
$rows_at{$marker->{"value"}} // []
l!l
30
MC/DC0%0/1
$rows_at{$marker->{"value"}} // []
$rows_at{$marker->{"value"}}
Truth table50%1/2
A
A= $rows_at{$marker->{"value"}}
Aresult
11
00
13889 3 ▶ my $row = $records->[ $at[0] // $marker->{first_row} ];
Condition33%1/3
$at[0] // $marker->{"first_row"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$at[0] // $marker->{"first_row"}
$at[0] $marker->{"first_row"}
Truth table33%1/3
A // B
A= $at[0]B= $marker->{"first_row"}
ABresult
1X1
011
000
13890 3 ▶ $row = {} unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 3 3
13891 my $more_url = $rows_url ? $rows_url->([ map {
13892 0 ▶ my $cell = $row->{$_};
Branch: 1 branch
BranchTrueFalseTotal
$rows_url ? : 0 3 3
13893 0 ▶ (defined($cell) && !ref($cell) && length($cell)) ? "$_:eq:$cell" : "$_:empty";
Branch: 1 branch
BranchTrueFalseTotal
defined $cell && !ref($cell) && length $cell ? : 0 0 0
Condition0%0/6
defined $cell and not ref $cell and length $cell
!ll&&!rl&&r
000
defined $cell and not ref $cell
!ll&&!rl&&r
000
MC/DC0%0/3
defined $cell and not ref $cell and length $cell
defined $cell not ref $cell length $cell
Truth table0%0/4
A and not(B) and C
A= defined $cellB= not ref $cellC= length $cell
ABCresult
0XX0
10X0
1100
1111
13894 } @loc_cols ]) : '';
13895 push @markers, {
13896 3 %{ $marker },
13897 'row_indexes' => \@at,
13898 'label' => _map_popup(
13899 3 $marker->{value}, [ @{$records}[@at] ], \@popup, $popup_rows, $more_url,
13900 ),
13901 };
13902 }
13903  
13904 } else {
13905 return { error => "Column not found: $lat_col", status => 400 }
13906 1 ▶ unless $col_set{$lat_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$lat_col} 0 1 1
13907 return { error => "Column not found: $lon_col", status => 400 }
13908 1 ▶ unless $col_set{$lon_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$lon_col} 0 1 1
13909  
13910 1 my (%by_key, %rows_at, $out_of_lat, $out_of_lon, $unusable);
13911  
13912 1 for my $index (0 .. $#$records) {
13913 6 my $row = $records->[$index];
13914 6 ▶ my $lat = _numeric_cell($row->{$lat_col} // '');
Condition50%1/2
$row->{$lat_col} // ""
l!l
60
MC/DC0%0/1
$row->{$lat_col} // ""
$row->{$lat_col}
Truth table50%1/2
A
A= $row->{$lat_col}
Aresult
11
00
13915 6 ▶ my $lon = _numeric_cell($row->{$lon_col} // '');
Condition50%1/2
$row->{$lon_col} // ""
l!l
60
MC/DC0%0/1
$row->{$lon_col} // ""
$row->{$lon_col}
Truth table50%1/2
A
A= $row->{$lon_col}
Aresult
11
00
13916  
13917 # A row is never plotted at (0,0): a fake point in the Gulf of
13918 # Guinea is worse than a missing one. A whole column outside its
13919 # range is named instead, because a transposed pair is the error a
13920 # user makes here.
13921 0 ▶ if (defined($lat) && ($lat < -90 || $lat > 90)) { $out_of_lat++; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lat and $lat < -90 || $lat > 90) 0 6 6
Condition33%2/6
defined $lat and $lat < -90 || $lat > 90
!ll&&!rl&&r
060
$lat < -90 or $lat > 90
l!l&&r!l&&!r
006
MC/DC0%0/3
defined $lat and $lat < -90 || $lat > 90
defined $lat $lat < -90 $lat > 90
Truth table25%1/4
A and B or C
A= defined $latB= $lat < -90C= $lat > 90
ABCresult
0XX0
1000
11X1
1011
13922 6 ▶ if (defined($lon) && ($lon < -180 || $lon > 180)) { $out_of_lon++; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lon and $lon < -180 || $lon > 180) 6 0 6
Condition33%2/6
defined $lon and $lon < -180 || $lon > 180
!ll&&!rl&&r
006
$lon < -180 or $lon > 180
l!l&&r!l&&!r
060
MC/DC0%0/3
defined $lon and $lon < -180 || $lon > 180
defined $lon $lon < -180 $lon > 180
Truth table25%1/4
A and B or C
A= defined $lonB= $lon < -180C= $lon > 180
ABCresult
0XX0
1000
11X1
1011
13923 0 ▶ unless (defined($lat) && defined($lon)) { $unusable++; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lat and defined $lon) 0 0 0
Condition0%0/3
defined $lat and defined $lon
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lat and defined $lon
defined $lat defined $lon
Truth table0%0/3
A and B
A= defined $latB= defined $lon
ABresult
0X0
100
111
13924  
13925 # Rows at one coordinate are one marker: that is what makes a large
13926 # file renderable, and "how many are here?" is the question a map
13927 # answers.
13928 0 my $key = sprintf('%.6f,%.6f', $lat, $lon);
13929 0 push @{ $rows_at{$key} }, $index;
13930 0 my $point = $by_key{$key};
13931 0 ▶ if ($point) { $point->{rows}++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($point) 0 0 0
13932  
13933 0 $point = $by_key{$key} = {
13934 'lat' => $lat, 'lon' => $lon, 'rows' => 1, 'first_row' => $index,
13935 };
13936 0 push @markers, $point;
13937 }
13938  
13939 1 ▶ return { error => $self->_i18n('error_map_coordinate_range', $lat_col, 'latitude'),
Branch: 1 branch
BranchTrueFalseTotal
if not @markers and $out_of_lat 0 1 1
Condition33%1/3
not @markers and $out_of_lat
!ll&&!rl&&r
010
MC/DC0%0/2
not @markers and $out_of_lat
not @markers $out_of_lat
Truth table33%1/3
not(A) and B
A= not @markersB= $out_of_lat
ABresult
0X0
100
111
13940 status => 400 } if !@markers && $out_of_lat;
13941 1 ▶ return { error => $self->_i18n('error_map_coordinate_range', $lon_col, 'longitude'),
Branch: 1 branch
BranchTrueFalseTotal
if not @markers and $out_of_lon 1 0 1
Condition33%1/3
not @markers and $out_of_lon
!ll&&!rl&&r
001
MC/DC0%0/2
not @markers and $out_of_lon
not @markers $out_of_lon
Truth table33%1/3
not(A) and B
A= not @markersB= $out_of_lon
ABresult
0X0
100
111
13942 status => 400 } if !@markers && $out_of_lon;
13943 0 ▶ return { error => $self->_i18n('error_map_coordinate_numeric', $lat_col, $lon_col),
Branch: 1 branch
BranchTrueFalseTotal
if not @markers and @$records 0 0 0
Condition0%0/3
not @markers and @$records
!ll&&!rl&&r
000
MC/DC0%0/2
not @markers and @$records
not @markers @$records
Truth table0%0/3
not(A) and B
A= not @markersB= @$records
ABresult
0X0
100
111
13944 status => 400 } if !@markers && @$records;
13945  
13946 0 $plotted_rows += $_->{rows} for @markers;
13947  
13948 0 ▶ push @messages, {
Branch: 1 branch
BranchTrueFalseTotal
if $plotted_rows < scalar @$records 0 0 0
13949 key => 'warn_map_coordinate_rows',
13950 text => $self->_i18n('warn_map_coordinate_rows', $plotted_rows,
13951 scalar(@$records), scalar(@$records) - $plotted_rows),
13952 } if $plotted_rows < scalar @$records;
13953  
13954 0 for my $marker (@markers) {
13955 0 my $key = sprintf('%.6f,%.6f', $marker->{lat}, $marker->{lon});
13956 0 $marker->{row_indexes} = $rows_at{$key};
13957 0 my $row = $records->[ $marker->{first_row} ];
13958 my $more_url = $rows_url ? $rows_url->([
13959 "$lat_col:eq:" . ($row->{$lat_col} // ''),
13960 0 ▶ "$lon_col:eq:" . ($row->{$lon_col} // ''),
Branch: 1 branch
BranchTrueFalseTotal
$rows_url ? : 0 0 0
Condition0%0/4
$row->{$lat_col} // ""
l!l
00
$row->{$lon_col} // ""
l!l
00
MC/DC0%0/1
$row->{$lat_col} // ""
$row->{$lat_col}
MC/DC0%0/1
$row->{$lon_col} // ""
$row->{$lon_col}
Truth table0%0/2
A
A= $row->{$lat_col}
Aresult
11
00
Truth table0%0/2
A
A= $row->{$lon_col}
Aresult
11
00
13961 ]) : '';
13962 $marker->{label} = _map_popup(
13963 sprintf('%.6f, %.6f', $marker->{lat}, $marker->{lon}),
13964 0 [ @{$records}[ @{ $rows_at{$key} } ] ], \@popup, $popup_rows, $more_url,
13965 );
13966 }
13967 }
13968  
13969 5 ▶ my $title = $by_location ? "Map of $loc_col" : "$lat_col and $lon_col";
Branch: 1 branch
BranchTrueFalseTotal
$by_location ? : 5 0 5
13970 my $cap = _map_limit($options->{max},
13971 5 _map_limit($cfg->{map_max_markers}, $DEFAULT_MAP_MAX_MARKERS));
13972  
13973 5 ▶ if (@markers > $cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (@markers > $cap) 0 5 5
13974 0 push @messages, {
13975 key => 'warn_map_markers_capped',
13976 text => $self->_i18n('warn_map_markers_capped', $cap, scalar @markers),
13977 };
13978 0 @markers = @markers[0 .. $cap - 1];
13979 }
13980  
13981 5 ▶ push @messages, {
Branch: 1 branch
BranchTrueFalseTotal
unless @markers 2 3 5
13982 key => 'error_map_no_markers',
13983 text => $self->_i18n('error_map_no_markers'),
13984 } unless @markers;
13985  
13986 # The outline of the area around each marker (bounds=county, or another
13987 # of Boundary's levels: bounds=state, bounds=city). Those
13988 # not known yet are looked up here, up to the online limit, unless the
13989 # caller said lookup => 0: the map page in a browser lists them for its
13990 # progress bar instead, as it does places.
13991 5 my $bounds_level = _map_bounds_level($options->{bounds});
13992 5 my ($outlines, @bounds_pending);
13993 5 my $bounds_coloured = 0; # set when the outlines are coloured by their totals
13994 5 my $bounds_as_markers = 0; # set when they take their markers' colours
13995 5 my $bounds_shade = ''; # the column they are filled by (shade=), when they are
13996 5 my $bounds_shade_as = ''; # 'one' colour, or the 'markers' colours
13997 5 my (@bounds_left_filters, $bounds_left_rows); # the rows whose marker has no outline
13998 5 my @bounds_groups; # why there are fewer outlines than markers (_map_outline_groups)
13999 5 ▶ if (defined($bounds_level) && @markers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bounds_level and @markers) 0 5 5
Condition33%1/3
defined $bounds_level and @markers
!ll&&!rl&&r
500
MC/DC0%0/2
defined $bounds_level and @markers
defined $bounds_level @markers
Truth table33%1/3
A and B
A= defined $bounds_levelB= @markers
ABresult
0X0
100
111
14000 0 ▶ my $lookup = (defined($options->{lookup}) && !$options->{lookup}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'lookup'} && !$options->{'lookup'} ? : 0 0 0
Condition0%0/3
defined $options->{"lookup"} and not $options->{"lookup"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options->{"lookup"} and not $options->{"lookup"}
defined $options->{"lookup"} not $options->{"lookup"}
Truth table0%0/3
A and not(B)
A= defined $options->{"lookup"}B= not $options->{"lookup"}
ABresult
0X0
100
111
14001 $outlines = $self->_map_boundary($bounds_level, 1)->outlines(
14002 0 [ map { [ $_->{lat}, $_->{lon} ] } @markers ],
14003 lookup => $lookup,
14004 );
14005 0 @bounds_pending = @{ $outlines->{pending} };
14006 0 ▶ if (@bounds_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bounds_pending) 0 0 0
14007 # The rows behind the markers left without an outline, for a link
14008 # to them in the table, as the rows not plotted have.
14009 0 require Database::BI::Model::Boundary;
14010 0 my %left = map { $_ => 1 } @bounds_pending;
14011 0 ▶ my @rows = map { @{ $_->{row_indexes} // [ $_->{first_row} ] } } grep {
Condition0%0/2
$_->{"row_indexes"} // [$_->{"first_row"}]
l!l
00
MC/DC0%0/1
$_->{"row_indexes"} // [$_->{"first_row"}]
$_->{"row_indexes"}
Truth table0%0/2
A
A= $_->{"row_indexes"}
Aresult
11
00
14012 0 my $key = Database::BI::Model::Boundary->point_key($_->{lat}, $_->{lon});
14013 0 ▶ defined($key) && $left{$key};
Condition0%0/3
defined $key && $left{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key && $left{$key}
defined $key $left{$key}
Truth table0%0/3
A && B
A= defined $keyB= $left{$key}
ABresult
0X0
100
111
14014 } @markers;
14015 0 $bounds_left_rows = scalar @rows;
14016 0 ▶ @bounds_left_filters = _map_rows_filters($records, \@rows,
Branch: 1 branch
BranchTrueFalseTotal
$by_location ? : 0 0 0
14017 [ $by_location ? @loc_cols : ($lat_col, $lon_col) ]);
14018 my $key = $outlines->{offline} ? 'warn_map_bounds_offline'
14019 0 ▶ : $outlines->{failed} ? 'warn_map_bounds_failed'
Branch: 2 branches
BranchTrueFalseTotal
$outlines->{'offline'} ? : 0 0 0
$outlines->{'failed'} ? : 0 0 0
14020 : 'warn_map_bounds_pending';
14021 push @messages, {
14022 key => $key,
14023 # One place "has" no outline; the key stays the plural's
14024 text => $self->_i18n($key . (@bounds_pending == 1 ? '_one' : ''),
14025 scalar(@bounds_pending), $outlines->{counts}{points},
14026 0 ▶ $self->_map_outline_noun($outlines, $bounds_level)),
Branch: 1 branch
BranchTrueFalseTotal
@bounds_pending == 1 ? : 0 0 0
14027 };
14028 }
14029 0 ▶ @bounds_groups = _map_outline_groups($outlines, \@markers, $records,
Branch: 1 branch
BranchTrueFalseTotal
$by_location ? : 0 0 0
14030 [ $by_location ? @loc_cols : ($lat_col, $lon_col) ]);
14031 }
14032  
14033 # Leaflet's CSS and JS belong in the head, which the layout owns; the rest of
14034 # the map - the search box, the buttons, the div and its script - goes in the
14035 # body where the map should appear.
14036 5 my ($map_head, $map_body);
14037 5 ▶ if (@markers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@markers) 3 2 5
14038 # HTML::OSM is a recommended dependency, so its absence - or an install
14039 # older than the options below - is a state the page reports and
14040 # survives: the markers are still counted, nothing is drawn, and the
14041 # rest of the application is untouched. The version check is not
14042 # optional: HTML::OSM rejects unknown constructor keys, so passing
14043 # search/max_zoom to an older copy would croak and turn the page into a
14044 # 500 rather than the warning this branch produces.
14045 #
14046 # The installed version comes back with the answer so the warning can
14047 # say which problem it is: "not installed" and "installed but older than
14048 # 0.12" have different fixes, and the old wording called both of them
14049 # "could not be loaded". The probe is a method (see
14050 # _map_renderer_version) so the tests can produce either state without
14051 # installing or removing the module.
14052 3 my ($osm_ok, $osm_version) = $self->_map_renderer_version;
14053 3 ▶ if ($osm_ok) {
Branch: 2 branches
BranchTrueFalseTotal
if ($osm_ok) { } 3 0 3
elsif (defined $osm_version) { } 0 0 0
14054 # Markers go in a cluster group rather than straight onto the map.
14055 # A card may plot up to map_max_markers (2000 by default), and at a
14056 # low zoom that many pins overlap into noise; markercluster draws a
14057 # count instead and splits it as the reader zooms in. A sparse map
14058 # looks the same - the plugin leaves markers that are far apart on
14059 # their own - but the plugin is only fetched once the map itself is.
14060 # The Leaflet assets are a config seam too. A deployment with no
14061 # outbound network access serves them from its own origin; any key
14062 # the operator leaves out keeps HTML::OSM's pinned CDN URL. The tile
14063 # layer has its own key, since a local tile server removes the last
14064 # outbound request the page makes.
14065 3 my %leaflet_assets;
14066 3 for my $asset (
14067 [ 'leaflet_js_url', 'js_url' ],
14068 [ 'leaflet_css_url', 'css_url' ],
14069 [ 'leaflet_cluster_js_url', 'cluster_js_url' ],
14070 [ 'leaflet_cluster_css_url', 'cluster_css_url' ],
14071 [ 'leaflet_cluster_default_css_url', 'cluster_default_css_url' ],
14072 ) {
14073 15 my ($key, $param) = @$asset;
14074 15 my $url = $cfg->{$key};
14075 15 ▶ $leaflet_assets{$param} = $url
Branch: 1 branch
BranchTrueFalseTotal
if defined $url and not ref $url and length $url 0 15 15
Condition33%2/6
defined $url and not ref $url and length $url
!ll&&!rl&&r
1500
defined $url and not ref $url
!ll&&!rl&&r
1500
MC/DC0%0/3
defined $url and not ref $url and length $url
defined $url not ref $url length $url
Truth table25%1/4
A and not(B) and C
A= defined $urlB= not ref $urlC= length $url
ABCresult
0XX0
10X0
1100
1111
14076 if defined $url && !ref $url && length $url;
14077 }
14078  
14079 3 my $tile_url = $cfg->{tile_url};
14080 3 ▶ $tile_url = undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tile_url and not ref $tile_url and length $tile_url 3 0 3
Condition33%2/6
defined $tile_url and not ref $tile_url and length $tile_url
!ll&&!rl&&r
300
defined $tile_url and not ref $tile_url
!ll&&!rl&&r
300
MC/DC0%0/3
defined $tile_url and not ref $tile_url and length $tile_url
defined $tile_url not ref $tile_url length $tile_url
Truth table25%1/4
A and not(B) and C
A= defined $tile_urlB= not ref $tile_urlC= length $tile_url
ABCresult
0XX0
10X0
1100
1111
14081 unless defined($tile_url) && !ref($tile_url) && length($tile_url);
14082  
14083 # options{tile_url} wins over the global config tile_url when set.
14084 # HTML::OSM >= 0.12 is the minimum, so tile_url is always accepted.
14085 3 ▶ my $eff_tile_url = $options->{tile_url} // $tile_url;
Condition33%1/3
$options->{"tile_url"} // $tile_url
l!l&&r!l&&!r
003
MC/DC0%0/2
$options->{"tile_url"} // $tile_url
$options->{"tile_url"} $tile_url
Truth table33%1/3
A // B
A= $options->{"tile_url"}B= $tile_url
ABresult
1X1
011
000
14086 3 ▶ $eff_tile_url = undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eff_tile_url and not ref $eff_tile_url and length $eff_tile_url 3 0 3
Condition33%2/6
defined $eff_tile_url and not ref $eff_tile_url and length $eff_tile_url
!ll&&!rl&&r
300
defined $eff_tile_url and not ref $eff_tile_url
!ll&&!rl&&r
300
MC/DC0%0/3
defined $eff_tile_url and not ref $eff_tile_url and length $eff_tile_url
defined $eff_tile_url not ref $eff_tile_url length $eff_tile_url
Truth table25%1/4
A and not(B) and C
A= defined $eff_tile_urlB= not ref $eff_tile_urlC= length $eff_tile_url
ABCresult
0XX0
10X0
1100
1111
14087 unless defined($eff_tile_url) && !ref($eff_tile_url) && length($eff_tile_url);
14088  
14089 my %osm_args = (
14090 height => '560px',
14091 width => '100%',
14092 cluster => 1,
14093 search => 0,
14094 ($eff_tile_url ? (tile_url => $eff_tile_url) : ()),
14095 (defined($options->{zoom}) ? (zoom => $options->{zoom}) : ()),
14096 3 ▶ (defined($options->{max_zoom}) ? (max_zoom => $options->{max_zoom}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$eff_tile_url ? : 0 3 3
defined $options->{'zoom'} ? : 0 3 3
defined $options->{'max_zoom'} ? : 0 3 3
14097 %leaflet_assets,
14098 );
14099 # The renderer is a third-party module: whatever it does, the page
14100 # reports it and survives, as it does when the module is absent.
14101 # The map page passes the colour and size columns as options; a
14102 # canvas card has them in its encode, as the page saved them. A
14103 # card's are stored unchecked, so one that names no column is
14104 # left out rather than refused.
14105 3 ▶ my $color_col = $options->{color_col} // $encode->{color} // '';
Condition40%2/5
($options->{"color_col"} // $encode->{"color"}) // ""
l!l
03
$options->{"color_col"} // $encode->{"color"}
l!l&&r!l&&!r
003
MC/DC0%0/2
($options->{"color_col"} // $encode->{"color"}) // ""
$options->{"color_col"} $encode->{"color"}
Truth table33%1/3
A // B
A= $options->{"color_col"}B= $encode->{"color"}
ABresult
1X1
011
000
14106 3 ▶ $color_col = '' if ref($color_col) || !$col_set{$color_col};
Branch: 1 branch
BranchTrueFalseTotal
if ref $color_col or not $col_set{$color_col} 3 0 3
Condition33%1/3
ref $color_col or not $col_set{$color_col}
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $color_col or not $col_set{$color_col}
ref $color_col not $col_set{$color_col}
Truth table33%1/3
A or not(B)
A= ref $color_colB= not $col_set{$color_col}
ABresult
1X1
011
000
14107 # A card's rules are stored unchecked: one that is not a rule is
14108 # left out rather than read as one.
14109 2 my $color_rules = [ grep { ref($_) eq 'HASH' }
14110 3 ▶ @{ ref($options->{color_rules}) eq 'ARRAY' ? $options->{color_rules} : [] } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{'color_rules'} eq 'ARRAY' ? : 1 2 3
14111 3 ▶ my $color_default = $options->{color_default} // $DEFAULT_MARKER_COLOR;
Condition33%1/3
$options->{"color_default"} // $DEFAULT_MARKER_COLOR
l!l&&r!l&&!r
030
MC/DC0%0/2
$options->{"color_default"} // $DEFAULT_MARKER_COLOR
$options->{"color_default"} $DEFAULT_MARKER_COLOR
Truth table33%1/3
A // B
A= $options->{"color_default"}B= $DEFAULT_MARKER_COLOR
ABresult
1X1
011
000
14112 3 ▶ my $size_col = $options->{size} // $encode->{size} // '';
Condition40%2/5
($options->{"size"} // $encode->{"size"}) // ""
l!l
03
$options->{"size"} // $encode->{"size"}
l!l&&r!l&&!r
003
MC/DC0%0/2
($options->{"size"} // $encode->{"size"}) // ""
$options->{"size"} $encode->{"size"}
Truth table33%1/3
A // B
A= $options->{"size"}B= $encode->{"size"}
ABresult
1X1
011
000
14113 3 ▶ $size_col = '' if ref($size_col) || !$col_set{$size_col};
Branch: 1 branch
BranchTrueFalseTotal
if ref $size_col or not $col_set{$size_col} 3 0 3
Condition33%1/3
ref $size_col or not $col_set{$size_col}
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $size_col or not $col_set{$size_col}
ref $size_col not $col_set{$size_col}
Truth table33%1/3
A or not(B)
A= ref $size_colB= not $col_set{$size_col}
ABresult
1X1
011
000
14114  
14115 # The scale bar, and the key on the map: colours and bubble sizes.
14116 3 my $size_range = [ _map_size_range($records, \@markers, $size_col) ];
14117 # When the outlines are shaded in the markers' colours by the column
14118 # the colours read, the shade key lists every colour with its totals,
14119 # so a second key of the same colours would only repeat it. Shaded by
14120 # another column, or in one colour, the marker key is still the only
14121 # place the colours are explained. Bubble sizes are listed either way.
14122 0 ▶ my $shade_covers = length($color_col) && $outlines && @{ $outlines->{features} }
Condition0%0/4
$options->{"shade"} // ""
l!l
00
$options->{"shade"} // ""
l!l
00
MC/DC0%0/1
$options->{"shade"} // ""
$options->{"shade"}
MC/DC0%0/1
$options->{"shade"} // ""
$options->{"shade"}
Truth table0%0/2
A
A= $options->{"shade"}
Aresult
11
00
Truth table0%0/2
A
A= $options->{"shade"}
Aresult
11
00
14123 && !ref($options->{shade} // '') && ($options->{shade} // '') eq $color_col
14124 3 ▶ && !defined($options->{shade_color});
Condition33%5/15
length $color_col && $outlines && @{$outlines->{'features'};} && !ref($options->{'shade'} // '') && ($options->{'shade'} // '') eq $color_col && !defined($options->{'shade_color'})
!ll&&!rl&&r
300
length $color_col and $outlines and @{$outlines->{"features"};} and not ref($options->{"shade"} // "") and ($options->{"shade"} // "") eq $color_col
!ll&&!rl&&r
300
length $color_col and $outlines and @{$outlines->{"features"};} and not ref($options->{"shade"} // "")
!ll&&!rl&&r
300
length $color_col and $outlines and @{$outlines->{"features"};}
!ll&&!rl&&r
300
length $color_col and $outlines
!ll&&!rl&&r
300
MC/DC0%0/6
length $color_col && $outlines && @{$outlines->{'features'};} && !ref($options->{'shade'} // '') && ($options->{'shade'} // '') eq $color_col && !defined($options->{'shade_color'})
length $color_col $outlines @{$outlines->{"features"};} not ref($options->{"shade"} // "") ($options->{"shade"} // "") eq $color_col !defined($options->{'shade_color'})
Truth table14%1/7
A and B and C and not(D) and E && not(F)
A= length $color_colB= $outlinesC= @{$outlines->{"features"};}D= not ref($options->{"shade"} // "")E= ($options->{"shade"} // "") eq $color_colF= !defined($options->{'shade_color'})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
14125 # HTML::OSM 0.19 can leave a key entry out while a layer is shown and
14126 # bring it back when the reader hides the layer, so the markers are
14127 # never left without a key; an older copy cannot, and gets no colour
14128 # entries at all.
14129 my $key_waits = $shade_covers
14130 0 ▶ && eval { version->parse($osm_version) >= version->parse($OSM_KEY_WAITS_VERSION) };
Condition33%1/3
$shade_covers && eval { do { 'version'->parse($osm_version) >= 'version'->parse($OSM_KEY_WAITS_VERSION) } }
!ll&&!rl&&r
300
MC/DC0%0/2
$shade_covers && eval { do { 'version'->parse($osm_version) >= 'version'->parse($OSM_KEY_WAITS_VERSION) } }
$shade_covers eval { do { 'version'->parse($osm_version) >= 'version'->parse($OSM_KEY_WAITS_VERSION) } }
Truth table33%1/3
A && B
A= $shade_coversB= eval { do { 'version'->parse($osm_version) >= 'version'->parse($OSM_KEY_WAITS_VERSION) } }
ABresult
0X0
100
111
14131 my %key = $self->_map_key((($shade_covers && !$key_waits) ? '' : $color_col), $color_rules,
14132 3 ▶ $color_default, defined($options->{scheme}), $size_col, $size_range);
Branch: 1 branch
BranchTrueFalseTotal
$shade_covers && !$key_waits ? : 0 3 3
Condition33%1/3
$shade_covers and not $key_waits
!ll&&!rl&&r
300
MC/DC0%0/2
$shade_covers and not $key_waits
$shade_covers not $key_waits
Truth table33%1/3
A and not(B)
A= $shade_coversB= not $key_waits
ABresult
0X0
100
111
14133 3 ▶ if ($key_waits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_waits) 0 3 3
14134 0 my $layer = $self->_i18n('map_bounds_layer', ucfirst $self->_map_outline_noun($outlines, $bounds_level));
14135 0 ▶ $_->{unless_layer} = $layer for grep { defined $_->{group} } @{ $key{legend} // [] };
Condition0%0/2
$key{"legend"} // []
l!l
00
MC/DC0%0/1
$key{"legend"} // []
$key{"legend"}
Truth table0%0/2
A
A= $key{"legend"}
Aresult
11
00
14136 }
14137 3 $osm_args{scale} = 1;
14138 # Every <script> the map writes carries the page's nonce.
14139 3 $osm_args{nonce} = $self->csp_nonce;
14140 # Without a zoom the map opens on every marker, as the panel promises
14141 3 ▶ $osm_args{fit} = 1 unless defined $options->{zoom};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"zoom"} 3 0 3
14142 3 ▶ %osm_args = (%osm_args, %key) if @{ $key{legend} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$key{"legend"} // [];} 0 3 3
Condition50%1/2
$key{"legend"} // []
l!l
03
MC/DC0%0/1
$key{"legend"} // []
$key{"legend"}
Truth table50%1/2
A
A= $key{"legend"}
Aresult
11
00
14143  
14144 3 my $drawn = eval {
14145 3 my $map = HTML::OSM->new(%osm_args);
14146  
14147 # Bubble maps: one radius per marker, scaled across the plotted
14148 # values. Circle markers keep their pixel size as the reader
14149 # zooms, which is what makes them comparable.
14150 3 my ($size_min, $size_max) = @{$size_range};
14151  
14152 3 for my $marker (@markers) {
14153 3 my $row = $records->[ $marker->{first_row} ];
14154 3 my ($color, $group);
14155 3 ▶ if (length $color_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $color_col) 0 3 3
14156 0 ▶ my $cval = (defined($row) ? $row->{$color_col} : undef) // '';
Branch: 1 branch
BranchTrueFalseTotal
defined $row ? : 0 0 0
Condition0%0/2
(defined $row ? $row->{$color_col} : undef) // ""
l!l
00
MC/DC0%0/1
(defined $row ? $row->{$color_col} : undef) // ""
(defined $row ? $row->{$color_col} : undef)
Truth table0%0/2
A
A= (defined $row ? $row->{$color_col} : undef)
Aresult
11
00
14157 0 $color = _apply_color_rule($cval, $color_rules, $color_default);
14158 # Which key entry the marker belongs to, so the key lists
14159 # only what is in view
14160 0 my $index = _color_rule_index($cval, $color_rules);
14161 0 ▶ $group = defined($index) ? "rule$index" : 'other';
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 0 0
14162 # Kept for the outline round it, which is drawn to match
14163 0 $marker->{colour} = _color_hex($color);
14164 }
14165 3 ▶ if (length $size_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $size_col) { } 0 3 3
14166 my $sv = defined($row)
14167 0 ▶ ? _numeric_cell($row->{$size_col} // '') : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $row ? : 0 0 0
Condition0%0/2
$row->{$size_col} // ""
l!l
00
MC/DC0%0/1
$row->{$size_col} // ""
$row->{$size_col}
Truth table0%0/2
A
A= $row->{$size_col}
Aresult
11
00
14168 $map->add_circle_marker(
14169 [ $marker->{lat}, $marker->{lon} ],
14170 html => $marker->{label},
14171 0 ▶ radius => _map_radius($sv, $size_min, $size_max),
Branch: 2 branches
BranchTrueFalseTotal
defined $color ? : 0 0 0
defined $group ? : 0 0 0
14172 (defined($color) ? (colour => _color_hex($color)) : ()),
14173 (defined($group) ? (group => $group) : ()),
14174 );
14175 } else {
14176 $map->add_marker(
14177 [ $marker->{lat}, $marker->{lon} ],
14178 html => $marker->{label},
14179 3 ▶ (defined($color) ? (icon => _marker_icon($color)) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $color ? : 0 3 3
defined $group ? : 0 3 3
14180 (defined($group) ? (group => $group) : ()),
14181 );
14182 }
14183 }
14184  
14185 # Heat map overlay: when heat=1 is requested, plot a density
14186 # heatmap in addition to the regular markers. The weight column
14187 # (weight=<col>) drives the intensity of each point; absent the
14188 # column every point has equal weight 1.0. Normalisation to [0,1]
14189 # ensures the heatmap library gets values in the expected range.
14190 3 ▶ if ($options->{heat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"heat"}) 0 3 3
14191 0 ▶ my $weight_col = $options->{weight} // '';
Condition0%0/2
$options->{"weight"} // ""
l!l
00
MC/DC0%0/1
$options->{"weight"} // ""
$options->{"weight"}
Truth table0%0/2
A
A= $options->{"weight"}
Aresult
11
00
14192 0 my @heat_pts;
14193 0 for my $marker (@markers) {
14194 0 my $row = $records->[ $marker->{first_row} ];
14195 0 my $w;
14196 0 ▶ if (length $weight_col && defined $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $weight_col and defined $row) 0 0 0
Condition0%0/3
length $weight_col and defined $row
!ll&&!rl&&r
000
MC/DC0%0/2
length $weight_col and defined $row
length $weight_col defined $row
Truth table0%0/3
A and B
A= length $weight_colB= defined $row
ABresult
0X0
100
111
14197 0 ▶ $w = _numeric_cell($row->{$weight_col} // '');
Condition0%0/2
$row->{$weight_col} // ""
l!l
00
MC/DC0%0/1
$row->{$weight_col} // ""
$row->{$weight_col}
Truth table0%0/2
A
A= $row->{$weight_col}
Aresult
11
00
14198 }
14199 push @heat_pts, [ $marker->{lat}, $marker->{lon},
14200 0 ▶ (defined($w) ? $w : 1) ];
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 0 0 0
14201 }
14202 # Normalise weights to [0,1] when a weight column was used.
14203 0 ▶ if (length $weight_col && @heat_pts) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $weight_col and @heat_pts) 0 0 0
Condition0%0/3
length $weight_col and @heat_pts
!ll&&!rl&&r
000
MC/DC0%0/2
length $weight_col and @heat_pts
length $weight_col @heat_pts
Truth table0%0/3
A and B
A= length $weight_colB= @heat_pts
ABresult
0X0
100
111
14204 0 my $max_w = 0;
14205 0 ▶ $max_w = $_->[2] > $max_w ? $_->[2] : $max_w for @heat_pts;
Branch: 1 branch
BranchTrueFalseTotal
$_->[2] > $max_w ? : 0 0 0
14206 0 ▶ if ($max_w > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_w > 0) 0 0 0
14207 0 $_->[2] = $_->[2] / $max_w for @heat_pts;
14208 }
14209 }
14210 0 eval { $map->add_heatmap(\@heat_pts) };
14211 }
14212  
14213 # The county outlines, under the markers, with a checkbox on
14214 # the map to hide them (HTML::OSM 0.15; an older copy ignores
14215 # name and draws them always). Grey, or with heat=1 coloured
14216 # by what the heat layer weighs in each, one layer a colour
14217 # under one name, so one checkbox still hides them all.
14218 # When the markers are coloured, each outline takes the colours
14219 # of the markers in it, so the two never disagree; the heat
14220 # layer's own scale is for a map whose markers are all alike.
14221 3 my $noun = $self->_map_outline_noun($outlines, $bounds_level);
14222 3 ▶ my @outline_layers = length($color_col)
Branch: 1 branch
BranchTrueFalseTotal
length $color_col ? : 0 3 3
14223 ? $self->_map_outline_marker_layers($outlines, \@markers) : ();
14224 3 ▶ $bounds_as_markers = 1 if @outline_layers;
Branch: 1 branch
BranchTrueFalseTotal
if @outline_layers 0 3 3
14225 # Colour shade fills each outline by a column's total. It says
14226 # what the heat layer's line colours would, so those stay grey.
14227 3 ▶ my $shade_col = $options->{shade} // '';
Condition50%1/2
$options->{"shade"} // ""
l!l
03
MC/DC0%0/1
$options->{"shade"} // ""
$options->{"shade"}
Truth table50%1/2
A
A= $options->{"shade"}
Aresult
11
00
14228 3 ▶ $shade_col = '' if ref($shade_col) || !$col_set{$shade_col};
Branch: 1 branch
BranchTrueFalseTotal
if ref $shade_col or not $col_set{$shade_col} 3 0 3
Condition33%1/3
ref $shade_col or not $col_set{$shade_col}
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $shade_col or not $col_set{$shade_col}
ref $shade_col not $col_set{$shade_col}
Truth table33%1/3
A or not(B)
A= ref $shade_colB= not $col_set{$shade_col}
ABresult
1X1
011
000
14229 @outline_layers = $self->_map_outline_layers($outlines, \@markers, $records,
14230 3 ▶ (($options->{heat} && !length($shade_col)) ? ($options->{weight} // '') : undef), $noun)
Branch: 2 branches
BranchTrueFalseTotal
unless @outline_layers 3 0 3
$options->{'heat'} && !length($shade_col) ? : 0 3 3
Condition20%1/5
$options->{"heat"} and not length $shade_col
!ll&&!rl&&r
300
$options->{"weight"} // ""
l!l
00
MC/DC0%0/2
$options->{"heat"} and not length $shade_col
$options->{"heat"} not length $shade_col
MC/DC0%0/1
$options->{"weight"} // ""
$options->{"weight"}
Truth table33%1/3
A and not(B)
A= $options->{"heat"}B= not length $shade_col
ABresult
0X0
100
111
Truth table0%0/2
A
A= $options->{"weight"}
Aresult
11
00
14231 unless @outline_layers;
14232 3 ▶ if (length $shade_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $shade_col) 0 3 3
14233 # Coloured markers and no colour asked for: each outline is
14234 # shaded in its markers' colour. When the shade is of the
14235 # column the rules read, a "less than" rule's colour is the
14236 # stronger the smaller the total.
14237 0 my $lower;
14238 0 ▶ if ($bounds_as_markers && !defined($options->{shade_color})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bounds_as_markers and not defined $options->{"shade_color"}) 0 0 0
Condition0%0/3
$bounds_as_markers and not defined $options->{"shade_color"}
!ll&&!rl&&r
000
MC/DC0%0/2
$bounds_as_markers and not defined $options->{"shade_color"}
$bounds_as_markers not defined $options->{"shade_color"}
Truth table0%0/3
A and not(B)
A= $bounds_as_markersB= not defined $options->{"shade_color"}
ABresult
0X0
100
111
14239 0 $lower = {};
14240 0 ▶ for my $rule ($shade_col eq $color_col ? @{$color_rules} : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$shade_col eq $color_col ? : 0 0 0
14241 0 my $hue = _color_hex($rule->{color});
14242 0 $hue =~ s/\A#(.)(.)(.)\z/#$1$1$2$2$3$3/;
14243 0 ▶ $lower->{ lc $hue } //= (($rule->{op} // '') =~ /\Al[te]\z/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($rule->{'op'} // '') =~ /\Al[te]\z/u ? : 0 0 0
Condition0%0/4
$$lower{lc $hue} //= ($rule->{"op"} // "") =~ /\Al[te]\z/u ? 1 : 0
l!l
00
$rule->{"op"} // ""
l!l
00
MC/DC0%0/1
$$lower{lc $hue} //= ($rule->{"op"} // "") =~ /\Al[te]\z/u ? 1 : 0
$$lower{lc $hue}
Truth table0%0/2
A
A= $$lower{lc $hue}
Aresult
11
00
14244 }
14245 }
14246 my @shaded = $self->_map_outline_shade(\@outline_layers, $outlines, \@markers,
14247 0 $records, $shade_col, $options->{shade_color}, $noun, $lower);
14248 0 ▶ if (@shaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shaded) 0 0 0
14249 0 @outline_layers = @shaded;
14250 0 $bounds_shade = $shade_col;
14251 0 ▶ $bounds_shade_as = $lower ? 'markers' : 'one';
Branch: 1 branch
BranchTrueFalseTotal
$lower ? : 0 0 0
14252 }
14253 }
14254 3 for my $layer (@outline_layers) {
14255 # Only a layer with a key was coloured to mean something;
14256 # a shaded map's key is for its fills.
14257 0 ▶ $bounds_coloured = 1 if $layer->{key} && !length($bounds_shade);
Branch: 1 branch
BranchTrueFalseTotal
if $layer->{"key"} and not length $bounds_shade 0 0 0
Condition0%0/3
$layer->{"key"} and not length $bounds_shade
!ll&&!rl&&r
000
MC/DC0%0/2
$layer->{"key"} and not length $bounds_shade
$layer->{"key"} not length $bounds_shade
Truth table0%0/3
A and not(B)
A= $layer->{"key"}B= not length $bounds_shade
ABresult
0X0
100
111
14258 $map->add_geojson(
14259 { type => 'FeatureCollection', features => $layer->{features} },
14260 style => { color => $layer->{colour}, weight => $layer->{weight},
14261 opacity => $layer->{opacity},
14262 ($layer->{fill}
14263 ? (fill => \1, fillColor => $layer->{fill}, fillOpacity => $SHADE_OPACITY)
14264 : (fill => \0)) },
14265 popup => 'name',
14266 name => $self->_i18n('map_bounds_layer', ucfirst $noun),
14267 ($layer->{key} ? (key => $layer->{key}) : ()),
14268 # HTML::OSM 0.17 draws each colour as a line of its own,
14269 # inside the outline's edge; an older copy ignores this
14270 # and draws the first alone, along the edge.
14271 0 ▶ ($layer->{bands} ? (bands => 'colours') : ()),
Branch: 3 branches
BranchTrueFalseTotal
$layer->{'fill'} ? : 0 0 0
$layer->{'key'} ? : 0 0 0
$layer->{'bands'} ? : 0 0 0
14272 );
14273 }
14274  
14275 3 ($map_head, $map_body) = $map->onload_render();
14276 3 ▶ ($map_head, $map_body) = _canvas_map_html($map_head, $map_body, $id_suffix)
Branch: 1 branch
BranchTrueFalseTotal
if defined $id_suffix and length $id_suffix 3 0 3
Condition33%1/3
defined $id_suffix and length $id_suffix
!ll&&!rl&&r
003
MC/DC0%0/2
defined $id_suffix and length $id_suffix
defined $id_suffix length $id_suffix
Truth table33%1/3
A and B
A= defined $id_suffixB= length $id_suffix
ABresult
0X0
100
111
14277 if defined($id_suffix) && length($id_suffix);
14278 3 1;
14279 };
14280 3 ▶ unless ($drawn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($drawn) 0 3 3
14281 0 $self->app->log->error('The map could not be drawn: ' . _tidy_error($@));
14282 0 ($map_head, $map_body) = ();
14283 0 push @messages, {
14284 key => 'error_map_renderer_failed',
14285 text => $self->_i18n('error_map_renderer_failed'),
14286 };
14287 }
14288 } elsif (defined $osm_version) {
14289 0 push @messages, {
14290 key => 'error_map_renderer_old',
14291 text => $self->_i18n('error_map_renderer_old', $osm_version),
14292 };
14293 } else {
14294 0 push @messages, {
14295 key => 'error_map_renderer_missing',
14296 text => $self->_i18n('error_map_renderer_missing'),
14297 };
14298 }
14299 }
14300  
14301 5 ▶ my %chart_spec = (
Branch: 3 branches
BranchTrueFalseTotal
$by_location ? : 5 0 5
@loc_cols == 1 ? : 5 0 5
length $loc_text ? : 0 5 5
14302 kind => 'map',
14303 title => $title,
14304 encode => ($by_location
14305 ? { loc => (@loc_cols == 1 ? $loc_cols[0] : [ @loc_cols ]),
14306 (length($loc_text) ? (loc_text => $loc_text) : ()) }
14307 : { lat => $lat_col, lon => $lon_col }),
14308 );
14309 5 ▶ $chart_spec{options}{popup} = \@popup if @popup;
Branch: 1 branch
BranchTrueFalseTotal
if @popup 5 0 5
14310 $chart_spec{options}{max} = $options->{max}
14311 5 ▶ if defined($options->{max}) && $options->{max} =~ /\A\d+\z/ && $options->{max} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"max"} and $options->{"max"} =~ /\A\d+\z/u and $options->{"max"} > 0 0 5 5
Condition33%2/6
defined $options->{"max"} and $options->{"max"} =~ /\A\d+\z/u and $options->{"max"} > 0
!ll&&!rl&&r
500
defined $options->{"max"} and $options->{"max"} =~ /\A\d+\z/u
!ll&&!rl&&r
500
MC/DC0%0/3
defined $options->{"max"} and $options->{"max"} =~ /\A\d+\z/u and $options->{"max"} > 0
defined $options->{"max"} $options->{"max"} =~ /\A\d+\z/u $options->{"max"} > 0
Truth table25%1/4
A and B and C
A= defined $options->{"max"}B= $options->{"max"} =~ /\A\d+\z/uC= $options->{"max"} > 0
ABCresult
0XX0
10X0
1100
1111
14312 5 ▶ $chart_spec{options}{zoom} = $options->{zoom} if defined $options->{zoom};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"zoom"} 0 5 5
14313 5 ▶ $chart_spec{options}{heat} = 1 if $options->{heat};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"heat"} 0 5 5
14314 5 ▶ $chart_spec{options}{bounds} = $bounds_level if defined $bounds_level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bounds_level 0 5 5
14315 $chart_spec{options}{shade} = $options->{shade}
14316 5 ▶ if defined($options->{shade}) && !ref($options->{shade}) && length($options->{shade});
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"shade"} and not ref $options->{"shade"} and length $options->{"shade"} 0 5 5
Condition33%2/6
defined $options->{"shade"} and not ref $options->{"shade"} and length $options->{"shade"}
!ll&&!rl&&r
500
defined $options->{"shade"} and not ref $options->{"shade"}
!ll&&!rl&&r
500
MC/DC0%0/3
defined $options->{"shade"} and not ref $options->{"shade"} and length $options->{"shade"}
defined $options->{"shade"} not ref $options->{"shade"} length $options->{"shade"}
Truth table25%1/4
A and not(B) and C
A= defined $options->{"shade"}B= not ref $options->{"shade"}C= length $options->{"shade"}
ABCresult
0XX0
10X0
1100
1111
14317 $chart_spec{options}{shade_color} = $options->{shade_color}
14318 5 ▶ if defined($chart_spec{options}{shade}) && defined($options->{shade_color}) && !ref($options->{shade_color});
Branch: 1 branch
BranchTrueFalseTotal
if defined $chart_spec{"options"}{"shade"} and defined $options->{"shade_color"} and not ref $options->{"shade_color"} 0 5 5
Condition33%2/6
defined $chart_spec{"options"}{"shade"} and defined $options->{"shade_color"} and not ref $options->{"shade_color"}
!ll&&!rl&&r
500
defined $chart_spec{"options"}{"shade"} and defined $options->{"shade_color"}
!ll&&!rl&&r
500
MC/DC0%0/3
defined $chart_spec{"options"}{"shade"} and defined $options->{"shade_color"} and not ref $options->{"shade_color"}
defined $chart_spec{"options"}{"shade"} defined $options->{"shade_color"} not ref $options->{"shade_color"}
Truth table25%1/4
A and B and not(C)
A= defined $chart_spec{"options"}{"shade"}B= defined $options->{"shade_color"}C= not ref $options->{"shade_color"}
ABCresult
0XX0
10X0
1100
1111
14319 $chart_spec{options}{weight} = $options->{weight}
14320 5 ▶ if defined($options->{weight}) && length($options->{weight});
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"weight"} and length $options->{"weight"} 0 5 5
Condition33%1/3
defined $options->{"weight"} and length $options->{"weight"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $options->{"weight"} and length $options->{"weight"}
defined $options->{"weight"} length $options->{"weight"}
Truth table33%1/3
A and B
A= defined $options->{"weight"}B= length $options->{"weight"}
ABresult
0X0
100
111
14321 5 ▶ if (defined($options->{size}) && length($options->{size})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"size"} and length $options->{"size"}) 0 5 5
Condition33%1/3
defined $options->{"size"} and length $options->{"size"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $options->{"size"} and length $options->{"size"}
defined $options->{"size"} length $options->{"size"}
Truth table33%1/3
A and B
A= defined $options->{"size"}B= length $options->{"size"}
ABresult
0X0
100
111
14322 0 $chart_spec{encode}{size} = $options->{size};
14323 }
14324 5 ▶ if (defined($options->{scheme}) && length($options->{scheme})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"scheme"} and length $options->{"scheme"}) 0 5 5
Condition33%1/3
defined $options->{"scheme"} and length $options->{"scheme"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $options->{"scheme"} and length $options->{"scheme"}
defined $options->{"scheme"} length $options->{"scheme"}
Truth table33%1/3
A and B
A= defined $options->{"scheme"}B= length $options->{"scheme"}
ABresult
0X0
100
111
14325 0 $chart_spec{options}{scheme} = $options->{scheme};
14326 }
14327 5 ▶ if (defined($options->{color_col}) && length($options->{color_col})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"color_col"} and length $options->{"color_col"}) 0 5 5
Condition33%1/3
defined $options->{"color_col"} and length $options->{"color_col"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $options->{"color_col"} and length $options->{"color_col"}
defined $options->{"color_col"} length $options->{"color_col"}
Truth table33%1/3
A and B
A= defined $options->{"color_col"}B= length $options->{"color_col"}
ABresult
0X0
100
111
14328 0 $chart_spec{encode}{color} = $options->{color_col};
14329 $chart_spec{options}{color_rules} = $options->{color_rules}
14330 0 ▶ if defined($options->{color_rules}) && @{ $options->{color_rules} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"color_rules"} and @{$options->{"color_rules"};} 0 0 0
Condition0%0/3
defined $options->{"color_rules"} and @{$options->{"color_rules"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options->{"color_rules"} and @{$options->{"color_rules"};}
defined $options->{"color_rules"} @{$options->{"color_rules"};}
Truth table0%0/3
A and B
A= defined $options->{"color_rules"}B= @{$options->{"color_rules"};}
ABresult
0X0
100
111
14331 $chart_spec{options}{color_default} = $options->{color_default}
14332 0 ▶ if defined($options->{color_default});
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"color_default"} 0 0 0
14333 }
14334  
14335 return {
14336 title => $title,
14337 map_head => $map_head,
14338 map_body => $map_body,
14339 messages => \@messages,
14340 marker_count => scalar @markers,
14341 row_count => $plotted_rows,
14342 source_rows => scalar @$records,
14343 loc_col => ($by_location ? $loc_col : undef),
14344 loc_text => $loc_text,
14345 lat_col => ($by_location ? undef : $lat_col),
14346 lon_col => ($by_location ? undef : $lon_col),
14347 backend => $backend,
14348 chart => \%chart_spec,
14349 unplotted => \@unplotted,
14350 unplotted_filters => \@unplotted_filters,
14351 pending => \@pending,
14352 bounds_level => $bounds_level,
14353 bounds_pending => \@bounds_pending,
14354 bounds_drawn => ($outlines ? $outlines->{counts}{drawn} : 0),
14355 bounds_noun => $self->_map_outline_noun($outlines, $bounds_level),
14356 # Coloured by the heat layer's weight column, or its rows when it has
14357 # none; not when the outlines had nothing to compare and stayed grey.
14358 bounds_heat => $bounds_coloured,
14359 5 ▶ bounds_weight => ($bounds_coloured ? ($options->{weight} // '') : ''),
Branch: 5 branches
BranchTrueFalseTotal
$by_location ? : 5 0 5
$by_location ? : 5 0 5
$by_location ? : 5 0 5
$outlines ? : 0 5 5
$bounds_coloured ? : 0 5 5
Condition25%1/4
$options->{"weight"} // ""
l!l
00
$bounds_left_rows // 0
l!l
05
MC/DC0%0/1
$options->{"weight"} // ""
$options->{"weight"}
MC/DC0%0/1
$bounds_left_rows // 0
$bounds_left_rows
Truth table0%0/2
A
A= $options->{"weight"}
Aresult
11
00
Truth table50%1/2
A
A= $bounds_left_rows
Aresult
11
00
14360 bounds_as_markers => $bounds_as_markers,
14361 bounds_shade => $bounds_shade,
14362 bounds_shade_as => $bounds_shade_as,
14363 bounds_left_filters => \@bounds_left_filters,
14364 bounds_groups => \@bounds_groups,
14365 bounds_left_rows => $bounds_left_rows // 0,
14366 };
14367 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
14368  
14369 # _map_renderer_version() -> ($ok, $installed_version)
14370 #
14371 # Loads the optional HTML::OSM and says whether it is new enough for the options
14372 # _map_data passes. $installed_version is undef when the module could not be
14373 # loaded at all; when it is defined but $ok is false the copy is too old.
14374 #
14375 # This is a method rather than an inline require so a test can replace it.
14376 # Test::Without::Module does not produce the missing case here: its @INC hook
14377 # is bypassed during Mojolicious' in-process dispatch, so the installed copy is
14378 # still found, and the branch an operator without the dependency is in cannot
14379 # be reached by hiding the module. Replacing one method exercises the same
14380 # else branch, the same message and the same page.
14381 4 ▶sub _map_renderer_version {
Subroutine _map_renderer_version: called CC=4 SCAR=14.5
14382 4 my $self = shift;
14383  
14384 4 my $version = eval {
14385 4 require HTML::OSM;
14386 4 HTML::OSM->VERSION;
14387 };
14388 4 ▶ return (0, undef) unless defined($version) && length($version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and length $version 0 4 4
Condition33%1/3
defined $version and length $version
!ll&&!rl&&r
004
MC/DC0%0/2
defined $version and length $version
defined $version length $version
Truth table33%1/3
A and B
A= defined $versionB= length $version
ABresult
0X0
100
111
14389  
14390 4 my $new_enough = eval { HTML::OSM->VERSION($MIN_HTML_OSM_VERSION); 1 };
14391 4 ▶ return ($new_enough ? 1 : 0, $version);
Branch: 1 branch
BranchTrueFalseTotal
$new_enough ? : 4 0 4
14392 }
14393  
14394 # _canvas_map_html($head, $body, $id_suffix) -> ($head, $body)
14395 #
14396 # HTML::OSM names the map element "map" and its reset button "reset-button".
14397 # With search => 0 (always set by _map_data), the search-box and
14398 # clear-search-button elements are absent. Suffixing still happens so a
14399 # canvas with two maps gets distinct element ids and local variable scopes.
14400 # The head is the same Leaflet stylesheet and script for every map, so only
14401 # the first head is passed to the layout.
14402 # _scope_card_scripts($html) -> $html
14403 #
14404 # Purpose: Close every inline script in a rendered card over a function of its
14405 # own, so two cards on one canvas cannot collide.
14406 # Entry: $html -- one card's markup, as _render_visual_markup returns it.
14407 # Exit: The same markup, with each inline script wrapped.
14408 # Notes: Called once, by _render_visual_html, so no caller has to remember
14409 # it and no card is scoped twice. HTML::D3 writes a chart as a
14410 # snippet that declares its SVG with
14411 # C<const>. A top-level C<const> belongs to the page's shared lexical
14412 # environment rather than to the <script> element it was written in,
14413 # so a canvas of two charts failed at the second one -- "Identifier
14414 # 'svg' has already been declared" -- and drew an empty card, while
14415 # either card on a page of its own was fine. A script with a C<src>,
14416 # or with a C<type> of its own (a data block, a module), is left
14417 # alone, and one that is already wrapped is not wrapped twice.
14418 27 ▶sub _scope_card_scripts {
Subroutine _scope_card_scripts: called CC=3 SCAR=11.0
14419 27 my ($html) = @_;
14420 27 ▶ return '' unless defined $html && length $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html and length $html 2 25 27
Condition100%3/3
defined $html and length $html
!ll&&!rl&&r
1125
MC/DC100%2/2
defined $html and length $html
defined $html length $html
Truth table100%3/3
A and B
A= defined $htmlB= length $html
ABresult
0X0
100
111
14421  
14422 # ~ is the delimiter because the lookaheads below need !, and the wrapper
14423 # the replacement builds is a pair of braces, which a brace-delimited
14424 # pattern would take for its own.
14425 25 $html =~ s~(<script\b(?![^>]*\bsrc=)(?![^>]*\btype=)[^>]*>)(.*?)(</script>)~_wrap_card_script($1, $2, $3)~gsex;
14426  
14427 25 return $html;
14428 }
14429  
14430 # _wrap_card_script($open, $body, $close) -> $html
14431 #
14432 # Purpose: One <script> element of a card, closed over a function of its own.
14433 # Entry: The element's opening tag, its body and its closing tag.
14434 # Exit: The same element, wrapped unless it already is.
14435 # Notes: Split out of _scope_card_scripts so that pattern can use a plain
14436 # delimiter: its lookaheads need the ones the wrapper is made of.
14437 21 ▶sub _wrap_card_script {
Subroutine _wrap_card_script: called CC=2 SCAR=6.9
14438 21 my ($open, $body, $close) = @_;
14439  
14440 # A body that already opens with the wrapper is left alone, so scoping a
14441 # card twice is scoping it once.
14442 21 my ($blank) = $body =~ /\A(\s*)/;
14443 21 ▶ return $open . $body . $close
Branch: 1 branch
BranchTrueFalseTotal
if CORE::index(substr($body, length $blank), "(function () {") == 0 2 19 21
14444 if CORE::index(substr($body, length $blank), '(function () {') == 0;
14445  
14446 19 return $open . "\n(function () {\n" . $body . "\n})();\n" . $close;
14447 }
14448  
14449 7 ▶sub _canvas_map_html {
Subroutine _canvas_map_html: called CC=4 SCAR=13.9
14450 7 my ($head, $body, $id_suffix) = @_;
14451  
14452 7 ▶ my $suffix = $id_suffix // 'map';
Condition100%2/2
$id_suffix // "map"
l!l
61
MC/DC100%1/1
$id_suffix // "map"
$id_suffix
Truth table100%2/2
A
A= $id_suffix
Aresult
11
00
14453 7 $suffix =~ s/[^A-Za-z0-9_-]//g;
14454 7 ▶ $suffix = 'map' unless length $suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless length $suffix 1 6 7
14455  
14456 7 my %ids = (
14457 'map' => "map-$suffix",
14458 'search-box' => "search-box-$suffix",
14459 'reset-button' => "reset-button-$suffix",
14460 'clear-search-button' => "clear-search-button-$suffix",
14461 );
14462 7 for my $old (sort keys %ids) {
14463 28 my $new = $ids{$old};
14464 28 $body =~ s/id="\Q$old\E"/id="$new"/g;
14465 28 $body =~ s{getElementById\('\Q$old\E'\)}{getElementById('$new')}g;
14466 28 $body =~ s{L\.map\('\Q$old\E'\)}{L.map('$new')}g;
14467 }
14468 7 $body =~ s{<div id="$ids{map}"></div>}{<div id="$ids{map}" class="canvas-map"></div>};
14469  
14470 # The card owns the map's size, so the page-wide #map rule is noise here.
14471 7 $head =~ s{<style>.*?</style>}{}s;
14472  
14473 # The scope around the map's script is not put on here: every card is
14474 # scoped once, by _render_visual_html, whichever kind it is.
14475 7 return ($head, $body);
14476 }
14477  
14478 47 ▶sub map_view ($self) {
Subroutine map_view: called CC=112 SCAR=77.9
Pod documented
14479 47 my @loc_cols = _map_loc_columns($self->every_param('loc'));
14480 47 my $loc_col = join(', ', @loc_cols);
14481 47 ▶ my $lat_col = $self->param('lat') // '';
Condition100%2/2
$self->param("lat") // ""
l!l
443
MC/DC100%1/1
$self->param("lat") // ""
$self->param("lat")
Truth table100%2/2
A
A= $self->param("lat")
Aresult
11
00
14482 47 ▶ my $lon_col = $self->param('lon') // '';
Condition100%2/2
$self->param("lon") // ""
l!l
443
MC/DC100%1/1
$self->param("lon") // ""
$self->param("lon")
Truth table100%2/2
A
A= $self->param("lon")
Aresult
11
00
14483 47 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
047
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
14484 47 my $back2 = _safe_back_url($self->param('back2'));
14485 47 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
047
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
14486  
14487 # Optional display params
14488 47 ▶ my $zoom_raw = $self->param('zoom') // '';
Condition50%1/2
$self->param("zoom") // ""
l!l
047
MC/DC0%0/1
$self->param("zoom") // ""
$self->param("zoom")
Truth table50%1/2
A
A= $self->param("zoom")
Aresult
11
00
14489 47 ▶ my $max_zoom_raw = $self->param('max_zoom') // '';
Condition50%1/2
$self->param("max_zoom") // ""
l!l
047
MC/DC0%0/1
$self->param("max_zoom") // ""
$self->param("max_zoom")
Truth table50%1/2
A
A= $self->param("max_zoom")
Aresult
11
00
14490 47 ▶ my $tiles_name = $self->param('tiles') // '';
Condition50%1/2
$self->param("tiles") // ""
l!l
047
MC/DC0%0/1
$self->param("tiles") // ""
$self->param("tiles")
Truth table50%1/2
A
A= $self->param("tiles")
Aresult
11
00
14491 47 ▶ my $heat = $self->param('heat') // 0;
Condition50%1/2
$self->param("heat") // 0
l!l
047
MC/DC0%0/1
$self->param("heat") // 0
$self->param("heat")
Truth table50%1/2
A
A= $self->param("heat")
Aresult
11
00
14492 47 ▶ my $bounds_raw = $self->param('bounds') // '';
Condition50%1/2
$self->param("bounds") // ""
l!l
047
MC/DC0%0/1
$self->param("bounds") // ""
$self->param("bounds")
Truth table50%1/2
A
A= $self->param("bounds")
Aresult
11
00
14493 47 ▶ my $loc_text_raw = $self->param('loc_text') // '';
Condition50%1/2
$self->param("loc_text") // ""
l!l
047
MC/DC0%0/1
$self->param("loc_text") // ""
$self->param("loc_text")
Truth table50%1/2
A
A= $self->param("loc_text")
Aresult
11
00
14494 47 ▶ my $weight_col = $self->param('weight') // '';
Condition50%1/2
$self->param("weight") // ""
l!l
047
MC/DC0%0/1
$self->param("weight") // ""
$self->param("weight")
Truth table50%1/2
A
A= $self->param("weight")
Aresult
11
00
14495 47 ▶ my $shade_col = $self->param('shade') // '';
Condition50%1/2
$self->param("shade") // ""
l!l
047
MC/DC0%0/1
$self->param("shade") // ""
$self->param("shade")
Truth table50%1/2
A
A= $self->param("shade")
Aresult
11
00
14496 47 ▶ my $shade_color = _parse_color($self->param('shade_color') // '');
Condition50%1/2
$self->param("shade_color") // ""
l!l
047
MC/DC0%0/1
$self->param("shade_color") // ""
$self->param("shade_color")
Truth table50%1/2
A
A= $self->param("shade_color")
Aresult
11
00
14497 47 ▶ my $size_col = $self->param('size') // '';
Condition100%2/2
$self->param("size") // ""
l!l
245
MC/DC100%1/1
$self->param("size") // ""
$self->param("size")
Truth table100%2/2
A
A= $self->param("size")
Aresult
11
00
14498 47 ▶ my $series_col = $self->param('series') // '';
Condition50%1/2
$self->param("series") // ""
l!l
047
MC/DC0%0/1
$self->param("series") // ""
$self->param("series")
Truth table50%1/2
A
A= $self->param("series")
Aresult
11
00
14499 47 ▶ my $scheme = $self->param('scheme') // 'tableau10';
Condition100%2/2
$self->param("scheme") // "tableau10"
l!l
245
MC/DC100%1/1
$self->param("scheme") // "tableau10"
$self->param("scheme")
Truth table100%2/2
A
A= $self->param("scheme")
Aresult
11
00
14500  
14501 # tiles= names a source from tile_sources in config; an arbitrary URL is
14502 # never accepted so a crafted share link cannot redirect tiles fetches.
14503 47 my $tile_url_override;
14504 47 ▶ if (length $tiles_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tiles_name) 0 47 47
14505 0 ▶ my $sources = $self->app->config('tile_sources') // {};
Condition0%0/2
$self->app->config("tile_sources") // {}
l!l
00
MC/DC0%0/1
$self->app->config("tile_sources") // {}
$self->app->config("tile_sources")
Truth table0%0/2
A
A= $self->app->config("tile_sources")
Aresult
11
00
14506 0 ▶ unless (exists $sources->{$tiles_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sources->{$tiles_name}) 0 0 0
14507 0 return $self->render(
14508 text => $self->_i18n('error_map_tiles_unknown', $tiles_name),
14509 status => 400,
14510 );
14511 }
14512 0 $tile_url_override = $sources->{$tiles_name}{url};
14513 }
14514  
14515 47 my $zoom_val;
14516 47 ▶ if (length $zoom_raw && $zoom_raw =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $zoom_raw and $zoom_raw =~ /\A\d+\z/u) 0 47 47
Condition33%1/3
length $zoom_raw and $zoom_raw =~ /\A\d+\z/u
!ll&&!rl&&r
4700
MC/DC0%0/2
length $zoom_raw and $zoom_raw =~ /\A\d+\z/u
length $zoom_raw $zoom_raw =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= length $zoom_rawB= $zoom_raw =~ /\A\d+\z/u
ABresult
0X0
100
111
14517 0 $zoom_val = int $zoom_raw;
14518 0 ▶ $zoom_val = 0 if $zoom_val < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zoom_val < 0 0 0 0
14519 0 ▶ $zoom_val = 19 if $zoom_val > 19;
Branch: 1 branch
BranchTrueFalseTotal
if $zoom_val > 19 0 0 0
14520 }
14521  
14522 47 my $max_zoom_val;
14523 47 ▶ if (length $max_zoom_raw && $max_zoom_raw =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $max_zoom_raw and $max_zoom_raw =~ /\A\d+\z/u) { } 0 47 47
Condition33%1/3
length $max_zoom_raw and $max_zoom_raw =~ /\A\d+\z/u
!ll&&!rl&&r
4700
MC/DC0%0/2
length $max_zoom_raw and $max_zoom_raw =~ /\A\d+\z/u
length $max_zoom_raw $max_zoom_raw =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= length $max_zoom_rawB= $max_zoom_raw =~ /\A\d+\z/u
ABresult
0X0
100
111
14524 0 $max_zoom_val = int $max_zoom_raw;
14525 0 ▶ $max_zoom_val = 0 if $max_zoom_val < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_zoom_val < 0 0 0 0
14526 0 ▶ $max_zoom_val = 28 if $max_zoom_val > 28;
Branch: 1 branch
BranchTrueFalseTotal
if $max_zoom_val > 28 0 0 0
14527 } else {
14528 47 my $cfg_max_zoom = $self->app->config('map_max_zoom');
14529 47 ▶ $max_zoom_val = int($cfg_max_zoom)
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg_max_zoom and $cfg_max_zoom =~ /\A\d+\z/u 0 47 47
Condition33%1/3
defined $cfg_max_zoom and $cfg_max_zoom =~ /\A\d+\z/u
!ll&&!rl&&r
4700
MC/DC0%0/2
defined $cfg_max_zoom and $cfg_max_zoom =~ /\A\d+\z/u
defined $cfg_max_zoom $cfg_max_zoom =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= defined $cfg_max_zoomB= $cfg_max_zoom =~ /\A\d+\z/u
ABresult
0X0
100
111
14530 if defined($cfg_max_zoom) && $cfg_max_zoom =~ /\A\d+\z/;
14531 }
14532  
14533 47 my $by_location = length $loc_col;
14534  
14535 47 my ($loc_text) = _map_loc_text($loc_text_raw);
14536 47 ▶ return $self->render(text => $self->_i18n('error_map_loc_text', $Database::BI::Model::Geocode::PLACE_TEXT_MAX, $loc_text_raw), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loc_text 0 47 47
14537 unless defined $loc_text;
14538  
14539 47 my $bounds_level;
14540 47 ▶ if (length $bounds_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bounds_raw) 0 47 47
14541 0 $bounds_level = _map_bounds_level($bounds_raw);
14542 0 ▶ return $self->render(text => $self->_i18n('error_map_bounds_unknown',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bounds_level 0 0 0
14543 join(', ', Database::BI::Model::Boundary->levels), $bounds_raw), status => 400)
14544 unless defined $bounds_level;
14545 }
14546  
14547 47 ▶ return $self->render(text => $self->_i18n('error_map_source'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $by_location or length $lat_col or length $lon_col 42 5 47
Condition100%6/6
$by_location or length $lat_col or length $lon_col
l!l&&r!l&&!r
4142
$by_location or length $lat_col
l!l&&r!l&&!r
1343
MC/DC100%3/3
$by_location or length $lat_col or length $lon_col
$by_location length $lat_col length $lon_col
Truth table100%4/4
A or B or C
A= $by_locationB= length $lat_colC= length $lon_col
ABCresult
1XX1
01X1
0011
0000
14548 unless $by_location || length($lat_col) || length($lon_col);
14549 5 ▶ return $self->render(text => $self->_i18n('error_map_coordinates'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $by_location or length $lat_col and length $lon_col 2 3 5
Condition100%6/6
$by_location or length $lat_col and length $lon_col
l!l&&r!l&&!r
122
length $lat_col and length $lon_col
!ll&&!rl&&r
112
MC/DC100%3/3
$by_location or length $lat_col and length $lon_col
$by_location length $lat_col length $lon_col
Truth table100%4/4
A or B and C
A= $by_locationB= length $lat_colC= length $lon_col
ABCresult
1XX1
0111
00X0
0100
14550 if !$by_location && (!length($lat_col) || !length($lon_col));
14551  
14552 3 my ($src, $records, $columns) = $self->_run_pipeline_source;
14553 3 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 2 3
14554 unless defined $records;
14555  
14556 2 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
14557 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
14558 0 (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//;
14559 0 return $self->render(text => $e, status => 400);
14560 }
14561  
14562 # geocode= says when the places not in the cache yet are looked up: 'wait'
14563 # in this request, before the page is sent; 'cached' not at all, so the map
14564 # shows what the cache knows (where Cancel leads). Without it, a page a
14565 # browser navigated to (a GET whose Accept names text/html, which fetch()
14566 # and scripts do not send) asks nothing here: it lists the places for the
14567 # page, which looks them up one at a time with a progress bar. Any other
14568 # client waits, as every client did before.
14569 2 ▶ my $geocode_mode = $self->param('geocode') // '';
Condition50%1/2
$self->param("geocode") // ""
l!l
02
MC/DC0%0/1
$self->param("geocode") // ""
$self->param("geocode")
Truth table50%1/2
A
A= $self->param("geocode")
Aresult
11
00
14570 2 ▶ return $self->render(text => $self->_i18n('error_map_geocode_mode', $geocode_mode), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $geocode_mode =~ /\A(?:wait|cached|)\z/u 0 2 2
14571 unless $geocode_mode =~ /\A(?:wait|cached|)\z/;
14572 2 ▶ my $progress = ($by_location || defined($bounds_level)) && !length($geocode_mode)
Condition64%9/14
($by_location || defined $bounds_level) && !length($geocode_mode) && $self->req->method eq 'GET' && ($self->req->headers->accept // '') =~ m[\btext/html\b]u
!ll&&!rl&&r
110
$by_location || defined $bounds_level and not length $geocode_mode and $self->req->method eq "GET"
!ll&&!rl&&r
101
$by_location || defined $bounds_level and not length $geocode_mode
!ll&&!rl&&r
101
$by_location or defined $bounds_level
l!l&&r!l&&!r
101
$self->req->headers->accept // ""
l!l
01
MC/DC0%0/5
($by_location || defined $bounds_level) && !length($geocode_mode) && $self->req->method eq 'GET' && ($self->req->headers->accept // '') =~ m[\btext/html\b]u
$by_location defined $bounds_level not length $geocode_mode $self->req->method eq "GET" $self->req->headers->accept
Truth table22%2/9
A or B and not(C) and D && E
A= $by_locationB= defined $bounds_levelC= not length $geocode_modeD= $self->req->method eq "GET"E= $self->req->headers->accept
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
14573 && $self->req->method eq 'GET'
14574 && ($self->req->headers->accept // '') =~ m{\btext/html\b};
14575  
14576 2 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 2 2
14577  
14578 2 my %col_set = map { $_ => 1 } @{ $columns };
14579 2 my %loc_set = map { $_ => 1 } @loc_cols;
14580 0 ▶ my @popup = grep { length($_) && !$loc_set{$_} && $col_set{$_} }
Condition0%0/6
length $_ && !$loc_set{$_} && $col_set{$_}
!ll&&!rl&&r
000
length $_ and not $loc_set{$_}
!ll&&!rl&&r
000
MC/DC0%0/3
length $_ && !$loc_set{$_} && $col_set{$_}
length $_ not $loc_set{$_} $col_set{$_}
Truth table0%0/4
A and not(B) && C
A= length $_B= not $loc_set{$_}C= $col_set{$_}
ABCresult
0XX0
10X0
1100
1111
14581 2 @{ $self->every_param('popup') };
14582  
14583 # Marker colour params. color= applies explicit threshold rules;
14584 # series= colours one value per distinct category from scheme=, which is the
14585 # categorical form the other chart pages already offer.
14586 2 ▶ my $color_col = $self->param('color') // '';
Condition50%1/2
$self->param("color") // ""
l!l
02
MC/DC0%0/1
$self->param("color") // ""
$self->param("color")
Truth table50%1/2
A
A= $self->param("color")
Aresult
11
00
14587 2 ▶ my $color_def_raw = $self->param('color_default') // '';
Condition50%1/2
$self->param("color_default") // ""
l!l
02
MC/DC0%0/1
$self->param("color_default") // ""
$self->param("color_default")
Truth table50%1/2
A
A= $self->param("color_default")
Aresult
11
00
14588 2 my ($color_rules, $color_default) =
14589 _parse_color_rules($self->every_param('color_rule'), $color_def_raw);
14590  
14591 2 ▶ if (length($color_col) && !$col_set{$color_col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $color_col and not $col_set{$color_col}) 0 2 2
Condition33%1/3
length $color_col and not $col_set{$color_col}
!ll&&!rl&&r
200
MC/DC0%0/2
length $color_col and not $col_set{$color_col}
length $color_col not $col_set{$color_col}
Truth table33%1/3
A and not(B)
A= length $color_colB= not $col_set{$color_col}
ABresult
0X0
100
111
14592 0 return $self->render(text => "Column not found: $color_col", status => 400);
14593 }
14594 2 ▶ return $self->render(text => 'Use either color= or series=, not both', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if length $color_col and length $series_col 0 2 2
Condition33%1/3
length $color_col and length $series_col
!ll&&!rl&&r
200
MC/DC0%0/2
length $color_col and length $series_col
length $color_col length $series_col
Truth table33%1/3
A and B
A= length $color_colB= length $series_col
ABresult
0X0
100
111
14595 if length($color_col) && length($series_col);
14596 return $self->render(text => "Column not found: $size_col", status => 400)
14597 2 ▶ if length($size_col) && !$col_set{$size_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $size_col and not $col_set{$size_col} 0 2 2
Condition33%1/3
length $size_col and not $col_set{$size_col}
!ll&&!rl&&r
200
MC/DC0%0/2
length $size_col and not $col_set{$size_col}
length $size_col not $col_set{$size_col}
Truth table33%1/3
A and not(B)
A= length $size_colB= not $col_set{$size_col}
ABresult
0X0
100
111
14598  
14599 2 my (@series_cats, $series_palette);
14600 2 ▶ if (length $series_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $series_col) 0 2 2
14601 return $self->render(text => "Column not found: $series_col", status => 400)
14602 0 ▶ unless $col_set{$series_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_set{$series_col} 0 0 0
14603 return $self->render(text => "Unknown colour scheme: $scheme", status => 400)
14604 0 ▶ unless $SCATTER_SCHEMES{$scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless $SCATTER_SCHEMES{$scheme} 0 0 0
14605  
14606 0 ▶ $series_palette = $CATEGORY_PALETTES{$scheme} // $CATEGORY_PALETTES{tableau10};
Condition0%0/3
$CATEGORY_PALETTES{$scheme} // $CATEGORY_PALETTES{"tableau10"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$CATEGORY_PALETTES{$scheme} // $CATEGORY_PALETTES{"tableau10"}
$CATEGORY_PALETTES{$scheme} $CATEGORY_PALETTES{"tableau10"}
Truth table0%0/3
A // B
A= $CATEGORY_PALETTES{$scheme}B= $CATEGORY_PALETTES{"tableau10"}
ABresult
1X1
011
000
14607 0 my %seen;
14608 0 @series_cats = grep { !$seen{$_}++ } map {
14609 0 my $value = $_->{$series_col};
14610 0 ▶ defined($value) ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
14611 } @$records;
14612  
14613 # A legend longer than two cycles of the palette helps nobody; the
14614 # remaining values keep a palette colour but are not listed.
14615 0 my $limit = @$series_palette * 2;
14616 0 ▶ @series_cats = @series_cats[0 .. $limit - 1] if @series_cats > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @series_cats > $limit 0 0 0
14617  
14618 0 $color_col = $series_col;
14619 $color_rules = [
14620 map {
14621 0 {
14622 0 op => 'eq',
14623 val => $series_cats[$_],
14624 color => $series_palette->[ $_ % @$series_palette ],
14625 }
14626 } 0 .. $#series_cats
14627 ];
14628 0 $color_default = $series_palette->[0];
14629 }
14630  
14631 2 my %options;
14632 2 ▶ $options{popup} = \@popup if @popup;
Branch: 1 branch
BranchTrueFalseTotal
if @popup 0 2 2
14633 2 ▶ $options{color_col} = $color_col if length $color_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $color_col 0 2 2
14634 2 ▶ $options{color_rules} = $color_rules if @$color_rules;
Branch: 1 branch
BranchTrueFalseTotal
if @$color_rules 0 2 2
14635 2 $options{color_default} = $color_default;
14636 2 ▶ $options{size} = $size_col if length $size_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $size_col 0 2 2
14637 2 ▶ $options{scheme} = $scheme if length $series_col;
Branch: 1 branch
BranchTrueFalseTotal
if length $series_col 0 2 2
14638 2 my $max = $self->param('max');
14639 2 ▶ $options{max} = $max if defined($max) && $max =~ /\A\d+\z/ && $max > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $max =~ /\A\d+\z/u and $max > 0 0 2 2
Condition33%2/6
defined $max and $max =~ /\A\d+\z/u and $max > 0
!ll&&!rl&&r
200
defined $max and $max =~ /\A\d+\z/u
!ll&&!rl&&r
200
MC/DC0%0/3
defined $max and $max =~ /\A\d+\z/u and $max > 0
defined $max $max =~ /\A\d+\z/u $max > 0
Truth table25%1/4
A and B and C
A= defined $maxB= $max =~ /\A\d+\z/uC= $max > 0
ABCresult
0XX0
10X0
1100
1111
14640 2 ▶ $options{zoom} = $zoom_val if defined $zoom_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $zoom_val 0 2 2
14641 2 ▶ $options{max_zoom} = $max_zoom_val if defined $max_zoom_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_zoom_val 0 2 2
14642 2 ▶ $options{tile_url} = $tile_url_override if defined $tile_url_override;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tile_url_override 0 2 2
14643 2 ▶ $options{heat} = 1 if $heat;
Branch: 1 branch
BranchTrueFalseTotal
if $heat 0 2 2
14644 2 ▶ $options{bounds} = $bounds_level if defined $bounds_level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bounds_level 0 2 2
14645 2 ▶ $options{weight} = $weight_col if length($weight_col) && $col_set{$weight_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $weight_col and $col_set{$weight_col} 0 2 2
Condition33%1/3
length $weight_col and $col_set{$weight_col}
!ll&&!rl&&r
200
MC/DC0%0/2
length $weight_col and $col_set{$weight_col}
length $weight_col $col_set{$weight_col}
Truth table33%1/3
A and B
A= length $weight_colB= $col_set{$weight_col}
ABresult
0X0
100
111
14646 # Colour shade: a column to fill the outlines by, and the colour to shade
14647 2 ▶ $options{shade} = $shade_col if length($shade_col) && $col_set{$shade_col};
Branch: 1 branch
BranchTrueFalseTotal
if length $shade_col and $col_set{$shade_col} 0 2 2
Condition33%1/3
length $shade_col and $col_set{$shade_col}
!ll&&!rl&&r
200
MC/DC0%0/2
length $shade_col and $col_set{$shade_col}
length $shade_col $col_set{$shade_col}
Truth table33%1/3
A and B
A= length $shade_colB= $col_set{$shade_col}
ABresult
0X0
100
111
14648 2 ▶ $options{shade_color} = $shade_color if defined($shade_color) && defined($options{shade});
Branch: 1 branch
BranchTrueFalseTotal
if defined $shade_color and defined $options{"shade"} 0 2 2
Condition33%1/3
defined $shade_color and defined $options{"shade"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $shade_color and defined $options{"shade"}
defined $shade_color defined $options{"shade"}
Truth table33%1/3
A and B
A= defined $shade_colorB= defined $options{"shade"}
ABresult
0X0
100
111
14649 2 ▶ $options{lookup} = 0 if $progress || $geocode_mode eq 'cached';
Branch: 1 branch
BranchTrueFalseTotal
if $progress or $geocode_mode eq "cached" 0 2 2
Condition33%1/3
$progress or $geocode_mode eq "cached"
l!l&&r!l&&!r
002
MC/DC0%0/2
$progress or $geocode_mode eq "cached"
$progress $geocode_mode eq "cached"
Truth table33%1/3
A or B
A= $progressB= $geocode_mode eq "cached"
ABresult
1X1
011
000
14650  
14651 # Each place that could not be plotted, and each marker with more rows
14652 # than its popup lists, links to its rows in the table, as a bar drills
14653 # into its category: the table this map came from, with
14654 # the place's filters added, and back2= to return here.
14655 2 ▶ my $rows_base = ($back =~ m{\A(?:https?://[^/]+)?/(?:open\?|view/)}) ? $back
Branch: 1 branch
BranchTrueFalseTotal
$back =~ m[\A(?:https?://[^/]+)?/(?:open\?|view/)]u ? : 0 2 2
Condition50%1/2
$self->param("l") // ""
l!l
20
MC/DC0%0/1
$self->param("l") // ""
$self->param("l")
Truth table50%1/2
A
A= $self->param("l")
Aresult
11
00
14656 : $self->_spec_to_url($self->param('l') // '');
14657 2 my $here = $self->req->url->path_query;
14658 0 ▶ my $rows_url = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
14659 0 my ($filters) = @_;
14660 0 ▶ return '' if $rows_base eq '/' || !@{$filters};
Branch: 1 branch
BranchTrueFalseTotal
if $rows_base eq "/" or not @{$filters;} 0 0 0
Condition0%0/3
$rows_base eq "/" or not @{$filters;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rows_base eq "/" or not @{$filters;}
$rows_base eq "/" not @{$filters;}
Truth table0%0/3
A or not(B)
A= $rows_base eq "/"B= not @{$filters;}
ABresult
1X1
011
000
14661 0 ▶ my $sep = ($rows_base =~ /\?/) ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$rows_base =~ /\?/u ? : 0 0 0
14662 return $rows_base . $sep . join('&',
14663 0 (map { 'f=' . url_escape(encode('UTF-8', $_)) } @{$filters}),
14664 'back2=' . url_escape($here),
14665 'back2_label=' . url_escape('Back to map'),
14666 );
14667 2 };
14668 2 $options{rows_url} = $rows_url;
14669  
14670 2 ▶ my $result = $self->_map_data(
Branch: 1 branch
BranchTrueFalseTotal
$by_location ? : 1 1 2
Condition50%1/2
$self->param("l") // ""
l!l
20
MC/DC0%0/1
$self->param("l") // ""
$self->param("l")
Truth table50%1/2
A
A= $self->param("l")
Aresult
11
00
14671 $records, $columns,
14672 ($by_location ? { loc => [ @loc_cols ], loc_text => $loc_text } : { lat => $lat_col, lon => $lon_col }),
14673 \%options,
14674 $self->param('l') // '',
14675 undef,
14676 );
14677  
14678 return $self->render(text => $result->{error}, status => $result->{status})
14679 2 ▶ if $result->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"error"} 2 0 2
14680  
14681 0 my ($source_url, $source_accessed) = $self->_url_attribution;
14682 0 my ($platform, $language) = $self->_resolve_template;
14683  
14684 my @unplotted = map {
14685 0 my $entry = { %{$_} };
14686 0 $entry->{url} = $rows_url->($entry->{filters});
14687 0 $entry;
14688 0 ▶ } @{ $result->{unplotted} // [] };
Condition0%0/2
$result->{"unplotted"} // []
l!l
00
MC/DC0%0/1
$result->{"unplotted"} // []
$result->{"unplotted"}
Truth table0%0/2
A
A= $result->{"unplotted"}
Aresult
11
00
14689 0 my $unplotted_rows = 0;
14690 0 $unplotted_rows += $_->{rows} for @unplotted;
14691  
14692 # This page with geocode= set to $mode, or without it for undef.
14693 0 ▶ my $here_with = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
14694 0 my ($mode) = @_;
14695 0 my $url = $self->req->url->clone;
14696 0 $url->query->remove('geocode');
14697 0 ▶ $url->query->append(geocode => $mode) if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 0 0
14698 0 return $url->path_query;
14699 0 };
14700  
14701 # The places to look up, each signed so that /geo/lookup asks only what a
14702 # map page listed: without that, any page on the web could post a string
14703 # for this server to send to OpenStreetMap.
14704 0 ▶ my @pending = $progress ? @{ $result->{pending} // [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$progress ? : 0 0 0
Condition0%0/2
$result->{"pending"} // []
l!l
00
MC/DC0%0/1
$result->{"pending"} // []
$result->{"pending"}
Truth table0%0/2
A
A= $result->{"pending"}
Aresult
11
00
14705 # The points whose outline is not known yet go on the same list,
14706 # after the places, each signed with its level for /geo/boundary.
14707 0 ▶ my @bounds_pending = $progress ? @{ $result->{bounds_pending} // [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$progress ? : 0 0 0
Condition0%0/2
$result->{"bounds_pending"} // []
l!l
00
MC/DC0%0/1
$result->{"bounds_pending"} // []
$result->{"bounds_pending"}
Truth table0%0/2
A
A= $result->{"bounds_pending"}
Aresult
11
00
14708 0 my @messages = @{ $result->{messages} };
14709 0 ▶ @messages = grep { $_->{key} ne 'warn_map_bounds_pending' } @messages if @bounds_pending;
Branch: 1 branch
BranchTrueFalseTotal
if @bounds_pending 0 0 0
14710 0 my %progress;
14711 0 ▶ if (@pending || @bounds_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending or @bounds_pending) 0 0 0
Condition0%0/3
@pending or @bounds_pending
l!l&&r!l&&!r
000
MC/DC0%0/2
@pending or @bounds_pending
@pending @bounds_pending
Truth table0%0/3
A or B
A= @pendingB= @bounds_pending
ABresult
1X1
011
000
14712 my $json = to_json([
14713 0 (map { { place => $_, sig => $self->_geo_lookup_sig($_) } } @pending),
14714 0 (map { {
14715 point => $_,
14716 level => $result->{bounds_level},
14717 0 sig => $self->_geo_boundary_sig($result->{bounds_level}, $_),
14718 } } @bounds_pending),
14719 ]);
14720 0 $json =~ s{</}{<\\/}g;
14721 0 %progress = (
14722 geocode_pending => scalar(@pending) + scalar(@bounds_pending),
14723 geocode_places => scalar @pending,
14724 geocode_outlines => scalar @bounds_pending,
14725 geocode_pending_json => $json,
14726 geocode_done_url => $here_with->(undef),
14727 geocode_cached_url => $here_with->('cached'),
14728 geocode_wait_url => $here_with->('wait'),
14729 );
14730 }
14731  
14732 # After Cancel, the places left are offered again.
14733 $progress{geocode_retry_url} = $here_with->(undef)
14734 0 ▶ if $geocode_mode eq 'cached' && grep { $_->{reason} eq 'later' } @unplotted;
Branch: 1 branch
BranchTrueFalseTotal
if $geocode_mode eq "cached" and grep {$_->{"reason"} eq "later";} @unplotted 0 0 0
Condition0%0/3
$geocode_mode eq "cached" and grep {$_->{"reason"} eq "later";} @unplotted
!ll&&!rl&&r
000
MC/DC0%0/2
$geocode_mode eq "cached" and grep {$_->{"reason"} eq "later";} @unplotted
$geocode_mode eq "cached" grep {$_->{"reason"} eq "later";} @unplotted
Truth table0%0/3
A and B
A= $geocode_mode eq "cached"B= grep {$_->{"reason"} eq "later";} @unplotted
ABresult
0X0
100
111
14735  
14736 $self->render(
14737 handler => 'tt',
14738 template => "$platform/$language/map",
14739 %progress,
14740 unplotted => \@unplotted,
14741 unplotted_rows => $unplotted_rows,
14742 unplotted_url => $rows_url->($result->{unplotted_filters} // []),
14743 # Where the outlines and the markers do not pair off, each with a link
14744 # to its rows
14745 0 ▶ bounds_groups => [ map { +{ %{$_}, url => $rows_url->($_->{filters}) } } @{ $result->{bounds_groups} // [] } ],
Condition0%0/4
$result->{"bounds_groups"} // []
l!l
00
$result->{"bounds_left_filters"} // []
l!l
00
MC/DC0%0/1
$result->{"bounds_groups"} // []
$result->{"bounds_groups"}
MC/DC0%0/1
$result->{"bounds_left_filters"} // []
$result->{"bounds_left_filters"}
Truth table0%0/2
A
A= $result->{"bounds_groups"}
Aresult
11
00
Truth table0%0/2
A
A= $result->{"bounds_left_filters"}
Aresult
11
00
14746 # The rows whose place has no outline, and this page again to
14747 # look those outlines up (as it was opened: waiting, or with progress)
14748 bounds_left_url => $rows_url->($result->{bounds_left_filters} // []),
14749 bounds_left_rows => $result->{bounds_left_rows},
14750 0 ▶ bounds_left => scalar(@{ $result->{bounds_pending} // [] }),
Branch: 4 branches
BranchTrueFalseTotal
$geocode_mode eq 'wait' ? : 0 0 0
@loc_cols > 1 ? : 0 0 0
$back2 ? : 0 0 0
$back2 ? : 0 0 0
Condition0%0/7
$result->{"bounds_pending"} // []
l!l
00
$back2 // $back
l!l&&r!l&&!r
000
$back2_lbl or "Back"
l!l
00
MC/DC0%0/1
$result->{"bounds_pending"} // []
$result->{"bounds_pending"}
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
Truth table0%0/2
A
A= $result->{"bounds_pending"}
Aresult
11
00
Truth table0%0/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
14751 bounds_retry_url => $here_with->($geocode_mode eq 'wait' ? 'wait' : undef),
14752 format => 'html',
14753 title => $result->{title},
14754 extra_head => $result->{map_head},
14755 map_body => $result->{map_body},
14756 messages => \@messages,
14757 marker_count => $result->{marker_count},
14758 bounds_drawn => $result->{bounds_drawn},
14759 bounds_heat => $result->{bounds_heat},
14760 bounds_noun => $result->{bounds_noun},
14761 bounds_as_markers => $result->{bounds_as_markers},
14762 bounds_shade => $result->{bounds_shade},
14763 bounds_shade_as => $result->{bounds_shade_as},
14764 bounds_weight => $result->{bounds_weight},
14765 row_count => $result->{row_count},
14766 source_rows => $result->{source_rows},
14767 loc_col => $result->{loc_col},
14768 loc_many => (@loc_cols > 1 ? 1 : 0),
14769 loc_text => $result->{loc_text},
14770 lat_col => $result->{lat_col},
14771 lon_col => $result->{lon_col},
14772 backend => $result->{backend},
14773 chart => $result->{chart},
14774 0 ▶ chart_json => $self->_chart_json($result->{chart}),
Condition0%0/2
$result->{"unplotted_filters"} // []
l!l
00
MC/DC0%0/1
$result->{"unplotted_filters"} // []
$result->{"unplotted_filters"}
Truth table0%0/2
A
A= $result->{"unplotted_filters"}
Aresult
11
00
14775 back_url => ($back2 // $back),
14776 back_label => ($back2 ? ($back2_lbl || 'Back') : 'Back to table'),
14777 ($back2 ? (back2_url => $back, back2_label => 'Back to table') : ()),
14778 source_url => $source_url,
14779 source_accessed => $source_accessed,
14780 language => $language,
14781 );
14782 }
14783  
14784 # _geo_lookup_sig($self, $place) -> $hex
14785 #
14786 # What the map page signs each place it lists with, and /geo/lookup checks.
14787 # The key is the application's secret, which BI.pm makes at random for each
14788 # start unless the config names one, so a list outlives neither a restart nor
14789 # another installation.
14790 14 ▶sub _geo_lookup_sig :Protected ($self, $place) {
Subroutine _geo_lookup_sig: called CC=1 SCAR=0.0
14791 14 return hmac_sha1_sum(encode('UTF-8', "geo-lookup\0$place"), $self->app->secrets->[0]);
14792 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
14793  
14794 # _geo_boundary_sig($self, $level, $point) -> $hex
14795 #
14796 # The same for a point whose outline the map page lists. Its own prefix, so a
14797 # place's signature is never a point's.
14798 0 ▶sub _geo_boundary_sig :Protected ($self, $level, $point) {
Subroutine _geo_boundary_sig: not called CC=1 SCAR=6.9
14799 0 return hmac_sha1_sum(encode('UTF-8', "geo-boundary\0$level\0$point"), $self->app->secrets->[0]);
14800 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
14801  
14802 # ---------------------------------------------------------------------------
14803 # POST /geo/boundary point=<lat,lon>&level=<level>&sig=<sig>[&online=0]
14804 #
14805 # One point's outline, for the map page's progress bar: found as the map would
14806 # find it, and remembered at once, so a run that is cancelled keeps what it
14807 # found. The point must carry the signature the map page gave it, or any page
14808 # on the web could have this server send coordinates to OpenStreetMap.
14809 # online=0 asks nothing, after OpenStreetMap has failed once.
14810 #
14811 # Replies 200 JSON:
14812 # { point, status => 'found' | 'nowhere' | 'held', stored => 0|1,
14813 # online_failed => 0|1, name }
14814 # 'nowhere' is a point in no area of the level (at sea); 'held' could not be settled now
14815 # and is asked again next time.
14816 # 400 JSON { error } without point= or sig=, or with an unknown level=;
14817 # 403 JSON { error } for a bad sig=.
14818 # ---------------------------------------------------------------------------
14819 0 ▶sub geo_boundary ($self) {
Subroutine geo_boundary: not called CC=14 SCAR=53.5
Pod undocumented
14820 0 ▶ my $point = $self->param('point') // '';
Condition0%0/2
$self->param("point") // ""
l!l
00
MC/DC0%0/1
$self->param("point") // ""
$self->param("point")
Truth table0%0/2
A
A= $self->param("point")
Aresult
11
00
14821 0 ▶ my $sig = $self->param('sig') // '';
Condition0%0/2
$self->param("sig") // ""
l!l
00
MC/DC0%0/1
$self->param("sig") // ""
$self->param("sig")
Truth table0%0/2
A
A= $self->param("sig")
Aresult
11
00
14822 0 ▶ my $level = _map_bounds_level($self->param('level') // 'county');
Condition0%0/2
$self->param("level") // "county"
l!l
00
MC/DC0%0/1
$self->param("level") // "county"
$self->param("level")
Truth table0%0/2
A
A= $self->param("level")
Aresult
11
00
14823  
14824 0 ▶ return $self->render(json => { error => $self->_i18n('error_geo_boundary_point') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $point and length $sig and defined $level 0 0 0
Condition0%0/6
length $point and length $sig and defined $level
!ll&&!rl&&r
000
length $point and length $sig
!ll&&!rl&&r
000
MC/DC0%0/3
length $point and length $sig and defined $level
length $point length $sig defined $level
Truth table0%0/4
A and B and C
A= length $pointB= length $sigC= defined $level
ABCresult
0XX0
10X0
1100
1111
14825 unless length($point) && length($sig) && defined($level);
14826 0 ▶ return $self->render(json => { error => $self->_i18n('error_geo_boundary_unsigned') }, status => 403)
Branch: 1 branch
BranchTrueFalseTotal
unless secure_compare($sig, $self->_geo_boundary_sig($level, $point)) 0 0 0
14827 unless secure_compare($sig, $self->_geo_boundary_sig($level, $point));
14828  
14829 0 ▶ my $online = ($self->param('online') // 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('online') // 1 ? : 0 0 0
Condition0%0/2
$self->param("online") // 1
l!l
00
MC/DC0%0/1
$self->param("online") // 1
$self->param("online")
Truth table0%0/2
A
A= $self->param("online")
Aresult
11
00
14830 0 my $answer = eval { $self->_map_boundary($level, $online)->lookup_point($point) };
14831 0 ▶ return $self->render(json => { error => $self->_i18n('error_geo_boundary_point') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $answer 0 0 0
14832 unless $answer;
14833  
14834 return $self->render(json => {
14835 point => $point,
14836 status => $answer->{status},
14837 stored => ($answer->{status} eq 'held' ? 0 : 1),
14838 online_failed => (($answer->{status} eq 'held' && $answer->{asked}) ? 1 : 0),
14839 name => $answer->{name},
14840 0 ▶ });
Branch: 2 branches
BranchTrueFalseTotal
$answer->{'status'} eq 'held' ? : 0 0 0
$answer->{'status'} eq 'held' && $answer->{'asked'} ? : 0 0 0
Condition0%0/3
$answer->{"status"} eq "held" and $answer->{"asked"}
!ll&&!rl&&r
000
MC/DC0%0/2
$answer->{"status"} eq "held" and $answer->{"asked"}
$answer->{"status"} eq "held" $answer->{"asked"}
Truth table0%0/3
A and B
A= $answer->{"status"} eq "held"B= $answer->{"asked"}
ABresult
0X0
100
111
14841 }
14842  
14843 # ---------------------------------------------------------------------------
14844 # POST /geo/lookup place=<place>&sig=<sig>[&online=0]
14845 #
14846 # One place, for the map page's progress bar: looked up as the map would, and
14847 # its answer saved in the shared place cache straight away, so a run that is
14848 # cancelled or interrupted keeps every place it found. The place must carry the
14849 # signature the map page gave it. online=0 keeps the lookup on this machine;
14850 # the page sends it once OpenStreetMap has failed, so that each place left does
14851 # not wait for it again.
14852 #
14853 # Replies 200 JSON:
14854 # { place, status => 'found' | 'not_found' | 'held' | 'refused',
14855 # stored => 0|1, online_failed => 0|1, messages => [ text ... ] }
14856 # 'held' is a place that could not be looked up now (OpenStreetMap did not
14857 # answer, or the geocoder is missing); it is not remembered, so the next map
14858 # asks again. stored says that the answer is in the cache: a found or
14859 # not-found place the store refused to keep is 0.
14860 # 400 JSON { error } without place= or sig=; 403 JSON { error } for a bad sig=.
14861 # ---------------------------------------------------------------------------
14862 7 ▶sub geo_lookup ($self) {
Subroutine geo_lookup: called CC=20 SCAR=32.0
Pod documented
14863 7 ▶ my $place = $self->param('place') // '';
Condition100%2/2
$self->param("place") // ""
l!l
61
MC/DC100%1/1
$self->param("place") // ""
$self->param("place")
Truth table100%2/2
A
A= $self->param("place")
Aresult
11
00
14864 7 ▶ my $sig = $self->param('sig') // '';
Condition100%2/2
$self->param("sig") // ""
l!l
52
MC/DC100%1/1
$self->param("sig") // ""
$self->param("sig")
Truth table100%2/2
A
A= $self->param("sig")
Aresult
11
00
14865  
14866 7 ▶ return $self->render(json => { error => $self->_i18n('error_geo_lookup_place') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $place and length $sig 2 5 7
Condition100%3/3
length $place and length $sig
!ll&&!rl&&r
115
MC/DC100%2/2
length $place and length $sig
length $place length $sig
Truth table100%3/3
A and B
A= length $placeB= length $sig
ABresult
0X0
100
111
14867 unless length($place) && length($sig);
14868 5 ▶ return $self->render(json => { error => $self->_i18n('error_geo_lookup_unsigned') }, status => 403)
Branch: 1 branch
BranchTrueFalseTotal
unless secure_compare($sig, $self->_geo_lookup_sig($place)) 2 3 5
14869 unless secure_compare($sig, $self->_geo_lookup_sig($place));
14870  
14871 3 my $cfg = $self->app->config;
14872 3 ▶ my $geocode = $self->_map_geocoder(($self->param('online') // 1) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->param('online') // 1 ? : 3 0 3
Condition50%1/2
$self->param("online") // 1
l!l
03
MC/DC0%0/1
$self->param("online") // 1
$self->param("online")
Truth table50%1/2
A
A= $self->param("online")
Aresult
11
00
14873 3 my $models = $self->_read_models;
14874 3 my $region = _map_region($cfg);
14875  
14876 3 ▶ my $result = $geocode->resolve_values(
Branch: 1 branch
BranchTrueFalseTotal
defined $region ? : 0 3 3
14877 [ $place ],
14878 cache => Database::BI::Model::Geocode->shared_cache_from_models($models),
14879 (defined($region) ? (region => $region) : ()),
14880 max => 1,
14881 );
14882  
14883 my $status = $result->{points}{$place} ? 'found'
14884 1 ▶ : (grep { $_ eq $place } @{ $result->{unresolved} }) ? 'not_found'
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $place;} @{$$result{'skipped'};}) ? : 0 0 0
14885 0 ▶ : (grep { $_ eq $place } @{ $result->{skipped} }) ? 'refused'
Branch: 2 branches
BranchTrueFalseTotal
$result->{'points'}{$place} ? : 2 1 3
grep({$_ eq $place;} @{$$result{'unresolved'};}) ? : 1 0 1
14886 : 'held';
14887  
14888 # A place another tab answered meanwhile is in the cache already.
14889 3 ▶ my $stored = ($status eq 'found' || $status eq 'not_found') && !$result->{counts}{attempted};
Condition66%4/6
($status eq 'found' || $status eq 'not_found') && !$result->{'counts'}{'attempted'}
!ll&&!rl&&r
021
$status eq "found" or $status eq "not_found"
l!l&&r!l&&!r
210
MC/DC33%1/3
($status eq 'found' || $status eq 'not_found') && !$result->{'counts'}{'attempted'}
$status eq "found" $status eq "not_found" !$result->{'counts'}{'attempted'}
Truth table60%3/5
A or B && not(C)
A= $status eq "found"B= $status eq "not_found"C= !$result->{'counts'}{'attempted'}
ABCresult
00X0
1X00
0100
1X11
0111
14890  
14891 3 ▶ if ($result->{counts}{attempted} && $status ne 'held') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"counts"}{"attempted"} and $status ne "held") 2 1 3
Condition66%2/3
$result->{"counts"}{"attempted"} and $status ne "held"
!ll&&!rl&&r
102
MC/DC50%1/2
$result->{"counts"}{"attempted"} and $status ne "held"
$result->{"counts"}{"attempted"} $status ne "held"
Truth table66%2/3
A and B
A= $result->{"counts"}{"attempted"}B= $status ne "held"
ABresult
0X0
100
111
14892 2 my $backend = $result->{backend};
14893 $stored = $self->_write_models(Database::BI::Model::Geocode->models_with_shared_cache(
14894 2 ▶ $models, { $place => $result->{cache}{$place} },
Branch: 2 branches
BranchTrueFalseTotal
$self->_write_models('Database::BI::Model::Geocode'->models_with_shared_cache($models, {$place, $result->{'cache'}{$place}}, defined $backend ? ('backend', $backend) : (), 'built', _iso8601_utc())) ? : 2 0 2
defined $backend ? : 1 1 2
14895 (defined($backend) ? (backend => $backend) : ()),
14896 built => _iso8601_utc(),
14897 )) ? 1 : 0;
14898 }
14899  
14900 0 my @messages = map { $_->{text} } grep {
14901 2 $_->{key} ne 'warn_map_geocoder_unavailable_data'
14902 3 } @{ $result->{messages} };
14903  
14904 return $self->render(json => {
14905 place => $place,
14906 status => $status,
14907 stored => $stored ? 1 : 0,
14908 3 ▶ online_failed => (grep { $_->{key} eq 'warn_map_geocoder_online_failed' } @{ $result->{messages} }) ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$stored ? : 3 0 3
grep({$_->{'key'} eq 'warn_map_geocoder_online_failed';} @{$$result{'messages'};}) ? : 0 3 3
14909 messages => \@messages,
14910 });
14911 }
14912  
14913 # _map_popup($heading, \@rows, \@columns, $limit, $more_url) -> $html
14914 #
14915 # The popup for one marker: what it is, how many rows it stands for, and the
14916 # rows themselves. A value every row shares is said once, as "Column: value";
14917 # the columns that differ are a small table of at most $limit rows, in a box
14918 # that scrolls, so a place with many rows does not cover the map. The rest
14919 # are counted, with a link to them in the table when $more_url is given.
14920 # Leaflet treats a popup string as HTML, and a value out of a user's file is
14921 # not markup, so every value is escaped here.
14922 19 ▶sub _map_popup {
Subroutine _map_popup: called CC=15 SCAR=27.1
14923 19 my ($heading, $rows, $columns, $limit, $more_url) = @_;
14924  
14925 19 ▶ my @rows = grep { ref($_) eq 'HASH' } @{ ref($rows) eq 'ARRAY' ? $rows : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 18 1 19
14926 19 ▶ my @cols = grep { defined($_) && !ref($_) && length($_) } @{ ref($columns) eq 'ARRAY' ? $columns : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $columns eq 'ARRAY' ? : 18 1 19
Condition83%5/6
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
2022
defined $_ and not ref $_
!ll&&!rl&&r
1122
MC/DC66%2/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
Truth table75%3/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
14927 19 $limit = _map_limit($limit, $DEFAULT_MAP_POPUP_ROWS);
14928 19 ▶ my $cell = sub { my $v = $_[0]->{ $_[1] }; (defined($v) && !ref($v)) ? $v : '' };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) ? : 301 5 306
Condition100%3/3
defined $v and not ref $v
!ll&&!rl&&r
32301
MC/DC100%2/2
defined $v and not ref $v
defined $v not ref $v
Truth table100%3/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
14929  
14930 19 ▶ my @lines = '<strong>' . xml_escape($heading // '') . '</strong>';
Condition100%2/2
$heading // ""
l!l
181
MC/DC100%1/1
$heading // ""
$heading
Truth table100%2/2
A
A= $heading
Aresult
11
00
14931 19 ▶ push @lines, scalar(@rows) . ' rows' if @rows > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @rows > 1 12 7 19
14932  
14933 # The same in every row, or not.
14934 19 my (@shared, @differ);
14935 19 for my $col (@cols) {
14936 22 my %seen = map { $cell->($_, $col) => 1 } @rows;
14937 22 ▶ if (keys(%seen) > 1) { push @differ, $col }
Branch: 2 branches
BranchTrueFalseTotal
if (keys %seen > 1) { } 11 11 22
elsif (@rows) { } 11 0 11
14938 11 elsif (@rows) { push @shared, [ $col, $cell->($rows[0], $col) ] }
14939 }
14940 7 push @lines, map { xml_escape($_->[0]) . ': ' . xml_escape($_->[1]) }
14941 19 grep { length $_->[1] } @shared;
14942  
14943 19 my $html = join('<br>', @lines);
14944 19 ▶ return $html unless @differ;
Branch: 1 branch
BranchTrueFalseTotal
unless @differ 9 10 19
14945  
14946 # The box keeps its size and scroll inline, one style a marker, so a page
14947 # without the stylesheet still cannot be covered; the cells' spacing is
14948 # .bi-map-popup-rows in the page's stylesheet, not repeated in every cell.
14949 10 ▶ my @shown = @rows > $limit ? @rows[0 .. $limit - 1] : @rows;
Branch: 1 branch
BranchTrueFalseTotal
@rows > $limit ? : 8 2 10
14950 $html .= '<div class="bi-map-popup-rows" style="max-height:14em;overflow:auto">'
14951 11 . '<table><tr>' . join('', map { '<th>' . xml_escape($_) . '</th>' } @differ) . '</tr>'
14952 . join('', map {
14953 10 my $row = $_;
14954 67 '<tr>' . join('', map { '<td>' . xml_escape($cell->($row, $_)) . '</td>' } @differ) . '</tr>';
14955 } @shown)
14956 . '</table></div>';
14957  
14958 10 ▶ if (my $more = @rows - @shown) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $more = @rows - @shown) 8 2 10
14959 8 ▶ $html .= "<div>and $more more"
Branch: 1 branch
BranchTrueFalseTotal
defined $more_url && length $more_url ? : 1 7 8
Condition100%3/3
defined $more_url and length $more_url
!ll&&!rl&&r
161
MC/DC100%2/2
defined $more_url and length $more_url
defined $more_url length $more_url
Truth table100%3/3
A and B
A= defined $more_urlB= length $more_url
ABresult
0X0
100
111
14960 . ((defined($more_url) && length($more_url))
14961 ? ': <a href="' . xml_escape($more_url) . '">show all ' . scalar(@rows) . ' in the table</a>'
14962 : '')
14963 . '</div>';
14964 }
14965  
14966 10 return $html;
14967 }
14968  
14969 # _map_size_range($records, \@markers, $col) -> ($min, $max)
14970 #
14971 # The numeric range of the bubble-size column across the markers that will be
14972 # drawn, or an empty list when the column is absent or has no numeric values.
14973 7 ▶sub _map_size_range {
Subroutine _map_size_range: called CC=11 SCAR=24.1
14974 7 my ($records, $markers, $col) = @_;
14975 7 ▶ return () unless defined($col) && length($col);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and length $col 5 2 7
Condition100%3/3
defined $col and length $col
!ll&&!rl&&r
142
MC/DC100%2/2
defined $col and length $col
defined $col length $col
Truth table100%3/3
A and B
A= defined $colB= length $col
ABresult
0X0
100
111
14976  
14977 2 my ($min, $max);
14978 2 for my $marker (@$markers) {
14979 6 my $row = $records->[ $marker->{first_row} ];
14980 6 ▶ next unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 1 5 6
14981 5 ▶ my $value = _numeric_cell($row->{$col} // '');
Condition100%2/2
$row->{$col} // ""
l!l
41
MC/DC100%1/1
$row->{$col} // ""
$row->{$col}
Truth table100%2/2
A
A= $row->{$col}
Aresult
11
00
14982 5 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 2 5
14983 2 ▶ $min = $value if !defined($min) || $value < $min;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $value < $min 1 1 2
Condition66%2/3
not defined $min or $value < $min
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $min or $value < $min
not defined $min $value < $min
Truth table66%2/3
not(A) or B
A= not defined $minB= $value < $min
ABresult
1X1
011
000
14984 2 ▶ $max = $value if !defined($max) || $value > $max;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $value > $max 2 0 2
Condition66%2/3
not defined $max or $value > $max
l!l&&r!l&&!r
110
MC/DC0%0/2
not defined $max or $value > $max
not defined $max $value > $max
Truth table66%2/3
not(A) or B
A= not defined $maxB= $value > $max
ABresult
1X1
011
000
14985 }
14986  
14987 2 return ($min, $max);
14988 }
14989  
14990 # _map_radius($value, $min, $max) -> $pixels
14991 #
14992 # A bubble radius for one marker: 6..30 px across the plotted range, and the
14993 # midpoint for a column that is constant or has no numeric value. Pixels, not
14994 # metres: a circle over the same place must not grow as the reader zooms in.
14995 11 ▶sub _map_radius {
Subroutine _map_radius: called CC=7 SCAR=19.5
14996 11 my ($value, $min, $max) = @_;
14997  
14998 11 ▶ return 10 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 10 11
14999 10 ▶ return 10 unless defined($min) && defined($max) && $max > $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min and defined $max and $max > $min 3 7 10
Condition83%5/6
defined $min and defined $max and $max > $min
!ll&&!rl&&r
127
defined $min and defined $max
!ll&&!rl&&r
109
MC/DC66%2/3
defined $min and defined $max and $max > $min
defined $min defined $max $max > $min
Truth table75%3/4
A and B and C
A= defined $minB= defined $maxC= $max > $min
ABCresult
0XX0
10X0
1100
1111
15000  
15001 7 my $fraction = ($value - $min) / ($max - $min);
15002 7 ▶ $fraction = 0 if $fraction < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fraction < 0 1 6 7
15003 7 ▶ $fraction = 1 if $fraction > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $fraction > 1 1 6 7
15004  
15005 7 return int(6 + $fraction * 24 + 0.5);
15006 }
15007  
15008 # _map_limit($value, $default) -> $integer
15009 #
15010 # A positive integer from a config value or request parameter, or the default.
15011 # An absent, empty or nonsensical map setting is an operator's typo, not a data
15012 # error, so the page falls back instead of refusing.
15013 64 ▶sub _map_limit {
Subroutine _map_limit: called CC=4 SCAR=13.9
15014 64 my ($value, $default) = @_;
15015  
15016 64 ▶ return $default unless defined($value) && ($value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/) && ($value > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u and $value > 0 54 10 64
Condition100%6/6
defined $value and $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u and $value > 0
!ll&&!rl&&r
52210
defined $value and $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u
!ll&&!rl&&r
421012
MC/DC100%3/3
defined $value and $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u and $value > 0
defined $value $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/u $value > 0
Truth table100%4/4
A and B and C
A= defined $valueB= $value =~ /\A\d{1,$MAX_WHOLE_NUMBER_DIGITS}\z/uC= $value > 0
ABCresult
0XX0
10X0
1100
1111
15017  
15018 10 return $value + 0;
15019 }
15020  
15021 # _parse_color($str) -> $color_or_undef
15022 #
15023 # Accepts a lowercase CSS named color from a fixed whitelist, or a hex color
15024 # string in #rgb or #rrggbb form. Returns the normalised lowercase string or
15025 # undef when the input is invalid.
15026 72 ▶sub _parse_color {
Subroutine _parse_color: called CC=6 SCAR=17.9
15027 72 my ($c) = @_;
15028 72 ▶ return undef unless defined($c) && length($c);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and length $c 52 20 72
Condition100%3/3
defined $c and length $c
!ll&&!rl&&r
15120
MC/DC100%2/2
defined $c and length $c
defined $c length $c
Truth table100%3/3
A and B
A= defined $cB= length $c
ABresult
0X0
100
111
15029 20 $c = lc $c;
15030 20 ▶ return $c if $NAMED_COLORS{$c};
Branch: 1 branch
BranchTrueFalseTotal
if $NAMED_COLORS{$c} 3 17 20
15031 17 ▶ return $c if $c =~ /\A#[0-9a-f]{3}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\A#[0-9a-f]{3}\z/u 2 15 17
15032 15 ▶ return $c if $c =~ /\A#[0-9a-f]{6}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\A#[0-9a-f]{6}\z/u 1 14 15
15033 14 return undef;
15034 }
15035  
15036 # _parse_color_rules(\@raw_rules, $default_raw) -> (\@rules, $default_color)
15037 #
15038 # Parses the ?color_rule=op:threshold:color repeatable param list and the
15039 # optional ?color_default=color param into a structured rules array and a
15040 # fallback colour. Invalid rule entries are silently dropped.
15041 5 ▶sub _parse_color_rules {
Subroutine _parse_color_rules: called CC=10 SCAR=23.1
15042 5 my ($raw_rules, $default_raw) = @_;
15043 5 ▶ my $default_color = _parse_color($default_raw // '') // $DEFAULT_MARKER_COLOR;
Condition80%4/5
_parse_color($default_raw // "") // $DEFAULT_MARKER_COLOR
l!l&&r!l&&!r
140
$default_raw // ""
l!l
41
MC/DC0%0/2
_parse_color($default_raw // "") // $DEFAULT_MARKER_COLOR
_parse_color($default_raw // "") $DEFAULT_MARKER_COLOR
MC/DC100%1/1
$default_raw // ""
$default_raw
Truth table66%2/3
A // B
A= _parse_color($default_raw // "")B= $DEFAULT_MARKER_COLOR
ABresult
1X1
011
000
Truth table100%2/2
A
A= $default_raw
Aresult
11
00
15044 5 my @rules;
15045 5 ▶ for my $raw (@{ $raw_rules // [] }) {
Condition100%2/2
$raw_rules // []
l!l
41
MC/DC100%1/1
$raw_rules // []
$raw_rules
Truth table100%2/2
A
A= $raw_rules
Aresult
11
00
15046 7 my ($op, $val, $color_str) = split /:/, $raw, 3;
15047 7 ▶ next unless defined($op) && defined($val) && defined($color_str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op and defined $val and defined $color_str 2 5 7
Condition83%5/6
defined $op and defined $val and defined $color_str
!ll&&!rl&&r
115
defined $op and defined $val
!ll&&!rl&&r
106
MC/DC66%2/3
defined $op and defined $val and defined $color_str
defined $op defined $val defined $color_str
Truth table75%3/4
A and B and C
A= defined $opB= defined $valC= defined $color_str
ABCresult
0XX0
10X0
1100
1111
15048 5 $op = lc $op;
15049 5 ▶ next unless $op =~ /\A(?:gt|ge|lt|le|eq|ne)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /\A(?:gt|ge|lt|le|eq|ne)\z/u 1 4 5
15050 4 my $color = _parse_color($color_str);
15051 4 ▶ next unless defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 2 2 4
15052 2 push @rules, { op => $op, val => $val, color => $color };
15053 }
15054 5 return (\@rules, $default_color);
15055 }
15056  
15057 # _map_colour_entries($color_col, \@rules, $default, $is_series) -> @entries
15058 #
15059 # The colour key of a map, as { color, label, group }: one entry for each rule,
15060 # and Other for the default colour. A series (series= with a scheme) is
15061 # listed by value, with no Other: its values past the palette's two cycles
15062 # take a palette colour but are not listed. group names the entry for
15063 # _color_rule_index's answer ("rule<N>", or "other"), which is how HTML::OSM
15064 # lists only the entries in view.
15065 6 ▶sub _map_colour_entries {
Subroutine _map_colour_entries: called CC=12 SCAR=25.0
15066 6 my ($color_col, $rules, $default, $is_series) = @_;
15067  
15068 6 my %op_label = (gt => '>', ge => '>=', lt => '<', le => '<=', eq => '=', ne => '!=');
15069 6 my @entries;
15070 6 ▶ $rules = [] unless ref($rules) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules eq "ARRAY" 1 5 6
15071 6 for my $index (0 .. $#{$rules}) {
15072 7 my $rule = $rules->[$index];
15073 7 ▶ next unless ref($rule) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 2 5 7
15074 5 ▶ my $val = $rule->{val} // '';
Condition50%1/2
$rule->{"val"} // ""
l!l
50
MC/DC0%0/1
$rule->{"val"} // ""
$rule->{"val"}
Truth table50%1/2
A
A= $rule->{"val"}
Aresult
11
00
15075 push @entries, {
15076 color => $rule->{color},
15077 label => $is_series
15078 ? (length($val) ? $val : '(blank)')
15079 5 ▶ : "$color_col " . ($op_label{ $rule->{op} // '' } // $rule->{op} // '') . " $val",
Branch: 2 branches
BranchTrueFalseTotal
$is_series ? : 2 3 5
length $val ? : 1 1 2
Condition57%4/7
($op_label{$rule->{"op"} // ""} // $rule->{"op"}) // ""
l!l
30
$op_label{$rule->{"op"} // ""} // $rule->{"op"}
l!l&&r!l&&!r
210
$rule->{"op"} // ""
l!l
30
MC/DC0%0/2
($op_label{$rule->{"op"} // ""} // $rule->{"op"}) // ""
$op_label{$rule->{"op"} // ""} $rule->{"op"}
MC/DC0%0/1
$rule->{"op"} // ""
$rule->{"op"}
Truth table66%2/3
A // B
A= $op_label{$rule->{"op"} // ""}B= $rule->{"op"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $rule->{"op"}
Aresult
11
00
15080 group => "rule$index",
15081 };
15082 }
15083 6 ▶ push @entries, { color => $default, label => 'Other', group => 'other' }
Branch: 1 branch
BranchTrueFalseTotal
if @entries and not $is_series 2 4 6
Condition100%3/3
@entries and not $is_series
!ll&&!rl&&r
312
MC/DC100%2/2
@entries and not $is_series
@entries not $is_series
Truth table100%3/3
A and not(B)
A= @entriesB= not $is_series
ABresult
0X0
100
111
15084 if @entries && !$is_series;
15085 6 return @entries;
15086 }
15087  
15088 # _map_key($self, $color_col, \@rules, $default, $is_series, $size_col, \@range)
15089 # -> (legend => \@entries, legend_title => $title, legend_empty => $text)
15090 #
15091 # What HTML::OSM is given to draw a map's key: the colour entries, each with
15092 # its group so the key lists only what is in view, then two or three bubble
15093 # sizes when a size column is set (those are always listed: they say what a
15094 # size means, wherever the reader is). The title is the colour column, or
15095 # the size column when there is no colour; with both, each size is labelled
15096 # with its column. The colours go to HTML::OSM as hex, which is all it takes.
15097 9 ▶sub _map_key :Protected ($self, $color_col, $rules, $default, $is_series, $size_col, $range) {
Subroutine _map_key: called CC=12 SCAR=24.9
15098 my @legend = map {
15099 9 { label => $_->{label}, colour => _color_hex($_->{color}), group => $_->{group} }
15100 2 ▶ } (length($color_col) ? _map_colour_entries($color_col, $rules, $default, $is_series) : ());
Branch: 1 branch
BranchTrueFalseTotal
length $color_col ? : 2 7 9
15101  
15102 9 ▶ my ($min, $max) = @{ $range // [] };
Condition100%2/2
$range // []
l!l
81
MC/DC100%1/1
$range // []
$range
Truth table100%2/2
A
A= $range
Aresult
11
00
15103 9 ▶ if (length($size_col) && defined($min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $size_col and defined $min) 2 7 9
Condition100%3/3
length $size_col and defined $min
!ll&&!rl&&r
612
MC/DC100%2/2
length $size_col and defined $min
length $size_col defined $min
Truth table100%3/3
A and B
A= length $size_colB= defined $min
ABresult
0X0
100
111
15104 2 ▶ my @values = ($max > $min) ? ($min, ($min + $max) / 2, $max) : ($min);
Branch: 1 branch
BranchTrueFalseTotal
$max > $min ? : 1 1 2
15105 2 for my $value (@values) {
15106 4 my $text = 0 + sprintf('%.4g', $value);
15107 4 ▶ push @legend, {
Branch: 2 branches
BranchTrueFalseTotal
@legend && length $color_col ? : 0 4 4
length $color_col ? : 0 4 4
Condition66%2/3
@legend and length $color_col
!ll&&!rl&&r
220
MC/DC0%0/2
@legend and length $color_col
@legend length $color_col
Truth table66%2/3
A and B
A= @legendB= length $color_col
ABresult
0X0
100
111
15108 label => (@legend && length($color_col) ? "$size_col $text" : "$text"),
15109 colour => (length($color_col) ? $MAP_SIZE_KEY_COLOR : _color_hex($default)),
15110 radius => _map_radius($value, $min, $max),
15111 };
15112 }
15113 }
15114  
15115 9 ▶ return () unless @legend;
Branch: 1 branch
BranchTrueFalseTotal
unless @legend 6 3 9
15116 return (
15117 3 ▶ legend => \@legend,
Branch: 1 branch
BranchTrueFalseTotal
length $color_col ? : 1 2 3
15118 legend_title => (length($color_col) ? $color_col : $size_col),
15119 legend_empty => $self->_i18n('map_legend_empty'),
15120 );
15121 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15122  
15123 # _color_rule_index($value, \@rules) -> $index | undef
15124 #
15125 # The index of the first rule that matches $value, or undef when none does
15126 # (the marker takes the default colour). _apply_color_rule says the colour.
15127 24 ▶sub _color_rule_index {
Subroutine _color_rule_index: called CC=24 SCAR=31.9
15128 24 my ($value, $rules) = @_;
15129 24 ▶ $value //= '';
Condition100%2/2
$value //= ""
l!l
222
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
15130 24 ▶ $rules = [] unless ref($rules) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules eq "ARRAY" 2 22 24
15131 24 for my $index (0 .. $#{$rules}) {
15132 29 ▶ next unless ref($rules->[$index]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules->[$index] eq "HASH" 3 26 29
15133 26 my ($op, $threshold) = @{ $rules->[$index] }{qw(op val)};
15134 26 ▶ $op //= '';
Condition100%2/2
$op //= ""
l!l
251
MC/DC100%1/1
$op //= ""
$op
Truth table100%2/2
A
A= $op
Aresult
11
00
15135 26 ▶ $threshold //= '';
Condition50%1/2
$threshold //= ""
l!l
260
MC/DC0%0/1
$threshold //= ""
$threshold
Truth table50%1/2
A
A= $threshold
Aresult
11
00
15136 26 my $match;
15137 26 ▶ if ( $value =~ /\A-?\d+(?:\.\d+)?\z/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A-?\d+(?:\.\d+)?\z/u and $threshold =~ /\A-?\d+(?:\.\d+)?\z/u) { } 16 10 26
Condition100%3/3
$value =~ /\A-?\d+(?:\.\d+)?\z/u and $threshold =~ /\A-?\d+(?:\.\d+)?\z/u
!ll&&!rl&&r
8216
MC/DC100%2/2
$value =~ /\A-?\d+(?:\.\d+)?\z/u and $threshold =~ /\A-?\d+(?:\.\d+)?\z/u
$value =~ /\A-?\d+(?:\.\d+)?\z/u $threshold =~ /\A-?\d+(?:\.\d+)?\z/u
Truth table100%3/3
A and B
A= $value =~ /\A-?\d+(?:\.\d+)?\z/uB= $threshold =~ /\A-?\d+(?:\.\d+)?\z/u
ABresult
0X0
100
111
15138 $threshold =~ /\A-?\d+(?:\.\d+)?\z/) {
15139 16 my ($num_val, $num_thr) = ($value + 0, $threshold + 0);
15140 16 ▶ $match = ($op eq 'gt' && $num_val > $num_thr)
Condition87%29/33
$op eq 'gt' && $num_val > $num_thr || $op eq 'ge' && $num_val >= $num_thr || $op eq 'lt' && $num_val < $num_thr || $op eq 'le' && $num_val <= $num_thr || $op eq 'eq' && $num_val == $num_thr || $op eq 'ne' && $num_val != $num_thr
l!l&&r!l&&!r
925
$op eq "gt" and $num_val > $num_thr or $op eq "ge" and $num_val >= $num_thr or $op eq "lt" and $num_val < $num_thr or $op eq "le" and $num_val <= $num_thr or $op eq "eq" and $num_val == $num_thr
l!l&&r!l&&!r
817
$op eq "gt" and $num_val > $num_thr or $op eq "ge" and $num_val >= $num_thr or $op eq "lt" and $num_val < $num_thr or $op eq "le" and $num_val <= $num_thr
l!l&&r!l&&!r
718
$op eq "gt" and $num_val > $num_thr or $op eq "ge" and $num_val >= $num_thr or $op eq "lt" and $num_val < $num_thr
l!l&&r!l&&!r
529
$op eq "gt" and $num_val > $num_thr or $op eq "ge" and $num_val >= $num_thr
l!l&&r!l&&!r
4111
$op eq "gt" and $num_val > $num_thr
!ll&&!rl&&r
934
$op eq "ge" and $num_val >= $num_thr
!ll&&!rl&&r
1101
$op eq "lt" and $num_val < $num_thr
!ll&&!rl&&r
902
$op eq "le" and $num_val <= $num_thr
!ll&&!rl&&r
801
$op eq "eq" and $num_val == $num_thr
!ll&&!rl&&r
701
$op eq "ne" and $num_val != $num_thr
!ll&&!rl&&r
412
MC/DC66%8/12
$op eq 'gt' && $num_val > $num_thr || $op eq 'ge' && $num_val >= $num_thr || $op eq 'lt' && $num_val < $num_thr || $op eq 'le' && $num_val <= $num_thr || $op eq 'eq' && $num_val == $num_thr || $op eq 'ne' && $num_val != $num_thr
$op eq "gt" $num_val > $num_thr $op eq "ge" $num_val >= $num_thr $op eq "lt" $num_val < $num_thr $op eq "le" $num_val <= $num_thr $op eq "eq" $num_val == $num_thr $op eq "ne" $num_val != $num_thr
Truth table7%9/127
A and B or C and D or E and F or G and H or I and J || K and L
A= $op eq "gt"B= $num_val > $num_thrC= $op eq "ge"D= $num_val >= $num_thrE= $op eq "lt"F= $num_val < $num_thrG= $op eq "le"H= $num_val <= $num_thrI= $op eq "eq"J= $num_val == $num_thrK= $op eq "ne"L= $num_val != $num_thr
ABCDEFGHIJKLresult
11XXXXXXXXXX1
0X11XXXXXXXX1
1011XXXXXXXX1
0X0X11XXXXXX1
0X1011XXXXXX1
100X11XXXXXX1
101011XXXXXX1
0X0X0X11XXXX1
0X0X1011XXXX1
0X100X11XXXX1
0X101011XXXX1
100X0X11XXXX1
100X1011XXXX1
10100X11XXXX1
10101011XXXX1
0X0X0X0X11XX1
0X0X0X1011XX1
0X0X100X11XX1
0X0X101011XX1
0X100X0X11XX1
0X100X1011XX1
0X10100X11XX1
0X10101011XX1
100X0X0X11XX1
100X0X1011XX1
100X100X11XX1
100X101011XX1
10100X0X11XX1
10100X1011XX1
1010100X11XX1
1010101011XX1
0X0X0X0X0X111
0X0X0X0X10111
0X0X0X100X111
0X0X0X1010111
0X0X100X0X111
0X0X100X10111
0X0X10100X111
0X0X101010111
0X100X0X0X111
0X100X0X10111
0X100X100X111
0X100X1010111
0X10100X0X111
0X10100X10111
0X1010100X111
0X10101010111
100X0X0X0X111
100X0X0X10111
100X0X100X111
100X0X1010111
100X100X0X111
100X100X10111
100X10100X111
100X101010111
10100X0X0X111
10100X0X10111
10100X100X111
10100X1010111
1010100X0X111
1010100X10111
101010100X111
1010101010111
0X0X0X0X0X0X0
0X0X0X0X0X100
0X0X0X0X100X0
0X0X0X0X10100
0X0X0X100X0X0
0X0X0X100X100
0X0X0X10100X0
0X0X0X1010100
0X0X100X0X0X0
0X0X100X0X100
0X0X100X100X0
0X0X100X10100
0X0X10100X0X0
0X0X10100X100
0X0X1010100X0
0X0X101010100
0X100X0X0X0X0
0X100X0X0X100
0X100X0X100X0
0X100X0X10100
0X100X100X0X0
0X100X100X100
0X100X10100X0
0X100X1010100
0X10100X0X0X0
0X10100X0X100
0X10100X100X0
0X10100X10100
0X1010100X0X0
0X1010100X100
0X101010100X0
0X10101010100
100X0X0X0X0X0
100X0X0X0X100
100X0X0X100X0
100X0X0X10100
100X0X100X0X0
100X0X100X100
100X0X10100X0
100X0X1010100
100X100X0X0X0
100X100X0X100
100X100X100X0
100X100X10100
100X10100X0X0
100X10100X100
100X1010100X0
100X101010100
10100X0X0X0X0
10100X0X0X100
10100X0X100X0
10100X0X10100
10100X100X0X0
10100X100X100
10100X10100X0
10100X1010100
1010100X0X0X0
1010100X0X100
1010100X100X0
1010100X10100
101010100X0X0
101010100X100
10101010100X0
1010101010100
15141 || ($op eq 'ge' && $num_val >= $num_thr)
15142 || ($op eq 'lt' && $num_val < $num_thr)
15143 || ($op eq 'le' && $num_val <= $num_thr)
15144 || ($op eq 'eq' && $num_val == $num_thr)
15145 || ($op eq 'ne' && $num_val != $num_thr);
15146 } else {
15147 10 ▶ $match = ($op eq 'eq' && lc($value) eq lc($threshold))
Condition88%8/9
$op eq 'eq' && lc $value eq lc $threshold || $op eq 'ne' && lc $value ne lc $threshold
l!l&&r!l&&!r
316
$op eq "eq" and lc $value eq lc $threshold
!ll&&!rl&&r
523
$op eq "ne" and lc $value ne lc $threshold
!ll&&!rl&&r
601
MC/DC75%3/4
$op eq 'eq' && lc $value eq lc $threshold || $op eq 'ne' && lc $value ne lc $threshold
$op eq "eq" lc $value eq lc $threshold $op eq "ne" lc $value ne lc $threshold
Truth table57%4/7
A and B || C and D
A= $op eq "eq"B= lc $value eq lc $thresholdC= $op eq "ne"D= lc $value ne lc $threshold
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
15148 || ($op eq 'ne' && lc($value) ne lc($threshold));
15149 }
15150 26 ▶ return $index if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 15 11 26
15151 }
15152 9 return undef;
15153 }
15154  
15155 # _apply_color_rule($value, \@rules, $default_color) -> $color
15156 #
15157 # Returns the first colour whose rule matches $value, or $default_color.
15158 # Numeric comparisons are used when both sides look like plain numbers.
15159 13 ▶sub _apply_color_rule {
Subroutine _apply_color_rule: called CC=2 SCAR=6.9
15160 13 my ($value, $rules, $default) = @_;
15161 13 my $index = _color_rule_index($value, $rules);
15162 13 ▶ return defined($index) ? $rules->[$index]{color} : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 9 4 13
15163 }
15164  
15165 # _marker_icon($color) -> $data_uri
15166 #
15167 # Returns a data:image/svg+xml URI for a 32x32 teardrop pin icon whose fill is
15168 # $color. The icon anchor is [16,32] (HTML::OSM's hardcoded default), so the
15169 # path tip lands at the bottom centre of the bounding box.
15170 2 ▶sub _marker_icon {
Subroutine _marker_icon: called CC=2 SCAR=6.9
15171 2 my ($color) = @_;
15172 2 ▶ $color //= $DEFAULT_MARKER_COLOR;
Condition66%2/3
$color //= $DEFAULT_MARKER_COLOR
l!l&&r!l&&!r
110
MC/DC0%0/2
$color //= $DEFAULT_MARKER_COLOR
$color $DEFAULT_MARKER_COLOR
Truth table66%2/3
A //= B
A= $colorB= $DEFAULT_MARKER_COLOR
ABresult
1X1
011
000
15173  
15174 # Commas in path data avoid spaces that would need percent-encoding.
15175 2 my $svg =
15176 "<svg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 32 32'>" .
15177 "<path d='M16,0C10,0,5,5,5,12C5,21,16,32,16,32C16,32,27,21,27,12C27,5,22,0,16,0Z'" .
15178 " fill='$color' stroke='#444' stroke-width='1.5'/>" .
15179 "<circle cx='16' cy='12' r='5' fill='white' opacity='0.65'/>" .
15180 "</svg>";
15181  
15182 # Minimal percent-encoding for a data: URI (RFC 2397).
15183 2 $svg =~ s/#/%23/g;
15184 2 $svg =~ s/</%3C/g;
15185 2 $svg =~ s/>/%3E/g;
15186 2 $svg =~ s/ /%20/g;
15187  
15188 2 return "data:image/svg+xml,$svg";
15189 }
15190  
15191 # _color_hex($color) -> $hex
15192 #
15193 # add_circle_marker() accepts hex colours only, while _parse_color() also
15194 # allows a fixed set of CSS names for the icon path. Translate the names so a
15195 # colour rule means the same thing on a bubble map as on a pin map.
15196 11 ▶sub _color_hex {
Subroutine _color_hex: called CC=5 SCAR=16.1
15197 11 my ($color) = @_;
15198  
15199 11 ▶ return $DEFAULT_MARKER_COLOR unless defined($color) && length($color);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color and length $color 2 9 11
Condition100%3/3
defined $color and length $color
!ll&&!rl&&r
119
MC/DC100%2/2
defined $color and length $color
defined $color length $color
Truth table100%3/3
A and B
A= defined $colorB= length $color
ABresult
0X0
100
111
15200 9 ▶ return $color if $color =~ /\A#[0-9a-f]{3}(?:[0-9a-f]{3})?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /\A#[0-9a-f]{3}(?:[0-9a-f]{3})?\z/u 6 3 9
15201  
15202 3 ▶ return $NAMED_COLOR_HEX{$color} // $DEFAULT_MARKER_COLOR;
Condition66%2/3
$NAMED_COLOR_HEX{$color} // $DEFAULT_MARKER_COLOR
l!l&&r!l&&!r
210
MC/DC0%0/2
$NAMED_COLOR_HEX{$color} // $DEFAULT_MARKER_COLOR
$NAMED_COLOR_HEX{$color} $DEFAULT_MARKER_COLOR
Truth table66%2/3
A // B
A= $NAMED_COLOR_HEX{$color}B= $DEFAULT_MARKER_COLOR
ABresult
1X1
011
000
15203 }
15204  
15205 # _iso8601_utc() -> $string
15206 #
15207 # When a geocode cache was built, in the form the model store records.
15208 4 ▶sub _iso8601_utc {
Subroutine _iso8601_utc: called CC=1 SCAR=0.0
15209 4 return Database::BI::Util::iso8601_utc();
15210 }
15211  
15212 # _catalog_entry($self, $mf, $analyses) -> \%entry
15213 #
15214 # Build one catalog entry for a Mojo::File data file.
15215 200 ▶sub _catalog_entry :Protected ($self, $mf, $analyses) {
Subroutine _catalog_entry: called CC=14 SCAR=27.2
15216 200 my $path = $mf->to_string;
15217 200 my $name = $mf->basename;
15218 200 ▶ my $size = -s $path // 0;
Condition50%1/2
-s $path // 0
l!l
2000
MC/DC0%0/1
-s $path // 0
-s $path
Truth table50%1/2
A
A= -s $path
Aresult
11
00
15219 200 ▶ my $mtime = (stat($path))[9] // 0;
Condition50%1/2
(stat $path)[9] // 0
l!l
2000
MC/DC0%0/1
(stat $path)[9] // 0
(stat $path)[9]
Truth table50%1/2
A
A= (stat $path)[9]
Aresult
11
00
15220  
15221 200 require POSIX;
15222 200 ▶ my $mtime_str = $mtime ? Database::BI::Util::minute_stamp(gmtime($mtime)) . ' UTC' : '';
Branch: 1 branch
BranchTrueFalseTotal
$mtime ? : 200 0 200
15223  
15224 # Find which analyses reference this file.
15225 # Normalize path separators so that Windows paths stored with backslashes
15226 # match Mojo::File paths that may use forward slashes, and vice versa.
15227 # Case-fold only the path portion on case-insensitive filesystems.
15228 200 my $use_ci = File::Spec->case_tolerant;
15229 200 (my $norm_path = $path) =~ s{\\}{/}g;
15230 200 ▶ $norm_path = lc $norm_path if $use_ci;
Branch: 1 branch
BranchTrueFalseTotal
if $use_ci 0 200 200
15231  
15232 200 my @refs;
15233 200 for my $aid (sort keys %$analyses) {
15234 145 my $a = $analyses->{$aid};
15235 145 ▶ for my $ds (@{ $a->{datasets} // [] }) {
Condition100%2/2
$a->{"datasets"} // []
l!l
11233
MC/DC100%1/1
$a->{"datasets"} // []
$a->{"datasets"}
Truth table100%2/2
A
A= $a->{"datasets"}
Aresult
11
00
15236 112 ▶ my $spec = $ds->{spec} // '';
Condition50%1/2
$ds->{"spec"} // ""
l!l
1120
MC/DC0%0/1
$ds->{"spec"} // ""
$ds->{"spec"}
Truth table50%1/2
A
A= $ds->{"spec"}
Aresult
11
00
15237 112 my $table_stem = $mf->basename; $table_stem =~ s/\.[^.]+\z//;
15238 112 (my $norm_spec = $spec) =~ s{\\}{/}g;
15239 112 ▶ $norm_spec = lc $norm_spec if $use_ci;
Branch: 1 branch
BranchTrueFalseTotal
if $use_ci 0 112 112
15240 112 ▶ if (CORE::index($norm_spec, $norm_path) >= 0 || CORE::index($spec, "table:$table_stem") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::index($norm_spec, $norm_path) != -1 or CORE::index($spec, "table:$table_stem") == 0) 11 101 112
Condition66%2/3
CORE::index($norm_spec, $norm_path) != -1 or CORE::index($spec, "table:$table_stem") == 0
l!l&&r!l&&!r
011101
MC/DC50%1/2
CORE::index($norm_spec, $norm_path) != -1 or CORE::index($spec, "table:$table_stem") == 0
CORE::index($norm_spec, $norm_path) != -1 CORE::index($spec, "table:$table_stem") == 0
Truth table66%2/3
A or B
A= CORE::index($norm_spec, $norm_path) != -1B= CORE::index($spec, "table:$table_stem") == 0
ABresult
1X1
011
000
15241 11 push @refs, { id => $aid, name => $a->{name} };
15242 11 last;
15243 }
15244 }
15245 }
15246  
15247 # Derive a human-readable type from the extension.
15248 200 my ($ext) = ($name =~ /\.([^.]+)\z/);
15249 200 ▶ $ext = lc($ext // '');
Condition50%1/2
$ext // ""
l!l
2000
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
Aresult
11
00
15250 200 my %type_map = (
15251 csv => 'CSV file', tsv => 'TSV file', psv => 'PSV file',
15252 xml => 'XML file', xlsx => 'XLSX spreadsheet',
15253 sql => 'SQLite database', sqlite => 'SQLite database',
15254 sqlite3 => 'SQLite database', db => 'Berkeley DB / SQLite',
15255 );
15256 200 ▶ my $type = $type_map{$ext} // 'Data file';
Condition50%1/2
$type_map{$ext} // "Data file"
l!l
2000
MC/DC0%0/1
$type_map{$ext} // "Data file"
$type_map{$ext}
Truth table50%1/2
A
A= $type_map{$ext}
Aresult
11
00
15257  
15258 return {
15259 200 name => $name,
15260 path => $path,
15261 type => $type,
15262 size => $size,
15263 mtime_str => $mtime_str,
15264 analysis_count => scalar @refs,
15265 analyses => \@refs,
15266 };
15267 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15268  
15269 # GET /catalog
15270 18 ▶sub catalog_view ($self) {
Subroutine catalog_view: called CC=5 SCAR=16.4
Pod documented
15271 18 ▶ return if $self->buffer_is_cached;
Branch: 1 branch
BranchTrueFalseTotal
if $self->buffer_is_cached 0 18 18
15272 18 my $dir = _data_dir_for($self->app);
15273 18 my $analyses = $self->_read_analyses;
15274  
15275 18 my @entries;
15276 18 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 18 0 18
15277 18 my $ext_re = $EXT_RE;
15278 18 for my $mf ($dir->list->each) {
15279 198 ▶ next unless $mf->to_string =~ $ext_re;
Branch: 1 branch
BranchTrueFalseTotal
unless $mf->to_string =~ /$ext_re/u 0 198 198
15280 198 push @entries, $self->_catalog_entry($mf, $analyses);
15281 }
15282 }
15283  
15284 18 my ($platform, $language) = $self->_resolve_template;
15285 18 $self->render(
15286 handler => 'tt',
15287 template => "$platform/$language/catalog",
15288 format => 'html',
15289 title => 'Dataset catalog',
15290 catalog_entries => \@entries,
15291 language => $language,
15292 );
15293 }
15294  
15295 =head2 canvases_view
15296  
15297 C<GET /canvases> -- The saved canvases, as cards that open, select and delete,
15298 with Import canvas. The main navigation's Canvases link. The list is the one
15299 the home page shows (C<layouts/canvas_list.html.tt>); here it also says when
15300 there are none, and how to make one.
15301  
15302 =head3 API SPECIFICATION
15303  
15304 =head4 INPUT
15305  
15306 None.
15307  
15308 =head4 OUTPUT
15309  
15310 C<200> with the HTML page. The cards are drawn by the page from
15311 C<GET /api/analyses>.
15312  
15313 =cut
15314  
15315 2 ▶sub canvases_view ($self) {
Subroutine canvases_view: called CC=1 SCAR=0.0
Pod documented
15316 2 my ($platform, $language) = $self->_resolve_template;
15317 2 $self->render(
15318 handler => 'tt',
15319 template => "$platform/$language/canvases",
15320 format => 'html',
15321 title => 'Canvases',
15322 canvases_empty => 'There are no saved canvases yet. To make one, open a table and click New canvas..., or import a canvas from a file.',
15323 language => $language,
15324 );
15325 }
15326  
15327 # _share_secret($self) -> $secret_string
15328 #
15329 # The key share and embed tokens are signed with: config share_secret, else
15330 # one derived from the application's first secret. It used to fall back to a
15331 # fixed string, the same on every install, so anyone could sign a token for
15332 # any canvas, with any expiry, and read it without the password. The first
15333 # secret is random for each start unless config secrets sets it, so without
15334 # either a restart ends every link (_share_links_last says which). It is
15335 # derived, not the secret itself, so a token is never a signature that the
15336 # session cookie or a map list would also accept.
15337 59 ▶sub _share_secret :Protected ($self) {
Subroutine _share_secret: called CC=4 SCAR=13.9
15338 59 ▶ my $s = $self->app->config('share_secret') // '';
Condition100%2/2
$self->app->config("share_secret") // ""
l!l
1247
MC/DC100%1/1
$self->app->config("share_secret") // ""
$self->app->config("share_secret")
Truth table100%2/2
A
A= $self->app->config("share_secret")
Aresult
11
00
15339 59 ▶ return $s if !ref($s) && length $s;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $s and length $s 11 48 59
Condition100%3/3
not ref $s and length $s
!ll&&!rl&&r
14711
MC/DC100%2/2
not ref $s and length $s
not ref $s length $s
Truth table100%3/3
not(A) and B
A= not ref $sB= length $s
ABresult
0X0
100
111
15340 48 require Digest::SHA;
15341 48 return Digest::SHA::hmac_sha256_hex('Database::BI share tokens', $self->app->secrets->[0]);
15342 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15343  
15344 # _share_links_last($self) -> 1 | 0
15345 #
15346 # Whether a share link outlives a restart: true when share_secret or secrets
15347 # is configured, so the key is the same next time.
15348 1 ▶sub _share_links_last :Protected ($self) {
Subroutine _share_links_last: called CC=6 SCAR=25.9
15349 1 ▶ my $s = $self->app->config('share_secret') // '';
Condition50%1/2
$self->app->config("share_secret") // ""
l!l
10
MC/DC0%0/1
$self->app->config("share_secret") // ""
$self->app->config("share_secret")
Truth table50%1/2
A
A= $self->app->config("share_secret")
Aresult
11
00
15350 1 my $secrets = $self->app->config('secrets');
15351 0 ▶ return (!ref($s) && length $s) || (ref($secrets) eq 'ARRAY' && @{$secrets}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!ref($s) && length $s || ref $secrets eq 'ARRAY' && @{$secrets;} ? : 1 0 1
Condition22%2/9
not ref $s and length $s or ref $secrets eq "ARRAY" and @{$secrets;}
l!l&&r!l&&!r
100
not ref $s and length $s
!ll&&!rl&&r
001
ref $secrets eq "ARRAY" and @{$secrets;}
!ll&&!rl&&r
000
MC/DC0%0/4
not ref $s and length $s or ref $secrets eq "ARRAY" and @{$secrets;}
not ref $s length $s ref $secrets eq "ARRAY" @{$secrets;}
Truth table14%1/7
not(A) and B or C and D
A= not ref $sB= length $sC= ref $secrets eq "ARRAY"D= @{$secrets;}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
15352 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15353  
15354 # _make_share_token($self, $id, $expires) -> $token_string
15355 #
15356 # Signs with HMAC-SHA256 using Digest::SHA (Perl core since 5.10).
15357 18 ▶sub _make_share_token :Protected ($self, $id, $expires) {
Subroutine _make_share_token: called CC=1 SCAR=0.0
15358 18 require Digest::SHA;
15359 18 require MIME::Base64;
15360 18 my $secret = $self->_share_secret;
15361 18 my $payload = "$id|$expires";
15362 18 my $hmac = Digest::SHA::hmac_sha256_hex($payload, $secret);
15363 18 return MIME::Base64::encode_base64url("$payload|$hmac");
15364 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15365  
15366 # _verify_share_token($self, $id, $token) -> 1 | undef
15367 50 ▶sub _verify_share_token :Protected ($self, $id, $token) {
Subroutine _verify_share_token: called CC=13 SCAR=25.7
15368 50 ▶ return undef unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 2 48 50
Condition100%3/3
defined $token and length $token
!ll&&!rl&&r
1148
MC/DC100%2/2
defined $token and length $token
defined $token length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
15369 48 require MIME::Base64;
15370 48 my $decoded = eval { MIME::Base64::decode_base64url($token) };
15371 48 ▶ return undef unless defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 0 48 48
15372 48 my ($tid, $expires, $hmac) = split /\|/, $decoded, 3;
15373 48 ▶ return undef unless defined $tid && defined $expires && defined $hmac;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tid and defined $expires and defined $hmac 6 42 48
Condition83%5/6
defined $tid and defined $expires and defined $hmac
!ll&&!rl&&r
6042
defined $tid and defined $expires
!ll&&!rl&&r
2442
MC/DC66%2/3
defined $tid and defined $expires and defined $hmac
defined $tid defined $expires defined $hmac
Truth table75%3/4
A and B and C
A= defined $tidB= defined $expiresC= defined $hmac
ABCresult
0XX0
10X0
1100
1111
15374 42 ▶ return undef unless $tid eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $tid eq $id 3 39 42
15375 39 ▶ return undef unless $expires =~ /\A\d+\z/ && $expires > time();
Branch: 1 branch
BranchTrueFalseTotal
unless $expires =~ /\A\d+\z/u and $expires > time 2 37 39
Condition100%3/3
$expires =~ /\A\d+\z/u and $expires > time
!ll&&!rl&&r
1137
MC/DC100%2/2
$expires =~ /\A\d+\z/u and $expires > time
$expires =~ /\A\d+\z/u $expires > time
Truth table100%3/3
A and B
A= $expires =~ /\A\d+\z/uB= $expires > time
ABresult
0X0
100
111
15376 37 require Digest::SHA;
15377 37 my $secret = $self->_share_secret;
15378 37 my $expected = Digest::SHA::hmac_sha256_hex("$id|$expires", $secret);
15379 37 ▶ return undef unless length($hmac) == length($expected);
Branch: 1 branch
BranchTrueFalseTotal
unless length $hmac == length $expected 1 36 37
15380 # Every character is compared, so the time taken says nothing about how
15381 # much of a guessed signature was right.
15382 36 my $diff = 0;
15383 36 $diff |= ord(substr($hmac, $_, 1)) ^ ord(substr($expected, $_, 1)) for 0 .. length($expected) - 1;
15384 36 ▶ return undef if $diff;
Branch: 1 branch
BranchTrueFalseTotal
if $diff 3 33 36
15385 33 return 1;
15386 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
15387  
15388 # POST /api/analyses/:id/share
15389 4 ▶sub analyses_share ($self) {
Subroutine analyses_share: called CC=5 SCAR=16.2
Pod documented
15390 4 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
40
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
15391 4 ▶ return $self->render(json => { error => _message_text('error_analysis_id_invalid') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 2 2 4
15392 unless $id =~ /\A[A-Za-z0-9]+\z/;
15393  
15394 2 my $analyses = $self->_read_analyses;
15395 return $self->render(json => { error => sprintf(_message_text('error_analysis_not_found'), $id) }, status => 404)
15396 2 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 1 1 2
15397  
15398 1 my $expires = time() + $SHARE_TOKEN_TTL;
15399 1 my $token = $self->_make_share_token($id, $expires);
15400 1 my $url = $self->req->url->base->to_string . "/share/$id?token=" . url_escape($token);
15401  
15402 1 ▶ $self->render(json => {
Branch: 1 branch
BranchTrueFalseTotal
$self->_share_links_last ? : 1 0 1
15403 token => $token, url => $url, expires => $expires,
15404 lasts_restart => $self->_share_links_last ? Mojo::JSON::true() : Mojo::JSON::false(),
15405 });
15406 }
15407  
15408 # GET /share/:id?token=...
15409 20 ▶sub share_view ($self) {
Subroutine share_view: called CC=9 SCAR=22.0
Pod documented
15410 20 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
200
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
15411 20 ▶ my $token = $self->param('token') // '';
Condition100%2/2
$self->param("token") // ""
l!l
191
MC/DC100%1/1
$self->param("token") // ""
$self->param("token")
Truth table100%2/2
A
A= $self->param("token")
Aresult
11
00
15412 20 ▶ return $self->render(text => _message_text('error_analysis_id_invalid'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 1 19 20
15413 unless $id =~ /\A[A-Za-z0-9]+\z/;
15414 19 ▶ return $self->render(text => _message_text('error_token_missing'), status => 401)
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 1 18 19
15415 unless length $token;
15416 18 ▶ unless ($self->_verify_share_token($id, $token)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_verify_share_token($id, $token)) 2 16 18
15417 2 return $self->render(text => _message_text('error_token_invalid'), status => 403);
15418 }
15419  
15420 16 my $analyses = $self->_read_analyses;
15421 return $self->render(text => sprintf(_message_text('error_analysis_not_found'), $id), status => 404)
15422 16 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 2 14 16
15423 14 my $a = $analyses->{$id};
15424  
15425 14 ▶ my @visuals = @{ $a->{visuals} // [] };
Condition100%2/2
$a->{"visuals"} // []
l!l
86
MC/DC100%1/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
15426 14 ▶ unless (@visuals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@visuals) 6 8 14
15427 6 return $self->render(text => 'This shared analysis has no visuals', status => 404);
15428 }
15429  
15430 # Reuse canvas_render logic by temporarily setting id in stash and
15431 # forwarding; set sharing_mode so the template hides admin controls.
15432 8 $self->stash(sharing_mode => 1);
15433 # Delegate to canvas_render which reads stash('id').
15434 8 return $self->canvas_render;
15435 }
15436  
15437 # GET /embed/:id?token=...
15438 20 ▶sub embed_view ($self) {
Subroutine embed_view: called CC=20 SCAR=30.8
Pod documented
15439 20 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
200
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
15440 20 ▶ my $token = $self->param('token') // '';
Condition100%2/2
$self->param("token") // ""
l!l
191
MC/DC100%1/1
$self->param("token") // ""
$self->param("token")
Truth table100%2/2
A
A= $self->param("token")
Aresult
11
00
15441 20 ▶ return $self->render(text => _message_text('error_analysis_id_invalid'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 1 19 20
15442 unless $id =~ /\A[A-Za-z0-9]+\z/;
15443 19 ▶ return $self->render(text => _message_text('error_token_missing'), status => 401)
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 1 18 19
15444 unless length $token;
15445 18 ▶ unless ($self->_verify_share_token($id, $token)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_verify_share_token($id, $token)) 2 16 18
15446 2 return $self->render(text => _message_text('error_token_invalid'), status => 403);
15447 }
15448  
15449 16 my $analyses = $self->_read_analyses;
15450 return $self->render(text => sprintf(_message_text('error_analysis_not_found'), $id), status => 404)
15451 16 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 2 14 16
15452 14 my $a = $analyses->{$id};
15453  
15454 # Every HTML page gets this from the after_dispatch hook in Database::BI;
15455 # the page made to be framed says it itself, so it never depends on that.
15456 # The sites embed_frame_ancestors names are allowed by frame-ancestors,
15457 # which the browsers that know it follow in place of this header; those
15458 # that know only this header refuse them, which is the safe way to fail.
15459 14 $self->res->headers->header('X-Frame-Options' => 'SAMEORIGIN');
15460 14 my @framers = Database::BI::_embed_frame_ancestors($self->app->config);
15461 14 ▶ $self->stash(csp_frame_ancestors => \@framers) if @framers;
Branch: 1 branch
BranchTrueFalseTotal
if @framers 0 14 14
15462  
15463 14 ▶ my @visuals = @{ $a->{visuals} // [] };
Condition100%2/2
$a->{"visuals"} // []
l!l
86
MC/DC100%1/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
15464 14 ▶ unless (@visuals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@visuals) 6 8 14
15465 6 return $self->render(text => 'This shared analysis has no visuals', status => 404);
15466 }
15467  
15468 # Drawn as on the canvas page, each card through its own failure: one card that
15469 # cannot be drawn says so, and leaves the others.
15470 8 my $ctx = $self->_canvas_render_context($a, $self->req->url->query->to_string);
15471 return $self->render(text => $ctx->{error}, status => $ctx->{status})
15472 8 ▶ if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"error"} 0 8 8
15473  
15474 8 my (@rendered, $head);
15475 8 for my $i (0 .. $#visuals) {
15476 8 my $vis = $visuals[$i];
15477 8 ▶ my $card = eval {
Condition50%1/2
eval { do { $self->_render_visual_html($vis, $ctx->{"records"}, $ctx->{"columns"}, $ctx->{"src"}, $i, $ctx->{"spec_key"}, 0, $ctx->{"cross_row"}) } } // {"error", _tidy_error($@)}
l!l
80
MC/DC0%0/1
eval { do { $self->_render_visual_html($vis, $ctx->{"records"}, $ctx->{"columns"}, $ctx->{"src"}, $i, $ctx->{"spec_key"}, 0, $ctx->{"cross_row"}) } } // {"error", _tidy_error($@)}
eval { do { $self->_render_visual_html($vis, $ctx->{"records"}, $ctx->{"columns"}, $ctx->{"src"}, $i, $ctx->{"spec_key"}, 0, $ctx->{"cross_row"}) } }
Truth table50%1/2
A
A= eval { do { $self->_render_visual_html($vis, $ctx->{"records"}, $ctx->{"columns"}, $ctx->{"src"}, $i, $ctx->{"spec_key"}, 0, $ctx->{"cross_row"}) } }
Aresult
11
00
15478 $self->_render_visual_html(
15479 $vis, $ctx->{records}, $ctx->{columns}, $ctx->{src}, $i, $ctx->{spec_key}, 0, $ctx->{cross_row},
15480 8 );
15481 } // { error => _tidy_error($@) };
15482 8 ▶ $head //= $card->{head} if length($card->{head} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($card->{"head"} // "") 0 8 8
Condition20%1/5
$card->{"head"} // ""
l!l
08
$head //= $card->{"head"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$card->{"head"} // ""
$card->{"head"}
MC/DC0%0/2
$head //= $card->{"head"}
$head $card->{"head"}
Truth table50%1/2
A
A= $card->{"head"}
Aresult
11
00
Truth table0%0/3
A //= B
A= $headB= $card->{"head"}
ABresult
1X1
011
000
15483 push @rendered, {
15484 id => $i,
15485 title => ($vis->{title} // $card->{title} // ''),
15486 html => $card->{html},
15487 error => $card->{error},
15488 8 ▶ kind => ($vis->{kind} // ''),
Condition85%6/7
($vis->{"title"} // $card->{"title"}) // ""
l!l
35
$vis->{"title"} // $card->{"title"}
l!l&&r!l&&!r
035
$vis->{"kind"} // ""
l!l
62
MC/DC50%1/2
($vis->{"title"} // $card->{"title"}) // ""
$vis->{"title"} $card->{"title"}
MC/DC100%1/1
$vis->{"kind"} // ""
$vis->{"kind"}
Truth table66%2/3
A // B
A= $vis->{"title"}B= $card->{"title"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $vis->{"kind"}
Aresult
11
00
15489 plain => _text_card_plain($vis),
15490 };
15491 }
15492  
15493 8 $self->stash(embed_mode => 1, sharing_mode => 1);
15494 8 my ($platform, $language) = $self->_resolve_template;
15495 $self->render(
15496 handler => 'tt',
15497 template => "$platform/$language/embed",
15498 format => 'html',
15499 title => $a->{name},
15500 canvas_id => $id,
15501 canvas_name => $a->{name},
15502 8 ▶ visuals => \@rendered,
Condition50%1/2
$head // ""
l!l
08
MC/DC0%0/1
$head // ""
$head
Truth table50%1/2
A
A= $head
Aresult
11
00
15503 card_head => ($head // ''),
15504 language => $language,
15505 );
15506 }
15507  
15508 # GET /a/:id/visual/:n/:format
15509 24 ▶sub visual_download ($self) {
Subroutine visual_download: called CC=16 SCAR=28.1
Pod documented
15510 24 ▶ my $id = $self->stash('id') // '';
Condition50%1/2
$self->stash("id") // ""
l!l
240
MC/DC0%0/1
$self->stash("id") // ""
$self->stash("id")
Truth table50%1/2
A
A= $self->stash("id")
Aresult
11
00
15511 24 ▶ my $n = $self->stash('n') // 0;
Condition50%1/2
$self->stash("n") // 0
l!l
240
MC/DC0%0/1
$self->stash("n") // 0
$self->stash("n")
Truth table50%1/2
A
A= $self->stash("n")
Aresult
11
00
15512 24 ▶ my $format = $self->stash('fmt') // '';
Condition50%1/2
$self->stash("fmt") // ""
l!l
240
MC/DC0%0/1
$self->stash("fmt") // ""
$self->stash("fmt")
Truth table50%1/2
A
A= $self->stash("fmt")
Aresult
11
00
15513  
15514 24 ▶ return $self->render(text => _message_text('error_analysis_id_invalid'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[A-Za-z0-9]+\z/u 1 23 24
15515 unless $id =~ /\A[A-Za-z0-9]+\z/;
15516 23 ▶ return $self->render(text => 'Format must be csv or png', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $format =~ /\A(?:csv|png)\z/u 1 22 23
15517 unless $format =~ /\A(?:csv|png)\z/;
15518 22 ▶ return $self->render(text => 'Visual index must be a non-negative integer', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /\A\d+\z/u 2 20 22
15519 unless $n =~ /\A\d+\z/;
15520  
15521 20 my $analyses = $self->_read_analyses;
15522 return $self->render(text => sprintf(_message_text('error_analysis_not_found'), $id), status => 404)
15523 20 ▶ unless exists $analyses->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $analyses->{$id} 3 17 20
15524 17 my $a = $analyses->{$id};
15525  
15526 17 ▶ my @visuals = @{ $a->{visuals} // [] };
Condition100%2/2
$a->{"visuals"} // []
l!l
116
MC/DC100%1/1
$a->{"visuals"} // []
$a->{"visuals"}
Truth table100%2/2
A
A= $a->{"visuals"}
Aresult
11
00
15527 17 ▶ return $self->render(text => "Visual $n not found", status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless $n < scalar @visuals 7 10 17
15528 unless $n < scalar @visuals;
15529 return $self->render(text => _message_text('error_text_card_no_data'), status => 404)
15530 10 ▶ if ($visuals[$n]{kind} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if ($visuals[$n]{"kind"} // "") eq "text" 0 10 10
Condition100%2/2
$visuals[$n]{"kind"} // ""
l!l
82
MC/DC100%1/1
$visuals[$n]{"kind"} // ""
$visuals[$n]{"kind"}
Truth table100%2/2
A
A= $visuals[$n]{"kind"}
Aresult
11
00
15531  
15532 10 ▶ if ($format eq 'csv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "csv") 9 1 10
15533 9 $self->stash(buffer => 0);
15534 # The canvas names its own source: l= on this URL is no way to reach another.
15535 9 my $ctx = $self->_canvas_render_context($a, $self->req->url->query->to_string);
15536 return $self->render(text => $ctx->{error}, status => $ctx->{status})
15537 9 ▶ if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"error"} 0 9 9
15538 9 my $records = $ctx->{records};
15539 9 ▶ for my $spec (@{ $visuals[$n]{filters} // [] }) {
Condition50%1/2
$visuals[$n]{"filters"} // []
l!l
09
MC/DC0%0/1
$visuals[$n]{"filters"} // []
$visuals[$n]{"filters"}
Truth table50%1/2
A
A= $visuals[$n]{"filters"}
Aresult
11
00
15540 0 $records = _apply_filter_spec($records, $spec);
15541 }
15542 $self->res->headers->content_disposition(
15543 9 "attachment; filename=\"visual-$n.csv\""
15544 );
15545 9 return $self->render(data => _serialize_csv($records, $ctx->{columns}), format => 'txt');
15546 }
15547  
15548 # PNG: not implemented in the server (canvas/SVG rendering is client-side).
15549 # Return 501 with a descriptive message.
15550 1 return $self->render(
15551 text => 'PNG download requires the browser SVG renderer; use the PNG button on the card',
15552 status => 501,
15553 );
15554 }
15555  
15556 # ask_api($self)
15557 #
15558 # POST /api/ask
15559 #
15560 # Translates a plain-English question into an analysis spec by
15561 # calling the configured AI provider. The model receives only column
15562 # names and types -- never raw row data, file paths, or spec_key.
15563 #
15564 # Body params:
15565 # l=<spec> (required) -- left table spec
15566 # q=<question> (required) -- natural language question
15567 # f=<filter> (repeatable, optional)
15568 #
15569 # Returns JSON: { spec, url, valid => 1 }
15570 # On error: { error } with 400, 503, or 502 status
15571 5 ▶sub ask_api ($self) {
Subroutine ask_api: called CC=20 SCAR=37.2
Pod documented
15572 5 ▶ my $api_key = $self->app->config('ai_api_key') // '';
Condition100%2/2
$self->app->config("ai_api_key") // ""
l!l
23
MC/DC100%1/1
$self->app->config("ai_api_key") // ""
$self->app->config("ai_api_key")
Truth table100%2/2
A
A= $self->app->config("ai_api_key")
Aresult
11
00
15573 5 ▶ return $self->render(json => { error => _message_text('ask_unavailable') }, status => 503)
Branch: 1 branch
BranchTrueFalseTotal
unless length $api_key 3 2 5
15574 unless length $api_key;
15575  
15576 2 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
11
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
15577 2 ▶ return $self->render(json => { error => 'Missing required parameter: l' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 1 1 2
15578 unless length $left_spec;
15579  
15580 1 ▶ my $question = $self->param('q') // '';
Condition50%1/2
$self->param("q") // ""
l!l
10
MC/DC0%0/1
$self->param("q") // ""
$self->param("q")
Truth table50%1/2
A
A= $self->param("q")
Aresult
11
00
15581 1 ▶ return $self->render(json => { error => _message_text('ask_no_question') }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $question 0 1 1
15582 unless length $question;
15583 1 ▶ return $self->render(json => { error => sprintf(_message_text('ask_question_too_long'), $ASK_MAX_QUESTION) }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if length $question > $ASK_MAX_QUESTION 0 1 1
15584 if length($question) > $ASK_MAX_QUESTION;
15585  
15586 1 my ($src, $records, $columns, $left_label) = $self->_run_pipeline_source;
15587 1 ▶ return $self->render(json => { error => 'Could not open data source' }, status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 0 1 1
15588 unless defined $records;
15589  
15590 1 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
15591  
15592 # Sample up to 200 rows for type inference; avoids slurping huge files.
15593 1 ▶ my @sample = @{$records}[0 .. ($#$records < 199 ? $#$records : 199)];
Branch: 1 branch
BranchTrueFalseTotal
$records->$#* < 199 ? : 1 0 1
15594  
15595 1 require Database::BI::Ask;
15596 1 my $typed_cols = Database::BI::Ask::infer_column_types(\@sample, $columns);
15597  
15598 1 ▶ my $ai_config = {
Condition50%2/4
$self->app->config("ai_provider") // "anthropic"
l!l
01
$self->app->config("ai_model") // "claude-haiku-5-5"
l!l
01
MC/DC0%0/1
$self->app->config("ai_provider") // "anthropic"
$self->app->config("ai_provider")
MC/DC0%0/1
$self->app->config("ai_model") // "claude-haiku-5-5"
$self->app->config("ai_model")
Truth table50%1/2
A
A= $self->app->config("ai_provider")
Aresult
11
00
Truth table50%1/2
A
A= $self->app->config("ai_model")
Aresult
11
00
15599 provider => $self->app->config('ai_provider') // 'anthropic',
15600 api_key => $api_key,
15601 model => $self->app->config('ai_model') // 'claude-haiku-5-5',
15602 };
15603  
15604 1 ▶ return $self->render(json => { error => sprintf(_message_text('ai_limit_reached'), _ai_calls_per_hour($self)) }, status => 429)
Branch: 1 branch
BranchTrueFalseTotal
unless _ai_call_allowed($self) 0 1 1
15605 unless _ai_call_allowed($self);
15606  
15607 1 ▶ my $result = Database::BI::Ask::ask(
Condition33%1/3
$left_label // $left_spec
l!l&&r!l&&!r
100
MC/DC0%0/2
$left_label // $left_spec
$left_label $left_spec
Truth table33%1/3
A // B
A= $left_labelB= $left_spec
ABresult
1X1
011
000
15608 question => $question,
15609 spec_key => $left_spec,
15610 label => $left_label // $left_spec,
15611 columns => $typed_cols,
15612 ai_config => $ai_config,
15613 );
15614  
15615 1 ▶ if (my $err = $result->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $result->{"error"}) 0 1 1
15616 0 ▶ my ($status, $msg) =
Branch: 5 branches
BranchTrueFalseTotal
$err eq 'ask_unavailable' ? : 0 0 0
$err eq 'ask_no_question' ? : 0 0 0
$err eq 'ask_provider_error' ? : 0 0 0
$err eq 'ask_unparseable' ? : 0 0 0
$err =~ /\Aask_invalid_spec:(.+)\z/u ? : 0 0 0
15617 $err eq 'ask_unavailable' ? (503, _message_text('ask_unavailable'))
15618 : $err eq 'ask_no_question' ? (400, _message_text('ask_no_question'))
15619 : $err eq 'ask_provider_error' ? (502, _message_text('ask_provider_error'))
15620 : $err eq 'ask_unparseable' ? (422, _message_text('ask_unparseable'))
15621 : $err =~ /\Aask_invalid_spec:(.+)\z/ ? (422, sprintf(_message_text('ask_invalid_spec'), $1))
15622 : (500, $err);
15623 0 return $self->render(json => { error => $msg }, status => $status);
15624 }
15625  
15626 1 return $self->render(json => $result);
15627 }
15628  
15629 # summary_api($self)
15630 #
15631 # POST /api/summary
15632 #
15633 # Runs the same pipeline as insights_view, computes insights, then generates
15634 # a narrative summary via Database::BI::Summary::generate().
15635 #
15636 # Body params:
15637 # l=<spec> (required) -- left table spec
15638 # f=<filter> (repeatable, optional)
15639 # ai=1 (optional) -- request LLM polish (honoured only when ai_api_key
15640 # is set in the app config)
15641 #
15642 # Returns JSON: { text, source, cached }
15643 # On error: { error } with 400 or 404 status
15644 3 ▶sub summary_api ($self) {
Subroutine summary_api: called CC=10 SCAR=24.3
Pod documented
15645 3 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
21
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
15646 3 ▶ return $self->render(json => { error => 'Missing required parameter: l' }, status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 1 2 3
15647 unless length $left_spec;
15648  
15649 2 my ($src, $records, $columns) = $self->_run_pipeline_source;
15650 2 ▶ return $self->render(json => { error => 'Could not open data source' }, status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 1 1 2
15651 unless defined $records;
15652  
15653 1 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
15654 0 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(json => { error => $e }, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
15655  
15656 1 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 0 1 1
15657  
15658 1 my $row_count = scalar @$records;
15659 1 (my $label = $left_spec) =~ s/\A(?:table:|path:|url:|mariadb:|redis:[^|]+\|)//;
15660  
15661 1 require Database::BI::Insights;
15662 1 my $insights = Database::BI::Insights::compute(
15663 records => $records,
15664 columns => $columns,
15665 spec_key => $left_spec,
15666 );
15667  
15668 1 ▶ my $use_ai = $self->param('ai') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('ai') ? : 0 1 1
15669 1 ▶ my $ai_config = {
Condition50%3/6
$self->app->config("ai_provider") // "anthropic"
l!l
01
$self->app->config("ai_api_key") // ""
l!l
01
$self->app->config("ai_model") // "claude-haiku-5-5"
l!l
01
MC/DC0%0/1
$self->app->config("ai_provider") // "anthropic"
$self->app->config("ai_provider")
MC/DC0%0/1
$self->app->config("ai_api_key") // ""
$self->app->config("ai_api_key")
MC/DC0%0/1
$self->app->config("ai_model") // "claude-haiku-5-5"
$self->app->config("ai_model")
Truth table50%1/2
A
A= $self->app->config("ai_provider")
Aresult
11
00
Truth table50%1/2
A
A= $self->app->config("ai_api_key")
Aresult
11
00
Truth table50%1/2
A
A= $self->app->config("ai_model")
Aresult
11
00
15670 provider => $self->app->config('ai_provider') // 'anthropic',
15671 api_key => $self->app->config('ai_api_key') // '',
15672 model => $self->app->config('ai_model') // 'claude-haiku-5-5',
15673 };
15674  
15675 1 require Database::BI::Summary;
15676 my $result = Database::BI::Summary::generate(
15677 insights => $insights,
15678 spec_key => $left_spec,
15679 label => $label,
15680 row_count => $row_count,
15681 use_ai => $use_ai,
15682 ai_config => $ai_config,
15683 0 ▶ ai_allowed => sub { _ai_call_allowed($self) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15684 1 );
15685  
15686 1 return $self->render(json => $result);
15687 }
15688  
15689 # _ai_calls_per_hour($c) -> $n
15690 #
15691 # Config ai_calls_per_hour, else $AI_CALLS_PER_HOUR; 0 means no limit.
15692 1 ▶sub _ai_calls_per_hour {
Subroutine _ai_calls_per_hour: called CC=3 SCAR=12.5
15693 1 my ($c) = @_;
15694 1 my $n = $c->app->config('ai_calls_per_hour');
15695 1 ▶ return defined($n) && $n =~ /\A\d+\z/ ? $n : $AI_CALLS_PER_HOUR;
Branch: 1 branch
BranchTrueFalseTotal
defined $n && $n =~ /\A\d+\z/u ? : 0 1 1
Condition33%1/3
defined $n and $n =~ /\A\d+\z/u
!ll&&!rl&&r
100
MC/DC0%0/2
defined $n and $n =~ /\A\d+\z/u
defined $n $n =~ /\A\d+\z/u
Truth table33%1/3
A and B
A= defined $nB= $n =~ /\A\d+\z/u
ABresult
0X0
100
111
15696 }
15697  
15698 # _ai_call_allowed($c) -> 1 | 0
15699 #
15700 # Takes one provider call from this process's budget: the times of the calls
15701 # in the last hour, kept per application (tests build many in one process).
15702 # Returns 0, taking nothing, when the hour's calls are used up.
15703 5 my %AI_CALLS;
15704 1 ▶sub _ai_call_allowed {
Subroutine _ai_call_allowed: called CC=4 SCAR=14.2
15705 1 my ($c) = @_;
15706 1 my $max = _ai_calls_per_hour($c);
15707 1 ▶ return 1 unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 0 1 1
15708 1 my $now = time;
15709 1 ▶ my $calls = $AI_CALLS{ Scalar::Util::refaddr($c->app) } //= [];
Condition50%1/2
$AI_CALLS{builtin::refaddr($c->app)} //= []
l!l
01
MC/DC0%0/1
$AI_CALLS{builtin::refaddr($c->app)} //= []
$AI_CALLS{builtin::refaddr($c->app)}
Truth table50%1/2
A
A= $AI_CALLS{builtin::refaddr($c->app)}
Aresult
11
00
15710 0 @{$calls} = grep { $_ > $now - $AI_WINDOW_S } @{$calls};
15711 1 ▶ return 0 if @{$calls} >= $max;
Branch: 1 branch
BranchTrueFalseTotal
if @{$calls;} >= $max 0 1 1
15712 1 push @{$calls}, $now;
15713 1 return 1;
15714 }
15715  
15716 # insights_view($self)
15717 #
15718 # Accepts the same l=, j=, f=, c= pipeline as /stats. Runs the standard
15719 # pipeline (date normalisation, calc fields, filters), then delegates to
15720 # Database::BI::Insights::compute() for pure-arithmetic insight detection.
15721 #
15722 # No LLM or external service is invoked. Every returned insight carries a
15723 # view_url linking to the chart or stats page that shows the underlying data.
15724 #
15725 # Stash keys:
15726 # insights -- arrayref of insight hashrefs (kind/headline/detail/view_url/method)
15727 # left_spec -- the l= value
15728 # left_label -- human-readable data-source label
15729 # row_count -- number of rows analysed
15730 # base_url -- current request URL (passed to Insights for fallback URLs)
15731 47 ▶sub insights_view ($self) {
Subroutine insights_view: called CC=16 SCAR=30.2
Pod documented
15732 47 ▶ my $left_spec = $self->param('l') // '';
Condition100%2/2
$self->param("l") // ""
l!l
461
MC/DC100%1/1
$self->param("l") // ""
$self->param("l")
Truth table100%2/2
A
A= $self->param("l")
Aresult
11
00
15733 47 ▶ return $self->render(text => 'Missing l= parameter', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless length $left_spec 2 45 47
15734 unless length $left_spec;
15735  
15736 45 ▶ my $back = _safe_back_url($self->param('back')) // '/';
Condition50%1/2
_safe_back_url($self->param("back")) // "/"
l!l
045
MC/DC0%0/1
_safe_back_url($self->param("back")) // "/"
_safe_back_url($self->param("back"))
Truth table50%1/2
A
A= _safe_back_url($self->param("back"))
Aresult
11
00
15737 45 my $back2 = _safe_back_url($self->param('back2'));
15738 45 ▶ my $back2_lbl = $self->param('back2_label') // '';
Condition50%1/2
$self->param("back2_label") // ""
l!l
045
MC/DC0%0/1
$self->param("back2_label") // ""
$self->param("back2_label")
Truth table50%1/2
A
A= $self->param("back2_label")
Aresult
11
00
15739  
15740 45 my ($src, $records, $columns) = $self->_run_pipeline_source;
15741 45 ▶ return $self->render(text => 'Could not open data source', status => 404)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $records 3 42 45
15742 unless defined $records;
15743  
15744 42 eval { ($records, $columns) = $self->_apply_calc_fields($records, $columns) };
15745 42 ▶ if ($@) { (my $e = $@) =~ s/ at \S+ line \d+\.?\n?$//; return $self->render(text => $e, status => 400) }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 41 42
15746  
15747 41 ▶ return unless $self->_reduce_or_render(\$records, \$columns);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reduce_or_render(\$records, \$columns) 1 40 41
15748  
15749 40 my $row_count = scalar @$records;
15750 40 (my $left_label = $left_spec) =~ s/\A(?:table:|path:|url:|mariadb:|redis:[^|]+\|)//;
15751  
15752 40 require Database::BI::Insights;
15753 40 my $insights = Database::BI::Insights::compute(
15754 records => $records,
15755 columns => $columns,
15756 spec_key => $left_spec,
15757 base_url => $self->req->url->path_query,
15758 );
15759  
15760 40 my ($platform, $language) = $self->_resolve_template;
15761 40 ▶ $self->render(
Branch: 3 branches
BranchTrueFalseTotal
length($self->app->config('ai_api_key') // '') ? : 0 40 40
$back2 ? : 0 40 40
$back2 ? : 0 40 40
Condition27%3/11
$self->app->config("ai_api_key") // ""
l!l
040
$back2 // $back
l!l&&r!l&&!r
0400
$back2_lbl or "Back"
l!l
00
$self->param("back_label") // "Back to table"
l!l
040
$self->param("back_label") // "Back to table"
l!l
00
MC/DC0%0/1
$self->app->config("ai_api_key") // ""
$self->app->config("ai_api_key")
MC/DC0%0/2
$back2 // $back
$back2 $back
MC/DC0%0/1
$back2_lbl or "Back"
$back2_lbl
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
MC/DC0%0/1
$self->param("back_label") // "Back to table"
$self->param("back_label")
Truth table50%1/2
A
A= $self->app->config("ai_api_key")
Aresult
11
00
Truth table33%1/3
A // B
A= $back2B= $back
ABresult
1X1
011
000
Truth table0%0/2
A
A= $back2_lbl
Aresult
11
00
Truth table50%1/2
A
A= $self->param("back_label")
Aresult
11
00
Truth table0%0/2
A
A= $self->param("back_label")
Aresult
11
00
15762 handler => 'tt',
15763 template => "$platform/$language/insights",
15764 format => 'html',
15765 title => 'Automatic insights',
15766 insights => $insights,
15767 left_spec => $left_spec,
15768 left_label => $left_label,
15769 row_count => $row_count,
15770 ai_available => (length($self->app->config('ai_api_key') // '') ? 1 : 0),
15771 back_url => ($back2 // $back),
15772 back_label => ($back2 ? ($back2_lbl || 'Back') : ($self->param('back_label') // 'Back to table')),
15773 ($back2 ? (back2_url => $back, back2_label => ($self->param('back_label') // 'Back to table')) : ()),
15774 language => $language,
15775 );
15776 }
15777  
15778 5 1;
15779  
15780 __END__
↑ Summary blib/lib/Database/BI/Model/Boundary.pm »