include/punk_observe/po_panel.h

filter: Statementstmt 100% 40 / 40 Branchbran 85.0% 34 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.5% 74 / 80 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_tenant.h ↑ Summary include/punk_observe/po_span.h »
1 /* po_panel.h - a dashboard panel, which is an OQL string with a title.
2 *
3 * A panel is stored in the metadata database, not in a segment: it is edited,
4 * and segments are immutable. What lives here is the part that must be true
5 * before it is stored.
6 *
7 * PANEL JSON IS UNTRUSTED. It arrives from a form, it renders into HTML, and
8 * its query string is parsed. So the query is VALIDATED AT SAVE TIME by the
9 * same parser that will execute it - which means a broken dashboard is a form
10 * error the author sees, not a run-time failure a reader meets at three in
11 * the morning.
12 *
13 * ORDER IS A NUMBER AND LAYOUT IS A COLUMN COUNT, and the absence of a drag
14 * grid is deliberate. A drag grid is several hundred lines of JavaScript, a
15 * collision algorithm, a mobile story and a persistence format, in exchange
16 * for an arrangement most people set once. A form with a number in it does
17 * the same job, works on a phone, and is accessible without any work.
18 */
19 #ifndef PO_PANEL_H
20 #define PO_PANEL_H
21  
22 #include "punk_observe/po_compat.h"
23 #include "punk_observe/po_query.h"
24 #include "punk_observe/po_parse.h"
25  
26 #define PO_PANEL_TITLE_MAX 128
27 #define PO_PANEL_COLS_MAX 6
28  
29 /* Visualisation hints. A hint the renderer does not know falls back to a
30 * table, which shows the data rather than nothing. */
31 #define PO_VIZ_LINE 0
32 #define PO_VIZ_AREA 1
33 #define PO_VIZ_BAR 2
34 #define PO_VIZ_STAT 3
35 #define PO_VIZ_TABLE 4
36  
37 104 static int po_viz_from(const char *s, size_t n) {
38 104 ▶ if (n == 4 && memcmp(s, "line", 4) == 0) return PO_VIZ_LINE;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(s, "line", 4) == 0) return PO_VIZ_LINE; 38 66 104
if (n == 4 && memcmp(s, "line", 4) == 0) return PO_VIZ_LINE; 27 11 38
39 77 ▶ if (n == 4 && memcmp(s, "area", 4) == 0) return PO_VIZ_AREA;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(s, "area", 4) == 0) return PO_VIZ_AREA; 11 66 77
if (n == 4 && memcmp(s, "area", 4) == 0) return PO_VIZ_AREA; 6 5 11
40 71 ▶ if (n == 3 && memcmp(s, "bar", 3) == 0) return PO_VIZ_BAR;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 3 && memcmp(s, "bar", 3) == 0) return PO_VIZ_BAR; 11 60 71
if (n == 3 && memcmp(s, "bar", 3) == 0) return PO_VIZ_BAR; 10 1 11
41 61 ▶ if (n == 4 && memcmp(s, "stat", 4) == 0) return PO_VIZ_STAT;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(s, "stat", 4) == 0) return PO_VIZ_STAT; 5 56 61
if (n == 4 && memcmp(s, "stat", 4) == 0) return PO_VIZ_STAT; 5 0 5
42 56 return PO_VIZ_TABLE;
43 }
44  
45 244 static const char *po_viz_name(int v) {
46 244 switch (v) {
47 55 case PO_VIZ_LINE: return "line";
48 34 case PO_VIZ_AREA: return "area";
49 38 case PO_VIZ_BAR: return "bar";
50 33 case PO_VIZ_STAT: return "stat";
51 84 default: return "table";
52 }
53 }
54  
55 #define PO_PANEL_OK 0
56 #define PO_PANEL_NO_QUERY 1
57 #define PO_PANEL_BAD_QUERY 2
58 #define PO_PANEL_NO_TITLE 3
59 #define PO_PANEL_BAD_TITLE 4
60  
61 typedef struct {
62 int viz;
63 int position;
64 int span; /* columns this panel occupies, 1..PO_PANEL_COLS_MAX */
65 char err[256];
66 } po_panel;
67  
68 /* Validate one panel. `qerr` receives the parser's own message, because "that
69 * query is wrong" is not a usable form error and the parser already knows
70 * exactly what is wrong and where. */
71 104 static int po_panel_check(po_panel *p, const char *title, size_t tlen,
72 const char *query, size_t qlen,
73 const char *viz, size_t vlen,
74 int position, int span) {
75 po_query q;
76 size_t i;
77  
78 104 memset(p, 0, sizeof(*p));
79 104 ▶ p->viz = po_viz_from(viz ? viz : "", vlen);
Branch: 1 branch
BranchTrueFalseTotal
p->viz = po_viz_from(viz ? viz : "", vlen); 104 0 104
80 104 p->position = position < 0 ? 0 : position;
81 104 ▶ p->span = (span < 1) ? 1 : (span > PO_PANEL_COLS_MAX
Branch: 1 branch
BranchTrueFalseTotal
p->span = (span < 1) ? 1 : (span > PO_PANEL_COLS_MAX 103 1 104
82 ? PO_PANEL_COLS_MAX : span);
83  
84 104 ▶ if (!title || !tlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!title || !tlen) { 104 0 104
if (!title || !tlen) { 2 102 104
85 2 memcpy(p->err, "a panel needs a title", 22);
86 2 return PO_PANEL_NO_TITLE;
87 }
88 102 ▶ if (tlen > PO_PANEL_TITLE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (tlen > PO_PANEL_TITLE_MAX) { 1 101 102
89 1 memcpy(p->err, "that title is too long", 23);
90 1 return PO_PANEL_BAD_TITLE;
91 }
92 /* Control characters in a title are refused rather than escaped. They
93 * have no legitimate use here, and a title is displayed in a dozen places
94 * of which only some are HTML. */
95 406 ▶ for (i = 0; i < tlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tlen; i++) { 307 99 406
96 307 unsigned char c = (unsigned char)title[i];
97 307 ▶ if (c < 0x20 || c == 0x7f) {
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c == 0x7f) { 305 2 307
if (c < 0x20 || c == 0x7f) { 0 305 305
98 2 memcpy(p->err, "a title cannot contain control characters", 42);
99 2 return PO_PANEL_BAD_TITLE;
100 }
101 }
102  
103 99 ▶ if (!query || !qlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!query || !qlen) { 99 0 99
if (!query || !qlen) { 2 97 99
104 2 memcpy(p->err, "a panel needs a query", 22);
105 2 return PO_PANEL_NO_QUERY;
106 }
107  
108 /* THE SAME PARSER THAT WILL EXECUTE IT. A panel validated by a different
109 * rule than the one that runs it is a panel that can be saved and cannot
110 * be shown. */
111 97 ▶ if (!po_parse(&q, query, qlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_parse(&q, query, qlen)) { 11 86 97
112 11 size_t n = strlen(q.err);
113 11 ▶ if (n >= sizeof(p->err)) n = sizeof(p->err) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof(p->err)) n = sizeof(p->err) - 1; 0 11 11
114 11 memcpy(p->err, q.err, n);
115 11 p->err[n] = '\0';
116 11 return PO_PANEL_BAD_QUERY;
117 }
118 86 po_query_free(&q);
119 86 return PO_PANEL_OK;
120 }
121  
122 #endif /* PO_PANEL_H */
« include/punk_observe/po_tenant.h ↑ Summary include/punk_observe/po_span.h »