include/punk_observe/po_view.h

filter: Statementstmt 94.4% 220 / 233 Branchbran 77.2% 190 / 246 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.5% 410 / 479 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_store.h ↑ Summary include/punk_observe/po_rollup.h »
1 /* po_view.h - the formatting every screen does once per row.
2 *
3 * Nine small functions, and their smallness is the point: they are called
4 * once per row per column, so a page of two thousand log lines runs them ten
5 * thousand times. In Perl that is ten thousand sub calls, ten thousand
6 * regular expressions and a scalar allocated for each.
7 *
8 * A NANOSECOND TIMESTAMP IS A DECIMAL STRING, NOT A NUMBER.
9 *
10 * Past 2^53 a double has lost the low digits, so splitting an instant into
11 * seconds and a fraction by dividing by a billion is the bug that renders two
12 * lines a microsecond apart as the same time. The split is done on the digits
13 * here, exactly as the Perl did it, and the seconds go to a calendar
14 * conversion that is integer arithmetic throughout.
15 *
16 * THE CALENDAR IS COMPUTED, NOT ASKED FOR.
17 *
18 * `gmtime` is not thread-safe, `gmtime_r` is POSIX and absent on MSVC, and
19 * `gmtime_s` is MSVC's with the arguments the other way round. Rather than
20 * three spellings behind two probes, the civil date is computed from the day
21 * number directly - it is a dozen lines of integer arithmetic, correct for
22 * any year, and the same on every platform.
23 */
24 #ifndef PO_VIEW_H
25 #define PO_VIEW_H
26  
27 #include "punk_observe/po_compat.h"
28  
29 /* ---- severity and span kind ----------------------------------------------
30 *
31 * OTLP's severity is a 24-point scale in six bands of four. The BAND is what
32 * a person reads and what the stylesheet colours; the number is what sorts
33 * and what a query compares. Both are kept, and this is the mapping between
34 * them. */
35 1474 static const char *po_severity_name(int n) {
36 1474 ▶ if (n >= 1 && n <= 4) return "trace";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 1 && n <= 4) return "trace"; 1474 0 1474
if (n >= 1 && n <= 4) return "trace"; 0 1474 1474
37 1474 ▶ if (n >= 5 && n <= 8) return "debug";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 5 && n <= 8) return "debug"; 1474 0 1474
if (n >= 5 && n <= 8) return "debug"; 0 1474 1474
38 1474 ▶ if (n >= 9 && n <= 12) return "info";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 9 && n <= 12) return "info"; 1474 0 1474
if (n >= 9 && n <= 12) return "info"; 1451 23 1474
39 23 ▶ if (n >= 13 && n <= 16) return "warn";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 13 && n <= 16) return "warn"; 23 0 23
if (n >= 13 && n <= 16) return "warn"; 0 23 23
40 23 ▶ if (n >= 17 && n <= 20) return "error";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 17 && n <= 20) return "error"; 23 0 23
if (n >= 17 && n <= 20) return "error"; 23 0 23
41 0 ▶ if (n >= 21 && n <= 24) return "fatal";
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 21 && n <= 24) return "fatal"; 0 0 0
if (n >= 21 && n <= 24) return "fatal"; 0 0 0
42 0 return "unset";
43 }
44  
45 /* OTLP SpanKind. 0 is UNSPECIFIED and 1 is INTERNAL, and both render as
46 * internal: a span that did not say is not a different kind of span, it is a
47 * span nobody labelled. */
48 10 static const char *po_span_kind_name(int k) {
49 10 switch (k) {
50 5 case 2: return "server";
51 5 case 3: return "client";
52 0 case 4: return "producer";
53 0 case 5: return "consumer";
54 0 default: return "internal";
55 }
56 }
57  
58 /* ---- the calendar, from a day number ------------------------------------
59 *
60 * Howard Hinnant's civil_from_days, which is exact for any year and needs no
61 * table and no library. The era arithmetic is what makes it branch-free
62 * across century leap years - the case a hand-rolled version gets wrong once
63 * every hundred years and nobody notices for a while. */
64 1422 static void po_civil(po_i64 days, int *y, int *m, int *d) {
65 1422 po_i64 z = days + 719468;
66 1422 ▶ po_i64 era = (z >= 0 ? z : z - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
po_i64 era = (z >= 0 ? z : z - 146096) / 146097; 0 1422 1422
67 1422 po_u64 doe = (po_u64)(z - era * 146097);
68 1422 po_u64 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
69 1422 po_i64 yy = (po_i64)yoe + era * 400;
70 1422 po_u64 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
71 1422 po_u64 mp = (5 * doy + 2) / 153;
72 1422 po_u64 dd = doy - (153 * mp + 2) / 5 + 1;
73 1422 ▶ po_u64 mm = mp + (mp < 10 ? 3 : -9);
Branch: 1 branch
BranchTrueFalseTotal
po_u64 mm = mp + (mp < 10 ? 3 : -9); 1422 0 1422
74 1422 *y = (int)(yy + (mm <= 2 ? 1 : 0));
75 1422 *m = (int)mm;
76 1422 *d = (int)dd;
77 1422 }
78  
79 /* Split a decimal nanosecond string into whole seconds and the fraction.
80 *
81 * On the DIGITS. The seconds still have to fit a 64-bit integer to be turned
82 * into a date, which they do for any year this side of the sun going out. */
83 2984 static int po_ns_split(const char *ns, size_t len, po_u64 *secs, char frac[10]) {
84 size_t i;
85 size_t whole;
86  
87 2984 ▶ if (!ns || !len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ns || !len) return 0; 2984 0 2984
if (!ns || !len) return 0; 0 2984 2984
88 59614 ▶ for (i = 0; i < len; i++) if (ns[i] < '0' || ns[i] > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (ns[i] < '0' || ns[i] > '9') return 0; 56630 0 56630
for (i = 0; i < len; i++) if (ns[i] < '0' || ns[i] > '9') return 0; 0 56630 56630
for (i = 0; i < len; i++) if (ns[i] < '0' || ns[i] > '9') return 0; 56630 2984 59614
89  
90 2984 ▶ if (len > 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 9) { 2980 4 2984
91 2980 whole = len - 9;
92 2980 memcpy(frac, ns + whole, 9);
93 }
94 else {
95 4 whole = 0;
96 /* Fewer than ten digits is a sub-second instant: pad on the LEFT, or
97 * "5" nanoseconds renders as half a second. */
98 4 memset(frac, '0', 9 - len);
99 4 memcpy(frac + (9 - len), ns, len);
100 }
101 2984 frac[9] = '\0';
102  
103 2984 *secs = 0;
104 32784 ▶ for (i = 0; i < whole; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < whole; i++) { 29800 2984 32784
105 29800 ▶ if (*secs > (PO_U64_MAX - (po_u64)(ns[i] - '0')) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*secs > (PO_U64_MAX - (po_u64)(ns[i] - '0')) / 10) return 0; 0 29800 29800
106 29800 *secs = *secs * 10 + (po_u64)(ns[i] - '0');
107 }
108 2984 return 1;
109 }
110  
111 /* HH:MM:SS.mmm, which is what a log line wants: the date is the same for
112 * every row on the page and the milliseconds are what tell two of them
113 * apart. */
114 1562 static size_t po_fmt_time(const char *ns, size_t len, char *out) {
115 1562 po_u64 secs = 0;
116 char frac[10];
117 po_u64 sod;
118  
119 1562 ▶ if (!po_ns_split(ns, len, &secs, frac)) { out[0] = '\0'; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_ns_split(ns, len, &secs, frac)) { out[0] = '\0'; return 0; } 0 1562 1562
120 1562 sod = secs % 86400;
121 1562 out[0] = (char)('0' + (int)(sod / 36000));
122 1562 out[1] = (char)('0' + (int)((sod / 3600) % 10));
123 1562 out[2] = ':';
124 1562 out[3] = (char)('0' + (int)((sod % 3600) / 600));
125 1562 out[4] = (char)('0' + (int)((sod % 3600) / 60 % 10));
126 1562 out[5] = ':';
127 1562 out[6] = (char)('0' + (int)((sod % 60) / 10));
128 1562 out[7] = (char)('0' + (int)(sod % 10));
129 1562 out[8] = '.';
130 1562 out[9] = frac[0];
131 1562 out[10] = frac[1];
132 1562 out[11] = frac[2];
133 1562 out[12] = '\0';
134 1562 return 12;
135 }
136  
137 /* The whole instant, for a detail page where there is only one of them. */
138 1422 static size_t po_fmt_date(const char *ns, size_t len, char *out) {
139 1422 po_u64 secs = 0;
140 char frac[10];
141 po_i64 days;
142 po_u64 sod;
143 int y, m, d;
144 1422 size_t n = 0;
145  
146 1422 ▶ if (!po_ns_split(ns, len, &secs, frac)) { out[0] = '\0'; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_ns_split(ns, len, &secs, frac)) { out[0] = '\0'; return 0; } 0 1422 1422
147 1422 days = (po_i64)(secs / 86400);
148 1422 sod = secs % 86400;
149 1422 po_civil(days, &y, &m, &d);
150  
151 { /* %04d-%02d-%02d %02d:%02d:%02dZ, without a formatter */
152 int parts[6];
153 1422 int widths[6] = { 4, 2, 2, 2, 2, 2 };
154 1422 const char *seps = "-- ::";
155 int i, j;
156 1422 parts[0] = y; parts[1] = m; parts[2] = d;
157 1422 parts[3] = (int)(sod / 3600);
158 1422 parts[4] = (int)((sod / 60) % 60);
159 1422 parts[5] = (int)(sod % 60);
160 9954 ▶ for (i = 0; i < 6; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) { 8532 1422 9954
161 8532 int v = parts[i], w = widths[i];
162 28440 ▶ for (j = w - 1; j >= 0; j--) { out[n + j] = (char)('0' + v % 10); v /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
for (j = w - 1; j >= 0; j--) { out[n + j] = (char)('0' + v % 10); v /= 10; } 19908 8532 28440
163 8532 n += (size_t)w;
164 8532 ▶ if (i < 5) out[n++] = seps[i];
Branch: 1 branch
BranchTrueFalseTotal
if (i < 5) out[n++] = seps[i]; 7110 1422 8532
165 }
166 1422 out[n++] = 'Z';
167 1422 out[n] = '\0';
168 }
169 1422 return n;
170 }
171  
172 /* A duration in the unit a person would say it in, and NEVER an exponent:
173 * `3.26s`, `812ms`, `40us`. A path attribute or a table cell containing
174 * `1e-05` is a cell nobody can read and, in an SVG, one that stops the chart
175 * drawing at all. */
176 /* n / d, rounded half to even. */
177 481 static po_u64 po_div_round_even(po_u64 n, po_u64 d) {
178 481 po_u64 q = n / d, r = n % d;
179 481 po_u64 half = d / 2;
180 481 ▶ if (r > half) return q + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r > half) return q + 1; 3 478 481
181 478 ▶ if (r < half) return q;
Branch: 1 branch
BranchTrueFalseTotal
if (r < half) return q; 478 0 478
182 0 ▶ return (q & 1) ? q + 1 : q; /* exactly half: to the even quotient */
Branch: 1 branch
BranchTrueFalseTotal
return (q & 1) ? q + 1 : q; /* exactly half: to the even quotient */ 0 0 0
183 }
184  
185 478 static size_t po_fmt_dur(po_u64 ns, char *out) {
186 po_u64 v;
187 478 int frac_digits = 0;
188 const char *unit;
189 478 size_t n = 0;
190 char tmp[24];
191 478 size_t t = 0;
192  
193 478 ▶ if (!ns) { out[0] = '0'; out[1] = '\0'; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) { out[0] = '0'; out[1] = '\0'; return 1; } 0 478 478
194  
195 /* ROUND HALF TO EVEN, which is what printf's %.0f does and therefore
196 * what every duration on every page has always shown. Half-up is the
197 * obvious spelling and differs on exactly the .5 cases - 500ns renders
198 * as 0us either way it is argued, and changing it during a port would be
199 * a silent difference in output nobody asked for. */
200 478 ▶ if (ns < 1000000ULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ns < 1000000ULL) { 410 68 478
201 410 v = po_div_round_even(ns, 1000);
202 410 unit = "us";
203 }
204 68 ▶ else if (ns < 1000000000ULL) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ns < 1000000000ULL) { 66 2 68
205 66 v = po_div_round_even(ns, 1000000);
206 66 unit = "ms";
207 }
208 else {
209 /* seconds to two decimals, which is where the fraction appears */
210 2 v = po_div_round_even(ns, 10000000); /* hundredths */
211 2 unit = "s";
212 2 frac_digits = 2;
213 }
214  
215 478 ▶ if (frac_digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits) { 2 476 478
216 2 po_u64 whole = v / 100, cents = v % 100;
217 2 ▶ if (!whole) tmp[t++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!whole) tmp[t++] = '0'; 0 2 2
218 2 else { char w[24]; size_t k = 0;
219 6 ▶ while (whole) { w[k++] = (char)('0' + (int)(whole % 10)); whole /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (whole) { w[k++] = (char)('0' + (int)(whole % 10)); whole /= 10; } 4 2 6
220 6 ▶ while (k) tmp[t++] = w[--k]; }
Branch: 1 branch
BranchTrueFalseTotal
while (k) tmp[t++] = w[--k]; } 4 2 6
221 2 tmp[t++] = '.';
222 2 tmp[t++] = (char)('0' + (int)(cents / 10));
223 2 tmp[t++] = (char)('0' + (int)(cents % 10));
224 }
225 476 ▶ else if (!v) tmp[t++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
else if (!v) tmp[t++] = '0'; 0 476 476
226 else {
227 476 char w[24]; size_t k = 0;
228 1287 ▶ while (v) { w[k++] = (char)('0' + (int)(v % 10)); v /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (v) { w[k++] = (char)('0' + (int)(v % 10)); v /= 10; } 811 476 1287
229 1287 ▶ while (k) tmp[t++] = w[--k];
Branch: 1 branch
BranchTrueFalseTotal
while (k) tmp[t++] = w[--k]; 811 476 1287
230 }
231  
232 478 memcpy(out, tmp, t); n = t;
233 1432 ▶ while (*unit) out[n++] = *unit++;
Branch: 1 branch
BranchTrueFalseTotal
while (*unit) out[n++] = *unit++; 954 478 1432
234 478 out[n] = '\0';
235 478 return n;
236 }
237  
238 /* Thousands separators. A five-figure count with no commas is a number
239 * people misread by an order of magnitude. */
240 202 static size_t po_fmt_count(const char *s, size_t len, char *out, size_t cap) {
241 202 size_t i, n = 0, digits = 0;
242 481 ▶ for (i = 0; i < len; i++) if (s[i] >= '0' && s[i] <= '9') digits++;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (s[i] >= '0' && s[i] <= '9') digits++; 279 0 279
for (i = 0; i < len; i++) if (s[i] >= '0' && s[i] <= '9') digits++; 279 0 279
for (i = 0; i < len; i++) if (s[i] >= '0' && s[i] <= '9') digits++; 279 202 481
243 202 ▶ if (digits != len || !len) {
Branch: 2 branches
BranchTrueFalseTotal
if (digits != len || !len) { 202 0 202
if (digits != len || !len) { 0 202 202
244 0 size_t k = len < cap - 1 ? len : cap - 1;
245 0 memcpy(out, s, k); out[k] = '\0';
246 0 return k;
247 }
248 481 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 279 202 481
249 279 ▶ if (i && (len - i) % 3 == 0 && n + 1 < cap) out[n++] = ',';
Branch: 3 branches
BranchTrueFalseTotal
if (i && (len - i) % 3 == 0 && n + 1 < cap) out[n++] = ','; 77 202 279
if (i && (len - i) % 3 == 0 && n + 1 < cap) out[n++] = ','; 8 69 77
if (i && (len - i) % 3 == 0 && n + 1 < cap) out[n++] = ','; 8 0 8
250 279 ▶ if (n + 1 < cap) out[n++] = s[i];
Branch: 1 branch
BranchTrueFalseTotal
if (n + 1 < cap) out[n++] = s[i]; 279 0 279
251 }
252 202 out[n] = '\0';
253 202 return n;
254 }
255  
256 /* Percent-encoding for a query string.
257 *
258 * NOT HTML escaping, which the template does afterwards, and not the same
259 * job: a `+` left as a plus is a SPACE by the time the form comes back, and
260 * the query that returns is not the query that was sent. */
261 352 static size_t po_url_esc(const char *s, size_t len, char *out, size_t cap) {
262 static const char hex[] = "0123456789ABCDEF";
263 352 size_t i, n = 0;
264 9294 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 8942 352 9294
265 8942 unsigned char c = (unsigned char)s[i];
266 8942 ▶ int safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 3 branches
BranchTrueFalseTotal
int safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 6636 4 6640
int safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 6620 2318 8938
int safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 470 6150 6620
267 2788 ▶ || (c >= '0' && c <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') 922 1866 2788
|| (c >= '0' && c <= '9') 609 313 922
268 17884 ▶ || c == '-' || c == '_' || c == '.' || c == '~';
Branch: 5 branches
BranchTrueFalseTotal
|| c == '-' || c == '_' || c == '.' || c == '~'; 6640 2302 8942
|| c == '-' || c == '_' || c == '.' || c == '~'; 2472 3 2475
|| c == '-' || c == '_' || c == '.' || c == '~'; 2472 0 2472
|| c == '-' || c == '_' || c == '.' || c == '~'; 2377 95 2472
|| c == '-' || c == '_' || c == '.' || c == '~'; 16 2361 2377
269 8942 ▶ if (safe) { if (n + 1 < cap) out[n++] = (char)c; }
Branch: 2 branches
BranchTrueFalseTotal
if (safe) { if (n + 1 < cap) out[n++] = (char)c; } 6581 2361 8942
if (safe) { if (n + 1 < cap) out[n++] = (char)c; } 6581 0 6581
270 2361 ▶ else if (n + 3 < cap) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n + 3 < cap) { 2361 0 2361
271 2361 out[n++] = '%';
272 2361 out[n++] = hex[c >> 4];
273 2361 out[n++] = hex[c & 15];
274 }
275 }
276 352 out[n] = '\0';
277 352 return n;
278 }
279  
280 /* A percentage of a span, clamped. Charts hand this a zero range more often
281 * than anything else - one point, or a gauge that has not moved - and a
282 * division by it is the whole chart gone rather than one bar. */
283 20 static double po_pct(po_u64 v, po_u64 total) {
284 po_u64 p;
285 20 ▶ if (!total) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!total) return 0; 0 20 20
286 /* A WHOLE number, truncated, which is what this has always returned. A
287 * bar is positioned in CSS percent and a fraction of one percent is
288 * under a pixel on any screen - but changing the value during a port
289 * would move every bar on every chart by a hair, for nothing. */
290 20 p = 100 * v / total;
291 20 ▶ return (double)(p > 100 ? 100 : p);
Branch: 1 branch
BranchTrueFalseTotal
return (double)(p > 100 ? 100 : p); 20 0 20
292 }
293  
294 /* A byte count in the unit a person would say it in.
295 *
296 * The boundary is 1024, not 1000: this counts what a store occupies, and
297 * every tool an operator would compare it against - du, ls, df - uses the
298 * binary one. Being right about the SI meaning of a kilobyte and wrong about
299 * the number on the next terminal is the worse answer. */
300 11 static size_t po_fmt_bytes(po_u64 n, char *out) {
301 static const char *UNIT[] = { " B", " KB", " MB", " GB" };
302 11 po_u64 div = 1;
303 11 int u = 0;
304 11 size_t k = 0;
305 po_u64 whole, tenth;
306 char tmp[24];
307 11 size_t t = 0;
308  
309 14 ▶ while (u < 3 && n / div >= 1024) { div *= 1024; u++; }
Branch: 2 branches
BranchTrueFalseTotal
while (u < 3 && n / div >= 1024) { div *= 1024; u++; } 14 0 14
while (u < 3 && n / div >= 1024) { div *= 1024; u++; } 3 11 14
310  
311 11 ▶ if (!u) { /* bytes: no fraction at all */
Branch: 1 branch
BranchTrueFalseTotal
if (!u) { /* bytes: no fraction at all */ 8 3 11
312 8 po_u64 v = n;
313 8 ▶ if (!v) tmp[t++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!v) tmp[t++] = '0'; 3 5 8
314 5 else { char w[24]; size_t z = 0;
315 20 ▶ while (v) { w[z++] = (char)('0' + (int)(v % 10)); v /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (v) { w[z++] = (char)('0' + (int)(v % 10)); v /= 10; } 15 5 20
316 20 ▶ while (z) tmp[t++] = w[--z]; }
Branch: 1 branch
BranchTrueFalseTotal
while (z) tmp[t++] = w[--z]; } 15 5 20
317 8 memcpy(out, tmp, t); k = t;
318 }
319 else {
320 /* One decimal, rounded half to even like the %.1f it replaces. */
321 3 po_u64 tenths = po_div_round_even(n * 10, div);
322 3 whole = tenths / 10;
323 3 tenth = tenths % 10;
324 3 ▶ if (!whole) tmp[t++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!whole) tmp[t++] = '0'; 0 3 3
325 3 else { char w[24]; size_t z = 0;
326 6 ▶ while (whole) { w[z++] = (char)('0' + (int)(whole % 10)); whole /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (whole) { w[z++] = (char)('0' + (int)(whole % 10)); whole /= 10; } 3 3 6
327 6 ▶ while (z) tmp[t++] = w[--z]; }
Branch: 1 branch
BranchTrueFalseTotal
while (z) tmp[t++] = w[--z]; } 3 3 6
328 3 tmp[t++] = '.';
329 3 tmp[t++] = (char)('0' + (int)tenth);
330 3 memcpy(out, tmp, t); k = t;
331 }
332 {
333 11 const char *up = UNIT[u];
334 36 ▶ while (*up) out[k++] = *up++;
Branch: 1 branch
BranchTrueFalseTotal
while (*up) out[k++] = *up++; 25 11 36
335 }
336 11 out[k] = '\0';
337 11 return k;
338 }
339  
340 /* ---- chart coordinates ----------------------------------------------------
341 *
342 * The x of a point, as a fraction of the window.
343 *
344 * THE SUBTRACTION IS ON INTEGERS. The Perl this replaces wrote
345 * `($row->{t} - $t0) / ($t1 - $t0)`, which converts two nanosecond instants
346 * to doubles first and loses their low digits before the difference is even
347 * taken. On a 720-pixel chart the error is far under a pixel, so nothing was
348 * ever visibly wrong - but it is the same mistake the rest of this
349 * distribution goes out of its way to avoid, and the fix costs nothing:
350 * subtract first, in the type that holds them, then divide once. */
351 30 static double po_chart_x(po_u64 t, po_u64 t0, po_u64 t1, double width) {
352 30 ▶ po_u64 span = t1 > t0 ? t1 - t0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
po_u64 span = t1 > t0 ? t1 - t0 : 1; 30 0 30
353 30 ▶ po_u64 off = t > t0 ? t - t0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
po_u64 off = t > t0 ? t - t0 : 0; 29 1 30
354 30 ▶ if (off > span) off = span;
Branch: 1 branch
BranchTrueFalseTotal
if (off > span) off = span; 0 30 30
355 30 return (double)off / (double)span * width;
356 }
357  
358 /* The y of a value, flipped: y grows DOWNWARD in SVG and upward on a chart. */
359 0 static double po_chart_y(double v, double lo, double hi, double height) {
360 0 double range = hi - lo;
361 0 ▶ if (range <= 0) range = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (range <= 0) range = 1; 0 0 0
362 0 return height - height * (v - lo) / range;
363 }
364  
365 /* ---- the time window ------------------------------------------------------
366 *
367 * THE DEFAULT IS AN HOUR AND THE CONTROL IS NOT OPTIONAL.
368 *
369 * A page that reads everything gets slower every day it runs, so the default
370 * has to be bounded. But a bounded default with no way to widen it is a dead
371 * end: what it says to somebody whose data is from this morning is "No traces
372 * in this window", which reads as "there is no data" and is a different and
373 * wrong answer.
374 *
375 * `all` is unbounded on purpose and is the only range that is. Nothing
376 * downstream special-cases it - an absent bound is the store's own "no
377 * limit", so the scan simply does not skip on time. */
378 typedef struct { const char *key; po_u64 secs; const char *label; } po_range;
379  
380 static const po_range PO_RANGES[] = {
381 { "15m", 900, "last 15 minutes" },
382 { "1h", 3600, "last hour" },
383 { "6h", 21600, "last 6 hours" },
384 { "24h", 86400, "last 24 hours" },
385 { "7d", 604800, "last 7 days" },
386 { "30d", 2592000, "last 30 days" },
387 { "all", 0, "everything" }
388 };
389 #define PO_NRANGES (int)(sizeof(PO_RANGES) / sizeof(PO_RANGES[0]))
390  
391 88 static const po_range *po_range_find(const char *k, size_t len) {
392 int i;
393 88 ▶ if (!k || !len) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !len) return NULL; 88 0 88
if (!k || !len) return NULL; 0 88 88
394 230 ▶ for (i = 0; i < PO_NRANGES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PO_NRANGES; i++) { 229 1 230
395 229 size_t n = strlen(PO_RANGES[i].key);
396 229 ▶ if (n == len && memcmp(PO_RANGES[i].key, k, n) == 0) return &PO_RANGES[i];
Branch: 2 branches
BranchTrueFalseTotal
if (n == len && memcmp(PO_RANGES[i].key, k, n) == 0) return &PO_RANGES[i]; 117 112 229
if (n == len && memcmp(PO_RANGES[i].key, k, n) == 0) return &PO_RANGES[i]; 87 30 117
397 }
398 1 return NULL;
399 }
400  
401 /* An explicit bound is only honoured when it is a plausible nanosecond
402 * instant: all digits and more than nine of them. Fewer than ten digits is
403 * under a second past the epoch, which is not a range anybody asked for -
404 * it is a truncated or mis-parsed parameter, and honouring it would show an
405 * empty page for a reason nobody could see. */
406 127 static int po_ns_plausible(const char *s, size_t len) {
407 size_t i;
408 127 ▶ if (!s || len < 10) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || len < 10) return 0; 127 0 127
if (!s || len < 10) return 0; 0 127 127
409 2540 ▶ for (i = 0; i < len; i++) if (s[i] < '0' || s[i] > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (s[i] < '0' || s[i] > '9') return 0; 2413 0 2413
for (i = 0; i < len; i++) if (s[i] < '0' || s[i] > '9') return 0; 0 2413 2413
for (i = 0; i < len; i++) if (s[i] < '0' || s[i] > '9') return 0; 2413 127 2540
410 127 return 1;
411 }
412  
413 /* THE DURATION BOX.
414 *
415 * A DURATION TYPED BY A PERSON, in nanoseconds, and WHICH END IT BOUNDS.
416 * Returns 1 and sets *ns and *is_max when the text is a duration, 0 when it
417 * is empty - which is not a filter, and not an error - and -1 when it is text
418 * that was meant to be a duration and is not.
419 *
420 * The third case is the reason this exists. The box used to accept /\A\d+\z/
421 * and drop anything else on the floor, so typing the placeholder's own words
422 * back at it - "slower than 100" - removed the filter and said nothing. The
423 * page showed a full unfiltered table, which is the one outcome that looks
424 * like an answer. A refusal is worse than a filter and far better than a
425 * silent one.
426 *
427 * A bare number is MILLISECONDS, because the field is named min_ms and that
428 * is what it has always meant. A unit overrides it, and the units are the
429 * query language's own, so `500ms` in the box and `duration > 500ms` in a
430 * query cannot disagree.
431 *
432 * `>` and `>=` bound the SLOW end, `<` and `<=` the fast one - "faster than
433 * 100ms" is a question about the same column and there is no reason one
434 * direction should be askable and the other not.
435 *
436 * THE BOUND IS INCLUSIVE EITHER WAY. A strict bound differs from an
437 * inclusive one by a single nanosecond, which is not a distinction a filter
438 * box can usefully make; the query language is where an exact comparison
439 * belongs, and it has one.
440 */
441 80 static int po_min_duration(const char *s, size_t len, po_u64 *ns, int *is_max) {
442 80 size_t i = 0, j, ds, de;
443 80 po_u64 whole = 0, unit;
444 80 po_u64 frac_num = 0, frac_den = 1;
445  
446 80 *ns = 0;
447 80 *is_max = 0;
448 80 ▶ if (!s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return 0; 1 79 80
449 86 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 83 3 86
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 7 76 83
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 76 76
450 80 ▶ while (len > i && (s[len-1] == ' ' || s[len-1] == '\t')) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len > i && (s[len-1] == ' ' || s[len-1] == '\t')) len--; 77 3 80
while (len > i && (s[len-1] == ' ' || s[len-1] == '\t')) len--; 1 76 77
while (len > i && (s[len-1] == ' ' || s[len-1] == '\t')) len--; 0 76 76
451 79 ▶ if (i >= len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return 0; 3 76 79
452  
453 /* The comparison, if one was typed. Which end it bounds is the whole of
454 * the difference between "slower than 100ms" and "faster than 100ms", and
455 * both are ordinary things to want. No operator means the slow end, which
456 * is what the box has always meant on its own. */
457 76 ▶ if (s[i] == '>' || s[i] == '<') {
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '>' || s[i] == '<') { 71 5 76
if (s[i] == '>' || s[i] == '<') { 9 62 71
458 14 *is_max = (s[i] == '<');
459 14 i++;
460 14 ▶ if (i < len && s[i] == '=') i++;
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '=') i++; 13 1 14
if (i < len && s[i] == '=') i++; 3 10 13
461 }
462 86 ▶ while (i < len && s[i] == ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] == ' ') i++; 85 1 86
while (i < len && s[i] == ' ') i++; 10 75 85
463 76 ▶ if (i >= len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return -1; 1 75 76
464  
465 75 ds = i;
466 243 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 235 8 243
while (i < len && s[i] >= '0' && s[i] <= '9') { 231 4 235
while (i < len && s[i] >= '0' && s[i] <= '9') { 168 63 231
467 /* Bounded here only so the accumulator cannot wrap; whether the
468 * number is actually representable depends on the unit, and that is
469 * not known until after the suffix. */
470 168 po_u64 d = (po_u64)(s[i] - '0');
471 168 ▶ if (whole > (~(po_u64)0 - d) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (whole > (~(po_u64)0 - d) / 10) return -1; 0 168 168
472 168 whole = whole * 10 + d;
473 168 i++;
474 }
475 75 de = i;
476 75 ▶ if (de == ds) return -1; /* no digits at all: prose */
Branch: 1 branch
BranchTrueFalseTotal
if (de == ds) return -1; /* no digits at all: prose */ 7 68 75
477  
478 /* A fraction, so `1.5s` works. Capped at nine places, which is the whole
479 * resolution there is. */
480 68 ▶ if (i < len && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '.') { 60 8 68
if (i < len && s[i] == '.') { 3 57 60
481 3 i++;
482 6 ▶ for (j = 0; i < len && s[i] >= '0' && s[i] <= '9'; i++, j++) {
Branch: 3 branches
BranchTrueFalseTotal
for (j = 0; i < len && s[i] >= '0' && s[i] <= '9'; i++, j++) { 5 1 6
for (j = 0; i < len && s[i] >= '0' && s[i] <= '9'; i++, j++) { 5 0 5
for (j = 0; i < len && s[i] >= '0' && s[i] <= '9'; i++, j++) { 3 2 5
483 3 ▶ if (j < 9) { frac_num = frac_num * 10 + (po_u64)(s[i] - '0');
Branch: 1 branch
BranchTrueFalseTotal
if (j < 9) { frac_num = frac_num * 10 + (po_u64)(s[i] - '0'); 3 0 3
484 3 frac_den *= 10; }
485 }
486 3 ▶ if (!j) return -1; /* "5." is a typo, not a duration */
Branch: 1 branch
BranchTrueFalseTotal
if (!j) return -1; /* "5." is a typo, not a duration */ 1 2 3
487 }
488  
489 68 ▶ while (i < len && s[i] == ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] == ' ') i++; 60 8 68
while (i < len && s[i] == ' ') i++; 1 59 60
490  
491 67 ▶ if (i == len) unit = 1000000ULL; /* bare number: milliseconds */
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) unit = 1000000ULL; /* bare number: milliseconds */ 8 59 67
492 else {
493 59 unit = po_dur_unit(s + i, len - i);
494 59 ▶ if (!unit) return -1; /* a suffix that is not a unit */
Branch: 1 branch
BranchTrueFalseTotal
if (!unit) return -1; /* a suffix that is not a unit */ 2 57 59
495 }
496  
497 /* `4000000000w` is a number and a unit and still not a duration. Refusing
498 * it is the same decision as refusing prose: the alternative is a wrapped
499 * value, which is a filter that runs and means something else. */
500 65 ▶ if (whole > (po_u64)~(po_u64)0 / unit) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (whole > (po_u64)~(po_u64)0 / unit) return -1; 2 63 65
501  
502 63 *ns = whole * unit + (frac_num * unit) / frac_den;
503 63 return 1;
504 }
505  
506 #endif /* PO_VIEW_H */
« include/punk_observe/po_store.h ↑ Summary include/punk_observe/po_rollup.h »