#include <config.h>
#endif
#include <pthread.h>
-#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
#include "log.h"
#include "common.h"
"\t\t\t\"name\": \"%s\",\n"
"\t\t\t\"type\": \"%s\",\n"
"\t\t\t\"family\": \"%s\",\n"
+ "\t\t\t\"context\": \"%s\",\n"
"\t\t\t\"title\": \"%s\",\n"
"\t\t\t\"priority\": %ld,\n"
"\t\t\t\"enabled\": %s,\n"
, st->name
, st->type
, st->family
+ , st->context
, st->title
, st->priority
, st->enabled?"true":"false"
"\t\t\t\"name\": \"%s\",\n"
"\t\t\t\"type\": \"%s\",\n"
"\t\t\t\"family\": \"%s\",\n"
+ "\t\t\t\"context\": \"%s\",\n"
"\t\t\t\"title\": \"%s\",\n"
"\t\t\t\"priority\": %ld,\n"
"\t\t\t\"enabled\": %d,\n"
, st->name
, st->type
, st->family
+ , st->context
, st->title
, st->priority
, st->enabled
// ----------------------------------------------------------------------------
-// RRDR options
+// RRDR dimension options
#define RRDR_EMPTY 0x01 // the dimension contains / the value is empty (null)
#define RRDR_RESET 0x02 // the dimension contains / the value is reset
#define RRDR_HIDDEN 0x04 // the dimension contains / the value is hidden
#define RRDR_NONZERO 0x08 // the dimension contains / the value is non-zero
+// RRDR result options
+#define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
+#define RRDR_RESULT_OPTION_RELATIVE 0x00000002
typedef struct rrdresult {
RRDSET *st; // the chart this result refers to
+ uint32_t result_options; // RRDR_RESULT_OPTION_*
+
int d; // the number of dimensions
- int n; // the number of values in the arrays
- int rows; // the number of rows used
+ long n; // the number of values in the arrays
+ long rows; // the number of rows used
uint8_t *od; // the options for the dimensions
calculated_number *v; // array n x d values
uint8_t *o; // array n x d options
- int c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
+ long c; // current line ( -1 ~ n ), ( -1 = none, use rrdr_rows() to get number of rows )
- int group; // how many collected values were grouped for each row
- int update_every; // what is the suggested update frequency in seconds
+ long group; // how many collected values were grouped for each row
+ long update_every; // what is the suggested update frequency in seconds
calculated_number min;
calculated_number max;
for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
if(!strcmp(d->name, tok)) {
r->od[c] &= ~RRDR_HIDDEN;
+
+ // since the user needs this dimension
+ // make it appear as NONZERO, to return it
+ // even if the dimension has only zeros
+ r->od[c] |= RRDR_NONZERO;
}
}
}
}
}
-uint32_t rrdr_check_options(RRDR *r, uint32_t options)
+uint32_t rrdr_check_options(RRDR *r, uint32_t options, const char *dims)
{
if(options & RRDR_OPTION_NONZERO) {
- long c, i;
- RRDDIM *rd;
-
- // find how many dimensions are not zero
- for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
- if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
- if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
- i++;
+ long i;
+
+ if(dims && *dims) {
+ // the caller wants specific dimensions
+ // disable NONZERO option
+ // to make sure we don't accidentally prevent
+ // the specific dimensions from being returned
+ i = 0;
+ }
+ else {
+ // find how many dimensions are not zero
+ long c;
+ RRDDIM *rd;
+ for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ; c++, rd = rd->next) {
+ if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
+ if(unlikely(!(r->od[c] & RRDR_NONZERO))) continue;
+ i++;
+ }
}
- // if with nonzero we get i = 0
- // but c != 0, then disable nonzero
- // to show all dimensions
+ // if with nonzero we get i = 0 (no dimensions will be returned)
+ // disable nonzero to show all dimensions
if(!i) options &= ~RRDR_OPTION_NONZERO;
}
void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t options, int string_value)
{
- int rows = rrdr_rows(r);
+ long rows = rrdr_rows(r);
long c, i;
RRDDIM *rd;
" %sname%s: %s%s%s,\n"
" %sview_update_every%s: %d,\n"
" %supdate_every%s: %d,\n"
- " %sfirst_entry_t%s: %u,\n"
- " %slast_entry_t%s: %u,\n"
- " %smin%s: "
+ " %sfirst_entry%s: %u,\n"
+ " %slast_entry%s: %u,\n"
+ " %sbefore%s: %u,\n"
+ " %safter%s: %u,\n"
+ " %sdimension_names%s: ["
, kq, kq
, kq, kq, sq, r->st->id, sq
, kq, kq, sq, r->st->name, sq
, kq, kq, r->st->update_every
, kq, kq, rrdset_first_entry_t(r->st)
, kq, kq, rrdset_last_entry_t(r->st)
- , kq, kq);
-
- buffer_rrd_value(wb, r->min);
- buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
- buffer_rrd_value(wb, r->max);
- buffer_sprintf(wb, ",\n"
- " %sbefore%s: %u,\n"
- " %safter%s: %u,\n"
- " %sdimension_names%s: ["
, kq, kq, r->before
, kq, kq, r->after
, kq, kq);
}
buffer_sprintf(wb, "],\n"
- " %sresult_latest_values%s: ["
+ " %sview_latest_values%s: ["
, kq, kq);
i = 0;
if(r) {;}
if(format) {;}
- char sq[2] = ""; // string quote
+ char kq[2] = "", // key quote
+ sq[2] = ""; // string quote
- if( options & RRDR_OPTION_GOOGLE_JSON )
+ if( options & RRDR_OPTION_GOOGLE_JSON ) {
+ kq[0] = '\0';
sq[0] = '\'';
- else
+ }
+ else {
+ kq[0] = '"';
sq[0] = '"';
+ }
if(string_value) buffer_strcat(wb, sq);
+
+ buffer_sprintf(wb, ",\n %smin%s: ", kq, kq);
+ buffer_rrd_value(wb, r->min);
+ buffer_sprintf(wb, ",\n %smax%s: ", kq, kq);
+ buffer_rrd_value(wb, r->max);
buffer_strcat(wb, "\n}\n");
}
static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
{
//info("RRD2JSON(): %s: BEGIN", r->st->id);
- int row_annotations = 0, dates = JSON_DATES_JS, dates_with_new = 0;
+ int row_annotations = 0, dates, dates_with_new = 0;
char kq[2] = "", // key quote
sq[2] = "", // string quote
pre_label[101] = "", // before each label
snprintf(post_value, 100, "}");
snprintf(post_line, 100, "]}");
snprintf(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
- snprintf(finish, 100, "\n ]\n}\n");
+ snprintf(finish, 100, "\n ]\n}");
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);
snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
else
snprintf(post_line, 100, "]");
snprintf(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
- snprintf(finish, 100, "\n ]\n}\n");
+ snprintf(finish, 100, "\n ]\n}");
buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
buffer_sprintf(wb, "%stime%s", sq, sq);
}
// for each line in the array
+ calculated_number total = 1;
for(i = start; i != end ;i += step) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
buffer_strcat(wb, post_date);
}
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
+ total = 0;
+ for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
+ calculated_number n = cn[c];
+
+ if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ total += n;
+ }
+ // prevent a division by zero
+ if(total == 0) total = 1;
+ }
+
// for each dimension
for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
else
buffer_strcat(wb, "null");
}
- else if((options & RRDR_OPTION_ABSOLUTE))
- buffer_rrd_value(wb, (n<0)?-n:n);
- else
+ else {
+ if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE))
+ n = n * 100 / total;
+
buffer_rrd_value(wb, n);
+ }
buffer_strcat(wb, post_value);
}
//info("RRD2JSON(): %s: END", r->st->id);
}
-static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline)
+static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startline, const char *separator, const char *endline, const char *betweenlines)
{
//info("RRD2CSV(): %s: BEGIN", r->st->id);
long c, i;
if(!i) {
buffer_strcat(wb, startline);
+ if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
buffer_strcat(wb, "time");
+ if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
}
buffer_strcat(wb, separator);
+ if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
buffer_strcat(wb, d->name);
+ if(options & RRDR_OPTION_LABEL_QUOTES) buffer_strcat(wb, "\"");
i++;
}
buffer_strcat(wb, endline);
}
// for each line in the array
+ calculated_number total = 1;
for(i = start; i != end ;i += step) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
+ buffer_strcat(wb, betweenlines);
buffer_strcat(wb, startline);
time_t now = r->t[i];
buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
}
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
+ total = 0;
+ for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
+ calculated_number n = cn[c];
+
+ if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ total += n;
+ }
+ // prevent a division by zero
+ if(total == 0) total = 1;
+ }
+
// for each dimension
for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
else
buffer_strcat(wb, "null");
}
- else if((options & RRDR_OPTION_ABSOLUTE))
- buffer_rrd_value(wb, (n<0)?-n:n);
- else
+ else {
+ if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE))
+ n = n * 100 / total;
+
buffer_rrd_value(wb, n);
+ }
}
buffer_strcat(wb, endline);
}
// for each line in the array
+ calculated_number total = 1;
for(i = start; i != end ;i += step) {
calculated_number *cn = &r->v[ i * r->d ];
uint8_t *co = &r->o[ i * r->d ];
- calculated_number sum = 0, min = 0, max = 0;
+ calculated_number sum = 0, min = 0, max = 0, v;
int all_null = 1, init = 1;
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
+ total = 0;
+ for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
+ calculated_number n = cn[c];
+
+ if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ total += n;
+ }
+ // prevent a division by zero
+ if(total == 0) total = 1;
+ }
+
// for each dimension
for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
calculated_number n = cn[c];
+ if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+ n = -n;
+
+ if(unlikely(options & RRDR_OPTION_PERCENTAGE))
+ n = n * 100 / total;
+
if(unlikely(init)) {
if(n > 0) {
min = 0;
if(likely(!(co[c] & RRDR_EMPTY))) {
all_null = 0;
- if((options & RRDR_OPTION_ABSOLUTE) && n < 0) n = -n;
sum += n;
}
else
buffer_strcat(wb, "null");
}
- else if(options & RRDR_OPTION_MIN2MAX)
- buffer_rrd_value(wb, max - min);
- else
- buffer_rrd_value(wb, sum);
+ else {
+ if(options & RRDR_OPTION_MIN2MAX)
+ v = max - min;
+ else
+ v = sum;
+
+ if(likely(i != start)) {
+ if(r->min > v) r->min = v;
+ if(r->max < v) r->max = v;
+ }
+ else {
+ r->min = v;
+ r->max = v;
+ }
+
+ buffer_rrd_value(wb, v);
+ }
}
buffer_strcat(wb, suffix);
//info("RRD2SSV(): %s: END", r->st->id);
r->c = 0;
}
-static RRDR *rrdr_create(RRDSET *st, int n)
+static RRDR *rrdr_create(RRDSET *st, long n)
{
if(unlikely(!st)) {
error("NULL value given!");
r->od = malloc(r->d * sizeof(uint8_t));
if(unlikely(!r->od)) goto cleanup;
+ // set the hidden flag on hidden dimensions
+ int c;
+ for(c = 0, rd = st->dimensions ; rd ; c++, rd = rd->next) {
+ if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] = RRDR_HIDDEN;
+ else r->od[c] = 0;
+ }
+
r->c = -1;
r->group = 1;
RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
{
int debug = st->debug;
+ int absolute_period_requested = -1;
time_t first_entry_t = rrdset_first_entry_t(st);
time_t last_entry_t = rrdset_last_entry_t(st);
if(before == 0 && after == 0) {
before = last_entry_t;
after = first_entry_t;
+ absolute_period_requested = 0;
}
// allow relative for before and after
- if(before <= st->update_every * st->entries)
+ if(before <= st->update_every * st->entries) {
before = last_entry_t + before;
+ absolute_period_requested = 0;
+ }
- if(after <= st->update_every * st->entries)
+ if(after <= st->update_every * st->entries) {
after = last_entry_t + after;
+ absolute_period_requested = 0;
+ }
+
+ if(absolute_period_requested == -1)
+ absolute_period_requested = 1;
// make sure they are within our timeframe
if(before > last_entry_t) before = last_entry_t;
return r;
}
+ if(absolute_period_requested == 1)
+ r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
+ else
+ r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
+
// find how many dimensions we have
long dimensions = r->d;
calculated_number *cn = rrdr_line_values(r);
uint8_t *co = rrdr_line_options(r);
- for(rd = st->dimensions, c = 0 ; likely(rd && c < dimensions) ; rd = rd->next, c++) {
+ for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
// update the dimension options
if(likely(found_non_zero[c])) r->od[c] |= RRDR_NONZERO;
- if(unlikely(rd->flags & RRDDIM_FLAG_HIDDEN)) r->od[c] |= RRDR_HIDDEN;
// store the specific point options
co[c] = group_options[c];
return 500;
}
- options = rrdr_check_options(r, options);
+ if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
+ wb->options |= WB_CONTENT_NO_CACHEABLE;
+ else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
+ wb->options |= WB_CONTENT_CACHEABLE;
+
+ options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
if(dimensions)
rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1);
- rrdr2csv(r, wb, options, "", ",", "\\n");
+ rrdr2csv(r, wb, options, "", ",", "\\n", "");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
- rrdr2csv(r, wb, options, "", ",", "\r\n");
+ rrdr2csv(r, wb, options, "", ",", "\r\n", "");
+ }
+ break;
+
+ case DATASOURCE_CSV_JSON_ARRAY:
+ wb->contenttype = CT_APPLICATION_JSON;
+ if(options & RRDR_OPTION_JSON_WRAP) {
+ rrdr_json_wrapper_begin(r, wb, format, options, 0);
+ buffer_strcat(wb, "[\n");
+ rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
+ buffer_strcat(wb, "\n]");
+ rrdr_json_wrapper_end(r, wb, format, options, 0);
+ }
+ else {
+ wb->contenttype = CT_TEXT_PLAIN;
+ buffer_strcat(wb, "[\n");
+ rrdr2csv(r, wb, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n");
+ buffer_strcat(wb, "\n]");
}
break;
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1);
- rrdr2csv(r, wb, options, "", "\t", "\\n");
+ rrdr2csv(r, wb, options, "", "\t", "\\n", "");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
- rrdr2csv(r, wb, options, "", "\t", "\r\n");
+ rrdr2csv(r, wb, options, "", "\t", "\r\n", "");
}
break;
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1);
buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
- rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n");
+ rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "");
buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_HTML;
buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
- rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n");
+ rrdr2csv(r, wb, options, "<tr><td>", "</td><td>", "</td></tr>\n", "");
buffer_strcat(wb, "</table>\n</center>\n</html>\n");
}
break;
return 200;
}
-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)
+time_t rrd_stats_json(int type, RRDSET *st, BUFFER *wb, long points, long group, int group_method, time_t after, time_t before, int only_non_zero)
{
int c;
pthread_rwlock_rdlock(&st->rwlock);