14 #define HOSTNAME_MAX 1024
15 char *hostname = "unknown";
17 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
19 pthread_rwlock_rdlock(&st->rwlock);
23 "\t\t\t\"id\": \"%s\",\n"
24 "\t\t\t\"name\": \"%s\",\n"
25 "\t\t\t\"type\": \"%s\",\n"
26 "\t\t\t\"family\": \"%s\",\n"
27 "\t\t\t\"context\": \"%s\",\n"
28 "\t\t\t\"title\": \"%s\",\n"
29 "\t\t\t\"priority\": %ld,\n"
30 "\t\t\t\"enabled\": %s,\n"
31 "\t\t\t\"units\": \"%s\",\n"
32 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
33 "\t\t\t\"chart_type\": \"%s\",\n"
34 "\t\t\t\"duration\": %ld,\n"
35 "\t\t\t\"first_entry\": %lu,\n"
36 "\t\t\t\"last_entry\": %lu,\n"
37 "\t\t\t\"update_every\": %d,\n"
38 "\t\t\t\"dimensions\": {\n"
46 , st->enabled?"true":"false"
49 , rrdset_type_name(st->chart_type)
50 , st->entries * st->update_every
51 , rrdset_first_entry_t(st)
52 , rrdset_last_entry_t(st)
56 unsigned long memory = st->memsize;
60 for(rd = st->dimensions; rd ; rd = rd->next) {
61 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
63 memory += rd->memsize;
67 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
81 pthread_rwlock_unlock(&st->rwlock);
84 void rrd_stats_api_v1_charts(BUFFER *wb)
89 buffer_sprintf(wb, "{\n"
90 "\t\"hostname\": \"%s\""
91 ",\n\t\"update_every\": %d"
92 ",\n\t\"history\": %d"
96 , rrd_default_history_entries
99 pthread_rwlock_rdlock(&rrdset_root_rwlock);
100 for(st = rrdset_root, c = 0; st ; st = st->next) {
102 if(c) buffer_strcat(wb, ",");
103 buffer_strcat(wb, "\n\t\t\"");
104 buffer_strcat(wb, st->id);
105 buffer_strcat(wb, "\": ");
106 rrd_stats_api_v1_chart(st, wb);
110 pthread_rwlock_unlock(&rrdset_root_rwlock);
112 buffer_strcat(wb, "\n\t}\n}\n");
116 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
118 time_t now = time(NULL);
120 pthread_rwlock_rdlock(&st->rwlock);
124 "\t\t\t\"id\": \"%s\",\n"
125 "\t\t\t\"name\": \"%s\",\n"
126 "\t\t\t\"type\": \"%s\",\n"
127 "\t\t\t\"family\": \"%s\",\n"
128 "\t\t\t\"context\": \"%s\",\n"
129 "\t\t\t\"title\": \"%s\",\n"
130 "\t\t\t\"priority\": %ld,\n"
131 "\t\t\t\"enabled\": %d,\n"
132 "\t\t\t\"units\": \"%s\",\n"
133 "\t\t\t\"url\": \"/data/%s/%s\",\n"
134 "\t\t\t\"chart_type\": \"%s\",\n"
135 "\t\t\t\"counter\": %ld,\n"
136 "\t\t\t\"entries\": %ld,\n"
137 "\t\t\t\"first_entry_t\": %lu,\n"
138 "\t\t\t\"last_entry\": %ld,\n"
139 "\t\t\t\"last_entry_t\": %lu,\n"
140 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
141 "\t\t\t\"update_every\": %d,\n"
142 "\t\t\t\"isdetail\": %d,\n"
143 "\t\t\t\"usec_since_last_update\": %llu,\n"
144 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
145 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
146 "\t\t\t\"dimensions\": [\n"
156 , st->name, options?options:""
157 , rrdset_type_name(st->chart_type)
160 , rrdset_first_entry_t(st)
161 , rrdset_last_slot(st)
162 , rrdset_last_entry_t(st)
163 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
166 , st->usec_since_last_update
167 , st->collected_total
168 , st->last_collected_total
171 unsigned long memory = st->memsize;
174 for(rd = st->dimensions; rd ; rd = rd->next) {
176 memory += rd->memsize;
180 "\t\t\t\t\t\"id\": \"%s\",\n"
181 "\t\t\t\t\t\"name\": \"%s\",\n"
182 "\t\t\t\t\t\"entries\": %ld,\n"
183 "\t\t\t\t\t\"isHidden\": %d,\n"
184 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
185 "\t\t\t\t\t\"multiplier\": %ld,\n"
186 "\t\t\t\t\t\"divisor\": %ld,\n"
187 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
188 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
189 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
190 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
191 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
192 "\t\t\t\t\t\"memory\": %lu\n"
197 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
198 , rrddim_algorithm_name(rd->algorithm)
201 , rd->last_collected_time.tv_sec
202 , rd->collected_value
203 , rd->calculated_value
204 , rd->last_collected_value
205 , rd->last_calculated_value
213 "\t\t\t\"memory\" : %lu\n"
218 pthread_rwlock_unlock(&st->rwlock);
222 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
223 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
225 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
227 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
228 rrd_stats_one_json(st, options, wb);
229 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
232 void rrd_stats_all_json(BUFFER *wb)
234 unsigned long memory = 0;
238 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
240 pthread_rwlock_rdlock(&rrdset_root_rwlock);
241 for(st = rrdset_root, c = 0; st ; st = st->next) {
243 if(c) buffer_strcat(wb, ",\n");
244 memory += rrd_stats_one_json(st, NULL, wb);
248 pthread_rwlock_unlock(&rrdset_root_rwlock);
250 buffer_sprintf(wb, "\n\t],\n"
251 "\t\"hostname\": \"%s\",\n"
252 "\t\"update_every\": %d,\n"
253 "\t\"history\": %d,\n"
254 "\t\"memory\": %lu\n"
258 , rrd_default_history_entries
265 // ----------------------------------------------------------------------------
267 // RRDR dimension options
268 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
269 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
270 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
271 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
273 // RRDR result options
274 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
275 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
277 typedef struct rrdresult {
278 RRDSET *st; // the chart this result refers to
280 uint32_t result_options; // RRDR_RESULT_OPTION_*
282 int d; // the number of dimensions
283 long n; // the number of values in the arrays
284 long rows; // the number of rows used
286 uint8_t *od; // the options for the dimensions
288 time_t *t; // array of n timestamps
289 calculated_number *v; // array n x d values
290 uint8_t *o; // array n x d options
292 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
294 long group; // how many collected values were grouped for each row
295 int update_every; // what is the suggested update frequency in seconds
297 calculated_number min;
298 calculated_number max;
303 int has_st_lock; // if st is read locked by us
306 #define rrdr_rows(r) ((r)->rows)
309 static void rrdr_dump(RRDR *r)
314 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
316 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
317 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
320 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
321 , (r->od[c] & RRDR_RESET)?"RESET ":""
322 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
323 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
328 fprintf(stderr, "RRDR does not have any values in it.\n");
332 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
334 // for each line in the array
335 for(i = 0; i < r->rows ;i++) {
336 calculated_number *cn = &r->v[ i * r->d ];
337 uint8_t *co = &r->o[ i * r->d ];
339 // print the id and the timestamp of the line
340 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
342 // for each dimension
343 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
344 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
345 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
347 if(co[c] & RRDR_EMPTY)
348 fprintf(stderr, "null ");
350 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
352 , (co[c] & RRDR_EMPTY)?"E":" "
353 , (co[c] & RRDR_RESET)?"R":" "
354 , (co[c] & RRDR_HIDDEN)?"H":" "
355 , (co[c] & RRDR_NONZERO)?"N":" "
359 fprintf(stderr, "\n");
364 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
366 char b[strlen(dims) + 1];
373 // disable all of them
374 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
375 r->od[c] |= RRDR_HIDDEN;
377 while(o && *o && (tok = mystrsep(&o, ",|"))) {
380 uint32_t hash = simple_hash(tok);
382 // find it and enable it
383 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
384 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
385 r->od[c] &= ~RRDR_HIDDEN;
387 // since the user needs this dimension
388 // make it appear as NONZERO, to return it
389 // even if the dimension has only zeros
390 r->od[c] |= RRDR_NONZERO;
396 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
399 case DATASOURCE_JSON:
400 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
403 case DATASOURCE_DATATABLE_JSON:
404 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
407 case DATASOURCE_DATATABLE_JSONP:
408 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
411 case DATASOURCE_JSONP:
412 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
416 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
420 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
424 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
427 case DATASOURCE_HTML:
428 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
431 case DATASOURCE_JS_ARRAY:
432 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
435 case DATASOURCE_SSV_COMMA:
436 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
440 buffer_strcat(wb, "unknown");
445 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
447 if(options & RRDR_OPTION_NONZERO) {
451 // the caller wants specific dimensions
452 // disable NONZERO option
453 // to make sure we don't accidentally prevent
454 // the specific dimensions from being returned
458 // find how many dimensions are not zero
461 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
462 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
463 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
468 // if with nonzero we get i = 0 (no dimensions will be returned)
469 // disable nonzero to show all dimensions
470 if(!i) options &= ~RRDR_OPTION_NONZERO;
476 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
478 long rows = rrdr_rows(r);
482 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
483 char kq[2] = "", // key quote
484 sq[2] = ""; // string quote
486 if( options & RRDR_OPTION_GOOGLE_JSON ) {
495 buffer_sprintf(wb, "{\n"
498 " %sname%s: %s%s%s,\n"
499 " %sview_update_every%s: %d,\n"
500 " %supdate_every%s: %d,\n"
501 " %sfirst_entry%s: %u,\n"
502 " %slast_entry%s: %u,\n"
505 " %sdimension_names%s: ["
507 , kq, kq, sq, r->st->id, sq
508 , kq, kq, sq, r->st->name, sq
509 , kq, kq, r->update_every
510 , kq, kq, r->st->update_every
511 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
512 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
513 , kq, kq, (uint32_t)r->before
514 , kq, kq, (uint32_t)r->after
517 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
518 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
519 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
521 if(i) buffer_strcat(wb, ", ");
522 buffer_strcat(wb, sq);
523 buffer_strcat(wb, rd->name);
524 buffer_strcat(wb, sq);
528 #ifdef NETDATA_INTERNAL_CHECKS
529 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
532 buffer_strcat(wb, sq);
533 buffer_strcat(wb, "no data");
534 buffer_strcat(wb, sq);
537 buffer_sprintf(wb, "],\n"
538 " %sdimension_ids%s: ["
541 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
542 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
543 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
545 if(i) buffer_strcat(wb, ", ");
546 buffer_strcat(wb, sq);
547 buffer_strcat(wb, rd->id);
548 buffer_strcat(wb, sq);
553 buffer_strcat(wb, sq);
554 buffer_strcat(wb, "no data");
555 buffer_strcat(wb, sq);
558 buffer_sprintf(wb, "],\n"
559 " %slatest_values%s: ["
562 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
563 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
564 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
566 if(i) buffer_strcat(wb, ", ");
569 storage_number n = rd->values[rrdset_last_slot(r->st)];
571 if(!does_storage_number_exist(n))
572 buffer_strcat(wb, "null");
574 buffer_rrd_value(wb, unpack_storage_number(n));
578 buffer_strcat(wb, "null");
581 buffer_sprintf(wb, "],\n"
582 " %sview_latest_values%s: ["
587 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
588 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
589 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
591 if(i) buffer_strcat(wb, ", ");
594 calculated_number *cn = &r->v[ (0) * r->d ];
595 uint8_t *co = &r->o[ (0) * r->d ];
597 if(co[c] & RRDR_EMPTY)
598 buffer_strcat(wb, "null");
600 buffer_rrd_value(wb, cn[c]);
605 buffer_strcat(wb, "null");
608 buffer_sprintf(wb, "],\n"
609 " %sdimensions%s: %ld,\n"
610 " %spoints%s: %ld,\n"
617 rrdr_buffer_print_format(wb, format);
619 buffer_sprintf(wb, "%s,\n"
625 if(string_value) buffer_strcat(wb, sq);
626 //info("JSONWRAPPER(): %s: END", r->st->id);
629 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
633 char kq[2] = "", // key quote
634 sq[2] = ""; // string quote
636 if( options & RRDR_OPTION_GOOGLE_JSON ) {
645 if(string_value) buffer_strcat(wb, sq);
647 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
648 buffer_rrd_value(wb, r->min);
649 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
650 buffer_rrd_value(wb, r->max);
651 buffer_strcat(wb, "\n}\n");
654 #define JSON_DATES_JS 1
655 #define JSON_DATES_TIMESTAMP 2
657 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
659 //info("RRD2JSON(): %s: BEGIN", r->st->id);
660 int row_annotations = 0, dates, dates_with_new = 0;
661 char kq[2] = "", // key quote
662 sq[2] = "", // string quote
663 pre_label[101] = "", // before each label
664 post_label[101] = "", // after each label
665 pre_date[101] = "", // the beginning of line, to the date
666 post_date[101] = "", // closing the date
667 pre_value[101] = "", // before each value
668 post_value[101] = "", // after each value
669 post_line[101] = "", // at the end of each row
670 normal_annotation[201] = "", // default row annotation
671 overflow_annotation[201] = "", // overflow row annotation
672 data_begin[101] = "", // between labels and values
673 finish[101] = ""; // at the end of everything
676 dates = JSON_DATES_JS;
677 if( options & RRDR_OPTION_GOOGLE_JSON ) {
686 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
687 snprintfz(post_date, 100, "%s}", sq);
688 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
689 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
690 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
691 strcpy(post_value, "}");
692 strcpy(post_line, "]}");
693 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
694 strcpy(finish, "\n ]\n}");
696 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);
697 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
699 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
700 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);
701 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);
702 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);
704 // remove the valueobjects flag
705 // google wants its own keys
706 if(options & RRDR_OPTION_OBJECTSROWS)
707 options &= ~RRDR_OPTION_OBJECTSROWS;
712 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
713 dates = JSON_DATES_TIMESTAMP;
717 dates = JSON_DATES_JS;
720 if( options & RRDR_OPTION_OBJECTSROWS )
721 strcpy(pre_date, " { ");
723 strcpy(pre_date, " [ ");
724 strcpy(pre_label, ", \"");
725 strcpy(post_label, "\"");
726 strcpy(pre_value, ", ");
727 if( options & RRDR_OPTION_OBJECTSROWS )
728 strcpy(post_line, "}");
730 strcpy(post_line, "]");
731 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
732 strcpy(finish, "\n ]\n}");
734 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
735 buffer_sprintf(wb, "%stime%s", sq, sq);
738 // -------------------------------------------------------------------------
739 // print the JSON header
744 // print the header lines
745 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
746 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
747 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
749 buffer_strcat(wb, pre_label);
750 buffer_strcat(wb, rd->name);
751 buffer_strcat(wb, post_label);
755 buffer_strcat(wb, pre_label);
756 buffer_strcat(wb, "no data");
757 buffer_strcat(wb, post_label);
760 // print the begin of row data
761 buffer_strcat(wb, data_begin);
763 // if all dimensions are hidden, print a null
765 buffer_strcat(wb, finish);
769 long start = 0, end = rrdr_rows(r), step = 1;
770 if((options & RRDR_OPTION_REVERSED)) {
771 start = rrdr_rows(r) - 1;
776 // for each line in the array
777 calculated_number total = 1;
778 for(i = start; i != end ;i += step) {
779 calculated_number *cn = &r->v[ i * r->d ];
780 uint8_t *co = &r->o[ i * r->d ];
782 time_t now = r->t[i];
784 if(dates == JSON_DATES_JS) {
785 // generate the local date time
786 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
787 if(!tm) { error("localtime_r() failed."); continue; }
789 if(likely(i != start)) buffer_strcat(wb, ",\n");
790 buffer_strcat(wb, pre_date);
792 if( options & RRDR_OPTION_OBJECTSROWS )
793 buffer_sprintf(wb, "%stime%s: ", kq, kq);
796 buffer_strcat(wb, "new ");
798 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
800 buffer_strcat(wb, post_date);
802 if(row_annotations) {
803 // google supports one annotation per row
804 int annotation_found = 0;
805 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
806 if(co[c] & RRDR_RESET) {
807 buffer_strcat(wb, overflow_annotation);
808 annotation_found = 1;
812 if(!annotation_found)
813 buffer_strcat(wb, normal_annotation);
817 // print the timestamp of the line
818 if(likely(i != start)) buffer_strcat(wb, ",\n");
819 buffer_strcat(wb, pre_date);
821 if( options & RRDR_OPTION_OBJECTSROWS )
822 buffer_sprintf(wb, "%stime%s: ", kq, kq);
824 buffer_rrd_value(wb, (calculated_number)r->t[i]);
826 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
828 buffer_strcat(wb, post_date);
831 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
833 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
834 calculated_number n = cn[c];
836 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
841 // prevent a division by zero
842 if(total == 0) total = 1;
845 // for each dimension
846 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
847 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
848 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
850 calculated_number n = cn[c];
852 buffer_strcat(wb, pre_value);
854 if( options & RRDR_OPTION_OBJECTSROWS )
855 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
857 if(co[c] & RRDR_EMPTY) {
858 if(options & RRDR_OPTION_NULL2ZERO)
859 buffer_strcat(wb, "0");
861 buffer_strcat(wb, "null");
864 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
867 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
870 buffer_rrd_value(wb, n);
873 buffer_strcat(wb, post_value);
876 buffer_strcat(wb, post_line);
879 buffer_strcat(wb, finish);
880 //info("RRD2JSON(): %s: END", r->st->id);
883 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
885 //info("RRD2CSV(): %s: BEGIN", r->st->id);
889 // print the csv header
890 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
891 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
892 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
895 buffer_strcat(wb, startline);
896 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
897 buffer_strcat(wb, "time");
898 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
900 buffer_strcat(wb, separator);
901 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
902 buffer_strcat(wb, d->name);
903 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
906 buffer_strcat(wb, endline);
909 // no dimensions present
913 long start = 0, end = rrdr_rows(r), step = 1;
914 if((options & RRDR_OPTION_REVERSED)) {
915 start = rrdr_rows(r) - 1;
920 // for each line in the array
921 calculated_number total = 1;
922 for(i = start; i != end ;i += step) {
923 calculated_number *cn = &r->v[ i * r->d ];
924 uint8_t *co = &r->o[ i * r->d ];
926 buffer_strcat(wb, betweenlines);
927 buffer_strcat(wb, startline);
929 time_t now = r->t[i];
931 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
932 // print the timestamp of the line
933 buffer_rrd_value(wb, (calculated_number)now);
935 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
938 // generate the local date time
939 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
940 if(!tm) { error("localtime() failed."); continue; }
941 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
944 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
946 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
947 calculated_number n = cn[c];
949 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
954 // prevent a division by zero
955 if(total == 0) total = 1;
958 // for each dimension
959 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
960 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
961 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
963 buffer_strcat(wb, separator);
965 calculated_number n = cn[c];
967 if(co[c] & RRDR_EMPTY) {
968 if(options & RRDR_OPTION_NULL2ZERO)
969 buffer_strcat(wb, "0");
971 buffer_strcat(wb, "null");
974 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
977 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
980 buffer_rrd_value(wb, n);
984 buffer_strcat(wb, endline);
986 //info("RRD2CSV(): %s: END", r->st->id);
989 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
993 calculated_number *cn = &r->v[ i * r->d ];
994 uint8_t *co = &r->o[ i * r->d ];
996 calculated_number sum = 0, min = 0, max = 0, v;
997 int all_null = 1, init = 1;
999 calculated_number total = 1;
1000 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1002 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1003 calculated_number n = cn[c];
1005 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1010 // prevent a division by zero
1011 if(total == 0) total = 1;
1014 // for each dimension
1015 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1016 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1017 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1019 calculated_number n = cn[c];
1021 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1024 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1025 n = n * 100 / total;
1027 if(unlikely(init)) {
1039 if(likely(!(co[c] & RRDR_EMPTY))) {
1044 if(n < min) min = n;
1045 if(n > max) max = n;
1048 if(unlikely(all_null)) {
1049 if(likely(*all_values_are_null))
1050 *all_values_are_null = 1;
1054 if(likely(*all_values_are_null))
1055 *all_values_are_null = 0;
1058 if(options & RRDR_OPTION_MIN2MAX)
1066 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1068 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1071 buffer_strcat(wb, prefix);
1072 long start = 0, end = rrdr_rows(r), step = 1;
1073 if((options & RRDR_OPTION_REVERSED)) {
1074 start = rrdr_rows(r) - 1;
1079 // for each line in the array
1080 for(i = start; i != end ;i += step) {
1081 int all_values_are_null = 0;
1082 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1084 if(likely(i != start)) {
1085 if(r->min > v) r->min = v;
1086 if(r->max < v) r->max = v;
1093 if(likely(i != start))
1094 buffer_strcat(wb, separator);
1096 if(all_values_are_null) {
1097 if(options & RRDR_OPTION_NULL2ZERO)
1098 buffer_strcat(wb, "0");
1100 buffer_strcat(wb, "null");
1103 buffer_rrd_value(wb, v);
1105 buffer_strcat(wb, suffix);
1106 //info("RRD2SSV(): %s: END", r->st->id);
1109 inline static calculated_number *rrdr_line_values(RRDR *r)
1111 return &r->v[ r->c * r->d ];
1114 inline static uint8_t *rrdr_line_options(RRDR *r)
1116 return &r->o[ r->c * r->d ];
1119 inline static int rrdr_line_init(RRDR *r, time_t t)
1123 if(unlikely(r->c >= r->n)) {
1124 error("requested to step above RRDR size for chart %s", r->st->name);
1134 inline static void rrdr_lock_rrdset(RRDR *r) {
1136 error("NULL value given!");
1140 pthread_rwlock_rdlock(&r->st->rwlock);
1144 inline static void rrdr_unlock_rrdset(RRDR *r) {
1146 error("NULL value given!");
1150 if(likely(r->has_st_lock)) {
1151 pthread_rwlock_unlock(&r->st->rwlock);
1156 inline static void rrdr_free(RRDR *r)
1159 error("NULL value given!");
1163 rrdr_unlock_rrdset(r);
1164 if(likely(r->t)) free(r->t);
1165 if(likely(r->v)) free(r->v);
1166 if(likely(r->o)) free(r->o);
1167 if(likely(r->od)) free(r->od);
1171 inline void rrdr_done(RRDR *r)
1177 static RRDR *rrdr_create(RRDSET *st, long n)
1180 error("NULL value given!");
1184 RRDR *r = calloc(1, sizeof(RRDR));
1185 if(unlikely(!r)) goto cleanup;
1189 rrdr_lock_rrdset(r);
1192 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1196 r->t = malloc(n * sizeof(time_t));
1197 if(unlikely(!r->t)) goto cleanup;
1199 r->v = malloc(n * r->d * sizeof(calculated_number));
1200 if(unlikely(!r->v)) goto cleanup;
1202 r->o = malloc(n * r->d * sizeof(uint8_t));
1203 if(unlikely(!r->o)) goto cleanup;
1205 r->od = malloc(r->d * sizeof(uint8_t));
1206 if(unlikely(!r->od)) goto cleanup;
1208 // set the hidden flag on hidden dimensions
1210 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1211 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1218 r->update_every = 1;
1223 error("Cannot allocate RRDR memory for %ld entries", n);
1224 if(likely(r)) rrdr_free(r);
1228 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1230 int debug = st->debug;
1231 int absolute_period_requested = -1;
1233 time_t first_entry_t = rrdset_first_entry_t(st);
1234 time_t last_entry_t = rrdset_last_entry_t(st);
1236 if(before == 0 && after == 0) {
1237 before = last_entry_t;
1238 after = first_entry_t;
1239 absolute_period_requested = 0;
1242 // allow relative for before and after
1243 if(((before < 0)?-before:before) <= (st->update_every * st->entries)) {
1244 before = last_entry_t + before;
1245 absolute_period_requested = 0;
1248 if(((after < 0)?-after:after) <= (st->update_every * st->entries)) {
1249 if(after == 0) after = -st->update_every;
1250 after = before + after;
1251 absolute_period_requested = 0;
1254 if(absolute_period_requested == -1)
1255 absolute_period_requested = 1;
1257 // make sure they are within our timeframe
1258 if(before > last_entry_t) before = last_entry_t;
1259 if(before < first_entry_t) before = first_entry_t;
1261 if(after > last_entry_t) after = last_entry_t;
1262 if(after < first_entry_t) after = first_entry_t;
1264 // check if they are upside down
1265 if(after > before) {
1266 time_t tmp = before;
1271 // the duration of the chart
1272 time_t duration = before - after;
1273 long available_points = duration / st->update_every;
1275 if(duration <= 0 || available_points <= 0)
1276 return rrdr_create(st, 1);
1278 // check the wanted points
1279 if(points < 0) points = -points;
1280 if(points > available_points) points = available_points;
1281 if(points == 0) points = available_points;
1283 // calculate proper grouping of source data
1284 long group = available_points / points;
1285 if(group <= 0) group = 1;
1287 // round group to the closest integer
1288 if(available_points % points > points / 2) group++;
1290 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1291 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1292 long points_new = (before_new - after_new) / st->update_every / group;
1294 // find the starting and ending slots in our round robin db
1295 long start_at_slot = rrdset_time2slot(st, before_new),
1296 stop_at_slot = rrdset_time2slot(st, after_new);
1298 #ifdef NETDATA_INTERNAL_CHECKS
1299 if(after_new < first_entry_t) {
1300 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1302 if(after_new > last_entry_t) {
1303 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1305 if(before_new < first_entry_t) {
1306 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1308 if(before_new > last_entry_t) {
1309 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1311 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1312 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1314 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1315 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1317 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1318 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1322 //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);
1325 before = before_new;
1326 duration = before - after;
1327 points = points_new;
1330 // before = the end time of the calculation
1331 // after = the start time of the calculation
1332 // duration = the duration of the calculation
1333 // group = the number of source points to aggregate / group together
1334 // method = the method of grouping source points
1335 // points = the number of points to generate
1338 // -------------------------------------------------------------------------
1339 // initialize our result set
1341 RRDR *r = rrdr_create(st, points);
1343 #ifdef NETDATA_INTERNAL_CHECKS
1344 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);
1349 #ifdef NETDATA_INTERNAL_CHECKS
1350 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);
1355 if(absolute_period_requested == 1)
1356 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1358 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1360 // find how many dimensions we have
1361 long dimensions = r->d;
1364 // -------------------------------------------------------------------------
1365 // checks for debugging
1367 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"
1369 , (uint32_t)first_entry_t
1370 , (uint32_t)last_entry_t
1371 , (uint32_t)(last_entry_t - first_entry_t)
1374 , (uint32_t)duration
1380 // -------------------------------------------------------------------------
1381 // temp arrays for keeping values per dimension
1383 calculated_number last_values[dimensions]; // keep the last value of each dimension
1384 calculated_number group_values[dimensions]; // keep sums when grouping
1385 long group_counts[dimensions]; // keep the number of values added to group_values
1386 uint8_t group_options[dimensions];
1387 uint8_t found_non_zero[dimensions];
1393 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1395 group_values[c] = 0;
1396 group_counts[c] = 0;
1397 group_options[c] = 0;
1398 found_non_zero[c] = 0;
1402 // -------------------------------------------------------------------------
1405 time_t now = rrdset_slot2time(st, start_at_slot),
1406 dt = st->update_every,
1409 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"
1421 r->update_every = group * st->update_every;
1425 //info("RRD2RRDR(): %s: STARTING", st->id);
1427 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1428 for(; !stop_now ; now -= dt, slot--, counter++) {
1429 if(unlikely(slot < 0)) slot = st->entries - 1;
1430 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1432 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1439 , (group_count + 1 == group)?"PRINT":" - "
1440 , (now >= after && now <= before)?"RANGE":" - "
1443 // make sure we return data in the proper time range
1444 if(unlikely(now > before)) continue;
1445 if(unlikely(now < after)) break;
1447 if(unlikely(group_count == 0)) {
1448 group_start_t = now;
1452 if(unlikely(group_count == group)) {
1453 if(unlikely(added >= points)) break;
1457 // do the calculations
1458 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1459 storage_number n = rd->values[slot];
1460 if(unlikely(!does_storage_number_exist(n))) continue;
1464 calculated_number value = unpack_storage_number(n);
1465 if(likely(value != 0.0)) {
1466 group_options[c] |= RRDR_NONZERO;
1467 found_non_zero[c] = 1;
1470 if(unlikely(did_storage_number_reset(n)))
1471 group_options[c] |= RRDR_RESET;
1473 switch(group_method) {
1475 if(unlikely(fabsl(value) > fabsl(group_values[c])))
1476 group_values[c] = value;
1482 group_values[c] += value;
1485 case GROUP_INCREMENTAL_SUM:
1486 if(unlikely(slot == start_at_slot))
1487 last_values[c] = value;
1489 group_values[c] += last_values[c] - value;
1490 last_values[c] = value;
1496 if(unlikely(add_this)) {
1497 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1501 calculated_number *cn = rrdr_line_values(r);
1502 uint8_t *co = rrdr_line_options(r);
1504 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1506 // update the dimension options
1507 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1509 // store the specific point options
1510 co[c] = group_options[c];
1513 if(unlikely(group_counts[c] == 0)) {
1515 co[c] |= RRDR_EMPTY;
1517 else if(unlikely(group_method == GROUP_AVERAGE)) {
1519 cn[c] = group_values[c] / group_counts[c];
1524 // GROUP_INCREMENTAL_SUM
1525 cn[c] = group_values[c];
1528 if(cn[c] < r->min) r->min = cn[c];
1529 if(cn[c] > r->max) r->max = cn[c];
1531 // reset them for the next loop
1532 group_values[c] = 0;
1533 group_counts[c] = 0;
1534 group_options[c] = 0;
1544 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1545 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1549 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, BUFFER *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp, int *value_is_null)
1551 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1553 if(value_is_null) *value_is_null = 1;
1557 if(rrdr_rows(r) == 0) {
1559 if(value_is_null) *value_is_null = 1;
1563 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1564 wb->options |= WB_CONTENT_NO_CACHEABLE;
1565 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1566 wb->options |= WB_CONTENT_CACHEABLE;
1568 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1571 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1573 if(latest_timestamp)
1574 *latest_timestamp = r->before;
1576 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1577 *n = rrdr2value(r, i, options, value_is_null);
1583 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)
1585 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1587 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1591 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1592 wb->options |= WB_CONTENT_NO_CACHEABLE;
1593 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1594 wb->options |= WB_CONTENT_CACHEABLE;
1596 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1599 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1601 if(latest_timestamp && rrdr_rows(r) > 0)
1602 *latest_timestamp = r->before;
1605 case DATASOURCE_SSV:
1606 if(options & RRDR_OPTION_JSON_WRAP) {
1607 wb->contenttype = CT_APPLICATION_JSON;
1608 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1609 rrdr2ssv(r, wb, options, "", " ", "");
1610 rrdr_json_wrapper_end(r, wb, format, options, 1);
1613 wb->contenttype = CT_TEXT_PLAIN;
1614 rrdr2ssv(r, wb, options, "", " ", "");
1618 case DATASOURCE_SSV_COMMA:
1619 if(options & RRDR_OPTION_JSON_WRAP) {
1620 wb->contenttype = CT_APPLICATION_JSON;
1621 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1622 rrdr2ssv(r, wb, options, "", ",", "");
1623 rrdr_json_wrapper_end(r, wb, format, options, 1);
1626 wb->contenttype = CT_TEXT_PLAIN;
1627 rrdr2ssv(r, wb, options, "", ",", "");
1631 case DATASOURCE_JS_ARRAY:
1632 if(options & RRDR_OPTION_JSON_WRAP) {
1633 wb->contenttype = CT_APPLICATION_JSON;
1634 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1635 rrdr2ssv(r, wb, options, "[", ",", "]");
1636 rrdr_json_wrapper_end(r, wb, format, options, 0);
1639 wb->contenttype = CT_APPLICATION_JSON;
1640 rrdr2ssv(r, wb, options, "[", ",", "]");
1644 case DATASOURCE_CSV:
1645 if(options & RRDR_OPTION_JSON_WRAP) {
1646 wb->contenttype = CT_APPLICATION_JSON;
1647 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1648 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1649 rrdr_json_wrapper_end(r, wb, format, options, 1);
1652 wb->contenttype = CT_TEXT_PLAIN;
1653 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1657 case DATASOURCE_CSV_JSON_ARRAY:
1658 wb->contenttype = CT_APPLICATION_JSON;
1659 if(options & RRDR_OPTION_JSON_WRAP) {
1660 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1661 buffer_strcat(wb, "[\n");
1662 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1663 buffer_strcat(wb, "\n]");
1664 rrdr_json_wrapper_end(r, wb, format, options, 0);
1667 wb->contenttype = CT_TEXT_PLAIN;
1668 buffer_strcat(wb, "[\n");
1669 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1670 buffer_strcat(wb, "\n]");
1674 case DATASOURCE_TSV:
1675 if(options & RRDR_OPTION_JSON_WRAP) {
1676 wb->contenttype = CT_APPLICATION_JSON;
1677 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1678 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1679 rrdr_json_wrapper_end(r, wb, format, options, 1);
1682 wb->contenttype = CT_TEXT_PLAIN;
1683 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1687 case DATASOURCE_HTML:
1688 if(options & RRDR_OPTION_JSON_WRAP) {
1689 wb->contenttype = CT_APPLICATION_JSON;
1690 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1691 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1692 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1693 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1694 rrdr_json_wrapper_end(r, wb, format, options, 1);
1697 wb->contenttype = CT_TEXT_HTML;
1698 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1699 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1700 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1704 case DATASOURCE_DATATABLE_JSONP:
1705 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1707 if(options & RRDR_OPTION_JSON_WRAP)
1708 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1710 rrdr2json(r, wb, options, 1);
1712 if(options & RRDR_OPTION_JSON_WRAP)
1713 rrdr_json_wrapper_end(r, wb, format, options, 0);
1716 case DATASOURCE_DATATABLE_JSON:
1717 wb->contenttype = CT_APPLICATION_JSON;
1719 if(options & RRDR_OPTION_JSON_WRAP)
1720 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1722 rrdr2json(r, wb, options, 1);
1724 if(options & RRDR_OPTION_JSON_WRAP)
1725 rrdr_json_wrapper_end(r, wb, format, options, 0);
1728 case DATASOURCE_JSONP:
1729 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1730 if(options & RRDR_OPTION_JSON_WRAP)
1731 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1733 rrdr2json(r, wb, options, 0);
1735 if(options & RRDR_OPTION_JSON_WRAP)
1736 rrdr_json_wrapper_end(r, wb, format, options, 0);
1739 case DATASOURCE_JSON:
1741 wb->contenttype = CT_APPLICATION_JSON;
1743 if(options & RRDR_OPTION_JSON_WRAP)
1744 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1746 rrdr2json(r, wb, options, 0);
1748 if(options & RRDR_OPTION_JSON_WRAP)
1749 rrdr_json_wrapper_end(r, wb, format, options, 0);
1757 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)
1760 pthread_rwlock_rdlock(&st->rwlock);
1763 // -------------------------------------------------------------------------
1764 // switch from JSON to google JSON
1769 case DATASOURCE_DATATABLE_JSON:
1770 case DATASOURCE_DATATABLE_JSONP:
1775 case DATASOURCE_JSON:
1781 // -------------------------------------------------------------------------
1782 // validate the parameters
1784 if(points < 1) points = 1;
1785 if(group < 1) group = 1;
1787 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1788 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1792 // our return value (the last timestamp printed)
1793 // this is required to detect re-transmit in google JSONP
1794 time_t last_timestamp = 0;
1797 // -------------------------------------------------------------------------
1798 // find how many dimensions we have
1802 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1804 pthread_rwlock_unlock(&st->rwlock);
1805 buffer_strcat(wb, "No dimensions yet.");
1810 // -------------------------------------------------------------------------
1811 // prepare various strings, to speed up the loop
1813 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);
1814 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1815 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1816 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1817 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1818 char post_value[21]; strcpy(post_value, "}");
1821 // -------------------------------------------------------------------------
1822 // checks for debugging
1825 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"
1827 , rrdset_first_entry_t(st)
1828 , rrdset_last_entry_t(st)
1829 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1838 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1840 if((before - after) > st->entries * st->update_every)
1841 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);
1845 // -------------------------------------------------------------------------
1846 // temp arrays for keeping values per dimension
1848 calculated_number group_values[dimensions]; // keep sums when grouping
1849 int print_hidden[dimensions]; // keep hidden flags
1850 int found_non_zero[dimensions];
1851 int found_non_existing[dimensions];
1854 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1855 group_values[c] = 0;
1856 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1857 found_non_zero[c] = 0;
1858 found_non_existing[c] = 0;
1862 // 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);
1863 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1864 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1866 // -------------------------------------------------------------------------
1867 // remove dimensions that contain only zeros
1870 if(only_non_zero) max_loop = 2;
1872 for(; max_loop ; max_loop--) {
1874 // -------------------------------------------------------------------------
1875 // print the JSON header
1877 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1878 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);
1879 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);
1880 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);
1882 // print the header for each dimension
1883 // and update the print_hidden array for the dimensions that should be hidden
1885 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1886 if(!print_hidden[c]) {
1888 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);
1892 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);
1895 // print the begin of row data
1896 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1899 // -------------------------------------------------------------------------
1902 int annotate_reset = 0;
1903 int annotation_count = 0;
1905 long t = rrdset_time2slot(st, before),
1906 stop_at_t = rrdset_time2slot(st, after),
1911 time_t now = rrdset_slot2time(st, t),
1912 dt = st->update_every;
1914 long count = 0, printed = 0, group_count = 0;
1917 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"
1924 , (uint32_t)rrdset_first_entry_t(st)
1925 , (uint32_t)rrdset_last_entry_t(st)
1931 for(; !stop_now ; now -= dt, t--, counter++) {
1932 if(t < 0) t = st->entries - 1;
1933 if(t == stop_at_t) stop_now = counter;
1937 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1944 , (group_count + 1 == group)?"PRINT":" - "
1945 , (now >= after && now <= before)?"RANGE":" - "
1949 // make sure we return data in the proper time range
1950 if(now > before) continue;
1951 if(now < after) break;
1953 //if(rrdset_slot2time(st, t) != now)
1954 // 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));
1959 // check if we have to print this now
1960 if(group_count == group) {
1961 if(printed >= points) {
1962 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1966 // generate the local date time
1967 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1968 if(!tm) { error("localtime() failed."); continue; }
1969 if(now > last_timestamp) last_timestamp = now;
1971 if(printed) buffer_strcat(wb, "]},\n");
1972 buffer_strcat(wb, pre_date);
1973 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1974 buffer_strcat(wb, post_date);
1979 // do the calculations
1980 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1981 storage_number n = rd->values[t];
1982 calculated_number value = unpack_storage_number(n);
1984 if(!does_storage_number_exist(n)) {
1986 found_non_existing[c]++;
1988 if(did_storage_number_reset(n)) annotate_reset = 1;
1990 switch(group_method) {
1992 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1996 group_values[c] += value;
2001 group_values[c] += value;
2002 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2008 if(annotate_reset) {
2010 buffer_strcat(wb, overflow_annotation);
2014 buffer_strcat(wb, normal_annotation);
2017 for(c = 0 ; c < dimensions ; c++) {
2018 if(found_non_existing[c] == group_count) {
2019 // all entries are non-existing
2021 buffer_strcat(wb, pre_value);
2022 buffer_strcat(wb, "null");
2023 buffer_strcat(wb, post_value);
2025 else if(!print_hidden[c]) {
2027 buffer_strcat(wb, pre_value);
2028 buffer_rrd_value(wb, group_values[c]);
2029 buffer_strcat(wb, post_value);
2031 if(group_values[c]) found_non_zero[c]++;
2034 // reset them for the next loop
2035 group_values[c] = 0;
2036 found_non_existing[c] = 0;
2039 // if all dimensions are hidden, print a null
2041 buffer_strcat(wb, pre_value);
2042 buffer_strcat(wb, "null");
2043 buffer_strcat(wb, post_value);
2051 if(printed) buffer_strcat(wb, "]}");
2052 buffer_strcat(wb, "\n ]\n}\n");
2054 if(only_non_zero && max_loop > 1) {
2056 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2057 group_values[c] = 0;
2058 found_non_existing[c] = 0;
2060 if(!print_hidden[c] && !found_non_zero[c]) {
2062 print_hidden[c] = 1;
2066 if(changed) buffer_flush(wb);
2073 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
2075 pthread_rwlock_unlock(&st->rwlock);
2076 return last_timestamp;