| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_API_QUERY_MAX_H |
| 4 | #define NETDATA_API_QUERY_MAX_H |
| 5 | |
| 6 | #include "../query.h" |
| 7 | #include "../rrdr.h" |
| 8 | |
| 9 | struct tg_max { |
| 10 | NETDATA_DOUBLE max; |
| 11 | size_t count; |
| 12 | }; |
| 13 | |
| 14 | static inline void tg_max_create(RRDR *r, const char *options __maybe_unused) { |
| 15 | r->time_grouping.data = onewayalloc_callocz(r->internal.owa, 1, sizeof(struct tg_max)); |
| 16 | } |
| 17 | |
| 18 | // resets when switches dimensions |
| 19 | // so, clear everything to restart |
| 20 | static inline void tg_max_reset(RRDR *r) { |
| 21 | struct tg_max *g = (struct tg_max *)r->time_grouping.data; |
| 22 | g->max = 0; |
| 23 | g->count = 0; |
| 24 | } |
| 25 | |
| 26 | static inline void tg_max_free(RRDR *r) { |
| 27 | onewayalloc_freez(r->internal.owa, r->time_grouping.data); |
| 28 | r->time_grouping.data = NULL; |
| 29 | } |
| 30 | |
| 31 | static inline void tg_max_add(RRDR *r, NETDATA_DOUBLE value) { |
| 32 | struct tg_max *g = (struct tg_max *)r->time_grouping.data; |
| 33 | |
| 34 | if(!g->count || fabsndd(value) > fabsndd(g->max)) { |
| 35 | g->max = value; |
| 36 | g->count++; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static inline NETDATA_DOUBLE tg_max_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) { |
| 41 | struct tg_max *g = (struct tg_max *)r->time_grouping.data; |
| 42 | |
| 43 | NETDATA_DOUBLE value; |
| 44 | |
| 45 | if(unlikely(!g->count)) { |
| 46 | value = 0.0; |
| 47 | *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY; |
| 48 | } |
| 49 | else { |
| 50 | value = g->max; |
| 51 | } |
| 52 | |
| 53 | g->max = 0.0; |
| 54 | g->count = 0; |
| 55 | |
| 56 | return value; |
| 57 | } |
| 58 | |
| 59 | #endif //NETDATA_API_QUERY_MAX_H |