countif.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_API_QUERY_COUNTIF_H
  3. #define NETDATA_API_QUERY_COUNTIF_H
  4. #include "../query.h"
  5. #include "../rrdr.h"
  6. enum tg_countif_cmp {
  7. TG_COUNTIF_EQUAL,
  8. TG_COUNTIF_NOTEQUAL,
  9. TG_COUNTIF_LESS,
  10. TG_COUNTIF_LESSEQUAL,
  11. TG_COUNTIF_GREATER,
  12. TG_COUNTIF_GREATEREQUAL,
  13. };
  14. struct tg_countif {
  15. enum tg_countif_cmp comparison;
  16. NETDATA_DOUBLE target;
  17. size_t count;
  18. size_t matched;
  19. };
  20. static inline void tg_countif_create(RRDR *r, const char *options __maybe_unused) {
  21. struct tg_countif *g = onewayalloc_callocz(r->internal.owa, 1, sizeof(struct tg_countif));
  22. r->time_grouping.data = g;
  23. if(options && *options) {
  24. // skip any leading spaces
  25. while(isspace(*options)) options++;
  26. // find the comparison function
  27. switch(*options) {
  28. case '!':
  29. options++;
  30. if(*options != '=' && *options != ':')
  31. options--;
  32. g->comparison = TG_COUNTIF_NOTEQUAL;
  33. break;
  34. case '>':
  35. options++;
  36. if(*options == '=' || *options == ':') {
  37. g->comparison = TG_COUNTIF_GREATEREQUAL;
  38. }
  39. else {
  40. options--;
  41. g->comparison = TG_COUNTIF_GREATER;
  42. }
  43. break;
  44. case '<':
  45. options++;
  46. if(*options == '>') {
  47. g->comparison = TG_COUNTIF_NOTEQUAL;
  48. }
  49. else if(*options == '=' || *options == ':') {
  50. g->comparison = TG_COUNTIF_LESSEQUAL;
  51. }
  52. else {
  53. options--;
  54. g->comparison = TG_COUNTIF_LESS;
  55. }
  56. break;
  57. default:
  58. case '=':
  59. case ':':
  60. g->comparison = TG_COUNTIF_EQUAL;
  61. break;
  62. }
  63. if(*options) options++;
  64. // skip everything up to the first digit
  65. while(isspace(*options)) options++;
  66. g->target = str2ndd(options, NULL);
  67. }
  68. else {
  69. g->target = 0.0;
  70. g->comparison = TG_COUNTIF_EQUAL;
  71. }
  72. }
  73. // resets when switches dimensions
  74. // so, clear everything to restart
  75. static inline void tg_countif_reset(RRDR *r) {
  76. struct tg_countif *g = (struct tg_countif *)r->time_grouping.data;
  77. g->matched = 0;
  78. g->count = 0;
  79. }
  80. static inline void tg_countif_free(RRDR *r) {
  81. onewayalloc_freez(r->internal.owa, r->time_grouping.data);
  82. r->time_grouping.data = NULL;
  83. }
  84. static inline void tg_countif_add(RRDR *r, NETDATA_DOUBLE value) {
  85. struct tg_countif *g = (struct tg_countif *)r->time_grouping.data;
  86. switch(g->comparison) {
  87. case TG_COUNTIF_GREATER:
  88. if(value > g->target) g->matched++;
  89. break;
  90. case TG_COUNTIF_GREATEREQUAL:
  91. if(value >= g->target) g->matched++;
  92. break;
  93. case TG_COUNTIF_LESS:
  94. if(value < g->target) g->matched++;
  95. break;
  96. case TG_COUNTIF_LESSEQUAL:
  97. if(value <= g->target) g->matched++;
  98. break;
  99. case TG_COUNTIF_EQUAL:
  100. if(value == g->target) g->matched++;
  101. break;
  102. case TG_COUNTIF_NOTEQUAL:
  103. if(value != g->target) g->matched++;
  104. break;
  105. }
  106. g->count++;
  107. }
  108. static inline NETDATA_DOUBLE tg_countif_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) {
  109. struct tg_countif *g = (struct tg_countif *)r->time_grouping.data;
  110. NETDATA_DOUBLE value;
  111. if(unlikely(!g->count)) {
  112. value = 0.0;
  113. *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
  114. }
  115. else {
  116. value = (NETDATA_DOUBLE)g->matched * 100 / (NETDATA_DOUBLE)g->count;
  117. }
  118. g->matched = 0;
  119. g->count = 0;
  120. return value;
  121. }
  122. #endif //NETDATA_API_QUERY_COUNTIF_H