3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
9 "\t\t\t\"id\": \"%s\",\n"
10 "\t\t\t\"name\": \"%s\",\n"
11 "\t\t\t\"type\": \"%s\",\n"
12 "\t\t\t\"family\": \"%s\",\n"
13 "\t\t\t\"context\": \"%s\",\n"
14 "\t\t\t\"title\": \"%s\",\n"
15 "\t\t\t\"priority\": %ld,\n"
16 "\t\t\t\"enabled\": %s,\n"
17 "\t\t\t\"units\": \"%s\",\n"
18 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
19 "\t\t\t\"chart_type\": \"%s\",\n"
20 "\t\t\t\"duration\": %ld,\n"
21 "\t\t\t\"first_entry\": %ld,\n"
22 "\t\t\t\"last_entry\": %ld,\n"
23 "\t\t\t\"update_every\": %d,\n"
24 "\t\t\t\"dimensions\": {\n"
32 , rrdset_flag_check(st, RRDSET_FLAG_ENABLED)?"true":"false"
35 , rrdset_type_name(st->chart_type)
36 , st->entries * st->update_every
37 , rrdset_first_entry_t(st)
38 , rrdset_last_entry_t(st)
42 unsigned long memory = st->memsize;
44 size_t dimensions = 0;
46 for(rd = st->dimensions; rd ; rd = rd->next) {
47 if(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 if(dimensions_count) *dimensions_count += dimensions;
63 if(memory_used) *memory_used += memory;
65 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
66 buffer_rrd_value(wb, st->green);
67 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
68 buffer_rrd_value(wb, st->red);
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
81 void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb)
83 size_t c, dimensions = 0, memory = 0, alarms = 0;
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"version\": \"%s\""
90 ",\n\t\"update_every\": %d"
91 ",\n\t\"history\": %d"
96 , host->rrd_update_every
97 , host->rrd_history_entries
100 rrdhost_rdlock(host);
101 for(st = host->rrdset_root, c = 0; st ; st = st->next) {
102 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
103 if(c) buffer_strcat(wb, ",");
104 buffer_strcat(wb, "\n\t\t\"");
105 buffer_strcat(wb, st->id);
106 buffer_strcat(wb, "\": ");
107 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
113 for(rc = host->alarms; rc ; rc = rc->next) {
117 rrdhost_unlock(host);
119 buffer_sprintf(wb, "\n\t}"
120 ",\n\t\"charts_count\": %zu"
121 ",\n\t\"dimensions_count\": %zu"
122 ",\n\t\"alarms_count\": %zu"
123 ",\n\t\"rrd_memory_bytes\": %zu"
132 // ----------------------------------------------------------------------------
134 // /api/v1/allmetrics?format=prometheus
136 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
139 for(n = 0; *s && n < usable ; d++, s++, n++) {
140 register char c = *s;
142 if(unlikely(!isalnum(c))) *d = '_';
150 #define PROMETHEUS_ELEMENT_MAX 256
152 void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb) {
153 rrdhost_rdlock(host);
155 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
156 prometheus_name_copy(hostname, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
160 for(st = host->rrdset_root; st ; st = st->next) {
161 char chart[PROMETHEUS_ELEMENT_MAX + 1];
162 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
164 buffer_strcat(wb, "\n");
165 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
168 // for each dimension
170 for(rd = st->dimensions; rd ; rd = rd->next) {
172 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
173 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
175 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
177 switch(rd->algorithm) {
178 case RRD_ALGORITHM_INCREMENTAL:
179 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
180 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
184 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
188 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
189 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
190 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
192 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
193 chart, dimension, hostname, rd->last_collected_value,
194 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
203 rrdhost_unlock(host);
206 // ----------------------------------------------------------------------------
208 // /api/v1/allmetrics?format=bash
210 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
213 for(n = 0; *s && n < usable ; d++, s++, n++) {
214 register char c = *s;
216 if(unlikely(!isalnum(c))) *d = '_';
217 else *d = (char)toupper(c);
224 #define SHELL_ELEMENT_MAX 100
226 void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb) {
227 rrdhost_rdlock(host);
231 for(st = host->rrdset_root; st ; st = st->next) {
232 calculated_number total = 0.0;
233 char chart[SHELL_ELEMENT_MAX + 1];
234 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
236 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
237 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
240 // for each dimension
242 for(rd = st->dimensions; rd ; rd = rd->next) {
244 char dimension[SHELL_ELEMENT_MAX + 1];
245 shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
247 calculated_number n = rd->last_stored_value;
249 if(isnan(n) || isinf(n))
250 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
252 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
254 if(!rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) total += n;
255 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
260 total = roundl(total);
261 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
266 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
269 for(rc = host->alarms; rc ;rc = rc->next) {
270 if(!rc->rrdset) continue;
272 char chart[SHELL_ELEMENT_MAX + 1];
273 shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
275 char alarm[SHELL_ELEMENT_MAX + 1];
276 shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
278 calculated_number n = rc->value;
280 if(isnan(n) || isinf(n))
281 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\" # %s\n", chart, alarm, rc->units);
284 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\" # %s\n", chart, alarm, n, rc->units);
287 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
290 rrdhost_unlock(host);
293 // ----------------------------------------------------------------------------
295 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
297 time_t now = now_realtime_sec();
303 "\t\t\t\"id\": \"%s\",\n"
304 "\t\t\t\"name\": \"%s\",\n"
305 "\t\t\t\"type\": \"%s\",\n"
306 "\t\t\t\"family\": \"%s\",\n"
307 "\t\t\t\"context\": \"%s\",\n"
308 "\t\t\t\"title\": \"%s\",\n"
309 "\t\t\t\"priority\": %ld,\n"
310 "\t\t\t\"enabled\": %d,\n"
311 "\t\t\t\"units\": \"%s\",\n"
312 "\t\t\t\"url\": \"/data/%s/%s\",\n"
313 "\t\t\t\"chart_type\": \"%s\",\n"
314 "\t\t\t\"counter\": %lu,\n"
315 "\t\t\t\"entries\": %ld,\n"
316 "\t\t\t\"first_entry_t\": %ld,\n"
317 "\t\t\t\"last_entry\": %lu,\n"
318 "\t\t\t\"last_entry_t\": %ld,\n"
319 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
320 "\t\t\t\"update_every\": %d,\n"
321 "\t\t\t\"isdetail\": %d,\n"
322 "\t\t\t\"usec_since_last_update\": %llu,\n"
323 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
324 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
325 "\t\t\t\"dimensions\": [\n"
333 , rrdset_flag_check(st, RRDSET_FLAG_ENABLED)?1:0
335 , st->name, options?options:""
336 , rrdset_type_name(st->chart_type)
339 , rrdset_first_entry_t(st)
340 , rrdset_last_slot(st)
341 , rrdset_last_entry_t(st)
342 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
344 , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?1:0
345 , st->usec_since_last_update
346 , st->collected_total
347 , st->last_collected_total
350 unsigned long memory = st->memsize;
353 for(rd = st->dimensions; rd ; rd = rd->next) {
355 memory += rd->memsize;
359 "\t\t\t\t\t\"id\": \"%s\",\n"
360 "\t\t\t\t\t\"name\": \"%s\",\n"
361 "\t\t\t\t\t\"entries\": %ld,\n"
362 "\t\t\t\t\t\"isHidden\": %d,\n"
363 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
364 "\t\t\t\t\t\"multiplier\": " COLLECTED_NUMBER_FORMAT ",\n"
365 "\t\t\t\t\t\"divisor\": " COLLECTED_NUMBER_FORMAT ",\n"
366 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
367 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
368 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
369 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
370 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
371 "\t\t\t\t\t\"memory\": %lu\n"
376 , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?1:0
377 , rrd_algorithm_name(rd->algorithm)
380 , rd->last_collected_time.tv_sec
381 , rd->collected_value
382 , rd->calculated_value
383 , rd->last_collected_value
384 , rd->last_calculated_value
392 "\t\t\t\"memory\" : %lu\n"
401 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
402 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
404 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
406 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
407 rrd_stats_one_json(st, options, wb);
408 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
411 void rrd_stats_all_json(RRDHOST *host, BUFFER *wb)
413 unsigned long memory = 0;
417 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
419 rrdhost_rdlock(host);
421 for(st = host->rrdset_root, c = 0; st ; st = st->next) {
422 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
423 if(c) buffer_strcat(wb, ",\n");
424 memory += rrd_stats_one_json(st, NULL, wb);
429 buffer_sprintf(wb, "\n\t],\n"
430 "\t\"hostname\": \"%s\",\n"
431 "\t\"update_every\": %d,\n"
432 "\t\"history\": %d,\n"
433 "\t\"memory\": %lu\n"
436 , host->rrd_update_every
437 , host->rrd_history_entries
441 rrdhost_unlock(host);
446 // ----------------------------------------------------------------------------
448 // RRDR dimension options
449 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
450 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
451 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
452 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
453 #define RRDR_SELECTED 0x10 // the dimension is selected
455 // RRDR result options
456 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
457 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
459 typedef struct rrdresult {
460 RRDSET *st; // the chart this result refers to
462 uint32_t result_options; // RRDR_RESULT_OPTION_*
464 int d; // the number of dimensions
465 long n; // the number of values in the arrays
466 long rows; // the number of rows used
468 uint8_t *od; // the options for the dimensions
470 time_t *t; // array of n timestamps
471 calculated_number *v; // array n x d values
472 uint8_t *o; // array n x d options
474 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
476 long group; // how many collected values were grouped for each row
477 int update_every; // what is the suggested update frequency in seconds
479 calculated_number min;
480 calculated_number max;
485 int has_st_lock; // if st is read locked by us
488 #define rrdr_rows(r) ((r)->rows)
491 static void rrdr_dump(RRDR *r)
496 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
498 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
499 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
502 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
503 , (r->od[c] & RRDR_RESET)?"RESET ":""
504 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
505 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
510 fprintf(stderr, "RRDR does not have any values in it.\n");
514 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
516 // for each line in the array
517 for(i = 0; i < r->rows ;i++) {
518 calculated_number *cn = &r->v[ i * r->d ];
519 uint8_t *co = &r->o[ i * r->d ];
521 // print the id and the timestamp of the line
522 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
524 // for each dimension
525 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
526 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
527 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
529 if(co[c] & RRDR_EMPTY)
530 fprintf(stderr, "null ");
532 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
534 , (co[c] & RRDR_EMPTY)?"E":" "
535 , (co[c] & RRDR_RESET)?"R":" "
536 , (co[c] & RRDR_HIDDEN)?"H":" "
537 , (co[c] & RRDR_NONZERO)?"N":" "
541 fprintf(stderr, "\n");
546 void rrdr_disable_not_selected_dimensions(RRDR *r, uint32_t options, const char *dims) {
547 if(unlikely(!dims || !*dims)) return;
549 char b[strlen(dims) + 1];
553 long c, dims_selected = 0, dims_not_hidden_not_zero = 0;
556 // disable all of them
557 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
558 r->od[c] |= RRDR_HIDDEN;
560 while(o && *o && (tok = mystrsep(&o, ",|"))) {
563 uint32_t hash = simple_hash(tok);
565 // find it and enable it
566 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
567 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || (hash == d->hash_name && !strcmp(d->name, tok)))) {
569 if(likely(r->od[c] & RRDR_HIDDEN)) {
570 r->od[c] |= RRDR_SELECTED;
571 r->od[c] &= ~RRDR_HIDDEN;
575 // since the user needs this dimension
576 // make it appear as NONZERO, to return it
577 // even if the dimension has only zeros
578 // unless option non_zero is set
579 if(likely(!(options & RRDR_OPTION_NONZERO)))
580 r->od[c] |= RRDR_NONZERO;
582 // count the visible dimensions
583 if(likely(r->od[c] & RRDR_NONZERO))
584 dims_not_hidden_not_zero++;
589 // check if all dimensions are hidden
590 if(unlikely(!dims_not_hidden_not_zero && dims_selected)) {
591 // there are a few selected dimensions
592 // but they are all zero
593 // enable the selected ones
594 // to avoid returning an empty chart
595 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
596 if(unlikely(r->od[c] & RRDR_SELECTED))
597 r->od[c] |= RRDR_NONZERO;
601 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
604 case DATASOURCE_JSON:
605 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
608 case DATASOURCE_DATATABLE_JSON:
609 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
612 case DATASOURCE_DATATABLE_JSONP:
613 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
616 case DATASOURCE_JSONP:
617 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
621 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
625 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
629 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
632 case DATASOURCE_HTML:
633 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
636 case DATASOURCE_JS_ARRAY:
637 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
640 case DATASOURCE_SSV_COMMA:
641 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
645 buffer_strcat(wb, "unknown");
650 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
654 if(options & RRDR_OPTION_NONZERO) {
657 // commented due to #1514
659 //if(dims && *dims) {
660 // the caller wants specific dimensions
661 // disable NONZERO option
662 // to make sure we don't accidentally prevent
663 // the specific dimensions from being returned
667 // find how many dimensions are not zero
670 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
671 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
672 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
677 // if with nonzero we get i = 0 (no dimensions will be returned)
678 // disable nonzero to show all dimensions
679 if(!i) options &= ~RRDR_OPTION_NONZERO;
685 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
687 long rows = rrdr_rows(r);
691 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
692 char kq[2] = "", // key quote
693 sq[2] = ""; // string quote
695 if( options & RRDR_OPTION_GOOGLE_JSON ) {
704 buffer_sprintf(wb, "{\n"
707 " %sname%s: %s%s%s,\n"
708 " %sview_update_every%s: %d,\n"
709 " %supdate_every%s: %d,\n"
710 " %sfirst_entry%s: %u,\n"
711 " %slast_entry%s: %u,\n"
714 " %sdimension_names%s: ["
716 , kq, kq, sq, r->st->id, sq
717 , kq, kq, sq, r->st->name, sq
718 , kq, kq, r->update_every
719 , kq, kq, r->st->update_every
720 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
721 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
722 , kq, kq, (uint32_t)r->before
723 , kq, kq, (uint32_t)r->after
726 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
727 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
728 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
730 if(i) buffer_strcat(wb, ", ");
731 buffer_strcat(wb, sq);
732 buffer_strcat(wb, rd->name);
733 buffer_strcat(wb, sq);
737 #ifdef NETDATA_INTERNAL_CHECKS
738 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
741 buffer_strcat(wb, sq);
742 buffer_strcat(wb, "no data");
743 buffer_strcat(wb, sq);
746 buffer_sprintf(wb, "],\n"
747 " %sdimension_ids%s: ["
750 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
751 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
752 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
754 if(i) buffer_strcat(wb, ", ");
755 buffer_strcat(wb, sq);
756 buffer_strcat(wb, rd->id);
757 buffer_strcat(wb, sq);
762 buffer_strcat(wb, sq);
763 buffer_strcat(wb, "no data");
764 buffer_strcat(wb, sq);
767 buffer_sprintf(wb, "],\n"
768 " %slatest_values%s: ["
771 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
772 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
773 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
775 if(i) buffer_strcat(wb, ", ");
778 storage_number n = rd->values[rrdset_last_slot(r->st)];
780 if(!does_storage_number_exist(n))
781 buffer_strcat(wb, "null");
783 buffer_rrd_value(wb, unpack_storage_number(n));
787 buffer_strcat(wb, "null");
790 buffer_sprintf(wb, "],\n"
791 " %sview_latest_values%s: ["
796 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
797 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
798 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
800 if(i) buffer_strcat(wb, ", ");
803 calculated_number *cn = &r->v[ (0) * r->d ];
804 uint8_t *co = &r->o[ (0) * r->d ];
806 if(co[c] & RRDR_EMPTY)
807 buffer_strcat(wb, "null");
809 buffer_rrd_value(wb, cn[c]);
814 buffer_strcat(wb, "null");
817 buffer_sprintf(wb, "],\n"
818 " %sdimensions%s: %ld,\n"
819 " %spoints%s: %ld,\n"
826 rrdr_buffer_print_format(wb, format);
828 buffer_sprintf(wb, "%s,\n"
834 if(string_value) buffer_strcat(wb, sq);
835 //info("JSONWRAPPER(): %s: END", r->st->id);
838 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
842 char kq[2] = "", // key quote
843 sq[2] = ""; // string quote
845 if( options & RRDR_OPTION_GOOGLE_JSON ) {
854 if(string_value) buffer_strcat(wb, sq);
856 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
857 buffer_rrd_value(wb, r->min);
858 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
859 buffer_rrd_value(wb, r->max);
860 buffer_strcat(wb, "\n}\n");
863 #define JSON_DATES_JS 1
864 #define JSON_DATES_TIMESTAMP 2
866 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
868 //info("RRD2JSON(): %s: BEGIN", r->st->id);
869 int row_annotations = 0, dates, dates_with_new = 0;
870 char kq[2] = "", // key quote
871 sq[2] = "", // string quote
872 pre_label[101] = "", // before each label
873 post_label[101] = "", // after each label
874 pre_date[101] = "", // the beginning of line, to the date
875 post_date[101] = "", // closing the date
876 pre_value[101] = "", // before each value
877 post_value[101] = "", // after each value
878 post_line[101] = "", // at the end of each row
879 normal_annotation[201] = "", // default row annotation
880 overflow_annotation[201] = "", // overflow row annotation
881 data_begin[101] = "", // between labels and values
882 finish[101] = ""; // at the end of everything
885 dates = JSON_DATES_JS;
886 if( options & RRDR_OPTION_GOOGLE_JSON ) {
895 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
896 snprintfz(post_date, 100, "%s}", sq);
897 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
898 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
899 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
900 strcpy(post_value, "}");
901 strcpy(post_line, "]}");
902 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
903 strcpy(finish, "\n ]\n}");
905 snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
906 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
908 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
909 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
910 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
911 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
913 // remove the valueobjects flag
914 // google wants its own keys
915 if(options & RRDR_OPTION_OBJECTSROWS)
916 options &= ~RRDR_OPTION_OBJECTSROWS;
921 if(options & RRDR_OPTION_GOOGLE_JSON) {
922 dates = JSON_DATES_JS;
926 dates = JSON_DATES_TIMESTAMP;
929 if( options & RRDR_OPTION_OBJECTSROWS )
930 strcpy(pre_date, " { ");
932 strcpy(pre_date, " [ ");
933 strcpy(pre_label, ", \"");
934 strcpy(post_label, "\"");
935 strcpy(pre_value, ", ");
936 if( options & RRDR_OPTION_OBJECTSROWS )
937 strcpy(post_line, "}");
939 strcpy(post_line, "]");
940 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
941 strcpy(finish, "\n ]\n}");
943 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
944 buffer_sprintf(wb, "%stime%s", sq, sq);
947 // -------------------------------------------------------------------------
948 // print the JSON header
953 // print the header lines
954 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
955 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
956 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
958 buffer_strcat(wb, pre_label);
959 buffer_strcat(wb, rd->name);
960 buffer_strcat(wb, post_label);
964 buffer_strcat(wb, pre_label);
965 buffer_strcat(wb, "no data");
966 buffer_strcat(wb, post_label);
969 // print the begin of row data
970 buffer_strcat(wb, data_begin);
972 // if all dimensions are hidden, print a null
974 buffer_strcat(wb, finish);
978 long start = 0, end = rrdr_rows(r), step = 1;
979 if((options & RRDR_OPTION_REVERSED)) {
980 start = rrdr_rows(r) - 1;
985 // for each line in the array
986 calculated_number total = 1;
987 for(i = start; i != end ;i += step) {
988 calculated_number *cn = &r->v[ i * r->d ];
989 uint8_t *co = &r->o[ i * r->d ];
991 time_t now = r->t[i];
993 if(dates == JSON_DATES_JS) {
994 // generate the local date time
995 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
996 if(!tm) { error("localtime_r() failed."); continue; }
998 if(likely(i != start)) buffer_strcat(wb, ",\n");
999 buffer_strcat(wb, pre_date);
1001 if( options & RRDR_OPTION_OBJECTSROWS )
1002 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1005 buffer_strcat(wb, "new ");
1007 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1009 buffer_strcat(wb, post_date);
1011 if(row_annotations) {
1012 // google supports one annotation per row
1013 int annotation_found = 0;
1014 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
1015 if(co[c] & RRDR_RESET) {
1016 buffer_strcat(wb, overflow_annotation);
1017 annotation_found = 1;
1021 if(!annotation_found)
1022 buffer_strcat(wb, normal_annotation);
1026 // print the timestamp of the line
1027 if(likely(i != start)) buffer_strcat(wb, ",\n");
1028 buffer_strcat(wb, pre_date);
1030 if( options & RRDR_OPTION_OBJECTSROWS )
1031 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1033 buffer_rrd_value(wb, (calculated_number)r->t[i]);
1035 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1037 buffer_strcat(wb, post_date);
1040 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1042 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1043 calculated_number n = cn[c];
1045 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1050 // prevent a division by zero
1051 if(total == 0) total = 1;
1054 // for each dimension
1055 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1056 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1057 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1059 calculated_number n = cn[c];
1061 buffer_strcat(wb, pre_value);
1063 if( options & RRDR_OPTION_OBJECTSROWS )
1064 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1066 if(co[c] & RRDR_EMPTY) {
1067 if(options & RRDR_OPTION_NULL2ZERO)
1068 buffer_strcat(wb, "0");
1070 buffer_strcat(wb, "null");
1073 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1076 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1077 n = n * 100 / total;
1079 buffer_rrd_value(wb, n);
1082 buffer_strcat(wb, post_value);
1085 buffer_strcat(wb, post_line);
1088 buffer_strcat(wb, finish);
1089 //info("RRD2JSON(): %s: END", r->st->id);
1092 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1094 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1098 // print the csv header
1099 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1100 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1101 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1104 buffer_strcat(wb, startline);
1105 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1106 buffer_strcat(wb, "time");
1107 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1109 buffer_strcat(wb, separator);
1110 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1111 buffer_strcat(wb, d->name);
1112 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1115 buffer_strcat(wb, endline);
1118 // no dimensions present
1122 long start = 0, end = rrdr_rows(r), step = 1;
1123 if((options & RRDR_OPTION_REVERSED)) {
1124 start = rrdr_rows(r) - 1;
1129 // for each line in the array
1130 calculated_number total = 1;
1131 for(i = start; i != end ;i += step) {
1132 calculated_number *cn = &r->v[ i * r->d ];
1133 uint8_t *co = &r->o[ i * r->d ];
1135 buffer_strcat(wb, betweenlines);
1136 buffer_strcat(wb, startline);
1138 time_t now = r->t[i];
1140 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1141 // print the timestamp of the line
1142 buffer_rrd_value(wb, (calculated_number)now);
1144 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1147 // generate the local date time
1148 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1149 if(!tm) { error("localtime() failed."); continue; }
1150 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1153 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1155 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1156 calculated_number n = cn[c];
1158 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1163 // prevent a division by zero
1164 if(total == 0) total = 1;
1167 // for each dimension
1168 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1169 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1170 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1172 buffer_strcat(wb, separator);
1174 calculated_number n = cn[c];
1176 if(co[c] & RRDR_EMPTY) {
1177 if(options & RRDR_OPTION_NULL2ZERO)
1178 buffer_strcat(wb, "0");
1180 buffer_strcat(wb, "null");
1183 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1186 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1187 n = n * 100 / total;
1189 buffer_rrd_value(wb, n);
1193 buffer_strcat(wb, endline);
1195 //info("RRD2CSV(): %s: END", r->st->id);
1198 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1202 calculated_number *cn = &r->v[ i * r->d ];
1203 uint8_t *co = &r->o[ i * r->d ];
1205 calculated_number sum = 0, min = 0, max = 0, v;
1206 int all_null = 1, init = 1;
1208 calculated_number total = 1;
1209 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1211 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1212 calculated_number n = cn[c];
1214 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1219 // prevent a division by zero
1220 if(total == 0) total = 1;
1223 // for each dimension
1224 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1225 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1226 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1228 calculated_number n = cn[c];
1230 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1233 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1234 n = n * 100 / total;
1236 if(unlikely(init)) {
1248 if(likely(!(co[c] & RRDR_EMPTY))) {
1253 if(n < min) min = n;
1254 if(n > max) max = n;
1257 if(unlikely(all_null)) {
1258 if(likely(all_values_are_null))
1259 *all_values_are_null = 1;
1263 if(likely(all_values_are_null))
1264 *all_values_are_null = 0;
1267 if(options & RRDR_OPTION_MIN2MAX)
1275 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1277 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1280 buffer_strcat(wb, prefix);
1281 long start = 0, end = rrdr_rows(r), step = 1;
1282 if((options & RRDR_OPTION_REVERSED)) {
1283 start = rrdr_rows(r) - 1;
1288 // for each line in the array
1289 for(i = start; i != end ;i += step) {
1290 int all_values_are_null = 0;
1291 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1293 if(likely(i != start)) {
1294 if(r->min > v) r->min = v;
1295 if(r->max < v) r->max = v;
1302 if(likely(i != start))
1303 buffer_strcat(wb, separator);
1305 if(all_values_are_null) {
1306 if(options & RRDR_OPTION_NULL2ZERO)
1307 buffer_strcat(wb, "0");
1309 buffer_strcat(wb, "null");
1312 buffer_rrd_value(wb, v);
1314 buffer_strcat(wb, suffix);
1315 //info("RRD2SSV(): %s: END", r->st->id);
1318 inline static calculated_number *rrdr_line_values(RRDR *r)
1320 return &r->v[ r->c * r->d ];
1323 inline static uint8_t *rrdr_line_options(RRDR *r)
1325 return &r->o[ r->c * r->d ];
1328 inline static int rrdr_line_init(RRDR *r, time_t t)
1332 if(unlikely(r->c >= r->n)) {
1333 error("requested to step above RRDR size for chart %s", r->st->name);
1343 inline static void rrdr_lock_rrdset(RRDR *r) {
1345 error("NULL value given!");
1349 rrdset_rdlock(r->st);
1353 inline static void rrdr_unlock_rrdset(RRDR *r) {
1355 error("NULL value given!");
1359 if(likely(r->has_st_lock)) {
1360 rrdset_unlock(r->st);
1365 inline static void rrdr_free(RRDR *r)
1368 error("NULL value given!");
1372 rrdr_unlock_rrdset(r);
1380 static inline void rrdr_done(RRDR *r)
1386 static RRDR *rrdr_create(RRDSET *st, long n)
1389 error("NULL value given!");
1393 RRDR *r = callocz(1, sizeof(RRDR));
1396 rrdr_lock_rrdset(r);
1399 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1403 r->t = mallocz(n * sizeof(time_t));
1404 r->v = mallocz(n * r->d * sizeof(calculated_number));
1405 r->o = mallocz(n * r->d * sizeof(uint8_t));
1406 r->od = mallocz(r->d * sizeof(uint8_t));
1408 // set the hidden flag on hidden dimensions
1410 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1411 if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
1412 r->od[c] = RRDR_HIDDEN;
1419 r->update_every = 1;
1424 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1426 int debug = rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?1:0;
1427 int absolute_period_requested = -1;
1429 time_t first_entry_t = rrdset_first_entry_t(st);
1430 time_t last_entry_t = rrdset_last_entry_t(st);
1432 if(before == 0 && after == 0) {
1433 // dump the all the data
1434 before = last_entry_t;
1435 after = first_entry_t;
1436 absolute_period_requested = 0;
1439 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1440 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1441 if(abs(before) % st->update_every) {
1442 // make sure it is multiple of st->update_every
1443 if(before < 0) before = before - st->update_every - before % st->update_every;
1444 else before = before + st->update_every - before % st->update_every;
1446 if(before > 0) before = first_entry_t + before;
1447 else before = last_entry_t + before;
1448 absolute_period_requested = 0;
1451 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1452 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1453 if(after == 0) after = -st->update_every;
1454 if(abs(after) % st->update_every) {
1455 // make sure it is multiple of st->update_every
1456 if(after < 0) after = after - st->update_every - after % st->update_every;
1457 else after = after + st->update_every - after % st->update_every;
1459 after = before + after;
1460 absolute_period_requested = 0;
1463 if(absolute_period_requested == -1)
1464 absolute_period_requested = 1;
1466 // make sure they are within our timeframe
1467 if(before > last_entry_t) before = last_entry_t;
1468 if(before < first_entry_t) before = first_entry_t;
1470 if(after > last_entry_t) after = last_entry_t;
1471 if(after < first_entry_t) after = first_entry_t;
1473 // check if they are upside down
1474 if(after > before) {
1475 time_t tmp = before;
1480 // the duration of the chart
1481 time_t duration = before - after;
1482 long available_points = duration / st->update_every;
1484 if(duration <= 0 || available_points <= 0)
1485 return rrdr_create(st, 1);
1487 // check the wanted points
1488 if(points < 0) points = -points;
1489 if(points > available_points) points = available_points;
1490 if(points == 0) points = available_points;
1492 // calculate proper grouping of source data
1493 long group = available_points / points;
1494 if(group <= 0) group = 1;
1496 // round group to the closest integer
1497 if(available_points % points > points / 2) group++;
1499 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1500 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1501 long points_new = (before_new - after_new) / st->update_every / group;
1503 // find the starting and ending slots in our round robin db
1504 long start_at_slot = rrdset_time2slot(st, before_new),
1505 stop_at_slot = rrdset_time2slot(st, after_new);
1507 #ifdef NETDATA_INTERNAL_CHECKS
1508 if(after_new < first_entry_t) {
1509 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1511 if(after_new > last_entry_t) {
1512 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1514 if(before_new < first_entry_t) {
1515 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1517 if(before_new > last_entry_t) {
1518 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1520 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1521 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1523 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1524 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1526 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1527 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1531 //info("RRD2RRDR(): %s: wanted %ld points, got %ld - group=%ld, wanted duration=%u, got %u - wanted %ld - %ld, got %ld - %ld", st->id, points, points_new, group, before - after, before_new - after_new, after, before, after_new, before_new);
1534 before = before_new;
1535 duration = before - after;
1536 points = points_new;
1539 // before = the end time of the calculation
1540 // after = the start time of the calculation
1541 // duration = the duration of the calculation
1542 // group = the number of source points to aggregate / group together
1543 // method = the method of grouping source points
1544 // points = the number of points to generate
1547 // -------------------------------------------------------------------------
1548 // initialize our result set
1550 RRDR *r = rrdr_create(st, points);
1552 #ifdef NETDATA_INTERNAL_CHECKS
1553 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1558 #ifdef NETDATA_INTERNAL_CHECKS
1559 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1564 if(absolute_period_requested == 1)
1565 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1567 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1569 // find how many dimensions we have
1570 long dimensions = r->d;
1573 // -------------------------------------------------------------------------
1574 // checks for debugging
1576 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %u, last_t: %u, all_duration: %u, after: %u, before: %u, duration: %u, points: %ld, group: %ld"
1578 , (uint32_t)first_entry_t
1579 , (uint32_t)last_entry_t
1580 , (uint32_t)(last_entry_t - first_entry_t)
1583 , (uint32_t)duration
1589 // -------------------------------------------------------------------------
1590 // temp arrays for keeping values per dimension
1592 calculated_number last_values[dimensions]; // keep the last value of each dimension
1593 calculated_number group_values[dimensions]; // keep sums when grouping
1594 long group_counts[dimensions]; // keep the number of values added to group_values
1595 uint8_t group_options[dimensions];
1596 uint8_t found_non_zero[dimensions];
1602 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1604 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1605 group_counts[c] = 0;
1606 group_options[c] = 0;
1607 found_non_zero[c] = 0;
1611 // -------------------------------------------------------------------------
1614 time_t now = rrdset_slot2time(st, start_at_slot),
1615 dt = st->update_every,
1618 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %u (stop_at_t: %ld), before_t: %u (start_at_t: %ld), start_t(now): %u, current_entry: %ld, entries: %ld"
1630 r->update_every = group * st->update_every;
1634 //info("RRD2RRDR(): %s: STARTING", st->id);
1636 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1637 for(; !stop_now ; now -= dt, slot--, counter++) {
1638 if(unlikely(slot < 0)) slot = st->entries - 1;
1639 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1641 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1648 , (group_count + 1 == group)?"PRINT":" - "
1649 , (now >= after && now <= before)?"RANGE":" - "
1652 // make sure we return data in the proper time range
1653 if(unlikely(now > before)) continue;
1654 if(unlikely(now < after)) break;
1656 if(unlikely(group_count == 0)) {
1657 group_start_t = now;
1661 if(unlikely(group_count == group)) {
1662 if(unlikely(added >= points)) break;
1666 // do the calculations
1667 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1668 storage_number n = rd->values[slot];
1669 if(unlikely(!does_storage_number_exist(n))) continue;
1673 calculated_number value = unpack_storage_number(n);
1674 if(likely(value != 0.0)) {
1675 group_options[c] |= RRDR_NONZERO;
1676 found_non_zero[c] = 1;
1679 if(unlikely(did_storage_number_reset(n)))
1680 group_options[c] |= RRDR_RESET;
1682 switch(group_method) {
1684 if(unlikely(isnan(group_values[c])) ||
1685 fabsl(value) < fabsl(group_values[c]))
1686 group_values[c] = value;
1690 if(unlikely(isnan(group_values[c])) ||
1691 fabsl(value) > fabsl(group_values[c]))
1692 group_values[c] = value;
1698 case GROUP_UNDEFINED:
1699 group_values[c] += value;
1702 case GROUP_INCREMENTAL_SUM:
1703 if(unlikely(slot == start_at_slot))
1704 last_values[c] = value;
1706 group_values[c] += last_values[c] - value;
1707 last_values[c] = value;
1713 if(unlikely(add_this)) {
1714 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1718 calculated_number *cn = rrdr_line_values(r);
1719 uint8_t *co = rrdr_line_options(r);
1721 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1723 // update the dimension options
1724 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1726 // store the specific point options
1727 co[c] = group_options[c];
1730 if(unlikely(group_counts[c] == 0)) {
1732 co[c] |= RRDR_EMPTY;
1733 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1736 switch(group_method) {
1739 if(unlikely(isnan(group_values[c])))
1742 cn[c] = group_values[c];
1743 group_values[c] = NAN;
1748 case GROUP_INCREMENTAL_SUM:
1749 cn[c] = group_values[c];
1750 group_values[c] = 0;
1755 case GROUP_UNDEFINED:
1756 cn[c] = group_values[c] / group_counts[c];
1757 group_values[c] = 0;
1761 if(cn[c] < r->min) r->min = cn[c];
1762 if(cn[c] > r->max) r->max = cn[c];
1765 // reset for the next loop
1766 group_counts[c] = 0;
1767 group_options[c] = 0;
1777 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1778 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1782 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, const char *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *db_after, time_t *db_before, int *value_is_null)
1784 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1786 if(value_is_null) *value_is_null = 1;
1790 if(rrdr_rows(r) == 0) {
1793 if(db_after) *db_after = 0;
1794 if(db_before) *db_before = 0;
1795 if(value_is_null) *value_is_null = 1;
1800 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1801 buffer_no_cacheable(wb);
1802 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1803 buffer_cacheable(wb);
1805 options = rrdr_check_options(r, options, dimensions);
1808 rrdr_disable_not_selected_dimensions(r, options, dimensions);
1810 if(db_after) *db_after = r->after;
1811 if(db_before) *db_before = r->before;
1813 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1814 *n = rrdr2value(r, i, options, value_is_null);
1820 int rrd2format(RRDSET *st, BUFFER *wb, BUFFER *dimensions, uint32_t format, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp)
1822 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1824 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1828 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1829 buffer_no_cacheable(wb);
1830 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1831 buffer_cacheable(wb);
1833 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1836 rrdr_disable_not_selected_dimensions(r, options, buffer_tostring(dimensions));
1838 if(latest_timestamp && rrdr_rows(r) > 0)
1839 *latest_timestamp = r->before;
1842 case DATASOURCE_SSV:
1843 if(options & RRDR_OPTION_JSON_WRAP) {
1844 wb->contenttype = CT_APPLICATION_JSON;
1845 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1846 rrdr2ssv(r, wb, options, "", " ", "");
1847 rrdr_json_wrapper_end(r, wb, format, options, 1);
1850 wb->contenttype = CT_TEXT_PLAIN;
1851 rrdr2ssv(r, wb, options, "", " ", "");
1855 case DATASOURCE_SSV_COMMA:
1856 if(options & RRDR_OPTION_JSON_WRAP) {
1857 wb->contenttype = CT_APPLICATION_JSON;
1858 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1859 rrdr2ssv(r, wb, options, "", ",", "");
1860 rrdr_json_wrapper_end(r, wb, format, options, 1);
1863 wb->contenttype = CT_TEXT_PLAIN;
1864 rrdr2ssv(r, wb, options, "", ",", "");
1868 case DATASOURCE_JS_ARRAY:
1869 if(options & RRDR_OPTION_JSON_WRAP) {
1870 wb->contenttype = CT_APPLICATION_JSON;
1871 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1872 rrdr2ssv(r, wb, options, "[", ",", "]");
1873 rrdr_json_wrapper_end(r, wb, format, options, 0);
1876 wb->contenttype = CT_APPLICATION_JSON;
1877 rrdr2ssv(r, wb, options, "[", ",", "]");
1881 case DATASOURCE_CSV:
1882 if(options & RRDR_OPTION_JSON_WRAP) {
1883 wb->contenttype = CT_APPLICATION_JSON;
1884 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1885 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1886 rrdr_json_wrapper_end(r, wb, format, options, 1);
1889 wb->contenttype = CT_TEXT_PLAIN;
1890 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1894 case DATASOURCE_CSV_JSON_ARRAY:
1895 wb->contenttype = CT_APPLICATION_JSON;
1896 if(options & RRDR_OPTION_JSON_WRAP) {
1897 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1898 buffer_strcat(wb, "[\n");
1899 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1900 buffer_strcat(wb, "\n]");
1901 rrdr_json_wrapper_end(r, wb, format, options, 0);
1904 wb->contenttype = CT_TEXT_PLAIN;
1905 buffer_strcat(wb, "[\n");
1906 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1907 buffer_strcat(wb, "\n]");
1911 case DATASOURCE_TSV:
1912 if(options & RRDR_OPTION_JSON_WRAP) {
1913 wb->contenttype = CT_APPLICATION_JSON;
1914 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1915 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1916 rrdr_json_wrapper_end(r, wb, format, options, 1);
1919 wb->contenttype = CT_TEXT_PLAIN;
1920 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1924 case DATASOURCE_HTML:
1925 if(options & RRDR_OPTION_JSON_WRAP) {
1926 wb->contenttype = CT_APPLICATION_JSON;
1927 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1928 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1929 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1930 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1931 rrdr_json_wrapper_end(r, wb, format, options, 1);
1934 wb->contenttype = CT_TEXT_HTML;
1935 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1936 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1937 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1941 case DATASOURCE_DATATABLE_JSONP:
1942 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1944 if(options & RRDR_OPTION_JSON_WRAP)
1945 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1947 rrdr2json(r, wb, options, 1);
1949 if(options & RRDR_OPTION_JSON_WRAP)
1950 rrdr_json_wrapper_end(r, wb, format, options, 0);
1953 case DATASOURCE_DATATABLE_JSON:
1954 wb->contenttype = CT_APPLICATION_JSON;
1956 if(options & RRDR_OPTION_JSON_WRAP)
1957 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1959 rrdr2json(r, wb, options, 1);
1961 if(options & RRDR_OPTION_JSON_WRAP)
1962 rrdr_json_wrapper_end(r, wb, format, options, 0);
1965 case DATASOURCE_JSONP:
1966 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1967 if(options & RRDR_OPTION_JSON_WRAP)
1968 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1970 rrdr2json(r, wb, options, 0);
1972 if(options & RRDR_OPTION_JSON_WRAP)
1973 rrdr_json_wrapper_end(r, wb, format, options, 0);
1976 case DATASOURCE_JSON:
1978 wb->contenttype = CT_APPLICATION_JSON;
1980 if(options & RRDR_OPTION_JSON_WRAP)
1981 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1983 rrdr2json(r, wb, options, 0);
1985 if(options & RRDR_OPTION_JSON_WRAP)
1986 rrdr_json_wrapper_end(r, wb, format, options, 0);
1994 time_t rrd_stats_json(int type, RRDSET *st, BUFFER *wb, long points, long group, int group_method, time_t after, time_t before, int only_non_zero)
2000 // -------------------------------------------------------------------------
2001 // switch from JSON to google JSON
2006 case DATASOURCE_DATATABLE_JSON:
2007 case DATASOURCE_DATATABLE_JSONP:
2012 case DATASOURCE_JSON:
2018 // -------------------------------------------------------------------------
2019 // validate the parameters
2021 if(points < 1) points = 1;
2022 if(group < 1) group = 1;
2024 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
2025 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
2029 // our return value (the last timestamp printed)
2030 // this is required to detect re-transmit in google JSONP
2031 time_t last_timestamp = 0;
2034 // -------------------------------------------------------------------------
2035 // find how many dimensions we have
2039 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
2042 buffer_strcat(wb, "No dimensions yet.");
2047 // -------------------------------------------------------------------------
2048 // prepare various strings, to speed up the loop
2050 char overflow_annotation[201]; snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
2051 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
2052 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
2053 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
2054 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
2055 char post_value[21]; strcpy(post_value, "}");
2058 // -------------------------------------------------------------------------
2059 // checks for debugging
2061 if(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)) {
2062 debug(D_RRD_STATS, "%s first_entry_t = %ld, last_entry_t = %ld, duration = %ld, after = %ld, before = %ld, duration = %ld, entries_to_show = %ld, group = %ld"
2064 , rrdset_first_entry_t(st)
2065 , rrdset_last_entry_t(st)
2066 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
2075 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
2077 if((before - after) > st->entries * st->update_every)
2078 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%ld) is higher than the capacity of the database (%ld)", st->name, before - after, st->entries * st->update_every);
2082 // -------------------------------------------------------------------------
2083 // temp arrays for keeping values per dimension
2085 calculated_number group_values[dimensions]; // keep sums when grouping
2086 int print_hidden[dimensions]; // keep hidden flags
2087 int found_non_zero[dimensions];
2088 int found_non_existing[dimensions];
2091 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2092 group_values[c] = 0;
2093 print_hidden[c] = rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?1:0;
2094 found_non_zero[c] = 0;
2095 found_non_existing[c] = 0;
2099 // error("OLD: points=%d after=%d before=%d group=%d, duration=%d", entries_to_show, before - (st->update_every * group * entries_to_show), before, group, before - after + 1);
2100 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
2101 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
2103 // -------------------------------------------------------------------------
2104 // remove dimensions that contain only zeros
2107 if(only_non_zero) max_loop = 2;
2109 for(; max_loop ; max_loop--) {
2111 // -------------------------------------------------------------------------
2112 // print the JSON header
2114 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
2115 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2116 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2117 buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2119 // print the header for each dimension
2120 // and update the print_hidden array for the dimensions that should be hidden
2122 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2123 if(!print_hidden[c]) {
2125 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2129 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, "no data", sq, kq, kq, sq, sq, kq, kq, sq, sq);
2132 // print the begin of row data
2133 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2136 // -------------------------------------------------------------------------
2139 int annotate_reset = 0;
2140 int annotation_count = 0;
2142 long t = rrdset_time2slot(st, before),
2143 stop_at_t = rrdset_time2slot(st, after),
2148 time_t now = rrdset_slot2time(st, t),
2149 dt = st->update_every;
2151 long count = 0, printed = 0, group_count = 0;
2154 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
2155 debug(D_RRD_STATS, "%s: REQUEST after:%u before:%u, points:%ld, group:%ld, CHART cur:%ld first: %u last:%u, CALC start_t:%ld, stop_t:%ld"
2162 , (uint32_t)rrdset_first_entry_t(st)
2163 , (uint32_t)rrdset_last_entry_t(st)
2169 for(; !stop_now ; now -= dt, t--, counter++) {
2170 if(t < 0) t = st->entries - 1;
2171 if(t == stop_at_t) stop_now = counter;
2175 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
2176 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
2183 , (group_count + 1 == group)?"PRINT":" - "
2184 , (now >= after && now <= before)?"RANGE":" - "
2188 // make sure we return data in the proper time range
2189 if(now > before) continue;
2190 if(now < after) break;
2192 //if(rrdset_slot2time(st, t) != now)
2193 // error("%s: slot=%ld, now=%ld, slot2time=%ld, diff=%ld, last_entry_t=%ld, rrdset_last_slot=%ld", st->id, t, now, rrdset_slot2time(st,t), now - rrdset_slot2time(st,t), rrdset_last_entry_t(st), rrdset_last_slot(st));
2198 // check if we have to print this now
2199 if(group_count == group) {
2200 if(printed >= points) {
2201 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2205 // generate the local date time
2206 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
2207 if(!tm) { error("localtime() failed."); continue; }
2208 if(now > last_timestamp) last_timestamp = now;
2210 if(printed) buffer_strcat(wb, "]},\n");
2211 buffer_strcat(wb, pre_date);
2212 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2213 buffer_strcat(wb, post_date);
2218 // do the calculations
2219 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2220 storage_number n = rd->values[t];
2221 calculated_number value = unpack_storage_number(n);
2223 if(!does_storage_number_exist(n)) {
2225 found_non_existing[c]++;
2227 if(did_storage_number_reset(n)) annotate_reset = 1;
2229 switch(group_method) {
2231 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2235 group_values[c] += value;
2240 group_values[c] += value;
2241 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2247 if(annotate_reset) {
2249 buffer_strcat(wb, overflow_annotation);
2253 buffer_strcat(wb, normal_annotation);
2256 for(c = 0 ; c < dimensions ; c++) {
2257 if(found_non_existing[c] == group_count) {
2258 // all entries are non-existing
2260 buffer_strcat(wb, pre_value);
2261 buffer_strcat(wb, "null");
2262 buffer_strcat(wb, post_value);
2264 else if(!print_hidden[c]) {
2266 buffer_strcat(wb, pre_value);
2267 buffer_rrd_value(wb, group_values[c]);
2268 buffer_strcat(wb, post_value);
2270 if(group_values[c]) found_non_zero[c]++;
2273 // reset them for the next loop
2274 group_values[c] = 0;
2275 found_non_existing[c] = 0;
2278 // if all dimensions are hidden, print a null
2280 buffer_strcat(wb, pre_value);
2281 buffer_strcat(wb, "null");
2282 buffer_strcat(wb, post_value);
2290 if(printed) buffer_strcat(wb, "]}");
2291 buffer_strcat(wb, "\n ]\n}\n");
2293 if(only_non_zero && max_loop > 1) {
2295 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2296 group_values[c] = 0;
2297 found_non_existing[c] = 0;
2299 if(!print_hidden[c] && !found_non_zero[c]) {
2301 print_hidden[c] = 1;
2305 if(changed) buffer_flush(wb);
2312 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %zu bytes", st->name, wb->len);
2315 return last_timestamp;