@cryptotaxi247 / netdata-1 / commits / 8a3ece5de

when API option percentage is used, then view_latest_values, min and max should also be percentages

Costa Tsaousis (ktsaou) committed Dec 23, 2017 at 18:11 UTC 8a3ece5de3c3766c142b435f788d21ece3264832
2 files changed +70 -8
src/rrd2json.c
+69 -7
@@ -717,6 +717,23 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t opti
717
718 i = 0;
719 if(rows) {
720 + calculated_number total = 1;
721 +
722 + if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
723 + total = 0;
724 + for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
725 + calculated_number *cn = &r->v[ (0) * r->d ];
726 + calculated_number n = cn[c];
727 +
728 + if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
729 + n = -n;
730 +
731 + total += n;
732 + }
733 + // prevent a division by zero
734 + if(total == 0) total = 1;
735 + }
736 +
737 for(c = 0, i = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
738 if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
739 if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
@@ -726,11 +743,23 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, uint32_t opti
743
744 calculated_number *cn = &r->v[ (0) * r->d ];
745 uint8_t *co = &r->o[ (0) * r->d ];
746 + calculated_number n = cn[c];
747
730 - if(co[c] & RRDR_EMPTY)
731 - buffer_strcat(wb, "null");
732 - else
733 - buffer_rrd_value(wb, cn[c]);
748 + if(co[c] & RRDR_EMPTY) {
749 + if(options & RRDR_OPTION_NULL2ZERO)
750 + buffer_strcat(wb, "0");
751 + else
752 + buffer_strcat(wb, "null");
753 + }
754 + else {
755 + if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
756 + n = -n;
757 +
758 + if(unlikely(options & RRDR_OPTION_PERCENTAGE))
759 + n = n * 100 / total;
760 +
761 + buffer_rrd_value(wb, n);
762 + }
763 }
764 }
765 if(!i) {
@@ -963,6 +992,7 @@ static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
992 buffer_strcat(wb, post_date);
993 }
994
995 + int set_min_max = 0;
996 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
997 total = 0;
998 for(c = 0, rd = r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
@@ -975,6 +1005,7 @@ static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
1005 }
1006 // prevent a division by zero
1007 if(total == 0) total = 1;
1008 + set_min_max = 1;
1009 }
1010
1011 // for each dimension
@@ -999,9 +1030,18 @@ static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
1030 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1031 n = -n;
1032
1002 - if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1033 + if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1034 n = n * 100 / total;
1035
1036 + if(unlikely(set_min_max)) {
1037 + r->min = r->max = n;
1038 + set_min_max = 0;
1039 + }
1040 +
1041 + if(n < r->min) r->min = n;
1042 + if(n > r->max) r->max = n;
1043 + }
1044 +
1045 buffer_rrd_value(wb, n);
1046 }
1047
@@ -1078,6 +1118,7 @@ static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startlin
1118 buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1119 }
1120
1121 + int set_min_max = 0;
1122 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1123 total = 0;
1124 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
@@ -1090,6 +1131,7 @@ static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startlin
1131 }
1132 // prevent a division by zero
1133 if(total == 0) total = 1;
1134 + set_min_max = 1;
1135 }
1136
1137 // for each dimension
@@ -1111,9 +1153,18 @@ static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startlin
1153 if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1154 n = -n;
1155
1114 - if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1156 + if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1157 n = n * 100 / total;
1158
1159 + if(unlikely(set_min_max)) {
1160 + r->min = r->max = n;
1161 + set_min_max = 0;
1162 + }
1163 +
1164 + if(n < r->min) r->min = n;
1165 + if(n > r->max) r->max = n;
1166 + }
1167 +
1168 buffer_rrd_value(wb, n);
1169 }
1170 }
@@ -1136,6 +1187,7 @@ inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, in
1187 int all_null = 1, init = 1;
1188
1189 calculated_number total = 1;
1190 + int set_min_max = 0;
1191 if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1192 total = 0;
1193 for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
@@ -1148,6 +1200,7 @@ inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, in
1200 }
1201 // prevent a division by zero
1202 if(total == 0) total = 1;
1203 + set_min_max = 1;
1204 }
1205
1206 // for each dimension
@@ -1160,9 +1213,18 @@ inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, in
1213 if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
1214 n = -n;
1215
1163 - if(unlikely(options & RRDR_OPTION_PERCENTAGE))
1216 + if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
1217 n = n * 100 / total;
1218
1219 + if(unlikely(set_min_max)) {
1220 + r->min = r->max = n;
1221 + set_min_max = 0;
1222 + }
1223 +
1224 + if(n < r->min) r->min = n;
1225 + if(n > r->max) r->max = n;
1226 + }
1227 +
1228 if(unlikely(init)) {
1229 if(n > 0) {
1230 min = 0;
web/dashboard.js
+1 -1
@@ -3755,7 +3755,7 @@ var NETDATA = window.NETDATA || {};
3755 //var labels = this.data.dimension_names;
3756 //var i = labels.length;
3757 //while(i--)
3758 - // this.legendSetLabelValue(labels[i], this.data.latest_values[i]);
3758 + // this.legendSetLabelValue(labels[i], this.data.view_latest_values[i]);
3759 }
3760 return;
3761 }