]> arthur.barton.de Git - netdata.git/blob - src/rrd2json.c
self-cleaning obsolete cgroups and network interfaces from memory; fixes #1163; fixes...
[netdata.git] / src / rrd2json.c
1 #include "common.h"
2
3 void rrd_stats_api_v1_chart_with_data(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memory_used)
4 {
5     rrdset_rdlock(st);
6
7     buffer_sprintf(wb,
8         "\t\t{\n"
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"
25         , st->id
26         , st->name
27         , st->type
28         , st->family
29         , st->context
30         , st->title
31         , st->priority
32         , rrdset_flag_check(st, RRDSET_FLAG_ENABLED)?"true":"false"
33         , st->units
34         , st->name
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)
39         , st->update_every
40         );
41
42     unsigned long memory = st->memsize;
43
44     size_t dimensions = 0;
45     RRDDIM *rd;
46     rrddim_foreach_read(rd, st) {
47         if(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) continue;
48
49         memory += rd->memsize;
50
51         buffer_sprintf(wb,
52             "%s"
53             "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
54             , dimensions?",\n":""
55             , rd->id
56             , rd->name
57             );
58
59         dimensions++;
60     }
61
62     if(dimensions_count) *dimensions_count += dimensions;
63     if(memory_used) *memory_used += memory;
64
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);
69
70     buffer_sprintf(wb,
71         "\n\t\t}"
72         );
73
74     rrdset_unlock(st);
75 }
76
77 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
78     rrd_stats_api_v1_chart_with_data(st, wb, NULL, NULL);
79 }
80
81 void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb)
82 {
83     size_t c, dimensions = 0, memory = 0, alarms = 0;
84     RRDSET *st;
85
86     time_t now = now_realtime_sec();
87
88     buffer_sprintf(wb, "{\n"
89            "\t\"hostname\": \"%s\""
90         ",\n\t\"version\": \"%s\""
91         ",\n\t\"os\": \"%s\""
92         ",\n\t\"update_every\": %d"
93         ",\n\t\"history\": %d"
94         ",\n\t\"charts\": {"
95         , host->hostname
96         , program_version
97         , host->os
98         , host->rrd_update_every
99         , host->rrd_history_entries
100         );
101
102     c = 0;
103     rrdhost_rdlock(host);
104     rrdset_foreach_read(st, host) {
105         if(rrdset_is_available_for_viewers(st)) {
106             if(c) buffer_strcat(wb, ",");
107             buffer_strcat(wb, "\n\t\t\"");
108             buffer_strcat(wb, st->id);
109             buffer_strcat(wb, "\": ");
110             rrd_stats_api_v1_chart_with_data(st, wb, &dimensions, &memory);
111
112             c++;
113             st->last_accessed_time = now;
114         }
115     }
116
117     RRDCALC *rc;
118     for(rc = host->alarms; rc ; rc = rc->next) {
119         if(rc->rrdset)
120             alarms++;
121     }
122     rrdhost_unlock(host);
123
124     buffer_sprintf(wb, "\n\t}"
125                     ",\n\t\"charts_count\": %zu"
126                     ",\n\t\"dimensions_count\": %zu"
127                     ",\n\t\"alarms_count\": %zu"
128                     ",\n\t\"rrd_memory_bytes\": %zu"
129                     ",\n\t\"hosts_count\": %zu"
130                     ",\n\t\"hosts\": ["
131                    , c
132                    , dimensions
133                    , alarms
134                    , memory
135                    , rrd_hosts_available
136     );
137
138     if(unlikely(rrd_hosts_available > 1)) {
139         rrd_rdlock();
140         RRDHOST *h;
141         rrdhost_foreach_read(h) {
142             buffer_sprintf(wb,
143                    "%s\n\t\t{"
144                    "\n\t\t\t\"hostname\": \"%s\""
145                    "\n\t\t}"
146                    , (h != localhost) ? "," : ""
147                    , h->hostname
148             );
149         }
150         rrd_unlock();
151     }
152     else {
153         buffer_sprintf(wb,
154                 "\n\t\t{"
155                 "\n\t\t\t\"hostname\": \"%s\""
156                 "\n\t\t}"
157                 , host->hostname
158         );
159     }
160
161     buffer_sprintf(wb, "\n\t]\n}\n");
162 }
163
164 // ----------------------------------------------------------------------------
165 // PROMETHEUS
166 // /api/v1/allmetrics?format=prometheus
167
168 static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable) {
169     size_t n;
170
171     for(n = 0; *s && n < usable ; d++, s++, n++) {
172         register char c = *s;
173
174         if(unlikely(!isalnum(c))) *d = '_';
175         else *d = c;
176     }
177     *d = '\0';
178
179     return n;
180 }
181
182 #define PROMETHEUS_ELEMENT_MAX 256
183
184 void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb) {
185     rrdhost_rdlock(host);
186
187     char hostname[PROMETHEUS_ELEMENT_MAX + 1];
188     prometheus_name_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
189
190     // for each chart
191     RRDSET *st;
192     rrdset_foreach_read(st, host) {
193         char chart[PROMETHEUS_ELEMENT_MAX + 1];
194         prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
195
196         buffer_strcat(wb, "\n");
197         if(rrdset_is_available_for_viewers(st)) {
198             rrdset_rdlock(st);
199
200             // for each dimension
201             RRDDIM *rd;
202             rrddim_foreach_read(rd, st) {
203                 if(rd->collections_counter) {
204                     char dimension[PROMETHEUS_ELEMENT_MAX + 1];
205                     prometheus_name_copy(dimension, rd->id, PROMETHEUS_ELEMENT_MAX);
206
207                     // buffer_sprintf(wb, "# HELP %s.%s %s\n", st->id, rd->id, st->units);
208
209                     switch(rd->algorithm) {
210                         case RRD_ALGORITHM_INCREMENTAL:
211                         case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
212                             buffer_sprintf(wb, "# TYPE %s_%s counter\n", chart, dimension);
213                             break;
214
215                         default:
216                             buffer_sprintf(wb, "# TYPE %s_%s gauge\n", chart, dimension);
217                             break;
218                     }
219
220                     // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
221                     // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
222                     //        (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
223
224                     buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
225                             chart, dimension, hostname, rd->last_collected_value,
226                             (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
227
228                 }
229             }
230
231             rrdset_unlock(st);
232         }
233     }
234
235     rrdhost_unlock(host);
236 }
237
238 // ----------------------------------------------------------------------------
239 // BASH
240 // /api/v1/allmetrics?format=bash
241
242 static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
243     size_t n;
244
245     for(n = 0; *s && n < usable ; d++, s++, n++) {
246         register char c = *s;
247
248         if(unlikely(!isalnum(c))) *d = '_';
249         else *d = (char)toupper(c);
250     }
251     *d = '\0';
252
253     return n;
254 }
255
256 #define SHELL_ELEMENT_MAX 100
257
258 void rrd_stats_api_v1_charts_allmetrics_shell(RRDHOST *host, BUFFER *wb) {
259     rrdhost_rdlock(host);
260
261     // for each chart
262     RRDSET *st;
263     rrdset_foreach_read(st, host) {
264         calculated_number total = 0.0;
265         char chart[SHELL_ELEMENT_MAX + 1];
266         shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
267
268         buffer_sprintf(wb, "\n# chart: %s (name: %s)\n", st->id, st->name);
269         if(rrdset_is_available_for_viewers(st)) {
270             rrdset_rdlock(st);
271
272             // for each dimension
273             RRDDIM *rd;
274             rrddim_foreach_read(rd, st) {
275                 if(rd->collections_counter) {
276                     char dimension[SHELL_ELEMENT_MAX + 1];
277                     shell_name_copy(dimension, rd->id, SHELL_ELEMENT_MAX);
278
279                     calculated_number n = rd->last_stored_value;
280
281                     if(isnan(n) || isinf(n))
282                         buffer_sprintf(wb, "NETDATA_%s_%s=\"\"      # %s\n", chart, dimension, st->units);
283                     else {
284                         if(rd->multiplier < 0 || rd->divisor < 0) n = -n;
285                         n = roundl(n);
286                         if(!rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)) total += n;
287                         buffer_sprintf(wb, "NETDATA_%s_%s=\"%0.0Lf\"      # %s\n", chart, dimension, n, st->units);
288                     }
289                 }
290             }
291
292             total = roundl(total);
293             buffer_sprintf(wb, "NETDATA_%s_VISIBLETOTAL=\"%0.0Lf\"      # %s\n", chart, total, st->units);
294             rrdset_unlock(st);
295         }
296     }
297
298     buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
299
300     RRDCALC *rc;
301     for(rc = host->alarms; rc ;rc = rc->next) {
302         if(!rc->rrdset) continue;
303
304         char chart[SHELL_ELEMENT_MAX + 1];
305         shell_name_copy(chart, rc->rrdset->id, SHELL_ELEMENT_MAX);
306
307         char alarm[SHELL_ELEMENT_MAX + 1];
308         shell_name_copy(alarm, rc->name, SHELL_ELEMENT_MAX);
309
310         calculated_number n = rc->value;
311
312         if(isnan(n) || isinf(n))
313             buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"\"      # %s\n", chart, alarm, rc->units);
314         else {
315             n = roundl(n);
316             buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_VALUE=\"%0.0Lf\"      # %s\n", chart, alarm, n, rc->units);
317         }
318
319         buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
320     }
321
322     rrdhost_unlock(host);
323 }
324
325 // ----------------------------------------------------------------------------
326
327 // RRDR dimension options
328 #define RRDR_EMPTY      0x01 // the dimension contains / the value is empty (null)
329 #define RRDR_RESET      0x02 // the dimension contains / the value is reset
330 #define RRDR_HIDDEN     0x04 // the dimension contains / the value is hidden
331 #define RRDR_NONZERO    0x08 // the dimension contains / the value is non-zero
332 #define RRDR_SELECTED   0x10 // the dimension is selected
333
334 // RRDR result options
335 #define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
336 #define RRDR_RESULT_OPTION_RELATIVE 0x00000002
337
338 typedef struct rrdresult {
339     RRDSET *st;         // the chart this result refers to
340
341     uint32_t result_options;    // RRDR_RESULT_OPTION_*
342
343     int d;                  // the number of dimensions
344     long n;                 // the number of values in the arrays
345     long rows;              // the number of rows used
346
347     uint8_t *od;            // the options for the dimensions
348
349     time_t *t;              // array of n timestamps
350     calculated_number *v;   // array n x d values
351     uint8_t *o;             // array n x d options
352
353     long c;                 // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
354
355     long group;             // how many collected values were grouped for each row
356     int update_every;       // what is the suggested update frequency in seconds
357
358     calculated_number min;
359     calculated_number max;
360
361     time_t before;
362     time_t after;
363
364     int has_st_lock;        // if st is read locked by us
365 } RRDR;
366
367 #define rrdr_rows(r) ((r)->rows)
368
369 /*
370 static void rrdr_dump(RRDR *r)
371 {
372     long c, i;
373     RRDDIM *d;
374
375     fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
376
377     for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
378         fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
379                 , d->id
380                 , d->name
381                 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
382                 , (r->od[c] & RRDR_RESET)?"RESET ":""
383                 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
384                 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
385                 );
386     }
387
388     if(r->rows <= 0) {
389         fprintf(stderr, "RRDR does not have any values in it.\n");
390         return;
391     }
392
393     fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
394
395     // for each line in the array
396     for(i = 0; i < r->rows ;i++) {
397         calculated_number *cn = &r->v[ i * r->d ];
398         uint8_t *co = &r->o[ i * r->d ];
399
400         // print the id and the timestamp of the line
401         fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
402
403         // for each dimension
404         for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
405             if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
406             if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
407
408             if(co[c] & RRDR_EMPTY)
409                 fprintf(stderr, "null ");
410             else
411                 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
412                     , cn[c]
413                     , (co[c] & RRDR_EMPTY)?"E":" "
414                     , (co[c] & RRDR_RESET)?"R":" "
415                     , (co[c] & RRDR_HIDDEN)?"H":" "
416                     , (co[c] & RRDR_NONZERO)?"N":" "
417                     );
418         }
419
420         fprintf(stderr, "\n");
421     }
422 }
423 */
424
425 void rrdr_disable_not_selected_dimensions(RRDR *r, uint32_t options, const char *dims) {
426     rrdset_check_rdlock(r->st);
427
428     if(unlikely(!dims || !*dims)) return;
429
430     char b[strlen(dims) + 1];
431     char *o = b, *tok;
432     strcpy(o, dims);
433
434     long c, dims_selected = 0, dims_not_hidden_not_zero = 0;
435     RRDDIM *d;
436
437     // disable all of them
438     for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
439         r->od[c] |= RRDR_HIDDEN;
440
441     while(o && *o && (tok = mystrsep(&o, ",|"))) {
442         if(!*tok) continue;
443         
444         uint32_t hash = simple_hash(tok);
445
446         // find it and enable it
447         for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
448             if(unlikely((hash == d->hash && !strcmp(d->id, tok)) || (hash == d->hash_name && !strcmp(d->name, tok)))) {
449
450                 if(likely(r->od[c] & RRDR_HIDDEN)) {
451                     r->od[c] |= RRDR_SELECTED;
452                     r->od[c] &= ~RRDR_HIDDEN;
453                     dims_selected++;
454                 }
455
456                 // since the user needs this dimension
457                 // make it appear as NONZERO, to return it
458                 // even if the dimension has only zeros
459                 // unless option non_zero is set
460                 if(likely(!(options & RRDR_OPTION_NONZERO)))
461                     r->od[c] |= RRDR_NONZERO;
462
463                 // count the visible dimensions
464                 if(likely(r->od[c] & RRDR_NONZERO))
465                     dims_not_hidden_not_zero++;
466             }
467         }
468     }
469
470     // check if all dimensions are hidden
471     if(unlikely(!dims_not_hidden_not_zero && dims_selected)) {
472         // there are a few selected dimensions
473         // but they are all zero
474         // enable the selected ones
475         // to avoid returning an empty chart
476         for(c = 0, d = r->st->dimensions; d ;c++, d = d->next)
477             if(unlikely(r->od[c] & RRDR_SELECTED))
478                 r->od[c] |= RRDR_NONZERO;
479     }
480 }
481
482 void rrdr_buffer_print_format(BUFFER *wb, uint32_t format)
483 {
484     switch(format) {
485     case DATASOURCE_JSON:
486         buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
487         break;
488
489     case DATASOURCE_DATATABLE_JSON:
490         buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
491         break;
492
493     case DATASOURCE_DATATABLE_JSONP:
494         buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
495         break;
496
497     case DATASOURCE_JSONP:
498         buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
499         break;
500
501     case DATASOURCE_SSV:
502         buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
503         break;
504
505     case DATASOURCE_CSV:
506         buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
507         break;
508
509     case DATASOURCE_TSV:
510         buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
511         break;
512
513     case DATASOURCE_HTML:
514         buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
515         break;
516
517     case DATASOURCE_JS_ARRAY:
518         buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
519         break;
520
521     case DATASOURCE_SSV_COMMA:
522         buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
523         break;
524
525     default:
526         buffer_strcat(wb, "unknown");
527         break;
528     }
529 }
530
531 uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
532 {
533     rrdset_check_rdlock(r->st);
534
535     (void)dims;
536
537     if(options & RRDR_OPTION_NONZERO) {
538         long i;
539
540         // commented due to #1514
541
542         //if(dims && *dims) {
543             // the caller wants specific dimensions
544             // disable NONZERO option
545             // to make sure we don't accidentally prevent
546             // the specific dimensions from being returned
547             // i = 0;
548         //}
549         //else {
550             // find how many dimensions are not zero
551             long c;
552             RRDDIM *rd;
553             for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
554                 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
555                 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
556                 i++;
557             }
558         //}
559
560         // if with nonzero we get i = 0 (no dimensions will be returned)
561         // disable nonzero to show all dimensions
562         if(!i) options &= ~RRDR_OPTION_NONZERO;
563     }
564
565     return options;
566 }
567
568 void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
569 {
570     rrdset_check_rdlock(r->st);
571
572     long rows = rrdr_rows(r);
573     long c, i;
574     RRDDIM *rd;
575
576     //info("JSONWRAPPER(): %s: BEGIN", r->st->id);
577     char kq[2] = "",                    // key quote
578         sq[2] = "";                     // string quote
579
580     if( options & RRDR_OPTION_GOOGLE_JSON ) {
581         kq[0] = '\0';
582         sq[0] = '\'';
583     }
584     else {
585         kq[0] = '"';
586         sq[0] = '"';
587     }
588
589     buffer_sprintf(wb, "{\n"
590             "   %sapi%s: 1,\n"
591             "   %sid%s: %s%s%s,\n"
592             "   %sname%s: %s%s%s,\n"
593             "   %sview_update_every%s: %d,\n"
594             "   %supdate_every%s: %d,\n"
595             "   %sfirst_entry%s: %u,\n"
596             "   %slast_entry%s: %u,\n"
597             "   %sbefore%s: %u,\n"
598             "   %safter%s: %u,\n"
599             "   %sdimension_names%s: ["
600             , kq, kq
601             , kq, kq, sq, r->st->id, sq
602             , kq, kq, sq, r->st->name, sq
603             , kq, kq, r->update_every
604             , kq, kq, r->st->update_every
605             , kq, kq, (uint32_t)rrdset_first_entry_t(r->st)
606             , kq, kq, (uint32_t)rrdset_last_entry_t(r->st)
607             , kq, kq, (uint32_t)r->before
608             , kq, kq, (uint32_t)r->after
609             , kq, kq);
610
611     for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
612         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
613         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
614
615         if(i) buffer_strcat(wb, ", ");
616         buffer_strcat(wb, sq);
617         buffer_strcat(wb, rd->name);
618         buffer_strcat(wb, sq);
619         i++;
620     }
621     if(!i) {
622 #ifdef NETDATA_INTERNAL_CHECKS
623         error("RRDR is empty for %s (RRDR has %d dimensions, options is 0x%08x)", r->st->id, r->d, options);
624 #endif
625         rows = 0;
626         buffer_strcat(wb, sq);
627         buffer_strcat(wb, "no data");
628         buffer_strcat(wb, sq);
629     }
630
631     buffer_sprintf(wb, "],\n"
632             "   %sdimension_ids%s: ["
633             , kq, kq);
634
635     for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
636         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
637         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
638
639         if(i) buffer_strcat(wb, ", ");
640         buffer_strcat(wb, sq);
641         buffer_strcat(wb, rd->id);
642         buffer_strcat(wb, sq);
643         i++;
644     }
645     if(!i) {
646         rows = 0;
647         buffer_strcat(wb, sq);
648         buffer_strcat(wb, "no data");
649         buffer_strcat(wb, sq);
650     }
651
652     buffer_sprintf(wb, "],\n"
653             "   %slatest_values%s: ["
654             , kq, kq);
655
656     for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
657         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
658         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
659
660         if(i) buffer_strcat(wb, ", ");
661         i++;
662
663         storage_number n = rd->values[rrdset_last_slot(r->st)];
664
665         if(!does_storage_number_exist(n))
666             buffer_strcat(wb, "null");
667         else
668             buffer_rrd_value(wb, unpack_storage_number(n));
669     }
670     if(!i) {
671         rows = 0;
672         buffer_strcat(wb, "null");
673     }
674
675     buffer_sprintf(wb, "],\n"
676             "   %sview_latest_values%s: ["
677             , kq, kq);
678
679     i = 0;
680     if(rows) {
681         for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
682             if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
683             if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
684
685             if(i) buffer_strcat(wb, ", ");
686             i++;
687
688             calculated_number *cn = &r->v[ (0) * r->d ];
689             uint8_t *co = &r->o[ (0) * r->d ];
690
691             if(co[c] & RRDR_EMPTY)
692                 buffer_strcat(wb, "null");
693             else
694                 buffer_rrd_value(wb, cn[c]);
695         }
696     }
697     if(!i) {
698         rows = 0;
699         buffer_strcat(wb, "null");
700     }
701
702     buffer_sprintf(wb, "],\n"
703             "   %sdimensions%s: %ld,\n"
704             "   %spoints%s: %ld,\n"
705             "   %sformat%s: %s"
706             , kq, kq, i
707             , kq, kq, rows
708             , kq, kq, sq
709             );
710
711     rrdr_buffer_print_format(wb, format);
712
713     buffer_sprintf(wb, "%s,\n"
714             "   %sresult%s: "
715             , sq
716             , kq, kq
717             );
718
719     if(string_value) buffer_strcat(wb, sq);
720     //info("JSONWRAPPER(): %s: END", r->st->id);
721 }
722
723 void rrdr_json_wrapper_end(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
724 {
725     (void)format;
726
727     char kq[2] = "",                    // key quote
728         sq[2] = "";                     // string quote
729
730     if( options & RRDR_OPTION_GOOGLE_JSON ) {
731         kq[0] = '\0';
732         sq[0] = '\'';
733     }
734     else {
735         kq[0] = '"';
736         sq[0] = '"';
737     }
738
739     if(string_value) buffer_strcat(wb, sq);
740
741     buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
742     buffer_rrd_value(wb, r->min);
743     buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
744     buffer_rrd_value(wb, r->max);
745     buffer_strcat(wb, "\n}\n");
746 }
747
748 #define JSON_DATES_JS 1
749 #define JSON_DATES_TIMESTAMP 2
750
751 static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
752 {
753     rrdset_check_rdlock(r->st);
754
755     //info("RRD2JSON(): %s: BEGIN", r->st->id);
756     int row_annotations = 0, dates, dates_with_new = 0;
757     char kq[2] = "",                    // key quote
758         sq[2] = "",                     // string quote
759         pre_label[101] = "",            // before each label
760         post_label[101] = "",           // after each label
761         pre_date[101] = "",             // the beginning of line, to the date
762         post_date[101] = "",            // closing the date
763         pre_value[101] = "",            // before each value
764         post_value[101] = "",           // after each value
765         post_line[101] = "",            // at the end of each row
766         normal_annotation[201] = "",    // default row annotation
767         overflow_annotation[201] = "",  // overflow row annotation
768         data_begin[101] = "",           // between labels and values
769         finish[101] = "";               // at the end of everything
770
771     if(datatable) {
772         dates = JSON_DATES_JS;
773         if( options & RRDR_OPTION_GOOGLE_JSON ) {
774             kq[0] = '\0';
775             sq[0] = '\'';
776         }
777         else {
778             kq[0] = '"';
779             sq[0] = '"';
780         }
781         row_annotations = 1;
782         snprintfz(pre_date,   100, "        {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
783         snprintfz(post_date,  100, "%s}", sq);
784         snprintfz(pre_label,  100, ",\n     {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
785         snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
786         snprintfz(pre_value,  100, ",{%sv%s:", kq, kq);
787         strcpy(post_value,         "}");
788         strcpy(post_line,          "]}");
789         snprintfz(data_begin, 100, "\n  ],\n    %srows%s:\n [\n", kq, kq);
790         strcpy(finish,             "\n  ]\n}");
791
792         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);
793         snprintfz(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
794
795         buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
796         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);
797         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);
798         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);
799
800         // remove the valueobjects flag
801         // google wants its own keys
802         if(options & RRDR_OPTION_OBJECTSROWS)
803             options &= ~RRDR_OPTION_OBJECTSROWS;
804     }
805     else {
806         kq[0] = '"';
807         sq[0] = '"';
808         if(options & RRDR_OPTION_GOOGLE_JSON) {
809             dates = JSON_DATES_JS;
810             dates_with_new = 1;
811         }
812         else {
813             dates = JSON_DATES_TIMESTAMP;
814             dates_with_new = 0;
815         }
816         if( options & RRDR_OPTION_OBJECTSROWS )
817             strcpy(pre_date, "      { ");
818         else
819             strcpy(pre_date, "      [ ");
820         strcpy(pre_label,  ", \"");
821         strcpy(post_label, "\"");
822         strcpy(pre_value,  ", ");
823         if( options & RRDR_OPTION_OBJECTSROWS )
824             strcpy(post_line, "}");
825         else
826             strcpy(post_line, "]");
827         snprintfz(data_begin, 100, "],\n    %sdata%s:\n [\n", kq, kq);
828         strcpy(finish,             "\n  ]\n}");
829
830         buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
831         buffer_sprintf(wb, "%stime%s", sq, sq);
832     }
833
834     // -------------------------------------------------------------------------
835     // print the JSON header
836
837     long c, i;
838     RRDDIM *rd;
839
840     // print the header lines
841     for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
842         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
843         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
844
845         buffer_strcat(wb, pre_label);
846         buffer_strcat(wb, rd->name);
847         buffer_strcat(wb, post_label);
848         i++;
849     }
850     if(!i) {
851         buffer_strcat(wb, pre_label);
852         buffer_strcat(wb, "no data");
853         buffer_strcat(wb, post_label);
854     }
855
856     // print the begin of row data
857     buffer_strcat(wb, data_begin);
858
859     // if all dimensions are hidden, print a null
860     if(!i) {
861         buffer_strcat(wb, finish);
862         return;
863     }
864
865     long start = 0, end = rrdr_rows(r), step = 1;
866     if((options & RRDR_OPTION_REVERSED)) {
867         start = rrdr_rows(r) - 1;
868         end = -1;
869         step = -1;
870     }
871
872     // for each line in the array
873     calculated_number total = 1;
874     for(i = start; i != end ;i += step) {
875         calculated_number *cn = &r->v[ i * r->d ];
876         uint8_t *co = &r->o[ i * r->d ];
877
878         time_t now = r->t[i];
879
880         if(dates == JSON_DATES_JS) {
881             // generate the local date time
882             struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
883             if(!tm) { error("localtime_r() failed."); continue; }
884
885             if(likely(i != start)) buffer_strcat(wb, ",\n");
886             buffer_strcat(wb, pre_date);
887
888             if( options & RRDR_OPTION_OBJECTSROWS )
889                 buffer_sprintf(wb, "%stime%s: ", kq, kq);
890
891             if(dates_with_new)
892                 buffer_strcat(wb, "new ");
893
894             buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
895
896             buffer_strcat(wb, post_date);
897
898             if(row_annotations) {
899                 // google supports one annotation per row
900                 int annotation_found = 0;
901                 for(c = 0, rd = r->st->dimensions; rd ;c++, rd = rd->next) {
902                     if(co[c] & RRDR_RESET) {
903                         buffer_strcat(wb, overflow_annotation);
904                         annotation_found = 1;
905                         break;
906                     }
907                 }
908                 if(!annotation_found)
909                     buffer_strcat(wb, normal_annotation);
910             }
911         }
912         else {
913             // print the timestamp of the line
914             if(likely(i != start)) buffer_strcat(wb, ",\n");
915             buffer_strcat(wb, pre_date);
916
917             if( options & RRDR_OPTION_OBJECTSROWS )
918                 buffer_sprintf(wb, "%stime%s: ", kq, kq);
919
920             buffer_rrd_value(wb, (calculated_number)r->t[i]);
921             // in ms
922             if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
923
924             buffer_strcat(wb, post_date);
925         }
926
927         if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
928             total = 0;
929             for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
930                 calculated_number n = cn[c];
931
932                 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
933                     n = -n;
934
935                 total += n;
936             }
937             // prevent a division by zero
938             if(total == 0) total = 1;
939         }
940
941         // for each dimension
942         for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
943             if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
944             if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
945
946             calculated_number n = cn[c];
947
948             buffer_strcat(wb, pre_value);
949
950             if( options & RRDR_OPTION_OBJECTSROWS )
951                 buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
952
953             if(co[c] & RRDR_EMPTY) {
954                 if(options & RRDR_OPTION_NULL2ZERO)
955                     buffer_strcat(wb, "0");
956                 else
957                     buffer_strcat(wb, "null");
958             }
959             else {
960                 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
961                     n = -n;
962
963                 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
964                     n = n * 100 / total;
965
966                 buffer_rrd_value(wb, n);
967             }
968
969             buffer_strcat(wb, post_value);
970         }
971
972         buffer_strcat(wb, post_line);
973     }
974
975     buffer_strcat(wb, finish);
976     //info("RRD2JSON(): %s: END", r->st->id);
977 }
978
979 static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
980 {
981     rrdset_check_rdlock(r->st);
982
983     //info("RRD2CSV(): %s: BEGIN", r->st->id);
984     long c, i;
985     RRDDIM *d;
986
987     // print the csv header
988     for(c = 0, i = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
989         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
990         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
991
992         if(!i) {
993             buffer_strcat(wb, startline);
994             if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
995             buffer_strcat(wb, "time");
996             if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
997         }
998         buffer_strcat(wb, separator);
999         if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1000         buffer_strcat(wb, d->name);
1001         if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
1002         i++;
1003     }
1004     buffer_strcat(wb, endline);
1005
1006     if(!i) {
1007         // no dimensions present
1008         return;
1009     }
1010
1011     long start = 0, end = rrdr_rows(r), step = 1;
1012     if((options & RRDR_OPTION_REVERSED)) {
1013         start = rrdr_rows(r) - 1;
1014         end = -1;
1015         step = -1;
1016     }
1017
1018     // for each line in the array
1019     calculated_number total = 1;
1020     for(i = start; i != end ;i += step) {
1021         calculated_number *cn = &r->v[ i * r->d ];
1022         uint8_t *co = &r->o[ i * r->d ];
1023
1024         buffer_strcat(wb, betweenlines);
1025         buffer_strcat(wb, startline);
1026
1027         time_t now = r->t[i];
1028
1029         if((options & RRDR_OPTION_SECONDS) || (options & RRDR_OPTION_MILLISECONDS)) {
1030             // print the timestamp of the line
1031             buffer_rrd_value(wb, (calculated_number)now);
1032             // in ms
1033             if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
1034         }
1035         else {
1036             // generate the local date time
1037             struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
1038             if(!tm) { error("localtime() failed."); continue; }
1039             buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1040         }
1041
1042         if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1043             total = 0;
1044             for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1045                 calculated_number n = cn[c];
1046
1047                 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1048                     n = -n;
1049
1050                 total += n;
1051             }
1052             // prevent a division by zero
1053             if(total == 0) total = 1;
1054         }
1055
1056         // for each dimension
1057         for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1058             if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1059             if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1060
1061             buffer_strcat(wb, separator);
1062
1063             calculated_number n = cn[c];
1064
1065             if(co[c] & RRDR_EMPTY) {
1066                 if(options & RRDR_OPTION_NULL2ZERO)
1067                     buffer_strcat(wb, "0");
1068                 else
1069                     buffer_strcat(wb, "null");
1070             }
1071             else {
1072                 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1073                     n = -n;
1074
1075                 if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1076                     n = n * 100 / total;
1077
1078                 buffer_rrd_value(wb, n);
1079             }
1080         }
1081
1082         buffer_strcat(wb, endline);
1083     }
1084     //info("RRD2CSV(): %s: END", r->st->id);
1085 }
1086
1087 inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
1088     rrdset_check_rdlock(r->st);
1089
1090     long c;
1091     RRDDIM *d;
1092
1093     calculated_number *cn = &r->v[ i * r->d ];
1094     uint8_t *co = &r->o[ i * r->d ];
1095
1096     calculated_number sum = 0, min = 0, max = 0, v;
1097     int all_null = 1, init = 1;
1098
1099     calculated_number total = 1;
1100     if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1101         total = 0;
1102         for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1103             calculated_number n = cn[c];
1104
1105             if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1106                 n = -n;
1107
1108             total += n;
1109         }
1110         // prevent a division by zero
1111         if(total == 0) total = 1;
1112     }
1113
1114     // for each dimension
1115     for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
1116         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
1117         if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
1118
1119         calculated_number n = cn[c];
1120
1121         if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1122             n = -n;
1123
1124         if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1125             n = n * 100 / total;
1126
1127         if(unlikely(init)) {
1128             if(n > 0) {
1129                 min = 0;
1130                 max = n;
1131             }
1132             else {
1133                 min = n;
1134                 max = 0;
1135             }
1136             init = 0;
1137         }
1138
1139         if(likely(!(co[c] & RRDR_EMPTY))) {
1140             all_null = 0;
1141             sum += n;
1142         }
1143
1144         if(n < min) min = n;
1145         if(n > max) max = n;
1146     }
1147
1148     if(unlikely(all_null)) {
1149         if(likely(all_values_are_null))
1150             *all_values_are_null = 1;
1151         return 0;
1152     }
1153     else {
1154         if(likely(all_values_are_null))
1155             *all_values_are_null = 0;
1156     }
1157
1158     if(options & RRDR_OPTION_MIN2MAX)
1159         v = max - min;
1160     else
1161         v = sum;
1162
1163     return v;
1164 }
1165
1166 static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
1167 {
1168     //info("RRD2SSV(): %s: BEGIN", r->st->id);
1169     long i;
1170
1171     buffer_strcat(wb, prefix);
1172     long start = 0, end = rrdr_rows(r), step = 1;
1173     if((options & RRDR_OPTION_REVERSED)) {
1174         start = rrdr_rows(r) - 1;
1175         end = -1;
1176         step = -1;
1177     }
1178
1179     // for each line in the array
1180     for(i = start; i != end ;i += step) {
1181         int all_values_are_null = 0;
1182         calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
1183
1184         if(likely(i != start)) {
1185             if(r->min > v) r->min = v;
1186             if(r->max < v) r->max = v;
1187         }
1188         else {
1189             r->min = v;
1190             r->max = v;
1191         }
1192
1193         if(likely(i != start))
1194             buffer_strcat(wb, separator);
1195
1196         if(all_values_are_null) {
1197             if(options & RRDR_OPTION_NULL2ZERO)
1198                 buffer_strcat(wb, "0");
1199             else
1200                 buffer_strcat(wb, "null");
1201         }
1202         else
1203             buffer_rrd_value(wb, v);
1204     }
1205     buffer_strcat(wb, suffix);
1206     //info("RRD2SSV(): %s: END", r->st->id);
1207 }
1208
1209 inline static calculated_number *rrdr_line_values(RRDR *r)
1210 {
1211     return &r->v[ r->c * r->d ];
1212 }
1213
1214 inline static uint8_t *rrdr_line_options(RRDR *r)
1215 {
1216     return &r->o[ r->c * r->d ];
1217 }
1218
1219 inline static int rrdr_line_init(RRDR *r, time_t t)
1220 {
1221     r->c++;
1222
1223     if(unlikely(r->c >= r->n)) {
1224         error("requested to step above RRDR size for chart %s", r->st->name);
1225         r->c = r->n - 1;
1226     }
1227
1228     // save the time
1229     r->t[r->c] = t;
1230
1231     return 1;
1232 }
1233
1234 inline static void rrdr_lock_rrdset(RRDR *r) {
1235     if(unlikely(!r)) {
1236         error("NULL value given!");
1237         return;
1238     }
1239
1240     rrdset_rdlock(r->st);
1241     r->has_st_lock = 1;
1242 }
1243
1244 inline static void rrdr_unlock_rrdset(RRDR *r) {
1245     if(unlikely(!r)) {
1246         error("NULL value given!");
1247         return;
1248     }
1249
1250     if(likely(r->has_st_lock)) {
1251         rrdset_unlock(r->st);
1252         r->has_st_lock = 0;
1253     }
1254 }
1255
1256 inline static void rrdr_free(RRDR *r)
1257 {
1258     if(unlikely(!r)) {
1259         error("NULL value given!");
1260         return;
1261     }
1262
1263     rrdr_unlock_rrdset(r);
1264     freez(r->t);
1265     freez(r->v);
1266     freez(r->o);
1267     freez(r->od);
1268     freez(r);
1269 }
1270
1271 static inline void rrdr_done(RRDR *r)
1272 {
1273     r->rows = r->c + 1;
1274     r->c = 0;
1275 }
1276
1277 static RRDR *rrdr_create(RRDSET *st, long n)
1278 {
1279     if(unlikely(!st)) {
1280         error("NULL value given!");
1281         return NULL;
1282     }
1283
1284     RRDR *r = callocz(1, sizeof(RRDR));
1285     r->st = st;
1286
1287     rrdr_lock_rrdset(r);
1288
1289     RRDDIM *rd;
1290     rrddim_foreach_read(rd, st) r->d++;
1291
1292     r->n = n;
1293
1294     r->t = mallocz(n * sizeof(time_t));
1295     r->v = mallocz(n * r->d * sizeof(calculated_number));
1296     r->o = mallocz(n * r->d * sizeof(uint8_t));
1297     r->od = mallocz(r->d * sizeof(uint8_t));
1298
1299     // set the hidden flag on hidden dimensions
1300     int c;
1301     for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
1302         if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
1303             r->od[c] = RRDR_HIDDEN;
1304         else
1305             r->od[c] = 0;
1306     }
1307
1308     r->c = -1;
1309     r->group = 1;
1310     r->update_every = 1;
1311
1312     return r;
1313 }
1314
1315 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method, int aligned)
1316 {
1317     int debug = rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?1:0;
1318     int absolute_period_requested = -1;
1319
1320     time_t first_entry_t = rrdset_first_entry_t(st);
1321     time_t last_entry_t  = rrdset_last_entry_t(st);
1322
1323     if(before == 0 && after == 0) {
1324         // dump the all the data
1325         before = last_entry_t;
1326         after = first_entry_t;
1327         absolute_period_requested = 0;
1328     }
1329
1330     // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
1331     if(((before < 0)?-before:before) <= API_RELATIVE_TIME_MAX) {
1332         if(abs(before) % st->update_every) {
1333             // make sure it is multiple of st->update_every
1334             if(before < 0) before = before - st->update_every - before % st->update_every;
1335             else           before = before + st->update_every - before % st->update_every;
1336         }
1337         if(before > 0) before = first_entry_t + before;
1338         else           before = last_entry_t  + before;
1339         absolute_period_requested = 0;
1340     }
1341
1342     // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
1343     if(((after < 0)?-after:after) <= API_RELATIVE_TIME_MAX) {
1344         if(after == 0) after = -st->update_every;
1345         if(abs(after) % st->update_every) {
1346             // make sure it is multiple of st->update_every
1347             if(after < 0) after = after - st->update_every - after % st->update_every;
1348             else          after = after + st->update_every - after % st->update_every;
1349         }
1350         after = before + after;
1351         absolute_period_requested = 0;
1352     }
1353
1354     if(absolute_period_requested == -1)
1355         absolute_period_requested = 1;
1356
1357     // make sure they are within our timeframe
1358     if(before > last_entry_t)  before = last_entry_t;
1359     if(before < first_entry_t) before = first_entry_t;
1360
1361     if(after > last_entry_t)  after = last_entry_t;
1362     if(after < first_entry_t) after = first_entry_t;
1363
1364     // check if they are upside down
1365     if(after > before) {
1366         time_t tmp = before;
1367         before = after;
1368         after = tmp;
1369     }
1370
1371     // the duration of the chart
1372     time_t duration = before - after;
1373     long available_points = duration / st->update_every;
1374
1375     if(duration <= 0 || available_points <= 0)
1376         return rrdr_create(st, 1);
1377
1378     // check the wanted points
1379     if(points < 0) points = -points;
1380     if(points > available_points) points = available_points;
1381     if(points == 0) points = available_points;
1382
1383     // calculate proper grouping of source data
1384     long group = available_points / points;
1385     if(group <= 0) group = 1;
1386
1387     // round group to the closest integer
1388     if(available_points % points > points / 2) group++;
1389
1390     time_t after_new  = (aligned) ? (after  - (after  % (group * st->update_every))) : after;
1391     time_t before_new = (aligned) ? (before - (before % (group * st->update_every))) : before;
1392     long points_new   = (before_new - after_new) / st->update_every / group;
1393
1394     // find the starting and ending slots in our round robin db
1395     long    start_at_slot = rrdset_time2slot(st, before_new),
1396             stop_at_slot  = rrdset_time2slot(st, after_new);
1397
1398 #ifdef NETDATA_INTERNAL_CHECKS
1399     if(after_new < first_entry_t) {
1400         error("after_new %u is too small, minimum %u", (uint32_t)after_new, (uint32_t)first_entry_t);
1401     }
1402     if(after_new > last_entry_t) {
1403         error("after_new %u is too big, maximum %u", (uint32_t)after_new, (uint32_t)last_entry_t);
1404     }
1405     if(before_new < first_entry_t) {
1406         error("before_new %u is too small, minimum %u", (uint32_t)before_new, (uint32_t)first_entry_t);
1407     }
1408     if(before_new > last_entry_t) {
1409         error("before_new %u is too big, maximum %u", (uint32_t)before_new, (uint32_t)last_entry_t);
1410     }
1411     if(start_at_slot < 0 || start_at_slot >= st->entries) {
1412         error("start_at_slot is invalid %ld, expected 0 to %ld", start_at_slot, st->entries - 1);
1413     }
1414     if(stop_at_slot < 0 || stop_at_slot >= st->entries) {
1415         error("stop_at_slot is invalid %ld, expected 0 to %ld", stop_at_slot, st->entries - 1);
1416     }
1417     if(points_new > (before_new - after_new) / group / st->update_every + 1) {
1418         error("points_new %ld is more than points %ld", points_new, (before_new - after_new) / group / st->update_every + 1);
1419     }
1420 #endif
1421
1422     //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);
1423
1424     after = after_new;
1425     before = before_new;
1426     duration = before - after;
1427     points = points_new;
1428
1429     // Now we have:
1430     // before = the end time of the calculation
1431     // after = the start time of the calculation
1432     // duration = the duration of the calculation
1433     // group = the number of source points to aggregate / group together
1434     // method = the method of grouping source points
1435     // points = the number of points to generate
1436
1437
1438     // -------------------------------------------------------------------------
1439     // initialize our result set
1440
1441     RRDR *r = rrdr_create(st, points);
1442     if(!r) {
1443 #ifdef NETDATA_INTERNAL_CHECKS
1444         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);
1445 #endif
1446         return NULL;
1447     }
1448     if(!r->d) {
1449 #ifdef NETDATA_INTERNAL_CHECKS
1450         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);
1451 #endif
1452         return r;
1453     }
1454
1455     if(absolute_period_requested == 1)
1456         r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
1457     else
1458         r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
1459
1460     // find how many dimensions we have
1461     long dimensions = r->d;
1462
1463
1464     // -------------------------------------------------------------------------
1465     // checks for debugging
1466
1467     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"
1468             , st->id
1469             , (uint32_t)first_entry_t
1470             , (uint32_t)last_entry_t
1471             , (uint32_t)(last_entry_t - first_entry_t)
1472             , (uint32_t)after
1473             , (uint32_t)before
1474             , (uint32_t)duration
1475             , points
1476             , group
1477             );
1478
1479
1480     // -------------------------------------------------------------------------
1481     // temp arrays for keeping values per dimension
1482
1483     calculated_number   last_values[dimensions]; // keep the last value of each dimension
1484     calculated_number   group_values[dimensions]; // keep sums when grouping
1485     long                group_counts[dimensions]; // keep the number of values added to group_values
1486     uint8_t             group_options[dimensions];
1487     uint8_t             found_non_zero[dimensions];
1488
1489
1490     // initialize them
1491     RRDDIM *rd;
1492     long c;
1493     rrdset_check_rdlock(st);
1494     for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1495         last_values[c] = 0;
1496         group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1497         group_counts[c] = 0;
1498         group_options[c] = 0;
1499         found_non_zero[c] = 0;
1500     }
1501
1502
1503     // -------------------------------------------------------------------------
1504     // the main loop
1505
1506     time_t  now = rrdset_slot2time(st, start_at_slot),
1507             dt = st->update_every,
1508             group_start_t = 0;
1509
1510     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"
1511             , st->id
1512             , (uint32_t)after
1513             , stop_at_slot
1514             , (uint32_t)before
1515             , start_at_slot
1516             , (uint32_t)now
1517             , st->current_entry
1518             , st->entries
1519             );
1520
1521     r->group = group;
1522     r->update_every = (int)group * st->update_every;
1523     r->before = now;
1524     r->after = now;
1525
1526     //info("RRD2RRDR(): %s: STARTING", st->id);
1527
1528     long slot = start_at_slot, counter = 0, stop_now = 0, added = 0, group_count = 0, add_this = 0;
1529     for(; !stop_now ; now -= dt, slot--, counter++) {
1530         if(unlikely(slot < 0)) slot = st->entries - 1;
1531         if(unlikely(slot == stop_at_slot)) stop_now = counter;
1532
1533         if(unlikely(debug)) debug(D_RRD_STATS, "ROW %s slot: %ld, entries_counter: %ld, group_count: %ld, added: %ld, now: %ld, %s %s"
1534                 , st->id
1535                 , slot
1536                 , counter
1537                 , group_count + 1
1538                 , added
1539                 , now
1540                 , (group_count + 1 == group)?"PRINT":"  -  "
1541                 , (now >= after && now <= before)?"RANGE":"  -  "
1542                 );
1543
1544         // make sure we return data in the proper time range
1545         if(unlikely(now > before)) continue;
1546         if(unlikely(now < after)) break;
1547
1548         if(unlikely(group_count == 0)) {
1549             group_start_t = now;
1550         }
1551         group_count++;
1552
1553         if(unlikely(group_count == group)) {
1554             if(unlikely(added >= points)) break;
1555             add_this = 1;
1556         }
1557
1558         // do the calculations
1559         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1560             storage_number n = rd->values[slot];
1561             if(unlikely(!does_storage_number_exist(n))) continue;
1562
1563             group_counts[c]++;
1564
1565             calculated_number value = unpack_storage_number(n);
1566             if(likely(value != 0.0)) {
1567                 group_options[c] |= RRDR_NONZERO;
1568                 found_non_zero[c] = 1;
1569             }
1570
1571             if(unlikely(did_storage_number_reset(n)))
1572                 group_options[c] |= RRDR_RESET;
1573
1574             switch(group_method) {
1575                 case GROUP_MIN:
1576                     if(unlikely(isnan(group_values[c])) ||
1577                             fabsl(value) < fabsl(group_values[c]))
1578                         group_values[c] = value;
1579                     break;
1580
1581                 case GROUP_MAX:
1582                     if(unlikely(isnan(group_values[c])) ||
1583                             fabsl(value) > fabsl(group_values[c]))
1584                         group_values[c] = value;
1585                     break;
1586
1587                 default:
1588                 case GROUP_SUM:
1589                 case GROUP_AVERAGE:
1590                 case GROUP_UNDEFINED:
1591                     group_values[c] += value;
1592                     break;
1593
1594                 case GROUP_INCREMENTAL_SUM:
1595                     if(unlikely(slot == start_at_slot))
1596                         last_values[c] = value;
1597
1598                     group_values[c] += last_values[c] - value;
1599                     last_values[c] = value;
1600                     break;
1601             }
1602         }
1603
1604         // added it
1605         if(unlikely(add_this)) {
1606             if(unlikely(!rrdr_line_init(r, group_start_t))) break;
1607
1608             r->after = now;
1609
1610             calculated_number *cn = rrdr_line_values(r);
1611             uint8_t *co = rrdr_line_options(r);
1612
1613             for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1614
1615                 // update the dimension options
1616                 if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
1617
1618                 // store the specific point options
1619                 co[c] = group_options[c];
1620
1621                 // store the value
1622                 if(unlikely(group_counts[c] == 0)) {
1623                     cn[c] = 0.0;
1624                     co[c] |= RRDR_EMPTY;
1625                     group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1626                 }
1627                 else {
1628                     switch(group_method) {
1629                         case GROUP_MIN:
1630                         case GROUP_MAX:
1631                             if(unlikely(isnan(group_values[c])))
1632                                 cn[c] = 0;
1633                             else {
1634                                 cn[c] = group_values[c];
1635                                 group_values[c] = NAN;
1636                             }
1637                             break;
1638
1639                         case GROUP_SUM:
1640                         case GROUP_INCREMENTAL_SUM:
1641                             cn[c] = group_values[c];
1642                             group_values[c] = 0;
1643                             break;
1644
1645                         default:
1646                         case GROUP_AVERAGE:
1647                         case GROUP_UNDEFINED:
1648                             cn[c] = group_values[c] / group_counts[c];
1649                             group_values[c] = 0;
1650                             break;
1651                     }
1652
1653                     if(cn[c] < r->min) r->min = cn[c];
1654                     if(cn[c] > r->max) r->max = cn[c];
1655                 }
1656
1657                 // reset for the next loop
1658                 group_counts[c] = 0;
1659                 group_options[c] = 0;
1660             }
1661
1662             added++;
1663             group_count = 0;
1664             add_this = 0;
1665         }
1666     }
1667
1668     rrdr_done(r);
1669     //info("RRD2RRDR(): %s: END %ld loops made, %ld points generated", st->id, counter, rrdr_rows(r));
1670     //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1671     return r;
1672 }
1673
1674 int rrdset2value_api_v1(
1675           RRDSET *st
1676         , BUFFER *wb
1677         , calculated_number *n
1678         , const char *dimensions
1679         , long points
1680         , long long after
1681         , long long before
1682         , int group_method
1683         , uint32_t options
1684         , time_t *db_after
1685         , time_t *db_before
1686         , int *value_is_null
1687 ) {
1688     RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1689     if(!r) {
1690         if(value_is_null) *value_is_null = 1;
1691         return 500;
1692     }
1693
1694     if(rrdr_rows(r) == 0) {
1695         rrdr_free(r);
1696
1697         if(db_after)  *db_after  = 0;
1698         if(db_before) *db_before = 0;
1699         if(value_is_null) *value_is_null = 1;
1700
1701         return 400;
1702     }
1703
1704     if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1705         buffer_no_cacheable(wb);
1706     else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1707         buffer_cacheable(wb);
1708
1709     options = rrdr_check_options(r, options, dimensions);
1710
1711     if(dimensions)
1712         rrdr_disable_not_selected_dimensions(r, options, dimensions);
1713
1714     if(db_after)  *db_after  = r->after;
1715     if(db_before) *db_before = r->before;
1716
1717     long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
1718     *n = rrdr2value(r, i, options, value_is_null);
1719
1720     rrdr_free(r);
1721     return 200;
1722 }
1723
1724 int rrdset2anything_api_v1(
1725           RRDSET *st
1726         , BUFFER *wb
1727         , BUFFER *dimensions
1728         , uint32_t format
1729         , long points
1730         , long long after
1731         , long long before
1732         , int group_method
1733         , uint32_t options
1734         , time_t *latest_timestamp
1735 ) {
1736     st->last_accessed_time = now_realtime_sec();
1737
1738     RRDR *r = rrd2rrdr(st, points, after, before, group_method, !(options & RRDR_OPTION_NOT_ALIGNED));
1739     if(!r) {
1740         buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
1741         return 500;
1742     }
1743
1744     if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
1745         buffer_no_cacheable(wb);
1746     else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
1747         buffer_cacheable(wb);
1748
1749     options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
1750
1751     if(dimensions)
1752         rrdr_disable_not_selected_dimensions(r, options, buffer_tostring(dimensions));
1753
1754     if(latest_timestamp && rrdr_rows(r) > 0)
1755         *latest_timestamp = r->before;
1756
1757     switch(format) {
1758     case DATASOURCE_SSV:
1759         if(options & RRDR_OPTION_JSON_WRAP) {
1760             wb->contenttype = CT_APPLICATION_JSON;
1761             rrdr_json_wrapper_begin(r, wb, format, options, 1);
1762             rrdr2ssv(r, wb, options, "", " ", "");
1763             rrdr_json_wrapper_end(r, wb, format, options, 1);
1764         }
1765         else {
1766             wb->contenttype = CT_TEXT_PLAIN;
1767             rrdr2ssv(r, wb, options, "", " ", "");
1768         }
1769         break;
1770
1771     case DATASOURCE_SSV_COMMA:
1772         if(options & RRDR_OPTION_JSON_WRAP) {
1773             wb->contenttype = CT_APPLICATION_JSON;
1774             rrdr_json_wrapper_begin(r, wb, format, options, 1);
1775             rrdr2ssv(r, wb, options, "", ",", "");
1776             rrdr_json_wrapper_end(r, wb, format, options, 1);
1777         }
1778         else {
1779             wb->contenttype = CT_TEXT_PLAIN;
1780             rrdr2ssv(r, wb, options, "", ",", "");
1781         }
1782         break;
1783
1784     case DATASOURCE_JS_ARRAY:
1785         if(options & RRDR_OPTION_JSON_WRAP) {
1786             wb->contenttype = CT_APPLICATION_JSON;
1787             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1788             rrdr2ssv(r, wb, options, "[", ",", "]");
1789             rrdr_json_wrapper_end(r, wb, format, options, 0);
1790         }
1791         else {
1792             wb->contenttype = CT_APPLICATION_JSON;
1793             rrdr2ssv(r, wb, options, "[", ",", "]");
1794         }
1795         break;
1796
1797     case DATASOURCE_CSV:
1798         if(options & RRDR_OPTION_JSON_WRAP) {
1799             wb->contenttype = CT_APPLICATION_JSON;
1800             rrdr_json_wrapper_begin(r, wb, format, options, 1);
1801             rrdr2csv(r, wb, options, "", ",", "\\n", "");
1802             rrdr_json_wrapper_end(r, wb, format, options, 1);
1803         }
1804         else {
1805             wb->contenttype = CT_TEXT_PLAIN;
1806             rrdr2csv(r, wb, options, "", ",", "\r\n", "");
1807         }
1808         break;
1809
1810     case DATASOURCE_CSV_JSON_ARRAY:
1811         wb->contenttype = CT_APPLICATION_JSON;
1812         if(options & RRDR_OPTION_JSON_WRAP) {
1813             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1814             buffer_strcat(wb, "[\n");
1815             rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1816             buffer_strcat(wb, "\n]");
1817             rrdr_json_wrapper_end(r, wb, format, options, 0);
1818         }
1819         else {
1820             wb->contenttype = CT_TEXT_PLAIN;
1821             buffer_strcat(wb, "[\n");
1822             rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
1823             buffer_strcat(wb, "\n]");
1824         }
1825         break;
1826
1827     case DATASOURCE_TSV:
1828         if(options & RRDR_OPTION_JSON_WRAP) {
1829             wb->contenttype = CT_APPLICATION_JSON;
1830             rrdr_json_wrapper_begin(r, wb, format, options, 1);
1831             rrdr2csv(r, wb, options, "", "\t", "\\n", "");
1832             rrdr_json_wrapper_end(r, wb, format, options, 1);
1833         }
1834         else {
1835             wb->contenttype = CT_TEXT_PLAIN;
1836             rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
1837         }
1838         break;
1839
1840     case DATASOURCE_HTML:
1841         if(options & RRDR_OPTION_JSON_WRAP) {
1842             wb->contenttype = CT_APPLICATION_JSON;
1843             rrdr_json_wrapper_begin(r, wb, format, options, 1);
1844             buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
1845             rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
1846             buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
1847             rrdr_json_wrapper_end(r, wb, format, options, 1);
1848         }
1849         else {
1850             wb->contenttype = CT_TEXT_HTML;
1851             buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
1852             rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
1853             buffer_strcat(wb, "</table>\n</center>\n</html>\n");
1854         }
1855         break;
1856
1857     case DATASOURCE_DATATABLE_JSONP:
1858         wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1859
1860         if(options & RRDR_OPTION_JSON_WRAP)
1861             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1862
1863         rrdr2json(r, wb, options, 1);
1864
1865         if(options & RRDR_OPTION_JSON_WRAP)
1866             rrdr_json_wrapper_end(r, wb, format, options, 0);
1867         break;
1868
1869     case DATASOURCE_DATATABLE_JSON:
1870         wb->contenttype = CT_APPLICATION_JSON;
1871
1872         if(options & RRDR_OPTION_JSON_WRAP)
1873             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1874
1875         rrdr2json(r, wb, options, 1);
1876
1877         if(options & RRDR_OPTION_JSON_WRAP)
1878             rrdr_json_wrapper_end(r, wb, format, options, 0);
1879         break;
1880
1881     case DATASOURCE_JSONP:
1882         wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
1883         if(options & RRDR_OPTION_JSON_WRAP)
1884             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1885
1886         rrdr2json(r, wb, options, 0);
1887
1888         if(options & RRDR_OPTION_JSON_WRAP)
1889             rrdr_json_wrapper_end(r, wb, format, options, 0);
1890         break;
1891
1892     case DATASOURCE_JSON:
1893     default:
1894         wb->contenttype = CT_APPLICATION_JSON;
1895
1896         if(options & RRDR_OPTION_JSON_WRAP)
1897             rrdr_json_wrapper_begin(r, wb, format, options, 0);
1898
1899         rrdr2json(r, wb, options, 0);
1900
1901         if(options & RRDR_OPTION_JSON_WRAP)
1902             rrdr_json_wrapper_end(r, wb, format, options, 0);
1903         break;
1904     }
1905
1906     rrdr_free(r);
1907     return 200;
1908 }