12 #define HOSTNAME_MAX 1024
13 char *hostname = "unknown";
15 void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
17 time_t now = time(NULL);
19 pthread_rwlock_rdlock(&st->rwlock);
23 "\t\t\t\"id\": \"%s\",\n"
24 "\t\t\t\"name\": \"%s\",\n"
25 "\t\t\t\"type\": \"%s\",\n"
26 "\t\t\t\"family\": \"%s\",\n"
27 "\t\t\t\"title\": \"%s\",\n"
28 "\t\t\t\"priority\": %ld,\n"
29 "\t\t\t\"enabled\": %s,\n"
30 "\t\t\t\"units\": \"%s\",\n"
31 "\t\t\t\"data_url\": \"/api/v1/data?chart=%s\",\n"
32 "\t\t\t\"chart_type\": \"%s\",\n"
33 "\t\t\t\"duration\": %ld,\n"
34 "\t\t\t\"first_entry\": %lu,\n"
35 "\t\t\t\"last_entry\": %lu,\n"
36 "\t\t\t\"update_every\": %d,\n"
37 "\t\t\t\"dimensions\": {\n"
44 , st->enabled?"true":"false"
47 , rrdset_type_name(st->chart_type)
48 , st->entries * st->update_every
49 , rrdset_first_entry_t(st)
50 , rrdset_last_entry_t(st)
54 unsigned long memory = st->memsize;
58 for(rd = st->dimensions; rd ; rd = rd->next) {
59 if(rd->flags & RRDDIM_FLAG_HIDDEN) continue;
61 memory += rd->memsize;
65 "\t\t\t\t\"%s\": { \"name\": \"%s\" }"
79 pthread_rwlock_unlock(&st->rwlock);
82 void rrd_stats_api_v1_charts(BUFFER *wb)
87 buffer_sprintf(wb, "{\n"
88 "\t\"hostname\": \"%s\""
89 ",\n\t\"update_every\": %d"
90 ",\n\t\"history\": %d"
94 , rrd_default_history_entries
97 pthread_rwlock_rdlock(&rrdset_root_rwlock);
98 for(st = rrdset_root, c = 0; st ; st = st->next) {
100 if(c) buffer_strcat(wb, ",");
101 buffer_strcat(wb, "\n\t\t\"");
102 buffer_strcat(wb, st->id);
103 buffer_strcat(wb, "\": ");
104 rrd_stats_api_v1_chart(st, wb);
108 pthread_rwlock_unlock(&rrdset_root_rwlock);
110 buffer_strcat(wb, "\n\t}\n}\n");
114 unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
116 time_t now = time(NULL);
118 pthread_rwlock_rdlock(&st->rwlock);
122 "\t\t\t\"id\": \"%s\",\n"
123 "\t\t\t\"name\": \"%s\",\n"
124 "\t\t\t\"type\": \"%s\",\n"
125 "\t\t\t\"family\": \"%s\",\n"
126 "\t\t\t\"title\": \"%s\",\n"
127 "\t\t\t\"priority\": %ld,\n"
128 "\t\t\t\"enabled\": %d,\n"
129 "\t\t\t\"units\": \"%s\",\n"
130 "\t\t\t\"url\": \"/data/%s/%s\",\n"
131 "\t\t\t\"chart_type\": \"%s\",\n"
132 "\t\t\t\"counter\": %ld,\n"
133 "\t\t\t\"entries\": %ld,\n"
134 "\t\t\t\"first_entry_t\": %lu,\n"
135 "\t\t\t\"last_entry\": %ld,\n"
136 "\t\t\t\"last_entry_t\": %lu,\n"
137 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
138 "\t\t\t\"update_every\": %d,\n"
139 "\t\t\t\"isdetail\": %d,\n"
140 "\t\t\t\"usec_since_last_update\": %llu,\n"
141 "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
142 "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n"
143 "\t\t\t\"dimensions\": [\n"
152 , st->name, options?options:""
153 , rrdset_type_name(st->chart_type)
156 , rrdset_first_entry_t(st)
157 , rrdset_last_slot(st)
158 , rrdset_last_entry_t(st)
159 , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st)
162 , st->usec_since_last_update
163 , st->collected_total
164 , st->last_collected_total
167 unsigned long memory = st->memsize;
170 for(rd = st->dimensions; rd ; rd = rd->next) {
172 memory += rd->memsize;
176 "\t\t\t\t\t\"id\": \"%s\",\n"
177 "\t\t\t\t\t\"name\": \"%s\",\n"
178 "\t\t\t\t\t\"entries\": %ld,\n"
179 "\t\t\t\t\t\"isHidden\": %d,\n"
180 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
181 "\t\t\t\t\t\"multiplier\": %ld,\n"
182 "\t\t\t\t\t\"divisor\": %ld,\n"
183 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
184 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
185 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
186 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
187 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
188 "\t\t\t\t\t\"memory\": %lu\n"
193 , (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0
194 , rrddim_algorithm_name(rd->algorithm)
197 , rd->last_collected_time.tv_sec
198 , rd->collected_value
199 , rd->calculated_value
200 , rd->last_collected_value
201 , rd->last_calculated_value
209 "\t\t\t\"memory\" : %lu\n"
214 pthread_rwlock_unlock(&st->rwlock);
218 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
219 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
221 void rrd_stats_graph_json(RRDSET *st, char *options, BUFFER *wb)
223 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
224 rrd_stats_one_json(st, options, wb);
225 buffer_strcat(wb, RRD_GRAPH_JSON_FOOTER);
228 void rrd_stats_all_json(BUFFER *wb)
230 unsigned long memory = 0;
234 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
236 pthread_rwlock_rdlock(&rrdset_root_rwlock);
237 for(st = rrdset_root, c = 0; st ; st = st->next) {
239 if(c) buffer_strcat(wb, ",\n");
240 memory += rrd_stats_one_json(st, NULL, wb);
244 pthread_rwlock_unlock(&rrdset_root_rwlock);
246 buffer_sprintf(wb, "\n\t],\n"
247 "\t\"hostname\": \"%s\",\n"
248 "\t\"update_every\": %d,\n"
249 "\t\"history\": %d,\n"
250 "\t\"memory\": %lu\n"
254 , rrd_default_history_entries
261 // ----------------------------------------------------------------------------
264 #define RRDR_EMPTY 0x01
265 #define RRDR_RESET 0x02
266 #define RRDR_HIDDEN 0x04
267 #define RRDR_NONZERO 0x08
270 typedef struct rrdresult {
271 RRDSET *st; // the chart this result refers to
273 int d; // the number of dimensions
274 int n; // the number of values in the arrays
276 uint8_t *od; // the options for the dimensions
278 time_t *t; // array of n timestamps
279 calculated_number *v; // array n x d values
280 uint8_t *o; // array n x d options
282 int c; // current line ( 0 ~ n )
284 int has_st_lock; // if st is read locked by us
287 static void rrdr_dump(RRDR *r)
292 fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
294 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
295 fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
298 , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
299 , (r->od[c] & RRDR_RESET)?"RESET ":""
300 , (r->od[c] & RRDR_HIDDEN)?"HIDDEN ":""
301 , (r->od[c] & RRDR_NONZERO)?"NONZERO ":""
306 fprintf(stderr, "RRDR does not have any values in it.\n");
310 fprintf(stderr, "RRDR includes %d values in it:\n", r->c + 1);
312 // for each line in the array
313 for(i = 0; i <= r->c ;i++) {
314 calculated_number *cn = &r->v[ i * r->d ];
315 uint8_t *co = &r->o[ i * r->d ];
317 // print the id and the timestamp of the line
318 fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
320 // for each dimension
321 for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
322 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
323 if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
325 if(co[c] & RRDR_EMPTY)
326 fprintf(stderr, "null ");
328 fprintf(stderr, CALCULATED_NUMBER_FORMAT " %s%s%s%s "
330 , (co[c] & RRDR_EMPTY)?"E":" "
331 , (co[c] & RRDR_RESET)?"R":" "
332 , (co[c] & RRDR_HIDDEN)?"H":" "
333 , (co[c] & RRDR_NONZERO)?"N":" "
337 fprintf(stderr, "\n");
341 inline static calculated_number *rrdr_line_values(RRDR *r)
343 return &r->v[ r->c * r->d ];
346 inline static uint8_t *rrdr_line_options(RRDR *r)
348 return &r->o[ r->c * r->d ];
351 inline static int rrdr_line_init(RRDR *r, time_t t)
354 if(unlikely(r->c >= r->n)) {
365 inline static void rrdr_lock_rrdset(RRDR *r) {
367 error("NULL value given!");
371 pthread_rwlock_rdlock(&r->st->rwlock);
375 inline static void rrdr_unlock_rrdset(RRDR *r) {
377 error("NULL value given!");
381 if(likely(r->has_st_lock)) {
382 pthread_rwlock_unlock(&r->st->rwlock);
387 inline static void rrdr_free(RRDR *r)
390 error("NULL value given!");
394 rrdr_unlock_rrdset(r);
395 if(likely(r->t)) free(r->t);
396 if(likely(r->v)) free(r->v);
397 if(likely(r->o)) free(r->o);
398 if(likely(r->od)) free(r->od);
402 static RRDR *rrdr_create(RRDSET *st, int n)
405 error("NULL value given!");
409 RRDR *r = calloc(1, sizeof(RRDR));
410 if(unlikely(!r)) goto cleanup;
417 for(rd = st->dimensions ; rd ; rd = rd->next) r->d++;
420 r->t = malloc(n * sizeof(time_t));
421 if(unlikely(!r->t)) goto cleanup;
423 r->t = malloc(n * sizeof(time_t));
424 if(unlikely(!r->t)) goto cleanup;
426 r->v = malloc(n * r->d * sizeof(calculated_number));
427 if(unlikely(!r->v)) goto cleanup;
429 r->o = malloc(n * r->d * sizeof(uint8_t));
430 if(unlikely(!r->o)) goto cleanup;
432 r->od = calloc(r->d, sizeof(uint8_t));
433 if(unlikely(!r->od)) goto cleanup;
440 error("Cannot allocate memory");
441 if(likely(r)) rrdr_free(r);
445 RRDR *rrd2rrdr(RRDSET *st, long points, time_t after, time_t before, int group_method)
447 int debug = st->debug;
449 time_t first_entry_t = rrdset_first_entry_t(st);
450 time_t last_entry_t = rrdset_last_entry_t(st);
452 // allow relative for before and after
453 if(before <= st->update_every * st->entries) before = last_entry_t + before;
454 if(after <= st->update_every * st->entries) after = last_entry_t + after;
456 // make sure they are within our timeframe
457 if(before > last_entry_t) before = last_entry_t;
458 if(before < first_entry_t) before = first_entry_t;
460 if(after > last_entry_t) after = last_entry_t;
461 if(after < first_entry_t) after = first_entry_t;
463 // check if they are upside down
470 // the duration of the chart
471 time_t duration = before - after;
472 if(duration <= 0) return NULL;
474 // check the required points
475 if(points > duration / st->update_every) points = 0;
476 if(points <= 0) points = duration / st->update_every;
478 // calculate proper grouping of source data
479 long group = duration / points;
480 if(group <= 0) group = 1;
481 if(duration / group > points) group++;
483 // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
486 // before = the end time of the calculation
487 // after = the start time of the calculation
488 // duration = the duration of the calculation
489 // group = the number of source points to aggregate / group together
490 // method = the method of grouping source points
491 // points = the number of points to generate
494 // -------------------------------------------------------------------------
495 // initialize our result set
497 RRDR *r = rrdr_create(st, points);
504 // find how many dimensions we have
505 long dimensions = r->d;
508 // -------------------------------------------------------------------------
509 // checks for debugging
511 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"
515 , last_entry_t - first_entry_t
524 // -------------------------------------------------------------------------
525 // temp arrays for keeping values per dimension
527 calculated_number group_values[dimensions]; // keep sums when grouping
528 long group_counts[dimensions]; // keep the number of values added to group_values
529 uint8_t group_options[dimensions];
530 uint8_t found_non_zero[dimensions];
536 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
539 group_options[c] = 0;
540 found_non_zero[c] = 0;
544 // -------------------------------------------------------------------------
547 long t = rrdset_time2slot(st, before), // rrdset_last_slot(st),
548 stop_at_t = rrdset_time2slot(st, after),
554 // align to group for proper panning of data
557 time_t now = rrdset_slot2time(st, t),
558 dt = st->update_every,
561 if(debug) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop slot %ld), before_t: %lu (start slot %ld), start_t(now): %lu"
570 for( ; !stop_now ; now -= dt, t--) {
571 if(unlikely(t < 0)) c = st->entries - 1;
572 if(t == stop_at_t) stop_now = 1;
574 if(debug) debug(D_RRD_STATS, "ROW %s c: %ld, group_count: %ld, added: %ld, now: %lu, %s %s"
580 , (group_count + 1 == group)?"PRINT":" - "
581 , (now >= after && now <= before)?"RANGE":" - "
584 // make sure we return data in the proper time range
585 if(unlikely(now > before)) continue;
586 if(unlikely(now < after)) break;
588 if(group_count == 0) group_start_t = now;
591 if(unlikely(group_count == group)) {
592 if(unlikely(added >= points)) break;
596 // do the calculations
597 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
598 storage_number n = rd->values[t];
599 if(unlikely(!does_storage_number_exist(n))) continue;
603 calculated_number value = unpack_storage_number(n);
605 group_options[c] |= RRDR_NONZERO;
606 found_non_zero[c] = 1;
609 if(unlikely(did_storage_number_reset(n)))
610 group_options[c] |= RRDR_RESET;
612 switch(group_method) {
614 if(unlikely(abs(value) > abs(group_values[c])))
615 group_values[c] = value;
621 group_values[c] += value;
627 if(unlikely(add_this)) {
628 if(!rrdr_line_init(r, group_start_t)) break;
630 calculated_number *cn = rrdr_line_values(r);
631 uint8_t *co = rrdr_line_options(r);
633 for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
635 // update the dimension options
636 if(found_non_zero[c]) r->od[c] |= RRDR_NONZERO;
637 if(rd->flags & RRDDIM_FLAG_HIDDEN) r->od[c] |= RRDR_HIDDEN;
639 // store the specific point options
640 co[c] = group_options[c];
643 if(group_counts[c] == 0) {
647 else if(unlikely(group_method == GROUP_AVERAGE)) {
648 cn[c] = group_values[c] / group_counts[c];
651 cn[c] = group_values[c];
654 // reset them for the next loop
657 group_options[c] = 0;
671 unsigned long rrd_stats_json(int type, RRDSET *st, BUFFER *wb, int points, int group, int group_method, time_t after, time_t before, int only_non_zero)
674 pthread_rwlock_rdlock(&st->rwlock);
677 // -------------------------------------------------------------------------
678 // switch from JSON to google JSON
683 case DATASOURCE_GOOGLE_JSON:
684 case DATASOURCE_GOOGLE_JSONP:
689 case DATASOURCE_JSON:
695 // -------------------------------------------------------------------------
696 // validate the parameters
698 if(points < 1) points = 1;
699 if(group < 1) group = 1;
701 if(before == 0 || before > rrdset_last_entry_t(st)) before = rrdset_last_entry_t(st);
702 if(after == 0 || after < rrdset_first_entry_t(st)) after = rrdset_first_entry_t(st);
706 // our return value (the last timestamp printed)
707 // this is required to detect re-transmit in google JSONP
708 time_t last_timestamp = 0;
711 // -------------------------------------------------------------------------
712 // find how many dimensions we have
716 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
718 pthread_rwlock_unlock(&st->rwlock);
719 buffer_strcat(wb, "No dimensions yet.");
724 // -------------------------------------------------------------------------
725 // prepare various strings, to speed up the loop
727 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);
728 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
729 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
730 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
731 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
732 char post_value[21]; snprintf(post_value, 20, "}");
735 // -------------------------------------------------------------------------
736 // checks for debugging
739 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"
741 , rrdset_first_entry_t(st)
742 , rrdset_last_entry_t(st)
743 , rrdset_last_entry_t(st) - rrdset_first_entry_t(st)
752 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
754 if((before - after) > st->entries * st->update_every)
755 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);
759 // -------------------------------------------------------------------------
760 // temp arrays for keeping values per dimension
762 calculated_number group_values[dimensions]; // keep sums when grouping
763 int print_hidden[dimensions]; // keep hidden flags
764 int found_non_zero[dimensions];
765 int found_non_existing[dimensions];
768 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
770 print_hidden[c] = (rd->flags & RRDDIM_FLAG_HIDDEN)?1:0;
771 found_non_zero[c] = 0;
772 found_non_existing[c] = 0;
776 // 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);
777 // rrd2array(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method, only_non_zero);
778 // rrd2rrdr(st, entries_to_show, before - (st->update_every * group * entries_to_show), before, group_method);
780 // -------------------------------------------------------------------------
781 // remove dimensions that contain only zeros
784 if(only_non_zero) max_loop = 2;
786 for(; max_loop ; max_loop--) {
788 // -------------------------------------------------------------------------
789 // print the JSON header
791 buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
792 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);
793 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);
794 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);
796 // print the header for each dimension
797 // and update the print_hidden array for the dimensions that should be hidden
799 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
800 if(!print_hidden[c]) {
802 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
806 buffer_sprintf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, "no data", sq, kq, kq, sq, sq, kq, kq, sq, sq);
809 // print the begin of row data
810 buffer_sprintf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
813 // -------------------------------------------------------------------------
816 int annotate_reset = 0;
817 int annotation_count = 0;
819 // the minimum line length we expect
820 int line_size = 4096 + (dimensions * 200);
822 long t = rrdset_time2slot(st, before),
823 stop_at_t = rrdset_time2slot(st, after),
828 time_t now = rrdset_slot2time(st, t),
829 dt = st->update_every;
831 long count = 0, printed = 0, group_count = 0;
834 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"
841 , rrdset_first_entry_t(st)
842 , rrdset_last_entry_t(st)
847 for(; !stop_now ; now -= dt, t--) {
848 if(t < 0) t = st->entries - 1;
849 if(t == stop_at_t) stop_now = 1;
853 if(st->debug) debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
860 , (group_count + 1 == group)?"PRINT":" - "
861 , (now >= after && now <= before)?"RANGE":" - "
865 // make sure we return data in the proper time range
866 if(now > before) continue;
867 if(now < after) break;
869 //if(rrdset_slot2time(st, t) != now)
870 // 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));
875 // check if we have to print this now
876 if(group_count == group) {
877 if(printed >= points) {
878 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
882 // generate the local date time
883 struct tm *tm = localtime(&now);
884 if(!tm) { error("localtime() failed."); continue; }
885 if(now > last_timestamp) last_timestamp = now;
887 if(printed) buffer_strcat(wb, "]},\n");
888 buffer_strcat(wb, pre_date);
889 buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
890 buffer_strcat(wb, post_date);
895 // do the calculations
896 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
897 storage_number n = rd->values[t];
898 calculated_number value = unpack_storage_number(n);
900 if(!does_storage_number_exist(n)) {
902 found_non_existing[c]++;
904 if(did_storage_number_reset(n)) annotate_reset = 1;
906 switch(group_method) {
908 if(abs(value) > abs(group_values[c])) group_values[c] = value;
912 group_values[c] += value;
917 group_values[c] += value;
918 if(print_this) group_values[c] /= ( group_count - found_non_existing[c] );
926 buffer_strcat(wb, overflow_annotation);
930 buffer_strcat(wb, normal_annotation);
933 for(c = 0 ; c < dimensions ; c++) {
934 if(found_non_existing[c] == group_count) {
935 // all entries are non-existing
937 buffer_strcat(wb, pre_value);
938 buffer_strcat(wb, "null");
939 buffer_strcat(wb, post_value);
941 else if(!print_hidden[c]) {
943 buffer_strcat(wb, pre_value);
944 buffer_rrd_value(wb, group_values[c]);
945 buffer_strcat(wb, post_value);
947 if(group_values[c]) found_non_zero[c]++;
950 // reset them for the next loop
952 found_non_existing[c] = 0;
955 // if all dimensions are hidden, print a null
957 buffer_strcat(wb, pre_value);
958 buffer_strcat(wb, "null");
959 buffer_strcat(wb, post_value);
967 if(printed) buffer_strcat(wb, "]}");
968 buffer_strcat(wb, "\n ]\n}\n");
970 if(only_non_zero && max_loop > 1) {
972 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
974 found_non_existing[c] = 0;
976 if(!print_hidden[c] && !found_non_zero[c]) {
982 if(changed) buffer_flush(wb);
989 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->len);
991 pthread_rwlock_unlock(&st->rwlock);
992 return last_timestamp;