master
h 169 lines 6.02 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_API_QUERIES_TRIMMED_MEAN_H
4 #define NETDATA_API_QUERIES_TRIMMED_MEAN_H
5
6 #include "../query.h"
7 #include "../rrdr.h"
8
9 struct tg_trimmed_mean {
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_trimmed_mean_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_trimmed_mean *g = (struct tg_trimmed_mean *)onewayalloc_callocz(r->internal.owa, 1, sizeof(struct tg_trimmed_mean));
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 = 1.0 - ((g->percent / 100.0) * 2.0);
34 r->time_grouping.data = g;
35 }
36
37 static inline void tg_trimmed_mean_create_1(RRDR *r, const char *options) {
38 tg_trimmed_mean_create_internal(r, options, 1.0);
39 }
40 static inline void tg_trimmed_mean_create_2(RRDR *r, const char *options) {
41 tg_trimmed_mean_create_internal(r, options, 2.0);
42 }
43 static inline void tg_trimmed_mean_create_3(RRDR *r, const char *options) {
44 tg_trimmed_mean_create_internal(r, options, 3.0);
45 }
46 static inline void tg_trimmed_mean_create_5(RRDR *r, const char *options) {
47 tg_trimmed_mean_create_internal(r, options, 5.0);
48 }
49 static inline void tg_trimmed_mean_create_10(RRDR *r, const char *options) {
50 tg_trimmed_mean_create_internal(r, options, 10.0);
51 }
52 static inline void tg_trimmed_mean_create_15(RRDR *r, const char *options) {
53 tg_trimmed_mean_create_internal(r, options, 15.0);
54 }
55 static inline void tg_trimmed_mean_create_20(RRDR *r, const char *options) {
56 tg_trimmed_mean_create_internal(r, options, 20.0);
57 }
58 static inline void tg_trimmed_mean_create_25(RRDR *r, const char *options) {
59 tg_trimmed_mean_create_internal(r, options, 25.0);
60 }
61
62 // resets when switches dimensions
63 // so, clear everything to restart
64 static inline void tg_trimmed_mean_reset(RRDR *r) {
65 struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
66 g->next_pos = 0;
67 }
68
69 static inline void tg_trimmed_mean_free(RRDR *r) {
70 struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
71 if(g) onewayalloc_freez(r->internal.owa, g->series);
72
73 onewayalloc_freez(r->internal.owa, r->time_grouping.data);
74 r->time_grouping.data = NULL;
75 }
76
77 static inline void tg_trimmed_mean_add(RRDR *r, NETDATA_DOUBLE value) {
78 struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
79
80 if(unlikely(g->next_pos >= g->series_size)) {
81 g->series = onewayalloc_doublesize( r->internal.owa, g->series, g->series_size * sizeof(NETDATA_DOUBLE));
82 g->series_size *= 2;
83 }
84
85 g->series[g->next_pos++] = value;
86 }
87
88 static inline NETDATA_DOUBLE tg_trimmed_mean_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) {
89 struct tg_trimmed_mean *g = (struct tg_trimmed_mean *)r->time_grouping.data;
90
91 NETDATA_DOUBLE value;
92 size_t available_slots = g->next_pos;
93
94 if(unlikely(!available_slots)) {
95 value = 0.0;
96 *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
97 }
98 else if(available_slots == 1) {
99 value = g->series[0];
100 }
101 else {
102 sort_series(g->series, available_slots);
103
104 NETDATA_DOUBLE min = g->series[0];
105 NETDATA_DOUBLE max = g->series[available_slots - 1];
106
107 if (min != max) {
108 size_t slots_to_use = (size_t)((NETDATA_DOUBLE)available_slots * g->percent);
109 if(!slots_to_use) slots_to_use = 1;
110
111 NETDATA_DOUBLE percent_to_use = (NETDATA_DOUBLE)slots_to_use / (NETDATA_DOUBLE)available_slots;
112 NETDATA_DOUBLE percent_delta = g->percent - percent_to_use;
113
114 NETDATA_DOUBLE percent_interpolation_slot = 0.0;
115 NETDATA_DOUBLE percent_last_slot = 0.0;
116 if(percent_delta > 0.0) {
117 NETDATA_DOUBLE percent_to_use_plus_1_slot = (NETDATA_DOUBLE)(slots_to_use + 1) / (NETDATA_DOUBLE)available_slots;
118 NETDATA_DOUBLE percent_1slot = percent_to_use_plus_1_slot - percent_to_use;
119
120 percent_interpolation_slot = percent_delta / percent_1slot;
121 percent_last_slot = 1 - percent_interpolation_slot;
122 }
123
124 int start_slot, stop_slot, step, last_slot, interpolation_slot;
125 if(min >= 0.0 && max >= 0.0) {
126 start_slot = (int)((available_slots - slots_to_use) / 2);
127 stop_slot = start_slot + (int)slots_to_use;
128 last_slot = stop_slot - 1;
129 interpolation_slot = stop_slot;
130 step = 1;
131 }
132 else {
133 start_slot = (int)available_slots - 1 - (int)((available_slots - slots_to_use) / 2);
134 stop_slot = start_slot - (int)slots_to_use;
135 last_slot = stop_slot + 1;
136 interpolation_slot = stop_slot;
137 step = -1;
138 }
139
140 value = 0.0;
141 for(int slot = start_slot; slot != stop_slot ; slot += step)
142 value += g->series[slot];
143
144 size_t counted = slots_to_use;
145 if(percent_interpolation_slot > 0.0 && interpolation_slot >= 0 && interpolation_slot < (int)available_slots) {
146 value += g->series[interpolation_slot] * percent_interpolation_slot;
147 value += g->series[last_slot] * percent_last_slot;
148 counted++;
149 }
150
151 value = value / (NETDATA_DOUBLE)counted;
152 }
153 else
154 value = min;
155 }
156
157 if(unlikely(!netdata_double_isnumber(value))) {
158 value = 0.0;
159 *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
160 }
161
162 //log_series_to_stderr(g->series, g->next_pos, value, "trimmed_mean");
163
164 g->next_pos = 0;
165
166 return value;
167 }
168
169 #endif //NETDATA_API_QUERIES_TRIMMED_MEAN_H