rrdr.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = 0x00000001, // don't output dimensions with just zero values
  17. RRDR_OPTION_REVERSED = 0x00000002, // output the rows in reverse order (oldest to newest)
  18. RRDR_OPTION_ABSOLUTE = 0x00000004, // values positive, for DATASOURCE_SSV before summing
  19. RRDR_OPTION_MIN2MAX = 0x00000008, // when adding dimensions, use max - min, instead of sum
  20. RRDR_OPTION_SECONDS = 0x00000010, // output seconds, instead of dates
  21. RRDR_OPTION_MILLISECONDS = 0x00000020, // output milliseconds, instead of dates
  22. RRDR_OPTION_NULL2ZERO = 0x00000040, // do not show nulls, convert them to zeros
  23. RRDR_OPTION_OBJECTSROWS = 0x00000080, // each row of values should be an object, not an array
  24. RRDR_OPTION_GOOGLE_JSON = 0x00000100, // comply with google JSON/JSONP specs
  25. RRDR_OPTION_JSON_WRAP = 0x00000200, // wrap the response in a JSON header with info about the result
  26. RRDR_OPTION_LABEL_QUOTES = 0x00000400, // in CSV output, wrap header labels in double quotes
  27. RRDR_OPTION_PERCENTAGE = 0x00000800, // give values as percentage of total
  28. RRDR_OPTION_NOT_ALIGNED = 0x00001000, // do not align charts for persistent timeframes
  29. RRDR_OPTION_DISPLAY_ABS = 0x00002000, // for badges, display the absolute value, but calculate colors with sign
  30. RRDR_OPTION_MATCH_IDS = 0x00004000, // when filtering dimensions, match only IDs
  31. RRDR_OPTION_MATCH_NAMES = 0x00008000, // when filtering dimensions, match only names
  32. RRDR_OPTION_NATURAL_POINTS = 0x00020000, // return the natural points of the database
  33. RRDR_OPTION_VIRTUAL_POINTS = 0x00040000, // return virtual points
  34. RRDR_OPTION_ANOMALY_BIT = 0x00080000, // Return the anomaly bit stored in each collected_number
  35. RRDR_OPTION_RETURN_RAW = 0x00100000, // Return raw data for aggregating across multiple nodes
  36. RRDR_OPTION_RETURN_JWAR = 0x00200000, // Return anomaly rates in jsonwrap
  37. RRDR_OPTION_SELECTED_TIER = 0x00400000, // Use the selected tier for the query
  38. RRDR_OPTION_ALL_DIMENSIONS = 0x00800000, // Return the full dimensions list
  39. RRDR_OPTION_SHOW_PLAN = 0x01000000, // Return the query plan in jsonwrap
  40. // internal ones - not to be exposed to the API
  41. RRDR_OPTION_INTERNAL_AR = 0x10000000, // internal use only, to let the formatters we want to render the anomaly rate
  42. RRDR_OPTION_HEALTH_RSRVD1 = 0x80000000, // reserved for RRDCALC_OPTION_NO_CLEAR_NOTIFICATION
  43. } RRDR_OPTIONS;
  44. typedef enum rrdr_value_flag {
  45. RRDR_VALUE_NOTHING = 0x00, // no flag set (a good default)
  46. RRDR_VALUE_EMPTY = 0x01, // the database value is empty
  47. RRDR_VALUE_RESET = 0x02, // the database value is marked as reset (overflown)
  48. } RRDR_VALUE_FLAGS;
  49. typedef enum rrdr_dimension_flag {
  50. RRDR_DIMENSION_DEFAULT = 0x00,
  51. RRDR_DIMENSION_HIDDEN = 0x04, // the dimension is hidden (not to be presented to callers)
  52. RRDR_DIMENSION_NONZERO = 0x08, // the dimension is non zero (contains non-zero values)
  53. RRDR_DIMENSION_QUERIED = 0x10, // the dimension is selected for evaluation in this RRDR
  54. } RRDR_DIMENSION_FLAGS;
  55. // RRDR result options
  56. typedef enum rrdr_result_flags {
  57. RRDR_RESULT_OPTION_ABSOLUTE = 0x00000001, // the query uses absolute time-frames
  58. // (can be cached by browsers and proxies)
  59. RRDR_RESULT_OPTION_RELATIVE = 0x00000002, // the query uses relative time-frames
  60. // (should not to be cached by browsers and proxies)
  61. RRDR_RESULT_OPTION_VARIABLE_STEP = 0x00000004, // the query uses variable-step time-frames
  62. RRDR_RESULT_OPTION_CANCEL = 0x00000008, // the query needs to be cancelled
  63. } RRDR_RESULT_OPTIONS;
  64. typedef struct rrdresult {
  65. RRDR_RESULT_OPTIONS result_options; // RRDR_RESULT_OPTION_*
  66. size_t d; // the number of dimensions
  67. size_t n; // the number of values in the arrays
  68. size_t rows; // the number of rows used
  69. RRDR_DIMENSION_FLAGS *od; // the options for the dimensions
  70. time_t *t; // array of n timestamps
  71. NETDATA_DOUBLE *v; // array n x d values
  72. RRDR_VALUE_FLAGS *o; // array n x d options for each value returned
  73. NETDATA_DOUBLE *ar; // array n x d of anomaly rates (0 - 100)
  74. size_t group; // how many collected values were grouped for each row
  75. time_t update_every; // what is the suggested update frequency in seconds
  76. NETDATA_DOUBLE min;
  77. NETDATA_DOUBLE max;
  78. time_t before;
  79. time_t after;
  80. // internal rrd2rrdr() members below this point
  81. struct {
  82. ONEWAYALLOC *owa; // the allocator used
  83. struct query_target *qt; // the QUERY_TARGET
  84. RRDR_OPTIONS query_options; // RRDR_OPTION_* (as run by the query)
  85. size_t points_wanted; // used by SES and DES
  86. size_t resampling_group; // used by AVERAGE
  87. NETDATA_DOUBLE resampling_divisor; // used by AVERAGE
  88. // grouping function pointers
  89. void (*grouping_create)(struct rrdresult *r, const char *options);
  90. void (*grouping_reset)(struct rrdresult *r);
  91. void (*grouping_free)(struct rrdresult *r);
  92. void (*grouping_add)(struct rrdresult *r, NETDATA_DOUBLE value);
  93. NETDATA_DOUBLE (*grouping_flush)(struct rrdresult *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr);
  94. TIER_QUERY_FETCH tier_query_fetch; // which value to use from STORAGE_POINT
  95. void *grouping_data; // the internal data of the grouping function
  96. #ifdef NETDATA_INTERNAL_CHECKS
  97. const char *log;
  98. #endif
  99. // statistics
  100. size_t db_points_read;
  101. size_t result_points_generated;
  102. size_t tier_points_read[RRD_STORAGE_TIERS];
  103. } internal;
  104. } RRDR;
  105. #define rrdr_rows(r) ((r)->rows)
  106. #include "database/rrd.h"
  107. void rrdr_free(ONEWAYALLOC *owa, RRDR *r);
  108. RRDR *rrdr_create(ONEWAYALLOC *owa, struct query_target *qt);
  109. #include "../web_api_v1.h"
  110. #include "web/api/queries/query.h"
  111. RRDR *rrd2rrdr_legacy(
  112. ONEWAYALLOC *owa,
  113. RRDSET *st, size_t points, time_t after, time_t before,
  114. RRDR_GROUPING group_method, time_t resampling_time, RRDR_OPTIONS options, const char *dimensions,
  115. const char *group_options, time_t timeout, size_t tier, QUERY_SOURCE query_source,
  116. STORAGE_PRIORITY priority);
  117. RRDR *rrd2rrdr(ONEWAYALLOC *owa, struct query_target *qt);
  118. bool query_target_calculate_window(struct query_target *qt);
  119. bool rrdr_relative_window_to_absolute(time_t *after, time_t *before);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif //NETDATA_QUERIES_RRDR_H