mirror of
https://github.com/netdata/netdata.git
synced 2025-05-11 04:10:55 +00:00

* Database engine prototype version 0 * Database engine initial integration with netdata POC * Scalable database engine with file and memory management. * Database engine integration with netdata * Added MIN MAX definitions to fix alpine build of travis CI * Bugfix for backends and new DB engine, remove useless rrdset_time2slot() calls and erroneous checks * DB engine disk protocol correction * Moved DB engine storage file location to /var/cache/netdata/{host}/dbengine * Fix configure to require openSSL for DB engine * Fix netdata daemon health not holding read lock when iterating chart dimensions * Optimized query API for new DB engine and old netdata DB fallback code-path * netdata database internal query API improvements and cleanup * Bugfix for DB engine queries returning empty values * Added netdata internal check for data queries for old and new DB * Added statistics to DB engine and fixed memory corruption bug * Added preliminary charts for DB engine statistics * Changed DB engine ratio statistics to incremental * Added netdata statistics charts for DB engine internal statistics * Fix for netdata not compiling successfully when missing dbengine dependencies * Added DB engine functional test to netdata unittest command parameter * Implemented DB engine dataset generator based on example.random chart * Fix build error in CI * Support older versions of libuv1 * Fixes segmentation fault when using multiple DB engine instances concurrently * Fix memory corruption bug * Fixed createdataset advanced option not exiting * Fix for DB engine not working on FreeBSD * Support FreeBSD library paths of new dependencies * Workaround for unsupported O_DIRECT in OS X * Fix unittest crashing during cleanup * Disable DB engine FS caching in Apple OS X since O_DIRECT is not available * Fix segfault when unittest and DB engine dataset generator don't have permissions to create temporary host * Modified DB engine dataset generator to create multiple files * Toned down overzealous page cache prefetcher * Reduce internal memory fragmentation for page-cache data pages * Added documentation describing the DB engine * Documentation bugfixes * Fixed unit tests compilation errors since last rebase * Added note to back-up the DB engine files in documentation * Added codacy fix. * Support old gcc versions for atomic counters in DB engine
217 lines
6.7 KiB
C
217 lines
6.7 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "json_wrapper.h"
|
|
|
|
void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, RRDR_OPTIONS options, int string_value) {
|
|
rrdset_check_rdlock(r->st);
|
|
|
|
long rows = rrdr_rows(r);
|
|
long c, i;
|
|
RRDDIM *rd;
|
|
|
|
//info("JSONWRAPPER(): %s: BEGIN", r->st->id);
|
|
char kq[2] = "", // key quote
|
|
sq[2] = ""; // string quote
|
|
|
|
if( options & RRDR_OPTION_GOOGLE_JSON ) {
|
|
kq[0] = '\0';
|
|
sq[0] = '\'';
|
|
}
|
|
else {
|
|
kq[0] = '"';
|
|
sq[0] = '"';
|
|
}
|
|
|
|
buffer_sprintf(wb, "{\n"
|
|
" %sapi%s: 1,\n"
|
|
" %sid%s: %s%s%s,\n"
|
|
" %sname%s: %s%s%s,\n"
|
|
" %sview_update_every%s: %d,\n"
|
|
" %supdate_every%s: %d,\n"
|
|
" %sfirst_entry%s: %u,\n"
|
|
" %slast_entry%s: %u,\n"
|
|
" %sbefore%s: %u,\n"
|
|
" %safter%s: %u,\n"
|
|
" %sdimension_names%s: ["
|
|
, kq, kq
|
|
, kq, kq, sq, r->st->id, sq
|
|
, kq, kq, sq, r->st->name, sq
|
|
, kq, kq, r->update_every
|
|
, kq, kq, r->st->update_every
|
|
, kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
|
|
, kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
|
|
, kq, kq, (uint32_t)r->before
|
|
, kq, kq, (uint32_t)r->after
|
|
, kq, kq);
|
|
|
|
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
|
|
if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
|
|
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
|
|
|
|
if(i) buffer_strcat(wb, ", ");
|
|
buffer_strcat(wb, sq);
|
|
buffer_strcat(wb, rd->name);
|
|
buffer_strcat(wb, sq);
|
|
i++;
|
|
}
|
|
if(!i) {
|
|
#ifdef NETDATA_INTERNAL_CHECKS
|
|
error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
|
|
#endif
|
|
rows = 0;
|
|
buffer_strcat(wb, sq);
|
|
buffer_strcat(wb, "no data");
|
|
buffer_strcat(wb, sq);
|
|
}
|
|
|
|
buffer_sprintf(wb, "],\n"
|
|
" %sdimension_ids%s: ["
|
|
, kq, kq);
|
|
|
|
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
|
|
if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
|
|
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
|
|
|
|
if(i) buffer_strcat(wb, ", ");
|
|
buffer_strcat(wb, sq);
|
|
buffer_strcat(wb, rd->id);
|
|
buffer_strcat(wb, sq);
|
|
i++;
|
|
}
|
|
if(!i) {
|
|
rows = 0;
|
|
buffer_strcat(wb, sq);
|
|
buffer_strcat(wb, "no data");
|
|
buffer_strcat(wb, sq);
|
|
}
|
|
|
|
buffer_sprintf(wb, "],\n"
|
|
" %slatest_values%s: ["
|
|
, kq, kq);
|
|
|
|
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
|
|
if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
|
|
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
|
|
|
|
if(i) buffer_strcat(wb, ", ");
|
|
i++;
|
|
|
|
calculated_number value = rd->last_stored_value;
|
|
if (NAN == value)
|
|
buffer_strcat(wb, "null");
|
|
else
|
|
buffer_rrd_value(wb, value);
|
|
/*
|
|
storage_number n = rd->values[rrdset_last_slot(r->st)];
|
|
|
|
if(!does_storage_number_exist(n))
|
|
buffer_strcat(wb, "null");
|
|
else
|
|
buffer_rrd_value(wb, unpack_storage_number(n));
|
|
*/
|
|
}
|
|
if(!i) {
|
|
rows = 0;
|
|
buffer_strcat(wb, "null");
|
|
}
|
|
|
|
buffer_sprintf(wb, "],\n"
|
|
" %sview_latest_values%s: ["
|
|
, kq, kq);
|
|
|
|
i = 0;
|
|
if(rows) {
|
|
calculated_number total = 1;
|
|
|
|
if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
|
|
total = 0;
|
|
for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
|
|
calculated_number *cn = &r->v[ (rrdr_rows(r) - 1) * r->d ];
|
|
calculated_number n = cn[c];
|
|
|
|
if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
|
|
n = -n;
|
|
|
|
total += n;
|
|
}
|
|
// prevent a division by zero
|
|
if(total == 0) total = 1;
|
|
}
|
|
|
|
for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
|
|
if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
|
|
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
|
|
|
|
if(i) buffer_strcat(wb, ", ");
|
|
i++;
|
|
|
|
calculated_number *cn = &r->v[ (rrdr_rows(r) - 1) * r->d ];
|
|
RRDR_VALUE_FLAGS *co = &r->o[ (rrdr_rows(r) - 1) * r->d ];
|
|
calculated_number n = cn[c];
|
|
|
|
if(co[c] & RRDR_VALUE_EMPTY) {
|
|
if(options & RRDR_OPTION_NULL2ZERO)
|
|
buffer_strcat(wb, "0");
|
|
else
|
|
buffer_strcat(wb, "null");
|
|
}
|
|
else {
|
|
if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
|
|
n = -n;
|
|
|
|
if(unlikely(options & RRDR_OPTION_PERCENTAGE))
|
|
n = n * 100 / total;
|
|
|
|
buffer_rrd_value(wb, n);
|
|
}
|
|
}
|
|
}
|
|
if(!i) {
|
|
rows = 0;
|
|
buffer_strcat(wb, "null");
|
|
}
|
|
|
|
buffer_sprintf(wb, "],\n"
|
|
" %sdimensions%s: %ld,\n"
|
|
" %spoints%s: %ld,\n"
|
|
" %sformat%s: %s"
|
|
, kq, kq, i
|
|
, kq, kq, rows
|
|
, kq, kq, sq
|
|
);
|
|
|
|
rrdr_buffer_print_format(wb, format);
|
|
|
|
buffer_sprintf(wb, "%s,\n"
|
|
" %sresult%s: "
|
|
, sq
|
|
, kq, kq
|
|
);
|
|
|
|
if(string_value) buffer_strcat(wb, sq);
|
|
//info("JSONWRAPPER(): %s: END", r->st->id);
|
|
}
|
|
|
|
void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value) {
|
|
(void)format;
|
|
|
|
char kq[2] = "", // key quote
|
|
sq[2] = ""; // string quote
|
|
|
|
if( options & RRDR_OPTION_GOOGLE_JSON ) {
|
|
kq[0] = '\0';
|
|
sq[0] = '\'';
|
|
}
|
|
else {
|
|
kq[0] = '"';
|
|
sq[0] = '"';
|
|
}
|
|
|
|
if(string_value) buffer_strcat(wb, sq);
|
|
|
|
buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
|
|
buffer_rrd_value(wb, r->min);
|
|
buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
|
|
buffer_rrd_value(wb, r->max);
|
|
buffer_strcat(wb, "\n}\n");
|
|
}
|