13 #define HOSTNAME_MAX 1024
14 char *hostname = "unknown";
16 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
18 pthread_rwlock_rdlock(&st->rwlock);
22 "\t\t\t\"id\": \"%s\",\n"
23 "\t\t\t\"name\": \"%s\",\n"
24 "\t\t\t\"type\": \"%s\",\n"
25 "\t\t\t\"family\": \"%s\",\n"
26 "\t\t\t\"context\": \"%s\",\n"
27 "\t\t\t\"title\": \"%s\",\n"
28 "\t\t\t\"priority\": %ld,\n"
29 "\t\t\t\"enabled\": %s,\n"
30 "\t\t\t\"units\": \"%s\",\n"
31 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
32 "\t\t\t\"chart_type\": \"%s\",\n"
33 "\t\t\t\"duration\": %ld,\n"
34 "\t\t\t\"first_entry\": %lu,\n"
35 "\t\t\t\"last_entry\": %lu,\n"
36 "\t\t\t\"update_every\": %d,\n"
37 "\t\t\t\"dimensions\": {\n"
45 , st->enabled?"true":"false"
48 , rrdset_type_name(st->chart_type)
49 , st->entries * st->update_every
50 , rrdset_first_entry_t(st)
51 , rrdset_last_entry_t(st)
55 unsigned long memory = st->memsize;
59 for(rd = st->dimensions; rd ; rd = rd->next) {
60 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
62 memory += rd->memsize;
66 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
80 pthread_rwlock_unlock(&st->rwlock);
83 void rrd_stats_api_v1_charts(BUFFER *wb)
88 buffer_sprintf(wb, "{\n"
89 "\t\"hostname\": \"%s\""
90 ",\n\t\"update_every\": %d"
91 ",\n\t\"history\": %d"
95 , rrd_default_history_entries
98 pthread_rwlock_rdlock(&rrdset_root_rwlock);
99 for(st = rrdset_root, c = 0; st ; st = st->next) {
101 if(c) buffer_strcat(wb, ",");
102 buffer_strcat(wb, "\n\t\t\"");
103 buffer_strcat(wb, st->id);
104 buffer_strcat(wb, "\": ");
105 rrd_stats_api_v1_chart(st, wb);
109 pthread_rwlock_unlock(&rrdset_root_rwlock);
111 buffer_strcat(wb, "\n\t}\n}\n");
115 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
117 time_t now = time(NULL);
119 pthread_rwlock_rdlock(&st->rwlock);
123 "\t\t\t\"id\": \"%s\",\n"
124 "\t\t\t\"name\": \"%s\",\n"
125 "\t\t\t\"type\": \"%s\",\n"
126 "\t\t\t\"family\": \"%s\",\n"
127 "\t\t\t\"context\": \"%s\",\n"
128 "\t\t\t\"title\": \"%s\",\n"
129 "\t\t\t\"priority\": %ld,\n"
130 "\t\t\t\"enabled\": %d,\n"
131 "\t\t\t\"units\": \"%s\",\n"
132 "\t\t\t\"url\": \"/data/%s/%s\",\n"
133 "\t\t\t\"chart_type\": \"%s\",\n"
134 "\t\t\t\"counter\": %ld,\n"
135 "\t\t\t\"entries\": %ld,\n"
136 "\t\t\t\"first_entry_t\": %lu,\n"
137 "\t\t\t\"last_entry\": %ld,\n"
138 "\t\t\t\"last_entry_t\": %lu,\n"
139 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
140 "\t\t\t\"update_every\": %d,\n"
141 "\t\t\t\"isdetail\": %d,\n"
142 "\t\t\t\"usec_since_last_update\": %llu,\n"
143 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
144 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
145 "\t\t\t\"dimensions\": [\n"
155 , st->name, options?options:""
156 , rrdset_type_name(st->chart_type)
159 , rrdset_first_entry_t(st)
160 , rrdset_last_slot(st)
161 , rrdset_last_entry_t(st)
162 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
165 , st->usec_since_last_update
166 , st->collected_total
167 , st->last_collected_total
170 unsigned long memory = st->memsize;
173 for(rd = st->dimensions; rd ; rd = rd->next) {
175 memory += rd->memsize;
179 "\t\t\t\t\t\"id\": \"%s\",\n"
180 "\t\t\t\t\t\"name\": \"%s\",\n"
181 "\t\t\t\t\t\"entries\": %ld,\n"
182 "\t\t\t\t\t\"isHidden\": %d,\n"
183 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
184 "\t\t\t\t\t\"multiplier\": %ld,\n"
185 "\t\t\t\t\t\"divisor\": %ld,\n"
186 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
187 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
188 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
189 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
190 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
191 "\t\t\t\t\t\"memory\": %lu\n"
196 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
197 , rrddim_algorithm_name(rd->algorithm)
200 , rd->last_collected_time.tv_sec
201 , rd->collected_value
202 , rd->calculated_value
203 , rd->last_collected_value
204 , rd->last_calculated_value
212 "\t\t\t\"memory\" : %lu\n"
217 pthread_rwlock_unlock(&st->rwlock);
221 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
222 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
224 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
226 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
227 rrd_stats_one_json(st, options, wb);
228 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
231 void rrd_stats_all_json(BUFFER *wb)
233 unsigned long memory = 0;
237 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
239 pthread_rwlock_rdlock(&rrdset_root_rwlock);
240 for(st = rrdset_root, c = 0; st ; st = st->next) {
242 if(c) buffer_strcat(wb, ",\n");
243 memory += rrd_stats_one_json(st, NULL, wb);
247 pthread_rwlock_unlock(&rrdset_root_rwlock);
249 buffer_sprintf(wb, "\n\t],\n"
250 "\t\"hostname\": \"%s\",\n"
251 "\t\"update_every\": %d,\n"
252 "\t\"history\": %d,\n"
253 "\t\"memory\": %lu\n"
257 , rrd_default_history_entries
264 // ----------------------------------------------------------------------------
266 // RRDR dimension options
267 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
268 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
269 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
270 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
272 // RRDR result options
273 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
274 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
276 typedef struct rrdresult {
277 RRDSET *st; // the chart this result refers to
279 uint32_t result_options; // RRDR_RESULT_OPTION_*
281 int d; // the number of dimensions
282 long n; // the number of values in the arrays
283 long rows; // the number of rows used
285 uint8_t *od; // the options for the dimensions
287 time_t *t; // array of n timestamps
288 calculated_number *v; // array n x d values
289 uint8_t *o; // array n x d options
291 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
293 long group; // how many collected values were grouped for each row
294 long update_every; // what is the suggested update frequency in seconds
296 calculated_number min;
297 calculated_number max;
302 int has_st_lock; // if st is read locked by us
305 #define rrdr_rows(r) ((r)->rows)
308 static void rrdr_dump(RRDR *r)
313 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
315 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
316 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
319 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
320 , (r->od[c] & RRDR_RESET)?"RESET ":""
321 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
322 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
327 fprintf(stderr, "RRDR does not have any values in it.\n");
331 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
333 // for each line in the array
334 for(i = 0; i < r->rows ;i++) {
335 calculated_number *cn = &r->v[ i * r->d ];
336 uint8_t *co = &r->o[ i * r->d ];
338 // print the id and the timestamp of the line
339 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
341 // for each dimension
342 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
343 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
344 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
346 if(co[c] & RRDR_EMPTY)
347 fprintf(stderr, "null ");
349 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
351 , (co[c] & RRDR_EMPTY)?"E":" "
352 , (co[c] & RRDR_RESET)?"R":" "
353 , (co[c] & RRDR_HIDDEN)?"H":" "
354 , (co[c] & RRDR_NONZERO)?"N":" "
358 fprintf(stderr, "\n");
363 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
365 char b[strlen(dims) + 1];
372 // disable all of them
373 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
374 r->od[c] |= RRDR_HIDDEN;
376 while(o && *o && (tok = mystrsep(&o, ", |"))) {
379 // find it and enable it
380 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
381 if(!strcmp(d->name, tok)) {
382 r->od[c] &= ~RRDR_HIDDEN;
384 // since the user needs this dimension
385 // make it appear as NONZERO, to return it
386 // even if the dimension has only zeros
387 r->od[c] |= RRDR_NONZERO;
393 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
396 case DATASOURCE_JSON:
397 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
400 case DATASOURCE_DATATABLE_JSON:
401 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
404 case DATASOURCE_DATATABLE_JSONP:
405 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
408 case DATASOURCE_JSONP:
409 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
413 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
417 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
421 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
424 case DATASOURCE_HTML:
425 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
428 case DATASOURCE_JS_ARRAY:
429 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
432 case DATASOURCE_SSV_COMMA:
433 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
437 buffer_strcat(wb, "unknown");
442 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
444 if(options & RRDR_OPTION_NONZERO) {
448 // the caller wants specific dimensions
449 // disable NONZERO option
450 // to make sure we don't accidentally prevent
451 // the specific dimensions from being returned
455 // find how many dimensions are not zero
458 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
459 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
460 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
465 // if with nonzero we get i = 0 (no dimensions will be returned)
466 // disable nonzero to show all dimensions
467 if(!i) options &= ~RRDR_OPTION_NONZERO;
473 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
475 long rows = rrdr_rows(r);
479 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
480 char kq[2] = "", // key quote
481 sq[2] = ""; // string quote
483 if( options & RRDR_OPTION_GOOGLE_JSON ) {
492 buffer_sprintf(wb, "{\n"
495 " %sname%s: %s%s%s,\n"
496 " %sview_update_every%s: %d,\n"
497 " %supdate_every%s: %d,\n"
498 " %sfirst_entry%s: %u,\n"
499 " %slast_entry%s: %u,\n"
502 " %sdimension_names%s: ["
504 , kq, kq, sq, r->st->id, sq
505 , kq, kq, sq, r->st->name, sq
506 , kq, kq, r->update_every
507 , kq, kq, r->st->update_every
508 , kq, kq, rrdset_first_entry_t(r->st)
509 , kq, kq, rrdset_last_entry_t(r->st)
514 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
515 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
516 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
518 if(i) buffer_strcat(wb, ", ");
519 buffer_strcat(wb, sq);
520 buffer_strcat(wb, rd->name);
521 buffer_strcat(wb, sq);
525 #ifdef NETDATA_INTERNAL_CHECKS
526 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
529 buffer_strcat(wb, sq);
530 buffer_strcat(wb, "no data");
531 buffer_strcat(wb, sq);
534 buffer_sprintf(wb, "],\n"
535 " %sdimension_ids%s: ["
538 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
539 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
540 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
542 if(i) buffer_strcat(wb, ", ");
543 buffer_strcat(wb, sq);
544 buffer_strcat(wb, rd->id);
545 buffer_strcat(wb, sq);
550 buffer_strcat(wb, sq);
551 buffer_strcat(wb, "no data");
552 buffer_strcat(wb, sq);
555 buffer_sprintf(wb, "],\n"
556 " %slatest_values%s: ["
559 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
560 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
561 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
563 if(i) buffer_strcat(wb, ", ");
566 storage_number n = rd->values[rrdset_last_slot(r->st)];
568 if(!does_storage_number_exist(n))
569 buffer_strcat(wb, "null");
571 buffer_rrd_value(wb, unpack_storage_number(n));
575 buffer_strcat(wb, "null");
578 buffer_sprintf(wb, "],\n"
579 " %sview_latest_values%s: ["
584 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
585 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
586 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
588 if(i) buffer_strcat(wb, ", ");
591 calculated_number *cn = &r->v[ (0) * r->d ];
592 uint8_t *co = &r->o[ (0) * r->d ];
594 if(co[c] & RRDR_EMPTY)
595 buffer_strcat(wb, "null");
597 buffer_rrd_value(wb, cn[c]);
602 buffer_strcat(wb, "null");
605 buffer_sprintf(wb, "],\n"
606 " %sdimensions%s: %d,\n"
614 rrdr_buffer_print_format(wb, format);
616 buffer_sprintf(wb, "%s,\n"
622 if(string_value) buffer_strcat(wb, sq);
623 //info("JSONWRAPPER(): %s: END", r->st->id);
626 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
631 char kq[2] = "", // key quote
632 sq[2] = ""; // string quote
634 if( options & RRDR_OPTION_GOOGLE_JSON ) {
643 if(string_value) buffer_strcat(wb, sq);
645 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
646 buffer_rrd_value(wb, r->min);
647 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
648 buffer_rrd_value(wb, r->max);
649 buffer_strcat(wb, "\n}\n");
652 #define JSON_DATES_JS 1
653 #define JSON_DATES_TIMESTAMP 2
655 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
657 //info("RRD2JSON(): %s: BEGIN", r->st->id);
658 int row_annotations = 0, dates, dates_with_new = 0;
659 char kq[2] = "", // key quote
660 sq[2] = "", // string quote
661 pre_label[101] = "", // before each label
662 post_label[101] = "", // after each label
663 pre_date[101] = "", // the beginning of line, to the date
664 post_date[101] = "", // closing the date
665 pre_value[101] = "", // before each value
666 post_value[101] = "", // after each value
667 post_line[101] = "", // at the end of each row
668 normal_annotation[201] = "", // default row annotation
669 overflow_annotation[201] = "", // overflow row annotation
670 data_begin[101] = "", // between labels and values
671 finish[101] = ""; // at the end of everything
674 dates = JSON_DATES_JS;
675 if( options & RRDR_OPTION_GOOGLE_JSON ) {
684 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
685 snprintf(post_date, 100, "%s}", sq);
686 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
687 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
688 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
689 snprintf(post_value, 100, "}");
690 snprintf(post_line, 100, "]}");
691 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
692 snprintf(finish, 100, "\n ]\n}");
694 snprintf(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);
695 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
697 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq, kq, kq);
698 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);
699 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);
700 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);
702 // remove the valueobjects flag
703 // google wants its own keys
704 if(options & RRDR_OPTION_OBJECTSROWS)
705 options &= ~RRDR_OPTION_OBJECTSROWS;
710 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
711 dates = JSON_DATES_TIMESTAMP;
715 dates = JSON_DATES_JS;
718 if( options & RRDR_OPTION_OBJECTSROWS )
719 snprintf(pre_date, 100, " { ");
721 snprintf(pre_date, 100, " [ ");
722 snprintf(pre_label, 100, ", \"");
723 snprintf(post_label, 100, "\"");
724 snprintf(pre_value, 100, ", ");
725 if( options & RRDR_OPTION_OBJECTSROWS )
726 snprintf(post_line, 100, "}");
728 snprintf(post_line, 100, "]");
729 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
730 snprintf(finish, 100, "\n ]\n}");
732 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
733 buffer_sprintf(wb, "%stime%s", sq, sq);
736 // -------------------------------------------------------------------------
737 // print the JSON header
742 // print the header lines
743 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
744 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
745 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
747 buffer_strcat(wb, pre_label);
748 buffer_strcat(wb, rd->name);
749 buffer_strcat(wb, post_label);
753 buffer_strcat(wb, pre_label);
754 buffer_strcat(wb, "no data");
755 buffer_strcat(wb, post_label);
758 // print the begin of row data
759 buffer_strcat(wb, data_begin);
761 // if all dimensions are hidden, print a null
763 buffer_strcat(wb, finish);
767 long start = 0, end = rrdr_rows(r), step = 1;
768 if((options & RRDR_OPTION_REVERSED)) {
769 start = rrdr_rows(r) - 1;
774 // for each line in the array
775 calculated_number total = 1;
776 for(i = start; i != end ;i += step) {
777 calculated_number *cn = &r->v[ i * r->d ];
778 uint8_t *co = &r->o[ i * r->d ];
780 time_t now = r->t[i];
782 if(dates == JSON_DATES_JS) {
783 // generate the local date time
784 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
785 if(!tm) { error("localtime_r() failed."); continue; }
787 if(likely(i != start)) buffer_strcat(wb, ",\n");
788 buffer_strcat(wb, pre_date);
790 if( options & RRDR_OPTION_OBJECTSROWS )
791 buffer_sprintf(wb, "%stime%s: ", kq, kq);
794 buffer_strcat(wb, "new ");
796 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
798 buffer_strcat(wb, post_date);
800 if(row_annotations) {
801 // google supports one annotation per row
802 int annotation_found = 0;
803 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
804 if(co[c] & RRDR_RESET) {
805 buffer_strcat(wb, overflow_annotation);
806 annotation_found = 1;
810 if(!annotation_found)
811 buffer_strcat(wb, normal_annotation);
815 // print the timestamp of the line
816 if(likely(i != start)) buffer_strcat(wb, ",\n");
817 buffer_strcat(wb, pre_date);
819 if( options & RRDR_OPTION_OBJECTSROWS )
820 buffer_sprintf(wb, "%stime%s: ", kq, kq);
822 buffer_rrd_value(wb, (calculated_number)r->t[i]);
824 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
826 buffer_strcat(wb, post_date);
829 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
831 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
832 calculated_number n = cn[c];
834 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
839 // prevent a division by zero
840 if(total == 0) total = 1;
843 // for each dimension
844 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
845 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
846 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
848 calculated_number n = cn[c];
850 buffer_strcat(wb, pre_value);
852 if( options & RRDR_OPTION_OBJECTSROWS )
853 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
855 if(co[c] & RRDR_EMPTY) {
856 if(options & RRDR_OPTION_NULL2ZERO)
857 buffer_strcat(wb, "0");
859 buffer_strcat(wb, "null");
862 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
865 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
868 buffer_rrd_value(wb, n);
871 buffer_strcat(wb, post_value);
874 buffer_strcat(wb, post_line);
877 buffer_strcat(wb, finish);
878 //info("RRD2JSON(): %s: END", r->st->id);
881 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
883 //info("RRD2CSV(): %s: BEGIN", r->st->id);
887 // print the csv header
888 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
889 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
890 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
893 buffer_strcat(wb, startline);
894 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
895 buffer_strcat(wb, "time");
896 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
898 buffer_strcat(wb, separator);
899 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
900 buffer_strcat(wb, d->name);
901 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
904 buffer_strcat(wb, endline);
907 // no dimensions present
911 long start = 0, end = rrdr_rows(r), step = 1;
912 if((options & RRDR_OPTION_REVERSED)) {
913 start = rrdr_rows(r) - 1;
918 // for each line in the array
919 calculated_number total = 1;
920 for(i = start; i != end ;i += step) {
921 calculated_number *cn = &r->v[ i * r->d ];
922 uint8_t *co = &r->o[ i * r->d ];
924 buffer_strcat(wb, betweenlines);
925 buffer_strcat(wb, startline);
927 time_t now = r->t[i];
929 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
930 // print the timestamp of the line
931 buffer_rrd_value(wb, (calculated_number)now);
933 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
936 // generate the local date time
937 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
938 if(!tm) { error("localtime() failed."); continue; }
939 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
942 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
944 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
945 calculated_number n = cn[c];
947 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
952 // prevent a division by zero
953 if(total == 0) total = 1;
956 // for each dimension
957 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
958 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
959 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
961 buffer_strcat(wb, separator);
963 calculated_number n = cn[c];
965 if(co[c] & RRDR_EMPTY) {
966 if(options & RRDR_OPTION_NULL2ZERO)
967 buffer_strcat(wb, "0");
969 buffer_strcat(wb, "null");
972 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
975 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
978 buffer_rrd_value(wb, n);
982 buffer_strcat(wb, endline);
984 //info("RRD2CSV(): %s: END", r->st->id);
987 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
989 //info("RRD2SSV(): %s: BEGIN", r->st->id);
993 buffer_strcat(wb, prefix);
994 long start = 0, end = rrdr_rows(r), step = 1;
995 if((options & RRDR_OPTION_REVERSED)) {
996 start = rrdr_rows(r) - 1;
1001 // for each line in the array
1002 calculated_number total = 1;
1003 for(i = start; i != end ;i += step) {
1005 calculated_number *cn = &r->v[ i * r->d ];
1006 uint8_t *co = &r->o[ i * r->d ];
1008 calculated_number sum = 0, min = 0, max = 0, v;
1009 int all_null = 1, init = 1;
1011 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1013 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1014 calculated_number n = cn[c];
1016 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1021 // prevent a division by zero
1022 if(total == 0) total = 1;
1025 // for each dimension
1026 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1027 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1028 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1030 calculated_number n = cn[c];
1032 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1035 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1036 n = n * 100 / total;
1038 if(unlikely(init)) {
1050 if(likely(!(co[c] & RRDR_EMPTY))) {
1055 if(n < min) min = n;
1056 if(n > max) max = n;
1059 if(likely(i != start))
1060 buffer_strcat(wb, separator);
1063 if(options & RRDR_OPTION_NULL2ZERO)
1064 buffer_strcat(wb, "0");
1066 buffer_strcat(wb, "null");
1069 if(options & RRDR_OPTION_MIN2MAX)
1074 if(likely(i != start)) {
1075 if(r->min > v) r->min = v;
1076 if(r->max < v) r->max = v;
1083 buffer_rrd_value(wb, v);
1086 buffer_strcat(wb, suffix);
1087 //info("RRD2SSV(): %s: END", r->st->id);
1090 inline static calculated_number *rrdr_line_values(RRDR *r)
1092 return &r->v[ r->c * r->d ];
1095 inline static uint8_t *rrdr_line_options(RRDR *r)
1097 return &r->o[ r->c * r->d ];
1100 inline static int rrdr_line_init(RRDR *r, time_t t)
1104 if(unlikely(r->c >= r->n)) {
1105 error("requested to step above RRDR size for chart %s", r->st->name);
1115 inline static void rrdr_lock_rrdset(RRDR *r) {
1117 error("NULL value given!");
1121 pthread_rwlock_rdlock(&r->st->rwlock);
1125 inline static void rrdr_unlock_rrdset(RRDR *r) {
1127 error("NULL value given!");
1131 if(likely(r->has_st_lock)) {
1132 pthread_rwlock_unlock(&r->st->rwlock);
1137 inline static void rrdr_free(RRDR *r)
1140 error("NULL value given!");
1144 rrdr_unlock_rrdset(r);
1145 if(likely(r->t)) free(r->t);
1146 if(likely(r->v)) free(r->v);
1147 if(likely(r->o)) free(r->o);
1148 if(likely(r->od)) free(r->od);
1152 inline void rrdr_done(RRDR *r)
1158 static RRDR *rrdr_create(RRDSET *st, long n)
1161 error("NULL value given!");
1165 RRDR *r = calloc(1, sizeof(RRDR));
1166 if(unlikely(!r)) goto cleanup;
1170 rrdr_lock_rrdset(r);
1173 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1177 r->t = malloc(n * sizeof(time_t));
1178 if(unlikely(!r->t)) goto cleanup;
1180 r->v = malloc(n * r->d * sizeof(calculated_number));
1181 if(unlikely(!r->v)) goto cleanup;
1183 r->o = malloc(n * r->d * sizeof(uint8_t));
1184 if(unlikely(!r->o)) goto cleanup;
1186 r->od = malloc(r->d * sizeof(uint8_t));
1187 if(unlikely(!r->od)) goto cleanup;
1189 // set the hidden flag on hidden dimensions
1191 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1192 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1199 r->update_every = 1;
1204 error("Cannot allocate RRDR memory for %d entries", n);
1205 if(likely(r)) rrdr_free(r);
1209 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
1211 int debug = st->debug;
1212 int absolute_period_requested = -1;
1214 time_t first_entry_t = rrdset_first_entry_t(st);
1215 time_t last_entry_t = rrdset_last_entry_t(st);
1217 if(before == 0 && after == 0) {
1218 before = last_entry_t;
1219 after = first_entry_t;
1220 absolute_period_requested = 0;
1223 // allow relative for before and after
1224 if(before <= st->update_every * st->entries) {
1225 before = last_entry_t + before;
1226 absolute_period_requested = 0;
1229 if(after <= st->update_every * st->entries) {
1230 after = last_entry_t + after;
1231 absolute_period_requested = 0;
1234 if(absolute_period_requested == -1)
1235 absolute_period_requested = 1;
1237 // make sure they are within our timeframe
1238 if(before > last_entry_t) before = last_entry_t;
1239 if(before < first_entry_t) before = first_entry_t;
1241 if(after > last_entry_t) after = last_entry_t;
1242 if(after < first_entry_t) after = first_entry_t;
1244 // check if they are upside down
1245 if(after > before) {
1246 time_t tmp = before;
1251 // the duration of the chart
1252 time_t duration = before - after;
1253 long available_points = duration / st->update_every;
1255 if(duration <= 0 || available_points <= 0)
1256 return rrdr_create(st, 1);
1258 // check the wanted points
1259 if(points < 0) points = -points;
1260 if(points > available_points) points = available_points;
1261 if(points == 0) points = available_points;
1263 // calculate proper grouping of source data
1264 long group = available_points / points;
1265 if(group <= 0) group = 1;
1267 // round group to the closest integer
1268 if(available_points % points > points / 2) group++;
1270 time_t after_new = after - (after % (group * st->update_every));
1271 time_t before_new = before - (before % (group * st->update_every));
1272 long points_new = (before_new - after_new) / st->update_every / group;
1274 // find the starting and ending slots in our round robin db
1275 long start_at_slot = rrdset_time2slot(st, before_new),
1276 stop_at_slot = rrdset_time2slot(st, after_new);
1278 #ifdef NETDATA_INTERNAL_CHECKS
1279 if(after_new < first_entry_t) {
1280 error("after_new %u is too small, minimum %u", after_new, first_entry_t);
1282 if(after_new > last_entry_t) {
1283 error("after_new %u is too big, maximum %u", after_new, last_entry_t);
1285 if(before_new < first_entry_t) {
1286 error("before_new %u is too small, minimum %u", before_new, first_entry_t);
1288 if(before_new > last_entry_t) {
1289 error("before_new %u is too big, maximum %u", before_new, last_entry_t);
1291 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1292 error("start_at_slot is invalid %ld, expected %ld to %ld", start_at_slot, 0, st->entries - 1);
1294 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1295 error("stop_at_slot is invalid %ld, expected %ld to %ld", stop_at_slot, 0, st->entries - 1);
1297 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1298 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1302 //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);
1305 before = before_new;
1306 duration = before - after;
1307 points = points_new;
1310 // before = the end time of the calculation
1311 // after = the start time of the calculation
1312 // duration = the duration of the calculation
1313 // group = the number of source points to aggregate / group together
1314 // method = the method of grouping source points
1315 // points = the number of points to generate
1318 // -------------------------------------------------------------------------
1319 // initialize our result set
1321 RRDR *r = rrdr_create(st, points);
1323 #ifdef NETDATA_INTERNAL_CHECKS
1324 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1329 #ifdef NETDATA_INTERNAL_CHECKS
1330 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1335 if(absolute_period_requested == 1)
1336 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1338 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1340 // find how many dimensions we have
1341 long dimensions = r->d;
1344 // -------------------------------------------------------------------------
1345 // checks for debugging
1347 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %lu, last_t: %lu, all_duration: %lu, after: %lu, before: %lu, duration: %lu, points: %ld, group: %ld"
1351 , last_entry_t - first_entry_t
1360 // -------------------------------------------------------------------------
1361 // temp arrays for keeping values per dimension
1363 calculated_number group_values[dimensions]; // keep sums when grouping
1364 long group_counts[dimensions]; // keep the number of values added to group_values
1365 uint8_t group_options[dimensions];
1366 uint8_t found_non_zero[dimensions];
1372 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1373 group_values[c] = 0;
1374 group_counts[c] = 0;
1375 group_options[c] = 0;
1376 found_non_zero[c] = 0;
1380 // -------------------------------------------------------------------------
1383 time_t now = rrdset_slot2time(st, start_at_slot),
1384 dt = st->update_every,
1387 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
1399 r->update_every = group * st->update_every;
1403 //info("RRD2RRDR(): %s: STARTING", st->id);
1405 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1406 for(; !stop_now ; now -= dt, slot--, counter++) {
1407 if(unlikely(slot < 0)) slot = st->entries - 1;
1408 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1410 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1417 , (group_count + 1 == group)?"PRINT":" - "
1418 , (now >= after && now <= before)?"RANGE":" - "
1421 // make sure we return data in the proper time range
1422 if(unlikely(now > before)) continue;
1423 if(unlikely(now < after)) break;
1425 if(unlikely(group_count == 0)) {
1426 group_start_t = now;
1430 if(unlikely(group_count == group)) {
1431 if(unlikely(added >= points)) break;
1435 // do the calculations
1436 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1437 storage_number n = rd->values[slot];
1438 if(unlikely(!does_storage_number_exist(n))) continue;
1442 calculated_number value = unpack_storage_number(n);
1443 if(likely(value != 0.0)) {
1444 group_options[c] |= RRDR_NONZERO;
1445 found_non_zero[c] = 1;
1448 if(unlikely(did_storage_number_reset(n)))
1449 group_options[c] |= RRDR_RESET;
1451 switch(group_method) {
1453 if(unlikely(abs(value) > abs(group_values[c])))
1454 group_values[c] = value;
1460 group_values[c] += value;
1466 if(unlikely(add_this)) {
1467 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1471 calculated_number *cn = rrdr_line_values(r);
1472 uint8_t *co = rrdr_line_options(r);
1474 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1476 // update the dimension options
1477 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1479 // store the specific point options
1480 co[c] = group_options[c];
1483 if(unlikely(group_counts[c] == 0)) {
1485 co[c] |= RRDR_EMPTY;
1487 else if(unlikely(group_method == GROUP_AVERAGE)) {
1488 cn[c] = group_values[c] / group_counts[c];
1491 cn[c] = group_values[c];
1494 if(cn[c] < r->min) r->min = cn[c];
1495 if(cn[c] > r->max) r->max = cn[c];
1497 // reset them for the next loop
1498 group_values[c] = 0;
1499 group_counts[c] = 0;
1500 group_options[c] = 0;
1510 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1511 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1515 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)
1517 RRDR *r = rrd2rrdr(st, points, after, before, group_method);
1519 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1523 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1524 wb->options |= WB_CONTENT_NO_CACHEABLE;
1525 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1526 wb->options |= WB_CONTENT_CACHEABLE;
1528 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1531 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1533 if(latest_timestamp && rrdr_rows(r) > 0)
1534 *latest_timestamp = r->before;
1537 case DATASOURCE_SSV:
1538 if(options & RRDR_OPTION_JSON_WRAP) {
1539 wb->contenttype = CT_APPLICATION_JSON;
1540 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1541 rrdr2ssv(r, wb, options, "", " ", "");
1542 rrdr_json_wrapper_end(r, wb, format, options, 1);
1545 wb->contenttype = CT_TEXT_PLAIN;
1546 rrdr2ssv(r, wb, options, "", " ", "");
1550 case DATASOURCE_SSV_COMMA:
1551 if(options & RRDR_OPTION_JSON_WRAP) {
1552 wb->contenttype = CT_APPLICATION_JSON;
1553 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1554 rrdr2ssv(r, wb, options, "", ",", "");
1555 rrdr_json_wrapper_end(r, wb, format, options, 1);
1558 wb->contenttype = CT_TEXT_PLAIN;
1559 rrdr2ssv(r, wb, options, "", ",", "");
1563 case DATASOURCE_JS_ARRAY:
1564 if(options & RRDR_OPTION_JSON_WRAP) {
1565 wb->contenttype = CT_APPLICATION_JSON;
1566 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1567 rrdr2ssv(r, wb, options, "[", ",", "]");
1568 rrdr_json_wrapper_end(r, wb, format, options, 0);
1571 wb->contenttype = CT_APPLICATION_JSON;
1572 rrdr2ssv(r, wb, options, "[", ",", "]");
1576 case DATASOURCE_CSV:
1577 if(options & RRDR_OPTION_JSON_WRAP) {
1578 wb->contenttype = CT_APPLICATION_JSON;
1579 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1580 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1581 rrdr_json_wrapper_end(r, wb, format, options, 1);
1584 wb->contenttype = CT_TEXT_PLAIN;
1585 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1589 case DATASOURCE_CSV_JSON_ARRAY:
1590 wb->contenttype = CT_APPLICATION_JSON;
1591 if(options & RRDR_OPTION_JSON_WRAP) {
1592 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1593 buffer_strcat(wb, "[\n");
1594 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1595 buffer_strcat(wb, "\n]");
1596 rrdr_json_wrapper_end(r, wb, format, options, 0);
1599 wb->contenttype = CT_TEXT_PLAIN;
1600 buffer_strcat(wb, "[\n");
1601 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1602 buffer_strcat(wb, "\n]");
1606 case DATASOURCE_TSV:
1607 if(options & RRDR_OPTION_JSON_WRAP) {
1608 wb->contenttype = CT_APPLICATION_JSON;
1609 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1610 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1611 rrdr_json_wrapper_end(r, wb, format, options, 1);
1614 wb->contenttype = CT_TEXT_PLAIN;
1615 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1619 case DATASOURCE_HTML:
1620 if(options & RRDR_OPTION_JSON_WRAP) {
1621 wb->contenttype = CT_APPLICATION_JSON;
1622 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1623 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1624 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1625 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1626 rrdr_json_wrapper_end(r, wb, format, options, 1);
1629 wb->contenttype = CT_TEXT_HTML;
1630 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1631 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1632 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1636 case DATASOURCE_DATATABLE_JSONP:
1637 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1639 if(options & RRDR_OPTION_JSON_WRAP)
1640 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1642 rrdr2json(r, wb, options, 1);
1644 if(options & RRDR_OPTION_JSON_WRAP)
1645 rrdr_json_wrapper_end(r, wb, format, options, 0);
1648 case DATASOURCE_DATATABLE_JSON:
1649 wb->contenttype = CT_APPLICATION_JSON;
1651 if(options & RRDR_OPTION_JSON_WRAP)
1652 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1654 rrdr2json(r, wb, options, 1);
1656 if(options & RRDR_OPTION_JSON_WRAP)
1657 rrdr_json_wrapper_end(r, wb, format, options, 0);
1660 case DATASOURCE_JSONP:
1661 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1662 if(options & RRDR_OPTION_JSON_WRAP)
1663 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1665 rrdr2json(r, wb, options, 0);
1667 if(options & RRDR_OPTION_JSON_WRAP)
1668 rrdr_json_wrapper_end(r, wb, format, options, 0);
1671 case DATASOURCE_JSON:
1673 wb->contenttype = CT_APPLICATION_JSON;
1675 if(options & RRDR_OPTION_JSON_WRAP)
1676 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1678 rrdr2json(r, wb, options, 0);
1680 if(options & RRDR_OPTION_JSON_WRAP)
1681 rrdr_json_wrapper_end(r, wb, format, options, 0);
1689 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)
1692 pthread_rwlock_rdlock(&st->rwlock);
1695 // -------------------------------------------------------------------------
1696 // switch from JSON to google JSON
1701 case DATASOURCE_DATATABLE_JSON:
1702 case DATASOURCE_DATATABLE_JSONP:
1707 case DATASOURCE_JSON:
1713 // -------------------------------------------------------------------------
1714 // validate the parameters
1716 if(points < 1) points = 1;
1717 if(group < 1) group = 1;
1719 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1720 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1724 // our return value (the last timestamp printed)
1725 // this is required to detect re-transmit in google JSONP
1726 time_t last_timestamp = 0;
1729 // -------------------------------------------------------------------------
1730 // find how many dimensions we have
1734 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1736 pthread_rwlock_unlock(&st->rwlock);
1737 buffer_strcat(wb, "No dimensions yet.");
1742 // -------------------------------------------------------------------------
1743 // prepare various strings, to speed up the loop
1745 char overflow_annotation[201]; snprintf(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);
1746 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1747 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1748 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1749 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1750 char post_value[21]; snprintf(post_value, 20, "}");
1753 // -------------------------------------------------------------------------
1754 // checks for debugging
1757 debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu"
1759 , rrdset_first_entry_t(st)
1760 , rrdset_last_entry_t(st)
1761 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1770 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1772 if((before - after) > st->entries * st->update_every)
1773 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
1777 // -------------------------------------------------------------------------
1778 // temp arrays for keeping values per dimension
1780 calculated_number group_values[dimensions]; // keep sums when grouping
1781 int print_hidden[dimensions]; // keep hidden flags
1782 int found_non_zero[dimensions];
1783 int found_non_existing[dimensions];
1786 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1787 group_values[c] = 0;
1788 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1789 found_non_zero[c] = 0;
1790 found_non_existing[c] = 0;
1794 // 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);
1795 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1796 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1798 // -------------------------------------------------------------------------
1799 // remove dimensions that contain only zeros
1802 if(only_non_zero) max_loop = 2;
1804 for(; max_loop ; max_loop--) {
1806 // -------------------------------------------------------------------------
1807 // print the JSON header
1809 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1810 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);
1811 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);
1812 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);
1814 // print the header for each dimension
1815 // and update the print_hidden array for the dimensions that should be hidden
1817 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1818 if(!print_hidden[c]) {
1820 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);
1824 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);
1827 // print the begin of row data
1828 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1831 // -------------------------------------------------------------------------
1834 int annotate_reset = 0;
1835 int annotation_count = 0;
1837 long t = rrdset_time2slot(st, before),
1838 stop_at_t = rrdset_time2slot(st, after),
1843 time_t now = rrdset_slot2time(st, t),
1844 dt = st->update_every;
1846 long count = 0, printed = 0, group_count = 0;
1849 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%lu before:%lu, points:%d, group:%d, CHART cur:%ld first: %lu last:%lu, CALC start_t:%ld, stop_t:%ld"
1856 , rrdset_first_entry_t(st)
1857 , rrdset_last_entry_t(st)
1863 for(; !stop_now ; now -= dt, t--, counter++) {
1864 if(t < 0) t = st->entries - 1;
1865 if(t == stop_at_t) stop_now = counter;
1869 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1876 , (group_count + 1 == group)?"PRINT":" - "
1877 , (now >= after && now <= before)?"RANGE":" - "
1881 // make sure we return data in the proper time range
1882 if(now > before) continue;
1883 if(now < after) break;
1885 //if(rrdset_slot2time(st, t) != now)
1886 // 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));
1891 // check if we have to print this now
1892 if(group_count == group) {
1893 if(printed >= points) {
1894 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1898 // generate the local date time
1899 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1900 if(!tm) { error("localtime() failed."); continue; }
1901 if(now > last_timestamp) last_timestamp = now;
1903 if(printed) buffer_strcat(wb, "]},\n");
1904 buffer_strcat(wb, pre_date);
1905 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1906 buffer_strcat(wb, post_date);
1911 // do the calculations
1912 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1913 storage_number n = rd->values[t];
1914 calculated_number value = unpack_storage_number(n);
1916 if(!does_storage_number_exist(n)) {
1918 found_non_existing[c]++;
1920 if(did_storage_number_reset(n)) annotate_reset = 1;
1922 switch(group_method) {
1924 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1928 group_values[c] += value;
1933 group_values[c] += value;
1934 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1940 if(annotate_reset) {
1942 buffer_strcat(wb, overflow_annotation);
1946 buffer_strcat(wb, normal_annotation);
1949 for(c = 0 ; c < dimensions ; c++) {
1950 if(found_non_existing[c] == group_count) {
1951 // all entries are non-existing
1953 buffer_strcat(wb, pre_value);
1954 buffer_strcat(wb, "null");
1955 buffer_strcat(wb, post_value);
1957 else if(!print_hidden[c]) {
1959 buffer_strcat(wb, pre_value);
1960 buffer_rrd_value(wb, group_values[c]);
1961 buffer_strcat(wb, post_value);
1963 if(group_values[c]) found_non_zero[c]++;
1966 // reset them for the next loop
1967 group_values[c] = 0;
1968 found_non_existing[c] = 0;
1971 // if all dimensions are hidden, print a null
1973 buffer_strcat(wb, pre_value);
1974 buffer_strcat(wb, "null");
1975 buffer_strcat(wb, post_value);
1983 if(printed) buffer_strcat(wb, "]}");
1984 buffer_strcat(wb, "\n ]\n}\n");
1986 if(only_non_zero && max_loop > 1) {
1988 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1989 group_values[c] = 0;
1990 found_non_existing[c] = 0;
1992 if(!print_hidden[c] && !found_non_zero[c]) {
1994 print_hidden[c] = 1;
1998 if(changed) buffer_flush(wb);
2005 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
2007 pthread_rwlock_unlock(&st->rwlock);
2008 return last_timestamp;