rrdr.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 __attribute__ ((__packed__)) rrdr_value_flag {
  48. // IMPORTANT:
  49. // THIS IS AN AGREED BIT MAP BETWEEN AGENT, CLOUD FRONT-END AND CLOUD BACK-END
  50. // DO NOT CHANGE THE MAPPINGS !
  51. RRDR_VALUE_NOTHING = 0, // no flag set (a good default)
  52. RRDR_VALUE_EMPTY = (1 << 0), // the database value is empty
  53. RRDR_VALUE_RESET = (1 << 1), // the database value is marked as reset (overflown)
  54. RRDR_VALUE_PARTIAL = (1 << 2), // the database provides partial data about this point (used in group-by)
  55. } RRDR_VALUE_FLAGS;
  56. typedef enum __attribute__ ((__packed__)) rrdr_dimension_flag {
  57. RRDR_DIMENSION_DEFAULT = 0,
  58. RRDR_DIMENSION_HIDDEN = (1 << 0), // the dimension is hidden (not to be presented to callers)
  59. RRDR_DIMENSION_NONZERO = (1 << 1), // the dimension is non zero (contains non-zero values)
  60. RRDR_DIMENSION_SELECTED = (1 << 2), // the dimension has been selected for query
  61. RRDR_DIMENSION_QUERIED = (1 << 3), // the dimension has been queried
  62. RRDR_DIMENSION_FAILED = (1 << 4), // the dimension failed to be queried
  63. RRDR_DIMENSION_GROUPED = (1 << 5), // the dimension has been grouped in this RRDR
  64. } RRDR_DIMENSION_FLAGS;
  65. // RRDR result options
  66. typedef enum __attribute__ ((__packed__)) rrdr_result_flags {
  67. RRDR_RESULT_FLAG_ABSOLUTE = (1 << 0), // the query uses absolute time-frames
  68. // (can be cached by browsers and proxies)
  69. RRDR_RESULT_FLAG_RELATIVE = (1 << 1), // the query uses relative time-frames
  70. // (should not to be cached by browsers and proxies)
  71. RRDR_RESULT_FLAG_CANCEL = (1 << 2), // the query needs to be cancelled
  72. } RRDR_RESULT_FLAGS;
  73. #define RRDR_DVIEW_ANOMALY_COUNT_MULTIPLIER 1000.0
  74. typedef struct rrdresult {
  75. size_t d; // the number of dimensions
  76. size_t n; // the number of values in the arrays (number of points per dimension)
  77. size_t rows; // the number of actual rows used
  78. RRDR_DIMENSION_FLAGS *od; // the options for the dimensions
  79. STRING **di; // array of d dimension ids
  80. STRING **dn; // array of d dimension names
  81. STRING **du; // array of d dimension units
  82. uint32_t *dgbs; // array of d dimension group by slots - NOT ALLOCATED when RRDR is created
  83. uint32_t *dgbc; // array of d dimension group by counts - NOT ALLOCATED when RRDR is created
  84. uint32_t *dp; // array of d dimension priority - NOT ALLOCATED when RRDR is created
  85. DICTIONARY **dl; // array of d dimension labels - NOT ALLOCATED when RRDR is created
  86. STORAGE_POINT *dqp; // array of d dimensions query points - NOT ALLOCATED when RRDR is created
  87. STORAGE_POINT *dview; // array of d dimensions group by view - NOT ALLOCATED when RRDR is created
  88. NETDATA_DOUBLE *vh; // array of n x d hidden values, while grouping - NOT ALLOCATED when RRDR is created
  89. DICTIONARY *label_keys;
  90. time_t *t; // array of n timestamps
  91. NETDATA_DOUBLE *v; // array n x d values
  92. RRDR_VALUE_FLAGS *o; // array n x d options for each value returned
  93. NETDATA_DOUBLE *ar; // array n x d of anomaly rates (0 - 100)
  94. uint32_t *gbc; // array n x d of group by count - NOT ALLOCATED when RRDR is created
  95. struct {
  96. size_t group; // how many collected values were grouped for each row - NEEDED BY GROUPING FUNCTIONS
  97. time_t after;
  98. time_t before;
  99. time_t update_every; // what is the suggested update frequency in seconds
  100. NETDATA_DOUBLE min;
  101. NETDATA_DOUBLE max;
  102. RRDR_RESULT_FLAGS flags; // RRDR_RESULT_FLAG_*
  103. } view;
  104. struct {
  105. size_t db_points_read;
  106. size_t result_points_generated;
  107. } stats;
  108. struct {
  109. void *data; // the internal data of the grouping function
  110. // grouping function pointers
  111. RRDR_TIME_GROUPING add_flush;
  112. void (*create)(struct rrdresult *r, const char *options);
  113. void (*reset)(struct rrdresult *r);
  114. void (*free)(struct rrdresult *r);
  115. void (*add)(struct rrdresult *r, NETDATA_DOUBLE value);
  116. NETDATA_DOUBLE (*flush)(struct rrdresult *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr);
  117. TIER_QUERY_FETCH tier_query_fetch; // which value to use from STORAGE_POINT
  118. size_t points_wanted; // used by SES and DES
  119. size_t resampling_group; // used by AVERAGE
  120. NETDATA_DOUBLE resampling_divisor; // used by AVERAGE
  121. } time_grouping;
  122. struct {
  123. struct rrdresult *r;
  124. } group_by;
  125. struct {
  126. time_t max_update_every;
  127. time_t expected_after;
  128. time_t trimmed_after;
  129. } partial_data_trimming;
  130. struct {
  131. ONEWAYALLOC *owa; // the allocator used
  132. struct query_target *qt; // the QUERY_TARGET
  133. size_t contexts; // temp needed between json_wrapper_begin2() and json_wrapper_end2()
  134. size_t queries_count; // temp needed to know if a query is the first executed
  135. #ifdef NETDATA_INTERNAL_CHECKS
  136. const char *log;
  137. #endif
  138. struct query_target *release_with_rrdr_qt;
  139. } internal;
  140. } RRDR;
  141. #define rrdr_rows(r) ((r)->rows)
  142. #include "database/rrd.h"
  143. void rrdr_free(ONEWAYALLOC *owa, RRDR *r);
  144. RRDR *rrdr_create(ONEWAYALLOC *owa, struct query_target *qt, size_t dimensions, size_t points);
  145. #include "../web_api_v1.h"
  146. #include "web/api/queries/query.h"
  147. RRDR *rrd2rrdr_legacy(
  148. ONEWAYALLOC *owa,
  149. RRDSET *st, size_t points, time_t after, time_t before,
  150. RRDR_TIME_GROUPING group_method, time_t resampling_time, RRDR_OPTIONS options, const char *dimensions,
  151. const char *group_options, time_t timeout_ms, size_t tier, QUERY_SOURCE query_source,
  152. STORAGE_PRIORITY priority);
  153. RRDR *rrd2rrdr(ONEWAYALLOC *owa, struct query_target *qt);
  154. bool query_target_calculate_window(struct query_target *qt);
  155. bool rrdr_relative_window_to_absolute(time_t *after, time_t *before, time_t *now_ptr);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif //NETDATA_QUERIES_RRDR_H