@cryptotaxi247 / netdata-1 / commits / 36f4670c5

add units per context to /api/v3/contexts (#19165)

Costa Tsaousis committed Dec 9, 2024 at 16:13 UTC 36f4670c5c6d3ff6f6c4ef9adb26ce6b7919a7b4
1 file changed +19
src/database/contexts/api_v2_contexts.c
+19
@@ -59,6 +59,7 @@ struct context_v2_entry {
59 size_t count;
60 STRING *id;
61 STRING *family;
62 + STRING *units;
63 uint32_t priority;
64 time_t first_time_s;
65 time_t last_time_s;
@@ -174,6 +175,7 @@ static ssize_t rrdcontext_to_json_v2_add_context(void *data, RRDCONTEXT_ACQUIRED
175 .count = 1,
176 .id = rc->id,
177 .family = string_dup(rc->family),
178 + .units = string_dup(rc->units),
179 .priority = rc->priority,
180 .first_time_s = rc->first_time_s,
181 .last_time_s = rc->last_time_s,
@@ -672,6 +674,21 @@ static bool contexts_conflict_callback(const DICTIONARY_ITEM *item __maybe_unuse
674 }
675 }
676
677 + if(o->units != n->units) {
678 + if((o->flags & RRD_FLAG_COLLECTED) && !(n->flags & RRD_FLAG_COLLECTED))
679 + // keep old
680 + ;
681 + else if(!(o->flags & RRD_FLAG_COLLECTED) && (n->flags & RRD_FLAG_COLLECTED)) {
682 + // keep new
683 + string_freez(o->units);
684 + o->units = string_dup(n->units);
685 + }
686 + else {
687 + // keep old
688 + ;
689 + }
690 + }
691 +
692 if(o->priority != n->priority) {
693 if((o->flags & RRD_FLAG_COLLECTED) && !(n->flags & RRD_FLAG_COLLECTED))
694 // keep o
@@ -705,6 +722,7 @@ static bool contexts_conflict_callback(const DICTIONARY_ITEM *item __maybe_unuse
722 static void contexts_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
723 struct context_v2_entry *z = value;
724 string_freez(z->family);
725 + string_freez((z->units));
726 }
727
728 int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode) {
@@ -946,6 +964,7 @@ int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTE
964 buffer_json_member_add_object(wb, string2str(z->id));
965 {
966 buffer_json_member_add_string(wb, "family", string2str(z->family));
967 + buffer_json_member_add_string(wb, "units", string2str(z->units));
968 buffer_json_member_add_uint64(wb, "priority", z->priority);
969 buffer_json_member_add_time_t(wb, "first_entry", z->first_time_s);
970 buffer_json_member_add_time_t(wb, "last_entry", collected ? ctl.now : z->last_time_s);