3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
5 pthread_rwlock_rdlock(&st->rwlock);
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 , st->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(rd->flags & 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);
74 pthread_rwlock_unlock(&st->rwlock);
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(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"
97 , rrd_default_history_entries
100 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
101 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
102 if(st->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 = localhost.alarms; rc ; rc = rc->next) {
117 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
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(BUFFER *wb)
154 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
156 char host[PROMETHEUS_ELEMENT_MAX + 1];
157 prometheus_name_copy(host, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
161 for(st = localhost.rrdset_root; st ; st = st->next) {
162 char chart[PROMETHEUS_ELEMENT_MAX + 1];
163 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
165 buffer_strcat(wb, "\n");
166 if(st->enabled && st->dimensions) {
167 pthread_rwlock_rdlock(&st->rwlock);
169 // for each dimension
171 for(rd = st->dimensions; rd ; rd = rd->next) {
173 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
174 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
176 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
178 switch(rd->algorithm) {
179 case RRDDIM_INCREMENTAL:
180 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
181 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
185 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
189 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
190 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
191 // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
193 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
194 chart, dimension, host, rd->last_collected_value,
195 (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
200 pthread_rwlock_unlock(&st->rwlock);
204 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
207 // ----------------------------------------------------------------------------
209 // /api/v1/allmetrics?format=bash
211 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
214 for(n = 0; *s && n < usable ; d++, s++, n++) {
215 register char c = *s;
217 if(unlikely(!isalnum(c))) *d = '_';
218 else *d = (char)toupper(c);
225 #define SHELL_ELEMENT_MAX 100
227 void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
229 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
231 char host[SHELL_ELEMENT_MAX + 1];
232 shell_name_copy(host, config_get("global", "hostname", "localhost"), SHELL_ELEMENT_MAX);
236 for(st = localhost.rrdset_root; st ; st = st->next) {
237 calculated_number total = 0.0;
238 char chart[SHELL_ELEMENT_MAX + 1];
239 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
241 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
242 if(st->enabled && st->dimensions) {
243 pthread_rwlock_rdlock(&st->rwlock);
245 // for each dimension
247 for(rd = st->dimensions; rd ; rd = rd->next) {
249 char dimension[SHELL_ELEMENT_MAX + 1];
250 shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
252 calculated_number n = rd->last_stored_value;
254 if(isnan(n) || isinf(n))
255 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
257 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
259 if(!(rd->flags & RRDDIM_FLAG_HIDDEN)) total += n;
260 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
265 total = roundl(total);
266 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
267 pthread_rwlock_unlock(&st->rwlock);
271 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
274 for(rc = localhost.alarms; rc ;rc = rc->next) {
275 if(!rc->rrdset) continue;
277 char chart[SHELL_ELEMENT_MAX + 1];
278 shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
280 char alarm[SHELL_ELEMENT_MAX + 1];
281 shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
283 calculated_number n = rc->value;
285 if(isnan(n) || isinf(n))
286 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\" # %s\n", chart, alarm, rc->units);
289 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\" # %s\n", chart, alarm, n, rc->units);
292 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
295 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
298 // ----------------------------------------------------------------------------
300 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
302 time_t now = now_realtime_sec();
304 pthread_rwlock_rdlock(&st->rwlock);
308 "\t\t\t\"id\": \"%s\",\n"
309 "\t\t\t\"name\": \"%s\",\n"
310 "\t\t\t\"type\": \"%s\",\n"
311 "\t\t\t\"family\": \"%s\",\n"
312 "\t\t\t\"context\": \"%s\",\n"
313 "\t\t\t\"title\": \"%s\",\n"
314 "\t\t\t\"priority\": %ld,\n"
315 "\t\t\t\"enabled\": %d,\n"
316 "\t\t\t\"units\": \"%s\",\n"
317 "\t\t\t\"url\": \"/data/%s/%s\",\n"
318 "\t\t\t\"chart_type\": \"%s\",\n"
319 "\t\t\t\"counter\": %lu,\n"
320 "\t\t\t\"entries\": %ld,\n"
321 "\t\t\t\"first_entry_t\": %ld,\n"
322 "\t\t\t\"last_entry\": %lu,\n"
323 "\t\t\t\"last_entry_t\": %ld,\n"
324 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
325 "\t\t\t\"update_every\": %d,\n"
326 "\t\t\t\"isdetail\": %d,\n"
327 "\t\t\t\"usec_since_last_update\": %llu,\n"
328 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
329 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
330 "\t\t\t\"dimensions\": [\n"
340 , st->name, options?options:""
341 , rrdset_type_name(st->chart_type)
344 , rrdset_first_entry_t(st)
345 , rrdset_last_slot(st)
346 , rrdset_last_entry_t(st)
347 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
350 , st->usec_since_last_update
351 , st->collected_total
352 , st->last_collected_total
355 unsigned long memory = st->memsize;
358 for(rd = st->dimensions; rd ; rd = rd->next) {
360 memory += rd->memsize;
364 "\t\t\t\t\t\"id\": \"%s\",\n"
365 "\t\t\t\t\t\"name\": \"%s\",\n"
366 "\t\t\t\t\t\"entries\": %ld,\n"
367 "\t\t\t\t\t\"isHidden\": %d,\n"
368 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
369 "\t\t\t\t\t\"multiplier\": %ld,\n"
370 "\t\t\t\t\t\"divisor\": %ld,\n"
371 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
372 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
373 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
374 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
375 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
376 "\t\t\t\t\t\"memory\": %lu\n"
381 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
382 , rrddim_algorithm_name(rd->algorithm)
385 , rd->last_collected_time.tv_sec
386 , rd->collected_value
387 , rd->calculated_value
388 , rd->last_collected_value
389 , rd->last_calculated_value
397 "\t\t\t\"memory\" : %lu\n"
402 pthread_rwlock_unlock(&st->rwlock);
406 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
407 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
409 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
411 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
412 rrd_stats_one_json(st, options, wb);
413 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
416 void rrd_stats_all_json(BUFFER *wb)
418 unsigned long memory = 0;
422 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
424 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
425 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
426 if(st->enabled && st->dimensions) {
427 if(c) buffer_strcat(wb, ",\n");
428 memory += rrd_stats_one_json(st, NULL, wb);
432 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
434 buffer_sprintf(wb, "\n\t],\n"
435 "\t\"hostname\": \"%s\",\n"
436 "\t\"update_every\": %d,\n"
437 "\t\"history\": %d,\n"
438 "\t\"memory\": %lu\n"
442 , rrd_default_history_entries
449 // ----------------------------------------------------------------------------
451 // RRDR dimension options
452 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
453 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
454 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
455 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
456 #define RRDR_SELECTED 0x10 // the dimension is selected
458 // RRDR result options
459 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
460 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
462 typedef struct rrdresult {
463 RRDSET *st; // the chart this result refers to
465 uint32_t result_options; // RRDR_RESULT_OPTION_*
467 int d; // the number of dimensions
468 long n; // the number of values in the arrays
469 long rows; // the number of rows used
471 uint8_t *od; // the options for the dimensions
473 time_t *t; // array of n timestamps
474 calculated_number *v; // array n x d values
475 uint8_t *o; // array n x d options
477 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
479 long group; // how many collected values were grouped for each row
480 int update_every; // what is the suggested update frequency in seconds
482 calculated_number min;
483 calculated_number max;
488 int has_st_lock; // if st is read locked by us
491 #define rrdr_rows(r) ((r)->rows)
494 static void rrdr_dump(RRDR *r)
499 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
501 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
502 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
505 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
506 , (r->od[c] & RRDR_RESET)?"RESET ":""
507 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
508 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
513 fprintf(stderr, "RRDR does not have any values in it.\n");
517 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
519 // for each line in the array
520 for(i = 0; i < r->rows ;i++) {
521 calculated_number *cn = &r->v[ i * r->d ];
522 uint8_t *co = &r->o[ i * r->d ];
524 // print the id and the timestamp of the line
525 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
527 // for each dimension
528 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
529 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
530 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
532 if(co[c] & RRDR_EMPTY)
533 fprintf(stderr, "null ");
535 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
537 , (co[c] & RRDR_EMPTY)?"E":" "
538 , (co[c] & RRDR_RESET)?"R":" "
539 , (co[c] & RRDR_HIDDEN)?"H":" "
540 , (co[c] & RRDR_NONZERO)?"N":" "
544 fprintf(stderr, "\n");
549 void rrdr_disable_not_selected_dimensions(RRDR *r, uint32_t options, const char *dims)
551 if(unlikely(!dims || !*dims)) return;
553 char b[strlen(dims) + 1];
557 long c, dims_selected = 0, dims_not_hidden_not_zero = 0;
560 // disable all of them
561 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
562 r->od[c] |= RRDR_HIDDEN;
564 while(o && *o && (tok = mystrsep(&o, ",|"))) {
567 uint32_t hash = simple_hash(tok);
569 // find it and enable it
570 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
571 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
573 if(likely(r->od[c] & RRDR_HIDDEN)) {
574 r->od[c] |= RRDR_SELECTED;
575 r->od[c] &= ~RRDR_HIDDEN;
579 // since the user needs this dimension
580 // make it appear as NONZERO, to return it
581 // even if the dimension has only zeros
582 // unless option non_zero is set
583 if(likely(!(options & RRDR_OPTION_NONZERO)))
584 r->od[c] |= RRDR_NONZERO;
586 // count the visible dimensions
587 if(likely(r->od[c] & RRDR_NONZERO))
588 dims_not_hidden_not_zero++;
593 // check if all dimensions are hidden
594 if(unlikely(!dims_not_hidden_not_zero && dims_selected)) {
595 // there are a few selected dimensions
596 // but they are all zero
597 // enable the selected ones
598 // to avoid returning an empty chart
599 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
600 if(unlikely(r->od[c] & RRDR_SELECTED))
601 r->od[c] |= RRDR_NONZERO;
605 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
608 case DATASOURCE_JSON:
609 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
612 case DATASOURCE_DATATABLE_JSON:
613 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
616 case DATASOURCE_DATATABLE_JSONP:
617 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
620 case DATASOURCE_JSONP:
621 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
625 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
629 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
633 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
636 case DATASOURCE_HTML:
637 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
640 case DATASOURCE_JS_ARRAY:
641 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
644 case DATASOURCE_SSV_COMMA:
645 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
649 buffer_strcat(wb, "unknown");
654 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
658 if(options & RRDR_OPTION_NONZERO) {
661 // commented due to #1514
663 //if(dims && *dims) {
664 // the caller wants specific dimensions
665 // disable NONZERO option
666 // to make sure we don't accidentally prevent
667 // the specific dimensions from being returned
671 // find how many dimensions are not zero
674 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
675 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
676 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
681 // if with nonzero we get i = 0 (no dimensions will be returned)
682 // disable nonzero to show all dimensions
683 if(!i) options &= ~RRDR_OPTION_NONZERO;
689 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
691 long rows = rrdr_rows(r);
695 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
696 char kq[2] = "", // key quote
697 sq[2] = ""; // string quote
699 if( options & RRDR_OPTION_GOOGLE_JSON ) {
708 buffer_sprintf(wb, "{\n"
711 " %sname%s: %s%s%s,\n"
712 " %sview_update_every%s: %d,\n"
713 " %supdate_every%s: %d,\n"
714 " %sfirst_entry%s: %u,\n"
715 " %slast_entry%s: %u,\n"
718 " %sdimension_names%s: ["
720 , kq, kq, sq, r->st->id, sq
721 , kq, kq, sq, r->st->name, sq
722 , kq, kq, r->update_every
723 , kq, kq, r->st->update_every
724 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
725 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
726 , kq, kq, (uint32_t)r->before
727 , kq, kq, (uint32_t)r->after
730 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
731 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
732 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
734 if(i) buffer_strcat(wb, ", ");
735 buffer_strcat(wb, sq);
736 buffer_strcat(wb, rd->name);
737 buffer_strcat(wb, sq);
741 #ifdef NETDATA_INTERNAL_CHECKS
742 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
745 buffer_strcat(wb, sq);
746 buffer_strcat(wb, "no data");
747 buffer_strcat(wb, sq);
750 buffer_sprintf(wb, "],\n"
751 " %sdimension_ids%s: ["
754 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
755 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
756 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
758 if(i) buffer_strcat(wb, ", ");
759 buffer_strcat(wb, sq);
760 buffer_strcat(wb, rd->id);
761 buffer_strcat(wb, sq);
766 buffer_strcat(wb, sq);
767 buffer_strcat(wb, "no data");
768 buffer_strcat(wb, sq);
771 buffer_sprintf(wb, "],\n"
772 " %slatest_values%s: ["
775 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
776 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
777 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
779 if(i) buffer_strcat(wb, ", ");
782 storage_number n = rd->values[rrdset_last_slot(r->st)];
784 if(!does_storage_number_exist(n))
785 buffer_strcat(wb, "null");
787 buffer_rrd_value(wb, unpack_storage_number(n));
791 buffer_strcat(wb, "null");
794 buffer_sprintf(wb, "],\n"
795 " %sview_latest_values%s: ["
800 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
801 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
802 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
804 if(i) buffer_strcat(wb, ", ");
807 calculated_number *cn = &r->v[ (0) * r->d ];
808 uint8_t *co = &r->o[ (0) * r->d ];
810 if(co[c] & RRDR_EMPTY)
811 buffer_strcat(wb, "null");
813 buffer_rrd_value(wb, cn[c]);
818 buffer_strcat(wb, "null");
821 buffer_sprintf(wb, "],\n"
822 " %sdimensions%s: %ld,\n"
823 " %spoints%s: %ld,\n"
830 rrdr_buffer_print_format(wb, format);
832 buffer_sprintf(wb, "%s,\n"
838 if(string_value) buffer_strcat(wb, sq);
839 //info("JSONWRAPPER(): %s: END", r->st->id);
842 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
846 char kq[2] = "", // key quote
847 sq[2] = ""; // string quote
849 if( options & RRDR_OPTION_GOOGLE_JSON ) {
858 if(string_value) buffer_strcat(wb, sq);
860 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
861 buffer_rrd_value(wb, r->min);
862 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
863 buffer_rrd_value(wb, r->max);
864 buffer_strcat(wb, "\n}\n");
867 #define JSON_DATES_JS 1
868 #define JSON_DATES_TIMESTAMP 2
870 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
872 //info("RRD2JSON(): %s: BEGIN", r->st->id);
873 int row_annotations = 0, dates, dates_with_new = 0;
874 char kq[2] = "", // key quote
875 sq[2] = "", // string quote
876 pre_label[101] = "", // before each label
877 post_label[101] = "", // after each label
878 pre_date[101] = "", // the beginning of line, to the date
879 post_date[101] = "", // closing the date
880 pre_value[101] = "", // before each value
881 post_value[101] = "", // after each value
882 post_line[101] = "", // at the end of each row
883 normal_annotation[201] = "", // default row annotation
884 overflow_annotation[201] = "", // overflow row annotation
885 data_begin[101] = "", // between labels and values
886 finish[101] = ""; // at the end of everything
889 dates = JSON_DATES_JS;
890 if( options & RRDR_OPTION_GOOGLE_JSON ) {
899 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
900 snprintfz(post_date, 100, "%s}", sq);
901 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
902 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
903 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
904 strcpy(post_value, "}");
905 strcpy(post_line, "]}");
906 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
907 strcpy(finish, "\n ]\n}");
909 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);
910 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
912 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
913 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);
914 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);
915 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);
917 // remove the valueobjects flag
918 // google wants its own keys
919 if(options & RRDR_OPTION_OBJECTSROWS)
920 options &= ~RRDR_OPTION_OBJECTSROWS;
925 if(options & RRDR_OPTION_GOOGLE_JSON) {
926 dates = JSON_DATES_JS;
930 dates = JSON_DATES_TIMESTAMP;
933 if( options & RRDR_OPTION_OBJECTSROWS )
934 strcpy(pre_date, " { ");
936 strcpy(pre_date, " [ ");
937 strcpy(pre_label, ", \"");
938 strcpy(post_label, "\"");
939 strcpy(pre_value, ", ");
940 if( options & RRDR_OPTION_OBJECTSROWS )
941 strcpy(post_line, "}");
943 strcpy(post_line, "]");
944 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
945 strcpy(finish, "\n ]\n}");
947 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
948 buffer_sprintf(wb, "%stime%s", sq, sq);
951 // -------------------------------------------------------------------------
952 // print the JSON header
957 // print the header lines
958 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
959 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
960 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
962 buffer_strcat(wb, pre_label);
963 buffer_strcat(wb, rd->name);
964 buffer_strcat(wb, post_label);
968 buffer_strcat(wb, pre_label);
969 buffer_strcat(wb, "no data");
970 buffer_strcat(wb, post_label);
973 // print the begin of row data
974 buffer_strcat(wb, data_begin);
976 // if all dimensions are hidden, print a null
978 buffer_strcat(wb, finish);
982 long start = 0, end = rrdr_rows(r), step = 1;
983 if((options & RRDR_OPTION_REVERSED)) {
984 start = rrdr_rows(r) - 1;
989 // for each line in the array
990 calculated_number total = 1;
991 for(i = start; i != end ;i += step) {
992 calculated_number *cn = &r->v[ i * r->d ];
993 uint8_t *co = &r->o[ i * r->d ];
995 time_t now = r->t[i];
997 if(dates == JSON_DATES_JS) {
998 // generate the local date time
999 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1000 if(!tm) { error("localtime_r() failed."); continue; }
1002 if(likely(i != start)) buffer_strcat(wb, ",\n");
1003 buffer_strcat(wb, pre_date);
1005 if( options & RRDR_OPTION_OBJECTSROWS )
1006 buffer_sprintf(wb, "%stime%s: ", kq, kq);
1009 buffer_strcat(wb, "new ");
1011 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1013 buffer_strcat(wb, post_date);
1015 if(row_annotations) {
1016 // google supports one annotation per row
1017 int annotation_found = 0;
1018 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
1019 if(co[c] & RRDR_RESET) {
1020 buffer_strcat(wb, overflow_annotation);
1021 annotation_found = 1;
1025 if(!annotation_found)
1026 buffer_strcat(wb, normal_annotation);
1030 // print the timestamp of the line
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);
1037 buffer_rrd_value(wb, (calculated_number)r->t[i]);
1039 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1041 buffer_strcat(wb, post_date);
1044 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1046 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1047 calculated_number n = cn[c];
1049 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1054 // prevent a division by zero
1055 if(total == 0) total = 1;
1058 // for each dimension
1059 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1060 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1061 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1063 calculated_number n = cn[c];
1065 buffer_strcat(wb, pre_value);
1067 if( options & RRDR_OPTION_OBJECTSROWS )
1068 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1070 if(co[c] & RRDR_EMPTY) {
1071 if(options & RRDR_OPTION_NULL2ZERO)
1072 buffer_strcat(wb, "0");
1074 buffer_strcat(wb, "null");
1077 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1080 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1081 n = n * 100 / total;
1083 buffer_rrd_value(wb, n);
1086 buffer_strcat(wb, post_value);
1089 buffer_strcat(wb, post_line);
1092 buffer_strcat(wb, finish);
1093 //info("RRD2JSON(): %s: END", r->st->id);
1096 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1098 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1102 // print the csv header
1103 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1104 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1105 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1108 buffer_strcat(wb, startline);
1109 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1110 buffer_strcat(wb, "time");
1111 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1113 buffer_strcat(wb, separator);
1114 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1115 buffer_strcat(wb, d->name);
1116 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1119 buffer_strcat(wb, endline);
1122 // no dimensions present
1126 long start = 0, end = rrdr_rows(r), step = 1;
1127 if((options & RRDR_OPTION_REVERSED)) {
1128 start = rrdr_rows(r) - 1;
1133 // for each line in the array
1134 calculated_number total = 1;
1135 for(i = start; i != end ;i += step) {
1136 calculated_number *cn = &r->v[ i * r->d ];
1137 uint8_t *co = &r->o[ i * r->d ];
1139 buffer_strcat(wb, betweenlines);
1140 buffer_strcat(wb, startline);
1142 time_t now = r->t[i];
1144 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1145 // print the timestamp of the line
1146 buffer_rrd_value(wb, (calculated_number)now);
1148 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1151 // generate the local date time
1152 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1153 if(!tm) { error("localtime() failed."); continue; }
1154 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1157 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1159 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1160 calculated_number n = cn[c];
1162 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1167 // prevent a division by zero
1168 if(total == 0) total = 1;
1171 // for each dimension
1172 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1173 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1174 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1176 buffer_strcat(wb, separator);
1178 calculated_number n = cn[c];
1180 if(co[c] & RRDR_EMPTY) {
1181 if(options & RRDR_OPTION_NULL2ZERO)
1182 buffer_strcat(wb, "0");
1184 buffer_strcat(wb, "null");
1187 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1190 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1191 n = n * 100 / total;
1193 buffer_rrd_value(wb, n);
1197 buffer_strcat(wb, endline);
1199 //info("RRD2CSV(): %s: END", r->st->id);
1202 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1206 calculated_number *cn = &r->v[ i * r->d ];
1207 uint8_t *co = &r->o[ i * r->d ];
1209 calculated_number sum = 0, min = 0, max = 0, v;
1210 int all_null = 1, init = 1;
1212 calculated_number total = 1;
1213 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1215 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1216 calculated_number n = cn[c];
1218 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1223 // prevent a division by zero
1224 if(total == 0) total = 1;
1227 // for each dimension
1228 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1229 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1230 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1232 calculated_number n = cn[c];
1234 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1237 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1238 n = n * 100 / total;
1240 if(unlikely(init)) {
1252 if(likely(!(co[c] & RRDR_EMPTY))) {
1257 if(n < min) min = n;
1258 if(n > max) max = n;
1261 if(unlikely(all_null)) {
1262 if(likely(all_values_are_null))
1263 *all_values_are_null = 1;
1267 if(likely(all_values_are_null))
1268 *all_values_are_null = 0;
1271 if(options & RRDR_OPTION_MIN2MAX)
1279 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1281 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1284 buffer_strcat(wb, prefix);
1285 long start = 0, end = rrdr_rows(r), step = 1;
1286 if((options & RRDR_OPTION_REVERSED)) {
1287 start = rrdr_rows(r) - 1;
1292 // for each line in the array
1293 for(i = start; i != end ;i += step) {
1294 int all_values_are_null = 0;
1295 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1297 if(likely(i != start)) {
1298 if(r->min > v) r->min = v;
1299 if(r->max < v) r->max = v;
1306 if(likely(i != start))
1307 buffer_strcat(wb, separator);
1309 if(all_values_are_null) {
1310 if(options & RRDR_OPTION_NULL2ZERO)
1311 buffer_strcat(wb, "0");
1313 buffer_strcat(wb, "null");
1316 buffer_rrd_value(wb, v);
1318 buffer_strcat(wb, suffix);
1319 //info("RRD2SSV(): %s: END", r->st->id);
1322 inline static calculated_number *rrdr_line_values(RRDR *r)
1324 return &r->v[ r->c * r->d ];
1327 inline static uint8_t *rrdr_line_options(RRDR *r)
1329 return &r->o[ r->c * r->d ];
1332 inline static int rrdr_line_init(RRDR *r, time_t t)
1336 if(unlikely(r->c >= r->n)) {
1337 error("requested to step above RRDR size for chart %s", r->st->name);
1347 inline static void rrdr_lock_rrdset(RRDR *r) {
1349 error("NULL value given!");
1353 pthread_rwlock_rdlock(&r->st->rwlock);
1357 inline static void rrdr_unlock_rrdset(RRDR *r) {
1359 error("NULL value given!");
1363 if(likely(r->has_st_lock)) {
1364 pthread_rwlock_unlock(&r->st->rwlock);
1369 inline static void rrdr_free(RRDR *r)
1372 error("NULL value given!");
1376 rrdr_unlock_rrdset(r);
1384 static inline void rrdr_done(RRDR *r)
1390 static RRDR *rrdr_create(RRDSET *st, long n)
1393 error("NULL value given!");
1397 RRDR *r = callocz(1, sizeof(RRDR));
1400 rrdr_lock_rrdset(r);
1403 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1407 r->t = mallocz(n * sizeof(time_t));
1408 r->v = mallocz(n * r->d * sizeof(calculated_number));
1409 r->o = mallocz(n * r->d * sizeof(uint8_t));
1410 r->od = mallocz(r->d * sizeof(uint8_t));
1412 // set the hidden flag on hidden dimensions
1414 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1415 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1421 r->update_every = 1;
1426 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1428 int debug = st->debug;
1429 int absolute_period_requested = -1;
1431 time_t first_entry_t = rrdset_first_entry_t(st);
1432 time_t last_entry_t = rrdset_last_entry_t(st);
1434 if(before == 0 && after == 0) {
1435 // dump the all the data
1436 before = last_entry_t;
1437 after = first_entry_t;
1438 absolute_period_requested = 0;
1441 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1442 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1443 if(abs(before) % st->update_every) {
1444 // make sure it is multiple of st->update_every
1445 if(before < 0) before = before - st->update_every - before % st->update_every;
1446 else before = before + st->update_every - before % st->update_every;
1448 if(before > 0) before = first_entry_t + before;
1449 else before = last_entry_t + before;
1450 absolute_period_requested = 0;
1453 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1454 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1455 if(after == 0) after = -st->update_every;
1456 if(abs(after) % st->update_every) {
1457 // make sure it is multiple of st->update_every
1458 if(after < 0) after = after - st->update_every - after % st->update_every;
1459 else after = after + st->update_every - after % st->update_every;
1461 after = before + after;
1462 absolute_period_requested = 0;
1465 if(absolute_period_requested == -1)
1466 absolute_period_requested = 1;
1468 // make sure they are within our timeframe
1469 if(before > last_entry_t) before = last_entry_t;
1470 if(before < first_entry_t) before = first_entry_t;
1472 if(after > last_entry_t) after = last_entry_t;
1473 if(after < first_entry_t) after = first_entry_t;
1475 // check if they are upside down
1476 if(after > before) {
1477 time_t tmp = before;
1482 // the duration of the chart
1483 time_t duration = before - after;
1484 long available_points = duration / st->update_every;
1486 if(duration <= 0 || available_points <= 0)
1487 return rrdr_create(st, 1);
1489 // check the wanted points
1490 if(points < 0) points = -points;
1491 if(points > available_points) points = available_points;
1492 if(points == 0) points = available_points;
1494 // calculate proper grouping of source data
1495 long group = available_points / points;
1496 if(group <= 0) group = 1;
1498 // round group to the closest integer
1499 if(available_points % points > points / 2) group++;
1501 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1502 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1503 long points_new = (before_new - after_new) / st->update_every / group;
1505 // find the starting and ending slots in our round robin db
1506 long start_at_slot = rrdset_time2slot(st, before_new),
1507 stop_at_slot = rrdset_time2slot(st, after_new);
1509 #ifdef NETDATA_INTERNAL_CHECKS
1510 if(after_new < first_entry_t) {
1511 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1513 if(after_new > last_entry_t) {
1514 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1516 if(before_new < first_entry_t) {
1517 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1519 if(before_new > last_entry_t) {
1520 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1522 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1523 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1525 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1526 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1528 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1529 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1533 //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);
1536 before = before_new;
1537 duration = before - after;
1538 points = points_new;
1541 // before = the end time of the calculation
1542 // after = the start time of the calculation
1543 // duration = the duration of the calculation
1544 // group = the number of source points to aggregate / group together
1545 // method = the method of grouping source points
1546 // points = the number of points to generate
1549 // -------------------------------------------------------------------------
1550 // initialize our result set
1552 RRDR *r = rrdr_create(st, points);
1554 #ifdef NETDATA_INTERNAL_CHECKS
1555 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);
1560 #ifdef NETDATA_INTERNAL_CHECKS
1561 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);
1566 if(absolute_period_requested == 1)
1567 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1569 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1571 // find how many dimensions we have
1572 long dimensions = r->d;
1575 // -------------------------------------------------------------------------
1576 // checks for debugging
1578 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"
1580 , (uint32_t)first_entry_t
1581 , (uint32_t)last_entry_t
1582 , (uint32_t)(last_entry_t - first_entry_t)
1585 , (uint32_t)duration
1591 // -------------------------------------------------------------------------
1592 // temp arrays for keeping values per dimension
1594 calculated_number last_values[dimensions]; // keep the last value of each dimension
1595 calculated_number group_values[dimensions]; // keep sums when grouping
1596 long group_counts[dimensions]; // keep the number of values added to group_values
1597 uint8_t group_options[dimensions];
1598 uint8_t found_non_zero[dimensions];
1604 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1606 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1607 group_counts[c] = 0;
1608 group_options[c] = 0;
1609 found_non_zero[c] = 0;
1613 // -------------------------------------------------------------------------
1616 time_t now = rrdset_slot2time(st, start_at_slot),
1617 dt = st->update_every,
1620 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"
1632 r->update_every = group * st->update_every;
1636 //info("RRD2RRDR(): %s: STARTING", st->id);
1638 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1639 for(; !stop_now ; now -= dt, slot--, counter++) {
1640 if(unlikely(slot < 0)) slot = st->entries - 1;
1641 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1643 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1650 , (group_count + 1 == group)?"PRINT":" - "
1651 , (now >= after && now <= before)?"RANGE":" - "
1654 // make sure we return data in the proper time range
1655 if(unlikely(now > before)) continue;
1656 if(unlikely(now < after)) break;
1658 if(unlikely(group_count == 0)) {
1659 group_start_t = now;
1663 if(unlikely(group_count == group)) {
1664 if(unlikely(added >= points)) break;
1668 // do the calculations
1669 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1670 storage_number n = rd->values[slot];
1671 if(unlikely(!does_storage_number_exist(n))) continue;
1675 calculated_number value = unpack_storage_number(n);
1676 if(likely(value != 0.0)) {
1677 group_options[c] |= RRDR_NONZERO;
1678 found_non_zero[c] = 1;
1681 if(unlikely(did_storage_number_reset(n)))
1682 group_options[c] |= RRDR_RESET;
1684 switch(group_method) {
1686 if(unlikely(isnan(group_values[c])) ||
1687 fabsl(value) < fabsl(group_values[c]))
1688 group_values[c] = value;
1692 if(unlikely(isnan(group_values[c])) ||
1693 fabsl(value) > fabsl(group_values[c]))
1694 group_values[c] = value;
1700 case GROUP_UNDEFINED:
1701 group_values[c] += value;
1704 case GROUP_INCREMENTAL_SUM:
1705 if(unlikely(slot == start_at_slot))
1706 last_values[c] = value;
1708 group_values[c] += last_values[c] - value;
1709 last_values[c] = value;
1715 if(unlikely(add_this)) {
1716 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1720 calculated_number *cn = rrdr_line_values(r);
1721 uint8_t *co = rrdr_line_options(r);
1723 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1725 // update the dimension options
1726 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1728 // store the specific point options
1729 co[c] = group_options[c];
1732 if(unlikely(group_counts[c] == 0)) {
1734 co[c] |= RRDR_EMPTY;
1735 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1738 switch(group_method) {
1741 if(unlikely(isnan(group_values[c])))
1744 cn[c] = group_values[c];
1745 group_values[c] = NAN;
1750 case GROUP_INCREMENTAL_SUM:
1751 cn[c] = group_values[c];
1752 group_values[c] = 0;
1757 case GROUP_UNDEFINED:
1758 cn[c] = group_values[c] / group_counts[c];
1759 group_values[c] = 0;
1763 if(cn[c] < r->min) r->min = cn[c];
1764 if(cn[c] > r->max) r->max = cn[c];
1767 // reset for the next loop
1768 group_counts[c] = 0;
1769 group_options[c] = 0;
1779 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1780 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1784 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)
1786 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1788 if(value_is_null) *value_is_null = 1;
1792 if(rrdr_rows(r) == 0) {
1795 if(db_after) *db_after = 0;
1796 if(db_before) *db_before = 0;
1797 if(value_is_null) *value_is_null = 1;
1802 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1803 buffer_no_cacheable(wb);
1804 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1805 buffer_cacheable(wb);
1807 options = rrdr_check_options(r, options, dimensions);
1810 rrdr_disable_not_selected_dimensions(r, options, dimensions);
1812 if(db_after) *db_after = r->after;
1813 if(db_before) *db_before = r->before;
1815 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1816 *n = rrdr2value(r, i, options, value_is_null);
1822 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)
1824 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1826 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1830 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1831 buffer_no_cacheable(wb);
1832 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1833 buffer_cacheable(wb);
1835 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1838 rrdr_disable_not_selected_dimensions(r, options, buffer_tostring(dimensions));
1840 if(latest_timestamp && rrdr_rows(r) > 0)
1841 *latest_timestamp = r->before;
1844 case DATASOURCE_SSV:
1845 if(options & RRDR_OPTION_JSON_WRAP) {
1846 wb->contenttype = CT_APPLICATION_JSON;
1847 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1848 rrdr2ssv(r, wb, options, "", " ", "");
1849 rrdr_json_wrapper_end(r, wb, format, options, 1);
1852 wb->contenttype = CT_TEXT_PLAIN;
1853 rrdr2ssv(r, wb, options, "", " ", "");
1857 case DATASOURCE_SSV_COMMA:
1858 if(options & RRDR_OPTION_JSON_WRAP) {
1859 wb->contenttype = CT_APPLICATION_JSON;
1860 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1861 rrdr2ssv(r, wb, options, "", ",", "");
1862 rrdr_json_wrapper_end(r, wb, format, options, 1);
1865 wb->contenttype = CT_TEXT_PLAIN;
1866 rrdr2ssv(r, wb, options, "", ",", "");
1870 case DATASOURCE_JS_ARRAY:
1871 if(options & RRDR_OPTION_JSON_WRAP) {
1872 wb->contenttype = CT_APPLICATION_JSON;
1873 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1874 rrdr2ssv(r, wb, options, "[", ",", "]");
1875 rrdr_json_wrapper_end(r, wb, format, options, 0);
1878 wb->contenttype = CT_APPLICATION_JSON;
1879 rrdr2ssv(r, wb, options, "[", ",", "]");
1883 case DATASOURCE_CSV:
1884 if(options & RRDR_OPTION_JSON_WRAP) {
1885 wb->contenttype = CT_APPLICATION_JSON;
1886 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1887 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1888 rrdr_json_wrapper_end(r, wb, format, options, 1);
1891 wb->contenttype = CT_TEXT_PLAIN;
1892 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1896 case DATASOURCE_CSV_JSON_ARRAY:
1897 wb->contenttype = CT_APPLICATION_JSON;
1898 if(options & RRDR_OPTION_JSON_WRAP) {
1899 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1900 buffer_strcat(wb, "[\n");
1901 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1902 buffer_strcat(wb, "\n]");
1903 rrdr_json_wrapper_end(r, wb, format, options, 0);
1906 wb->contenttype = CT_TEXT_PLAIN;
1907 buffer_strcat(wb, "[\n");
1908 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1909 buffer_strcat(wb, "\n]");
1913 case DATASOURCE_TSV:
1914 if(options & RRDR_OPTION_JSON_WRAP) {
1915 wb->contenttype = CT_APPLICATION_JSON;
1916 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1917 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1918 rrdr_json_wrapper_end(r, wb, format, options, 1);
1921 wb->contenttype = CT_TEXT_PLAIN;
1922 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1926 case DATASOURCE_HTML:
1927 if(options & RRDR_OPTION_JSON_WRAP) {
1928 wb->contenttype = CT_APPLICATION_JSON;
1929 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1930 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1931 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1932 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1933 rrdr_json_wrapper_end(r, wb, format, options, 1);
1936 wb->contenttype = CT_TEXT_HTML;
1937 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1938 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1939 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1943 case DATASOURCE_DATATABLE_JSONP:
1944 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1946 if(options & RRDR_OPTION_JSON_WRAP)
1947 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1949 rrdr2json(r, wb, options, 1);
1951 if(options & RRDR_OPTION_JSON_WRAP)
1952 rrdr_json_wrapper_end(r, wb, format, options, 0);
1955 case DATASOURCE_DATATABLE_JSON:
1956 wb->contenttype = CT_APPLICATION_JSON;
1958 if(options & RRDR_OPTION_JSON_WRAP)
1959 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1961 rrdr2json(r, wb, options, 1);
1963 if(options & RRDR_OPTION_JSON_WRAP)
1964 rrdr_json_wrapper_end(r, wb, format, options, 0);
1967 case DATASOURCE_JSONP:
1968 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1969 if(options & RRDR_OPTION_JSON_WRAP)
1970 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1972 rrdr2json(r, wb, options, 0);
1974 if(options & RRDR_OPTION_JSON_WRAP)
1975 rrdr_json_wrapper_end(r, wb, format, options, 0);
1978 case DATASOURCE_JSON:
1980 wb->contenttype = CT_APPLICATION_JSON;
1982 if(options & RRDR_OPTION_JSON_WRAP)
1983 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1985 rrdr2json(r, wb, options, 0);
1987 if(options & RRDR_OPTION_JSON_WRAP)
1988 rrdr_json_wrapper_end(r, wb, format, options, 0);
1996 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)
1999 pthread_rwlock_rdlock(&st->rwlock);
2002 // -------------------------------------------------------------------------
2003 // switch from JSON to google JSON
2008 case DATASOURCE_DATATABLE_JSON:
2009 case DATASOURCE_DATATABLE_JSONP:
2014 case DATASOURCE_JSON:
2020 // -------------------------------------------------------------------------
2021 // validate the parameters
2023 if(points < 1) points = 1;
2024 if(group < 1) group = 1;
2026 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
2027 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
2031 // our return value (the last timestamp printed)
2032 // this is required to detect re-transmit in google JSONP
2033 time_t last_timestamp = 0;
2036 // -------------------------------------------------------------------------
2037 // find how many dimensions we have
2041 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
2043 pthread_rwlock_unlock(&st->rwlock);
2044 buffer_strcat(wb, "No dimensions yet.");
2049 // -------------------------------------------------------------------------
2050 // prepare various strings, to speed up the loop
2052 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);
2053 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
2054 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
2055 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
2056 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
2057 char post_value[21]; strcpy(post_value, "}");
2060 // -------------------------------------------------------------------------
2061 // checks for debugging
2064 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"
2066 , rrdset_first_entry_t(st)
2067 , rrdset_last_entry_t(st)
2068 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
2077 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
2079 if((before - after) > st->entries * st->update_every)
2080 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);
2084 // -------------------------------------------------------------------------
2085 // temp arrays for keeping values per dimension
2087 calculated_number group_values[dimensions]; // keep sums when grouping
2088 int print_hidden[dimensions]; // keep hidden flags
2089 int found_non_zero[dimensions];
2090 int found_non_existing[dimensions];
2093 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2094 group_values[c] = 0;
2095 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
2096 found_non_zero[c] = 0;
2097 found_non_existing[c] = 0;
2101 // 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);
2102 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
2103 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
2105 // -------------------------------------------------------------------------
2106 // remove dimensions that contain only zeros
2109 if(only_non_zero) max_loop = 2;
2111 for(; max_loop ; max_loop--) {
2113 // -------------------------------------------------------------------------
2114 // print the JSON header
2116 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
2117 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);
2118 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);
2119 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);
2121 // print the header for each dimension
2122 // and update the print_hidden array for the dimensions that should be hidden
2124 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2125 if(!print_hidden[c]) {
2127 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);
2131 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);
2134 // print the begin of row data
2135 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2138 // -------------------------------------------------------------------------
2141 int annotate_reset = 0;
2142 int annotation_count = 0;
2144 long t = rrdset_time2slot(st, before),
2145 stop_at_t = rrdset_time2slot(st, after),
2150 time_t now = rrdset_slot2time(st, t),
2151 dt = st->update_every;
2153 long count = 0, printed = 0, group_count = 0;
2156 if(st->debug) 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"
2163 , (uint32_t)rrdset_first_entry_t(st)
2164 , (uint32_t)rrdset_last_entry_t(st)
2170 for(; !stop_now ; now -= dt, t--, counter++) {
2171 if(t < 0) t = st->entries - 1;
2172 if(t == stop_at_t) stop_now = counter;
2176 if(st->debug) 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);
2314 pthread_rwlock_unlock(&st->rwlock);
2315 return last_timestamp;