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