13 #define HOSTNAME_MAX 1024
14 char *hostname = "unknown";
16 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
18 pthread_rwlock_rdlock(&st->rwlock);
22 "\t\t\t\"id\": \"%s\",\n"
23 "\t\t\t\"name\": \"%s\",\n"
24 "\t\t\t\"type\": \"%s\",\n"
25 "\t\t\t\"family\": \"%s\",\n"
26 "\t\t\t\"title\": \"%s\",\n"
27 "\t\t\t\"priority\": %ld,\n"
28 "\t\t\t\"enabled\": %s,\n"
29 "\t\t\t\"units\": \"%s\",\n"
30 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
31 "\t\t\t\"chart_type\": \"%s\",\n"
32 "\t\t\t\"duration\": %ld,\n"
33 "\t\t\t\"first_entry\": %lu,\n"
34 "\t\t\t\"last_entry\": %lu,\n"
35 "\t\t\t\"update_every\": %d,\n"
36 "\t\t\t\"dimensions\": {\n"
43 , st->enabled?"true":"false"
46 , rrdset_type_name(st->chart_type)
47 , st->entries * st->update_every
48 , rrdset_first_entry_t(st)
49 , rrdset_last_entry_t(st)
53 unsigned long memory = st->memsize;
57 for(rd = st->dimensions; rd ; rd = rd->next) {
58 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
60 memory += rd->memsize;
64 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
78 pthread_rwlock_unlock(&st->rwlock);
81 void rrd_stats_api_v1_charts(BUFFER *wb)
86 buffer_sprintf(wb, "{\n"
87 "\t\"hostname\": \"%s\""
88 ",\n\t\"update_every\": %d"
89 ",\n\t\"history\": %d"
93 , rrd_default_history_entries
96 pthread_rwlock_rdlock(&rrdset_root_rwlock);
97 for(st = rrdset_root, c = 0; st ; st = st->next) {
99 if(c) buffer_strcat(wb, ",");
100 buffer_strcat(wb, "\n\t\t\"");
101 buffer_strcat(wb, st->id);
102 buffer_strcat(wb, "\": ");
103 rrd_stats_api_v1_chart(st, wb);
107 pthread_rwlock_unlock(&rrdset_root_rwlock);
109 buffer_strcat(wb, "\n\t}\n}\n");
113 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
115 time_t now = time(NULL);
117 pthread_rwlock_rdlock(&st->rwlock);
121 "\t\t\t\"id\": \"%s\",\n"
122 "\t\t\t\"name\": \"%s\",\n"
123 "\t\t\t\"type\": \"%s\",\n"
124 "\t\t\t\"family\": \"%s\",\n"
125 "\t\t\t\"title\": \"%s\",\n"
126 "\t\t\t\"priority\": %ld,\n"
127 "\t\t\t\"enabled\": %d,\n"
128 "\t\t\t\"units\": \"%s\",\n"
129 "\t\t\t\"url\": \"/data/%s/%s\",\n"
130 "\t\t\t\"chart_type\": \"%s\",\n"
131 "\t\t\t\"counter\": %ld,\n"
132 "\t\t\t\"entries\": %ld,\n"
133 "\t\t\t\"first_entry_t\": %lu,\n"
134 "\t\t\t\"last_entry\": %ld,\n"
135 "\t\t\t\"last_entry_t\": %lu,\n"
136 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
137 "\t\t\t\"update_every\": %d,\n"
138 "\t\t\t\"isdetail\": %d,\n"
139 "\t\t\t\"usec_since_last_update\": %llu,\n"
140 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
141 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
142 "\t\t\t\"dimensions\": [\n"
151 , st->name, options?options:""
152 , rrdset_type_name(st->chart_type)
155 , rrdset_first_entry_t(st)
156 , rrdset_last_slot(st)
157 , rrdset_last_entry_t(st)
158 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
161 , st->usec_since_last_update
162 , st->collected_total
163 , st->last_collected_total
166 unsigned long memory = st->memsize;
169 for(rd = st->dimensions; rd ; rd = rd->next) {
171 memory += rd->memsize;
175 "\t\t\t\t\t\"id\": \"%s\",\n"
176 "\t\t\t\t\t\"name\": \"%s\",\n"
177 "\t\t\t\t\t\"entries\": %ld,\n"
178 "\t\t\t\t\t\"isHidden\": %d,\n"
179 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
180 "\t\t\t\t\t\"multiplier\": %ld,\n"
181 "\t\t\t\t\t\"divisor\": %ld,\n"
182 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
183 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
184 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
186 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
187 "\t\t\t\t\t\"memory\": %lu\n"
192 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
193 , rrddim_algorithm_name(rd->algorithm)
196 , rd->last_collected_time.tv_sec
197 , rd->collected_value
198 , rd->calculated_value
199 , rd->last_collected_value
200 , rd->last_calculated_value
208 "\t\t\t\"memory\" : %lu\n"
213 pthread_rwlock_unlock(&st->rwlock);
217 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
218 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
220 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
222 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
223 rrd_stats_one_json(st, options, wb);
224 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
227 void rrd_stats_all_json(BUFFER *wb)
229 unsigned long memory = 0;
233 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
235 pthread_rwlock_rdlock(&rrdset_root_rwlock);
236 for(st = rrdset_root, c = 0; st ; st = st->next) {
238 if(c) buffer_strcat(wb, ",\n");
239 memory += rrd_stats_one_json(st, NULL, wb);
243 pthread_rwlock_unlock(&rrdset_root_rwlock);
245 buffer_sprintf(wb, "\n\t],\n"
246 "\t\"hostname\": \"%s\",\n"
247 "\t\"update_every\": %d,\n"
248 "\t\"history\": %d,\n"
249 "\t\"memory\": %lu\n"
253 , rrd_default_history_entries
260 // ----------------------------------------------------------------------------
263 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
264 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
265 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
266 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
269 typedef struct rrdresult {
270 RRDSET *st; // the chart this result refers to
272 int d; // the number of dimensions
273 long n; // the number of values in the arrays
274 long rows; // the number of rows used
276 uint8_t *od; // the options for the dimensions
278 time_t *t; // array of n timestamps
279 calculated_number *v; // array n x d values
280 uint8_t *o; // array n x d options
282 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
284 long group; // how many collected values were grouped for each row
285 long update_every; // what is the suggested update frequency in seconds
287 calculated_number min;
288 calculated_number max;
293 int has_st_lock; // if st is read locked by us
296 #define rrdr_rows(r) ((r)->rows)
299 static void rrdr_dump(RRDR *r)
304 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
306 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
307 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
310 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
311 , (r->od[c] & RRDR_RESET)?"RESET ":""
312 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
313 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
318 fprintf(stderr, "RRDR does not have any values in it.\n");
322 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
324 // for each line in the array
325 for(i = 0; i < r->rows ;i++) {
326 calculated_number *cn = &r->v[ i * r->d ];
327 uint8_t *co = &r->o[ i * r->d ];
329 // print the id and the timestamp of the line
330 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
332 // for each dimension
333 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
334 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
335 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
337 if(co[c] & RRDR_EMPTY)
338 fprintf(stderr, "null ");
340 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
342 , (co[c] & RRDR_EMPTY)?"E":" "
343 , (co[c] & RRDR_RESET)?"R":" "
344 , (co[c] & RRDR_HIDDEN)?"H":" "
345 , (co[c] & RRDR_NONZERO)?"N":" "
349 fprintf(stderr, "\n");
354 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
356 char b[strlen(dims) + 1];
363 // disable all of them
364 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
365 r->od[c] |= RRDR_HIDDEN;
367 while(o && *o && (tok = mystrsep(&o, ", |"))) {
370 // find it and enable it
371 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
372 if(!strcmp(d->name, tok)) {
373 r->od[c] &= ~RRDR_HIDDEN;
375 // since the user needs this dimension
376 // make it appear as NONZERO, to return it
377 // even if the dimension has only zeros
378 r->od[c] |= RRDR_NONZERO;
384 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
387 case DATASOURCE_JSON:
388 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
391 case DATASOURCE_DATATABLE_JSON:
392 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
395 case DATASOURCE_DATATABLE_JSONP:
396 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
399 case DATASOURCE_JSONP:
400 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
404 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
408 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
412 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
415 case DATASOURCE_HTML:
416 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
419 case DATASOURCE_JS_ARRAY:
420 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
423 case DATASOURCE_SSV_COMMA:
424 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
428 buffer_strcat(wb, "unknown");
433 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
435 if(options & RRDR_OPTION_NONZERO) {
439 // the caller wants specific dimensions
440 // disable NONZERO option
441 // to make sure we don't accidentally prevent
442 // the specific dimensions from being returned
446 // find how many dimensions are not zero
449 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
450 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
451 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
456 // if with nonzero we get i = 0 (no dimensions will be returned)
457 // disable nonzero to show all dimensions
458 if(!i) options &= ~RRDR_OPTION_NONZERO;
464 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
466 long rows = rrdr_rows(r);
470 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
471 char kq[2] = "", // key quote
472 sq[2] = ""; // string quote
474 if( options & RRDR_OPTION_GOOGLE_JSON ) {
483 buffer_sprintf(wb, "{\n"
486 " %sname%s: %s%s%s,\n"
487 " %sview_update_every%s: %d,\n"
488 " %supdate_every%s: %d,\n"
489 " %sfirst_entry%s: %u,\n"
490 " %slast_entry%s: %u,\n"
493 " %sdimension_names%s: ["
495 , kq, kq, sq, r->st->id, sq
496 , kq, kq, sq, r->st->name, sq
497 , kq, kq, r->update_every
498 , kq, kq, r->st->update_every
499 , kq, kq, rrdset_first_entry_t(r->st)
500 , kq, kq, rrdset_last_entry_t(r->st)
505 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
506 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
507 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
509 if(i) buffer_strcat(wb, ", ");
510 buffer_strcat(wb, sq);
511 buffer_strcat(wb, rd->name);
512 buffer_strcat(wb, sq);
516 #ifdef NETDATA_INTERNAL_CHECKS
517 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
520 buffer_strcat(wb, sq);
521 buffer_strcat(wb, "no data");
522 buffer_strcat(wb, sq);
525 buffer_sprintf(wb, "],\n"
526 " %sdimension_ids%s: ["
529 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
530 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
531 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
533 if(i) buffer_strcat(wb, ", ");
534 buffer_strcat(wb, sq);
535 buffer_strcat(wb, rd->id);
536 buffer_strcat(wb, sq);
541 buffer_strcat(wb, sq);
542 buffer_strcat(wb, "no data");
543 buffer_strcat(wb, sq);
546 buffer_sprintf(wb, "],\n"
547 " %slatest_values%s: ["
550 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
551 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
552 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
554 if(i) buffer_strcat(wb, ", ");
557 storage_number n = rd->values[rrdset_last_slot(r->st)];
559 if(!does_storage_number_exist(n))
560 buffer_strcat(wb, "null");
562 buffer_rrd_value(wb, unpack_storage_number(n));
566 buffer_strcat(wb, "null");
569 buffer_sprintf(wb, "],\n"
570 " %sview_latest_values%s: ["
575 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
576 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
577 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
579 if(i) buffer_strcat(wb, ", ");
582 calculated_number *cn = &r->v[ (0) * r->d ];
583 uint8_t *co = &r->o[ (0) * r->d ];
585 if(co[c] & RRDR_EMPTY)
586 buffer_strcat(wb, "null");
588 buffer_rrd_value(wb, cn[c]);
593 buffer_strcat(wb, "null");
596 buffer_sprintf(wb, "],\n"
597 " %sdimensions%s: %d,\n"
605 rrdr_buffer_print_format(wb, format);
607 buffer_sprintf(wb, "%s,\n"
613 if(string_value) buffer_strcat(wb, sq);
614 //info("JSONWRAPPER(): %s: END", r->st->id);
617 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
622 char kq[2] = "", // key quote
623 sq[2] = ""; // string quote
625 if( options & RRDR_OPTION_GOOGLE_JSON ) {
634 if(string_value) buffer_strcat(wb, sq);
636 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
637 buffer_rrd_value(wb, r->min);
638 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
639 buffer_rrd_value(wb, r->max);
640 buffer_strcat(wb, "\n}\n");
643 #define JSON_DATES_JS 1
644 #define JSON_DATES_TIMESTAMP 2
646 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
648 //info("RRD2JSON(): %s: BEGIN", r->st->id);
649 int row_annotations = 0, dates, dates_with_new = 0;
650 char kq[2] = "", // key quote
651 sq[2] = "", // string quote
652 pre_label[101] = "", // before each label
653 post_label[101] = "", // after each label
654 pre_date[101] = "", // the beginning of line, to the date
655 post_date[101] = "", // closing the date
656 pre_value[101] = "", // before each value
657 post_value[101] = "", // after each value
658 post_line[101] = "", // at the end of each row
659 normal_annotation[201] = "", // default row annotation
660 overflow_annotation[201] = "", // overflow row annotation
661 data_begin[101] = "", // between labels and values
662 finish[101] = ""; // at the end of everything
665 dates = JSON_DATES_JS;
666 if( options & RRDR_OPTION_GOOGLE_JSON ) {
675 snprintf(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
676 snprintf(post_date, 100, "%s}", sq);
677 snprintf(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
678 snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
679 snprintf(pre_value, 100, ",{%sv%s:", kq, kq);
680 snprintf(post_value, 100, "}");
681 snprintf(post_line, 100, "]}");
682 snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
683 snprintf(finish, 100, "\n ]\n}");
685 snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
686 snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
688 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq, kq, kq);
689 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);
690 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);
691 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);
693 // remove the valueobjects flag
694 // google wants its own keys
695 if(options & RRDR_OPTION_OBJECTSROWS)
696 options &= ~RRDR_OPTION_OBJECTSROWS;
701 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
702 dates = JSON_DATES_TIMESTAMP;
706 dates = JSON_DATES_JS;
709 if( options & RRDR_OPTION_OBJECTSROWS )
710 snprintf(pre_date, 100, " { ");
712 snprintf(pre_date, 100, " [ ");
713 snprintf(pre_label, 100, ", \"");
714 snprintf(post_label, 100, "\"");
715 snprintf(pre_value, 100, ", ");
716 if( options & RRDR_OPTION_OBJECTSROWS )
717 snprintf(post_line, 100, "}");
719 snprintf(post_line, 100, "]");
720 snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
721 snprintf(finish, 100, "\n ]\n}");
723 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
724 buffer_sprintf(wb, "%stime%s", sq, sq);
727 // -------------------------------------------------------------------------
728 // print the JSON header
733 // print the header lines
734 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
735 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
736 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
738 buffer_strcat(wb, pre_label);
739 buffer_strcat(wb, rd->name);
740 buffer_strcat(wb, post_label);
744 buffer_strcat(wb, pre_label);
745 buffer_strcat(wb, "no data");
746 buffer_strcat(wb, post_label);
749 // print the begin of row data
750 buffer_strcat(wb, data_begin);
752 // if all dimensions are hidden, print a null
754 buffer_strcat(wb, finish);
758 long start = 0, end = rrdr_rows(r), step = 1;
759 if((options & RRDR_OPTION_REVERSED)) {
760 start = rrdr_rows(r) - 1;
765 // for each line in the array
766 calculated_number total = 1;
767 for(i = start; i != end ;i += step) {
768 calculated_number *cn = &r->v[ i * r->d ];
769 uint8_t *co = &r->o[ i * r->d ];
771 time_t now = r->t[i];
773 if(dates == JSON_DATES_JS) {
774 // generate the local date time
775 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
776 if(!tm) { error("localtime_r() failed."); continue; }
778 if(likely(i != start)) buffer_strcat(wb, ",\n");
779 buffer_strcat(wb, pre_date);
781 if( options & RRDR_OPTION_OBJECTSROWS )
782 buffer_sprintf(wb, "%stime%s: ", kq, kq);
785 buffer_strcat(wb, "new ");
787 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
789 buffer_strcat(wb, post_date);
791 if(row_annotations) {
792 // google supports one annotation per row
793 int annotation_found = 0;
794 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
795 if(co[c] & RRDR_RESET) {
796 buffer_strcat(wb, overflow_annotation);
797 annotation_found = 1;
801 if(!annotation_found)
802 buffer_strcat(wb, normal_annotation);
806 // print the timestamp of the line
807 if(likely(i != start)) buffer_strcat(wb, ",\n");
808 buffer_strcat(wb, pre_date);
810 if( options & RRDR_OPTION_OBJECTSROWS )
811 buffer_sprintf(wb, "%stime%s: ", kq, kq);
813 buffer_rrd_value(wb, (calculated_number)r->t[i]);
815 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
817 buffer_strcat(wb, post_date);
820 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
822 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
823 calculated_number n = cn[c];
825 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
830 // prevent a division by zero
831 if(total == 0) total = 1;
834 // for each dimension
835 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
836 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
837 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
839 calculated_number n = cn[c];
841 buffer_strcat(wb, pre_value);
843 if( options & RRDR_OPTION_OBJECTSROWS )
844 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
846 if(co[c] & RRDR_EMPTY) {
847 if(options & RRDR_OPTION_NULL2ZERO)
848 buffer_strcat(wb, "0");
850 buffer_strcat(wb, "null");
853 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
856 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
859 buffer_rrd_value(wb, n);
862 buffer_strcat(wb, post_value);
865 buffer_strcat(wb, post_line);
868 buffer_strcat(wb, finish);
869 //info("RRD2JSON(): %s: END", r->st->id);
872 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
874 //info("RRD2CSV(): %s: BEGIN", r->st->id);
878 // print the csv header
879 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
880 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
881 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
884 buffer_strcat(wb, startline);
885 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
886 buffer_strcat(wb, "time");
887 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
889 buffer_strcat(wb, separator);
890 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
891 buffer_strcat(wb, d->name);
892 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
895 buffer_strcat(wb, endline);
898 // no dimensions present
902 long start = 0, end = rrdr_rows(r), step = 1;
903 if((options & RRDR_OPTION_REVERSED)) {
904 start = rrdr_rows(r) - 1;
909 // for each line in the array
910 calculated_number total = 1;
911 for(i = start; i != end ;i += step) {
912 calculated_number *cn = &r->v[ i * r->d ];
913 uint8_t *co = &r->o[ i * r->d ];
915 buffer_strcat(wb, betweenlines);
916 buffer_strcat(wb, startline);
918 time_t now = r->t[i];
920 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
921 // print the timestamp of the line
922 buffer_rrd_value(wb, (calculated_number)now);
924 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
927 // generate the local date time
928 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
929 if(!tm) { error("localtime() failed."); continue; }
930 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
933 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
935 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
936 calculated_number n = cn[c];
938 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
943 // prevent a division by zero
944 if(total == 0) total = 1;
947 // for each dimension
948 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
949 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
950 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
952 buffer_strcat(wb, separator);
954 calculated_number n = cn[c];
956 if(co[c] & RRDR_EMPTY) {
957 if(options & RRDR_OPTION_NULL2ZERO)
958 buffer_strcat(wb, "0");
960 buffer_strcat(wb, "null");
963 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
966 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
969 buffer_rrd_value(wb, n);
973 buffer_strcat(wb, endline);
975 //info("RRD2CSV(): %s: END", r->st->id);
978 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
980 //info("RRD2SSV(): %s: BEGIN", r->st->id);
984 buffer_strcat(wb, prefix);
985 long start = 0, end = rrdr_rows(r), step = 1;
986 if((options & RRDR_OPTION_REVERSED)) {
987 start = rrdr_rows(r) - 1;
992 // for each line in the array
993 calculated_number total = 1;
994 for(i = start; i != end ;i += step) {
996 calculated_number *cn = &r->v[ i * r->d ];
997 uint8_t *co = &r->o[ i * r->d ];
999 calculated_number sum = 0, min = 0, max = 0, v;
1000 int all_null = 1, init = 1;
1002 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1004 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1005 calculated_number n = cn[c];
1007 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1012 // prevent a division by zero
1013 if(total == 0) total = 1;
1016 // for each dimension
1017 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1018 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1019 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1021 calculated_number n = cn[c];
1023 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1026 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1027 n = n * 100 / total;
1029 if(unlikely(init)) {
1041 if(likely(!(co[c] & RRDR_EMPTY))) {
1046 if(n < min) min = n;
1047 if(n > max) max = n;
1050 if(likely(i != start))
1051 buffer_strcat(wb, separator);
1054 if(options & RRDR_OPTION_NULL2ZERO)
1055 buffer_strcat(wb, "0");
1057 buffer_strcat(wb, "null");
1060 if(options & RRDR_OPTION_MIN2MAX)
1065 if(likely(i != start)) {
1066 if(r->min > v) r->min = v;
1067 if(r->max < v) r->max = v;
1074 buffer_rrd_value(wb, v);
1077 buffer_strcat(wb, suffix);
1078 //info("RRD2SSV(): %s: END", r->st->id);
1081 inline static calculated_number *rrdr_line_values(RRDR *r)
1083 return &r->v[ r->c * r->d ];
1086 inline static uint8_t *rrdr_line_options(RRDR *r)
1088 return &r->o[ r->c * r->d ];
1091 inline static int rrdr_line_init(RRDR *r, time_t t)
1095 if(unlikely(r->c >= r->n)) {
1096 error("requested to step above RRDR size for chart %s", r->st->name);
1106 inline static void rrdr_lock_rrdset(RRDR *r) {
1108 error("NULL value given!");
1112 pthread_rwlock_rdlock(&r->st->rwlock);
1116 inline static void rrdr_unlock_rrdset(RRDR *r) {
1118 error("NULL value given!");
1122 if(likely(r->has_st_lock)) {
1123 pthread_rwlock_unlock(&r->st->rwlock);
1128 inline static void rrdr_free(RRDR *r)
1131 error("NULL value given!");
1135 rrdr_unlock_rrdset(r);
1136 if(likely(r->t)) free(r->t);
1137 if(likely(r->v)) free(r->v);
1138 if(likely(r->o)) free(r->o);
1139 if(likely(r->od)) free(r->od);
1143 inline void rrdr_done(RRDR *r)
1149 static RRDR *rrdr_create(RRDSET *st, long n)
1152 error("NULL value given!");
1156 RRDR *r = calloc(1, sizeof(RRDR));
1157 if(unlikely(!r)) goto cleanup;
1161 rrdr_lock_rrdset(r);
1164 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1168 r->t = malloc(n * sizeof(time_t));
1169 if(unlikely(!r->t)) goto cleanup;
1171 r->v = malloc(n * r->d * sizeof(calculated_number));
1172 if(unlikely(!r->v)) goto cleanup;
1174 r->o = malloc(n * r->d * sizeof(uint8_t));
1175 if(unlikely(!r->o)) goto cleanup;
1177 r->od = malloc(r->d * sizeof(uint8_t));
1178 if(unlikely(!r->od)) goto cleanup;
1180 // set the hidden flag on hidden dimensions
1182 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1183 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1190 r->update_every = 1;
1195 error("Cannot allocate RRDR memory for %d entries", n);
1196 if(likely(r)) rrdr_free(r);
1200 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
1202 int debug = st->debug;
1204 time_t first_entry_t = rrdset_first_entry_t(st);
1205 time_t last_entry_t = rrdset_last_entry_t(st);
1207 if(before == 0 && after == 0) {
1208 before = last_entry_t;
1209 after = first_entry_t;
1212 // allow relative for before and after
1213 if(before <= st->update_every * st->entries)
1214 before = last_entry_t + before;
1216 if(after <= st->update_every * st->entries)
1217 after = last_entry_t + after;
1219 // make sure they are within our timeframe
1220 if(before > last_entry_t) before = last_entry_t;
1221 if(before < first_entry_t) before = first_entry_t;
1223 if(after > last_entry_t) after = last_entry_t;
1224 if(after < first_entry_t) after = first_entry_t;
1226 // check if they are upside down
1227 if(after > before) {
1228 time_t tmp = before;
1233 // the duration of the chart
1234 time_t duration = before - after;
1235 long available_points = duration / st->update_every;
1237 if(duration <= 0 || available_points <= 0)
1238 return rrdr_create(st, 1);
1240 // check the wanted points
1241 if(points < 0) points = -points;
1242 if(points > available_points) points = available_points;
1243 if(points == 0) points = available_points;
1245 // calculate proper grouping of source data
1246 long group = available_points / points;
1247 if(group <= 0) group = 1;
1249 // round group to the closest integer
1250 if(available_points % points > points / 2) group++;
1252 time_t after_new = after - (after % (group * st->update_every));
1253 time_t before_new = before - (before % (group * st->update_every));
1254 long points_new = (before_new - after_new) / st->update_every / group;
1256 // find the starting and ending slots in our round robin db
1257 long start_at_slot = rrdset_time2slot(st, before_new),
1258 stop_at_slot = rrdset_time2slot(st, after_new);
1260 #ifdef NETDATA_INTERNAL_CHECKS
1261 if(after_new < first_entry_t) {
1262 error("after_new %u is too small, minimum %u", after_new, first_entry_t);
1264 if(after_new > last_entry_t) {
1265 error("after_new %u is too big, maximum %u", after_new, last_entry_t);
1267 if(before_new < first_entry_t) {
1268 error("before_new %u is too small, minimum %u", before_new, first_entry_t);
1270 if(before_new > last_entry_t) {
1271 error("before_new %u is too big, maximum %u", before_new, last_entry_t);
1273 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1274 error("start_at_slot is invalid %ld, expected %ld to %ld", start_at_slot, 0, st->entries - 1);
1276 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1277 error("stop_at_slot is invalid %ld, expected %ld to %ld", stop_at_slot, 0, st->entries - 1);
1279 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1280 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1284 //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);
1287 before = before_new;
1288 duration = before - after;
1289 points = points_new;
1292 // before = the end time of the calculation
1293 // after = the start time of the calculation
1294 // duration = the duration of the calculation
1295 // group = the number of source points to aggregate / group together
1296 // method = the method of grouping source points
1297 // points = the number of points to generate
1300 // -------------------------------------------------------------------------
1301 // initialize our result set
1303 RRDR *r = rrdr_create(st, points);
1305 #ifdef NETDATA_INTERNAL_CHECKS
1306 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1311 #ifdef NETDATA_INTERNAL_CHECKS
1312 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%d", st->id, after, before, duration, points);
1317 // find how many dimensions we have
1318 long dimensions = r->d;
1321 // -------------------------------------------------------------------------
1322 // checks for debugging
1324 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"
1328 , last_entry_t - first_entry_t
1337 // -------------------------------------------------------------------------
1338 // temp arrays for keeping values per dimension
1340 calculated_number group_values[dimensions]; // keep sums when grouping
1341 long group_counts[dimensions]; // keep the number of values added to group_values
1342 uint8_t group_options[dimensions];
1343 uint8_t found_non_zero[dimensions];
1349 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1350 group_values[c] = 0;
1351 group_counts[c] = 0;
1352 group_options[c] = 0;
1353 found_non_zero[c] = 0;
1357 // -------------------------------------------------------------------------
1360 time_t now = rrdset_slot2time(st, start_at_slot),
1361 dt = st->update_every,
1364 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"
1376 r->update_every = group * st->update_every;
1380 //info("RRD2RRDR(): %s: STARTING", st->id);
1382 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1383 for(; !stop_now ; now -= dt, slot--, counter++) {
1384 if(unlikely(slot < 0)) slot = st->entries - 1;
1385 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1387 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
1394 , (group_count + 1 == group)?"PRINT":" - "
1395 , (now >= after && now <= before)?"RANGE":" - "
1398 // make sure we return data in the proper time range
1399 if(unlikely(now > before)) continue;
1400 if(unlikely(now < after)) break;
1402 if(unlikely(group_count == 0)) {
1403 group_start_t = now;
1407 if(unlikely(group_count == group)) {
1408 if(unlikely(added >= points)) break;
1412 // do the calculations
1413 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1414 storage_number n = rd->values[slot];
1415 if(unlikely(!does_storage_number_exist(n))) continue;
1419 calculated_number value = unpack_storage_number(n);
1420 if(likely(value != 0.0)) {
1421 group_options[c] |= RRDR_NONZERO;
1422 found_non_zero[c] = 1;
1425 if(unlikely(did_storage_number_reset(n)))
1426 group_options[c] |= RRDR_RESET;
1428 switch(group_method) {
1430 if(unlikely(abs(value) > abs(group_values[c])))
1431 group_values[c] = value;
1437 group_values[c] += value;
1443 if(unlikely(add_this)) {
1444 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1448 calculated_number *cn = rrdr_line_values(r);
1449 uint8_t *co = rrdr_line_options(r);
1451 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1453 // update the dimension options
1454 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1456 // store the specific point options
1457 co[c] = group_options[c];
1460 if(unlikely(group_counts[c] == 0)) {
1462 co[c] |= RRDR_EMPTY;
1464 else if(unlikely(group_method == GROUP_AVERAGE)) {
1465 cn[c] = group_values[c] / group_counts[c];
1468 cn[c] = group_values[c];
1471 if(cn[c] < r->min) r->min = cn[c];
1472 if(cn[c] > r->max) r->max = cn[c];
1474 // reset them for the next loop
1475 group_values[c] = 0;
1476 group_counts[c] = 0;
1477 group_options[c] = 0;
1487 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1488 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1492 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)
1494 RRDR *r = rrd2rrdr(st, points, after, before, group_method);
1496 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1500 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1503 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1505 if(latest_timestamp && rrdr_rows(r) > 0)
1506 *latest_timestamp = r->before;
1509 case DATASOURCE_SSV:
1510 if(options & RRDR_OPTION_JSON_WRAP) {
1511 wb->contenttype = CT_APPLICATION_JSON;
1512 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1513 rrdr2ssv(r, wb, options, "", " ", "");
1514 rrdr_json_wrapper_end(r, wb, format, options, 1);
1517 wb->contenttype = CT_TEXT_PLAIN;
1518 rrdr2ssv(r, wb, options, "", " ", "");
1522 case DATASOURCE_SSV_COMMA:
1523 if(options & RRDR_OPTION_JSON_WRAP) {
1524 wb->contenttype = CT_APPLICATION_JSON;
1525 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1526 rrdr2ssv(r, wb, options, "", ",", "");
1527 rrdr_json_wrapper_end(r, wb, format, options, 1);
1530 wb->contenttype = CT_TEXT_PLAIN;
1531 rrdr2ssv(r, wb, options, "", ",", "");
1535 case DATASOURCE_JS_ARRAY:
1536 if(options & RRDR_OPTION_JSON_WRAP) {
1537 wb->contenttype = CT_APPLICATION_JSON;
1538 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1539 rrdr2ssv(r, wb, options, "[", ",", "]");
1540 rrdr_json_wrapper_end(r, wb, format, options, 0);
1543 wb->contenttype = CT_APPLICATION_JSON;
1544 rrdr2ssv(r, wb, options, "[", ",", "]");
1548 case DATASOURCE_CSV:
1549 if(options & RRDR_OPTION_JSON_WRAP) {
1550 wb->contenttype = CT_APPLICATION_JSON;
1551 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1552 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1553 rrdr_json_wrapper_end(r, wb, format, options, 1);
1556 wb->contenttype = CT_TEXT_PLAIN;
1557 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1561 case DATASOURCE_CSV_JSON_ARRAY:
1562 wb->contenttype = CT_APPLICATION_JSON;
1563 if(options & RRDR_OPTION_JSON_WRAP) {
1564 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1565 buffer_strcat(wb, "[\n");
1566 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1567 buffer_strcat(wb, "\n]");
1568 rrdr_json_wrapper_end(r, wb, format, options, 0);
1571 wb->contenttype = CT_TEXT_PLAIN;
1572 buffer_strcat(wb, "[\n");
1573 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1574 buffer_strcat(wb, "\n]");
1578 case DATASOURCE_TSV:
1579 if(options & RRDR_OPTION_JSON_WRAP) {
1580 wb->contenttype = CT_APPLICATION_JSON;
1581 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1582 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1583 rrdr_json_wrapper_end(r, wb, format, options, 1);
1586 wb->contenttype = CT_TEXT_PLAIN;
1587 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1591 case DATASOURCE_HTML:
1592 if(options & RRDR_OPTION_JSON_WRAP) {
1593 wb->contenttype = CT_APPLICATION_JSON;
1594 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1595 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1596 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1597 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1598 rrdr_json_wrapper_end(r, wb, format, options, 1);
1601 wb->contenttype = CT_TEXT_HTML;
1602 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1603 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1604 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1608 case DATASOURCE_DATATABLE_JSONP:
1609 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1611 if(options & RRDR_OPTION_JSON_WRAP)
1612 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1614 rrdr2json(r, wb, options, 1);
1616 if(options & RRDR_OPTION_JSON_WRAP)
1617 rrdr_json_wrapper_end(r, wb, format, options, 0);
1620 case DATASOURCE_DATATABLE_JSON:
1621 wb->contenttype = CT_APPLICATION_JSON;
1623 if(options & RRDR_OPTION_JSON_WRAP)
1624 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1626 rrdr2json(r, wb, options, 1);
1628 if(options & RRDR_OPTION_JSON_WRAP)
1629 rrdr_json_wrapper_end(r, wb, format, options, 0);
1632 case DATASOURCE_JSONP:
1633 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1634 if(options & RRDR_OPTION_JSON_WRAP)
1635 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1637 rrdr2json(r, wb, options, 0);
1639 if(options & RRDR_OPTION_JSON_WRAP)
1640 rrdr_json_wrapper_end(r, wb, format, options, 0);
1643 case DATASOURCE_JSON:
1645 wb->contenttype = CT_APPLICATION_JSON;
1647 if(options & RRDR_OPTION_JSON_WRAP)
1648 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1650 rrdr2json(r, wb, options, 0);
1652 if(options & RRDR_OPTION_JSON_WRAP)
1653 rrdr_json_wrapper_end(r, wb, format, options, 0);
1661 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)
1664 pthread_rwlock_rdlock(&st->rwlock);
1667 // -------------------------------------------------------------------------
1668 // switch from JSON to google JSON
1673 case DATASOURCE_DATATABLE_JSON:
1674 case DATASOURCE_DATATABLE_JSONP:
1679 case DATASOURCE_JSON:
1685 // -------------------------------------------------------------------------
1686 // validate the parameters
1688 if(points < 1) points = 1;
1689 if(group < 1) group = 1;
1691 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1692 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1696 // our return value (the last timestamp printed)
1697 // this is required to detect re-transmit in google JSONP
1698 time_t last_timestamp = 0;
1701 // -------------------------------------------------------------------------
1702 // find how many dimensions we have
1706 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1708 pthread_rwlock_unlock(&st->rwlock);
1709 buffer_strcat(wb, "No dimensions yet.");
1714 // -------------------------------------------------------------------------
1715 // prepare various strings, to speed up the loop
1717 char overflow_annotation[201]; snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
1718 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1719 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1720 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1721 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1722 char post_value[21]; snprintf(post_value, 20, "}");
1725 // -------------------------------------------------------------------------
1726 // checks for debugging
1729 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"
1731 , rrdset_first_entry_t(st)
1732 , rrdset_last_entry_t(st)
1733 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1742 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
1744 if((before - after) > st->entries * st->update_every)
1745 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);
1749 // -------------------------------------------------------------------------
1750 // temp arrays for keeping values per dimension
1752 calculated_number group_values[dimensions]; // keep sums when grouping
1753 int print_hidden[dimensions]; // keep hidden flags
1754 int found_non_zero[dimensions];
1755 int found_non_existing[dimensions];
1758 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1759 group_values[c] = 0;
1760 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1761 found_non_zero[c] = 0;
1762 found_non_existing[c] = 0;
1766 // 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);
1767 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1768 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1770 // -------------------------------------------------------------------------
1771 // remove dimensions that contain only zeros
1774 if(only_non_zero) max_loop = 2;
1776 for(; max_loop ; max_loop--) {
1778 // -------------------------------------------------------------------------
1779 // print the JSON header
1781 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1782 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);
1783 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);
1784 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);
1786 // print the header for each dimension
1787 // and update the print_hidden array for the dimensions that should be hidden
1789 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1790 if(!print_hidden[c]) {
1792 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);
1796 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);
1799 // print the begin of row data
1800 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1803 // -------------------------------------------------------------------------
1806 int annotate_reset = 0;
1807 int annotation_count = 0;
1809 long t = rrdset_time2slot(st, before),
1810 stop_at_t = rrdset_time2slot(st, after),
1815 time_t now = rrdset_slot2time(st, t),
1816 dt = st->update_every;
1818 long count = 0, printed = 0, group_count = 0;
1821 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"
1828 , rrdset_first_entry_t(st)
1829 , rrdset_last_entry_t(st)
1835 for(; !stop_now ; now -= dt, t--, counter++) {
1836 if(t < 0) t = st->entries - 1;
1837 if(t == stop_at_t) stop_now = counter;
1841 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
1848 , (group_count + 1 == group)?"PRINT":" - "
1849 , (now >= after && now <= before)?"RANGE":" - "
1853 // make sure we return data in the proper time range
1854 if(now > before) continue;
1855 if(now < after) break;
1857 //if(rrdset_slot2time(st, t) != now)
1858 // 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));
1863 // check if we have to print this now
1864 if(group_count == group) {
1865 if(printed >= points) {
1866 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1870 // generate the local date time
1871 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1872 if(!tm) { error("localtime() failed."); continue; }
1873 if(now > last_timestamp) last_timestamp = now;
1875 if(printed) buffer_strcat(wb, "]},\n");
1876 buffer_strcat(wb, pre_date);
1877 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1878 buffer_strcat(wb, post_date);
1883 // do the calculations
1884 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1885 storage_number n = rd->values[t];
1886 calculated_number value = unpack_storage_number(n);
1888 if(!does_storage_number_exist(n)) {
1890 found_non_existing[c]++;
1892 if(did_storage_number_reset(n)) annotate_reset = 1;
1894 switch(group_method) {
1896 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1900 group_values[c] += value;
1905 group_values[c] += value;
1906 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1912 if(annotate_reset) {
1914 buffer_strcat(wb, overflow_annotation);
1918 buffer_strcat(wb, normal_annotation);
1921 for(c = 0 ; c < dimensions ; c++) {
1922 if(found_non_existing[c] == group_count) {
1923 // all entries are non-existing
1925 buffer_strcat(wb, pre_value);
1926 buffer_strcat(wb, "null");
1927 buffer_strcat(wb, post_value);
1929 else if(!print_hidden[c]) {
1931 buffer_strcat(wb, pre_value);
1932 buffer_rrd_value(wb, group_values[c]);
1933 buffer_strcat(wb, post_value);
1935 if(group_values[c]) found_non_zero[c]++;
1938 // reset them for the next loop
1939 group_values[c] = 0;
1940 found_non_existing[c] = 0;
1943 // if all dimensions are hidden, print a null
1945 buffer_strcat(wb, pre_value);
1946 buffer_strcat(wb, "null");
1947 buffer_strcat(wb, post_value);
1955 if(printed) buffer_strcat(wb, "]}");
1956 buffer_strcat(wb, "\n ]\n}\n");
1958 if(only_non_zero && max_loop > 1) {
1960 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1961 group_values[c] = 0;
1962 found_non_existing[c] = 0;
1964 if(!print_hidden[c] && !found_non_zero[c]) {
1966 print_hidden[c] = 1;
1970 if(changed) buffer_flush(wb);
1977 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
1979 pthread_rwlock_unlock(&st->rwlock);
1980 return last_timestamp;