From 2f29e30c3381b43f0c22084b426929e3af9a0a56 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 25 Aug 2020 20:24:18 +0200 Subject: [PATCH] add {node,path,hook_list}_to_json() --- include/villas/hook_list.hpp | 2 ++ include/villas/node.h | 2 ++ include/villas/path.h | 2 ++ lib/api/requests/nodes.cpp | 37 +------------------------- lib/api/requests/paths.cpp | 51 +----------------------------------- lib/hook_list.cpp | 13 +++++++++ lib/node.cpp | 40 ++++++++++++++++++++++++++++ lib/path.cpp | 42 +++++++++++++++++++++++++++++ 8 files changed, 103 insertions(+), 86 deletions(-) diff --git a/include/villas/hook_list.hpp b/include/villas/hook_list.hpp index c9851b735..35e18d047 100644 --- a/include/villas/hook_list.hpp +++ b/include/villas/hook_list.hpp @@ -74,3 +74,5 @@ void hook_list_start(struct vlist *hs); void hook_list_stop(struct vlist *hs); struct vlist * hook_list_get_signals(struct vlist *hs); + +json_t * hook_list_to_json(struct vlist *hs); \ No newline at end of file diff --git a/include/villas/node.h b/include/villas/node.h index ed7ebc93d..901bdafac 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -215,4 +215,6 @@ bool node_is_enabled(const struct node *n); struct vlist * node_get_signals(struct node *n, enum NodeDir dir); +json_t * node_to_json(struct node *); + /** @} */ diff --git a/include/villas/path.h b/include/villas/path.h index 7e9bf9f4d..b84a88785 100644 --- a/include/villas/path.h +++ b/include/villas/path.h @@ -165,4 +165,6 @@ bool path_is_reversed(const struct vpath *p); struct vlist * path_get_signals(struct vpath *p); +json_t * path_to_json(struct vpath *p); + /** @} */ diff --git a/lib/api/requests/nodes.cpp b/lib/api/requests/nodes.cpp index 88eb4fdd5..dded6ba1e 100644 --- a/lib/api/requests/nodes.cpp +++ b/lib/api/requests/nodes.cpp @@ -55,42 +55,7 @@ public: for (size_t i = 0; i < vlist_length(nodes); i++) { struct node *n = (struct node *) vlist_at(nodes, i); - struct vlist *output_signals; - - json_t *json_node; - json_t *json_signals_in = nullptr; - json_t *json_signals_out = nullptr; - - char uuid[37]; - uuid_unparse(n->uuid, uuid); - - json_signals_in = signal_list_to_json(&n->in.signals); - - output_signals = node_output_signals(n); - if (output_signals) - json_signals_out = signal_list_to_json(output_signals); - - json_node = json_pack("{ s: s, s: s, s: s, s: i, s: { s: i, s: o? }, s: { s: i, s: o? } }", - "name", node_name_short(n), - "uuid", uuid, - "state", state_print(n->state), - "affinity", n->affinity, - "in", - "vectorize", n->in.vectorize, - "signals", json_signals_in, - "out", - "vectorize", n->out.vectorize, - "signals", json_signals_out - ); - - if (n->stats) - json_object_set_new(json_node, "stats", n->stats->toJson()); - - /* Add all additional fields of node here. - * This can be used for metadata */ - json_object_update(json_node, n->cfg); - - json_array_append_new(json_nodes, json_node); + json_array_append_new(json_nodes, node_to_json(n)); } return new Response(session, json_nodes); diff --git a/lib/api/requests/paths.cpp b/lib/api/requests/paths.cpp index 4364755c1..41d5a0578 100644 --- a/lib/api/requests/paths.cpp +++ b/lib/api/requests/paths.cpp @@ -56,56 +56,7 @@ public: for (size_t i = 0; i < vlist_length(paths); i++) { struct vpath *p = (struct vpath *) vlist_at(paths, i); - char uuid[37]; - uuid_unparse(p->uuid, uuid); - - json_t *json_signals = json_array(); - json_t *json_hooks = json_array(); - json_t *json_sources = json_array(); - json_t *json_destinations = json_array(); - - for (size_t i = 0; i < vlist_length(&p->signals); i++) { - struct signal *sig = (struct signal *) vlist_at_safe(&p->signals, i); - - json_array_append(json_signals, signal_to_json(sig)); - } - - for (size_t i = 0; i < vlist_length(&p->hooks); i++) { - Hook *h = (Hook *) vlist_at_safe(&p->hooks, i); - - json_array_append(json_hooks, h->getConfig()); - } - - for (size_t i = 0; i < vlist_length(&p->sources); i++) { - struct vpath_source *pd = (struct vpath_source *) vlist_at_safe(&p->sources, i); - - json_array_append_new(json_sources, json_string(node_name_short(pd->node))); - } - - for (size_t i = 0; i < vlist_length(&p->destinations); i++) { - struct vpath_destination *pd = (struct vpath_destination *) vlist_at_safe(&p->destinations, i); - - json_array_append_new(json_destinations, json_string(node_name_short(pd->node))); - } - - json_t *json_path = json_pack("{ s: s, s: s, s: s, s: b, s: b s: b, s: b, s: b, s: b s: i, s: o, s: o, s: o, s: o }", - "uuid", uuid, - "state", state_print(p->state), - "mode", p->mode == PathMode::ANY ? "any" : "all", - "enabled", p->enabled, - "builtin", p->builtin, - "reverse", p->reverse, - "original_sequence_no", p->original_sequence_no, - "last_sequence", p->last_sequence, - "poll", p->poll, - "queuelen", p->queuelen, - "signals", json_signals, - "hooks", json_hooks, - "in", json_sources, - "out", json_destinations - ); - - json_array_append_new(json_paths, json_path); + json_array_append_new(json_paths, path_to_json(p)); } return new Response(session, json_paths); diff --git a/lib/hook_list.cpp b/lib/hook_list.cpp index e92377014..0d3acbce7 100644 --- a/lib/hook_list.cpp +++ b/lib/hook_list.cpp @@ -201,3 +201,16 @@ vlist * hook_list_get_signals(vlist *hs) return h->getSignals(); } + +json_t * hook_list_to_json(vlist *hs) +{ + json_t *json_hooks = json_array(); + + for (size_t i = 0; i < vlist_length(hs); i++) { + Hook *h = (Hook *) vlist_at_safe(hs, i); + + json_array_append(json_hooks, h->getConfig()); + } + + return json_hooks; +} \ No newline at end of file diff --git a/lib/node.cpp b/lib/node.cpp index 8fcabe223..82071c5b4 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -666,3 +666,43 @@ struct vlist * node_get_signals(struct node *n, enum NodeDir dir) return node_direction_get_signals(nd); } + +json_t * node_to_json(struct node *n) +{ + struct vlist *output_signals; + + json_t *json_node; + json_t *json_signals_in = nullptr; + json_t *json_signals_out = nullptr; + + char uuid[37]; + uuid_unparse(n->uuid, uuid); + + json_signals_in = signal_list_to_json(&n->in.signals); + + output_signals = node_output_signals(n); + if (output_signals) + json_signals_out = signal_list_to_json(output_signals); + + json_node = json_pack("{ s: s, s: s, s: s, s: i, s: { s: i, s: o? }, s: { s: i, s: o? } }", + "name", node_name_short(n), + "uuid", uuid, + "state", state_print(n->state), + "affinity", n->affinity, + "in", + "vectorize", n->in.vectorize, + "signals", json_signals_in, + "out", + "vectorize", n->out.vectorize, + "signals", json_signals_out + ); + + if (n->stats) + json_object_set_new(json_node, "stats", n->stats->toJson()); + + /* Add all additional fields of node here. + * This can be used for metadata */ + json_object_update(json_node, n->cfg); + + return json_node; +} \ No newline at end of file diff --git a/lib/path.cpp b/lib/path.cpp index 4d4f2c637..9847e5fa6 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -842,3 +842,45 @@ struct vlist * path_get_signals(struct vpath *p) { return &p->signals; } + +json_t * path_to_json(struct vpath *p) +{ + char uuid[37]; + uuid_unparse(p->uuid, uuid); + + json_t *json_signals = signal_list_to_json(&p->signals); + json_t *json_hooks = hook_list_to_json(&p->hooks); + json_t *json_sources = json_array(); + json_t *json_destinations = json_array(); + + for (size_t i = 0; i < vlist_length(&p->sources); i++) { + struct vpath_source *pd = (struct vpath_source *) vlist_at_safe(&p->sources, i); + + json_array_append_new(json_sources, json_string(node_name_short(pd->node))); + } + + for (size_t i = 0; i < vlist_length(&p->destinations); i++) { + struct vpath_destination *pd = (struct vpath_destination *) vlist_at_safe(&p->destinations, i); + + json_array_append_new(json_destinations, json_string(node_name_short(pd->node))); + } + + json_t *json_path = json_pack("{ s: s, s: s, s: s, s: b, s: b s: b, s: b, s: b, s: b s: i, s: o, s: o, s: o, s: o }", + "uuid", uuid, + "state", state_print(p->state), + "mode", p->mode == PathMode::ANY ? "any" : "all", + "enabled", p->enabled, + "builtin", p->builtin, + "reverse", p->reverse, + "original_sequence_no", p->original_sequence_no, + "last_sequence", p->last_sequence, + "poll", p->poll, + "queuelen", p->queuelen, + "signals", json_signals, + "hooks", json_hooks, + "in", json_sources, + "out", json_destinations + ); + + return json_path; +} \ No newline at end of file