rrdr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_QUERIES_RRDR_H
  3. #define NETDATA_QUERIES_RRDR_H
  4. #include "libnetdata/libnetdata.h"
  5. #include "web/api/queries/query.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef enum tier_query_fetch {
  10. TIER_QUERY_FETCH_SUM,
  11. TIER_QUERY_FETCH_MIN,
  12. TIER_QUERY_FETCH_MAX,
  13. TIER_QUERY_FETCH_AVERAGE
  14. } TIER_QUERY_FETCH;
  15. typedef enum rrdr_options {
  16. RRDR_OPTION_NONZERO = (1 << 0), // don't output dimensions with just zero values
  17. RRDR_OPTION_REVERSED = (1 << 1), // output the rows in reverse order (oldest to newest)
  18. RRDR_OPTION_ABSOLUTE = (1 << 2), // values positive, for DATASOURCE_SSV before summing
  19. RRDR_OPTION_MIN2MAX = (1 << 3), // when adding dimensions, use max - min, instead of sum
  20. RRDR_OPTION_SECONDS = (1 << 4), // output seconds, instead of dates
  21. RRDR_OPTION_MILLISECONDS = (1 << 5), // output milliseconds, instead of dates
  22. RRDR_OPTION_NULL2ZERO = (1 << 6), // do not show nulls, convert them to zeros
  23. RRDR_OPTION_OBJECTSROWS = (1 << 7), // each row of values should be an object, not an array
  24. RRDR_OPTION_GOOGLE_JSON = (1 << 8), // comply with google JSON/JSONP specs
  25. RRDR_OPTION_JSON_WRAP = (1 << 9), // wrap the response in a JSON header with info about the result
  26. RRDR_OPTION_LABEL_QUOTES = (1 << 10), // in CSV output, wrap header labels in double quotes
  27. RRDR_OPTION_PERCENTAGE = (1 << 11), // give values as percentage of total
  28. RRDR_OPTION_NOT_ALIGNED = (1 << 12), // do not align charts for persistent timeframes
  29. RRDR_OPTION_DISPLAY_ABS = (1 << 13), // for badges, display the absolute value, but calculate colors with sign
  30. RRDR_OPTION_MATCH_IDS = (1 << 14), // when filtering dimensions, match only IDs
  31. RRDR_OPTION_MATCH_NAMES = (1 << 15), // when filtering dimensions, match only names
  32. RRDR_OPTION_NATURAL_POINTS = (1 << 16), // return the natural points of the database
  33. RRDR_OPTION_VIRTUAL_POINTS = (1 << 17), // return virtual points
  34. RRDR_OPTION_ANOMALY_BIT = (1 << 18), // Return the anomaly bit stored in each collected_number
  35. RRDR_OPTION_RETURN_RAW = (1 << 19), // Return raw data for aggregating across multiple nodes
  36. RRDR_OPTION_RETURN_JWAR = (1 << 20), // Return anomaly rates in jsonwrap
  37. RRDR_OPTION_SELECTED_TIER = (1 << 21), // Use the selected tier for the query
  38. RRDR_OPTION_ALL_DIMENSIONS = (1 << 22), // Return the full dimensions list
  39. RRDR_OPTION_SHOW_DETAILS = (1 << 23), // v2 returns detailed object tree
  40. RRDR_OPTION_DEBUG = (1 << 24), // v2 returns request description
  41. RRDR_OPTION_MINIFY = (1 << 25), // remove JSON spaces and newlines from JSON output
  42. RRDR_OPTION_GROUP_BY_LABELS = (1 << 26), // v2 returns flattened labels per dimension of the chart
  43. // internal ones - not to be exposed to the API
  44. RRDR_OPTION_HEALTH_RSRVD1 = (1 << 30), // reserved for RRDCALC_OPTION_NO_CLEAR_NOTIFICATION
  45. RRDR_OPTION_INTERNAL_AR = (1 << 31), // internal use only, to let the formatters know we want to render the anomaly rate
  46. } RRDR_OPTIONS;
  47. typedef enum context_v2_options {
  48. CONTEXT_V2_OPTION_MINIFY = (1 << 0), // remove JSON spaces and newlines from JSON output
  49. CONTEXT_V2_OPTION_DEBUG = (1 << 1), // show the request
  50. CONTEXT_V2_OPTION_ALERTS_WITH_CONFIGURATIONS = (1 << 2), // include alert configurations (used by /api/v2/alert_transitions)
  51. CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES = (1 << 3), // include alert instances (used by /api/v2/alerts)
  52. CONTEXT_V2_OPTION_ALERTS_WITH_VALUES = (1 << 4), // include alert latest values (used by /api/v2/alerts)
  53. CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY = (1 << 5), // include alerts summary counters (used by /api/v2/alerts)
  54. } CONTEXTS_V2_OPTIONS;
  55. typedef enum context_v2_alert_status {
  56. CONTEXT_V2_ALERT_UNINITIALIZED = (1 << 5), // include UNINITIALIZED alerts
  57. CONTEXT_V2_ALERT_UNDEFINED = (1 << 6), // include UNDEFINED alerts
  58. CONTEXT_V2_ALERT_CLEAR = (1 << 7), // include CLEAR alerts
  59. CONTEXT_V2_ALERT_RAISED = (1 << 8), // include WARNING & CRITICAL alerts
  60. CONTEXT_V2_ALERT_WARNING = (1 << 9), // include WARNING alerts
  61. CONTEXT_V2_ALERT_CRITICAL = (1 << 10), // include CRITICAL alerts
  62. } CONTEXTS_V2_ALERT_STATUS;
  63. #define CONTEXTS_V2_ALERT_STATUSES (CONTEXT_V2_ALERT_UNINITIALIZED|CONTEXT_V2_ALERT_UNDEFINED|CONTEXT_V2_ALERT_CLEAR|CONTEXT_V2_ALERT_RAISED|CONTEXT_V2_ALERT_WARNING|CONTEXT_V2_ALERT_CRITICAL)
  64. typedef enum __attribute__ ((__packed__)) rrdr_value_flag {
  65. // IMPORTANT:
  66. // THIS IS AN AGREED BIT MAP BETWEEN AGENT, CLOUD FRONT-END AND CLOUD BACK-END
  67. // DO NOT CHANGE THE MAPPINGS !
  68. RRDR_VALUE_NOTHING = 0, // no flag set (a good default)
  69. RRDR_VALUE_EMPTY = (1 << 0), // the database value is empty
  70. RRDR_VALUE_RESET = (1 << 1), // the database value is marked as reset (overflown)
  71. RRDR_VALUE_PARTIAL = (1 << 2), // the database provides partial data about this point (used in group-by)
  72. } RRDR_VALUE_FLAGS;
  73. typedef enum __attribute__ ((__packed__)) rrdr_dimension_flag {
  74. RRDR_DIMENSION_DEFAULT = 0,
  75. RRDR_DIMENSION_HIDDEN = (1 << 0), // the dimension is hidden (not to be presented to callers)
  76. RRDR_DIMENSION_NONZERO = (1 << 1), // the dimension is non zero (contains non-zero values)
  77. RRDR_DIMENSION_SELECTED = (1 << 2), // the dimension has been selected for query
  78. RRDR_DIMENSION_QUERIED = (1 << 3), // the dimension has been queried
  79. RRDR_DIMENSION_FAILED = (1 << 4), // the dimension failed to be queried
  80. RRDR_DIMENSION_GROUPED = (1 << 5), // the dimension has been grouped in this RRDR
  81. } RRDR_DIMENSION_FLAGS;
  82. // RRDR result options
  83. typedef enum __attribute__ ((__packed__)) rrdr_result_flags {
  84. RRDR_RESULT_FLAG_ABSOLUTE = (1 << 0), // the query uses absolute time-frames
  85. // (can be cached by browsers and proxies)
  86. RRDR_RESULT_FLAG_RELATIVE = (1 << 1), // the query uses relative time-frames
  87. // (should not to be cached by browsers and proxies)
  88. RRDR_RESULT_FLAG_CANCEL = (1 << 2), // the query needs to be cancelled
  89. } RRDR_RESULT_FLAGS;
  90. #define RRDR_DVIEW_ANOMALY_COUNT_MULTIPLIER 1000.0
  91. typedef struct rrdresult {
  92. size_t d; // the number of dimensions
  93. size_t n; // the number of values in the arrays (number of points per dimension)
  94. size_t rows; // the number of actual rows used
  95. RRDR_DIMENSION_FLAGS *od; // the options for the dimensions
  96. STRING **di; // array of d dimension ids
  97. STRING **dn; // array of d dimension names
  98. STRING **du; // array of d dimension units
  99. uint32_t *dgbs; // array of d dimension group by slots - NOT ALLOCATED when RRDR is created
  100. uint32_t *dgbc; // array of d dimension group by counts - NOT ALLOCATED when RRDR is created
  101. uint32_t *dp; // array of d dimension priority - NOT ALLOCATED when RRDR is created
  102. DICTIONARY **dl; // array of d dimension labels - NOT ALLOCATED when RRDR is created
  103. STORAGE_POINT *dqp; // array of d dimensions query points - NOT ALLOCATED when RRDR is created
  104. STORAGE_POINT *dview; // array of d dimensions group by view - NOT ALLOCATED when RRDR is created
  105. NETDATA_DOUBLE *vh; // array of n x d hidden values, while grouping - NOT ALLOCATED when RRDR is created
  106. DICTIONARY *label_keys;
  107. time_t *t; // array of n timestamps
  108. NETDATA_DOUBLE *v; // array n x d values
  109. RRDR_VALUE_FLAGS *o; // array n x d options for each value returned
  110. NETDATA_DOUBLE *ar; // array n x d of anomaly rates (0 - 100)
  111. uint32_t *gbc; // array n x d of group by count - NOT ALLOCATED when RRDR is created
  112. struct {
  113. size_t group; // how many collected values were grouped for each row - NEEDED BY GROUPING FUNCTIONS
  114. time_t after;
  115. time_t before;
  116. time_t update_every; // what is the suggested update frequency in seconds
  117. NETDATA_DOUBLE min;
  118. NETDATA_DOUBLE max;
  119. RRDR_RESULT_FLAGS flags; // RRDR_RESULT_FLAG_*
  120. } view;
  121. struct {
  122. size_t db_points_read;
  123. size_t result_points_generated;
  124. } stats;
  125. struct {
  126. void *data; // the internal data of the grouping function
  127. // grouping function pointers
  128. RRDR_TIME_GROUPING add_flush;
  129. void (*create)(struct rrdresult *r, const char *options);
  130. void (*reset)(struct rrdresult *r);
  131. void (*free)(struct rrdresult *r);
  132. void (*add)(struct rrdresult *r, NETDATA_DOUBLE value);
  133. NETDATA_DOUBLE (*flush)(struct rrdresult *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr);
  134. TIER_QUERY_FETCH tier_query_fetch; // which value to use from STORAGE_POINT
  135. size_t points_wanted; // used by SES and DES
  136. size_t resampling_group; // used by AVERAGE
  137. NETDATA_DOUBLE resampling_divisor; // used by AVERAGE
  138. } time_grouping;
  139. struct {
  140. struct rrdresult *r;
  141. } group_by;
  142. struct {
  143. time_t max_update_every;
  144. time_t expected_after;
  145. time_t trimmed_after;
  146. } partial_data_trimming;
  147. struct {
  148. ONEWAYALLOC *owa; // the allocator used
  149. struct query_target *qt; // the QUERY_TARGET
  150. size_t contexts; // temp needed between json_wrapper_begin2() and json_wrapper_end2()
  151. size_t queries_count; // temp needed to know if a query is the first executed
  152. #ifdef NETDATA_INTERNAL_CHECKS
  153. const char *log;
  154. #endif
  155. struct query_target *release_with_rrdr_qt;
  156. } internal;
  157. } RRDR;
  158. #define rrdr_rows(r) ((r)->rows)
  159. #include "database/rrd.h"
  160. void rrdr_free(ONEWAYALLOC *owa, RRDR *r);
  161. RRDR *rrdr_create(ONEWAYALLOC *owa, struct query_target *qt, size_t dimensions, size_t points);
  162. #include "../web_api_v1.h"
  163. #include "web/api/queries/query.h"
  164. RRDR *rrd2rrdr_legacy(
  165. ONEWAYALLOC *owa,
  166. RRDSET *st, size_t points, time_t after, time_t before,
  167. RRDR_TIME_GROUPING group_method, time_t resampling_time, RRDR_OPTIONS options, const char *dimensions,
  168. const char *group_options, time_t timeout_ms, size_t tier, QUERY_SOURCE query_source,
  169. STORAGE_PRIORITY priority);
  170. RRDR *rrd2rrdr(ONEWAYALLOC *owa, struct query_target *qt);
  171. bool query_target_calculate_window(struct query_target *qt);
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif //NETDATA_QUERIES_RRDR_H