max.h 1.4 KB

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