3 inline void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
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\" }"
61 if(dimensions_count) *dimensions_count += c;
62 if(memory_used) *memory_used += memory;
64 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
65 buffer_rrd_value(wb, st->green);
66 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
67 buffer_rrd_value(wb, st->red);
73 pthread_rwlock_unlock(&st->rwlock);
76 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
77 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
80 void rrd_stats_api_v1_charts(BUFFER *wb)
82 size_t c, dimensions = 0, memory = 0;
85 buffer_sprintf(wb, "{\n"
86 "\t\"hostname\": \"%s\""
87 ",\n\t\"update_every\": %d"
88 ",\n\t\"history\": %d"
92 , rrd_default_history_entries
95 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
96 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
97 if(st->enabled && st->dimensions) {
98 if(c) buffer_strcat(wb, ",");
99 buffer_strcat(wb, "\n\t\t\"");
100 buffer_strcat(wb, st->id);
101 buffer_strcat(wb, "\": ");
102 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
106 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
108 buffer_sprintf(wb, "\n\t}"
109 ",\n\t\"charts_count\": %zu"
110 ",\n\t\"dimensions_count\": %zu"
111 ",\n\t\"rrd_memory_bytes\": %zu"
120 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
122 time_t now = time(NULL);
124 pthread_rwlock_rdlock(&st->rwlock);
128 "\t\t\t\"id\": \"%s\",\n"
129 "\t\t\t\"name\": \"%s\",\n"
130 "\t\t\t\"type\": \"%s\",\n"
131 "\t\t\t\"family\": \"%s\",\n"
132 "\t\t\t\"context\": \"%s\",\n"
133 "\t\t\t\"title\": \"%s\",\n"
134 "\t\t\t\"priority\": %ld,\n"
135 "\t\t\t\"enabled\": %d,\n"
136 "\t\t\t\"units\": \"%s\",\n"
137 "\t\t\t\"url\": \"/data/%s/%s\",\n"
138 "\t\t\t\"chart_type\": \"%s\",\n"
139 "\t\t\t\"counter\": %lu,\n"
140 "\t\t\t\"entries\": %ld,\n"
141 "\t\t\t\"first_entry_t\": %ld,\n"
142 "\t\t\t\"last_entry\": %lu,\n"
143 "\t\t\t\"last_entry_t\": %ld,\n"
144 "\t\t\t\"last_entry_secs_ago\": %ld,\n"
145 "\t\t\t\"update_every\": %d,\n"
146 "\t\t\t\"isdetail\": %d,\n"
147 "\t\t\t\"usec_since_last_update\": %llu,\n"
148 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
149 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
150 "\t\t\t\"dimensions\": [\n"
160 , st->name, options?options:""
161 , rrdset_type_name(st->chart_type)
164 , rrdset_first_entry_t(st)
165 , rrdset_last_slot(st)
166 , rrdset_last_entry_t(st)
167 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
170 , st->usec_since_last_update
171 , st->collected_total
172 , st->last_collected_total
175 unsigned long memory = st->memsize;
178 for(rd = st->dimensions; rd ; rd = rd->next) {
180 memory += rd->memsize;
184 "\t\t\t\t\t\"id\": \"%s\",\n"
185 "\t\t\t\t\t\"name\": \"%s\",\n"
186 "\t\t\t\t\t\"entries\": %ld,\n"
187 "\t\t\t\t\t\"isHidden\": %d,\n"
188 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
189 "\t\t\t\t\t\"multiplier\": %ld,\n"
190 "\t\t\t\t\t\"divisor\": %ld,\n"
191 "\t\t\t\t\t\"last_entry_t\": %ld,\n"
192 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
193 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
194 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
195 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
196 "\t\t\t\t\t\"memory\": %lu\n"
201 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
202 , rrddim_algorithm_name(rd->algorithm)
205 , rd->last_collected_time.tv_sec
206 , rd->collected_value
207 , rd->calculated_value
208 , rd->last_collected_value
209 , rd->last_calculated_value
217 "\t\t\t\"memory\" : %lu\n"
222 pthread_rwlock_unlock(&st->rwlock);
226 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
227 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
229 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
231 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
232 rrd_stats_one_json(st, options, wb);
233 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
236 void rrd_stats_all_json(BUFFER *wb)
238 unsigned long memory = 0;
242 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
244 pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
245 for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
246 if(st->enabled && st->dimensions) {
247 if(c) buffer_strcat(wb, ",\n");
248 memory += rrd_stats_one_json(st, NULL, wb);
252 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
254 buffer_sprintf(wb, "\n\t],\n"
255 "\t\"hostname\": \"%s\",\n"
256 "\t\"update_every\": %d,\n"
257 "\t\"history\": %d,\n"
258 "\t\"memory\": %lu\n"
262 , rrd_default_history_entries
269 // ----------------------------------------------------------------------------
271 // RRDR dimension options
272 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
273 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
274 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
275 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
277 // RRDR result options
278 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
279 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
281 typedef struct rrdresult {
282 RRDSET *st; // the chart this result refers to
284 uint32_t result_options; // RRDR_RESULT_OPTION_*
286 int d; // the number of dimensions
287 long n; // the number of values in the arrays
288 long rows; // the number of rows used
290 uint8_t *od; // the options for the dimensions
292 time_t *t; // array of n timestamps
293 calculated_number *v; // array n x d values
294 uint8_t *o; // array n x d options
296 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
298 long group; // how many collected values were grouped for each row
299 int update_every; // what is the suggested update frequency in seconds
301 calculated_number min;
302 calculated_number max;
307 int has_st_lock; // if st is read locked by us
310 #define rrdr_rows(r) ((r)->rows)
313 static void rrdr_dump(RRDR *r)
318 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
320 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
321 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
324 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
325 , (r->od[c] & RRDR_RESET)?"RESET ":""
326 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
327 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
332 fprintf(stderr, "RRDR does not have any values in it.\n");
336 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
338 // for each line in the array
339 for(i = 0; i < r->rows ;i++) {
340 calculated_number *cn = &r->v[ i * r->d ];
341 uint8_t *co = &r->o[ i * r->d ];
343 // print the id and the timestamp of the line
344 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
346 // for each dimension
347 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
348 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
349 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
351 if(co[c] & RRDR_EMPTY)
352 fprintf(stderr, "null ");
354 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
356 , (co[c] & RRDR_EMPTY)?"E":" "
357 , (co[c] & RRDR_RESET)?"R":" "
358 , (co[c] & RRDR_HIDDEN)?"H":" "
359 , (co[c] & RRDR_NONZERO)?"N":" "
363 fprintf(stderr, "\n");
368 void rrdr_disable_not_selected_dimensions(RRDR *r, const char *dims)
370 char b[strlen(dims) + 1];
377 // disable all of them
378 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
379 r->od[c] |= RRDR_HIDDEN;
381 while(o && *o && (tok = mystrsep(&o, ",|"))) {
384 uint32_t hash = simple_hash(tok);
386 // find it and enable it
387 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
388 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || !strcmp(d->name, tok))) {
389 r->od[c] &= ~RRDR_HIDDEN;
391 // since the user needs this dimension
392 // make it appear as NONZERO, to return it
393 // even if the dimension has only zeros
394 r->od[c] |= RRDR_NONZERO;
400 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
403 case DATASOURCE_JSON:
404 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
407 case DATASOURCE_DATATABLE_JSON:
408 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
411 case DATASOURCE_DATATABLE_JSONP:
412 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
415 case DATASOURCE_JSONP:
416 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
420 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
424 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
428 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
431 case DATASOURCE_HTML:
432 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
435 case DATASOURCE_JS_ARRAY:
436 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
439 case DATASOURCE_SSV_COMMA:
440 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
444 buffer_strcat(wb, "unknown");
449 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
451 if(options & RRDR_OPTION_NONZERO) {
455 // the caller wants specific dimensions
456 // disable NONZERO option
457 // to make sure we don't accidentally prevent
458 // the specific dimensions from being returned
462 // find how many dimensions are not zero
465 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
466 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
467 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
472 // if with nonzero we get i = 0 (no dimensions will be returned)
473 // disable nonzero to show all dimensions
474 if(!i) options &= ~RRDR_OPTION_NONZERO;
480 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
482 long rows = rrdr_rows(r);
486 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
487 char kq[2] = "", // key quote
488 sq[2] = ""; // string quote
490 if( options & RRDR_OPTION_GOOGLE_JSON ) {
499 buffer_sprintf(wb, "{\n"
502 " %sname%s: %s%s%s,\n"
503 " %sview_update_every%s: %d,\n"
504 " %supdate_every%s: %d,\n"
505 " %sfirst_entry%s: %u,\n"
506 " %slast_entry%s: %u,\n"
509 " %sdimension_names%s: ["
511 , kq, kq, sq, r->st->id, sq
512 , kq, kq, sq, r->st->name, sq
513 , kq, kq, r->update_every
514 , kq, kq, r->st->update_every
515 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
516 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
517 , kq, kq, (uint32_t)r->before
518 , kq, kq, (uint32_t)r->after
521 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
522 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
523 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
525 if(i) buffer_strcat(wb, ", ");
526 buffer_strcat(wb, sq);
527 buffer_strcat(wb, rd->name);
528 buffer_strcat(wb, sq);
532 #ifdef NETDATA_INTERNAL_CHECKS
533 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
536 buffer_strcat(wb, sq);
537 buffer_strcat(wb, "no data");
538 buffer_strcat(wb, sq);
541 buffer_sprintf(wb, "],\n"
542 " %sdimension_ids%s: ["
545 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
546 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
547 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
549 if(i) buffer_strcat(wb, ", ");
550 buffer_strcat(wb, sq);
551 buffer_strcat(wb, rd->id);
552 buffer_strcat(wb, sq);
557 buffer_strcat(wb, sq);
558 buffer_strcat(wb, "no data");
559 buffer_strcat(wb, sq);
562 buffer_sprintf(wb, "],\n"
563 " %slatest_values%s: ["
566 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
567 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
568 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
570 if(i) buffer_strcat(wb, ", ");
573 storage_number n = rd->values[rrdset_last_slot(r->st)];
575 if(!does_storage_number_exist(n))
576 buffer_strcat(wb, "null");
578 buffer_rrd_value(wb, unpack_storage_number(n));
582 buffer_strcat(wb, "null");
585 buffer_sprintf(wb, "],\n"
586 " %sview_latest_values%s: ["
591 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
592 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
593 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
595 if(i) buffer_strcat(wb, ", ");
598 calculated_number *cn = &r->v[ (0) * r->d ];
599 uint8_t *co = &r->o[ (0) * r->d ];
601 if(co[c] & RRDR_EMPTY)
602 buffer_strcat(wb, "null");
604 buffer_rrd_value(wb, cn[c]);
609 buffer_strcat(wb, "null");
612 buffer_sprintf(wb, "],\n"
613 " %sdimensions%s: %ld,\n"
614 " %spoints%s: %ld,\n"
621 rrdr_buffer_print_format(wb, format);
623 buffer_sprintf(wb, "%s,\n"
629 if(string_value) buffer_strcat(wb, sq);
630 //info("JSONWRAPPER(): %s: END", r->st->id);
633 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
637 char kq[2] = "", // key quote
638 sq[2] = ""; // string quote
640 if( options & RRDR_OPTION_GOOGLE_JSON ) {
649 if(string_value) buffer_strcat(wb, sq);
651 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
652 buffer_rrd_value(wb, r->min);
653 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
654 buffer_rrd_value(wb, r->max);
655 buffer_strcat(wb, "\n}\n");
658 #define JSON_DATES_JS 1
659 #define JSON_DATES_TIMESTAMP 2
661 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
663 //info("RRD2JSON(): %s: BEGIN", r->st->id);
664 int row_annotations = 0, dates, dates_with_new = 0;
665 char kq[2] = "", // key quote
666 sq[2] = "", // string quote
667 pre_label[101] = "", // before each label
668 post_label[101] = "", // after each label
669 pre_date[101] = "", // the beginning of line, to the date
670 post_date[101] = "", // closing the date
671 pre_value[101] = "", // before each value
672 post_value[101] = "", // after each value
673 post_line[101] = "", // at the end of each row
674 normal_annotation[201] = "", // default row annotation
675 overflow_annotation[201] = "", // overflow row annotation
676 data_begin[101] = "", // between labels and values
677 finish[101] = ""; // at the end of everything
680 dates = JSON_DATES_JS;
681 if( options & RRDR_OPTION_GOOGLE_JSON ) {
690 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
691 snprintfz(post_date, 100, "%s}", sq);
692 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
693 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
694 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
695 strcpy(post_value, "}");
696 strcpy(post_line, "]}");
697 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
698 strcpy(finish, "\n ]\n}");
700 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);
701 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
703 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
704 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);
705 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);
706 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);
708 // remove the valueobjects flag
709 // google wants its own keys
710 if(options & RRDR_OPTION_OBJECTSROWS)
711 options &= ~RRDR_OPTION_OBJECTSROWS;
716 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
717 dates = JSON_DATES_TIMESTAMP;
721 dates = JSON_DATES_JS;
724 if( options & RRDR_OPTION_OBJECTSROWS )
725 strcpy(pre_date, " { ");
727 strcpy(pre_date, " [ ");
728 strcpy(pre_label, ", \"");
729 strcpy(post_label, "\"");
730 strcpy(pre_value, ", ");
731 if( options & RRDR_OPTION_OBJECTSROWS )
732 strcpy(post_line, "}");
734 strcpy(post_line, "]");
735 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
736 strcpy(finish, "\n ]\n}");
738 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
739 buffer_sprintf(wb, "%stime%s", sq, sq);
742 // -------------------------------------------------------------------------
743 // print the JSON header
748 // print the header lines
749 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
750 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
751 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
753 buffer_strcat(wb, pre_label);
754 buffer_strcat(wb, rd->name);
755 buffer_strcat(wb, post_label);
759 buffer_strcat(wb, pre_label);
760 buffer_strcat(wb, "no data");
761 buffer_strcat(wb, post_label);
764 // print the begin of row data
765 buffer_strcat(wb, data_begin);
767 // if all dimensions are hidden, print a null
769 buffer_strcat(wb, finish);
773 long start = 0, end = rrdr_rows(r), step = 1;
774 if((options & RRDR_OPTION_REVERSED)) {
775 start = rrdr_rows(r) - 1;
780 // for each line in the array
781 calculated_number total = 1;
782 for(i = start; i != end ;i += step) {
783 calculated_number *cn = &r->v[ i * r->d ];
784 uint8_t *co = &r->o[ i * r->d ];
786 time_t now = r->t[i];
788 if(dates == JSON_DATES_JS) {
789 // generate the local date time
790 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
791 if(!tm) { error("localtime_r() failed."); continue; }
793 if(likely(i != start)) buffer_strcat(wb, ",\n");
794 buffer_strcat(wb, pre_date);
796 if( options & RRDR_OPTION_OBJECTSROWS )
797 buffer_sprintf(wb, "%stime%s: ", kq, kq);
800 buffer_strcat(wb, "new ");
802 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
804 buffer_strcat(wb, post_date);
806 if(row_annotations) {
807 // google supports one annotation per row
808 int annotation_found = 0;
809 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
810 if(co[c] & RRDR_RESET) {
811 buffer_strcat(wb, overflow_annotation);
812 annotation_found = 1;
816 if(!annotation_found)
817 buffer_strcat(wb, normal_annotation);
821 // print the timestamp of the line
822 if(likely(i != start)) buffer_strcat(wb, ",\n");
823 buffer_strcat(wb, pre_date);
825 if( options & RRDR_OPTION_OBJECTSROWS )
826 buffer_sprintf(wb, "%stime%s: ", kq, kq);
828 buffer_rrd_value(wb, (calculated_number)r->t[i]);
830 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
832 buffer_strcat(wb, post_date);
835 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
837 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
838 calculated_number n = cn[c];
840 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
845 // prevent a division by zero
846 if(total == 0) total = 1;
849 // for each dimension
850 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
851 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
852 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
854 calculated_number n = cn[c];
856 buffer_strcat(wb, pre_value);
858 if( options & RRDR_OPTION_OBJECTSROWS )
859 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
861 if(co[c] & RRDR_EMPTY) {
862 if(options & RRDR_OPTION_NULL2ZERO)
863 buffer_strcat(wb, "0");
865 buffer_strcat(wb, "null");
868 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
871 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
874 buffer_rrd_value(wb, n);
877 buffer_strcat(wb, post_value);
880 buffer_strcat(wb, post_line);
883 buffer_strcat(wb, finish);
884 //info("RRD2JSON(): %s: END", r->st->id);
887 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
889 //info("RRD2CSV(): %s: BEGIN", r->st->id);
893 // print the csv header
894 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
895 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
896 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
899 buffer_strcat(wb, startline);
900 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
901 buffer_strcat(wb, "time");
902 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
904 buffer_strcat(wb, separator);
905 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
906 buffer_strcat(wb, d->name);
907 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
910 buffer_strcat(wb, endline);
913 // no dimensions present
917 long start = 0, end = rrdr_rows(r), step = 1;
918 if((options & RRDR_OPTION_REVERSED)) {
919 start = rrdr_rows(r) - 1;
924 // for each line in the array
925 calculated_number total = 1;
926 for(i = start; i != end ;i += step) {
927 calculated_number *cn = &r->v[ i * r->d ];
928 uint8_t *co = &r->o[ i * r->d ];
930 buffer_strcat(wb, betweenlines);
931 buffer_strcat(wb, startline);
933 time_t now = r->t[i];
935 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
936 // print the timestamp of the line
937 buffer_rrd_value(wb, (calculated_number)now);
939 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
942 // generate the local date time
943 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
944 if(!tm) { error("localtime() failed."); continue; }
945 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
948 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
950 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
951 calculated_number n = cn[c];
953 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
958 // prevent a division by zero
959 if(total == 0) total = 1;
962 // for each dimension
963 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
964 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
965 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
967 buffer_strcat(wb, separator);
969 calculated_number n = cn[c];
971 if(co[c] & RRDR_EMPTY) {
972 if(options & RRDR_OPTION_NULL2ZERO)
973 buffer_strcat(wb, "0");
975 buffer_strcat(wb, "null");
978 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
981 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
984 buffer_rrd_value(wb, n);
988 buffer_strcat(wb, endline);
990 //info("RRD2CSV(): %s: END", r->st->id);
993 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
997 calculated_number *cn = &r->v[ i * r->d ];
998 uint8_t *co = &r->o[ i * r->d ];
1000 calculated_number sum = 0, min = 0, max = 0, v;
1001 int all_null = 1, init = 1;
1003 calculated_number total = 1;
1004 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1006 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1007 calculated_number n = cn[c];
1009 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1014 // prevent a division by zero
1015 if(total == 0) total = 1;
1018 // for each dimension
1019 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1020 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1021 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1023 calculated_number n = cn[c];
1025 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1028 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1029 n = n * 100 / total;
1031 if(unlikely(init)) {
1043 if(likely(!(co[c] & RRDR_EMPTY))) {
1048 if(n < min) min = n;
1049 if(n > max) max = n;
1052 if(unlikely(all_null)) {
1053 if(likely(all_values_are_null))
1054 *all_values_are_null = 1;
1058 if(likely(all_values_are_null))
1059 *all_values_are_null = 0;
1062 if(options & RRDR_OPTION_MIN2MAX)
1070 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1072 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1075 buffer_strcat(wb, prefix);
1076 long start = 0, end = rrdr_rows(r), step = 1;
1077 if((options & RRDR_OPTION_REVERSED)) {
1078 start = rrdr_rows(r) - 1;
1083 // for each line in the array
1084 for(i = start; i != end ;i += step) {
1085 int all_values_are_null = 0;
1086 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1088 if(likely(i != start)) {
1089 if(r->min > v) r->min = v;
1090 if(r->max < v) r->max = v;
1097 if(likely(i != start))
1098 buffer_strcat(wb, separator);
1100 if(all_values_are_null) {
1101 if(options & RRDR_OPTION_NULL2ZERO)
1102 buffer_strcat(wb, "0");
1104 buffer_strcat(wb, "null");
1107 buffer_rrd_value(wb, v);
1109 buffer_strcat(wb, suffix);
1110 //info("RRD2SSV(): %s: END", r->st->id);
1113 inline static calculated_number *rrdr_line_values(RRDR *r)
1115 return &r->v[ r->c * r->d ];
1118 inline static uint8_t *rrdr_line_options(RRDR *r)
1120 return &r->o[ r->c * r->d ];
1123 inline static int rrdr_line_init(RRDR *r, time_t t)
1127 if(unlikely(r->c >= r->n)) {
1128 error("requested to step above RRDR size for chart %s", r->st->name);
1138 inline static void rrdr_lock_rrdset(RRDR *r) {
1140 error("NULL value given!");
1144 pthread_rwlock_rdlock(&r->st->rwlock);
1148 inline static void rrdr_unlock_rrdset(RRDR *r) {
1150 error("NULL value given!");
1154 if(likely(r->has_st_lock)) {
1155 pthread_rwlock_unlock(&r->st->rwlock);
1160 inline static void rrdr_free(RRDR *r)
1163 error("NULL value given!");
1167 rrdr_unlock_rrdset(r);
1175 static inline void rrdr_done(RRDR *r)
1181 static RRDR *rrdr_create(RRDSET *st, long n)
1184 error("NULL value given!");
1188 RRDR *r = callocz(1, sizeof(RRDR));
1191 rrdr_lock_rrdset(r);
1194 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
1198 r->t = mallocz(n * sizeof(time_t));
1199 r->v = mallocz(n * r->d * sizeof(calculated_number));
1200 r->o = mallocz(n * r->d * sizeof(uint8_t));
1201 r->od = mallocz(r->d * sizeof(uint8_t));
1203 // set the hidden flag on hidden dimensions
1205 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1206 if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
1212 r->update_every = 1;
1217 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1219 int debug = st->debug;
1220 int absolute_period_requested = -1;
1222 time_t first_entry_t = rrdset_first_entry_t(st);
1223 time_t last_entry_t = rrdset_last_entry_t(st);
1225 if(before == 0 && after == 0) {
1226 // dump the all the data
1227 before = last_entry_t;
1228 after = first_entry_t;
1229 absolute_period_requested = 0;
1232 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1233 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1234 if(abs(before) % st->update_every) {
1235 // make sure it is multiple of st->update_every
1236 if(before < 0) before = before - st->update_every - before % st->update_every;
1237 else before = before + st->update_every - before % st->update_every;
1239 if(before > 0) before = first_entry_t + before;
1240 else before = last_entry_t + before;
1241 absolute_period_requested = 0;
1244 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1245 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1246 if(after == 0) after = -st->update_every;
1247 if(abs(after) % st->update_every) {
1248 // make sure it is multiple of st->update_every
1249 if(after < 0) after = after - st->update_every - after % st->update_every;
1250 else after = after + st->update_every - after % st->update_every;
1252 after = before + after;
1253 absolute_period_requested = 0;
1256 if(absolute_period_requested == -1)
1257 absolute_period_requested = 1;
1259 // make sure they are within our timeframe
1260 if(before > last_entry_t) before = last_entry_t;
1261 if(before < first_entry_t) before = first_entry_t;
1263 if(after > last_entry_t) after = last_entry_t;
1264 if(after < first_entry_t) after = first_entry_t;
1266 // check if they are upside down
1267 if(after > before) {
1268 time_t tmp = before;
1273 // the duration of the chart
1274 time_t duration = before - after;
1275 long available_points = duration / st->update_every;
1277 if(duration <= 0 || available_points <= 0)
1278 return rrdr_create(st, 1);
1280 // check the wanted points
1281 if(points < 0) points = -points;
1282 if(points > available_points) points = available_points;
1283 if(points == 0) points = available_points;
1285 // calculate proper grouping of source data
1286 long group = available_points / points;
1287 if(group <= 0) group = 1;
1289 // round group to the closest integer
1290 if(available_points % points > points / 2) group++;
1292 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1293 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1294 long points_new = (before_new - after_new) / st->update_every / group;
1296 // find the starting and ending slots in our round robin db
1297 long start_at_slot = rrdset_time2slot(st, before_new),
1298 stop_at_slot = rrdset_time2slot(st, after_new);
1300 #ifdef NETDATA_INTERNAL_CHECKS
1301 if(after_new < first_entry_t) {
1302 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1304 if(after_new > last_entry_t) {
1305 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1307 if(before_new < first_entry_t) {
1308 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1310 if(before_new > last_entry_t) {
1311 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1313 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1314 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1316 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1317 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1319 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1320 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1324 //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);
1327 before = before_new;
1328 duration = before - after;
1329 points = points_new;
1332 // before = the end time of the calculation
1333 // after = the start time of the calculation
1334 // duration = the duration of the calculation
1335 // group = the number of source points to aggregate / group together
1336 // method = the method of grouping source points
1337 // points = the number of points to generate
1340 // -------------------------------------------------------------------------
1341 // initialize our result set
1343 RRDR *r = rrdr_create(st, points);
1345 #ifdef NETDATA_INTERNAL_CHECKS
1346 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);
1351 #ifdef NETDATA_INTERNAL_CHECKS
1352 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);
1357 if(absolute_period_requested == 1)
1358 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1360 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1362 // find how many dimensions we have
1363 long dimensions = r->d;
1366 // -------------------------------------------------------------------------
1367 // checks for debugging
1369 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"
1371 , (uint32_t)first_entry_t
1372 , (uint32_t)last_entry_t
1373 , (uint32_t)(last_entry_t - first_entry_t)
1376 , (uint32_t)duration
1382 // -------------------------------------------------------------------------
1383 // temp arrays for keeping values per dimension
1385 calculated_number last_values[dimensions]; // keep the last value of each dimension
1386 calculated_number group_values[dimensions]; // keep sums when grouping
1387 long group_counts[dimensions]; // keep the number of values added to group_values
1388 uint8_t group_options[dimensions];
1389 uint8_t found_non_zero[dimensions];
1395 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1397 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1398 group_counts[c] = 0;
1399 group_options[c] = 0;
1400 found_non_zero[c] = 0;
1404 // -------------------------------------------------------------------------
1407 time_t now = rrdset_slot2time(st, start_at_slot),
1408 dt = st->update_every,
1411 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"
1423 r->update_every = group * st->update_every;
1427 //info("RRD2RRDR(): %s: STARTING", st->id);
1429 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1430 for(; !stop_now ; now -= dt, slot--, counter++) {
1431 if(unlikely(slot < 0)) slot = st->entries - 1;
1432 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1434 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1441 , (group_count + 1 == group)?"PRINT":" - "
1442 , (now >= after && now <= before)?"RANGE":" - "
1445 // make sure we return data in the proper time range
1446 if(unlikely(now > before)) continue;
1447 if(unlikely(now < after)) break;
1449 if(unlikely(group_count == 0)) {
1450 group_start_t = now;
1454 if(unlikely(group_count == group)) {
1455 if(unlikely(added >= points)) break;
1459 // do the calculations
1460 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1461 storage_number n = rd->values[slot];
1462 if(unlikely(!does_storage_number_exist(n))) continue;
1466 calculated_number value = unpack_storage_number(n);
1467 if(likely(value != 0.0)) {
1468 group_options[c] |= RRDR_NONZERO;
1469 found_non_zero[c] = 1;
1472 if(unlikely(did_storage_number_reset(n)))
1473 group_options[c] |= RRDR_RESET;
1475 switch(group_method) {
1477 if(unlikely(isnan(group_values[c])) ||
1478 fabsl(value) < fabsl(group_values[c]))
1479 group_values[c] = value;
1483 if(unlikely(isnan(group_values[c])) ||
1484 fabsl(value) > fabsl(group_values[c]))
1485 group_values[c] = value;
1491 case GROUP_UNDEFINED:
1492 group_values[c] += value;
1495 case GROUP_INCREMENTAL_SUM:
1496 if(unlikely(slot == start_at_slot))
1497 last_values[c] = value;
1499 group_values[c] += last_values[c] - value;
1500 last_values[c] = value;
1506 if(unlikely(add_this)) {
1507 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1511 calculated_number *cn = rrdr_line_values(r);
1512 uint8_t *co = rrdr_line_options(r);
1514 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1516 // update the dimension options
1517 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1519 // store the specific point options
1520 co[c] = group_options[c];
1523 if(unlikely(group_counts[c] == 0)) {
1525 co[c] |= RRDR_EMPTY;
1526 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1529 switch(group_method) {
1532 if(unlikely(isnan(group_values[c])))
1535 cn[c] = group_values[c];
1536 group_values[c] = NAN;
1541 case GROUP_INCREMENTAL_SUM:
1542 cn[c] = group_values[c];
1543 group_values[c] = 0;
1548 case GROUP_UNDEFINED:
1549 cn[c] = group_values[c] / group_counts[c];
1550 group_values[c] = 0;
1554 if(cn[c] < r->min) r->min = cn[c];
1555 if(cn[c] > r->max) r->max = cn[c];
1558 // reset for the next loop
1559 group_counts[c] = 0;
1560 group_options[c] = 0;
1570 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1571 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1575 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)
1577 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1579 if(value_is_null) *value_is_null = 1;
1583 if(rrdr_rows(r) == 0) {
1586 if(db_after) *db_after = 0;
1587 if(db_before) *db_before = 0;
1588 if(value_is_null) *value_is_null = 1;
1593 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1594 buffer_no_cacheable(wb);
1595 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1596 buffer_cacheable(wb);
1598 options = rrdr_check_options(r, options, dimensions);
1601 rrdr_disable_not_selected_dimensions(r, dimensions);
1603 if(db_after) *db_after = r->after;
1604 if(db_before) *db_before = r->before;
1606 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1607 *n = rrdr2value(r, i, options, value_is_null);
1613 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)
1615 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1617 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1621 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1622 buffer_no_cacheable(wb);
1623 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1624 buffer_cacheable(wb);
1626 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1629 rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
1631 if(latest_timestamp && rrdr_rows(r) > 0)
1632 *latest_timestamp = r->before;
1635 case DATASOURCE_SSV:
1636 if(options & RRDR_OPTION_JSON_WRAP) {
1637 wb->contenttype = CT_APPLICATION_JSON;
1638 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1639 rrdr2ssv(r, wb, options, "", " ", "");
1640 rrdr_json_wrapper_end(r, wb, format, options, 1);
1643 wb->contenttype = CT_TEXT_PLAIN;
1644 rrdr2ssv(r, wb, options, "", " ", "");
1648 case DATASOURCE_SSV_COMMA:
1649 if(options & RRDR_OPTION_JSON_WRAP) {
1650 wb->contenttype = CT_APPLICATION_JSON;
1651 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1652 rrdr2ssv(r, wb, options, "", ",", "");
1653 rrdr_json_wrapper_end(r, wb, format, options, 1);
1656 wb->contenttype = CT_TEXT_PLAIN;
1657 rrdr2ssv(r, wb, options, "", ",", "");
1661 case DATASOURCE_JS_ARRAY:
1662 if(options & RRDR_OPTION_JSON_WRAP) {
1663 wb->contenttype = CT_APPLICATION_JSON;
1664 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1665 rrdr2ssv(r, wb, options, "[", ",", "]");
1666 rrdr_json_wrapper_end(r, wb, format, options, 0);
1669 wb->contenttype = CT_APPLICATION_JSON;
1670 rrdr2ssv(r, wb, options, "[", ",", "]");
1674 case DATASOURCE_CSV:
1675 if(options & RRDR_OPTION_JSON_WRAP) {
1676 wb->contenttype = CT_APPLICATION_JSON;
1677 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1678 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1679 rrdr_json_wrapper_end(r, wb, format, options, 1);
1682 wb->contenttype = CT_TEXT_PLAIN;
1683 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1687 case DATASOURCE_CSV_JSON_ARRAY:
1688 wb->contenttype = CT_APPLICATION_JSON;
1689 if(options & RRDR_OPTION_JSON_WRAP) {
1690 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1691 buffer_strcat(wb, "[\n");
1692 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1693 buffer_strcat(wb, "\n]");
1694 rrdr_json_wrapper_end(r, wb, format, options, 0);
1697 wb->contenttype = CT_TEXT_PLAIN;
1698 buffer_strcat(wb, "[\n");
1699 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1700 buffer_strcat(wb, "\n]");
1704 case DATASOURCE_TSV:
1705 if(options & RRDR_OPTION_JSON_WRAP) {
1706 wb->contenttype = CT_APPLICATION_JSON;
1707 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1708 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1709 rrdr_json_wrapper_end(r, wb, format, options, 1);
1712 wb->contenttype = CT_TEXT_PLAIN;
1713 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1717 case DATASOURCE_HTML:
1718 if(options & RRDR_OPTION_JSON_WRAP) {
1719 wb->contenttype = CT_APPLICATION_JSON;
1720 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1721 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1722 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1723 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1724 rrdr_json_wrapper_end(r, wb, format, options, 1);
1727 wb->contenttype = CT_TEXT_HTML;
1728 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1729 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1730 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1734 case DATASOURCE_DATATABLE_JSONP:
1735 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1737 if(options & RRDR_OPTION_JSON_WRAP)
1738 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1740 rrdr2json(r, wb, options, 1);
1742 if(options & RRDR_OPTION_JSON_WRAP)
1743 rrdr_json_wrapper_end(r, wb, format, options, 0);
1746 case DATASOURCE_DATATABLE_JSON:
1747 wb->contenttype = CT_APPLICATION_JSON;
1749 if(options & RRDR_OPTION_JSON_WRAP)
1750 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1752 rrdr2json(r, wb, options, 1);
1754 if(options & RRDR_OPTION_JSON_WRAP)
1755 rrdr_json_wrapper_end(r, wb, format, options, 0);
1758 case DATASOURCE_JSONP:
1759 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1760 if(options & RRDR_OPTION_JSON_WRAP)
1761 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1763 rrdr2json(r, wb, options, 0);
1765 if(options & RRDR_OPTION_JSON_WRAP)
1766 rrdr_json_wrapper_end(r, wb, format, options, 0);
1769 case DATASOURCE_JSON:
1771 wb->contenttype = CT_APPLICATION_JSON;
1773 if(options & RRDR_OPTION_JSON_WRAP)
1774 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1776 rrdr2json(r, wb, options, 0);
1778 if(options & RRDR_OPTION_JSON_WRAP)
1779 rrdr_json_wrapper_end(r, wb, format, options, 0);
1787 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)
1790 pthread_rwlock_rdlock(&st->rwlock);
1793 // -------------------------------------------------------------------------
1794 // switch from JSON to google JSON
1799 case DATASOURCE_DATATABLE_JSON:
1800 case DATASOURCE_DATATABLE_JSONP:
1805 case DATASOURCE_JSON:
1811 // -------------------------------------------------------------------------
1812 // validate the parameters
1814 if(points < 1) points = 1;
1815 if(group < 1) group = 1;
1817 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
1818 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
1822 // our return value (the last timestamp printed)
1823 // this is required to detect re-transmit in google JSONP
1824 time_t last_timestamp = 0;
1827 // -------------------------------------------------------------------------
1828 // find how many dimensions we have
1832 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1834 pthread_rwlock_unlock(&st->rwlock);
1835 buffer_strcat(wb, "No dimensions yet.");
1840 // -------------------------------------------------------------------------
1841 // prepare various strings, to speed up the loop
1843 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);
1844 char normal_annotation[201]; snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1845 char pre_date[51]; snprintfz(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1846 char post_date[21]; snprintfz(post_date, 20, "%s}", sq);
1847 char pre_value[21]; snprintfz(pre_value, 20, ",{%sv%s:", kq, kq);
1848 char post_value[21]; strcpy(post_value, "}");
1851 // -------------------------------------------------------------------------
1852 // checks for debugging
1855 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"
1857 , rrdset_first_entry_t(st)
1858 , rrdset_last_entry_t(st)
1859 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
1868 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%ld) is earlier than the oldest (%ld)", st->name, before, after);
1870 if((before - after) > st->entries * st->update_every)
1871 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);
1875 // -------------------------------------------------------------------------
1876 // temp arrays for keeping values per dimension
1878 calculated_number group_values[dimensions]; // keep sums when grouping
1879 int print_hidden[dimensions]; // keep hidden flags
1880 int found_non_zero[dimensions];
1881 int found_non_existing[dimensions];
1884 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1885 group_values[c] = 0;
1886 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
1887 found_non_zero[c] = 0;
1888 found_non_existing[c] = 0;
1892 // 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);
1893 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
1894 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
1896 // -------------------------------------------------------------------------
1897 // remove dimensions that contain only zeros
1900 if(only_non_zero) max_loop = 2;
1902 for(; max_loop ; max_loop--) {
1904 // -------------------------------------------------------------------------
1905 // print the JSON header
1907 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
1908 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);
1909 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);
1910 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);
1912 // print the header for each dimension
1913 // and update the print_hidden array for the dimensions that should be hidden
1915 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1916 if(!print_hidden[c]) {
1918 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);
1922 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);
1925 // print the begin of row data
1926 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1929 // -------------------------------------------------------------------------
1932 int annotate_reset = 0;
1933 int annotation_count = 0;
1935 long t = rrdset_time2slot(st, before),
1936 stop_at_t = rrdset_time2slot(st, after),
1941 time_t now = rrdset_slot2time(st, t),
1942 dt = st->update_every;
1944 long count = 0, printed = 0, group_count = 0;
1947 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"
1954 , (uint32_t)rrdset_first_entry_t(st)
1955 , (uint32_t)rrdset_last_entry_t(st)
1961 for(; !stop_now ; now -= dt, t--, counter++) {
1962 if(t < 0) t = st->entries - 1;
1963 if(t == stop_at_t) stop_now = counter;
1967 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %ld, %s %s"
1974 , (group_count + 1 == group)?"PRINT":" - "
1975 , (now >= after && now <= before)?"RANGE":" - "
1979 // make sure we return data in the proper time range
1980 if(now > before) continue;
1981 if(now < after) break;
1983 //if(rrdset_slot2time(st, t) != now)
1984 // 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));
1989 // check if we have to print this now
1990 if(group_count == group) {
1991 if(printed >= points) {
1992 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
1996 // generate the local date time
1997 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1998 if(!tm) { error("localtime() failed."); continue; }
1999 if(now > last_timestamp) last_timestamp = now;
2001 if(printed) buffer_strcat(wb, "]},\n");
2002 buffer_strcat(wb, pre_date);
2003 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2004 buffer_strcat(wb, post_date);
2009 // do the calculations
2010 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2011 storage_number n = rd->values[t];
2012 calculated_number value = unpack_storage_number(n);
2014 if(!does_storage_number_exist(n)) {
2016 found_non_existing[c]++;
2018 if(did_storage_number_reset(n)) annotate_reset = 1;
2020 switch(group_method) {
2022 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2026 group_values[c] += value;
2031 group_values[c] += value;
2032 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
2038 if(annotate_reset) {
2040 buffer_strcat(wb, overflow_annotation);
2044 buffer_strcat(wb, normal_annotation);
2047 for(c = 0 ; c < dimensions ; c++) {
2048 if(found_non_existing[c] == group_count) {
2049 // all entries are non-existing
2051 buffer_strcat(wb, pre_value);
2052 buffer_strcat(wb, "null");
2053 buffer_strcat(wb, post_value);
2055 else if(!print_hidden[c]) {
2057 buffer_strcat(wb, pre_value);
2058 buffer_rrd_value(wb, group_values[c]);
2059 buffer_strcat(wb, post_value);
2061 if(group_values[c]) found_non_zero[c]++;
2064 // reset them for the next loop
2065 group_values[c] = 0;
2066 found_non_existing[c] = 0;
2069 // if all dimensions are hidden, print a null
2071 buffer_strcat(wb, pre_value);
2072 buffer_strcat(wb, "null");
2073 buffer_strcat(wb, post_value);
2081 if(printed) buffer_strcat(wb, "]}");
2082 buffer_strcat(wb, "\n ]\n}\n");
2084 if(only_non_zero && max_loop > 1) {
2086 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2087 group_values[c] = 0;
2088 found_non_existing[c] = 0;
2090 if(!print_hidden[c] && !found_non_zero[c]) {
2092 print_hidden[c] = 1;
2096 if(changed) buffer_flush(wb);
2103 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %lu bytes", st->name, wb->len);
2105 pthread_rwlock_unlock(&st->rwlock);
2106 return last_timestamp;