value.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "value.h"
  3. inline NETDATA_DOUBLE rrdr2value(RRDR *r, long i, RRDR_OPTIONS options, int *all_values_are_null, uint8_t *anomaly_rate, RRDDIM *temp_rd) {
  4. long c;
  5. RRDDIM *d;
  6. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  7. RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
  8. uint8_t *ar = &r->ar[ i * r->d ];
  9. NETDATA_DOUBLE sum = 0, min = 0, max = 0, v;
  10. int all_null = 1, init = 1;
  11. NETDATA_DOUBLE total = 1;
  12. size_t total_anomaly_rate = 0;
  13. int set_min_max = 0;
  14. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  15. total = 0;
  16. for (c = 0, d = temp_rd ? temp_rd : r->st->dimensions; d && c < r->d; c++, d = d->next) {
  17. NETDATA_DOUBLE n = cn[c];
  18. if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  19. n = -n;
  20. total += n;
  21. }
  22. // prevent a division by zero
  23. if(total == 0) total = 1;
  24. set_min_max = 1;
  25. }
  26. // for each dimension
  27. for (c = 0, d = temp_rd ? temp_rd : r->st->dimensions; d && c < r->d; c++, d = d->next) {
  28. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  29. if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  30. NETDATA_DOUBLE n = cn[c];
  31. if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  32. n = -n;
  33. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  34. n = n * 100 / total;
  35. if(unlikely(set_min_max)) {
  36. r->min = r->max = n;
  37. set_min_max = 0;
  38. }
  39. if(n < r->min) r->min = n;
  40. if(n > r->max) r->max = n;
  41. }
  42. if(unlikely(init)) {
  43. if(n > 0) {
  44. min = 0;
  45. max = n;
  46. }
  47. else {
  48. min = n;
  49. max = 0;
  50. }
  51. init = 0;
  52. }
  53. if(likely(!(co[c] & RRDR_VALUE_EMPTY))) {
  54. all_null = 0;
  55. sum += n;
  56. }
  57. if(n < min) min = n;
  58. if(n > max) max = n;
  59. total_anomaly_rate += ar[c];
  60. }
  61. if(anomaly_rate) {
  62. if(!r->d) *anomaly_rate = 0;
  63. else *anomaly_rate = total_anomaly_rate / r->d;
  64. }
  65. if(unlikely(all_null)) {
  66. if(likely(all_values_are_null))
  67. *all_values_are_null = 1;
  68. return 0;
  69. }
  70. else {
  71. if(likely(all_values_are_null))
  72. *all_values_are_null = 0;
  73. }
  74. if(options & RRDR_OPTION_MIN2MAX)
  75. v = max - min;
  76. else
  77. v = sum;
  78. return v;
  79. }