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 rrddim_foreach_read(rd, st) {
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
101 rrdhost_rdlock(host);
102 rrdset_foreach_read(st, host) {
103 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
104 if(c) buffer_strcat(wb, ",");
105 buffer_strcat(wb, "\n\t\t\"");
106 buffer_strcat(wb, st->id);
107 buffer_strcat(wb, "\": ");
108 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
114 for(rc = host->alarms; rc ; rc = rc->next) {
118 rrdhost_unlock(host);
120 buffer_sprintf(wb, "\n\t}"
121 ",\n\t\"charts_count\": %zu"
122 ",\n\t\"dimensions_count\": %zu"
123 ",\n\t\"alarms_count\": %zu"
124 ",\n\t\"rrd_memory_bytes\": %zu"
125 ",\n\t\"hosts_count\": %zu"
131 , rrd_hosts_available
134 if(unlikely(rrd_hosts_available > 1)) {
137 rrdhost_foreach_read(h)
140 "\n\t\t\t\"hostname\": \"%s\""
142 , (h != localhost)?",":""
150 "\n\t\t\t\"hostname\": \"%s\""
156 buffer_sprintf(wb, "\n\t]\n}\n");
159 // ----------------------------------------------------------------------------
161 // /api/v1/allmetrics?format=prometheus
163 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
166 for(n = 0; *s && n < usable ; d++, s++, n++) {
167 register char c = *s;
169 if(unlikely(!isalnum(c))) *d = '_';
177 #define PROMETHEUS_ELEMENT_MAX 256
179 void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb) {
180 rrdhost_rdlock(host);
182 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
183 prometheus_name_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
187 rrdset_foreach_read(st, host) {
188 char chart[PROMETHEUS_ELEMENT_MAX + 1];
189 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
191 buffer_strcat(wb, "\n");
192 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
195 // for each dimension
197 rrddim_foreach_read(rd, st) {
198 if(rd->collections_counter) {
199 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
200 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
202 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
204 switch(rd->algorithm) {
205 case RRD_ALGORITHM_INCREMENTAL:
206 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
207 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
211 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
215 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
216 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
217 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
219 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
220 chart, dimension, hostname, rd->last_collected_value,
221 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
230 rrdhost_unlock(host);
233 // ----------------------------------------------------------------------------
235 // /api/v1/allmetrics?format=bash
237 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
240 for(n = 0; *s && n < usable ; d++, s++, n++) {
241 register char c = *s;
243 if(unlikely(!isalnum(c))) *d = '_';
244 else *d = (char)toupper(c);
251 #define SHELL_ELEMENT_MAX 100
253 void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb) {
254 rrdhost_rdlock(host);
258 rrdset_foreach_read(st, host) {
259 calculated_number total = 0.0;
260 char chart[SHELL_ELEMENT_MAX + 1];
261 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
263 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
264 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
267 // for each dimension
269 rrddim_foreach_read(rd, st) {
270 if(rd->collections_counter) {
271 char dimension[SHELL_ELEMENT_MAX + 1];
272 shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
274 calculated_number n = rd->last_stored_value;
276 if(isnan(n) || isinf(n))
277 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
279 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
281 if(!rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) total += n;
282 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
287 total = roundl(total);
288 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
293 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
296 for(rc = host->alarms; rc ;rc = rc->next) {
297 if(!rc->rrdset) continue;
299 char chart[SHELL_ELEMENT_MAX + 1];
300 shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
302 char alarm[SHELL_ELEMENT_MAX + 1];
303 shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
305 calculated_number n = rc->value;
307 if(isnan(n) || isinf(n))
308 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\" # %s\n", chart, alarm, rc->units);
311 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\" # %s\n", chart, alarm, n, rc->units);
314 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
317 rrdhost_unlock(host);
320 // ----------------------------------------------------------------------------
322 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
324 time_t now = now_realtime_sec();
330 "\t\t\t\"id\": \"%s\",\n"
331 "\t\t\t\"name\": \"%s\",\n"
332 "\t\t\t\"type\": \"%s\",\n"
333 "\t\t\t\"family\": \"%s\",\n"
334 "\t\t\t\"context\": \"%s\",\n"
335 "\t\t\t\"title\": \"%s\",\n"
336 "\t\t\t\"priority\": %ld,\n"
337 "\t\t\t\"enabled\": %d,\n"
338 "\t\t\t\"units\": \"%s\",\n"
339 "\t\t\t\"url\": \"/data/%s/%s\",\n"
340 "\t\t\t\"chart_type\": \"%s\",\n"
341 "\t\t\t\"counter\": %lu,\n"
342 "\t\t\t\"entries\": %ld,\n"
343 "\t\t\t\"first_entry_t\": %ld,\n"
344 "\t\t\t\"last_entry\": %lu,\n"
345 "\t\t\t\"last_entry_t\": %ld,\n"
346 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
347 "\t\t\t\"update_every\": %d,\n"
348 "\t\t\t\"isdetail\": %d,\n"
349 "\t\t\t\"usec_since_last_update\": %llu,\n"
350 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
351 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
352 "\t\t\t\"dimensions\": [\n"
360 , rrdset_flag_check(st, RRDSET_FLAG_ENABLED)?1:0
362 , st->name, options?options:""
363 , rrdset_type_name(st->chart_type)
366 , rrdset_first_entry_t(st)
367 , rrdset_last_slot(st)
368 , rrdset_last_entry_t(st)
369 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
371 , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?1:0
372 , st->usec_since_last_update
373 , st->collected_total
374 , st->last_collected_total
377 unsigned long memory = st->memsize;
380 rrddim_foreach_read(rd, st) {
382 memory += rd->memsize;
386 "\t\t\t\t\t\"id\": \"%s\",\n"
387 "\t\t\t\t\t\"name\": \"%s\",\n"
388 "\t\t\t\t\t\"entries\": %ld,\n"
389 "\t\t\t\t\t\"isHidden\": %d,\n"
390 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
391 "\t\t\t\t\t\"multiplier\": " COLLECTED_NUMBER_FORMAT ",\n"
392 "\t\t\t\t\t\"divisor\": " COLLECTED_NUMBER_FORMAT ",\n"
393 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
394 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
395 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
396 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
397 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
398 "\t\t\t\t\t\"memory\": %lu\n"
403 , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?1:0
404 , rrd_algorithm_name(rd->algorithm)
407 , rd->last_collected_time.tv_sec
408 , rd->collected_value
409 , rd->calculated_value
410 , rd->last_collected_value
411 , rd->last_calculated_value
419 "\t\t\t\"memory\" : %lu\n"
428 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
429 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
431 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
433 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
434 rrd_stats_one_json(st, options, wb);
435 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
438 void rrd_stats_all_json(RRDHOST *host, BUFFER *wb)
440 unsigned long memory = 0;
444 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
446 rrdhost_rdlock(host);
447 rrdset_foreach_read(st, host) {
448 if(rrdset_flag_check(st, RRDSET_FLAG_ENABLED) && st->dimensions) {
449 if(c) buffer_strcat(wb, ",\n");
450 memory += rrd_stats_one_json(st, NULL, wb);
454 rrdhost_unlock(host);
456 buffer_sprintf(wb, "\n\t],\n"
457 "\t\"hostname\": \"%s\",\n"
458 "\t\"update_every\": %d,\n"
459 "\t\"history\": %d,\n"
460 "\t\"memory\": %lu\n"
463 , host->rrd_update_every
464 , host->rrd_history_entries
471 // ----------------------------------------------------------------------------
473 // RRDR dimension options
474 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
475 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
476 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
477 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
478 #define RRDR_SELECTED 0x10 // the dimension is selected
480 // RRDR result options
481 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
482 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
484 typedef struct rrdresult {
485 RRDSET *st; // the chart this result refers to
487 uint32_t result_options; // RRDR_RESULT_OPTION_*
489 int d; // the number of dimensions
490 long n; // the number of values in the arrays
491 long rows; // the number of rows used
493 uint8_t *od; // the options for the dimensions
495 time_t *t; // array of n timestamps
496 calculated_number *v; // array n x d values
497 uint8_t *o; // array n x d options
499 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
501 long group; // how many collected values were grouped for each row
502 int update_every; // what is the suggested update frequency in seconds
504 calculated_number min;
505 calculated_number max;
510 int has_st_lock; // if st is read locked by us
513 #define rrdr_rows(r) ((r)->rows)
516 static void rrdr_dump(RRDR *r)
521 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
523 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
524 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
527 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
528 , (r->od[c] & RRDR_RESET)?"RESET ":""
529 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
530 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
535 fprintf(stderr, "RRDR does not have any values in it.\n");
539 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
541 // for each line in the array
542 for(i = 0; i < r->rows ;i++) {
543 calculated_number *cn = &r->v[ i * r->d ];
544 uint8_t *co = &r->o[ i * r->d ];
546 // print the id and the timestamp of the line
547 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
549 // for each dimension
550 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
551 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
552 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
554 if(co[c] & RRDR_EMPTY)
555 fprintf(stderr, "null ");
557 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
559 , (co[c] & RRDR_EMPTY)?"E":" "
560 , (co[c] & RRDR_RESET)?"R":" "
561 , (co[c] & RRDR_HIDDEN)?"H":" "
562 , (co[c] & RRDR_NONZERO)?"N":" "
566 fprintf(stderr, "\n");
571 void rrdr_disable_not_selected_dimensions(RRDR *r, uint32_t options, const char *dims) {
572 rrdset_check_rdlock(r->st);
574 if(unlikely(!dims || !*dims)) return;
576 char b[strlen(dims) + 1];
580 long c, dims_selected = 0, dims_not_hidden_not_zero = 0;
583 // disable all of them
584 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
585 r->od[c] |= RRDR_HIDDEN;
587 while(o && *o && (tok = mystrsep(&o, ",|"))) {
590 uint32_t hash = simple_hash(tok);
592 // find it and enable it
593 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
594 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || (hash == d->hash_name && !strcmp(d->name, tok)))) {
596 if(likely(r->od[c] & RRDR_HIDDEN)) {
597 r->od[c] |= RRDR_SELECTED;
598 r->od[c] &= ~RRDR_HIDDEN;
602 // since the user needs this dimension
603 // make it appear as NONZERO, to return it
604 // even if the dimension has only zeros
605 // unless option non_zero is set
606 if(likely(!(options & RRDR_OPTION_NONZERO)))
607 r->od[c] |= RRDR_NONZERO;
609 // count the visible dimensions
610 if(likely(r->od[c] & RRDR_NONZERO))
611 dims_not_hidden_not_zero++;
616 // check if all dimensions are hidden
617 if(unlikely(!dims_not_hidden_not_zero && dims_selected)) {
618 // there are a few selected dimensions
619 // but they are all zero
620 // enable the selected ones
621 // to avoid returning an empty chart
622 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
623 if(unlikely(r->od[c] & RRDR_SELECTED))
624 r->od[c] |= RRDR_NONZERO;
628 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
631 case DATASOURCE_JSON:
632 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
635 case DATASOURCE_DATATABLE_JSON:
636 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
639 case DATASOURCE_DATATABLE_JSONP:
640 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
643 case DATASOURCE_JSONP:
644 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
648 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
652 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
656 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
659 case DATASOURCE_HTML:
660 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
663 case DATASOURCE_JS_ARRAY:
664 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
667 case DATASOURCE_SSV_COMMA:
668 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
672 buffer_strcat(wb, "unknown");
677 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
679 rrdset_check_rdlock(r->st);
683 if(options & RRDR_OPTION_NONZERO) {
686 // commented due to #1514
688 //if(dims && *dims) {
689 // the caller wants specific dimensions
690 // disable NONZERO option
691 // to make sure we don't accidentally prevent
692 // the specific dimensions from being returned
696 // find how many dimensions are not zero
699 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
700 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
701 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
706 // if with nonzero we get i = 0 (no dimensions will be returned)
707 // disable nonzero to show all dimensions
708 if(!i) options &= ~RRDR_OPTION_NONZERO;
714 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
716 rrdset_check_rdlock(r->st);
718 long rows = rrdr_rows(r);
722 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
723 char kq[2] = "", // key quote
724 sq[2] = ""; // string quote
726 if( options & RRDR_OPTION_GOOGLE_JSON ) {
735 buffer_sprintf(wb, "{\n"
738 " %sname%s: %s%s%s,\n"
739 " %sview_update_every%s: %d,\n"
740 " %supdate_every%s: %d,\n"
741 " %sfirst_entry%s: %u,\n"
742 " %slast_entry%s: %u,\n"
745 " %sdimension_names%s: ["
747 , kq, kq, sq, r->st->id, sq
748 , kq, kq, sq, r->st->name, sq
749 , kq, kq, r->update_every
750 , kq, kq, r->st->update_every
751 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
752 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
753 , kq, kq, (uint32_t)r->before
754 , kq, kq, (uint32_t)r->after
757 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
758 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
759 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
761 if(i) buffer_strcat(wb, ", ");
762 buffer_strcat(wb, sq);
763 buffer_strcat(wb, rd->name);
764 buffer_strcat(wb, sq);
768 #ifdef NETDATA_INTERNAL_CHECKS
769 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
772 buffer_strcat(wb, sq);
773 buffer_strcat(wb, "no data");
774 buffer_strcat(wb, sq);
777 buffer_sprintf(wb, "],\n"
778 " %sdimension_ids%s: ["
781 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
782 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
783 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
785 if(i) buffer_strcat(wb, ", ");
786 buffer_strcat(wb, sq);
787 buffer_strcat(wb, rd->id);
788 buffer_strcat(wb, sq);
793 buffer_strcat(wb, sq);
794 buffer_strcat(wb, "no data");
795 buffer_strcat(wb, sq);
798 buffer_sprintf(wb, "],\n"
799 " %slatest_values%s: ["
802 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
803 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
804 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
806 if(i) buffer_strcat(wb, ", ");
809 storage_number n = rd->values[rrdset_last_slot(r->st)];
811 if(!does_storage_number_exist(n))
812 buffer_strcat(wb, "null");
814 buffer_rrd_value(wb, unpack_storage_number(n));
818 buffer_strcat(wb, "null");
821 buffer_sprintf(wb, "],\n"
822 " %sview_latest_values%s: ["
827 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
828 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
829 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
831 if(i) buffer_strcat(wb, ", ");
834 calculated_number *cn = &r->v[ (0) * r->d ];
835 uint8_t *co = &r->o[ (0) * r->d ];
837 if(co[c] & RRDR_EMPTY)
838 buffer_strcat(wb, "null");
840 buffer_rrd_value(wb, cn[c]);
845 buffer_strcat(wb, "null");
848 buffer_sprintf(wb, "],\n"
849 " %sdimensions%s: %ld,\n"
850 " %spoints%s: %ld,\n"
857 rrdr_buffer_print_format(wb, format);
859 buffer_sprintf(wb, "%s,\n"
865 if(string_value) buffer_strcat(wb, sq);
866 //info("JSONWRAPPER(): %s: END", r->st->id);
869 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
873 char kq[2] = "", // key quote
874 sq[2] = ""; // string quote
876 if( options & RRDR_OPTION_GOOGLE_JSON ) {
885 if(string_value) buffer_strcat(wb, sq);
887 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
888 buffer_rrd_value(wb, r->min);
889 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
890 buffer_rrd_value(wb, r->max);
891 buffer_strcat(wb, "\n}\n");
894 #define JSON_DATES_JS 1
895 #define JSON_DATES_TIMESTAMP 2
897 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
899 rrdset_check_rdlock(r->st);
901 //info("RRD2JSON(): %s: BEGIN", r->st->id);
902 int row_annotations = 0, dates, dates_with_new = 0;
903 char kq[2] = "", // key quote
904 sq[2] = "", // string quote
905 pre_label[101] = "", // before each label
906 post_label[101] = "", // after each label
907 pre_date[101] = "", // the beginning of line, to the date
908 post_date[101] = "", // closing the date
909 pre_value[101] = "", // before each value
910 post_value[101] = "", // after each value
911 post_line[101] = "", // at the end of each row
912 normal_annotation[201] = "", // default row annotation
913 overflow_annotation[201] = "", // overflow row annotation
914 data_begin[101] = "", // between labels and values
915 finish[101] = ""; // at the end of everything
918 dates = JSON_DATES_JS;
919 if( options & RRDR_OPTION_GOOGLE_JSON ) {
928 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
929 snprintfz(post_date, 100, "%s}", sq);
930 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
931 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
932 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
933 strcpy(post_value, "}");
934 strcpy(post_line, "]}");
935 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
936 strcpy(finish, "\n ]\n}");
938 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);
939 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
941 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
942 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);
943 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);
944 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);
946 // remove the valueobjects flag
947 // google wants its own keys
948 if(options & RRDR_OPTION_OBJECTSROWS)
949 options &= ~RRDR_OPTION_OBJECTSROWS;
954 if(options & RRDR_OPTION_GOOGLE_JSON) {
955 dates = JSON_DATES_JS;
959 dates = JSON_DATES_TIMESTAMP;
962 if( options & RRDR_OPTION_OBJECTSROWS )
963 strcpy(pre_date, " { ");
965 strcpy(pre_date, " [ ");
966 strcpy(pre_label, ", \"");
967 strcpy(post_label, "\"");
968 strcpy(pre_value, ", ");
969 if( options & RRDR_OPTION_OBJECTSROWS )
970 strcpy(post_line, "}");
972 strcpy(post_line, "]");
973 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
974 strcpy(finish, "\n ]\n}");
976 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
977 buffer_sprintf(wb, "%stime%s", sq, sq);
980 // -------------------------------------------------------------------------
981 // print the JSON header
986 // print the header lines
987 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
988 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
989 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
991 buffer_strcat(wb, pre_label);
992 buffer_strcat(wb, rd->name);
993 buffer_strcat(wb, post_label);
997 buffer_strcat(wb, pre_label);
998 buffer_strcat(wb, "no data");
999 buffer_strcat(wb, post_label);
1002 // print the begin of row data
1003 buffer_strcat(wb, data_begin);
1005 // if all dimensions are hidden, print a null
1007 buffer_strcat(wb, finish);
1011 long start = 0, end = rrdr_rows(r), step = 1;
1012 if((options & RRDR_OPTION_REVERSED)) {
1013 start = rrdr_rows(r) - 1;
1018 // for each line in the array
1019 calculated_number total = 1;
1020 for(i = start; i != end ;i += step) {
1021 calculated_number *cn = &r->v[ i * r->d ];
1022 uint8_t *co = &r->o[ i * r->d ];
1024 time_t now = r->t[i];
1026 if(dates == JSON_DATES_JS) {
1027 // generate the local date time
1028 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1029 if(!tm) { error("localtime_r() failed."); continue; }
1031 if(likely(i != start)) buffer_strcat(wb, ",\n");
1032 buffer_strcat(wb, pre_date);
1034 if( options & RRDR_OPTION_OBJECTSROWS )
1035 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1038 buffer_strcat(wb, "new ");
1040 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1042 buffer_strcat(wb, post_date);
1044 if(row_annotations) {
1045 // google supports one annotation per row
1046 int annotation_found = 0;
1047 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
1048 if(co[c] & RRDR_RESET) {
1049 buffer_strcat(wb, overflow_annotation);
1050 annotation_found = 1;
1054 if(!annotation_found)
1055 buffer_strcat(wb, normal_annotation);
1059 // print the timestamp of the line
1060 if(likely(i != start)) buffer_strcat(wb, ",\n");
1061 buffer_strcat(wb, pre_date);
1063 if( options & RRDR_OPTION_OBJECTSROWS )
1064 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1066 buffer_rrd_value(wb, (calculated_number)r->t[i]);
1068 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1070 buffer_strcat(wb, post_date);
1073 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1075 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1076 calculated_number n = cn[c];
1078 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1083 // prevent a division by zero
1084 if(total == 0) total = 1;
1087 // for each dimension
1088 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1089 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1090 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1092 calculated_number n = cn[c];
1094 buffer_strcat(wb, pre_value);
1096 if( options & RRDR_OPTION_OBJECTSROWS )
1097 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1099 if(co[c] & RRDR_EMPTY) {
1100 if(options & RRDR_OPTION_NULL2ZERO)
1101 buffer_strcat(wb, "0");
1103 buffer_strcat(wb, "null");
1106 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1109 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1110 n = n * 100 / total;
1112 buffer_rrd_value(wb, n);
1115 buffer_strcat(wb, post_value);
1118 buffer_strcat(wb, post_line);
1121 buffer_strcat(wb, finish);
1122 //info("RRD2JSON(): %s: END", r->st->id);
1125 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1127 rrdset_check_rdlock(r->st);
1129 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1133 // print the csv header
1134 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1135 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1136 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1139 buffer_strcat(wb, startline);
1140 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1141 buffer_strcat(wb, "time");
1142 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1144 buffer_strcat(wb, separator);
1145 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1146 buffer_strcat(wb, d->name);
1147 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1150 buffer_strcat(wb, endline);
1153 // no dimensions present
1157 long start = 0, end = rrdr_rows(r), step = 1;
1158 if((options & RRDR_OPTION_REVERSED)) {
1159 start = rrdr_rows(r) - 1;
1164 // for each line in the array
1165 calculated_number total = 1;
1166 for(i = start; i != end ;i += step) {
1167 calculated_number *cn = &r->v[ i * r->d ];
1168 uint8_t *co = &r->o[ i * r->d ];
1170 buffer_strcat(wb, betweenlines);
1171 buffer_strcat(wb, startline);
1173 time_t now = r->t[i];
1175 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1176 // print the timestamp of the line
1177 buffer_rrd_value(wb, (calculated_number)now);
1179 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1182 // generate the local date time
1183 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1184 if(!tm) { error("localtime() failed."); continue; }
1185 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1188 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1190 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1191 calculated_number n = cn[c];
1193 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1198 // prevent a division by zero
1199 if(total == 0) total = 1;
1202 // for each dimension
1203 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1204 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1205 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1207 buffer_strcat(wb, separator);
1209 calculated_number n = cn[c];
1211 if(co[c] & RRDR_EMPTY) {
1212 if(options & RRDR_OPTION_NULL2ZERO)
1213 buffer_strcat(wb, "0");
1215 buffer_strcat(wb, "null");
1218 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1221 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1222 n = n * 100 / total;
1224 buffer_rrd_value(wb, n);
1228 buffer_strcat(wb, endline);
1230 //info("RRD2CSV(): %s: END", r->st->id);
1233 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1234 rrdset_check_rdlock(r->st);
1239 calculated_number *cn = &r->v[ i * r->d ];
1240 uint8_t *co = &r->o[ i * r->d ];
1242 calculated_number sum = 0, min = 0, max = 0, v;
1243 int all_null = 1, init = 1;
1245 calculated_number total = 1;
1246 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1248 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1249 calculated_number n = cn[c];
1251 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1256 // prevent a division by zero
1257 if(total == 0) total = 1;
1260 // for each dimension
1261 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1262 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1263 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1265 calculated_number n = cn[c];
1267 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1270 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1271 n = n * 100 / total;
1273 if(unlikely(init)) {
1285 if(likely(!(co[c] & RRDR_EMPTY))) {
1290 if(n < min) min = n;
1291 if(n > max) max = n;
1294 if(unlikely(all_null)) {
1295 if(likely(all_values_are_null))
1296 *all_values_are_null = 1;
1300 if(likely(all_values_are_null))
1301 *all_values_are_null = 0;
1304 if(options & RRDR_OPTION_MIN2MAX)
1312 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1314 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1317 buffer_strcat(wb, prefix);
1318 long start = 0, end = rrdr_rows(r), step = 1;
1319 if((options & RRDR_OPTION_REVERSED)) {
1320 start = rrdr_rows(r) - 1;
1325 // for each line in the array
1326 for(i = start; i != end ;i += step) {
1327 int all_values_are_null = 0;
1328 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1330 if(likely(i != start)) {
1331 if(r->min > v) r->min = v;
1332 if(r->max < v) r->max = v;
1339 if(likely(i != start))
1340 buffer_strcat(wb, separator);
1342 if(all_values_are_null) {
1343 if(options & RRDR_OPTION_NULL2ZERO)
1344 buffer_strcat(wb, "0");
1346 buffer_strcat(wb, "null");
1349 buffer_rrd_value(wb, v);
1351 buffer_strcat(wb, suffix);
1352 //info("RRD2SSV(): %s: END", r->st->id);
1355 inline static calculated_number *rrdr_line_values(RRDR *r)
1357 return &r->v[ r->c * r->d ];
1360 inline static uint8_t *rrdr_line_options(RRDR *r)
1362 return &r->o[ r->c * r->d ];
1365 inline static int rrdr_line_init(RRDR *r, time_t t)
1369 if(unlikely(r->c >= r->n)) {
1370 error("requested to step above RRDR size for chart %s", r->st->name);
1380 inline static void rrdr_lock_rrdset(RRDR *r) {
1382 error("NULL value given!");
1386 rrdset_rdlock(r->st);
1390 inline static void rrdr_unlock_rrdset(RRDR *r) {
1392 error("NULL value given!");
1396 if(likely(r->has_st_lock)) {
1397 rrdset_unlock(r->st);
1402 inline static void rrdr_free(RRDR *r)
1405 error("NULL value given!");
1409 rrdr_unlock_rrdset(r);
1417 static inline void rrdr_done(RRDR *r)
1423 static RRDR *rrdr_create(RRDSET *st, long n)
1426 error("NULL value given!");
1430 RRDR *r = callocz(1, sizeof(RRDR));
1433 rrdr_lock_rrdset(r);
1436 rrddim_foreach_read(rd, st) r->d++;
1440 r->t = mallocz(n * sizeof(time_t));
1441 r->v = mallocz(n * r->d * sizeof(calculated_number));
1442 r->o = mallocz(n * r->d * sizeof(uint8_t));
1443 r->od = mallocz(r->d * sizeof(uint8_t));
1445 // set the hidden flag on hidden dimensions
1447 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1448 if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
1449 r->od[c] = RRDR_HIDDEN;
1456 r->update_every = 1;
1461 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1463 int debug = rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?1:0;
1464 int absolute_period_requested = -1;
1466 time_t first_entry_t = rrdset_first_entry_t(st);
1467 time_t last_entry_t = rrdset_last_entry_t(st);
1469 if(before == 0 && after == 0) {
1470 // dump the all the data
1471 before = last_entry_t;
1472 after = first_entry_t;
1473 absolute_period_requested = 0;
1476 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1477 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1478 if(abs(before) % st->update_every) {
1479 // make sure it is multiple of st->update_every
1480 if(before < 0) before = before - st->update_every - before % st->update_every;
1481 else before = before + st->update_every - before % st->update_every;
1483 if(before > 0) before = first_entry_t + before;
1484 else before = last_entry_t + before;
1485 absolute_period_requested = 0;
1488 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1489 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1490 if(after == 0) after = -st->update_every;
1491 if(abs(after) % st->update_every) {
1492 // make sure it is multiple of st->update_every
1493 if(after < 0) after = after - st->update_every - after % st->update_every;
1494 else after = after + st->update_every - after % st->update_every;
1496 after = before + after;
1497 absolute_period_requested = 0;
1500 if(absolute_period_requested == -1)
1501 absolute_period_requested = 1;
1503 // make sure they are within our timeframe
1504 if(before > last_entry_t) before = last_entry_t;
1505 if(before < first_entry_t) before = first_entry_t;
1507 if(after > last_entry_t) after = last_entry_t;
1508 if(after < first_entry_t) after = first_entry_t;
1510 // check if they are upside down
1511 if(after > before) {
1512 time_t tmp = before;
1517 // the duration of the chart
1518 time_t duration = before - after;
1519 long available_points = duration / st->update_every;
1521 if(duration <= 0 || available_points <= 0)
1522 return rrdr_create(st, 1);
1524 // check the wanted points
1525 if(points < 0) points = -points;
1526 if(points > available_points) points = available_points;
1527 if(points == 0) points = available_points;
1529 // calculate proper grouping of source data
1530 long group = available_points / points;
1531 if(group <= 0) group = 1;
1533 // round group to the closest integer
1534 if(available_points % points > points / 2) group++;
1536 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1537 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1538 long points_new = (before_new - after_new) / st->update_every / group;
1540 // find the starting and ending slots in our round robin db
1541 long start_at_slot = rrdset_time2slot(st, before_new),
1542 stop_at_slot = rrdset_time2slot(st, after_new);
1544 #ifdef NETDATA_INTERNAL_CHECKS
1545 if(after_new < first_entry_t) {
1546 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1548 if(after_new > last_entry_t) {
1549 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1551 if(before_new < first_entry_t) {
1552 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1554 if(before_new > last_entry_t) {
1555 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1557 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1558 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1560 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1561 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1563 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1564 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1568 //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);
1571 before = before_new;
1572 duration = before - after;
1573 points = points_new;
1576 // before = the end time of the calculation
1577 // after = the start time of the calculation
1578 // duration = the duration of the calculation
1579 // group = the number of source points to aggregate / group together
1580 // method = the method of grouping source points
1581 // points = the number of points to generate
1584 // -------------------------------------------------------------------------
1585 // initialize our result set
1587 RRDR *r = rrdr_create(st, points);
1589 #ifdef NETDATA_INTERNAL_CHECKS
1590 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);
1595 #ifdef NETDATA_INTERNAL_CHECKS
1596 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);
1601 if(absolute_period_requested == 1)
1602 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1604 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1606 // find how many dimensions we have
1607 long dimensions = r->d;
1610 // -------------------------------------------------------------------------
1611 // checks for debugging
1613 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"
1615 , (uint32_t)first_entry_t
1616 , (uint32_t)last_entry_t
1617 , (uint32_t)(last_entry_t - first_entry_t)
1620 , (uint32_t)duration
1626 // -------------------------------------------------------------------------
1627 // temp arrays for keeping values per dimension
1629 calculated_number last_values[dimensions]; // keep the last value of each dimension
1630 calculated_number group_values[dimensions]; // keep sums when grouping
1631 long group_counts[dimensions]; // keep the number of values added to group_values
1632 uint8_t group_options[dimensions];
1633 uint8_t found_non_zero[dimensions];
1639 rrdset_check_rdlock(st);
1640 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1642 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1643 group_counts[c] = 0;
1644 group_options[c] = 0;
1645 found_non_zero[c] = 0;
1649 // -------------------------------------------------------------------------
1652 time_t now = rrdset_slot2time(st, start_at_slot),
1653 dt = st->update_every,
1656 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"
1668 r->update_every = group * st->update_every;
1672 //info("RRD2RRDR(): %s: STARTING", st->id);
1674 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1675 for(; !stop_now ; now -= dt, slot--, counter++) {
1676 if(unlikely(slot < 0)) slot = st->entries - 1;
1677 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1679 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1686 , (group_count + 1 == group)?"PRINT":" - "
1687 , (now >= after && now <= before)?"RANGE":" - "
1690 // make sure we return data in the proper time range
1691 if(unlikely(now > before)) continue;
1692 if(unlikely(now < after)) break;
1694 if(unlikely(group_count == 0)) {
1695 group_start_t = now;
1699 if(unlikely(group_count == group)) {
1700 if(unlikely(added >= points)) break;
1704 // do the calculations
1705 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1706 storage_number n = rd->values[slot];
1707 if(unlikely(!does_storage_number_exist(n))) continue;
1711 calculated_number value = unpack_storage_number(n);
1712 if(likely(value != 0.0)) {
1713 group_options[c] |= RRDR_NONZERO;
1714 found_non_zero[c] = 1;
1717 if(unlikely(did_storage_number_reset(n)))
1718 group_options[c] |= RRDR_RESET;
1720 switch(group_method) {
1722 if(unlikely(isnan(group_values[c])) ||
1723 fabsl(value) < fabsl(group_values[c]))
1724 group_values[c] = value;
1728 if(unlikely(isnan(group_values[c])) ||
1729 fabsl(value) > fabsl(group_values[c]))
1730 group_values[c] = value;
1736 case GROUP_UNDEFINED:
1737 group_values[c] += value;
1740 case GROUP_INCREMENTAL_SUM:
1741 if(unlikely(slot == start_at_slot))
1742 last_values[c] = value;
1744 group_values[c] += last_values[c] - value;
1745 last_values[c] = value;
1751 if(unlikely(add_this)) {
1752 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1756 calculated_number *cn = rrdr_line_values(r);
1757 uint8_t *co = rrdr_line_options(r);
1759 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1761 // update the dimension options
1762 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1764 // store the specific point options
1765 co[c] = group_options[c];
1768 if(unlikely(group_counts[c] == 0)) {
1770 co[c] |= RRDR_EMPTY;
1771 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1774 switch(group_method) {
1777 if(unlikely(isnan(group_values[c])))
1780 cn[c] = group_values[c];
1781 group_values[c] = NAN;
1786 case GROUP_INCREMENTAL_SUM:
1787 cn[c] = group_values[c];
1788 group_values[c] = 0;
1793 case GROUP_UNDEFINED:
1794 cn[c] = group_values[c] / group_counts[c];
1795 group_values[c] = 0;
1799 if(cn[c] < r->min) r->min = cn[c];
1800 if(cn[c] > r->max) r->max = cn[c];
1803 // reset for the next loop
1804 group_counts[c] = 0;
1805 group_options[c] = 0;
1815 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1816 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1820 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)
1822 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1824 if(value_is_null) *value_is_null = 1;
1828 if(rrdr_rows(r) == 0) {
1831 if(db_after) *db_after = 0;
1832 if(db_before) *db_before = 0;
1833 if(value_is_null) *value_is_null = 1;
1838 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1839 buffer_no_cacheable(wb);
1840 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1841 buffer_cacheable(wb);
1843 options = rrdr_check_options(r, options, dimensions);
1846 rrdr_disable_not_selected_dimensions(r, options, dimensions);
1848 if(db_after) *db_after = r->after;
1849 if(db_before) *db_before = r->before;
1851 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1852 *n = rrdr2value(r, i, options, value_is_null);
1858 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)
1860 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1862 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1866 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1867 buffer_no_cacheable(wb);
1868 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1869 buffer_cacheable(wb);
1871 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1874 rrdr_disable_not_selected_dimensions(r, options, buffer_tostring(dimensions));
1876 if(latest_timestamp && rrdr_rows(r) > 0)
1877 *latest_timestamp = r->before;
1880 case DATASOURCE_SSV:
1881 if(options & RRDR_OPTION_JSON_WRAP) {
1882 wb->contenttype = CT_APPLICATION_JSON;
1883 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1884 rrdr2ssv(r, wb, options, "", " ", "");
1885 rrdr_json_wrapper_end(r, wb, format, options, 1);
1888 wb->contenttype = CT_TEXT_PLAIN;
1889 rrdr2ssv(r, wb, options, "", " ", "");
1893 case DATASOURCE_SSV_COMMA:
1894 if(options & RRDR_OPTION_JSON_WRAP) {
1895 wb->contenttype = CT_APPLICATION_JSON;
1896 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1897 rrdr2ssv(r, wb, options, "", ",", "");
1898 rrdr_json_wrapper_end(r, wb, format, options, 1);
1901 wb->contenttype = CT_TEXT_PLAIN;
1902 rrdr2ssv(r, wb, options, "", ",", "");
1906 case DATASOURCE_JS_ARRAY:
1907 if(options & RRDR_OPTION_JSON_WRAP) {
1908 wb->contenttype = CT_APPLICATION_JSON;
1909 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1910 rrdr2ssv(r, wb, options, "[", ",", "]");
1911 rrdr_json_wrapper_end(r, wb, format, options, 0);
1914 wb->contenttype = CT_APPLICATION_JSON;
1915 rrdr2ssv(r, wb, options, "[", ",", "]");
1919 case DATASOURCE_CSV:
1920 if(options & RRDR_OPTION_JSON_WRAP) {
1921 wb->contenttype = CT_APPLICATION_JSON;
1922 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1923 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1924 rrdr_json_wrapper_end(r, wb, format, options, 1);
1927 wb->contenttype = CT_TEXT_PLAIN;
1928 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1932 case DATASOURCE_CSV_JSON_ARRAY:
1933 wb->contenttype = CT_APPLICATION_JSON;
1934 if(options & RRDR_OPTION_JSON_WRAP) {
1935 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1936 buffer_strcat(wb, "[\n");
1937 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1938 buffer_strcat(wb, "\n]");
1939 rrdr_json_wrapper_end(r, wb, format, options, 0);
1942 wb->contenttype = CT_TEXT_PLAIN;
1943 buffer_strcat(wb, "[\n");
1944 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1945 buffer_strcat(wb, "\n]");
1949 case DATASOURCE_TSV:
1950 if(options & RRDR_OPTION_JSON_WRAP) {
1951 wb->contenttype = CT_APPLICATION_JSON;
1952 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1953 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1954 rrdr_json_wrapper_end(r, wb, format, options, 1);
1957 wb->contenttype = CT_TEXT_PLAIN;
1958 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1962 case DATASOURCE_HTML:
1963 if(options & RRDR_OPTION_JSON_WRAP) {
1964 wb->contenttype = CT_APPLICATION_JSON;
1965 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1966 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1967 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1968 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1969 rrdr_json_wrapper_end(r, wb, format, options, 1);
1972 wb->contenttype = CT_TEXT_HTML;
1973 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1974 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1975 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1979 case DATASOURCE_DATATABLE_JSONP:
1980 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1982 if(options & RRDR_OPTION_JSON_WRAP)
1983 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1985 rrdr2json(r, wb, options, 1);
1987 if(options & RRDR_OPTION_JSON_WRAP)
1988 rrdr_json_wrapper_end(r, wb, format, options, 0);
1991 case DATASOURCE_DATATABLE_JSON:
1992 wb->contenttype = CT_APPLICATION_JSON;
1994 if(options & RRDR_OPTION_JSON_WRAP)
1995 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1997 rrdr2json(r, wb, options, 1);
1999 if(options & RRDR_OPTION_JSON_WRAP)
2000 rrdr_json_wrapper_end(r, wb, format, options, 0);
2003 case DATASOURCE_JSONP:
2004 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
2005 if(options & RRDR_OPTION_JSON_WRAP)
2006 rrdr_json_wrapper_begin(r, wb, format, options, 0);
2008 rrdr2json(r, wb, options, 0);
2010 if(options & RRDR_OPTION_JSON_WRAP)
2011 rrdr_json_wrapper_end(r, wb, format, options, 0);
2014 case DATASOURCE_JSON:
2016 wb->contenttype = CT_APPLICATION_JSON;
2018 if(options & RRDR_OPTION_JSON_WRAP)
2019 rrdr_json_wrapper_begin(r, wb, format, options, 0);
2021 rrdr2json(r, wb, options, 0);
2023 if(options & RRDR_OPTION_JSON_WRAP)
2024 rrdr_json_wrapper_end(r, wb, format, options, 0);
2032 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)
2038 // -------------------------------------------------------------------------
2039 // switch from JSON to google JSON
2044 case DATASOURCE_DATATABLE_JSON:
2045 case DATASOURCE_DATATABLE_JSONP:
2050 case DATASOURCE_JSON:
2056 // -------------------------------------------------------------------------
2057 // validate the parameters
2059 if(points < 1) points = 1;
2060 if(group < 1) group = 1;
2062 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
2063 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
2067 // our return value (the last timestamp printed)
2068 // this is required to detect re-transmit in google JSONP
2069 time_t last_timestamp = 0;
2072 // -------------------------------------------------------------------------
2073 // find how many dimensions we have
2077 rrddim_foreach_read(rd, st) dimensions++;
2080 buffer_strcat(wb, "No dimensions yet.");
2085 // -------------------------------------------------------------------------
2086 // prepare various strings, to speed up the loop
2088 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);
2089 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
2090 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
2091 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
2092 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
2093 char post_value[21]; strcpy(post_value, "}");
2096 // -------------------------------------------------------------------------
2097 // checks for debugging
2099 if(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)) {
2100 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"
2102 , rrdset_first_entry_t(st)
2103 , rrdset_last_entry_t(st)
2104 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
2113 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
2115 if((before - after) > st->entries * st->update_every)
2116 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);
2120 // -------------------------------------------------------------------------
2121 // temp arrays for keeping values per dimension
2123 calculated_number group_values[dimensions]; // keep sums when grouping
2124 int print_hidden[dimensions]; // keep hidden flags
2125 int found_non_zero[dimensions];
2126 int found_non_existing[dimensions];
2129 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2130 group_values[c] = 0;
2131 print_hidden[c] = rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?1:0;
2132 found_non_zero[c] = 0;
2133 found_non_existing[c] = 0;
2137 // 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);
2138 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
2139 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
2141 // -------------------------------------------------------------------------
2142 // remove dimensions that contain only zeros
2145 if(only_non_zero) max_loop = 2;
2147 for(; max_loop ; max_loop--) {
2149 // -------------------------------------------------------------------------
2150 // print the JSON header
2152 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
2153 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);
2154 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);
2155 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);
2157 // print the header for each dimension
2158 // and update the print_hidden array for the dimensions that should be hidden
2160 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2161 if(!print_hidden[c]) {
2163 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);
2167 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);
2170 // print the begin of row data
2171 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2174 // -------------------------------------------------------------------------
2177 int annotate_reset = 0;
2178 int annotation_count = 0;
2180 long t = rrdset_time2slot(st, before),
2181 stop_at_t = rrdset_time2slot(st, after),
2186 time_t now = rrdset_slot2time(st, t),
2187 dt = st->update_every;
2189 long count = 0, printed = 0, group_count = 0;
2192 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
2193 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"
2200 , (uint32_t)rrdset_first_entry_t(st)
2201 , (uint32_t)rrdset_last_entry_t(st)
2207 for(; !stop_now ; now -= dt, t--, counter++) {
2208 if(t < 0) t = st->entries - 1;
2209 if(t == stop_at_t) stop_now = counter;
2213 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
2214 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
2221 , (group_count + 1 == group)?"PRINT":" - "
2222 , (now >= after && now <= before)?"RANGE":" - "
2226 // make sure we return data in the proper time range
2227 if(now > before) continue;
2228 if(now < after) break;
2230 //if(rrdset_slot2time(st, t) != now)
2231 // 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));
2236 // check if we have to print this now
2237 if(group_count == group) {
2238 if(printed >= points) {
2239 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2243 // generate the local date time
2244 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
2245 if(!tm) { error("localtime() failed."); continue; }
2246 if(now > last_timestamp) last_timestamp = now;
2248 if(printed) buffer_strcat(wb, "]},\n");
2249 buffer_strcat(wb, pre_date);
2250 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2251 buffer_strcat(wb, post_date);
2256 // do the calculations
2257 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2258 storage_number n = rd->values[t];
2259 calculated_number value = unpack_storage_number(n);
2261 if(!does_storage_number_exist(n)) {
2263 found_non_existing[c]++;
2265 if(did_storage_number_reset(n)) annotate_reset = 1;
2267 switch(group_method) {
2269 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2273 group_values[c] += value;
2278 group_values[c] += value;
2279 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2285 if(annotate_reset) {
2287 buffer_strcat(wb, overflow_annotation);
2291 buffer_strcat(wb, normal_annotation);
2294 for(c = 0 ; c < dimensions ; c++) {
2295 if(found_non_existing[c] == group_count) {
2296 // all entries are non-existing
2298 buffer_strcat(wb, pre_value);
2299 buffer_strcat(wb, "null");
2300 buffer_strcat(wb, post_value);
2302 else if(!print_hidden[c]) {
2304 buffer_strcat(wb, pre_value);
2305 buffer_rrd_value(wb, group_values[c]);
2306 buffer_strcat(wb, post_value);
2308 if(group_values[c]) found_non_zero[c]++;
2311 // reset them for the next loop
2312 group_values[c] = 0;
2313 found_non_existing[c] = 0;
2316 // if all dimensions are hidden, print a null
2318 buffer_strcat(wb, pre_value);
2319 buffer_strcat(wb, "null");
2320 buffer_strcat(wb, post_value);
2328 if(printed) buffer_strcat(wb, "]}");
2329 buffer_strcat(wb, "\n ]\n}\n");
2331 if(only_non_zero && max_loop > 1) {
2333 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2334 group_values[c] = 0;
2335 found_non_existing[c] = 0;
2337 if(!print_hidden[c] && !found_non_zero[c]) {
2339 print_hidden[c] = 1;
2343 if(changed) buffer_flush(wb);
2350 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %zu bytes", st->name, wb->len);
2353 return last_timestamp;