3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
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 , rrdset_flag_check(st, RRDSET_FLAG_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;
44 size_t dimensions = 0;
46 rrddim_foreach_read(rd, st) {
47 if(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) continue;
49 memory += rd->memsize;
53 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
62 if(dimensions_count) *dimensions_count += dimensions;
63 if(memory_used) *memory_used += memory;
65 buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": ");
66 buffer_rrd_value(wb, st->green);
67 buffer_strcat(wb, ",\n\t\t\t\"red\": ");
68 buffer_rrd_value(wb, st->red);
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78 rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
81 void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb) {
82 static char *custom_dashboard_info_js_filename = NULL;
83 size_t c, dimensions = 0, memory = 0, alarms = 0;
86 time_t now = now_realtime_sec();
88 if(unlikely(!custom_dashboard_info_js_filename))
89 custom_dashboard_info_js_filename = config_get(CONFIG_SECTION_WEB, "custom dashboard_info.js", "");
91 buffer_sprintf(wb, "{\n"
92 "\t\"hostname\": \"%s\""
93 ",\n\t\"version\": \"%s\""
95 ",\n\t\"update_every\": %d"
96 ",\n\t\"history\": %ld"
97 ",\n\t\"custom_info\": \"%s\""
102 , host->rrd_update_every
103 , host->rrd_history_entries
104 , custom_dashboard_info_js_filename
108 rrdhost_rdlock(host);
109 rrdset_foreach_read(st, host) {
110 if(rrdset_is_available_for_viewers(st)) {
111 if(c) buffer_strcat(wb, ",");
112 buffer_strcat(wb, "\n\t\t\"");
113 buffer_strcat(wb, st->id);
114 buffer_strcat(wb, "\": ");
115 rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
118 st->last_accessed_time = now;
123 for(rc = host->alarms; rc ; rc = rc->next) {
127 rrdhost_unlock(host);
129 buffer_sprintf(wb, "\n\t}"
130 ",\n\t\"charts_count\": %zu"
131 ",\n\t\"dimensions_count\": %zu"
132 ",\n\t\"alarms_count\": %zu"
133 ",\n\t\"rrd_memory_bytes\": %zu"
134 ",\n\t\"hosts_count\": %zu"
140 , rrd_hosts_available
143 if(unlikely(rrd_hosts_available > 1)) {
146 rrdhost_foreach_read(h) {
149 "\n\t\t\t\"hostname\": \"%s\""
151 , (h != localhost) ? "," : ""
160 "\n\t\t\t\"hostname\": \"%s\""
166 buffer_sprintf(wb, "\n\t]\n}\n");
169 // ----------------------------------------------------------------------------
171 // /api/v1/allmetrics?format=prometheus
173 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
176 for(n = 0; *s && n < usable ; d++, s++, n++) {
177 register char c = *s;
179 if(unlikely(!isalnum(c))) *d = '_';
187 #define PROMETHEUS_ELEMENT_MAX 256
189 void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb) {
190 rrdhost_rdlock(host);
192 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
193 prometheus_name_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
197 rrdset_foreach_read(st, host) {
198 char chart[PROMETHEUS_ELEMENT_MAX + 1];
199 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
201 buffer_strcat(wb, "\n");
202 if(rrdset_is_available_for_viewers(st)) {
205 // for each dimension
207 rrddim_foreach_read(rd, st) {
208 if(rd->collections_counter) {
209 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
210 prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
212 // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
214 switch(rd->algorithm) {
215 case RRD_ALGORITHM_INCREMENTAL:
216 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
217 buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
221 buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
225 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
226 // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n, timeval_msec(&rd->last_collected_time));
228 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
229 chart, dimension, hostname, rd->last_collected_value, timeval_msec(&rd->last_collected_time)
239 rrdhost_unlock(host);
242 // ----------------------------------------------------------------------------
244 // /api/v1/allmetrics?format=bash
246 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
249 for(n = 0; *s && n < usable ; d++, s++, n++) {
250 register char c = *s;
252 if(unlikely(!isalnum(c))) *d = '_';
253 else *d = (char)toupper(c);
260 #define SHELL_ELEMENT_MAX 100
262 void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb) {
263 rrdhost_rdlock(host);
267 rrdset_foreach_read(st, host) {
268 calculated_number total = 0.0;
269 char chart[SHELL_ELEMENT_MAX + 1];
270 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
272 buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
273 if(rrdset_is_available_for_viewers(st)) {
276 // for each dimension
278 rrddim_foreach_read(rd, st) {
279 if(rd->collections_counter) {
280 char dimension[SHELL_ELEMENT_MAX + 1];
281 shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
283 calculated_number n = rd->last_stored_value;
285 if(isnan(n) || isinf(n))
286 buffer_sprintf(wb, "NETDATA_%s_%s=\"\" # %s\n", chart, dimension, st->units);
288 if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
290 if(!rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) total += n;
291 buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\" # %s\n", chart, dimension, n, st->units);
296 total = roundl(total);
297 buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\" # %s\n", chart, total, st->units);
302 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
305 for(rc = host->alarms; rc ;rc = rc->next) {
306 if(!rc->rrdset) continue;
308 char chart[SHELL_ELEMENT_MAX + 1];
309 shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
311 char alarm[SHELL_ELEMENT_MAX + 1];
312 shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
314 calculated_number n = rc->value;
316 if(isnan(n) || isinf(n))
317 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\" # %s\n", chart, alarm, rc->units);
320 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\" # %s\n", chart, alarm, n, rc->units);
323 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
326 rrdhost_unlock(host);
329 // ----------------------------------------------------------------------------
331 void rrd_stats_api_v1_charts_allmetrics_json(RRDHOST *host, BUFFER *wb) {
332 rrdhost_rdlock(host);
334 buffer_strcat(wb, "{");
336 size_t chart_counter = 0;
337 size_t dimension_counter = 0;
341 rrdset_foreach_read(st, host) {
342 if(rrdset_is_available_for_viewers(st)) {
345 buffer_sprintf(wb, "%s\n"
347 "\t\t\"name\":\"%s\",\n"
348 "\t\t\"context\":\"%s\",\n"
349 "\t\t\"units\":\"%s\",\n"
350 "\t\t\"last_updated\": %ld,\n"
351 "\t\t\"dimensions\": {"
352 , chart_counter?",":""
357 , rrdset_last_entry_t(st)
361 dimension_counter = 0;
363 // for each dimension
365 rrddim_foreach_read(rd, st) {
366 if(rd->collections_counter) {
368 buffer_sprintf(wb, "%s\n"
370 "\t\t\t\t\"name\": \"%s\",\n"
371 "\t\t\t\t\"value\": "
372 , dimension_counter?",":""
377 if(isnan(rd->last_stored_value))
378 buffer_strcat(wb, "null");
380 buffer_sprintf(wb, CALCULATED_NUMBER_FORMAT, rd->last_stored_value);
382 buffer_strcat(wb, "\n\t\t\t}");
388 buffer_strcat(wb, "\n\t\t}\n\t}");
393 buffer_strcat(wb, "\n}");
394 rrdhost_unlock(host);
397 // ----------------------------------------------------------------------------
399 // RRDR dimension options
400 #define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
401 #define RRDR_RESET 0x02 // the dimension contains / the value is reset
402 #define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
403 #define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
404 #define RRDR_SELECTED 0x10 // the dimension is selected
406 // RRDR result options
407 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
408 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
410 typedef struct rrdresult {
411 RRDSET *st; // the chart this result refers to
413 uint32_t result_options; // RRDR_RESULT_OPTION_*
415 int d; // the number of dimensions
416 long n; // the number of values in the arrays
417 long rows; // the number of rows used
419 uint8_t *od; // the options for the dimensions
421 time_t *t; // array of n timestamps
422 calculated_number *v; // array n x d values
423 uint8_t *o; // array n x d options
425 long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
427 long group; // how many collected values were grouped for each row
428 int update_every; // what is the suggested update frequency in seconds
430 calculated_number min;
431 calculated_number max;
436 int has_st_lock; // if st is read locked by us
439 #define rrdr_rows(r) ((r)->rows)
442 static void rrdr_dump(RRDR *r)
447 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
449 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
450 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
453 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
454 , (r->od[c] & RRDR_RESET)?"RESET ":""
455 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
456 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
461 fprintf(stderr, "RRDR does not have any values in it.\n");
465 fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
467 // for each line in the array
468 for(i = 0; i < r->rows ;i++) {
469 calculated_number *cn = &r->v[ i * r->d ];
470 uint8_t *co = &r->o[ i * r->d ];
472 // print the id and the timestamp of the line
473 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
475 // for each dimension
476 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
477 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
478 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
480 if(co[c] & RRDR_EMPTY)
481 fprintf(stderr, "null ");
483 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
485 , (co[c] & RRDR_EMPTY)?"E":" "
486 , (co[c] & RRDR_RESET)?"R":" "
487 , (co[c] & RRDR_HIDDEN)?"H":" "
488 , (co[c] & RRDR_NONZERO)?"N":" "
492 fprintf(stderr, "\n");
497 void rrdr_disable_not_selected_dimensions(RRDR *r, uint32_t options, const char *dims) {
498 rrdset_check_rdlock(r->st);
500 if(unlikely(!dims || !*dims)) return;
502 char b[strlen(dims) + 1];
506 long c, dims_selected = 0, dims_not_hidden_not_zero = 0;
509 // disable all of them
510 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
511 r->od[c] |= RRDR_HIDDEN;
513 while(o && *o && (tok = mystrsep(&o, ",|"))) {
516 uint32_t hash = simple_hash(tok);
518 // find it and enable it
519 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
520 if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || (hash == d->hash_name && !strcmp(d->name, tok)))) {
522 if(likely(r->od[c] & RRDR_HIDDEN)) {
523 r->od[c] |= RRDR_SELECTED;
524 r->od[c] &= ~RRDR_HIDDEN;
528 // since the user needs this dimension
529 // make it appear as NONZERO, to return it
530 // even if the dimension has only zeros
531 // unless option non_zero is set
532 if(likely(!(options & RRDR_OPTION_NONZERO)))
533 r->od[c] |= RRDR_NONZERO;
535 // count the visible dimensions
536 if(likely(r->od[c] & RRDR_NONZERO))
537 dims_not_hidden_not_zero++;
542 // check if all dimensions are hidden
543 if(unlikely(!dims_not_hidden_not_zero && dims_selected)) {
544 // there are a few selected dimensions
545 // but they are all zero
546 // enable the selected ones
547 // to avoid returning an empty chart
548 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
549 if(unlikely(r->od[c] & RRDR_SELECTED))
550 r->od[c] |= RRDR_NONZERO;
554 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
557 case DATASOURCE_JSON:
558 buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
561 case DATASOURCE_DATATABLE_JSON:
562 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
565 case DATASOURCE_DATATABLE_JSONP:
566 buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
569 case DATASOURCE_JSONP:
570 buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
574 buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
578 buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
582 buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
585 case DATASOURCE_HTML:
586 buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
589 case DATASOURCE_JS_ARRAY:
590 buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
593 case DATASOURCE_SSV_COMMA:
594 buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
598 buffer_strcat(wb, "unknown");
603 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
605 rrdset_check_rdlock(r->st);
609 if(options & RRDR_OPTION_NONZERO) {
612 // commented due to #1514
614 //if(dims && *dims) {
615 // the caller wants specific dimensions
616 // disable NONZERO option
617 // to make sure we don't accidentally prevent
618 // the specific dimensions from being returned
622 // find how many dimensions are not zero
625 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
626 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
627 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
632 // if with nonzero we get i = 0 (no dimensions will be returned)
633 // disable nonzero to show all dimensions
634 if(!i) options &= ~RRDR_OPTION_NONZERO;
640 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
642 rrdset_check_rdlock(r->st);
644 long rows = rrdr_rows(r);
648 //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
649 char kq[2] = "", // key quote
650 sq[2] = ""; // string quote
652 if( options & RRDR_OPTION_GOOGLE_JSON ) {
661 buffer_sprintf(wb, "{\n"
664 " %sname%s: %s%s%s,\n"
665 " %sview_update_every%s: %d,\n"
666 " %supdate_every%s: %d,\n"
667 " %sfirst_entry%s: %u,\n"
668 " %slast_entry%s: %u,\n"
671 " %sdimension_names%s: ["
673 , kq, kq, sq, r->st->id, sq
674 , kq, kq, sq, r->st->name, sq
675 , kq, kq, r->update_every
676 , kq, kq, r->st->update_every
677 , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
678 , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
679 , kq, kq, (uint32_t)r->before
680 , kq, kq, (uint32_t)r->after
683 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
684 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
685 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
687 if(i) buffer_strcat(wb, ", ");
688 buffer_strcat(wb, sq);
689 buffer_strcat(wb, rd->name);
690 buffer_strcat(wb, sq);
694 #ifdef NETDATA_INTERNAL_CHECKS
695 error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
698 buffer_strcat(wb, sq);
699 buffer_strcat(wb, "no data");
700 buffer_strcat(wb, sq);
703 buffer_sprintf(wb, "],\n"
704 " %sdimension_ids%s: ["
707 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
708 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
709 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
711 if(i) buffer_strcat(wb, ", ");
712 buffer_strcat(wb, sq);
713 buffer_strcat(wb, rd->id);
714 buffer_strcat(wb, sq);
719 buffer_strcat(wb, sq);
720 buffer_strcat(wb, "no data");
721 buffer_strcat(wb, sq);
724 buffer_sprintf(wb, "],\n"
725 " %slatest_values%s: ["
728 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
729 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
730 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
732 if(i) buffer_strcat(wb, ", ");
735 storage_number n = rd->values[rrdset_last_slot(r->st)];
737 if(!does_storage_number_exist(n))
738 buffer_strcat(wb, "null");
740 buffer_rrd_value(wb, unpack_storage_number(n));
744 buffer_strcat(wb, "null");
747 buffer_sprintf(wb, "],\n"
748 " %sview_latest_values%s: ["
753 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
754 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
755 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
757 if(i) buffer_strcat(wb, ", ");
760 calculated_number *cn = &r->v[ (0) * r->d ];
761 uint8_t *co = &r->o[ (0) * r->d ];
763 if(co[c] & RRDR_EMPTY)
764 buffer_strcat(wb, "null");
766 buffer_rrd_value(wb, cn[c]);
771 buffer_strcat(wb, "null");
774 buffer_sprintf(wb, "],\n"
775 " %sdimensions%s: %ld,\n"
776 " %spoints%s: %ld,\n"
783 rrdr_buffer_print_format(wb, format);
785 buffer_sprintf(wb, "%s,\n"
791 if(string_value) buffer_strcat(wb, sq);
792 //info("JSONWRAPPER(): %s: END", r->st->id);
795 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
799 char kq[2] = "", // key quote
800 sq[2] = ""; // string quote
802 if( options & RRDR_OPTION_GOOGLE_JSON ) {
811 if(string_value) buffer_strcat(wb, sq);
813 buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
814 buffer_rrd_value(wb, r->min);
815 buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
816 buffer_rrd_value(wb, r->max);
817 buffer_strcat(wb, "\n}\n");
820 #define JSON_DATES_JS 1
821 #define JSON_DATES_TIMESTAMP 2
823 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
825 rrdset_check_rdlock(r->st);
827 //info("RRD2JSON(): %s: BEGIN", r->st->id);
828 int row_annotations = 0, dates, dates_with_new = 0;
829 char kq[2] = "", // key quote
830 sq[2] = "", // string quote
831 pre_label[101] = "", // before each label
832 post_label[101] = "", // after each label
833 pre_date[101] = "", // the beginning of line, to the date
834 post_date[101] = "", // closing the date
835 pre_value[101] = "", // before each value
836 post_value[101] = "", // after each value
837 post_line[101] = "", // at the end of each row
838 normal_annotation[201] = "", // default row annotation
839 overflow_annotation[201] = "", // overflow row annotation
840 data_begin[101] = "", // between labels and values
841 finish[101] = ""; // at the end of everything
844 dates = JSON_DATES_JS;
845 if( options & RRDR_OPTION_GOOGLE_JSON ) {
854 snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
855 snprintfz(post_date, 100, "%s}", sq);
856 snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
857 snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
858 snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
859 strcpy(post_value, "}");
860 strcpy(post_line, "]}");
861 snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
862 strcpy(finish, "\n ]\n}");
864 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);
865 snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
867 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
868 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);
869 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);
870 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);
872 // remove the valueobjects flag
873 // google wants its own keys
874 if(options & RRDR_OPTION_OBJECTSROWS)
875 options &= ~RRDR_OPTION_OBJECTSROWS;
880 if(options & RRDR_OPTION_GOOGLE_JSON) {
881 dates = JSON_DATES_JS;
885 dates = JSON_DATES_TIMESTAMP;
888 if( options & RRDR_OPTION_OBJECTSROWS )
889 strcpy(pre_date, " { ");
891 strcpy(pre_date, " [ ");
892 strcpy(pre_label, ", \"");
893 strcpy(post_label, "\"");
894 strcpy(pre_value, ", ");
895 if( options & RRDR_OPTION_OBJECTSROWS )
896 strcpy(post_line, "}");
898 strcpy(post_line, "]");
899 snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
900 strcpy(finish, "\n ]\n}");
902 buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
903 buffer_sprintf(wb, "%stime%s", sq, sq);
906 // -------------------------------------------------------------------------
907 // print the JSON header
912 // print the header lines
913 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
914 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
915 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
917 buffer_strcat(wb, pre_label);
918 buffer_strcat(wb, rd->name);
919 buffer_strcat(wb, post_label);
923 buffer_strcat(wb, pre_label);
924 buffer_strcat(wb, "no data");
925 buffer_strcat(wb, post_label);
928 // print the begin of row data
929 buffer_strcat(wb, data_begin);
931 // if all dimensions are hidden, print a null
933 buffer_strcat(wb, finish);
937 long start = 0, end = rrdr_rows(r), step = 1;
938 if((options & RRDR_OPTION_REVERSED)) {
939 start = rrdr_rows(r) - 1;
944 // for each line in the array
945 calculated_number total = 1;
946 for(i = start; i != end ;i += step) {
947 calculated_number *cn = &r->v[ i * r->d ];
948 uint8_t *co = &r->o[ i * r->d ];
950 time_t now = r->t[i];
952 if(dates == JSON_DATES_JS) {
953 // generate the local date time
954 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
955 if(!tm) { error("localtime_r() failed."); continue; }
957 if(likely(i != start)) buffer_strcat(wb, ",\n");
958 buffer_strcat(wb, pre_date);
960 if( options & RRDR_OPTION_OBJECTSROWS )
961 buffer_sprintf(wb, "%stime%s: ", kq, kq);
964 buffer_strcat(wb, "new ");
966 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
968 buffer_strcat(wb, post_date);
970 if(row_annotations) {
971 // google supports one annotation per row
972 int annotation_found = 0;
973 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
974 if(co[c] & RRDR_RESET) {
975 buffer_strcat(wb, overflow_annotation);
976 annotation_found = 1;
980 if(!annotation_found)
981 buffer_strcat(wb, normal_annotation);
985 // print the timestamp of the line
986 if(likely(i != start)) buffer_strcat(wb, ",\n");
987 buffer_strcat(wb, pre_date);
989 if( options & RRDR_OPTION_OBJECTSROWS )
990 buffer_sprintf(wb, "%stime%s: ", kq, kq);
992 buffer_rrd_value(wb, (calculated_number)r->t[i]);
994 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
996 buffer_strcat(wb, post_date);
999 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1001 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1002 calculated_number n = cn[c];
1004 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1009 // prevent a division by zero
1010 if(total == 0) total = 1;
1013 // for each dimension
1014 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
1015 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1016 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1018 calculated_number n = cn[c];
1020 buffer_strcat(wb, pre_value);
1022 if( options & RRDR_OPTION_OBJECTSROWS )
1023 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
1025 if(co[c] & RRDR_EMPTY) {
1026 if(options & RRDR_OPTION_NULL2ZERO)
1027 buffer_strcat(wb, "0");
1029 buffer_strcat(wb, "null");
1032 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1035 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1036 n = n * 100 / total;
1038 buffer_rrd_value(wb, n);
1041 buffer_strcat(wb, post_value);
1044 buffer_strcat(wb, post_line);
1047 buffer_strcat(wb, finish);
1048 //info("RRD2JSON(): %s: END", r->st->id);
1051 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
1053 rrdset_check_rdlock(r->st);
1055 //info("RRD2CSV(): %s: BEGIN", r->st->id);
1059 // print the csv header
1060 for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1061 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1062 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1065 buffer_strcat(wb, startline);
1066 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1067 buffer_strcat(wb, "time");
1068 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1070 buffer_strcat(wb, separator);
1071 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1072 buffer_strcat(wb, d->name);
1073 if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1076 buffer_strcat(wb, endline);
1079 // no dimensions present
1083 long start = 0, end = rrdr_rows(r), step = 1;
1084 if((options & RRDR_OPTION_REVERSED)) {
1085 start = rrdr_rows(r) - 1;
1090 // for each line in the array
1091 calculated_number total = 1;
1092 for(i = start; i != end ;i += step) {
1093 calculated_number *cn = &r->v[ i * r->d ];
1094 uint8_t *co = &r->o[ i * r->d ];
1096 buffer_strcat(wb, betweenlines);
1097 buffer_strcat(wb, startline);
1099 time_t now = r->t[i];
1101 if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1102 // print the timestamp of the line
1103 buffer_rrd_value(wb, (calculated_number)now);
1105 if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1108 // generate the local date time
1109 struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1110 if(!tm) { error("localtime() failed."); continue; }
1111 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1114 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1116 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1117 calculated_number n = cn[c];
1119 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1124 // prevent a division by zero
1125 if(total == 0) total = 1;
1128 // for each dimension
1129 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1130 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1131 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1133 buffer_strcat(wb, separator);
1135 calculated_number n = cn[c];
1137 if(co[c] & RRDR_EMPTY) {
1138 if(options & RRDR_OPTION_NULL2ZERO)
1139 buffer_strcat(wb, "0");
1141 buffer_strcat(wb, "null");
1144 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1147 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1148 n = n * 100 / total;
1150 buffer_rrd_value(wb, n);
1154 buffer_strcat(wb, endline);
1156 //info("RRD2CSV(): %s: END", r->st->id);
1159 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1160 rrdset_check_rdlock(r->st);
1165 calculated_number *cn = &r->v[ i * r->d ];
1166 uint8_t *co = &r->o[ i * r->d ];
1168 calculated_number sum = 0, min = 0, max = 0, v;
1169 int all_null = 1, init = 1;
1171 calculated_number total = 1;
1172 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1174 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1175 calculated_number n = cn[c];
1177 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1182 // prevent a division by zero
1183 if(total == 0) total = 1;
1186 // for each dimension
1187 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1188 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1189 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1191 calculated_number n = cn[c];
1193 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1196 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1197 n = n * 100 / total;
1199 if(unlikely(init)) {
1211 if(likely(!(co[c] & RRDR_EMPTY))) {
1216 if(n < min) min = n;
1217 if(n > max) max = n;
1220 if(unlikely(all_null)) {
1221 if(likely(all_values_are_null))
1222 *all_values_are_null = 1;
1226 if(likely(all_values_are_null))
1227 *all_values_are_null = 0;
1230 if(options & RRDR_OPTION_MIN2MAX)
1238 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1240 //info("RRD2SSV(): %s: BEGIN", r->st->id);
1243 buffer_strcat(wb, prefix);
1244 long start = 0, end = rrdr_rows(r), step = 1;
1245 if((options & RRDR_OPTION_REVERSED)) {
1246 start = rrdr_rows(r) - 1;
1251 // for each line in the array
1252 for(i = start; i != end ;i += step) {
1253 int all_values_are_null = 0;
1254 calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1256 if(likely(i != start)) {
1257 if(r->min > v) r->min = v;
1258 if(r->max < v) r->max = v;
1265 if(likely(i != start))
1266 buffer_strcat(wb, separator);
1268 if(all_values_are_null) {
1269 if(options & RRDR_OPTION_NULL2ZERO)
1270 buffer_strcat(wb, "0");
1272 buffer_strcat(wb, "null");
1275 buffer_rrd_value(wb, v);
1277 buffer_strcat(wb, suffix);
1278 //info("RRD2SSV(): %s: END", r->st->id);
1281 inline static calculated_number *rrdr_line_values(RRDR *r)
1283 return &r->v[ r->c * r->d ];
1286 inline static uint8_t *rrdr_line_options(RRDR *r)
1288 return &r->o[ r->c * r->d ];
1291 inline static int rrdr_line_init(RRDR *r, time_t t)
1295 if(unlikely(r->c >= r->n)) {
1296 error("requested to step above RRDR size for chart %s", r->st->name);
1306 inline static void rrdr_lock_rrdset(RRDR *r) {
1308 error("NULL value given!");
1312 rrdset_rdlock(r->st);
1316 inline static void rrdr_unlock_rrdset(RRDR *r) {
1318 error("NULL value given!");
1322 if(likely(r->has_st_lock)) {
1323 rrdset_unlock(r->st);
1328 inline static void rrdr_free(RRDR *r)
1331 error("NULL value given!");
1335 rrdr_unlock_rrdset(r);
1343 static inline void rrdr_done(RRDR *r)
1349 static RRDR *rrdr_create(RRDSET *st, long n)
1352 error("NULL value given!");
1356 RRDR *r = callocz(1, sizeof(RRDR));
1359 rrdr_lock_rrdset(r);
1362 rrddim_foreach_read(rd, st) r->d++;
1366 r->t = mallocz(n * sizeof(time_t));
1367 r->v = mallocz(n * r->d * sizeof(calculated_number));
1368 r->o = mallocz(n * r->d * sizeof(uint8_t));
1369 r->od = mallocz(r->d * sizeof(uint8_t));
1371 // set the hidden flag on hidden dimensions
1373 for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1374 if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
1375 r->od[c] = RRDR_HIDDEN;
1382 r->update_every = 1;
1387 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1389 int debug = rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?1:0;
1390 int absolute_period_requested = -1;
1392 time_t first_entry_t = rrdset_first_entry_t(st);
1393 time_t last_entry_t = rrdset_last_entry_t(st);
1395 if(before == 0 && after == 0) {
1396 // dump the all the data
1397 before = last_entry_t;
1398 after = first_entry_t;
1399 absolute_period_requested = 0;
1402 // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1403 if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1404 if(abs(before) % st->update_every) {
1405 // make sure it is multiple of st->update_every
1406 if(before < 0) before = before - st->update_every - before % st->update_every;
1407 else before = before + st->update_every - before % st->update_every;
1409 if(before > 0) before = first_entry_t + before;
1410 else before = last_entry_t + before;
1411 absolute_period_requested = 0;
1414 // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1415 if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1416 if(after == 0) after = -st->update_every;
1417 if(abs(after) % st->update_every) {
1418 // make sure it is multiple of st->update_every
1419 if(after < 0) after = after - st->update_every - after % st->update_every;
1420 else after = after + st->update_every - after % st->update_every;
1422 after = before + after;
1423 absolute_period_requested = 0;
1426 if(absolute_period_requested == -1)
1427 absolute_period_requested = 1;
1429 // make sure they are within our timeframe
1430 if(before > last_entry_t) before = last_entry_t;
1431 if(before < first_entry_t) before = first_entry_t;
1433 if(after > last_entry_t) after = last_entry_t;
1434 if(after < first_entry_t) after = first_entry_t;
1436 // check if they are upside down
1437 if(after > before) {
1438 time_t tmp = before;
1443 // the duration of the chart
1444 time_t duration = before - after;
1445 long available_points = duration / st->update_every;
1447 if(duration <= 0 || available_points <= 0)
1448 return rrdr_create(st, 1);
1450 // check the wanted points
1451 if(points < 0) points = -points;
1452 if(points > available_points) points = available_points;
1453 if(points == 0) points = available_points;
1455 // calculate proper grouping of source data
1456 long group = available_points / points;
1457 if(group <= 0) group = 1;
1459 // round group to the closest integer
1460 if(available_points % points > points / 2) group++;
1462 time_t after_new = (aligned) ? (after - (after % (group * st->update_every))) : after;
1463 time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1464 long points_new = (before_new - after_new) / st->update_every / group;
1466 // find the starting and ending slots in our round robin db
1467 long start_at_slot = rrdset_time2slot(st, before_new),
1468 stop_at_slot = rrdset_time2slot(st, after_new);
1470 #ifdef NETDATA_INTERNAL_CHECKS
1471 if(after_new < first_entry_t) {
1472 error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1474 if(after_new > last_entry_t) {
1475 error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1477 if(before_new < first_entry_t) {
1478 error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1480 if(before_new > last_entry_t) {
1481 error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1483 if(start_at_slot < 0 || start_at_slot >= st->entries) {
1484 error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1486 if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1487 error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1489 if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1490 error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1494 //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);
1497 before = before_new;
1498 duration = before - after;
1499 points = points_new;
1502 // before = the end time of the calculation
1503 // after = the start time of the calculation
1504 // duration = the duration of the calculation
1505 // group = the number of source points to aggregate / group together
1506 // method = the method of grouping source points
1507 // points = the number of points to generate
1510 // -------------------------------------------------------------------------
1511 // initialize our result set
1513 RRDR *r = rrdr_create(st, points);
1515 #ifdef NETDATA_INTERNAL_CHECKS
1516 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);
1521 #ifdef NETDATA_INTERNAL_CHECKS
1522 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);
1527 if(absolute_period_requested == 1)
1528 r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1530 r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1532 // find how many dimensions we have
1533 long dimensions = r->d;
1536 // -------------------------------------------------------------------------
1537 // checks for debugging
1539 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"
1541 , (uint32_t)first_entry_t
1542 , (uint32_t)last_entry_t
1543 , (uint32_t)(last_entry_t - first_entry_t)
1546 , (uint32_t)duration
1552 // -------------------------------------------------------------------------
1553 // temp arrays for keeping values per dimension
1555 calculated_number last_values[dimensions]; // keep the last value of each dimension
1556 calculated_number group_values[dimensions]; // keep sums when grouping
1557 long group_counts[dimensions]; // keep the number of values added to group_values
1558 uint8_t group_options[dimensions];
1559 uint8_t found_non_zero[dimensions];
1565 rrdset_check_rdlock(st);
1566 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1568 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1569 group_counts[c] = 0;
1570 group_options[c] = 0;
1571 found_non_zero[c] = 0;
1575 // -------------------------------------------------------------------------
1578 time_t now = rrdset_slot2time(st, start_at_slot),
1579 dt = st->update_every,
1582 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"
1594 r->update_every = (int)group * st->update_every;
1598 //info("RRD2RRDR(): %s: STARTING", st->id);
1600 long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1601 for(; !stop_now ; now -= dt, slot--, counter++) {
1602 if(unlikely(slot < 0)) slot = st->entries - 1;
1603 if(unlikely(slot == stop_at_slot)) stop_now = counter;
1605 if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1612 , (group_count + 1 == group)?"PRINT":" - "
1613 , (now >= after && now <= before)?"RANGE":" - "
1616 // make sure we return data in the proper time range
1617 if(unlikely(now > before)) continue;
1618 if(unlikely(now < after)) break;
1620 if(unlikely(group_count == 0)) {
1621 group_start_t = now;
1625 if(unlikely(group_count == group)) {
1626 if(unlikely(added >= points)) break;
1630 // do the calculations
1631 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1632 storage_number n = rd->values[slot];
1633 if(unlikely(!does_storage_number_exist(n))) continue;
1637 calculated_number value = unpack_storage_number(n);
1638 if(likely(value != 0.0)) {
1639 group_options[c] |= RRDR_NONZERO;
1640 found_non_zero[c] = 1;
1643 if(unlikely(did_storage_number_reset(n)))
1644 group_options[c] |= RRDR_RESET;
1646 switch(group_method) {
1648 if(unlikely(isnan(group_values[c])) ||
1649 fabsl(value) < fabsl(group_values[c]))
1650 group_values[c] = value;
1654 if(unlikely(isnan(group_values[c])) ||
1655 fabsl(value) > fabsl(group_values[c]))
1656 group_values[c] = value;
1662 case GROUP_UNDEFINED:
1663 group_values[c] += value;
1666 case GROUP_INCREMENTAL_SUM:
1667 if(unlikely(slot == start_at_slot))
1668 last_values[c] = value;
1670 group_values[c] += last_values[c] - value;
1671 last_values[c] = value;
1677 if(unlikely(add_this)) {
1678 if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1682 calculated_number *cn = rrdr_line_values(r);
1683 uint8_t *co = rrdr_line_options(r);
1685 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1687 // update the dimension options
1688 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1690 // store the specific point options
1691 co[c] = group_options[c];
1694 if(unlikely(group_counts[c] == 0)) {
1696 co[c] |= RRDR_EMPTY;
1697 group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1700 switch(group_method) {
1703 if(unlikely(isnan(group_values[c])))
1706 cn[c] = group_values[c];
1707 group_values[c] = NAN;
1712 case GROUP_INCREMENTAL_SUM:
1713 cn[c] = group_values[c];
1714 group_values[c] = 0;
1719 case GROUP_UNDEFINED:
1720 cn[c] = group_values[c] / group_counts[c];
1721 group_values[c] = 0;
1725 if(cn[c] < r->min) r->min = cn[c];
1726 if(cn[c] > r->max) r->max = cn[c];
1729 // reset for the next loop
1730 group_counts[c] = 0;
1731 group_options[c] = 0;
1741 //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1742 //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1746 int rrdset2value_api_v1(
1749 , calculated_number *n
1750 , const char *dimensions
1758 , int *value_is_null
1760 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1762 if(value_is_null) *value_is_null = 1;
1766 if(rrdr_rows(r) == 0) {
1769 if(db_after) *db_after = 0;
1770 if(db_before) *db_before = 0;
1771 if(value_is_null) *value_is_null = 1;
1776 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1777 buffer_no_cacheable(wb);
1778 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1779 buffer_cacheable(wb);
1781 options = rrdr_check_options(r, options, dimensions);
1784 rrdr_disable_not_selected_dimensions(r, options, dimensions);
1786 if(db_after) *db_after = r->after;
1787 if(db_before) *db_before = r->before;
1789 long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1790 *n = rrdr2value(r, i, options, value_is_null);
1796 int rrdset2anything_api_v1(
1799 , BUFFER *dimensions
1806 , time_t *latest_timestamp
1808 st->last_accessed_time = now_realtime_sec();
1810 RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1812 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1816 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1817 buffer_no_cacheable(wb);
1818 else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1819 buffer_cacheable(wb);
1821 options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1824 rrdr_disable_not_selected_dimensions(r, options, buffer_tostring(dimensions));
1826 if(latest_timestamp && rrdr_rows(r) > 0)
1827 *latest_timestamp = r->before;
1830 case DATASOURCE_SSV:
1831 if(options & RRDR_OPTION_JSON_WRAP) {
1832 wb->contenttype = CT_APPLICATION_JSON;
1833 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1834 rrdr2ssv(r, wb, options, "", " ", "");
1835 rrdr_json_wrapper_end(r, wb, format, options, 1);
1838 wb->contenttype = CT_TEXT_PLAIN;
1839 rrdr2ssv(r, wb, options, "", " ", "");
1843 case DATASOURCE_SSV_COMMA:
1844 if(options & RRDR_OPTION_JSON_WRAP) {
1845 wb->contenttype = CT_APPLICATION_JSON;
1846 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1847 rrdr2ssv(r, wb, options, "", ",", "");
1848 rrdr_json_wrapper_end(r, wb, format, options, 1);
1851 wb->contenttype = CT_TEXT_PLAIN;
1852 rrdr2ssv(r, wb, options, "", ",", "");
1856 case DATASOURCE_JS_ARRAY:
1857 if(options & RRDR_OPTION_JSON_WRAP) {
1858 wb->contenttype = CT_APPLICATION_JSON;
1859 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1860 rrdr2ssv(r, wb, options, "[", ",", "]");
1861 rrdr_json_wrapper_end(r, wb, format, options, 0);
1864 wb->contenttype = CT_APPLICATION_JSON;
1865 rrdr2ssv(r, wb, options, "[", ",", "]");
1869 case DATASOURCE_CSV:
1870 if(options & RRDR_OPTION_JSON_WRAP) {
1871 wb->contenttype = CT_APPLICATION_JSON;
1872 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1873 rrdr2csv(r, wb, options, "", ",", "\\n", "");
1874 rrdr_json_wrapper_end(r, wb, format, options, 1);
1877 wb->contenttype = CT_TEXT_PLAIN;
1878 rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1882 case DATASOURCE_CSV_JSON_ARRAY:
1883 wb->contenttype = CT_APPLICATION_JSON;
1884 if(options & RRDR_OPTION_JSON_WRAP) {
1885 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1886 buffer_strcat(wb, "[\n");
1887 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1888 buffer_strcat(wb, "\n]");
1889 rrdr_json_wrapper_end(r, wb, format, options, 0);
1892 wb->contenttype = CT_TEXT_PLAIN;
1893 buffer_strcat(wb, "[\n");
1894 rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1895 buffer_strcat(wb, "\n]");
1899 case DATASOURCE_TSV:
1900 if(options & RRDR_OPTION_JSON_WRAP) {
1901 wb->contenttype = CT_APPLICATION_JSON;
1902 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1903 rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1904 rrdr_json_wrapper_end(r, wb, format, options, 1);
1907 wb->contenttype = CT_TEXT_PLAIN;
1908 rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1912 case DATASOURCE_HTML:
1913 if(options & RRDR_OPTION_JSON_WRAP) {
1914 wb->contenttype = CT_APPLICATION_JSON;
1915 rrdr_json_wrapper_begin(r, wb, format, options, 1);
1916 buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1917 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1918 buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1919 rrdr_json_wrapper_end(r, wb, format, options, 1);
1922 wb->contenttype = CT_TEXT_HTML;
1923 buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1924 rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1925 buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1929 case DATASOURCE_DATATABLE_JSONP:
1930 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1932 if(options & RRDR_OPTION_JSON_WRAP)
1933 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1935 rrdr2json(r, wb, options, 1);
1937 if(options & RRDR_OPTION_JSON_WRAP)
1938 rrdr_json_wrapper_end(r, wb, format, options, 0);
1941 case DATASOURCE_DATATABLE_JSON:
1942 wb->contenttype = CT_APPLICATION_JSON;
1944 if(options & RRDR_OPTION_JSON_WRAP)
1945 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1947 rrdr2json(r, wb, options, 1);
1949 if(options & RRDR_OPTION_JSON_WRAP)
1950 rrdr_json_wrapper_end(r, wb, format, options, 0);
1953 case DATASOURCE_JSONP:
1954 wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1955 if(options & RRDR_OPTION_JSON_WRAP)
1956 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1958 rrdr2json(r, wb, options, 0);
1960 if(options & RRDR_OPTION_JSON_WRAP)
1961 rrdr_json_wrapper_end(r, wb, format, options, 0);
1964 case DATASOURCE_JSON:
1966 wb->contenttype = CT_APPLICATION_JSON;
1968 if(options & RRDR_OPTION_JSON_WRAP)
1969 rrdr_json_wrapper_begin(r, wb, format, options, 0);
1971 rrdr2json(r, wb, options, 0);
1973 if(options & RRDR_OPTION_JSON_WRAP)
1974 rrdr_json_wrapper_end(r, wb, format, options, 0);