trimmed_mean.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_API_QUERIES_TRIMMED_MEAN_H
  3. #define NETDATA_API_QUERIES_TRIMMED_MEAN_H
  4. #include "../query.h"
  5. #include "../rrdr.h"
  6. struct tg_trimmed_mean {
  7. size_t series_size;
  8. size_t next_pos;
  9. NETDATA_DOUBLE percent;
  10. NETDATA_DOUBLE *series;
  11. };
  12. static inline void tg_trimmed_mean_create_internal(RRDR *r, const char *options, NETDATA_DOUBLE def) {
  13. long entries = r->view.group;
  14. if(entries < 10) entries = 10;
  15. struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)onewayalloc_callocz(r->internal.owa, 1, sizeof(struct tg_trimmed_mean));
  16. g->series = onewayalloc_mallocz(r->internal.owa, entries * sizeof(NETDATA_DOUBLE));
  17. g->series_size = (size_t)entries;
  18. g->percent = def;
  19. if(options && *options) {
  20. g->percent = str2ndd(options, NULL);
  21. if(!netdata_double_isnumber(g->percent)) g->percent = 0.0;
  22. if(g->percent < 0.0) g->percent = 0.0;
  23. if(g->percent > 50.0) g->percent = 50.0;
  24. }
  25. g->percent = 1.0 - ((g->percent / 100.0) * 2.0);
  26. r->time_grouping.data = g;
  27. }
  28. static inline void tg_trimmed_mean_create_1(RRDR *r, const char *options) {
  29. tg_trimmed_mean_create_internal(r, options, 1.0);
  30. }
  31. static inline void tg_trimmed_mean_create_2(RRDR *r, const char *options) {
  32. tg_trimmed_mean_create_internal(r, options, 2.0);
  33. }
  34. static inline void tg_trimmed_mean_create_3(RRDR *r, const char *options) {
  35. tg_trimmed_mean_create_internal(r, options, 3.0);
  36. }
  37. static inline void tg_trimmed_mean_create_5(RRDR *r, const char *options) {
  38. tg_trimmed_mean_create_internal(r, options, 5.0);
  39. }
  40. static inline void tg_trimmed_mean_create_10(RRDR *r, const char *options) {
  41. tg_trimmed_mean_create_internal(r, options, 10.0);
  42. }
  43. static inline void tg_trimmed_mean_create_15(RRDR *r, const char *options) {
  44. tg_trimmed_mean_create_internal(r, options, 15.0);
  45. }
  46. static inline void tg_trimmed_mean_create_20(RRDR *r, const char *options) {
  47. tg_trimmed_mean_create_internal(r, options, 20.0);
  48. }
  49. static inline void tg_trimmed_mean_create_25(RRDR *r, const char *options) {
  50. tg_trimmed_mean_create_internal(r, options, 25.0);
  51. }
  52. // resets when switches dimensions
  53. // so, clear everything to restart
  54. static inline void tg_trimmed_mean_reset(RRDR *r) {
  55. struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
  56. g->next_pos = 0;
  57. }
  58. static inline void tg_trimmed_mean_free(RRDR *r) {
  59. struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
  60. if(g) onewayalloc_freez(r->internal.owa, g->series);
  61. onewayalloc_freez(r->internal.owa, r->time_grouping.data);
  62. r->time_grouping.data = NULL;
  63. }
  64. static inline void tg_trimmed_mean_add(RRDR *r, NETDATA_DOUBLE value) {
  65. struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
  66. if(unlikely(g->next_pos >= g->series_size)) {
  67. g->series = onewayalloc_doublesize( r->internal.owa, g->series, g->series_size * sizeof(NETDATA_DOUBLE));
  68. g->series_size *= 2;
  69. }
  70. g->series[g->next_pos++] = value;
  71. }
  72. static inline NETDATA_DOUBLE tg_trimmed_mean_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) {
  73. struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
  74. NETDATA_DOUBLE value;
  75. size_t available_slots = g->next_pos;
  76. if(unlikely(!available_slots)) {
  77. value = 0.0;
  78. *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
  79. }
  80. else if(available_slots == 1) {
  81. value = g->series[0];
  82. }
  83. else {
  84. sort_series(g->series, available_slots);
  85. NETDATA_DOUBLE min = g->series[0];
  86. NETDATA_DOUBLE max = g->series[available_slots - 1];
  87. if (min != max) {
  88. size_t slots_to_use = (size_t)((NETDATA_DOUBLE)available_slots * g->percent);
  89. if(!slots_to_use) slots_to_use = 1;
  90. NETDATA_DOUBLE percent_to_use = (NETDATA_DOUBLE)slots_to_use / (NETDATA_DOUBLE)available_slots;
  91. NETDATA_DOUBLE percent_delta = g->percent - percent_to_use;
  92. NETDATA_DOUBLE percent_interpolation_slot = 0.0;
  93. NETDATA_DOUBLE percent_last_slot = 0.0;
  94. if(percent_delta > 0.0) {
  95. NETDATA_DOUBLE percent_to_use_plus_1_slot = (NETDATA_DOUBLE)(slots_to_use + 1) / (NETDATA_DOUBLE)available_slots;
  96. NETDATA_DOUBLE percent_1slot = percent_to_use_plus_1_slot - percent_to_use;
  97. percent_interpolation_slot = percent_delta / percent_1slot;
  98. percent_last_slot = 1 - percent_interpolation_slot;
  99. }
  100. int start_slot, stop_slot, step, last_slot, interpolation_slot;
  101. if(min >= 0.0 && max >= 0.0) {
  102. start_slot = (int)((available_slots - slots_to_use) / 2);
  103. stop_slot = start_slot + (int)slots_to_use;
  104. last_slot = stop_slot - 1;
  105. interpolation_slot = stop_slot;
  106. step = 1;
  107. }
  108. else {
  109. start_slot = (int)available_slots - 1 - (int)((available_slots - slots_to_use) / 2);
  110. stop_slot = start_slot - (int)slots_to_use;
  111. last_slot = stop_slot + 1;
  112. interpolation_slot = stop_slot;
  113. step = -1;
  114. }
  115. value = 0.0;
  116. for(int slot = start_slot; slot != stop_slot ; slot += step)
  117. value += g->series[slot];
  118. size_t counted = slots_to_use;
  119. if(percent_interpolation_slot > 0.0 && interpolation_slot >= 0 && interpolation_slot < (int)available_slots) {
  120. value += g->series[interpolation_slot] * percent_interpolation_slot;
  121. value += g->series[last_slot] * percent_last_slot;
  122. counted++;
  123. }
  124. value = value / (NETDATA_DOUBLE)counted;
  125. }
  126. else
  127. value = min;
  128. }
  129. if(unlikely(!netdata_double_isnumber(value))) {
  130. value = 0.0;
  131. *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
  132. }
  133. //log_series_to_stderr(g->series, g->next_pos, value, "trimmed_mean");
  134. g->next_pos = 0;
  135. return value;
  136. }
  137. #endif //NETDATA_API_QUERIES_TRIMMED_MEAN_H