| 1 |
|
| /* po_plot.h - the arithmetic behind a figure. |
| 2 |
|
| * |
| 3 |
|
| * The figures themselves are written in xs/plot.xs, because they need the |
| 4 |
|
| * query result and that arrives as Perl data. What is here is everything that |
| 5 |
|
| * does not: the bucket ladder, the axis conversion, the colour roles and the |
| 6 |
|
| * severity bands. Testable without a store and without an interpreter. |
| 7 |
|
| */ |
| 8 |
|
| #ifndef PO_PLOT_H |
| 9 |
|
| #define PO_PLOT_H |
| 10 |
|
| |
| 11 |
|
| #include "punk_observe/po_compat.h" |
| 12 |
|
| #include "punk_observe/po_nsarith.h" |
| 13 |
|
| |
| 14 |
|
| /* Milliseconds since the epoch, for a chart axis. |
| 15 |
|
| * |
| 16 |
|
| * Milliseconds fit a double with room to spare - 1.8e12 against a 9e15 |
| 17 |
|
| * ceiling - so an axis is exact to the millisecond, which is finer than any |
| 18 |
|
| * screen can resolve. What is NOT done is arithmetic on the nanoseconds |
| 19 |
|
| * first: dividing by a million before the conversion rounds the value in the |
| 20 |
|
| * digits a chart is drawn from. So the last six digits are DROPPED, as |
| 21 |
|
| * characters, and what is left is converted once. |
| 22 |
|
| * |
| 23 |
|
| * Past 2^53 the answer stops being exact, which is a year 287396 problem. |
| 24 |
|
| * Returned as a po_u64 so the caller decides how to cross to Perl. */ |
| 25 |
648 |
| static po_u64 po_plot_ms(const char *ns, size_t len) { |
| 26 |
|
| char buf[32]; |
| 27 |
648 |
| size_t n = 0, i; |
| 28 |
648 |
| po_u64 v = 0; |
| 29 |
|
| |
| 30 |
648 |
▶ | if (!ns) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ns) return 0; |
0 |
648 |
648 |
|
| 31 |
12447 |
▶ | for (i = 0; i < len && n < sizeof(buf); i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < len && n < sizeof(buf); i++) |
11799 |
648 |
12447 |
| for (i = 0; i < len && n < sizeof(buf); i++) |
11799 |
0 |
11799 |
|
| 32 |
11799 |
▶ | if (ns[i] >= '0' && ns[i] <= '9') buf[n++] = ns[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ns[i] >= '0' && ns[i] <= '9') buf[n++] = ns[i]; |
11799 |
0 |
11799 |
| if (ns[i] >= '0' && ns[i] <= '9') buf[n++] = ns[i]; |
11799 |
0 |
11799 |
|
| 33 |
648 |
▶ | if (n <= 6) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 6) return 0; |
2 |
646 |
648 |
|
| 34 |
646 |
| n -= 6; |
| 35 |
8567 |
▶ | for (i = 0; i < n; i++) v = v * 10 + (po_u64)(buf[i] - '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) v = v * 10 + (po_u64)(buf[i] - '0'); |
7921 |
646 |
8567 |
|
| 36 |
646 |
| return v; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| /* Whole seconds between two instants, as the ladder wants them. Zero for |
| 40 |
|
| * anything that does not parse, which makes the caller fall back to its |
| 41 |
|
| * default rather than dividing by a width it invented. */ |
| 42 |
29 |
| static po_u64 po_plot_span_seconds(const char *from, size_t flen, |
| 43 |
|
| const char *to, size_t tlen) { |
| 44 |
29 |
| po_u64 a = 0, b = 0; |
| 45 |
29 |
| int ok = 1; |
| 46 |
|
| size_t i; |
| 47 |
|
| |
| 48 |
29 |
▶ | if (!from || !to) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!from || !to) return 0; |
29 |
0 |
29 |
| if (!from || !to) return 0; |
0 |
29 |
29 |
|
| 49 |
580 |
▶ | for (i = 0; i < flen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < flen; i++) { |
551 |
29 |
580 |
|
| 50 |
551 |
▶ | if (from[i] < '0' || from[i] > '9') { ok = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (from[i] < '0' || from[i] > '9') { ok = 0; break; } |
551 |
0 |
551 |
| if (from[i] < '0' || from[i] > '9') { ok = 0; break; } |
0 |
551 |
551 |
|
| 51 |
551 |
| a = a * 10 + (po_u64)(from[i] - '0'); |
| 52 |
|
| } |
| 53 |
580 |
▶ | for (i = 0; ok && i < tlen; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; ok && i < tlen; i++) { |
580 |
0 |
580 |
| for (i = 0; ok && i < tlen; i++) { |
551 |
29 |
580 |
|
| 54 |
551 |
▶ | if (to[i] < '0' || to[i] > '9') { ok = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (to[i] < '0' || to[i] > '9') { ok = 0; break; } |
551 |
0 |
551 |
| if (to[i] < '0' || to[i] > '9') { ok = 0; break; } |
0 |
551 |
551 |
|
| 55 |
551 |
| b = b * 10 + (po_u64)(to[i] - '0'); |
| 56 |
|
| } |
| 57 |
29 |
▶ | if (!ok || !flen || !tlen) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ok || !flen || !tlen) return 0; |
29 |
0 |
29 |
| if (!ok || !flen || !tlen) return 0; |
29 |
0 |
29 |
| if (!ok || !flen || !tlen) return 0; |
0 |
29 |
29 |
|
| 58 |
29 |
▶ | if (b <= a) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b <= a) return 0; |
0 |
29 |
29 |
|
| 59 |
29 |
| return (b - a) / 1000000000ULL; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| /* The bucket ladder. Widths a person recognises rather than whatever falls |
| 63 |
|
| * out of dividing the window: "every 37 seconds" is a correct answer and an |
| 64 |
|
| * unreadable axis. */ |
| 65 |
|
| typedef struct { po_u64 secs; const char *name; } po_plot_step; |
| 66 |
|
| |
| 67 |
|
| static const po_plot_step PO_PLOT_LADDER[] = { |
| 68 |
|
| { 1, "1s" }, { 5, "5s" }, { 10, "10s" }, { 30, "30s" }, |
| 69 |
|
| { 60, "1m" }, { 300, "5m" }, { 600, "10m" }, { 1800, "30m" }, |
| 70 |
|
| { 3600, "1h" }, { 10800, "3h" }, { 21600, "6h" }, { 43200, "12h" }, |
| 71 |
|
| { 86400, "1d" } |
| 72 |
|
| }; |
| 73 |
|
| #define PO_PLOT_NLADDER (sizeof(PO_PLOT_LADDER) / sizeof(PO_PLOT_LADDER[0])) |
| 74 |
|
| |
| 75 |
|
| /* Aim for roughly eighty bars: enough to show a shape, few enough that each |
| 76 |
|
| * is wide enough to see and to hit with a pointer. */ |
| 77 |
29 |
| static const char *po_plot_bucket_for(const char *from, size_t flen, |
| 78 |
|
| const char *to, size_t tlen) { |
| 79 |
29 |
| po_u64 secs = po_plot_span_seconds(from, flen, to, tlen); |
| 80 |
|
| size_t i; |
| 81 |
|
| double want; |
| 82 |
|
| |
| 83 |
29 |
▶ | if (!secs) return "1m"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!secs) return "1m"; |
0 |
29 |
29 |
|
| 84 |
29 |
| want = (double)secs / 80.0; |
| 85 |
119 |
▶ | for (i = 0; i < PO_PLOT_NLADDER; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PO_PLOT_NLADDER; i++) |
119 |
0 |
119 |
|
| 86 |
119 |
▶ | if ((double)PO_PLOT_LADDER[i].secs >= want) return PO_PLOT_LADDER[i].name; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((double)PO_PLOT_LADDER[i].secs >= want) return PO_PLOT_LADDER[i].name; |
29 |
90 |
119 |
|
| 87 |
0 |
| return PO_PLOT_LADDER[PO_PLOT_NLADDER - 1].name; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| /* COLOUR IS NAMED BY ROLE, NEVER BY VALUE. |
| 91 |
|
| * |
| 92 |
|
| * "series:2" is resolved against the stylesheet's custom properties in the |
| 93 |
|
| * browser, at draw time. A figure carrying a hex would be a figure that is |
| 94 |
|
| * wrong in one of the two themes, and the server has no idea which theme the |
| 95 |
|
| * reader is in - nor whether they are about to change it. */ |
| 96 |
77 |
| static const char *po_plot_role(int i) { |
| 97 |
|
| static const char *const R[8] = { |
| 98 |
|
| "series:0", "series:1", "series:2", "series:3", |
| 99 |
|
| "series:4", "series:5", "series:6", "series:7" |
| 100 |
|
| }; |
| 101 |
77 |
| return R[((i % 8) + 8) % 8]; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* OTLP's twenty-four point severity scale, in the six bands it is defined in. |
| 105 |
|
| * |
| 106 |
|
| * Writes into `out` and returns its length, because two of the answers are |
| 107 |
|
| * not constants: a severity outside the scale is reported AS ITSELF rather |
| 108 |
|
| * than folded into a band, and something that is not a number at all is |
| 109 |
|
| * `unlabelled`. Both exist so that a row with an unexpected severity still |
| 110 |
|
| * appears on the screen somebody opened to find it - a bar silently dropped |
| 111 |
|
| * because its band was unrecognised is the one they were looking for. |
| 112 |
|
| * |
| 113 |
|
| * `out` needs 32 bytes. */ |
| 114 |
22 |
| static size_t po_plot_severity_name(const char *s, size_t len, char *out) { |
| 115 |
22 |
| po_u64 n = 0; |
| 116 |
|
| size_t i; |
| 117 |
22 |
| const char *name = NULL; |
| 118 |
|
| |
| 119 |
22 |
▶ | if (!s || !len) { memcpy(out, "unlabelled", 10); return 10; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s || !len) { memcpy(out, "unlabelled", 10); return 10; } |
22 |
0 |
22 |
| if (!s || !len) { memcpy(out, "unlabelled", 10); return 10; } |
0 |
22 |
22 |
|
| 120 |
55 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
33 |
22 |
55 |
|
| 121 |
33 |
▶ | if (s[i] < '0' || s[i] > '9') { memcpy(out, "unlabelled", 10); return 10; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] < '0' || s[i] > '9') { memcpy(out, "unlabelled", 10); return 10; } |
33 |
0 |
33 |
| if (s[i] < '0' || s[i] > '9') { memcpy(out, "unlabelled", 10); return 10; } |
0 |
33 |
33 |
|
| 122 |
33 |
| n = n * 10 + (po_u64)(s[i] - '0'); |
| 123 |
33 |
▶ | if (n > 1000000) break; /* not a severity; still not a crash */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 1000000) break; /* not a severity; still not a crash */ |
0 |
33 |
33 |
|
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
22 |
▶ | if (n >= 1 && n <= 4) name = "trace"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n >= 1 && n <= 4) name = "trace"; |
22 |
0 |
22 |
| if (n >= 1 && n <= 4) name = "trace"; |
0 |
22 |
22 |
|
| 127 |
22 |
▶ | else if (n >= 5 && n <= 8) name = "debug"; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n >= 5 && n <= 8) name = "debug"; |
22 |
0 |
22 |
| else if (n >= 5 && n <= 8) name = "debug"; |
0 |
22 |
22 |
|
| 128 |
22 |
▶ | else if (n >= 9 && n <= 12) name = "info"; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n >= 9 && n <= 12) name = "info"; |
22 |
0 |
22 |
| else if (n >= 9 && n <= 12) name = "info"; |
11 |
11 |
22 |
|
| 129 |
11 |
▶ | else if (n >= 13 && n <= 16) name = "warn"; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n >= 13 && n <= 16) name = "warn"; |
11 |
0 |
11 |
| else if (n >= 13 && n <= 16) name = "warn"; |
0 |
11 |
11 |
|
| 130 |
11 |
▶ | else if (n >= 17 && n <= 20) name = "error"; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n >= 17 && n <= 20) name = "error"; |
11 |
0 |
11 |
| else if (n >= 17 && n <= 20) name = "error"; |
11 |
0 |
11 |
|
| 131 |
0 |
▶ | else if (n >= 21 && n <= 24) name = "fatal"; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n >= 21 && n <= 24) name = "fatal"; |
0 |
0 |
0 |
| else if (n >= 21 && n <= 24) name = "fatal"; |
0 |
0 |
0 |
|
| 132 |
|
| |
| 133 |
22 |
▶ | if (name) { size_t l = strlen(name); memcpy(out, name, l); return l; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { size_t l = strlen(name); memcpy(out, name, l); return l; } |
22 |
0 |
22 |
|
| 134 |
|
| { |
| 135 |
0 |
| size_t o = 9; |
| 136 |
0 |
| memcpy(out, "severity ", 9); |
| 137 |
0 |
▶ | if (len > 20) len = 20; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 20) len = 20; |
0 |
0 |
0 |
|
| 138 |
0 |
| memcpy(out + o, s, len); |
| 139 |
0 |
| return o + len; |
| 140 |
|
| } |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| /* The six band names, in the order a stacked chart wants them: least severe |
| 144 |
|
| * at the bottom, so the eye lands on the top of the stack. */ |
| 145 |
|
| static const char *const PO_PLOT_BANDS[6] = { |
| 146 |
|
| "trace", "debug", "info", "warn", "error", "fatal" |
| 147 |
|
| }; |
| 148 |
|
| |
| 149 |
|
| #endif /* PO_PLOT_H */ |