3 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
5 pthread_rwlock_rdlock(&st->rwlock);
9 "\t\t\t\"id\": \"%s\",\n"
10 "\t\t\t\"name\": \"%s\",\n"
11 "\t\t\t\"type\": \"%s\",\n"
12 "\t\t\t\"family\": \"%s\",\n"
13 "\t\t\t\"context\": \"%s\",\n"
14 "\t\t\t\"title\": \"%s\",\n"
15 "\t\t\t\"priority\": %ld,\n"
16 "\t\t\t\"enabled\": %s,\n"
17 "\t\t\t\"units\": \"%s\",\n"
18 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
19 "\t\t\t\"chart_type\": \"%s\",\n"
20 "\t\t\t\"duration\": %ld,\n"
21 "\t\t\t\"first_entry\": %ld,\n"
22 "\t\t\t\"last_entry\": %ld,\n"
23 "\t\t\t\"update_every\": %d,\n"
24 "\t\t\t\"dimensions\": {\n"
32 , st->enabled?"true":"false"
35 , rrdset_type_name(st->chart_type)
36 , st->entries * st->update_every
37 , rrdset_first_entry_t(st)
38 , rrdset_last_entry_t(st)
42 unsigned long memory = st->memsize;
46 for(rd = st->dimensions; rd ; rd = rd->next) {
47 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
63 buffer_rrd_value(wb, st->green);
64 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
65 buffer_rrd_value(wb, st->red);
71 pthread_rwlock_unlock(&st->rwlock);
74 void rrd_stats_api_v1_charts(BUFFER *wb)
79 buffer_sprintf(wb, "{\n"
80 "\t\"hostname\": \"%s\""
81 ",\n\t\"update_every\": %d"
82 ",\n\t\"history\": %d"
86 , rrd_default_history_entries
89 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
90 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
91 if(st->enabled && st->dimensions) {
92 if(c) buffer_strcat(wb, ",");
93 buffer_strcat(wb, "\n\t\t\"");
94 buffer_strcat(wb, st->id);
95 buffer_strcat(wb, "\": ");
96 rrd_stats_api_v1_chart(st, wb);
100 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
102 buffer_strcat(wb, "\n\t}\n}\n");
106 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
108 time_t now = time(NULL);
110 pthread_rwlock_rdlock(&st->rwlock);
114 "\t\t\t\"id\": \"%s\",\n"
115 "\t\t\t\"name\": \"%s\",\n"
116 "\t\t\t\"type\": \"%s\",\n"
117 "\t\t\t\"family\": \"%s\",\n"
118 "\t\t\t\"context\": \"%s\",\n"
119 "\t\t\t\"title\": \"%s\",\n"
120 "\t\t\t\"priority\": %ld,\n"
121 "\t\t\t\"enabled\": %d,\n"
122 "\t\t\t\"units\": \"%s\",\n"
123 "\t\t\t\"url\": \"/data/%s/%s\",\n"
124 "\t\t\t\"chart_type\": \"%s\",\n"
125 "\t\t\t\"counter\": %lu,\n"
126 "\t\t\t\"entries\": %ld,\n"
127 "\t\t\t\"first_entry_t\": %ld,\n"
128 "\t\t\t\"last_entry\": %lu,\n"
129 "\t\t\t\"last_entry_t\": %ld,\n"
130 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
131 "\t\t\t\"update_every\": %d,\n"
132 "\t\t\t\"isdetail\": %d,\n"
133 "\t\t\t\"usec_since_last_update\": %llu,\n"
134 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
135 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
136 "\t\t\t\"dimensions\": [\n"
146 , st->name, options?options:""
147 , rrdset_type_name(st->chart_type)
150 , rrdset_first_entry_t(st)
151 , rrdset_last_slot(st)
152 , rrdset_last_entry_t(st)
153 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
156 , st->usec_since_last_update
157 , st->collected_total
158 , st->last_collected_total
161 unsigned long memory = st->memsize;
164 for(rd = st->dimensions; rd ; rd = rd->next) {
166 memory += rd->memsize;
170 "\t\t\t\t\t\"id\": \"%s\",\n"
171 "\t\t\t\t\t\"name\": \"%s\",\n"
172 "\t\t\t\t\t\"entries\": %ld,\n"
173 "\t\t\t\t\t\"isHidden\": %d,\n"
174 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
175 "\t\t\t\t\t\"multiplier\": %ld,\n"
176 "\t\t\t\t\t\"divisor\": %ld,\n"
177 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
178 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
179 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
180 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
181 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
182 "\t\t\t\t\t\"memory\": %lu\n"
187 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
188 , rrddim_algorithm_name(rd->algorithm)
191 , rd->last_collected_time.tv_sec
192 , rd->collected_value
193 , rd->calculated_value
194 , rd->last_collected_value
195 , rd->last_calculated_value
203 "\t\t\t\"memory\" : %lu\n"
208 pthread_rwlock_unlock(&st->rwlock);
212 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
213 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
215 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
217 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
218 rrd_stats_one_json(st, options, wb);
219 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
222 void rrd_stats_all_json(BUFFER *wb)
224 unsigned long memory = 0;
228 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
230 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
231 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
232 if(st->enabled && st->dimensions) {
233 if(c) buffer_strcat(wb, ",\n");
234 memory += rrd_stats_one_json(st, NULL, wb);
238 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
240 buffer_sprintf(wb, "\n\t],\n"
241 "\t\"hostname\": \"%s\",\n"
242 "\t\"update_every\": %d,\n"
243 "\t\"history\": %d,\n"
244 "\t\"memory\": %lu\n"
248 , rrd_default_history_entries
255 // ----------------------------------------------------------------------------
257 // RRDR dimension options
258 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
259 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
260 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
261 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
263 // RRDR result options
264 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
265 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
267 typedef struct rrdresult {
268 RRDSET *st; // the chart this result refers to
270 uint32_t result_options; // RRDR_RESULT_OPTION_*
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 int 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 uint32_t hash = simple_hash(tok);
372 // find it and enable it
373 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
374 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
375 r->od[c] &= ~RRDR_HIDDEN;
377 // since the user needs this dimension
378 // make it appear as NONZERO, to return it
379 // even if the dimension has only zeros
380 r->od[c] |= RRDR_NONZERO;
386 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
389 case DATASOURCE_JSON:
390 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
393 case DATASOURCE_DATATABLE_JSON:
394 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
397 case DATASOURCE_DATATABLE_JSONP:
398 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
401 case DATASOURCE_JSONP:
402 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
406 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
410 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
414 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
417 case DATASOURCE_HTML:
418 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
421 case DATASOURCE_JS_ARRAY:
422 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
425 case DATASOURCE_SSV_COMMA:
426 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
430 buffer_strcat(wb, "unknown");
435 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
437 if(options & RRDR_OPTION_NONZERO) {
441 // the caller wants specific dimensions
442 // disable NONZERO option
443 // to make sure we don't accidentally prevent
444 // the specific dimensions from being returned
448 // find how many dimensions are not zero
451 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
452 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
453 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
458 // if with nonzero we get i = 0 (no dimensions will be returned)
459 // disable nonzero to show all dimensions
460 if(!i) options &= ~RRDR_OPTION_NONZERO;
466 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
468 long rows = rrdr_rows(r);
472 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
473 char kq[2] = "", // key quote
474 sq[2] = ""; // string quote
476 if( options & RRDR_OPTION_GOOGLE_JSON ) {
485 buffer_sprintf(wb, "{\n"
488 " %sname%s: %s%s%s,\n"
489 " %sview_update_every%s: %d,\n"
490 " %supdate_every%s: %d,\n"
491 " %sfirst_entry%s: %u,\n"
492 " %slast_entry%s: %u,\n"
495 " %sdimension_names%s: ["
497 , kq, kq, sq, r->st->id, sq
498 , kq, kq, sq, r->st->name, sq
499 , kq, kq, r->update_every
500 , kq, kq, r->st->update_every
501 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
502 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
503 , kq, kq, (uint32_t)r->before
504 , kq, kq, (uint32_t)r->after
507 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
508 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
509 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
511 if(i) buffer_strcat(wb, ", ");
512 buffer_strcat(wb, sq);
513 buffer_strcat(wb, rd->name);
514 buffer_strcat(wb, sq);
518 #ifdef NETDATA_INTERNAL_CHECKS
519 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
522 buffer_strcat(wb, sq);
523 buffer_strcat(wb, "no data");
524 buffer_strcat(wb, sq);
527 buffer_sprintf(wb, "],\n"
528 " %sdimension_ids%s: ["
531 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
532 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
533 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
535 if(i) buffer_strcat(wb, ", ");
536 buffer_strcat(wb, sq);
537 buffer_strcat(wb, rd->id);
538 buffer_strcat(wb, sq);
543 buffer_strcat(wb, sq);
544 buffer_strcat(wb, "no data");
545 buffer_strcat(wb, sq);
548 buffer_sprintf(wb, "],\n"
549 " %slatest_values%s: ["
552 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
553 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
554 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
556 if(i) buffer_strcat(wb, ", ");
559 storage_number n = rd->values[rrdset_last_slot(r->st)];
561 if(!does_storage_number_exist(n))
562 buffer_strcat(wb, "null");
564 buffer_rrd_value(wb, unpack_storage_number(n));
568 buffer_strcat(wb, "null");
571 buffer_sprintf(wb, "],\n"
572 " %sview_latest_values%s: ["
577 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
578 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
579 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
581 if(i) buffer_strcat(wb, ", ");
584 calculated_number *cn = &r->v[ (0) * r->d ];
585 uint8_t *co = &r->o[ (0) * r->d ];
587 if(co[c] & RRDR_EMPTY)
588 buffer_strcat(wb, "null");
590 buffer_rrd_value(wb, cn[c]);
595 buffer_strcat(wb, "null");
598 buffer_sprintf(wb, "],\n"
599 " %sdimensions%s: %ld,\n"
600 " %spoints%s: %ld,\n"
607 rrdr_buffer_print_format(wb, format);
609 buffer_sprintf(wb, "%s,\n"
615 if(string_value) buffer_strcat(wb, sq);
616 //info("JSONWRAPPER(): %s: END", r->st->id);
619 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
623 char kq[2] = "", // key quote
624 sq[2] = ""; // string quote
626 if( options & RRDR_OPTION_GOOGLE_JSON ) {
635 if(string_value) buffer_strcat(wb, sq);
637 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
638 buffer_rrd_value(wb, r->min);
639 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
640 buffer_rrd_value(wb, r->max);
641 buffer_strcat(wb, "\n}\n");
644 #define JSON_DATES_JS 1
645 #define JSON_DATES_TIMESTAMP 2
647 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
649 //info("RRD2JSON(): %s: BEGIN", r->st->id);
650 int row_annotations = 0, dates, dates_with_new = 0;
651 char kq[2] = "", // key quote
652 sq[2] = "", // string quote
653 pre_label[101] = "", // before each label
654 post_label[101] = "", // after each label
655 pre_date[101] = "", // the beginning of line, to the date
656 post_date[101] = "", // closing the date
657 pre_value[101] = "", // before each value
658 post_value[101] = "", // after each value
659 post_line[101] = "", // at the end of each row
660 normal_annotation[201] = "", // default row annotation
661 overflow_annotation[201] = "", // overflow row annotation
662 data_begin[101] = "", // between labels and values
663 finish[101] = ""; // at the end of everything
666 dates = JSON_DATES_JS;
667 if( options & RRDR_OPTION_GOOGLE_JSON ) {
676 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
677 snprintfz(post_date, 100, "%s}", sq);
678 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
679 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
680 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
681 strcpy(post_value, "}");
682 strcpy(post_line, "]}");
683 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
684 strcpy(finish, "\n ]\n}");
686 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);
687 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
689 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
690 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);
691 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);
692 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);
694 // remove the valueobjects flag
695 // google wants its own keys
696 if(options & RRDR_OPTION_OBJECTSROWS)
697 options &= ~RRDR_OPTION_OBJECTSROWS;
702 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
703 dates = JSON_DATES_TIMESTAMP;
707 dates = JSON_DATES_JS;
710 if( options & RRDR_OPTION_OBJECTSROWS )
711 strcpy(pre_date, " { ");
713 strcpy(pre_date, " [ ");
714 strcpy(pre_label, ", \"");
715 strcpy(post_label, "\"");
716 strcpy(pre_value, ", ");
717 if( options & RRDR_OPTION_OBJECTSROWS )
718 strcpy(post_line, "}");
720 strcpy(post_line, "]");
721 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
722 strcpy(finish, "\n ]\n}");
724 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
725 buffer_sprintf(wb, "%stime%s", sq, sq);
728 // -------------------------------------------------------------------------
729 // print the JSON header
734 // print the header lines
735 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
736 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
737 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
739 buffer_strcat(wb, pre_label);
740 buffer_strcat(wb, rd->name);
741 buffer_strcat(wb, post_label);
745 buffer_strcat(wb, pre_label);
746 buffer_strcat(wb, "no data");
747 buffer_strcat(wb, post_label);
750 // print the begin of row data
751 buffer_strcat(wb, data_begin);
753 // if all dimensions are hidden, print a null
755 buffer_strcat(wb, finish);
759 long start = 0, end = rrdr_rows(r), step = 1;
760 if((options & RRDR_OPTION_REVERSED)) {
761 start = rrdr_rows(r) - 1;
766 // for each line in the array
767 calculated_number total = 1;
768 for(i = start; i != end ;i += step) {
769 calculated_number *cn = &r->v[ i * r->d ];
770 uint8_t *co = &r->o[ i * r->d ];
772 time_t now = r->t[i];
774 if(dates == JSON_DATES_JS) {
775 // generate the local date time
776 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
777 if(!tm) { error("localtime_r() failed."); continue; }
779 if(likely(i != start)) buffer_strcat(wb, ",\n");
780 buffer_strcat(wb, pre_date);
782 if( options & RRDR_OPTION_OBJECTSROWS )
783 buffer_sprintf(wb, "%stime%s: ", kq, kq);
786 buffer_strcat(wb, "new ");
788 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
790 buffer_strcat(wb, post_date);
792 if(row_annotations) {
793 // google supports one annotation per row
794 int annotation_found = 0;
795 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
796 if(co[c] & RRDR_RESET) {
797 buffer_strcat(wb, overflow_annotation);
798 annotation_found = 1;
802 if(!annotation_found)
803 buffer_strcat(wb, normal_annotation);
807 // print the timestamp of the line
808 if(likely(i != start)) buffer_strcat(wb, ",\n");
809 buffer_strcat(wb, pre_date);
811 if( options & RRDR_OPTION_OBJECTSROWS )
812 buffer_sprintf(wb, "%stime%s: ", kq, kq);
814 buffer_rrd_value(wb, (calculated_number)r->t[i]);
816 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
818 buffer_strcat(wb, post_date);
821 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
823 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
824 calculated_number n = cn[c];
826 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
831 // prevent a division by zero
832 if(total == 0) total = 1;
835 // for each dimension
836 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
837 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
838 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
840 calculated_number n = cn[c];
842 buffer_strcat(wb, pre_value);
844 if( options & RRDR_OPTION_OBJECTSROWS )
845 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
847 if(co[c] & RRDR_EMPTY) {
848 if(options & RRDR_OPTION_NULL2ZERO)
849 buffer_strcat(wb, "0");
851 buffer_strcat(wb, "null");
854 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
857 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
860 buffer_rrd_value(wb, n);
863 buffer_strcat(wb, post_value);
866 buffer_strcat(wb, post_line);
869 buffer_strcat(wb, finish);
870 //info("RRD2JSON(): %s: END", r->st->id);
873 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
875 //info("RRD2CSV(): %s: BEGIN", r->st->id);
879 // print the csv header
880 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
881 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
882 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
885 buffer_strcat(wb, startline);
886 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
887 buffer_strcat(wb, "time");
888 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
890 buffer_strcat(wb, separator);
891 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
892 buffer_strcat(wb, d->name);
893 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
896 buffer_strcat(wb, endline);
899 // no dimensions present
903 long start = 0, end = rrdr_rows(r), step = 1;
904 if((options & RRDR_OPTION_REVERSED)) {
905 start = rrdr_rows(r) - 1;
910 // for each line in the array
911 calculated_number total = 1;
912 for(i = start; i != end ;i += step) {
913 calculated_number *cn = &r->v[ i * r->d ];
914 uint8_t *co = &r->o[ i * r->d ];
916 buffer_strcat(wb, betweenlines);
917 buffer_strcat(wb, startline);
919 time_t now = r->t[i];
921 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
922 // print the timestamp of the line
923 buffer_rrd_value(wb, (calculated_number)now);
925 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
928 // generate the local date time
929 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
930 if(!tm) { error("localtime() failed."); continue; }
931 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
934 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
936 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
937 calculated_number n = cn[c];
939 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
944 // prevent a division by zero
945 if(total == 0) total = 1;
948 // for each dimension
949 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
950 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
951 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
953 buffer_strcat(wb, separator);
955 calculated_number n = cn[c];
957 if(co[c] & RRDR_EMPTY) {
958 if(options & RRDR_OPTION_NULL2ZERO)
959 buffer_strcat(wb, "0");
961 buffer_strcat(wb, "null");
964 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
967 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
970 buffer_rrd_value(wb, n);
974 buffer_strcat(wb, endline);
976 //info("RRD2CSV(): %s: END", r->st->id);
979 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
983 calculated_number *cn = &r->v[ i * r->d ];
984 uint8_t *co = &r->o[ i * r->d ];
986 calculated_number sum = 0, min = 0, max = 0, v;
987 int all_null = 1, init = 1;
989 calculated_number total = 1;
990 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
992 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
993 calculated_number n = cn[c];
995 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1000 // prevent a division by zero
1001 if(total == 0) total = 1;
1004 // for each dimension
1005 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1006 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1007 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1009 calculated_number n = cn[c];
1011 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1014 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1015 n = n * 100 / total;
1017 if(unlikely(init)) {
1029 if(likely(!(co[c] & RRDR_EMPTY))) {
1034 if(n < min) min = n;
1035 if(n > max) max = n;
1038 if(unlikely(all_null)) {
1039 if(likely(all_values_are_null))
1040 *all_values_are_null = 1;
1044 if(likely(all_values_are_null))
1045 *all_values_are_null = 0;
1048 if(options & RRDR_OPTION_MIN2MAX)
1056 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1058 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1061 buffer_strcat(wb, prefix);
1062 long start = 0, end = rrdr_rows(r), step = 1;
1063 if((options & RRDR_OPTION_REVERSED)) {
1064 start = rrdr_rows(r) - 1;
1069 // for each line in the array
1070 for(i = start; i != end ;i += step) {
1071 int all_values_are_null = 0;
1072 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1074 if(likely(i != start)) {
1075 if(r->min > v) r->min = v;
1076 if(r->max < v) r->max = v;
1083 if(likely(i != start))
1084 buffer_strcat(wb, separator);
1086 if(all_values_are_null) {
1087 if(options & RRDR_OPTION_NULL2ZERO)
1088 buffer_strcat(wb, "0");
1090 buffer_strcat(wb, "null");
1093 buffer_rrd_value(wb, v);
1095 buffer_strcat(wb, suffix);
1096 //info("RRD2SSV(): %s: END", r->st->id);
1099 inline static calculated_number *rrdr_line_values(RRDR *r)
1101 return &r->v[ r->c * r->d ];
1104 inline static uint8_t *rrdr_line_options(RRDR *r)
1106 return &r->o[ r->c * r->d ];
1109 inline static int rrdr_line_init(RRDR *r, time_t t)
1113 if(unlikely(r->c >= r->n)) {
1114 error("requested to step above RRDR size for chart %s", r->st->name);
1124 inline static void rrdr_lock_rrdset(RRDR *r) {
1126 error("NULL value given!");
1130 pthread_rwlock_rdlock(&r->st->rwlock);
1134 inline static void rrdr_unlock_rrdset(RRDR *r) {
1136 error("NULL value given!");
1140 if(likely(r->has_st_lock)) {
1141 pthread_rwlock_unlock(&r->st->rwlock);
1146 inline static void rrdr_free(RRDR *r)
1149 error("NULL value given!");
1153 rrdr_unlock_rrdset(r);
1161 inline void rrdr_done(RRDR *r)
1167 static RRDR *rrdr_create(RRDSET *st, long n)
1170 error("NULL value given!");
1174 RRDR *r = callocz(1, sizeof(RRDR));
1177 rrdr_lock_rrdset(r);
1180 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1184 r->t = mallocz(n * sizeof(time_t));
1185 r->v = mallocz(n * r->d * sizeof(calculated_number));
1186 r->o = mallocz(n * r->d * sizeof(uint8_t));
1187 r->od = mallocz(r->d * sizeof(uint8_t));
1189 // set the hidden flag on hidden dimensions
1191 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1192 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1198 r->update_every = 1;
1203 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1205 int debug = st->debug;
1206 int absolute_period_requested = -1;
1208 time_t first_entry_t = rrdset_first_entry_t(st);
1209 time_t last_entry_t = rrdset_last_entry_t(st);
1211 if(before == 0 && after == 0) {
1212 before = last_entry_t;
1213 after = first_entry_t;
1214 absolute_period_requested = 0;
1217 // allow relative for before and after (smaller than 3 years)
1218 if(((before < 0)?-before:before) <= (3 * 365 * 86400)) {
1219 before = last_entry_t + before;
1220 absolute_period_requested = 0;
1223 if(((after < 0)?-after:after) <= (3 * 365 * 86400)) {
1224 if(after == 0) after = -st->update_every;
1225 after = before + after;
1226 absolute_period_requested = 0;
1229 if(absolute_period_requested == -1)
1230 absolute_period_requested = 1;
1232 // make sure they are within our timeframe
1233 if(before > last_entry_t) before = last_entry_t;
1234 if(before < first_entry_t) before = first_entry_t;
1236 if(after > last_entry_t) after = last_entry_t;
1237 if(after < first_entry_t) after = first_entry_t;
1239 // check if they are upside down
1240 if(after > before) {
1241 time_t tmp = before;
1246 // the duration of the chart
1247 time_t duration = before - after;
1248 long available_points = duration / st->update_every;
1250 if(duration <= 0 || available_points <= 0)
1251 return rrdr_create(st, 1);
1253 // check the wanted points
1254 if(points < 0) points = -points;
1255 if(points > available_points) points = available_points;
1256 if(points == 0) points = available_points;
1258 // calculate proper grouping of source data
1259 long group = available_points / points;
1260 if(group <= 0) group = 1;
1262 // round group to the closest integer
1263 if(available_points % points > points / 2) group++;
1265 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1266 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1267 long points_new = (before_new - after_new) / st->update_every / group;
1269 // find the starting and ending slots in our round robin db
1270 long start_at_slot = rrdset_time2slot(st, before_new),
1271 stop_at_slot = rrdset_time2slot(st, after_new);
1273 #ifdef NETDATA_INTERNAL_CHECKS
1274 if(after_new < first_entry_t) {
1275 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1277 if(after_new > last_entry_t) {
1278 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1280 if(before_new < first_entry_t) {
1281 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1283 if(before_new > last_entry_t) {
1284 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1286 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1287 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1289 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1290 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1292 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1293 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1297 //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);
1300 before = before_new;
1301 duration = before - after;
1302 points = points_new;
1305 // before = the end time of the calculation
1306 // after = the start time of the calculation
1307 // duration = the duration of the calculation
1308 // group = the number of source points to aggregate / group together
1309 // method = the method of grouping source points
1310 // points = the number of points to generate
1313 // -------------------------------------------------------------------------
1314 // initialize our result set
1316 RRDR *r = rrdr_create(st, points);
1318 #ifdef NETDATA_INTERNAL_CHECKS
1319 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);
1324 #ifdef NETDATA_INTERNAL_CHECKS
1325 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);
1330 if(absolute_period_requested == 1)
1331 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1333 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1335 // find how many dimensions we have
1336 long dimensions = r->d;
1339 // -------------------------------------------------------------------------
1340 // checks for debugging
1342 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"
1344 , (uint32_t)first_entry_t
1345 , (uint32_t)last_entry_t
1346 , (uint32_t)(last_entry_t - first_entry_t)
1349 , (uint32_t)duration
1355 // -------------------------------------------------------------------------
1356 // temp arrays for keeping values per dimension
1358 calculated_number last_values[dimensions]; // keep the last value of each dimension
1359 calculated_number group_values[dimensions]; // keep sums when grouping
1360 long group_counts[dimensions]; // keep the number of values added to group_values
1361 uint8_t group_options[dimensions];
1362 uint8_t found_non_zero[dimensions];
1368 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1370 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1371 group_counts[c] = 0;
1372 group_options[c] = 0;
1373 found_non_zero[c] = 0;
1377 // -------------------------------------------------------------------------
1380 time_t now = rrdset_slot2time(st, start_at_slot),
1381 dt = st->update_every,
1384 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"
1396 r->update_every = group * st->update_every;
1400 //info("RRD2RRDR(): %s: STARTING", st->id);
1402 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1403 for(; !stop_now ; now -= dt, slot--, counter++) {
1404 if(unlikely(slot < 0)) slot = st->entries - 1;
1405 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1407 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1414 , (group_count + 1 == group)?"PRINT":" - "
1415 , (now >= after && now <= before)?"RANGE":" - "
1418 // make sure we return data in the proper time range
1419 if(unlikely(now > before)) continue;
1420 if(unlikely(now < after)) break;
1422 if(unlikely(group_count == 0)) {
1423 group_start_t = now;
1427 if(unlikely(group_count == group)) {
1428 if(unlikely(added >= points)) break;
1432 // do the calculations
1433 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1434 storage_number n = rd->values[slot];
1435 if(unlikely(!does_storage_number_exist(n))) continue;
1439 calculated_number value = unpack_storage_number(n);
1440 if(likely(value != 0.0)) {
1441 group_options[c] |= RRDR_NONZERO;
1442 found_non_zero[c] = 1;
1445 if(unlikely(did_storage_number_reset(n)))
1446 group_options[c] |= RRDR_RESET;
1448 switch(group_method) {
1450 if(unlikely(isnan(group_values[c])) ||
1451 fabsl(value) < fabsl(group_values[c]))
1452 group_values[c] = value;
1456 if(unlikely(isnan(group_values[c])) ||
1457 fabsl(value) > fabsl(group_values[c]))
1458 group_values[c] = value;
1464 case GROUP_UNDEFINED:
1465 group_values[c] += value;
1468 case GROUP_INCREMENTAL_SUM:
1469 if(unlikely(slot == start_at_slot))
1470 last_values[c] = value;
1472 group_values[c] += last_values[c] - value;
1473 last_values[c] = value;
1479 if(unlikely(add_this)) {
1480 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1484 calculated_number *cn = rrdr_line_values(r);
1485 uint8_t *co = rrdr_line_options(r);
1487 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1489 // update the dimension options
1490 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1492 // store the specific point options
1493 co[c] = group_options[c];
1496 if(unlikely(group_counts[c] == 0)) {
1498 co[c] |= RRDR_EMPTY;
1499 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1502 switch(group_method) {
1505 if(unlikely(isnan(group_values[c])))
1508 cn[c] = group_values[c];
1509 group_values[c] = NAN;
1514 case GROUP_INCREMENTAL_SUM:
1515 cn[c] = group_values[c];
1516 group_values[c] = 0;
1521 case GROUP_UNDEFINED:
1522 cn[c] = group_values[c] / group_counts[c];
1523 group_values[c] = 0;
1527 if(cn[c] < r->min) r->min = cn[c];
1528 if(cn[c] > r->max) r->max = cn[c];
1531 // reset for the next loop
1532 group_counts[c] = 0;
1533 group_options[c] = 0;
1543 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1544 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1548 int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, const char *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *db_after, time_t *db_before, int *value_is_null)
1550 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1552 if(value_is_null) *value_is_null = 1;
1556 if(rrdr_rows(r) == 0) {
1559 if(db_after) *db_after = 0;
1560 if(db_before) *db_before = 0;
1561 if(value_is_null) *value_is_null = 1;
1566 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1567 wb->options |= WB_CONTENT_NO_CACHEABLE;
1568 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1569 wb->options |= WB_CONTENT_CACHEABLE;
1571 options = rrdr_check_options(r, options, dimensions);
1574 rrdr_disable_not_selected_dimensions(r, dimensions);
1576 if(db_after) *db_after = r->after;
1577 if(db_before) *db_before = r->before;
1579 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1580 *n = rrdr2value(r, i, options, value_is_null);
1586 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)
1588 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1590 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1594 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1595 wb->options |= WB_CONTENT_NO_CACHEABLE;
1596 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1597 wb->options |= WB_CONTENT_CACHEABLE;
1599 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1602 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1604 if(latest_timestamp && rrdr_rows(r) > 0)
1605 *latest_timestamp = r->before;
1608 case DATASOURCE_SSV:
1609 if(options & RRDR_OPTION_JSON_WRAP) {
1610 wb->contenttype = CT_APPLICATION_JSON;
1611 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1612 rrdr2ssv(r, wb, options, "", " ", "");
1613 rrdr_json_wrapper_end(r, wb, format, options, 1);
1616 wb->contenttype = CT_TEXT_PLAIN;
1617 rrdr2ssv(r, wb, options, "", " ", "");
1621 case DATASOURCE_SSV_COMMA:
1622 if(options & RRDR_OPTION_JSON_WRAP) {
1623 wb->contenttype = CT_APPLICATION_JSON;
1624 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1625 rrdr2ssv(r, wb, options, "", ",", "");
1626 rrdr_json_wrapper_end(r, wb, format, options, 1);
1629 wb->contenttype = CT_TEXT_PLAIN;
1630 rrdr2ssv(r, wb, options, "", ",", "");
1634 case DATASOURCE_JS_ARRAY:
1635 if(options & RRDR_OPTION_JSON_WRAP) {
1636 wb->contenttype = CT_APPLICATION_JSON;
1637 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1638 rrdr2ssv(r, wb, options, "[", ",", "]");
1639 rrdr_json_wrapper_end(r, wb, format, options, 0);
1642 wb->contenttype = CT_APPLICATION_JSON;
1643 rrdr2ssv(r, wb, options, "[", ",", "]");
1647 case DATASOURCE_CSV:
1648 if(options & RRDR_OPTION_JSON_WRAP) {
1649 wb->contenttype = CT_APPLICATION_JSON;
1650 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1651 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1652 rrdr_json_wrapper_end(r, wb, format, options, 1);
1655 wb->contenttype = CT_TEXT_PLAIN;
1656 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1660 case DATASOURCE_CSV_JSON_ARRAY:
1661 wb->contenttype = CT_APPLICATION_JSON;
1662 if(options & RRDR_OPTION_JSON_WRAP) {
1663 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1664 buffer_strcat(wb, "[\n");
1665 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1666 buffer_strcat(wb, "\n]");
1667 rrdr_json_wrapper_end(r, wb, format, options, 0);
1670 wb->contenttype = CT_TEXT_PLAIN;
1671 buffer_strcat(wb, "[\n");
1672 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1673 buffer_strcat(wb, "\n]");
1677 case DATASOURCE_TSV:
1678 if(options & RRDR_OPTION_JSON_WRAP) {
1679 wb->contenttype = CT_APPLICATION_JSON;
1680 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1681 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1682 rrdr_json_wrapper_end(r, wb, format, options, 1);
1685 wb->contenttype = CT_TEXT_PLAIN;
1686 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1690 case DATASOURCE_HTML:
1691 if(options & RRDR_OPTION_JSON_WRAP) {
1692 wb->contenttype = CT_APPLICATION_JSON;
1693 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1694 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1695 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1696 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1697 rrdr_json_wrapper_end(r, wb, format, options, 1);
1700 wb->contenttype = CT_TEXT_HTML;
1701 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1702 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1703 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1707 case DATASOURCE_DATATABLE_JSONP:
1708 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1710 if(options & RRDR_OPTION_JSON_WRAP)
1711 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1713 rrdr2json(r, wb, options, 1);
1715 if(options & RRDR_OPTION_JSON_WRAP)
1716 rrdr_json_wrapper_end(r, wb, format, options, 0);
1719 case DATASOURCE_DATATABLE_JSON:
1720 wb->contenttype = CT_APPLICATION_JSON;
1722 if(options & RRDR_OPTION_JSON_WRAP)
1723 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1725 rrdr2json(r, wb, options, 1);
1727 if(options & RRDR_OPTION_JSON_WRAP)
1728 rrdr_json_wrapper_end(r, wb, format, options, 0);
1731 case DATASOURCE_JSONP:
1732 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1733 if(options & RRDR_OPTION_JSON_WRAP)
1734 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1736 rrdr2json(r, wb, options, 0);
1738 if(options & RRDR_OPTION_JSON_WRAP)
1739 rrdr_json_wrapper_end(r, wb, format, options, 0);
1742 case DATASOURCE_JSON:
1744 wb->contenttype = CT_APPLICATION_JSON;
1746 if(options & RRDR_OPTION_JSON_WRAP)
1747 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1749 rrdr2json(r, wb, options, 0);
1751 if(options & RRDR_OPTION_JSON_WRAP)
1752 rrdr_json_wrapper_end(r, wb, format, options, 0);
1760 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)
1763 pthread_rwlock_rdlock(&st->rwlock);
1766 // -------------------------------------------------------------------------
1767 // switch from JSON to google JSON
1772 case DATASOURCE_DATATABLE_JSON:
1773 case DATASOURCE_DATATABLE_JSONP:
1778 case DATASOURCE_JSON:
1784 // -------------------------------------------------------------------------
1785 // validate the parameters
1787 if(points < 1) points = 1;
1788 if(group < 1) group = 1;
1790 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1791 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1795 // our return value (the last timestamp printed)
1796 // this is required to detect re-transmit in google JSONP
1797 time_t last_timestamp = 0;
1800 // -------------------------------------------------------------------------
1801 // find how many dimensions we have
1805 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1807 pthread_rwlock_unlock(&st->rwlock);
1808 buffer_strcat(wb, "No dimensions yet.");
1813 // -------------------------------------------------------------------------
1814 // prepare various strings, to speed up the loop
1816 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);
1817 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1818 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1819 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1820 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1821 char post_value[21]; strcpy(post_value, "}");
1824 // -------------------------------------------------------------------------
1825 // checks for debugging
1828 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"
1830 , rrdset_first_entry_t(st)
1831 , rrdset_last_entry_t(st)
1832 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1841 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
1843 if((before - after) > st->entries * st->update_every)
1844 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);
1848 // -------------------------------------------------------------------------
1849 // temp arrays for keeping values per dimension
1851 calculated_number group_values[dimensions]; // keep sums when grouping
1852 int print_hidden[dimensions]; // keep hidden flags
1853 int found_non_zero[dimensions];
1854 int found_non_existing[dimensions];
1857 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1858 group_values[c] = 0;
1859 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1860 found_non_zero[c] = 0;
1861 found_non_existing[c] = 0;
1865 // 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);
1866 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1867 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1869 // -------------------------------------------------------------------------
1870 // remove dimensions that contain only zeros
1873 if(only_non_zero) max_loop = 2;
1875 for(; max_loop ; max_loop--) {
1877 // -------------------------------------------------------------------------
1878 // print the JSON header
1880 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1881 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);
1882 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);
1883 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);
1885 // print the header for each dimension
1886 // and update the print_hidden array for the dimensions that should be hidden
1888 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1889 if(!print_hidden[c]) {
1891 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
1895 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);
1898 // print the begin of row data
1899 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1902 // -------------------------------------------------------------------------
1905 int annotate_reset = 0;
1906 int annotation_count = 0;
1908 long t = rrdset_time2slot(st, before),
1909 stop_at_t = rrdset_time2slot(st, after),
1914 time_t now = rrdset_slot2time(st, t),
1915 dt = st->update_every;
1917 long count = 0, printed = 0, group_count = 0;
1920 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"
1927 , (uint32_t)rrdset_first_entry_t(st)
1928 , (uint32_t)rrdset_last_entry_t(st)
1934 for(; !stop_now ; now -= dt, t--, counter++) {
1935 if(t < 0) t = st->entries - 1;
1936 if(t == stop_at_t) stop_now = counter;
1940 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
1947 , (group_count + 1 == group)?"PRINT":" - "
1948 , (now >= after && now <= before)?"RANGE":" - "
1952 // make sure we return data in the proper time range
1953 if(now > before) continue;
1954 if(now < after) break;
1956 //if(rrdset_slot2time(st, t) != now)
1957 // 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));
1962 // check if we have to print this now
1963 if(group_count == group) {
1964 if(printed >= points) {
1965 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1969 // generate the local date time
1970 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1971 if(!tm) { error("localtime() failed."); continue; }
1972 if(now > last_timestamp) last_timestamp = now;
1974 if(printed) buffer_strcat(wb, "]},\n");
1975 buffer_strcat(wb, pre_date);
1976 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1977 buffer_strcat(wb, post_date);
1982 // do the calculations
1983 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1984 storage_number n = rd->values[t];
1985 calculated_number value = unpack_storage_number(n);
1987 if(!does_storage_number_exist(n)) {
1989 found_non_existing[c]++;
1991 if(did_storage_number_reset(n)) annotate_reset = 1;
1993 switch(group_method) {
1995 if(abs(value) > abs(group_values[c])) group_values[c] = value;
1999 group_values[c] += value;
2004 group_values[c] += value;
2005 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2011 if(annotate_reset) {
2013 buffer_strcat(wb, overflow_annotation);
2017 buffer_strcat(wb, normal_annotation);
2020 for(c = 0 ; c < dimensions ; c++) {
2021 if(found_non_existing[c] == group_count) {
2022 // all entries are non-existing
2024 buffer_strcat(wb, pre_value);
2025 buffer_strcat(wb, "null");
2026 buffer_strcat(wb, post_value);
2028 else if(!print_hidden[c]) {
2030 buffer_strcat(wb, pre_value);
2031 buffer_rrd_value(wb, group_values[c]);
2032 buffer_strcat(wb, post_value);
2034 if(group_values[c]) found_non_zero[c]++;
2037 // reset them for the next loop
2038 group_values[c] = 0;
2039 found_non_existing[c] = 0;
2042 // if all dimensions are hidden, print a null
2044 buffer_strcat(wb, pre_value);
2045 buffer_strcat(wb, "null");
2046 buffer_strcat(wb, post_value);
2054 if(printed) buffer_strcat(wb, "]}");
2055 buffer_strcat(wb, "\n ]\n}\n");
2057 if(only_non_zero && max_loop > 1) {
2059 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2060 group_values[c] = 0;
2061 found_non_existing[c] = 0;
2063 if(!print_hidden[c] && !found_non_zero[c]) {
2065 print_hidden[c] = 1;
2069 if(changed) buffer_flush(wb);
2076 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2078 pthread_rwlock_unlock(&st->rwlock);
2079 return last_timestamp;