3 #define HOSTNAME_MAX 1024
4 char *hostname = "unknown";
6 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
8 pthread_rwlock_rdlock(&st->rwlock);
12 "\t\t\t\"id\": \"%s\",\n"
13 "\t\t\t\"name\": \"%s\",\n"
14 "\t\t\t\"type\": \"%s\",\n"
15 "\t\t\t\"family\": \"%s\",\n"
16 "\t\t\t\"context\": \"%s\",\n"
17 "\t\t\t\"title\": \"%s\",\n"
18 "\t\t\t\"priority\": %ld,\n"
19 "\t\t\t\"enabled\": %s,\n"
20 "\t\t\t\"units\": \"%s\",\n"
21 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
22 "\t\t\t\"chart_type\": \"%s\",\n"
23 "\t\t\t\"duration\": %ld,\n"
24 "\t\t\t\"first_entry\": %ld,\n"
25 "\t\t\t\"last_entry\": %ld,\n"
26 "\t\t\t\"update_every\": %d,\n"
27 "\t\t\t\"dimensions\": {\n"
35 , st->enabled?"true":"false"
38 , rrdset_type_name(st->chart_type)
39 , st->entries * st->update_every
40 , rrdset_first_entry_t(st)
41 , rrdset_last_entry_t(st)
45 unsigned long memory = st->memsize;
49 for(rd = st->dimensions; rd ; rd = rd->next) {
50 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
52 memory += rd->memsize;
56 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
70 pthread_rwlock_unlock(&st->rwlock);
73 void rrd_stats_api_v1_charts(BUFFER *wb)
78 buffer_sprintf(wb, "{\n"
79 "\t\"hostname\": \"%s\""
80 ",\n\t\"update_every\": %d"
81 ",\n\t\"history\": %d"
85 , rrd_default_history_entries
88 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
89 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
90 if(st->enabled && st->dimensions) {
91 if(c) buffer_strcat(wb, ",");
92 buffer_strcat(wb, "\n\t\t\"");
93 buffer_strcat(wb, st->id);
94 buffer_strcat(wb, "\": ");
95 rrd_stats_api_v1_chart(st, wb);
99 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
101 buffer_strcat(wb, "\n\t}\n}\n");
105 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
107 time_t now = time(NULL);
109 pthread_rwlock_rdlock(&st->rwlock);
113 "\t\t\t\"id\": \"%s\",\n"
114 "\t\t\t\"name\": \"%s\",\n"
115 "\t\t\t\"type\": \"%s\",\n"
116 "\t\t\t\"family\": \"%s\",\n"
117 "\t\t\t\"context\": \"%s\",\n"
118 "\t\t\t\"title\": \"%s\",\n"
119 "\t\t\t\"priority\": %ld,\n"
120 "\t\t\t\"enabled\": %d,\n"
121 "\t\t\t\"units\": \"%s\",\n"
122 "\t\t\t\"url\": \"/data/%s/%s\",\n"
123 "\t\t\t\"chart_type\": \"%s\",\n"
124 "\t\t\t\"counter\": %lu,\n"
125 "\t\t\t\"entries\": %ld,\n"
126 "\t\t\t\"first_entry_t\": %ld,\n"
127 "\t\t\t\"last_entry\": %lu,\n"
128 "\t\t\t\"last_entry_t\": %ld,\n"
129 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
130 "\t\t\t\"update_every\": %d,\n"
131 "\t\t\t\"isdetail\": %d,\n"
132 "\t\t\t\"usec_since_last_update\": %llu,\n"
133 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
134 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
135 "\t\t\t\"dimensions\": [\n"
145 , st->name, options?options:""
146 , rrdset_type_name(st->chart_type)
149 , rrdset_first_entry_t(st)
150 , rrdset_last_slot(st)
151 , rrdset_last_entry_t(st)
152 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
155 , st->usec_since_last_update
156 , st->collected_total
157 , st->last_collected_total
160 unsigned long memory = st->memsize;
163 for(rd = st->dimensions; rd ; rd = rd->next) {
165 memory += rd->memsize;
169 "\t\t\t\t\t\"id\": \"%s\",\n"
170 "\t\t\t\t\t\"name\": \"%s\",\n"
171 "\t\t\t\t\t\"entries\": %ld,\n"
172 "\t\t\t\t\t\"isHidden\": %d,\n"
173 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
174 "\t\t\t\t\t\"multiplier\": %ld,\n"
175 "\t\t\t\t\t\"divisor\": %ld,\n"
176 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
177 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
178 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
179 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
180 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
181 "\t\t\t\t\t\"memory\": %lu\n"
186 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
187 , rrddim_algorithm_name(rd->algorithm)
190 , rd->last_collected_time.tv_sec
191 , rd->collected_value
192 , rd->calculated_value
193 , rd->last_collected_value
194 , rd->last_calculated_value
202 "\t\t\t\"memory\" : %lu\n"
207 pthread_rwlock_unlock(&st->rwlock);
211 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
212 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
214 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
216 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
217 rrd_stats_one_json(st, options, wb);
218 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
221 void rrd_stats_all_json(BUFFER *wb)
223 unsigned long memory = 0;
227 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
229 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
230 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
231 if(st->enabled && st->dimensions) {
232 if(c) buffer_strcat(wb, ",\n");
233 memory += rrd_stats_one_json(st, NULL, wb);
237 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
239 buffer_sprintf(wb, "\n\t],\n"
240 "\t\"hostname\": \"%s\",\n"
241 "\t\"update_every\": %d,\n"
242 "\t\"history\": %d,\n"
243 "\t\"memory\": %lu\n"
247 , rrd_default_history_entries
254 // ----------------------------------------------------------------------------
256 // RRDR dimension options
257 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
258 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
259 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
260 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
262 // RRDR result options
263 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
264 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
266 typedef struct rrdresult {
267 RRDSET *st; // the chart this result refers to
269 uint32_t result_options; // RRDR_RESULT_OPTION_*
271 int d; // the number of dimensions
272 long n; // the number of values in the arrays
273 long rows; // the number of rows used
275 uint8_t *od; // the options for the dimensions
277 time_t *t; // array of n timestamps
278 calculated_number *v; // array n x d values
279 uint8_t *o; // array n x d options
281 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
283 long group; // how many collected values were grouped for each row
284 int update_every; // what is the suggested update frequency in seconds
286 calculated_number min;
287 calculated_number max;
292 int has_st_lock; // if st is read locked by us
295 #define rrdr_rows(r) ((r)->rows)
298 static void rrdr_dump(RRDR *r)
303 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
305 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
306 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
309 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
310 , (r->od[c] & RRDR_RESET)?"RESET ":""
311 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
312 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
317 fprintf(stderr, "RRDR does not have any values in it.\n");
321 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
323 // for each line in the array
324 for(i = 0; i < r->rows ;i++) {
325 calculated_number *cn = &r->v[ i * r->d ];
326 uint8_t *co = &r->o[ i * r->d ];
328 // print the id and the timestamp of the line
329 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
331 // for each dimension
332 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
333 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
334 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
336 if(co[c] & RRDR_EMPTY)
337 fprintf(stderr, "null ");
339 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
341 , (co[c] & RRDR_EMPTY)?"E":" "
342 , (co[c] & RRDR_RESET)?"R":" "
343 , (co[c] & RRDR_HIDDEN)?"H":" "
344 , (co[c] & RRDR_NONZERO)?"N":" "
348 fprintf(stderr, "\n");
353 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
355 char b[strlen(dims) + 1];
362 // disable all of them
363 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
364 r->od[c] |= RRDR_HIDDEN;
366 while(o && *o && (tok = mystrsep(&o, ",|"))) {
369 uint32_t hash = simple_hash(tok);
371 // find it and enable it
372 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
373 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
374 r->od[c] &= ~RRDR_HIDDEN;
376 // since the user needs this dimension
377 // make it appear as NONZERO, to return it
378 // even if the dimension has only zeros
379 r->od[c] |= RRDR_NONZERO;
385 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
388 case DATASOURCE_JSON:
389 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
392 case DATASOURCE_DATATABLE_JSON:
393 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
396 case DATASOURCE_DATATABLE_JSONP:
397 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
400 case DATASOURCE_JSONP:
401 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
405 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
409 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
413 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
416 case DATASOURCE_HTML:
417 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
420 case DATASOURCE_JS_ARRAY:
421 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
424 case DATASOURCE_SSV_COMMA:
425 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
429 buffer_strcat(wb, "unknown");
434 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
436 if(options & RRDR_OPTION_NONZERO) {
440 // the caller wants specific dimensions
441 // disable NONZERO option
442 // to make sure we don't accidentally prevent
443 // the specific dimensions from being returned
447 // find how many dimensions are not zero
450 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
451 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
452 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
457 // if with nonzero we get i = 0 (no dimensions will be returned)
458 // disable nonzero to show all dimensions
459 if(!i) options &= ~RRDR_OPTION_NONZERO;
465 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
467 long rows = rrdr_rows(r);
471 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
472 char kq[2] = "", // key quote
473 sq[2] = ""; // string quote
475 if( options & RRDR_OPTION_GOOGLE_JSON ) {
484 buffer_sprintf(wb, "{\n"
487 " %sname%s: %s%s%s,\n"
488 " %sview_update_every%s: %d,\n"
489 " %supdate_every%s: %d,\n"
490 " %sfirst_entry%s: %u,\n"
491 " %slast_entry%s: %u,\n"
494 " %sdimension_names%s: ["
496 , kq, kq, sq, r->st->id, sq
497 , kq, kq, sq, r->st->name, sq
498 , kq, kq, r->update_every
499 , kq, kq, r->st->update_every
500 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
501 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
502 , kq, kq, (uint32_t)r->before
503 , kq, kq, (uint32_t)r->after
506 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
507 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
508 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
510 if(i) buffer_strcat(wb, ", ");
511 buffer_strcat(wb, sq);
512 buffer_strcat(wb, rd->name);
513 buffer_strcat(wb, sq);
517 #ifdef NETDATA_INTERNAL_CHECKS
518 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
521 buffer_strcat(wb, sq);
522 buffer_strcat(wb, "no data");
523 buffer_strcat(wb, sq);
526 buffer_sprintf(wb, "],\n"
527 " %sdimension_ids%s: ["
530 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
531 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
532 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
534 if(i) buffer_strcat(wb, ", ");
535 buffer_strcat(wb, sq);
536 buffer_strcat(wb, rd->id);
537 buffer_strcat(wb, sq);
542 buffer_strcat(wb, sq);
543 buffer_strcat(wb, "no data");
544 buffer_strcat(wb, sq);
547 buffer_sprintf(wb, "],\n"
548 " %slatest_values%s: ["
551 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
552 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
553 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
555 if(i) buffer_strcat(wb, ", ");
558 storage_number n = rd->values[rrdset_last_slot(r->st)];
560 if(!does_storage_number_exist(n))
561 buffer_strcat(wb, "null");
563 buffer_rrd_value(wb, unpack_storage_number(n));
567 buffer_strcat(wb, "null");
570 buffer_sprintf(wb, "],\n"
571 " %sview_latest_values%s: ["
576 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
577 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
578 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
580 if(i) buffer_strcat(wb, ", ");
583 calculated_number *cn = &r->v[ (0) * r->d ];
584 uint8_t *co = &r->o[ (0) * r->d ];
586 if(co[c] & RRDR_EMPTY)
587 buffer_strcat(wb, "null");
589 buffer_rrd_value(wb, cn[c]);
594 buffer_strcat(wb, "null");
597 buffer_sprintf(wb, "],\n"
598 " %sdimensions%s: %ld,\n"
599 " %spoints%s: %ld,\n"
606 rrdr_buffer_print_format(wb, format);
608 buffer_sprintf(wb, "%s,\n"
614 if(string_value) buffer_strcat(wb, sq);
615 //info("JSONWRAPPER(): %s: END", r->st->id);
618 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 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
676 snprintfz(post_date, 100, "%s}", sq);
677 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
678 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
679 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
680 strcpy(post_value, "}");
681 strcpy(post_line, "]}");
682 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
683 strcpy(finish, "\n ]\n}");
685 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);
686 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
688 buffer_sprintf(wb, "{\n %scols%s:\n [\n", 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 strcpy(pre_date, " { ");
712 strcpy(pre_date, " [ ");
713 strcpy(pre_label, ", \"");
714 strcpy(post_label, "\"");
715 strcpy(pre_value, ", ");
716 if( options & RRDR_OPTION_OBJECTSROWS )
717 strcpy(post_line, "}");
719 strcpy(post_line, "]");
720 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
721 strcpy(finish, "\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 + 1, 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 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
982 calculated_number *cn = &r->v[ i * r->d ];
983 uint8_t *co = &r->o[ i * r->d ];
985 calculated_number sum = 0, min = 0, max = 0, v;
986 int all_null = 1, init = 1;
988 calculated_number total = 1;
989 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
991 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
992 calculated_number n = cn[c];
994 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
999 // prevent a division by zero
1000 if(total == 0) total = 1;
1003 // for each dimension
1004 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1005 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1006 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1008 calculated_number n = cn[c];
1010 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1013 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1014 n = n * 100 / total;
1016 if(unlikely(init)) {
1028 if(likely(!(co[c] & RRDR_EMPTY))) {
1033 if(n < min) min = n;
1034 if(n > max) max = n;
1037 if(unlikely(all_null)) {
1038 if(likely(*all_values_are_null))
1039 *all_values_are_null = 1;
1043 if(likely(*all_values_are_null))
1044 *all_values_are_null = 0;
1047 if(options & RRDR_OPTION_MIN2MAX)
1055 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1057 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1060 buffer_strcat(wb, prefix);
1061 long start = 0, end = rrdr_rows(r), step = 1;
1062 if((options & RRDR_OPTION_REVERSED)) {
1063 start = rrdr_rows(r) - 1;
1068 // for each line in the array
1069 for(i = start; i != end ;i += step) {
1070 int all_values_are_null = 0;
1071 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1073 if(likely(i != start)) {
1074 if(r->min > v) r->min = v;
1075 if(r->max < v) r->max = v;
1082 if(likely(i != start))
1083 buffer_strcat(wb, separator);
1085 if(all_values_are_null) {
1086 if(options & RRDR_OPTION_NULL2ZERO)
1087 buffer_strcat(wb, "0");
1089 buffer_strcat(wb, "null");
1092 buffer_rrd_value(wb, v);
1094 buffer_strcat(wb, suffix);
1095 //info("RRD2SSV(): %s: END", r->st->id);
1098 inline static calculated_number *rrdr_line_values(RRDR *r)
1100 return &r->v[ r->c * r->d ];
1103 inline static uint8_t *rrdr_line_options(RRDR *r)
1105 return &r->o[ r->c * r->d ];
1108 inline static int rrdr_line_init(RRDR *r, time_t t)
1112 if(unlikely(r->c >= r->n)) {
1113 error("requested to step above RRDR size for chart %s", r->st->name);
1123 inline static void rrdr_lock_rrdset(RRDR *r) {
1125 error("NULL value given!");
1129 pthread_rwlock_rdlock(&r->st->rwlock);
1133 inline static void rrdr_unlock_rrdset(RRDR *r) {
1135 error("NULL value given!");
1139 if(likely(r->has_st_lock)) {
1140 pthread_rwlock_unlock(&r->st->rwlock);
1145 inline static void rrdr_free(RRDR *r)
1148 error("NULL value given!");
1152 rrdr_unlock_rrdset(r);
1160 inline void rrdr_done(RRDR *r)
1166 static RRDR *rrdr_create(RRDSET *st, long n)
1169 error("NULL value given!");
1173 RRDR *r = callocz(1, sizeof(RRDR));
1176 rrdr_lock_rrdset(r);
1179 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1183 r->t = mallocz(n * sizeof(time_t));
1184 r->v = mallocz(n * r->d * sizeof(calculated_number));
1185 r->o = mallocz(n * r->d * sizeof(uint8_t));
1186 r->od = mallocz(r->d * sizeof(uint8_t));
1188 // set the hidden flag on hidden dimensions
1190 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1191 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1197 r->update_every = 1;
1202 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1204 int debug = st->debug;
1205 int absolute_period_requested = -1;
1207 time_t first_entry_t = rrdset_first_entry_t(st);
1208 time_t last_entry_t = rrdset_last_entry_t(st);
1210 if(before == 0 && after == 0) {
1211 before = last_entry_t;
1212 after = first_entry_t;
1213 absolute_period_requested = 0;
1216 // allow relative for before and after
1217 if(((before < 0)?-before:before) <= (st->update_every * st->entries)) {
1218 before = last_entry_t + before;
1219 absolute_period_requested = 0;
1222 if(((after < 0)?-after:after) <= (st->update_every * st->entries)) {
1223 if(after == 0) after = -st->update_every;
1224 after = before + after;
1225 absolute_period_requested = 0;
1228 if(absolute_period_requested == -1)
1229 absolute_period_requested = 1;
1231 // make sure they are within our timeframe
1232 if(before > last_entry_t) before = last_entry_t;
1233 if(before < first_entry_t) before = first_entry_t;
1235 if(after > last_entry_t) after = last_entry_t;
1236 if(after < first_entry_t) after = first_entry_t;
1238 // check if they are upside down
1239 if(after > before) {
1240 time_t tmp = before;
1245 // the duration of the chart
1246 time_t duration = before - after;
1247 long available_points = duration / st->update_every;
1249 if(duration <= 0 || available_points <= 0)
1250 return rrdr_create(st, 1);
1252 // check the wanted points
1253 if(points < 0) points = -points;
1254 if(points > available_points) points = available_points;
1255 if(points == 0) points = available_points;
1257 // calculate proper grouping of source data
1258 long group = available_points / points;
1259 if(group <= 0) group = 1;
1261 // round group to the closest integer
1262 if(available_points % points > points / 2) group++;
1264 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1265 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1266 long points_new = (before_new - after_new) / st->update_every / group;
1268 // find the starting and ending slots in our round robin db
1269 long start_at_slot = rrdset_time2slot(st, before_new),
1270 stop_at_slot = rrdset_time2slot(st, after_new);
1272 #ifdef NETDATA_INTERNAL_CHECKS
1273 if(after_new < first_entry_t) {
1274 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1276 if(after_new > last_entry_t) {
1277 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1279 if(before_new < first_entry_t) {
1280 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1282 if(before_new > last_entry_t) {
1283 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1285 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1286 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1288 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1289 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1291 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1292 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1296 //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);
1299 before = before_new;
1300 duration = before - after;
1301 points = points_new;
1304 // before = the end time of the calculation
1305 // after = the start time of the calculation
1306 // duration = the duration of the calculation
1307 // group = the number of source points to aggregate / group together
1308 // method = the method of grouping source points
1309 // points = the number of points to generate
1312 // -------------------------------------------------------------------------
1313 // initialize our result set
1315 RRDR *r = rrdr_create(st, points);
1317 #ifdef NETDATA_INTERNAL_CHECKS
1318 error("Cannot create RRDR for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1323 #ifdef NETDATA_INTERNAL_CHECKS
1324 error("Returning empty RRDR (no dimensions in RRDSET) for %s, after=%u, before=%u, duration=%u, points=%ld", st->id, (uint32_t)after, (uint32_t)before, (uint32_t)duration, points);
1329 if(absolute_period_requested == 1)
1330 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1332 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1334 // find how many dimensions we have
1335 long dimensions = r->d;
1338 // -------------------------------------------------------------------------
1339 // checks for debugging
1341 if(debug) debug(D_RRD_STATS, "INFO %s first_t: %u, last_t: %u, all_duration: %u, after: %u, before: %u, duration: %u, points: %ld, group: %ld"
1343 , (uint32_t)first_entry_t
1344 , (uint32_t)last_entry_t
1345 , (uint32_t)(last_entry_t - first_entry_t)
1348 , (uint32_t)duration
1354 // -------------------------------------------------------------------------
1355 // temp arrays for keeping values per dimension
1357 calculated_number last_values[dimensions]; // keep the last value of each dimension
1358 calculated_number group_values[dimensions]; // keep sums when grouping
1359 long group_counts[dimensions]; // keep the number of values added to group_values
1360 uint8_t group_options[dimensions];
1361 uint8_t found_non_zero[dimensions];
1367 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1369 group_values[c] = 0;
1370 group_counts[c] = 0;
1371 group_options[c] = 0;
1372 found_non_zero[c] = 0;
1376 // -------------------------------------------------------------------------
1379 time_t now = rrdset_slot2time(st, start_at_slot),
1380 dt = st->update_every,
1383 if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %u (stop_at_t: %ld), before_t: %u (start_at_t: %ld), start_t(now): %u, current_entry: %ld, entries: %ld"
1395 r->update_every = group * st->update_every;
1399 //info("RRD2RRDR(): %s: STARTING", st->id);
1401 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1402 for(; !stop_now ; now -= dt, slot--, counter++) {
1403 if(unlikely(slot < 0)) slot = st->entries - 1;
1404 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1406 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1413 , (group_count + 1 == group)?"PRINT":" - "
1414 , (now >= after && now <= before)?"RANGE":" - "
1417 // make sure we return data in the proper time range
1418 if(unlikely(now > before)) continue;
1419 if(unlikely(now < after)) break;
1421 if(unlikely(group_count == 0)) {
1422 group_start_t = now;
1426 if(unlikely(group_count == group)) {
1427 if(unlikely(added >= points)) break;
1431 // do the calculations
1432 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1433 storage_number n = rd->values[slot];
1434 if(unlikely(!does_storage_number_exist(n))) continue;
1438 calculated_number value = unpack_storage_number(n);
1439 if(likely(value != 0.0)) {
1440 group_options[c] |= RRDR_NONZERO;
1441 found_non_zero[c] = 1;
1444 if(unlikely(did_storage_number_reset(n)))
1445 group_options[c] |= RRDR_RESET;
1447 switch(group_method) {
1449 if(unlikely(fabsl(value) > fabsl(group_values[c])))
1450 group_values[c] = value;
1456 group_values[c] += value;
1459 case GROUP_INCREMENTAL_SUM:
1460 if(unlikely(slot == start_at_slot))
1461 last_values[c] = value;
1463 group_values[c] += last_values[c] - value;
1464 last_values[c] = value;
1470 if(unlikely(add_this)) {
1471 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1475 calculated_number *cn = rrdr_line_values(r);
1476 uint8_t *co = rrdr_line_options(r);
1478 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1480 // update the dimension options
1481 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1483 // store the specific point options
1484 co[c] = group_options[c];
1487 if(unlikely(group_counts[c] == 0)) {
1489 co[c] |= RRDR_EMPTY;
1491 else if(unlikely(group_method == GROUP_AVERAGE)) {
1493 cn[c] = group_values[c] / group_counts[c];
1498 // GROUP_INCREMENTAL_SUM
1499 cn[c] = group_values[c];
1502 if(cn[c] < r->min) r->min = cn[c];
1503 if(cn[c] > r->max) r->max = cn[c];
1505 // reset them for the next loop
1506 group_values[c] = 0;
1507 group_counts[c] = 0;
1508 group_options[c] = 0;
1518 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1519 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1523 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)
1525 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1527 if(value_is_null) *value_is_null = 1;
1531 if(rrdr_rows(r) == 0) {
1533 if(value_is_null) *value_is_null = 1;
1537 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1538 wb->options |= WB_CONTENT_NO_CACHEABLE;
1539 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1540 wb->options |= WB_CONTENT_CACHEABLE;
1542 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1545 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1547 if(latest_timestamp)
1548 *latest_timestamp = r->before;
1550 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1551 *n = rrdr2value(r, i, options, value_is_null);
1557 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)
1559 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1561 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1565 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1566 wb->options |= WB_CONTENT_NO_CACHEABLE;
1567 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1568 wb->options |= WB_CONTENT_CACHEABLE;
1570 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1573 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1575 if(latest_timestamp && rrdr_rows(r) > 0)
1576 *latest_timestamp = r->before;
1579 case DATASOURCE_SSV:
1580 if(options & RRDR_OPTION_JSON_WRAP) {
1581 wb->contenttype = CT_APPLICATION_JSON;
1582 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1583 rrdr2ssv(r, wb, options, "", " ", "");
1584 rrdr_json_wrapper_end(r, wb, format, options, 1);
1587 wb->contenttype = CT_TEXT_PLAIN;
1588 rrdr2ssv(r, wb, options, "", " ", "");
1592 case DATASOURCE_SSV_COMMA:
1593 if(options & RRDR_OPTION_JSON_WRAP) {
1594 wb->contenttype = CT_APPLICATION_JSON;
1595 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1596 rrdr2ssv(r, wb, options, "", ",", "");
1597 rrdr_json_wrapper_end(r, wb, format, options, 1);
1600 wb->contenttype = CT_TEXT_PLAIN;
1601 rrdr2ssv(r, wb, options, "", ",", "");
1605 case DATASOURCE_JS_ARRAY:
1606 if(options & RRDR_OPTION_JSON_WRAP) {
1607 wb->contenttype = CT_APPLICATION_JSON;
1608 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1609 rrdr2ssv(r, wb, options, "[", ",", "]");
1610 rrdr_json_wrapper_end(r, wb, format, options, 0);
1613 wb->contenttype = CT_APPLICATION_JSON;
1614 rrdr2ssv(r, wb, options, "[", ",", "]");
1618 case DATASOURCE_CSV:
1619 if(options & RRDR_OPTION_JSON_WRAP) {
1620 wb->contenttype = CT_APPLICATION_JSON;
1621 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1622 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1623 rrdr_json_wrapper_end(r, wb, format, options, 1);
1626 wb->contenttype = CT_TEXT_PLAIN;
1627 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1631 case DATASOURCE_CSV_JSON_ARRAY:
1632 wb->contenttype = CT_APPLICATION_JSON;
1633 if(options & RRDR_OPTION_JSON_WRAP) {
1634 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1635 buffer_strcat(wb, "[\n");
1636 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1637 buffer_strcat(wb, "\n]");
1638 rrdr_json_wrapper_end(r, wb, format, options, 0);
1641 wb->contenttype = CT_TEXT_PLAIN;
1642 buffer_strcat(wb, "[\n");
1643 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1644 buffer_strcat(wb, "\n]");
1648 case DATASOURCE_TSV:
1649 if(options & RRDR_OPTION_JSON_WRAP) {
1650 wb->contenttype = CT_APPLICATION_JSON;
1651 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1652 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1653 rrdr_json_wrapper_end(r, wb, format, options, 1);
1656 wb->contenttype = CT_TEXT_PLAIN;
1657 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1661 case DATASOURCE_HTML:
1662 if(options & RRDR_OPTION_JSON_WRAP) {
1663 wb->contenttype = CT_APPLICATION_JSON;
1664 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1665 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1666 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1667 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1668 rrdr_json_wrapper_end(r, wb, format, options, 1);
1671 wb->contenttype = CT_TEXT_HTML;
1672 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1673 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1674 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1678 case DATASOURCE_DATATABLE_JSONP:
1679 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1681 if(options & RRDR_OPTION_JSON_WRAP)
1682 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1684 rrdr2json(r, wb, options, 1);
1686 if(options & RRDR_OPTION_JSON_WRAP)
1687 rrdr_json_wrapper_end(r, wb, format, options, 0);
1690 case DATASOURCE_DATATABLE_JSON:
1691 wb->contenttype = CT_APPLICATION_JSON;
1693 if(options & RRDR_OPTION_JSON_WRAP)
1694 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1696 rrdr2json(r, wb, options, 1);
1698 if(options & RRDR_OPTION_JSON_WRAP)
1699 rrdr_json_wrapper_end(r, wb, format, options, 0);
1702 case DATASOURCE_JSONP:
1703 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1704 if(options & RRDR_OPTION_JSON_WRAP)
1705 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1707 rrdr2json(r, wb, options, 0);
1709 if(options & RRDR_OPTION_JSON_WRAP)
1710 rrdr_json_wrapper_end(r, wb, format, options, 0);
1713 case DATASOURCE_JSON:
1715 wb->contenttype = CT_APPLICATION_JSON;
1717 if(options & RRDR_OPTION_JSON_WRAP)
1718 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1720 rrdr2json(r, wb, options, 0);
1722 if(options & RRDR_OPTION_JSON_WRAP)
1723 rrdr_json_wrapper_end(r, wb, format, options, 0);
1731 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)
1734 pthread_rwlock_rdlock(&st->rwlock);
1737 // -------------------------------------------------------------------------
1738 // switch from JSON to google JSON
1743 case DATASOURCE_DATATABLE_JSON:
1744 case DATASOURCE_DATATABLE_JSONP:
1749 case DATASOURCE_JSON:
1755 // -------------------------------------------------------------------------
1756 // validate the parameters
1758 if(points < 1) points = 1;
1759 if(group < 1) group = 1;
1761 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1762 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1766 // our return value (the last timestamp printed)
1767 // this is required to detect re-transmit in google JSONP
1768 time_t last_timestamp = 0;
1771 // -------------------------------------------------------------------------
1772 // find how many dimensions we have
1776 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1778 pthread_rwlock_unlock(&st->rwlock);
1779 buffer_strcat(wb, "No dimensions yet.");
1784 // -------------------------------------------------------------------------
1785 // prepare various strings, to speed up the loop
1787 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);
1788 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1789 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1790 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1791 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1792 char post_value[21]; strcpy(post_value, "}");
1795 // -------------------------------------------------------------------------
1796 // checks for debugging
1799 debug(D_RRD_STATS, "%s first_entry_t = %ld, last_entry_t = %ld, duration = %ld, after = %ld, before = %ld, duration = %ld, entries_to_show = %ld, group = %ld"
1801 , rrdset_first_entry_t(st)
1802 , rrdset_last_entry_t(st)
1803 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1812 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
1814 if((before - after) > st->entries * st->update_every)
1815 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%ld) is higher than the capacity of the database (%ld)", st->name, before - after, st->entries * st->update_every);
1819 // -------------------------------------------------------------------------
1820 // temp arrays for keeping values per dimension
1822 calculated_number group_values[dimensions]; // keep sums when grouping
1823 int print_hidden[dimensions]; // keep hidden flags
1824 int found_non_zero[dimensions];
1825 int found_non_existing[dimensions];
1828 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1829 group_values[c] = 0;
1830 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1831 found_non_zero[c] = 0;
1832 found_non_existing[c] = 0;
1836 // 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);
1837 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1838 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1840 // -------------------------------------------------------------------------
1841 // remove dimensions that contain only zeros
1844 if(only_non_zero) max_loop = 2;
1846 for(; max_loop ; max_loop--) {
1848 // -------------------------------------------------------------------------
1849 // print the JSON header
1851 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1852 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);
1853 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);
1854 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);
1856 // print the header for each dimension
1857 // and update the print_hidden array for the dimensions that should be hidden
1859 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1860 if(!print_hidden[c]) {
1862 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);
1866 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);
1869 // print the begin of row data
1870 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1873 // -------------------------------------------------------------------------
1876 int annotate_reset = 0;
1877 int annotation_count = 0;
1879 long t = rrdset_time2slot(st, before),
1880 stop_at_t = rrdset_time2slot(st, after),
1885 time_t now = rrdset_slot2time(st, t),
1886 dt = st->update_every;
1888 long count = 0, printed = 0, group_count = 0;
1891 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%u before:%u, points:%ld, group:%ld, CHART cur:%ld first: %u last:%u, CALC start_t:%ld, stop_t:%ld"
1898 , (uint32_t)rrdset_first_entry_t(st)
1899 , (uint32_t)rrdset_last_entry_t(st)
1905 for(; !stop_now ; now -= dt, t--, counter++) {
1906 if(t < 0) t = st->entries - 1;
1907 if(t == stop_at_t) stop_now = counter;
1911 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
1918 , (group_count + 1 == group)?"PRINT":" - "
1919 , (now >= after && now <= before)?"RANGE":" - "
1923 // make sure we return data in the proper time range
1924 if(now > before) continue;
1925 if(now < after) break;
1927 //if(rrdset_slot2time(st, t) != now)
1928 // 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));
1933 // check if we have to print this now
1934 if(group_count == group) {
1935 if(printed >= points) {
1936 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1940 // generate the local date time
1941 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1942 if(!tm) { error("localtime() failed."); continue; }
1943 if(now > last_timestamp) last_timestamp = now;
1945 if(printed) buffer_strcat(wb, "]},\n");
1946 buffer_strcat(wb, pre_date);
1947 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1948 buffer_strcat(wb, post_date);
1953 // do the calculations
1954 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1955 storage_number n = rd->values[t];
1956 calculated_number value = unpack_storage_number(n);
1958 if(!does_storage_number_exist(n)) {
1960 found_non_existing[c]++;
1962 if(did_storage_number_reset(n)) annotate_reset = 1;
1964 switch(group_method) {
1966 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1970 group_values[c] += value;
1975 group_values[c] += value;
1976 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
1982 if(annotate_reset) {
1984 buffer_strcat(wb, overflow_annotation);
1988 buffer_strcat(wb, normal_annotation);
1991 for(c = 0 ; c < dimensions ; c++) {
1992 if(found_non_existing[c] == group_count) {
1993 // all entries are non-existing
1995 buffer_strcat(wb, pre_value);
1996 buffer_strcat(wb, "null");
1997 buffer_strcat(wb, post_value);
1999 else if(!print_hidden[c]) {
2001 buffer_strcat(wb, pre_value);
2002 buffer_rrd_value(wb, group_values[c]);
2003 buffer_strcat(wb, post_value);
2005 if(group_values[c]) found_non_zero[c]++;
2008 // reset them for the next loop
2009 group_values[c] = 0;
2010 found_non_existing[c] = 0;
2013 // if all dimensions are hidden, print a null
2015 buffer_strcat(wb, pre_value);
2016 buffer_strcat(wb, "null");
2017 buffer_strcat(wb, post_value);
2025 if(printed) buffer_strcat(wb, "]}");
2026 buffer_strcat(wb, "\n ]\n}\n");
2028 if(only_non_zero && max_loop > 1) {
2030 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2031 group_values[c] = 0;
2032 found_non_existing[c] = 0;
2034 if(!print_hidden[c] && !found_non_zero[c]) {
2036 print_hidden[c] = 1;
2040 if(changed) buffer_flush(wb);
2047 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2049 pthread_rwlock_unlock(&st->rwlock);
2050 return last_timestamp;