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 long 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 // find it and enable it
381 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
382 if(!strcmp(d->name, tok)) {
383 r->od[c] &= ~RRDR_HIDDEN;
385 // since the user needs this dimension
386 // make it appear as NONZERO, to return it
387 // even if the dimension has only zeros
388 r->od[c] |= RRDR_NONZERO;
394 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
397 case DATASOURCE_JSON:
398 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
401 case DATASOURCE_DATATABLE_JSON:
402 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
405 case DATASOURCE_DATATABLE_JSONP:
406 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
409 case DATASOURCE_JSONP:
410 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
414 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
418 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
422 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
425 case DATASOURCE_HTML:
426 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
429 case DATASOURCE_JS_ARRAY:
430 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
433 case DATASOURCE_SSV_COMMA:
434 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
438 buffer_strcat(wb, "unknown");
443 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
445 if(options & RRDR_OPTION_NONZERO) {
449 // the caller wants specific dimensions
450 // disable NONZERO option
451 // to make sure we don't accidentally prevent
452 // the specific dimensions from being returned
456 // find how many dimensions are not zero
459 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
460 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
461 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
466 // if with nonzero we get i = 0 (no dimensions will be returned)
467 // disable nonzero to show all dimensions
468 if(!i) options &= ~RRDR_OPTION_NONZERO;
474 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
476 long rows = rrdr_rows(r);
480 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
481 char kq[2] = "", // key quote
482 sq[2] = ""; // string quote
484 if( options & RRDR_OPTION_GOOGLE_JSON ) {
493 buffer_sprintf(wb, "{\n"
496 " %sname%s: %s%s%s,\n"
497 " %sview_update_every%s: %d,\n"
498 " %supdate_every%s: %d,\n"
499 " %sfirst_entry%s: %u,\n"
500 " %slast_entry%s: %u,\n"
503 " %sdimension_names%s: ["
505 , kq, kq, sq, r->st->id, sq
506 , kq, kq, sq, r->st->name, sq
507 , kq, kq, r->update_every
508 , kq, kq, r->st->update_every
509 , kq, kq, rrdset_first_entry_t(r->st)
510 , kq, kq, rrdset_last_entry_t(r->st)
515 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
516 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
517 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
519 if(i) buffer_strcat(wb, ", ");
520 buffer_strcat(wb, sq);
521 buffer_strcat(wb, rd->name);
522 buffer_strcat(wb, sq);
526 #ifdef NETDATA_INTERNAL_CHECKS
527 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
530 buffer_strcat(wb, sq);
531 buffer_strcat(wb, "no data");
532 buffer_strcat(wb, sq);
535 buffer_sprintf(wb, "],\n"
536 " %sdimension_ids%s: ["
539 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
540 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
541 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
543 if(i) buffer_strcat(wb, ", ");
544 buffer_strcat(wb, sq);
545 buffer_strcat(wb, rd->id);
546 buffer_strcat(wb, sq);
551 buffer_strcat(wb, sq);
552 buffer_strcat(wb, "no data");
553 buffer_strcat(wb, sq);
556 buffer_sprintf(wb, "],\n"
557 " %slatest_values%s: ["
560 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
561 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
562 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
564 if(i) buffer_strcat(wb, ", ");
567 storage_number n = rd->values[rrdset_last_slot(r->st)];
569 if(!does_storage_number_exist(n))
570 buffer_strcat(wb, "null");
572 buffer_rrd_value(wb, unpack_storage_number(n));
576 buffer_strcat(wb, "null");
579 buffer_sprintf(wb, "],\n"
580 " %sview_latest_values%s: ["
585 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
586 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
587 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
589 if(i) buffer_strcat(wb, ", ");
592 calculated_number *cn = &r->v[ (0) * r->d ];
593 uint8_t *co = &r->o[ (0) * r->d ];
595 if(co[c] & RRDR_EMPTY)
596 buffer_strcat(wb, "null");
598 buffer_rrd_value(wb, cn[c]);
603 buffer_strcat(wb, "null");
606 buffer_sprintf(wb, "],\n"
607 " %sdimensions%s: %d,\n"
615 rrdr_buffer_print_format(wb, format);
617 buffer_sprintf(wb, "%s,\n"
623 if(string_value) buffer_strcat(wb, sq);
624 //info("JSONWRAPPER(): %s: END", r->st->id);
627 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
632 char kq[2] = "", // key quote
633 sq[2] = ""; // string quote
635 if( options & RRDR_OPTION_GOOGLE_JSON ) {
644 if(string_value) buffer_strcat(wb, sq);
646 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
647 buffer_rrd_value(wb, r->min);
648 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
649 buffer_rrd_value(wb, r->max);
650 buffer_strcat(wb, "\n}\n");
653 #define JSON_DATES_JS 1
654 #define JSON_DATES_TIMESTAMP 2
656 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
658 //info("RRD2JSON(): %s: BEGIN", r->st->id);
659 int row_annotations = 0, dates, dates_with_new = 0;
660 char kq[2] = "", // key quote
661 sq[2] = "", // string quote
662 pre_label[101] = "", // before each label
663 post_label[101] = "", // after each label
664 pre_date[101] = "", // the beginning of line, to the date
665 post_date[101] = "", // closing the date
666 pre_value[101] = "", // before each value
667 post_value[101] = "", // after each value
668 post_line[101] = "", // at the end of each row
669 normal_annotation[201] = "", // default row annotation
670 overflow_annotation[201] = "", // overflow row annotation
671 data_begin[101] = "", // between labels and values
672 finish[101] = ""; // at the end of everything
675 dates = JSON_DATES_JS;
676 if( options & RRDR_OPTION_GOOGLE_JSON ) {
685 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
686 snprintfz(post_date, 100, "%s}", sq);
687 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
688 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
689 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
690 strcpy(post_value, "}");
691 strcpy(post_line, "]}");
692 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
693 strcpy(finish, "\n ]\n}");
695 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);
696 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
698 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq, kq, kq);
699 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);
700 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);
701 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);
703 // remove the valueobjects flag
704 // google wants its own keys
705 if(options & RRDR_OPTION_OBJECTSROWS)
706 options &= ~RRDR_OPTION_OBJECTSROWS;
711 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
712 dates = JSON_DATES_TIMESTAMP;
716 dates = JSON_DATES_JS;
719 if( options & RRDR_OPTION_OBJECTSROWS )
720 strcpy(pre_date, " { ");
722 strcpy(pre_date, " [ ");
723 strcpy(pre_label, ", \"");
724 strcpy(post_label, "\"");
725 strcpy(pre_value, ", ");
726 if( options & RRDR_OPTION_OBJECTSROWS )
727 strcpy(post_line, "}");
729 strcpy(post_line, "]");
730 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
731 strcpy(finish, "\n ]\n}");
733 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
734 buffer_sprintf(wb, "%stime%s", sq, sq);
737 // -------------------------------------------------------------------------
738 // print the JSON header
743 // print the header lines
744 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
745 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
746 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
748 buffer_strcat(wb, pre_label);
749 buffer_strcat(wb, rd->name);
750 buffer_strcat(wb, post_label);
754 buffer_strcat(wb, pre_label);
755 buffer_strcat(wb, "no data");
756 buffer_strcat(wb, post_label);
759 // print the begin of row data
760 buffer_strcat(wb, data_begin);
762 // if all dimensions are hidden, print a null
764 buffer_strcat(wb, finish);
768 long start = 0, end = rrdr_rows(r), step = 1;
769 if((options & RRDR_OPTION_REVERSED)) {
770 start = rrdr_rows(r) - 1;
775 // for each line in the array
776 calculated_number total = 1;
777 for(i = start; i != end ;i += step) {
778 calculated_number *cn = &r->v[ i * r->d ];
779 uint8_t *co = &r->o[ i * r->d ];
781 time_t now = r->t[i];
783 if(dates == JSON_DATES_JS) {
784 // generate the local date time
785 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
786 if(!tm) { error("localtime_r() failed."); continue; }
788 if(likely(i != start)) buffer_strcat(wb, ",\n");
789 buffer_strcat(wb, pre_date);
791 if( options & RRDR_OPTION_OBJECTSROWS )
792 buffer_sprintf(wb, "%stime%s: ", kq, kq);
795 buffer_strcat(wb, "new ");
797 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
799 buffer_strcat(wb, post_date);
801 if(row_annotations) {
802 // google supports one annotation per row
803 int annotation_found = 0;
804 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
805 if(co[c] & RRDR_RESET) {
806 buffer_strcat(wb, overflow_annotation);
807 annotation_found = 1;
811 if(!annotation_found)
812 buffer_strcat(wb, normal_annotation);
816 // print the timestamp of the line
817 if(likely(i != start)) buffer_strcat(wb, ",\n");
818 buffer_strcat(wb, pre_date);
820 if( options & RRDR_OPTION_OBJECTSROWS )
821 buffer_sprintf(wb, "%stime%s: ", kq, kq);
823 buffer_rrd_value(wb, (calculated_number)r->t[i]);
825 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
827 buffer_strcat(wb, post_date);
830 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
832 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
833 calculated_number n = cn[c];
835 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
840 // prevent a division by zero
841 if(total == 0) total = 1;
844 // for each dimension
845 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
846 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
847 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
849 calculated_number n = cn[c];
851 buffer_strcat(wb, pre_value);
853 if( options & RRDR_OPTION_OBJECTSROWS )
854 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
856 if(co[c] & RRDR_EMPTY) {
857 if(options & RRDR_OPTION_NULL2ZERO)
858 buffer_strcat(wb, "0");
860 buffer_strcat(wb, "null");
863 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
866 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
869 buffer_rrd_value(wb, n);
872 buffer_strcat(wb, post_value);
875 buffer_strcat(wb, post_line);
878 buffer_strcat(wb, finish);
879 //info("RRD2JSON(): %s: END", r->st->id);
882 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
884 //info("RRD2CSV(): %s: BEGIN", r->st->id);
888 // print the csv header
889 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
890 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
891 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
894 buffer_strcat(wb, startline);
895 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
896 buffer_strcat(wb, "time");
897 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
899 buffer_strcat(wb, separator);
900 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
901 buffer_strcat(wb, d->name);
902 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
905 buffer_strcat(wb, endline);
908 // no dimensions present
912 long start = 0, end = rrdr_rows(r), step = 1;
913 if((options & RRDR_OPTION_REVERSED)) {
914 start = rrdr_rows(r) - 1;
919 // for each line in the array
920 calculated_number total = 1;
921 for(i = start; i != end ;i += step) {
922 calculated_number *cn = &r->v[ i * r->d ];
923 uint8_t *co = &r->o[ i * r->d ];
925 buffer_strcat(wb, betweenlines);
926 buffer_strcat(wb, startline);
928 time_t now = r->t[i];
930 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
931 // print the timestamp of the line
932 buffer_rrd_value(wb, (calculated_number)now);
934 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
937 // generate the local date time
938 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
939 if(!tm) { error("localtime() failed."); continue; }
940 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
943 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
945 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
946 calculated_number n = cn[c];
948 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
953 // prevent a division by zero
954 if(total == 0) total = 1;
957 // for each dimension
958 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
959 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
960 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
962 buffer_strcat(wb, separator);
964 calculated_number n = cn[c];
966 if(co[c] & RRDR_EMPTY) {
967 if(options & RRDR_OPTION_NULL2ZERO)
968 buffer_strcat(wb, "0");
970 buffer_strcat(wb, "null");
973 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
976 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
979 buffer_rrd_value(wb, n);
983 buffer_strcat(wb, endline);
985 //info("RRD2CSV(): %s: END", r->st->id);
988 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
992 calculated_number *cn = &r->v[ i * r->d ];
993 uint8_t *co = &r->o[ i * r->d ];
995 calculated_number sum = 0, min = 0, max = 0, v;
996 int all_null = 1, init = 1;
998 calculated_number total = 1;
999 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1001 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1002 calculated_number n = cn[c];
1004 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1009 // prevent a division by zero
1010 if(total == 0) total = 1;
1013 // for each dimension
1014 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1015 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1016 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1018 calculated_number n = cn[c];
1020 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1023 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1024 n = n * 100 / total;
1026 if(unlikely(init)) {
1038 if(likely(!(co[c] & RRDR_EMPTY))) {
1043 if(n < min) min = n;
1044 if(n > max) max = n;
1047 if(unlikely(all_null)) {
1048 if(likely(*all_values_are_null))
1049 *all_values_are_null = 1;
1053 if(likely(*all_values_are_null))
1054 *all_values_are_null = 0;
1057 if(options & RRDR_OPTION_MIN2MAX)
1065 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1067 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1070 buffer_strcat(wb, prefix);
1071 long start = 0, end = rrdr_rows(r), step = 1;
1072 if((options & RRDR_OPTION_REVERSED)) {
1073 start = rrdr_rows(r) - 1;
1078 // for each line in the array
1079 for(i = start; i != end ;i += step) {
1080 int all_values_are_null = 0;
1081 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1083 if(likely(i != start)) {
1084 if(r->min > v) r->min = v;
1085 if(r->max < v) r->max = v;
1092 if(likely(i != start))
1093 buffer_strcat(wb, separator);
1095 if(all_values_are_null) {
1096 if(options & RRDR_OPTION_NULL2ZERO)
1097 buffer_strcat(wb, "0");
1099 buffer_strcat(wb, "null");
1102 buffer_rrd_value(wb, v);
1104 buffer_strcat(wb, suffix);
1105 //info("RRD2SSV(): %s: END", r->st->id);
1108 inline static calculated_number *rrdr_line_values(RRDR *r)
1110 return &r->v[ r->c * r->d ];
1113 inline static uint8_t *rrdr_line_options(RRDR *r)
1115 return &r->o[ r->c * r->d ];
1118 inline static int rrdr_line_init(RRDR *r, time_t t)
1122 if(unlikely(r->c >= r->n)) {
1123 error("requested to step above RRDR size for chart %s", r->st->name);
1133 inline static void rrdr_lock_rrdset(RRDR *r) {
1135 error("NULL value given!");
1139 pthread_rwlock_rdlock(&r->st->rwlock);
1143 inline static void rrdr_unlock_rrdset(RRDR *r) {
1145 error("NULL value given!");
1149 if(likely(r->has_st_lock)) {
1150 pthread_rwlock_unlock(&r->st->rwlock);
1155 inline static void rrdr_free(RRDR *r)
1158 error("NULL value given!");
1162 rrdr_unlock_rrdset(r);
1163 if(likely(r->t)) free(r->t);
1164 if(likely(r->v)) free(r->v);
1165 if(likely(r->o)) free(r->o);
1166 if(likely(r->od)) free(r->od);
1170 inline void rrdr_done(RRDR *r)
1176 static RRDR *rrdr_create(RRDSET *st, long n)
1179 error("NULL value given!");
1183 RRDR *r = calloc(1, sizeof(RRDR));
1184 if(unlikely(!r)) goto cleanup;
1188 rrdr_lock_rrdset(r);
1191 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1195 r->t = malloc(n * sizeof(time_t));
1196 if(unlikely(!r->t)) goto cleanup;
1198 r->v = malloc(n * r->d * sizeof(calculated_number));
1199 if(unlikely(!r->v)) goto cleanup;
1201 r->o = malloc(n * r->d * sizeof(uint8_t));
1202 if(unlikely(!r->o)) goto cleanup;
1204 r->od = malloc(r->d * sizeof(uint8_t));
1205 if(unlikely(!r->od)) goto cleanup;
1207 // set the hidden flag on hidden dimensions
1209 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1210 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1217 r->update_every = 1;
1222 error("Cannot allocate RRDR memory for %d entries", n);
1223 if(likely(r)) rrdr_free(r);
1227 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1229 int debug = st->debug;
1230 int absolute_period_requested = -1;
1232 time_t first_entry_t = rrdset_first_entry_t(st);
1233 time_t last_entry_t = rrdset_last_entry_t(st);
1235 if(before == 0 && after == 0) {
1236 before = last_entry_t;
1237 after = first_entry_t;
1238 absolute_period_requested = 0;
1241 // allow relative for before and after
1242 if(((before < 0)?-before:before) <= (st->update_every * st->entries)) {
1243 before = last_entry_t + before;
1244 absolute_period_requested = 0;
1247 if(((after < 0)?-after:after) <= (st->update_every * st->entries)) {
1248 if(after == 0) after = -st->update_every;
1249 after = before + after;
1250 absolute_period_requested = 0;
1253 if(absolute_period_requested == -1)
1254 absolute_period_requested = 1;
1256 // make sure they are within our timeframe
1257 if(before > last_entry_t) before = last_entry_t;
1258 if(before < first_entry_t) before = first_entry_t;
1260 if(after > last_entry_t) after = last_entry_t;
1261 if(after < first_entry_t) after = first_entry_t;
1263 // check if they are upside down
1264 if(after > before) {
1265 time_t tmp = before;
1270 // the duration of the chart
1271 time_t duration = before - after;
1272 long available_points = duration / st->update_every;
1274 if(duration <= 0 || available_points <= 0)
1275 return rrdr_create(st, 1);
1277 // check the wanted points
1278 if(points < 0) points = -points;
1279 if(points > available_points) points = available_points;
1280 if(points == 0) points = available_points;
1282 // calculate proper grouping of source data
1283 long group = available_points / points;
1284 if(group <= 0) group = 1;
1286 // round group to the closest integer
1287 if(available_points % points > points / 2) group++;
1289 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1290 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1291 long points_new = (before_new - after_new) / st->update_every / group;
1293 // find the starting and ending slots in our round robin db
1294 long start_at_slot = rrdset_time2slot(st, before_new),
1295 stop_at_slot = rrdset_time2slot(st, after_new);
1297 #ifdef NETDATA_INTERNAL_CHECKS
1298 if(after_new < first_entry_t) {
1299 error("after_new %u is too small, minimum %u", after_new, first_entry_t);
1301 if(after_new > last_entry_t) {
1302 error("after_new %u is too big, maximum %u", after_new, last_entry_t);
1304 if(before_new < first_entry_t) {
1305 error("before_new %u is too small, minimum %u", before_new, first_entry_t);
1307 if(before_new > last_entry_t) {
1308 error("before_new %u is too big, maximum %u", before_new, last_entry_t);
1310 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1311 error("start_at_slot is invalid %ld, expected %ld to %ld", start_at_slot, 0, st->entries - 1);
1313 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1314 error("stop_at_slot is invalid %ld, expected %ld to %ld", stop_at_slot, 0, st->entries - 1);
1316 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1317 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1321 //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);
1324 before = before_new;
1325 duration = before - after;
1326 points = points_new;
1329 // before = the end time of the calculation
1330 // after = the start time of the calculation
1331 // duration = the duration of the calculation
1332 // group = the number of source points to aggregate / group together
1333 // method = the method of grouping source points
1334 // points = the number of points to generate
1337 // -------------------------------------------------------------------------
1338 // initialize our result set
1340 RRDR *r = rrdr_create(st, points);
1342 #ifdef NETDATA_INTERNAL_CHECKS
1343 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1348 #ifdef NETDATA_INTERNAL_CHECKS
1349 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1354 if(absolute_period_requested == 1)
1355 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1357 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1359 // find how many dimensions we have
1360 long dimensions = r->d;
1363 // -------------------------------------------------------------------------
1364 // checks for debugging
1366 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"
1370 , last_entry_t - first_entry_t
1379 // -------------------------------------------------------------------------
1380 // temp arrays for keeping values per dimension
1382 calculated_number last_values[dimensions]; // keep the last value of each dimension
1383 calculated_number group_values[dimensions]; // keep sums when grouping
1384 long group_counts[dimensions]; // keep the number of values added to group_values
1385 uint8_t group_options[dimensions];
1386 uint8_t found_non_zero[dimensions];
1392 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1394 group_values[c] = 0;
1395 group_counts[c] = 0;
1396 group_options[c] = 0;
1397 found_non_zero[c] = 0;
1401 // -------------------------------------------------------------------------
1404 time_t now = rrdset_slot2time(st, start_at_slot),
1405 dt = st->update_every,
1408 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"
1420 r->update_every = group * st->update_every;
1424 //info("RRD2RRDR(): %s: STARTING", st->id);
1426 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1427 for(; !stop_now ; now -= dt, slot--, counter++) {
1428 if(unlikely(slot < 0)) slot = st->entries - 1;
1429 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1431 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1438 , (group_count + 1 == group)?"PRINT":" - "
1439 , (now >= after && now <= before)?"RANGE":" - "
1442 // make sure we return data in the proper time range
1443 if(unlikely(now > before)) continue;
1444 if(unlikely(now < after)) break;
1446 if(unlikely(group_count == 0)) {
1447 group_start_t = now;
1451 if(unlikely(group_count == group)) {
1452 if(unlikely(added >= points)) break;
1456 // do the calculations
1457 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1458 storage_number n = rd->values[slot];
1459 if(unlikely(!does_storage_number_exist(n))) continue;
1463 calculated_number value = unpack_storage_number(n);
1464 if(likely(value != 0.0)) {
1465 group_options[c] |= RRDR_NONZERO;
1466 found_non_zero[c] = 1;
1469 if(unlikely(did_storage_number_reset(n)))
1470 group_options[c] |= RRDR_RESET;
1472 switch(group_method) {
1474 if(unlikely(fabsl(value) > fabsl(group_values[c])))
1475 group_values[c] = value;
1481 group_values[c] += value;
1484 case GROUP_INCREMENTAL_SUM:
1485 if(unlikely(slot == start_at_slot))
1486 last_values[c] = value;
1488 group_values[c] = value - last_values[c];
1489 last_values[c] = value;
1495 if(unlikely(add_this)) {
1496 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1500 calculated_number *cn = rrdr_line_values(r);
1501 uint8_t *co = rrdr_line_options(r);
1503 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1505 // update the dimension options
1506 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1508 // store the specific point options
1509 co[c] = group_options[c];
1512 if(unlikely(group_counts[c] == 0)) {
1514 co[c] |= RRDR_EMPTY;
1516 else if(unlikely(group_method == GROUP_AVERAGE)) {
1518 cn[c] = group_values[c] / group_counts[c];
1523 // GROUP_INCREMENTAL_SUM
1524 cn[c] = group_values[c];
1527 if(cn[c] < r->min) r->min = cn[c];
1528 if(cn[c] > r->max) r->max = cn[c];
1530 // reset them for the next loop
1531 group_values[c] = 0;
1532 group_counts[c] = 0;
1533 group_options[c] = 0;
1543 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1544 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1548 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)
1550 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1552 if(value_is_null) *value_is_null = 1;
1556 if(rrdr_rows(r) == 0) {
1558 if(value_is_null) *value_is_null = 1;
1562 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1563 wb->options |= WB_CONTENT_NO_CACHEABLE;
1564 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1565 wb->options |= WB_CONTENT_CACHEABLE;
1567 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1570 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1572 if(latest_timestamp)
1573 *latest_timestamp = r->before;
1575 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1576 *n = rrdr2value(r, i, options, value_is_null);
1582 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)
1584 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1586 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1590 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1591 wb->options |= WB_CONTENT_NO_CACHEABLE;
1592 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1593 wb->options |= WB_CONTENT_CACHEABLE;
1595 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1598 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1600 if(latest_timestamp && rrdr_rows(r) > 0)
1601 *latest_timestamp = r->before;
1604 case DATASOURCE_SSV:
1605 if(options & RRDR_OPTION_JSON_WRAP) {
1606 wb->contenttype = CT_APPLICATION_JSON;
1607 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1608 rrdr2ssv(r, wb, options, "", " ", "");
1609 rrdr_json_wrapper_end(r, wb, format, options, 1);
1612 wb->contenttype = CT_TEXT_PLAIN;
1613 rrdr2ssv(r, wb, options, "", " ", "");
1617 case DATASOURCE_SSV_COMMA:
1618 if(options & RRDR_OPTION_JSON_WRAP) {
1619 wb->contenttype = CT_APPLICATION_JSON;
1620 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1621 rrdr2ssv(r, wb, options, "", ",", "");
1622 rrdr_json_wrapper_end(r, wb, format, options, 1);
1625 wb->contenttype = CT_TEXT_PLAIN;
1626 rrdr2ssv(r, wb, options, "", ",", "");
1630 case DATASOURCE_JS_ARRAY:
1631 if(options & RRDR_OPTION_JSON_WRAP) {
1632 wb->contenttype = CT_APPLICATION_JSON;
1633 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1634 rrdr2ssv(r, wb, options, "[", ",", "]");
1635 rrdr_json_wrapper_end(r, wb, format, options, 0);
1638 wb->contenttype = CT_APPLICATION_JSON;
1639 rrdr2ssv(r, wb, options, "[", ",", "]");
1643 case DATASOURCE_CSV:
1644 if(options & RRDR_OPTION_JSON_WRAP) {
1645 wb->contenttype = CT_APPLICATION_JSON;
1646 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1647 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1648 rrdr_json_wrapper_end(r, wb, format, options, 1);
1651 wb->contenttype = CT_TEXT_PLAIN;
1652 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1656 case DATASOURCE_CSV_JSON_ARRAY:
1657 wb->contenttype = CT_APPLICATION_JSON;
1658 if(options & RRDR_OPTION_JSON_WRAP) {
1659 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1660 buffer_strcat(wb, "[\n");
1661 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1662 buffer_strcat(wb, "\n]");
1663 rrdr_json_wrapper_end(r, wb, format, options, 0);
1666 wb->contenttype = CT_TEXT_PLAIN;
1667 buffer_strcat(wb, "[\n");
1668 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1669 buffer_strcat(wb, "\n]");
1673 case DATASOURCE_TSV:
1674 if(options & RRDR_OPTION_JSON_WRAP) {
1675 wb->contenttype = CT_APPLICATION_JSON;
1676 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1677 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1678 rrdr_json_wrapper_end(r, wb, format, options, 1);
1681 wb->contenttype = CT_TEXT_PLAIN;
1682 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1686 case DATASOURCE_HTML:
1687 if(options & RRDR_OPTION_JSON_WRAP) {
1688 wb->contenttype = CT_APPLICATION_JSON;
1689 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1690 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1691 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1692 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1693 rrdr_json_wrapper_end(r, wb, format, options, 1);
1696 wb->contenttype = CT_TEXT_HTML;
1697 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1698 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1699 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1703 case DATASOURCE_DATATABLE_JSONP:
1704 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1706 if(options & RRDR_OPTION_JSON_WRAP)
1707 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1709 rrdr2json(r, wb, options, 1);
1711 if(options & RRDR_OPTION_JSON_WRAP)
1712 rrdr_json_wrapper_end(r, wb, format, options, 0);
1715 case DATASOURCE_DATATABLE_JSON:
1716 wb->contenttype = CT_APPLICATION_JSON;
1718 if(options & RRDR_OPTION_JSON_WRAP)
1719 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1721 rrdr2json(r, wb, options, 1);
1723 if(options & RRDR_OPTION_JSON_WRAP)
1724 rrdr_json_wrapper_end(r, wb, format, options, 0);
1727 case DATASOURCE_JSONP:
1728 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1729 if(options & RRDR_OPTION_JSON_WRAP)
1730 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1732 rrdr2json(r, wb, options, 0);
1734 if(options & RRDR_OPTION_JSON_WRAP)
1735 rrdr_json_wrapper_end(r, wb, format, options, 0);
1738 case DATASOURCE_JSON:
1740 wb->contenttype = CT_APPLICATION_JSON;
1742 if(options & RRDR_OPTION_JSON_WRAP)
1743 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1745 rrdr2json(r, wb, options, 0);
1747 if(options & RRDR_OPTION_JSON_WRAP)
1748 rrdr_json_wrapper_end(r, wb, format, options, 0);
1756 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)
1759 pthread_rwlock_rdlock(&st->rwlock);
1762 // -------------------------------------------------------------------------
1763 // switch from JSON to google JSON
1768 case DATASOURCE_DATATABLE_JSON:
1769 case DATASOURCE_DATATABLE_JSONP:
1774 case DATASOURCE_JSON:
1780 // -------------------------------------------------------------------------
1781 // validate the parameters
1783 if(points < 1) points = 1;
1784 if(group < 1) group = 1;
1786 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1787 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1791 // our return value (the last timestamp printed)
1792 // this is required to detect re-transmit in google JSONP
1793 time_t last_timestamp = 0;
1796 // -------------------------------------------------------------------------
1797 // find how many dimensions we have
1801 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1803 pthread_rwlock_unlock(&st->rwlock);
1804 buffer_strcat(wb, "No dimensions yet.");
1809 // -------------------------------------------------------------------------
1810 // prepare various strings, to speed up the loop
1812 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);
1813 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1814 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1815 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1816 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1817 char post_value[21]; strcpy(post_value, "}");
1820 // -------------------------------------------------------------------------
1821 // checks for debugging
1824 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"
1826 , rrdset_first_entry_t(st)
1827 , rrdset_last_entry_t(st)
1828 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1837 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1839 if((before - after) > st->entries * st->update_every)
1840 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);
1844 // -------------------------------------------------------------------------
1845 // temp arrays for keeping values per dimension
1847 calculated_number group_values[dimensions]; // keep sums when grouping
1848 int print_hidden[dimensions]; // keep hidden flags
1849 int found_non_zero[dimensions];
1850 int found_non_existing[dimensions];
1853 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1854 group_values[c] = 0;
1855 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1856 found_non_zero[c] = 0;
1857 found_non_existing[c] = 0;
1861 // 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);
1862 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1863 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1865 // -------------------------------------------------------------------------
1866 // remove dimensions that contain only zeros
1869 if(only_non_zero) max_loop = 2;
1871 for(; max_loop ; max_loop--) {
1873 // -------------------------------------------------------------------------
1874 // print the JSON header
1876 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1877 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);
1878 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);
1879 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);
1881 // print the header for each dimension
1882 // and update the print_hidden array for the dimensions that should be hidden
1884 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1885 if(!print_hidden[c]) {
1887 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);
1891 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);
1894 // print the begin of row data
1895 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1898 // -------------------------------------------------------------------------
1901 int annotate_reset = 0;
1902 int annotation_count = 0;
1904 long t = rrdset_time2slot(st, before),
1905 stop_at_t = rrdset_time2slot(st, after),
1910 time_t now = rrdset_slot2time(st, t),
1911 dt = st->update_every;
1913 long count = 0, printed = 0, group_count = 0;
1916 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"
1923 , rrdset_first_entry_t(st)
1924 , rrdset_last_entry_t(st)
1930 for(; !stop_now ; now -= dt, t--, counter++) {
1931 if(t < 0) t = st->entries - 1;
1932 if(t == stop_at_t) stop_now = counter;
1936 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1943 , (group_count + 1 == group)?"PRINT":" - "
1944 , (now >= after && now <= before)?"RANGE":" - "
1948 // make sure we return data in the proper time range
1949 if(now > before) continue;
1950 if(now < after) break;
1952 //if(rrdset_slot2time(st, t) != now)
1953 // 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));
1958 // check if we have to print this now
1959 if(group_count == group) {
1960 if(printed >= points) {
1961 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1965 // generate the local date time
1966 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1967 if(!tm) { error("localtime() failed."); continue; }
1968 if(now > last_timestamp) last_timestamp = now;
1970 if(printed) buffer_strcat(wb, "]},\n");
1971 buffer_strcat(wb, pre_date);
1972 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1973 buffer_strcat(wb, post_date);
1978 // do the calculations
1979 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1980 storage_number n = rd->values[t];
1981 calculated_number value = unpack_storage_number(n);
1983 if(!does_storage_number_exist(n)) {
1985 found_non_existing[c]++;
1987 if(did_storage_number_reset(n)) annotate_reset = 1;
1989 switch(group_method) {
1991 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1995 group_values[c] += value;
2000 group_values[c] += value;
2001 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2007 if(annotate_reset) {
2009 buffer_strcat(wb, overflow_annotation);
2013 buffer_strcat(wb, normal_annotation);
2016 for(c = 0 ; c < dimensions ; c++) {
2017 if(found_non_existing[c] == group_count) {
2018 // all entries are non-existing
2020 buffer_strcat(wb, pre_value);
2021 buffer_strcat(wb, "null");
2022 buffer_strcat(wb, post_value);
2024 else if(!print_hidden[c]) {
2026 buffer_strcat(wb, pre_value);
2027 buffer_rrd_value(wb, group_values[c]);
2028 buffer_strcat(wb, post_value);
2030 if(group_values[c]) found_non_zero[c]++;
2033 // reset them for the next loop
2034 group_values[c] = 0;
2035 found_non_existing[c] = 0;
2038 // if all dimensions are hidden, print a null
2040 buffer_strcat(wb, pre_value);
2041 buffer_strcat(wb, "null");
2042 buffer_strcat(wb, post_value);
2050 if(printed) buffer_strcat(wb, "]}");
2051 buffer_strcat(wb, "\n ]\n}\n");
2053 if(only_non_zero && max_loop > 1) {
2055 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2056 group_values[c] = 0;
2057 found_non_existing[c] = 0;
2059 if(!print_hidden[c] && !found_non_zero[c]) {
2061 print_hidden[c] = 1;
2065 if(changed) buffer_flush(wb);
2072 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
2074 pthread_rwlock_unlock(&st->rwlock);
2075 return last_timestamp;