value.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "value.h"
  3. inline calculated_number rrdr2value(RRDR *r, long i, RRDR_OPTIONS options, int *all_values_are_null, RRDDIM *temp_rd) {
  4. if (r->st_needs_lock)
  5. rrdset_check_rdlock(r->st);
  6. long c;
  7. RRDDIM *d;
  8. calculated_number *cn = &r->v[ i * r->d ];
  9. RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
  10. calculated_number sum = 0, min = 0, max = 0, v;
  11. int all_null = 1, init = 1;
  12. calculated_number total = 1;
  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. calculated_number 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. calculated_number 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. }
  60. if(unlikely(all_null)) {
  61. if(likely(all_values_are_null))
  62. *all_values_are_null = 1;
  63. return 0;
  64. }
  65. else {
  66. if(likely(all_values_are_null))
  67. *all_values_are_null = 0;
  68. }
  69. if(options & RRDR_OPTION_MIN2MAX)
  70. v = max - min;
  71. else
  72. v = sum;
  73. return v;
  74. }