skip to content
==> include/mesh/views.h <==
1/* @ajax/mesh-views: the fleet's views (docs/plan-fleet-scheduling.md F6) as records.
2 *
3 * `/fleet`, `/jobs` and `/pipes` on the http port, `view fleet|jobs|pipes
4 * [json]` on the control port, and mesh-top's tabs all read one producer per
5 * view as records:
6 *
7 *     member id=2 state=up leader=0 host=being gpu=cuda share=0.48 -- busy: cpu psi 0.31
8 *
9 * A record is a kind, `key=value` fields (a value has no spaces or `=`: each
10 * becomes `_`; an empty value is `-`, "none") and an optional free text after
11 * ` -- `. The text form is one record a line; the JSON form groups records by
12 * kind in first-seen order, every record an object of its fields plus "text":
13 * `-` is null, a field named in MESH_VIEWS_STRING_KEYS is a string, any other
14 * that is a JSON number is a number. */
15#ifndef MESH_VIEWS_H
16#define MESH_VIEWS_H
17
18#include "mesh/buf.h"
19
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdint.h>
23
24typedef struct {
25    char *k, *v;
26} mesh_kv;
27
28typedef struct {
29    char *kind;
30    mesh_kv *kv;
31    size_t nkv, cap;
32    char *text; /* "" when none */
33} mesh_rec;
34
35typedef struct {
36    mesh_rec *items;
37    size_t len, cap;
38} mesh_recs;
39
40#define MESH_RECS_INIT {NULL, 0, 0}
41
42/* A new record at the end of `rs` (the pointer is valid until the next add). */
43mesh_rec *mesh_recs_add(mesh_recs *rs, const char *kind);
44void mesh_recs_free(mesh_recs *rs);
45/* A deep copy appended to dst. */
46void mesh_recs_extend(mesh_recs *dst, const mesh_recs *src);
47
48/* A field, its value made one token (whitespace and `=` become `_`, empty becomes `-`). */
49void mesh_rec_kv(mesh_rec *r, const char *k, const char *v);
50void mesh_rec_kvf(mesh_rec *r, const char *k, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
51void mesh_rec_kvu(mesh_rec *r, const char *k, uint64_t v);
52void mesh_rec_kvi(mesh_rec *r, const char *k, int64_t v);
53/* `-` (Rust's `opt` with None). */
54void mesh_rec_none(mesh_rec *r, const char *k);
55/* A float with p decimals; `-` when !some or not finite. */
56void mesh_rec_f(mesh_rec *r, const char *k, bool some, double v, int p);
57/* The free text (newlines and carriage returns become spaces). */
58void mesh_rec_text(mesh_rec *r, const char *t);
59/* A field's value, NULL when absent or `-`. */
60const char *mesh_rec_get(const mesh_rec *r, const char *k);
61
62extern const char *const MESH_VIEWS_STRING_KEYS[37];
63
64void mesh_views_text(mesh_buf *out, const mesh_recs *rs);
65void mesh_views_json(mesh_buf *out, const mesh_recs *rs);
66/* Parses the text form (appends to out). */
67void mesh_views_parse(const char *text, mesh_recs *out);
68/* `?format=json|text` of a query: 0 and *json set, or -1 with *err (free it). */
69int mesh_views_format_of(const char *query, bool *json, char **err);
70void mesh_views_render(mesh_buf *out, const mesh_recs *rs, bool json);
71/* Whether v is written as a JSON number (exposed for tests). */
72bool mesh_views_json_number(const char *v);
73
74#endif