]> arthur.barton.de Git - netdata.git/blob - src/rrd2json.c
e2a88d9ef451e4cd9734482b14f60121e8ae69fb
[netdata.git] / src / rrd2json.c
1 #include <pthread.h>
2 #include <sys/time.h>
3 #include <stdlib.h>
4
5 #include "log.h"
6 #include "common.h"
7 #include "rrd2json.h"
8
9 #define HOSTNAME_MAX 1024
10 char *hostname = "unknown";
11
12 unsigned long rrd_stats_one_json(RRDSET *st, char *options, struct web_buffer *wb)
13 {
14         time_t now = time(NULL);
15         web_buffer_increase(wb, 65536);
16
17         pthread_rwlock_rdlock(&st->rwlock);
18
19         web_buffer_printf(wb,
20                 "\t\t{\n"
21                 "\t\t\t\"id\": \"%s\",\n"
22                 "\t\t\t\"name\": \"%s\",\n"
23                 "\t\t\t\"type\": \"%s\",\n"
24                 "\t\t\t\"family\": \"%s\",\n"
25                 "\t\t\t\"title\": \"%s\",\n"
26                 "\t\t\t\"priority\": %ld,\n"
27                 "\t\t\t\"enabled\": %d,\n"
28                 "\t\t\t\"units\": \"%s\",\n"
29                 "\t\t\t\"url\": \"/data/%s/%s\",\n"
30                 "\t\t\t\"chart_type\": \"%s\",\n"
31                 "\t\t\t\"counter\": %ld,\n"
32                 "\t\t\t\"entries\": %ld,\n"
33                 "\t\t\t\"first_entry_t\": %lu,\n"
34                 "\t\t\t\"last_entry\": %ld,\n"
35                 "\t\t\t\"last_entry_t\": %lu,\n"
36                 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
37                 "\t\t\t\"update_every\": %d,\n"
38                 "\t\t\t\"isdetail\": %d,\n"
39                 "\t\t\t\"usec_since_last_update\": %llu,\n"
40                 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
41                 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
42                 "\t\t\t\"dimensions\": [\n"
43                 , st->id
44                 , st->name
45                 , st->type
46                 , st->family
47                 , st->title
48                 , st->priority
49                 , st->enabled
50                 , st->units
51                 , st->name, options?options:""
52                 , rrdset_type_name(st->chart_type)
53                 , st->counter
54                 , st->entries
55                 , rrdset_first_entry_t(st)
56                 , rrdset_last_slot(st)
57                 , rrdset_last_entry_t(st)
58                 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
59                 , st->update_every
60                 , st->isdetail
61                 , st->usec_since_last_update
62                 , st->collected_total
63                 , st->last_collected_total
64                 );
65
66         unsigned long memory = st->memsize;
67
68         RRDDIM *rd;
69         for(rd = st->dimensions; rd ; rd = rd->next) {
70                 web_buffer_increase(wb, 4096);
71
72                 memory += rd->memsize;
73
74                 web_buffer_printf(wb,
75                         "\t\t\t\t{\n"
76                         "\t\t\t\t\t\"id\": \"%s\",\n"
77                         "\t\t\t\t\t\"name\": \"%s\",\n"
78                         "\t\t\t\t\t\"entries\": %ld,\n"
79                         "\t\t\t\t\t\"isHidden\": %d,\n"
80                         "\t\t\t\t\t\"algorithm\": \"%s\",\n"
81                         "\t\t\t\t\t\"multiplier\": %ld,\n"
82                         "\t\t\t\t\t\"divisor\": %ld,\n"
83                         "\t\t\t\t\t\"last_entry_t\": %lu,\n"
84                         "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
85                         "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
86                         "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
87                         "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
88                         "\t\t\t\t\t\"memory\": %lu\n"
89                         "\t\t\t\t}%s\n"
90                         , rd->id
91                         , rd->name
92                         , rd->entries
93                         , rd->hidden
94                         , rrddim_algorithm_name(rd->algorithm)
95                         , rd->multiplier
96                         , rd->divisor
97                         , rd->last_collected_time.tv_sec
98                         , rd->collected_value
99                         , rd->calculated_value
100                         , rd->last_collected_value
101                         , rd->last_calculated_value
102                         , rd->memsize
103                         , rd->next?",":""
104                         );
105         }
106
107         web_buffer_printf(wb,
108                 "\t\t\t],\n"
109                 "\t\t\t\"memory\" : %lu\n"
110                 "\t\t}"
111                 , memory
112                 );
113
114         pthread_rwlock_unlock(&st->rwlock);
115         return memory;
116 }
117
118 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
119 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
120
121 void rrd_stats_graph_json(RRDSET *st, char *options, struct web_buffer *wb)
122 {
123         web_buffer_increase(wb, 2048);
124
125         web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
126         rrd_stats_one_json(st, options, wb);
127         web_buffer_printf(wb, RRD_GRAPH_JSON_FOOTER);
128 }
129
130 void rrd_stats_all_json(struct web_buffer *wb)
131 {
132         web_buffer_increase(wb, 2048);
133
134         unsigned long memory = 0;
135         long c;
136         RRDSET *st;
137
138         web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
139
140         pthread_rwlock_rdlock(&rrdset_root_rwlock);
141         for(st = rrdset_root, c = 0; st ; st = st->next) {
142                 if(st->enabled) {
143                         if(c) web_buffer_printf(wb, "%s", ",\n");
144                         memory += rrd_stats_one_json(st, NULL, wb);
145                         c++;
146                 }
147         }
148         pthread_rwlock_unlock(&rrdset_root_rwlock);
149         
150         web_buffer_printf(wb, "\n\t],\n"
151                 "\t\"hostname\": \"%s\",\n"
152                 "\t\"update_every\": %d,\n"
153                 "\t\"history\": %d,\n"
154                 "\t\"memory\": %lu\n"
155                 "}\n"
156                 , hostname
157                 , rrd_update_every
158                 , rrd_default_history_entries
159                 , memory
160                 );
161 }
162
163
164
165 // ----------------------------------------------------------------------------
166
167 // RRDR options
168 #define RRDR_EMPTY      0x01
169 #define RRDR_RESET      0x02
170 #define RRDR_HIDDEN     0x04
171 #define RRDR_NONZERO    0x08
172
173
174 typedef struct rrdresult {
175         RRDSET *st;                     // the chart this result refers to
176
177         int d;                                  // the number of dimensions
178         int n;                                  // the number of values in the arrays
179
180         uint8_t *od;                    // the options for the dimensions
181
182         time_t *t;                              // array of n timestamps
183         calculated_number *v;   // array n x d values
184         uint8_t *o;                             // array n x d options
185
186         int c;                                  // current line ( 0 ~ n )
187
188         int has_st_lock;                // if st is read locked by us
189 } RRDR;
190
191 static void rrdr_dump(RRDR *r)
192 {
193         long c, i;
194         RRDDIM *d;
195
196         fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
197
198         for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
199                 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
200                                 , d->id
201                                 , d->name
202                                 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
203                                 , (r->od[c] & RRDR_RESET)?"RESET ":""
204                                 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
205                                 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
206                                 );
207         }
208
209         if(r->c < 0) {
210                 fprintf(stderr, "RRDR does not have any values in it.\n");
211                 return;
212         }
213
214         fprintf(stderr, "RRDR includes %d values in it:\n", r->c + 1);
215
216         // for each line in the array
217         for(i = 0; i <= r->c ;i++) {
218                 calculated_number *cn = &r->v[ i * r->d ];
219                 uint8_t *co = &r->o[ i * r->d ];
220
221                 // print the id and the timestamp of the line
222                 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
223
224                 // for each dimension
225                 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
226                         if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
227                         if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
228
229                         if(co[c] & RRDR_EMPTY)
230                                 fprintf(stderr, "null ");
231                         else
232                                 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
233                                         , cn[c]
234                                         , (co[c] & RRDR_EMPTY)?"E":" "
235                                         , (co[c] & RRDR_RESET)?"R":" "
236                                         , (co[c] & RRDR_HIDDEN)?"H":" "
237                                         , (co[c] & RRDR_NONZERO)?"N":" "
238                                         );
239                 }
240
241                 fprintf(stderr, "\n");
242         }
243 }
244
245 inline static calculated_number *rrdr_line_values(RRDR *r)
246 {
247         return &r->v[ r->c * r->d ];
248 }
249
250 inline static uint8_t *rrdr_line_options(RRDR *r)
251 {
252         return &r->o[ r->c * r->d ];
253 }
254
255 inline static int rrdr_line_init(RRDR *r, time_t t)
256 {
257         r->c++;
258         if(unlikely(r->c >= r->n)) {
259                 r->c = r->n - 1;
260                 return 0;
261         }
262
263         // save the time
264         r->t[r->c] = t;
265
266         return 1;
267 }
268
269 inline static void rrdr_lock_rrdset(RRDR *r) {
270         if(unlikely(!r)) {
271                 error("NULL value given!");
272                 return;
273         }
274
275         pthread_rwlock_rdlock(&r->st->rwlock);
276         r->has_st_lock = 1;
277 }
278
279 inline static void rrdr_unlock_rrdset(RRDR *r) {
280         if(unlikely(!r)) {
281                 error("NULL value given!");
282                 return;
283         }
284
285         if(likely(r->has_st_lock)) {
286                 pthread_rwlock_unlock(&r->st->rwlock);
287                 r->has_st_lock = 0;
288         }
289 }
290
291 inline static void rrdr_free(RRDR *r)
292 {
293         if(unlikely(!r)) {
294                 error("NULL value given!");
295                 return;
296         }
297
298         rrdr_unlock_rrdset(r);
299         if(likely(r->t)) free(r->t);
300         if(likely(r->v)) free(r->v);
301         if(likely(r->o)) free(r->o);
302         if(likely(r->od)) free(r->od);
303         free(r);
304 }
305
306 static RRDR *rrdr_create(RRDSET *st, int n)
307 {
308         if(unlikely(!st)) {
309                 error("NULL value given!");
310                 return NULL;
311         }
312
313         RRDR *r = calloc(1, sizeof(RRDR));
314         if(unlikely(!r)) goto cleanup;
315
316         r->st = st;
317
318         rrdr_lock_rrdset(r);
319
320         RRDDIM *rd;
321         for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
322
323         r->n = n;
324         r->t = malloc(n * sizeof(time_t));
325         if(unlikely(!r->t)) goto cleanup;
326
327         r->t = malloc(n * sizeof(time_t));
328         if(unlikely(!r->t)) goto cleanup;
329
330         r->v = malloc(n * r->d * sizeof(calculated_number));
331         if(unlikely(!r->v)) goto cleanup;
332
333         r->o = malloc(n * r->d * sizeof(uint8_t));
334         if(unlikely(!r->o)) goto cleanup;
335
336         r->od = calloc(r->d, sizeof(uint8_t));
337         if(unlikely(!r->od)) goto cleanup;
338
339         r->c = -1;
340
341         return r;
342
343 cleanup:
344         error("Cannot allocate memory");
345         if(likely(r)) rrdr_free(r);
346         return NULL;
347 }
348
349 RRDR *rrd2rrdr(RRDSET *st, long points, time_t after, time_t before, int group_method)
350 {
351         int debug = st->debug;
352
353         time_t first_entry_t = rrdset_first_entry_t(st);
354         time_t last_entry_t = rrdset_last_entry_t(st);
355
356         // allow relative for before and after
357         if(before <= st->update_every * st->entries) before = last_entry_t + before;
358         if(after <= st->update_every * st->entries) after = last_entry_t + after;
359
360         // make sure they are within our timeframe
361         if(before > last_entry_t) before = last_entry_t;
362         if(before < first_entry_t) before = first_entry_t;
363
364         if(after > last_entry_t) after = last_entry_t;
365         if(after < first_entry_t) after = first_entry_t;
366
367         // check if they are upside down
368         if(after > before) {
369                 time_t t = before;
370                 before = after;
371                 after = t;
372         }
373
374         // the duration of the chart
375         time_t duration = before - after;
376         if(duration <= 0) return NULL;
377
378         // check the required points
379         if(points <= 0) points = duration;
380
381         // calculate proper grouping of source data
382         long group = duration / points;
383         if(group <= 0) group = 1;
384         if(duration / group > points) group++;
385
386         // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
387
388         // Now we have:
389         // before = the end time of the calculation
390         // after = the start time of the calculation
391         // duration = the duration of the calculation
392         // group = the number of source points to aggregate / group together
393         // method = the method of grouping source points
394         // points = the number of points to generate
395
396
397         // -------------------------------------------------------------------------
398         // initialize our result set
399
400         RRDR *r = rrdr_create(st, points);
401         if(!r) return NULL;
402         if(!r->d) {
403                 rrdr_free(r);
404                 return NULL;
405         }
406
407         // find how many dimensions we have
408         long dimensions = r->d;
409
410
411         // -------------------------------------------------------------------------
412         // checks for debugging
413
414         if(debug) debug(D_RRD_STATS, "INFO %s first_t: %lu, last_t: %lu, all_duration: %lu, after: %lu, before: %lu, duration: %lu, points: %ld, group: %ld"
415                         , st->id
416                         , first_entry_t
417                         , last_entry_t
418                         , last_entry_t - first_entry_t
419                         , after
420                         , before
421                         , duration
422                         , points
423                         , group
424                         );
425
426
427         // -------------------------------------------------------------------------
428         // temp arrays for keeping values per dimension
429
430         calculated_number       group_values[dimensions]; // keep sums when grouping
431         long                            group_counts[dimensions]; // keep the number of values added to group_values
432         uint8_t                         group_options[dimensions];
433         uint8_t                         found_non_zero[dimensions];
434
435
436         // initialize them
437         RRDDIM *rd;
438         long c;
439         for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
440                 group_values[c] = 0;
441                 group_counts[c] = 0;
442                 group_options[c] = 0;
443                 found_non_zero[c] = 0;
444         }
445
446
447         // -------------------------------------------------------------------------
448         // the main loop
449
450         long    t = rrdset_time2slot(st, before), // rrdset_last_slot(st),
451                         stop_at_t = rrdset_time2slot(st, after),
452                         added = 0,
453                         group_count = 0,
454                         add_this = 0,
455                         stop_now = 0;
456
457         // align to group for proper panning of data
458         t -= t % group;
459
460         time_t  now = rrdset_slot2time(st, t),
461                         dt = st->update_every,
462                         group_start_t = 0;
463
464         if(debug) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop slot %ld), before_t: %lu (start slot %ld), start_t(now): %lu"
465                         , st->id
466                         , after
467                         , stop_at_t
468                         , before
469                         , t
470                         , now
471                         );
472
473         for( ; !stop_now ; now -= dt, t--) {
474                 if(unlikely(t < 0)) c = st->entries - 1;
475                 if(t == stop_at_t) stop_now = 1;
476
477                 if(debug) debug(D_RRD_STATS, "ROW %s c: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
478                                 , st->id
479                                 , t
480                                 , group_count + 1
481                                 , added
482                                 , now
483                                 , (group_count + 1 == group)?"PRINT":"  -  "
484                                 , (now >= after && now <= before)?"RANGE":"  -  "
485                                 );
486
487                 // make sure we return data in the proper time range
488                 if(unlikely(now > before)) continue;
489                 if(unlikely(now < after)) break;
490
491                 if(group_count == 0) group_start_t = now;
492                 group_count++;
493
494                 if(unlikely(group_count == group)) {
495                         if(unlikely(added >= points)) break;
496                         add_this = 1;
497                 }
498
499                 // do the calculations
500                 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
501                         storage_number n = rd->values[t];
502                         if(unlikely(!does_storage_number_exist(n))) continue;
503
504                         group_counts[c]++;
505
506                         calculated_number value = unpack_storage_number(n);
507                         if(value != 0.0) {
508                                 group_options[c] |= RRDR_NONZERO;
509                                 found_non_zero[c] = 1;
510                         }
511
512                         if(unlikely(did_storage_number_reset(n)))
513                                 group_options[c] |= RRDR_RESET;
514
515                         switch(group_method) {
516                                 case GROUP_MAX:
517                                         if(unlikely(abs(value) > abs(group_values[c])))
518                                                 group_values[c] = value;
519                                         break;
520
521                                 default:
522                                 case GROUP_SUM:
523                                 case GROUP_AVERAGE:
524                                         group_values[c] += value;
525                                         break;
526                         }
527                 }
528
529                 // added it
530                 if(unlikely(add_this)) {
531                         if(!rrdr_line_init(r, group_start_t)) break;
532
533                         calculated_number *cn = rrdr_line_values(r);
534                         uint8_t *co = rrdr_line_options(r);
535
536                         for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
537
538                                 // update the dimension options
539                                 if(found_non_zero[c]) r->od[c] |= RRDR_NONZERO;
540                                 if(rd->hidden) r->od[c] |= RRDR_HIDDEN;
541
542                                 // store the specific point options
543                                 co[c] = group_options[c];
544
545                                 // store the value
546                                 if(group_counts[c] == 0) {
547                                         cn[c] = 0.0;
548                                         co[c] |= RRDR_EMPTY;
549                                 }
550                                 else if(unlikely(group_method == GROUP_AVERAGE)) {
551                                         cn[c] = group_values[c] / group_counts[c];
552                                 }
553                                 else {
554                                         cn[c] = group_values[c];
555                                 }
556
557                                 // reset them for the next loop
558                                 group_values[c] = 0;
559                                 group_counts[c] = 0;
560                                 group_options[c] = 0;
561                         }
562
563                         added++;
564                         group_count = 0;
565                         add_this = 0;
566                 }
567         }
568
569         rrdr_dump(r);
570         rrdr_free(r);
571         return NULL;
572 }
573
574 unsigned long rrd_stats_json(int type, RRDSET *st, struct web_buffer *wb, int points, int group, int group_method, time_t after, time_t before, int only_non_zero)
575 {
576         int c;
577         pthread_rwlock_rdlock(&st->rwlock);
578
579
580         // -------------------------------------------------------------------------
581         // switch from JSON to google JSON
582
583         char kq[2] = "\"";
584         char sq[2] = "\"";
585         switch(type) {
586                 case DATASOURCE_GOOGLE_JSON:
587                 case DATASOURCE_GOOGLE_JSONP:
588                         kq[0] = '\0';
589                         sq[0] = '\'';
590                         break;
591
592                 case DATASOURCE_JSON:
593                 default:
594                         break;
595         }
596
597
598         // -------------------------------------------------------------------------
599         // validate the parameters
600
601         if(points < 1) points = 1;
602         if(group < 1) group = 1;
603
604         if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
605         if(after  == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
606
607         // ---
608
609         // our return value (the last timestamp printed)
610         // this is required to detect re-transmit in google JSONP
611         time_t last_timestamp = 0;
612
613
614         // -------------------------------------------------------------------------
615         // find how many dimensions we have
616
617         int dimensions = 0;
618         RRDDIM *rd;
619         for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
620         if(!dimensions) {
621                 pthread_rwlock_unlock(&st->rwlock);
622                 web_buffer_printf(wb, "No dimensions yet.");
623                 return 0;
624         }
625
626
627         // -------------------------------------------------------------------------
628         // prepare various strings, to speed up the loop
629
630         char overflow_annotation[201]; snprintf(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);
631         char normal_annotation[201];   snprintf(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
632         char pre_date[51];             snprintf(pre_date,             50, "             {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
633         char post_date[21];            snprintf(post_date,            20, "%s}", sq);
634         char pre_value[21];            snprintf(pre_value,            20, ",{%sv%s:", kq, kq);
635         char post_value[21];           snprintf(post_value,           20, "}");
636
637
638         // -------------------------------------------------------------------------
639         // checks for debugging
640
641         if(st->debug) {
642                 debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu"
643                         , st->id
644                         , rrdset_first_entry_t(st)
645                         , rrdset_last_entry_t(st)
646                         , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
647                         , after
648                         , before
649                         , before - after
650                         , points
651                         , group
652                         );
653
654                 if(before < after)
655                         debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
656
657                 if((before - after) > st->entries * st->update_every)
658                         debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
659         }
660
661
662         // -------------------------------------------------------------------------
663         // temp arrays for keeping values per dimension
664
665         calculated_number group_values[dimensions]; // keep sums when grouping
666         int               print_hidden[dimensions]; // keep hidden flags
667         int               found_non_zero[dimensions];
668         int               found_non_existing[dimensions];
669
670         // initialize them
671         for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
672                 group_values[c] = 0;
673                 print_hidden[c] = rd->hidden;
674                 found_non_zero[c] = 0;
675                 found_non_existing[c] = 0;
676         }
677
678
679         // error("OLD: points=%d after=%d before=%d group=%d, duration=%d", entries_to_show, before - (st->update_every * group * entries_to_show), before, group, before - after + 1);
680         // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
681         // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
682
683         // -------------------------------------------------------------------------
684         // remove dimensions that contain only zeros
685
686         int max_loop = 1;
687         if(only_non_zero) max_loop = 2;
688
689         for(; max_loop ; max_loop--) {
690
691                 // -------------------------------------------------------------------------
692                 // print the JSON header
693
694                 web_buffer_printf(wb, "{\n      %scols%s:\n     [\n", kq, kq);
695                 web_buffer_printf(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);
696                 web_buffer_printf(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);
697                 web_buffer_printf(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);
698
699                 // print the header for each dimension
700                 // and update the print_hidden array for the dimensions that should be hidden
701                 int pc = 0;
702                 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
703                         if(!print_hidden[c]) {
704                                 pc++;
705                                 web_buffer_printf(wb, ",\n              {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
706                         }
707                 }
708                 if(!pc) {
709                         web_buffer_printf(wb, ",\n              {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, "no data", sq, kq, kq, sq, sq, kq, kq, sq, sq);
710                 }
711
712                 // print the begin of row data
713                 web_buffer_printf(wb, "\n       ],\n    %srows%s:\n     [\n", kq, kq);
714
715
716                 // -------------------------------------------------------------------------
717                 // the main loop
718
719                 int annotate_reset = 0;
720                 int annotation_count = 0;
721
722                 // the minimum line length we expect
723                 int line_size = 4096 + (dimensions * 200);
724
725                 long    t = rrdset_time2slot(st, before),
726                                 stop_at_t = rrdset_time2slot(st, after),
727                                 stop_now = 0;
728
729                 t -= t % group;
730
731                 time_t  now = rrdset_slot2time(st, t),
732                                 dt = st->update_every;
733
734                 long count = 0, printed = 0, group_count = 0;
735                 last_timestamp = 0;
736
737                 if(st->debug) debug(D_RRD_STATS, "%s: REQUEST after:%lu before:%lu, points:%d, group:%d, CHART cur:%ld first: %lu last:%lu, CALC start_t:%ld, stop_t:%ld"
738                                         , st->id
739                                         , after
740                                         , before
741                                         , points
742                                         , group
743                                         , st->current_entry
744                                         , rrdset_first_entry_t(st)
745                                         , rrdset_last_entry_t(st)
746                                         , t
747                                         , stop_at_t
748                                         );
749
750                 for(; !stop_now ; now -= dt, t--) {
751                         if(t < 0) t = st->entries - 1;
752                         if(t == stop_at_t) stop_now = 1;
753
754                         int print_this = 0;
755
756                         if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
757                                         , st->id
758                                         , t
759                                         , count + 1
760                                         , group_count + 1
761                                         , printed
762                                         , now
763                                         , (group_count + 1 == group)?"PRINT":"  -  "
764                                         , (now >= after && now <= before)?"RANGE":"  -  "
765                                         );
766
767
768                         // make sure we return data in the proper time range
769                         if(now > before) continue;
770                         if(now < after) break;
771
772                         //if(rrdset_slot2time(st, t) != now)
773                         //      error("%s: slot=%ld, now=%ld, slot2time=%ld, diff=%ld, last_entry_t=%ld, rrdset_last_slot=%ld", st->id, t, now, rrdset_slot2time(st,t), now - rrdset_slot2time(st,t), rrdset_last_entry_t(st), rrdset_last_slot(st));
774
775                         count++;
776                         group_count++;
777
778                         // check if we have to print this now
779                         if(group_count == group) {
780                                 if(printed >= points) {
781                                         // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
782                                         break;
783                                 }
784
785                                 // check if we may exceed the buffer provided
786                                 web_buffer_increase(wb, line_size);
787
788                                 // generate the local date time
789                                 struct tm *tm = localtime(&now);
790                                 if(!tm) { error("localtime() failed."); continue; }
791                                 if(now > last_timestamp) last_timestamp = now;
792
793                                 if(printed) web_buffer_strcpy(wb, "]},\n");
794                                 web_buffer_strcpy(wb, pre_date);
795                                 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
796                                 web_buffer_strcpy(wb, post_date);
797
798                                 print_this = 1;
799                         }
800
801                         // do the calculations
802                         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
803                                 storage_number n = rd->values[t];
804                                 calculated_number value = unpack_storage_number(n);
805
806                                 if(!does_storage_number_exist(n)) {
807                                         value = 0.0;
808                                         found_non_existing[c]++;
809                                 }
810                                 if(did_storage_number_reset(n)) annotate_reset = 1;
811
812                                 switch(group_method) {
813                                         case GROUP_MAX:
814                                                 if(abs(value) > abs(group_values[c])) group_values[c] = value;
815                                                 break;
816
817                                         case GROUP_SUM:
818                                                 group_values[c] += value;
819                                                 break;
820
821                                         default:
822                                         case GROUP_AVERAGE:
823                                                 group_values[c] += value;
824                                                 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
825                                                 break;
826                                 }
827                         }
828
829                         if(print_this) {
830                                 if(annotate_reset) {
831                                         annotation_count++;
832                                         web_buffer_strcpy(wb, overflow_annotation);
833                                         annotate_reset = 0;
834                                 }
835                                 else
836                                         web_buffer_strcpy(wb, normal_annotation);
837
838                                 pc = 0;
839                                 for(c = 0 ; c < dimensions ; c++) {
840                                         if(found_non_existing[c] == group_count) {
841                                                 // all entries are non-existing
842                                                 pc++;
843                                                 web_buffer_strcpy(wb, pre_value);
844                                                 web_buffer_strcpy(wb, "null");
845                                                 web_buffer_strcpy(wb, post_value);
846                                         }
847                                         else if(!print_hidden[c]) {
848                                                 pc++;
849                                                 web_buffer_strcpy(wb, pre_value);
850                                                 web_buffer_rrd_value(wb, group_values[c]);
851                                                 web_buffer_strcpy(wb, post_value);
852
853                                                 if(group_values[c]) found_non_zero[c]++;
854                                         }
855
856                                         // reset them for the next loop
857                                         group_values[c] = 0;
858                                         found_non_existing[c] = 0;
859                                 }
860
861                                 // if all dimensions are hidden, print a null
862                                 if(!pc) {
863                                         web_buffer_strcpy(wb, pre_value);
864                                         web_buffer_strcpy(wb, "null");
865                                         web_buffer_strcpy(wb, post_value);
866                                 }
867
868                                 printed++;
869                                 group_count = 0;
870                         }
871                 }
872
873                 if(printed) web_buffer_printf(wb, "]}");
874                 web_buffer_printf(wb, "\n       ]\n}\n");
875
876                 if(only_non_zero && max_loop > 1) {
877                         int changed = 0;
878                         for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
879                                 group_values[c] = 0;
880                                 found_non_existing[c] = 0;
881
882                                 if(!print_hidden[c] && !found_non_zero[c]) {
883                                         changed = 1;
884                                         print_hidden[c] = 1;
885                                 }
886                         }
887
888                         if(changed) web_buffer_reset(wb);
889                         else break;
890                 }
891                 else break;
892
893         } // max_loop
894
895         debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
896
897         pthread_rwlock_unlock(&st->rwlock);
898         return last_timestamp;
899 }