rrdcontext.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDCONTEXT_H
  3. #define NETDATA_RRDCONTEXT_H 1
  4. // ----------------------------------------------------------------------------
  5. // RRDMETRIC
  6. typedef struct rrdmetric_acquired RRDMETRIC_ACQUIRED;
  7. // ----------------------------------------------------------------------------
  8. // RRDINSTANCE
  9. typedef struct rrdinstance_acquired RRDINSTANCE_ACQUIRED;
  10. // ----------------------------------------------------------------------------
  11. // RRDCONTEXT
  12. typedef struct rrdcontext_acquired RRDCONTEXT_ACQUIRED;
  13. // ----------------------------------------------------------------------------
  14. #include "../rrd.h"
  15. bool rrdinstance_acquired_id_and_name_are_same(RRDINSTANCE_ACQUIRED *ria);
  16. const char *rrdmetric_acquired_id(RRDMETRIC_ACQUIRED *rma);
  17. const char *rrdmetric_acquired_name(RRDMETRIC_ACQUIRED *rma);
  18. bool rrdmetric_acquired_has_name(RRDMETRIC_ACQUIRED *rma);
  19. STRING *rrdmetric_acquired_id_dup(RRDMETRIC_ACQUIRED *rma);
  20. STRING *rrdmetric_acquired_name_dup(RRDMETRIC_ACQUIRED *rma);
  21. NETDATA_DOUBLE rrdmetric_acquired_last_stored_value(RRDMETRIC_ACQUIRED *rma);
  22. time_t rrdmetric_acquired_first_entry(RRDMETRIC_ACQUIRED *rma);
  23. time_t rrdmetric_acquired_last_entry(RRDMETRIC_ACQUIRED *rma);
  24. bool rrdmetric_acquired_belongs_to_instance(RRDMETRIC_ACQUIRED *rma, RRDINSTANCE_ACQUIRED *ria);
  25. const char *rrdinstance_acquired_id(RRDINSTANCE_ACQUIRED *ria);
  26. const char *rrdinstance_acquired_name(RRDINSTANCE_ACQUIRED *ria);
  27. bool rrdinstance_acquired_has_name(RRDINSTANCE_ACQUIRED *ria);
  28. const char *rrdinstance_acquired_units(RRDINSTANCE_ACQUIRED *ria);
  29. STRING *rrdinstance_acquired_units_dup(RRDINSTANCE_ACQUIRED *ria);
  30. DICTIONARY *rrdinstance_acquired_labels(RRDINSTANCE_ACQUIRED *ria);
  31. DICTIONARY *rrdinstance_acquired_functions(RRDINSTANCE_ACQUIRED *ria);
  32. RRDHOST *rrdinstance_acquired_rrdhost(RRDINSTANCE_ACQUIRED *ria);
  33. RRDSET *rrdinstance_acquired_rrdset(RRDINSTANCE_ACQUIRED *ria);
  34. bool rrdinstance_acquired_belongs_to_context(RRDINSTANCE_ACQUIRED *ria, RRDCONTEXT_ACQUIRED *rca);
  35. time_t rrdinstance_acquired_update_every(RRDINSTANCE_ACQUIRED *ria);
  36. const char *rrdcontext_acquired_units(RRDCONTEXT_ACQUIRED *rca);
  37. const char *rrdcontext_acquired_title(RRDCONTEXT_ACQUIRED *rca);
  38. RRDSET_TYPE rrdcontext_acquired_chart_type(RRDCONTEXT_ACQUIRED *rca);
  39. // ----------------------------------------------------------------------------
  40. // public API for rrdhost
  41. void rrdhost_load_rrdcontext_data(RRDHOST *host);
  42. void rrdhost_create_rrdcontexts(RRDHOST *host);
  43. void rrdhost_destroy_rrdcontexts(RRDHOST *host);
  44. void rrdcontext_host_child_connected(RRDHOST *host);
  45. void rrdcontext_host_child_disconnected(RRDHOST *host);
  46. int rrdcontext_foreach_instance_with_rrdset_in_context(RRDHOST *host, const char *context, int (*callback)(RRDSET *st, void *data), void *data);
  47. typedef enum {
  48. RRDCONTEXT_OPTION_NONE = 0,
  49. RRDCONTEXT_OPTION_SHOW_METRICS = (1 << 0),
  50. RRDCONTEXT_OPTION_SHOW_INSTANCES = (1 << 1),
  51. RRDCONTEXT_OPTION_SHOW_LABELS = (1 << 2),
  52. RRDCONTEXT_OPTION_SHOW_QUEUED = (1 << 3),
  53. RRDCONTEXT_OPTION_SHOW_FLAGS = (1 << 4),
  54. RRDCONTEXT_OPTION_SHOW_DELETED = (1 << 5),
  55. RRDCONTEXT_OPTION_DEEPSCAN = (1 << 6),
  56. RRDCONTEXT_OPTION_SHOW_UUIDS = (1 << 7),
  57. RRDCONTEXT_OPTION_SHOW_HIDDEN = (1 << 8),
  58. RRDCONTEXT_OPTION_SKIP_ID = (1 << 31), // internal use
  59. } RRDCONTEXT_TO_JSON_OPTIONS;
  60. #define RRDCONTEXT_OPTIONS_ALL (RRDCONTEXT_OPTION_SHOW_METRICS|RRDCONTEXT_OPTION_SHOW_INSTANCES|RRDCONTEXT_OPTION_SHOW_LABELS|RRDCONTEXT_OPTION_SHOW_QUEUED|RRDCONTEXT_OPTION_SHOW_FLAGS|RRDCONTEXT_OPTION_SHOW_DELETED|RRDCONTEXT_OPTION_SHOW_UUIDS|RRDCONTEXT_OPTION_SHOW_HIDDEN)
  61. int rrdcontext_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, const char *context, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  62. int rrdcontexts_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  63. // ----------------------------------------------------------------------------
  64. // public API for rrdcontexts
  65. const char *rrdcontext_acquired_id(RRDCONTEXT_ACQUIRED *rca);
  66. bool rrdcontext_acquired_belongs_to_host(RRDCONTEXT_ACQUIRED *rca, RRDHOST *host);
  67. // ----------------------------------------------------------------------------
  68. // public API for rrddims
  69. void rrdcontext_updated_rrddim(RRDDIM *rd);
  70. void rrdcontext_removed_rrddim(RRDDIM *rd);
  71. void rrdcontext_updated_rrddim_algorithm(RRDDIM *rd);
  72. void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd);
  73. void rrdcontext_updated_rrddim_divisor(RRDDIM *rd);
  74. void rrdcontext_updated_rrddim_flags(RRDDIM *rd);
  75. void rrdcontext_collected_rrddim(RRDDIM *rd);
  76. int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid);
  77. // ----------------------------------------------------------------------------
  78. // public API for rrdsets
  79. void rrdcontext_updated_rrdset(RRDSET *st);
  80. void rrdcontext_removed_rrdset(RRDSET *st);
  81. void rrdcontext_updated_rrdset_name(RRDSET *st);
  82. void rrdcontext_updated_rrdset_flags(RRDSET *st);
  83. void rrdcontext_updated_retention_rrdset(RRDSET *st);
  84. void rrdcontext_collected_rrdset(RRDSET *st);
  85. int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid);
  86. // ----------------------------------------------------------------------------
  87. // public API for ACLK
  88. void rrdcontext_hub_checkpoint_command(void *cmd);
  89. void rrdcontext_hub_stop_streaming_command(void *cmd);
  90. // ----------------------------------------------------------------------------
  91. // public API for threads
  92. void rrdcontext_db_rotation(void);
  93. void *rrdcontext_main(void *);
  94. // ----------------------------------------------------------------------------
  95. // public API for queries
  96. typedef enum __attribute__ ((__packed__)) {
  97. QUERY_STATUS_NONE = 0,
  98. QUERY_STATUS_QUERIED = (1 << 0),
  99. QUERY_STATUS_DIMENSION_HIDDEN = (1 << 1),
  100. QUERY_STATUS_EXCLUDED = (1 << 2),
  101. QUERY_STATUS_FAILED = (1 << 3),
  102. } QUERY_STATUS;
  103. typedef struct query_plan_entry {
  104. size_t tier;
  105. time_t after;
  106. time_t before;
  107. } QUERY_PLAN_ENTRY;
  108. #define QUERY_PLANS_MAX (RRD_STORAGE_TIERS)
  109. typedef struct query_metrics_counts { // counts the number of metrics related to an object
  110. size_t selected; // selected to be queried
  111. size_t excluded; // not selected to be queried
  112. size_t queried; // successfully queried
  113. size_t failed; // failed to be queried
  114. } QUERY_METRICS_COUNTS;
  115. typedef struct query_instances_counts { // counts the number of instances related to an object
  116. size_t selected; // selected to be queried
  117. size_t excluded; // not selected to be queried
  118. size_t queried; // successfully queried
  119. size_t failed; // failed to be queried
  120. } QUERY_INSTANCES_COUNTS;
  121. typedef struct query_alerts_counts { // counts the number of alerts related to an object
  122. size_t clear; // number of alerts in clear state
  123. size_t warning; // number of alerts in warning state
  124. size_t critical; // number of alerts in critical state
  125. size_t other; // number of alerts in any other state
  126. } QUERY_ALERTS_COUNTS;
  127. typedef struct query_node {
  128. uint32_t slot;
  129. RRDHOST *rrdhost;
  130. char node_id[UUID_STR_LEN];
  131. usec_t duration_ut;
  132. STORAGE_POINT query_points;
  133. QUERY_INSTANCES_COUNTS instances;
  134. QUERY_METRICS_COUNTS metrics;
  135. QUERY_ALERTS_COUNTS alerts;
  136. } QUERY_NODE;
  137. typedef struct query_context {
  138. uint32_t slot;
  139. RRDCONTEXT_ACQUIRED *rca;
  140. STORAGE_POINT query_points;
  141. QUERY_INSTANCES_COUNTS instances;
  142. QUERY_METRICS_COUNTS metrics;
  143. QUERY_ALERTS_COUNTS alerts;
  144. } QUERY_CONTEXT;
  145. typedef struct query_instance {
  146. uint32_t slot;
  147. uint32_t query_host_id;
  148. RRDINSTANCE_ACQUIRED *ria;
  149. STRING *id_fqdn; // never access this directly - it is created on demand via query_instance_id_fqdn()
  150. STRING *name_fqdn; // never access this directly - it is created on demand via query_instance_name_fqdn()
  151. STORAGE_POINT query_points;
  152. QUERY_METRICS_COUNTS metrics;
  153. QUERY_ALERTS_COUNTS alerts;
  154. } QUERY_INSTANCE;
  155. typedef struct query_dimension {
  156. uint32_t slot;
  157. uint32_t priority;
  158. RRDMETRIC_ACQUIRED *rma;
  159. QUERY_STATUS status;
  160. } QUERY_DIMENSION;
  161. typedef struct query_metric {
  162. RRDR_DIMENSION_FLAGS status;
  163. struct query_metric_tier {
  164. STORAGE_METRIC_HANDLE *db_metric_handle;
  165. time_t db_first_time_s; // the oldest timestamp available for this tier
  166. time_t db_last_time_s; // the latest timestamp available for this tier
  167. time_t db_update_every_s; // latest update every for this tier
  168. long weight;
  169. } tiers[RRD_STORAGE_TIERS];
  170. struct {
  171. size_t used;
  172. QUERY_PLAN_ENTRY array[QUERY_PLANS_MAX];
  173. } plan;
  174. struct {
  175. uint32_t query_node_id;
  176. uint32_t query_context_id;
  177. uint32_t query_instance_id;
  178. uint32_t query_dimension_id;
  179. } link;
  180. STORAGE_POINT query_points;
  181. struct {
  182. uint32_t slot;
  183. uint32_t first_slot;
  184. STRING *id;
  185. STRING *name;
  186. STRING *units;
  187. } grouped_as;
  188. usec_t duration_ut;
  189. } QUERY_METRIC;
  190. #define MAX_QUERY_TARGET_ID_LENGTH 255
  191. #define MAX_QUERY_GROUP_BY_PASSES 2
  192. typedef bool (*qt_interrupt_callback_t)(void *data);
  193. struct group_by_pass {
  194. RRDR_GROUP_BY group_by;
  195. char *group_by_label;
  196. RRDR_GROUP_BY_FUNCTION aggregation;
  197. };
  198. typedef struct query_target_request {
  199. size_t version;
  200. const char *scope_nodes;
  201. const char *scope_contexts;
  202. // selecting / filtering metrics to be queried
  203. RRDHOST *host; // the host to be queried (can be NULL, hosts will be used)
  204. RRDCONTEXT_ACQUIRED *rca; // the context to be queried (can be NULL)
  205. RRDINSTANCE_ACQUIRED *ria; // the instance to be queried (can be NULL)
  206. RRDMETRIC_ACQUIRED *rma; // the metric to be queried (can be NULL)
  207. RRDSET *st; // the chart to be queried (NULL, for context queries)
  208. const char *nodes; // hosts simple pattern
  209. const char *contexts; // contexts simple pattern (context queries)
  210. const char *instances; // charts simple pattern (for context queries)
  211. const char *dimensions; // dimensions simple pattern
  212. const char *chart_label_key; // select only the chart having this label key
  213. const char *labels; // select only the charts having this combo of label key:value
  214. const char *alerts; // select only the charts having this combo of alert name:status
  215. time_t after; // the requested timeframe
  216. time_t before; // the requested timeframe
  217. size_t points; // the requested number of points to be returned
  218. uint32_t format; // DATASOURCE_FORMAT
  219. RRDR_OPTIONS options;
  220. time_t timeout_ms; // the timeout of the query in milliseconds
  221. size_t tier;
  222. QUERY_SOURCE query_source;
  223. STORAGE_PRIORITY priority;
  224. // resampling metric values across time
  225. time_t resampling_time;
  226. // grouping metric values across time
  227. RRDR_TIME_GROUPING time_group_method;
  228. const char *time_group_options;
  229. // group by across multiple time-series
  230. struct group_by_pass group_by[MAX_QUERY_GROUP_BY_PASSES];
  231. usec_t received_ut;
  232. qt_interrupt_callback_t interrupt_callback;
  233. void *interrupt_callback_data;
  234. } QUERY_TARGET_REQUEST;
  235. #define GROUP_BY_MAX_LABEL_KEYS 10
  236. struct query_tier_statistics {
  237. size_t queries;
  238. size_t points;
  239. time_t update_every;
  240. struct {
  241. time_t first_time_s;
  242. time_t last_time_s;
  243. } retention;
  244. };
  245. struct query_versions {
  246. uint64_t contexts_hard_hash;
  247. uint64_t contexts_soft_hash;
  248. uint64_t alerts_hard_hash;
  249. uint64_t alerts_soft_hash;
  250. };
  251. struct query_timings {
  252. usec_t received_ut;
  253. usec_t preprocessed_ut;
  254. usec_t executed_ut;
  255. usec_t finished_ut;
  256. };
  257. #define query_view_update_every(qt) ((qt)->window.group * (qt)->window.query_granularity)
  258. typedef struct query_target {
  259. char id[MAX_QUERY_TARGET_ID_LENGTH + 1]; // query identifier (for logging)
  260. QUERY_TARGET_REQUEST request;
  261. struct {
  262. time_t now; // the current timestamp, the absolute max for any query timestamp
  263. bool relative; // true when the request made with relative timestamps, true if it was absolute
  264. bool aligned;
  265. time_t after; // the absolute timestamp this query is about
  266. time_t before; // the absolute timestamp this query is about
  267. time_t query_granularity;
  268. size_t points; // the number of points the query will return (maybe different from the request)
  269. size_t group;
  270. RRDR_TIME_GROUPING time_group_method;
  271. const char *time_group_options;
  272. size_t resampling_group;
  273. NETDATA_DOUBLE resampling_divisor;
  274. RRDR_OPTIONS options;
  275. size_t tier;
  276. } window;
  277. struct {
  278. size_t queries[RRD_STORAGE_TIERS];
  279. time_t first_time_s; // the combined first_time_t of all metrics in the query, across all tiers
  280. time_t last_time_s; // the combined last_time_T of all metrics in the query, across all tiers
  281. time_t minimum_latest_update_every_s; // the min update every of the metrics in the query
  282. struct query_tier_statistics tiers[RRD_STORAGE_TIERS];
  283. } db;
  284. struct {
  285. QUERY_METRIC *array; // the metrics to be queried (all of them should be queried, no exceptions)
  286. uint32_t used; // how many items of the array are used
  287. uint32_t size; // the size of the array
  288. SIMPLE_PATTERN *pattern;
  289. } query;
  290. struct {
  291. QUERY_DIMENSION *array;
  292. uint32_t used; // how many items of the array are used
  293. uint32_t size; // the size of the array
  294. } dimensions;
  295. struct {
  296. QUERY_INSTANCE *array;
  297. uint32_t used; // how many items of the array are used
  298. uint32_t size; // the size of the array
  299. SIMPLE_PATTERN *pattern;
  300. SIMPLE_PATTERN *labels_pattern;
  301. SIMPLE_PATTERN *alerts_pattern;
  302. SIMPLE_PATTERN *chart_label_key_pattern;
  303. } instances;
  304. struct {
  305. QUERY_CONTEXT *array;
  306. uint32_t used; // how many items of the array are used
  307. uint32_t size; // the size of the array
  308. SIMPLE_PATTERN *pattern;
  309. SIMPLE_PATTERN *scope_pattern;
  310. } contexts;
  311. struct {
  312. QUERY_NODE *array;
  313. uint32_t used; // how many items of the array are used
  314. uint32_t size; // the size of the array
  315. SIMPLE_PATTERN *pattern;
  316. SIMPLE_PATTERN *scope_pattern;
  317. } nodes;
  318. struct {
  319. size_t used;
  320. char *label_keys[GROUP_BY_MAX_LABEL_KEYS * MAX_QUERY_GROUP_BY_PASSES];
  321. } group_by[MAX_QUERY_GROUP_BY_PASSES];
  322. STORAGE_POINT query_points;
  323. struct query_versions versions;
  324. struct query_timings timings;
  325. struct {
  326. SPINLOCK spinlock;
  327. bool used; // when true, this query is currently being used
  328. size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries
  329. struct query_target *prev;
  330. struct query_target *next;
  331. } internal;
  332. } QUERY_TARGET;
  333. static inline NEVERNULL QUERY_NODE *query_node(QUERY_TARGET *qt, size_t id) {
  334. internal_fatal(id >= qt->nodes.used, "QUERY: invalid query host id");
  335. return &qt->nodes.array[id];
  336. }
  337. static inline NEVERNULL QUERY_CONTEXT *query_context(QUERY_TARGET *qt, size_t query_context_id) {
  338. internal_fatal(query_context_id >= qt->contexts.used, "QUERY: invalid query context id");
  339. return &qt->contexts.array[query_context_id];
  340. }
  341. static inline NEVERNULL QUERY_INSTANCE *query_instance(QUERY_TARGET *qt, size_t query_instance_id) {
  342. internal_fatal(query_instance_id >= qt->instances.used, "QUERY: invalid query instance id");
  343. return &qt->instances.array[query_instance_id];
  344. }
  345. static inline NEVERNULL QUERY_DIMENSION *query_dimension(QUERY_TARGET *qt, size_t query_dimension_id) {
  346. internal_fatal(query_dimension_id >= qt->dimensions.used, "QUERY: invalid query dimension id");
  347. return &qt->dimensions.array[query_dimension_id];
  348. }
  349. static inline NEVERNULL QUERY_METRIC *query_metric(QUERY_TARGET *qt, size_t id) {
  350. internal_fatal(id >= qt->query.used, "QUERY: invalid query metric id");
  351. return &qt->query.array[id];
  352. }
  353. static inline const char *query_metric_id(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  354. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  355. return rrdmetric_acquired_id(qd->rma);
  356. }
  357. static inline const char *query_metric_name(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  358. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  359. return rrdmetric_acquired_name(qd->rma);
  360. }
  361. struct storage_engine *query_metric_storage_engine(QUERY_TARGET *qt, QUERY_METRIC *qm, size_t tier);
  362. STRING *query_instance_id_fqdn(QUERY_INSTANCE *qi, size_t version);
  363. STRING *query_instance_name_fqdn(QUERY_INSTANCE *qi, size_t version);
  364. void query_target_free(void);
  365. void query_target_release(QUERY_TARGET *qt);
  366. QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr);
  367. struct api_v2_contexts_request {
  368. char *scope_nodes;
  369. char *scope_contexts;
  370. char *nodes;
  371. char *contexts;
  372. char *q;
  373. time_t timeout_ms;
  374. qt_interrupt_callback_t interrupt_callback;
  375. void *interrupt_callback_data;
  376. };
  377. typedef enum __attribute__ ((__packed__)) {
  378. CONTEXTS_V2_DEBUG = (1 << 0),
  379. CONTEXTS_V2_SEARCH = (1 << 1),
  380. CONTEXTS_V2_NODES = (1 << 2),
  381. CONTEXTS_V2_NODES_DETAILED = (1 << 3),
  382. CONTEXTS_V2_CONTEXTS = (1 << 4),
  383. } CONTEXTS_V2_OPTIONS;
  384. int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_OPTIONS options);
  385. RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o);
  386. void buffer_json_agents_array_v2(BUFFER *wb, struct query_timings *timings, time_t now_s);
  387. void buffer_json_node_add_v2(BUFFER *wb, RRDHOST *host, size_t ni, usec_t duration_ut);
  388. void buffer_json_query_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  389. void buffer_json_cloud_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  390. // ----------------------------------------------------------------------------
  391. // scope
  392. typedef ssize_t (*foreach_host_cb_t)(void *data, RRDHOST *host, bool queryable);
  393. ssize_t query_scope_foreach_host(SIMPLE_PATTERN *scope_hosts_sp, SIMPLE_PATTERN *hosts_sp,
  394. foreach_host_cb_t cb, void *data,
  395. struct query_versions *versions,
  396. char *host_node_id_str);
  397. typedef ssize_t (*foreach_context_cb_t)(void *data, RRDCONTEXT_ACQUIRED *rca, bool queryable_context);
  398. ssize_t query_scope_foreach_context(RRDHOST *host, const char *scope_contexts, SIMPLE_PATTERN *scope_contexts_sp, SIMPLE_PATTERN *contexts_sp, foreach_context_cb_t cb, bool queryable_host, void *data);
  399. // ----------------------------------------------------------------------------
  400. // public API for weights
  401. typedef ssize_t (*weights_add_metric_t)(void *data, RRDHOST *host, RRDCONTEXT_ACQUIRED *rca, RRDINSTANCE_ACQUIRED *ria, RRDMETRIC_ACQUIRED *rma);
  402. ssize_t weights_foreach_rrdmetric_in_context(RRDCONTEXT_ACQUIRED *rca,
  403. SIMPLE_PATTERN *instances_sp,
  404. SIMPLE_PATTERN *chart_label_key_sp,
  405. SIMPLE_PATTERN *labels_sp,
  406. SIMPLE_PATTERN *alerts_sp,
  407. SIMPLE_PATTERN *dimensions_sp,
  408. bool match_ids, bool match_names,
  409. size_t version,
  410. weights_add_metric_t cb,
  411. void *data);
  412. bool rrdcontext_retention_match(RRDCONTEXT_ACQUIRED *rca, time_t after, time_t before);
  413. #define query_matches_retention(after, before, first_entry_s, last_entry_s, update_every_s) \
  414. (((first_entry_s) - ((update_every_s) * 2) <= (before)) && \
  415. ((last_entry_s) + ((update_every_s) * 2) >= (after)))
  416. #define query_target_aggregatable(qt) ((qt)->window.options & RRDR_OPTION_RETURN_RAW)
  417. static inline bool query_target_has_percentage_of_instance(QUERY_TARGET *qt) {
  418. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++)
  419. if(qt->request.group_by[g].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  420. return true;
  421. return false;
  422. }
  423. static inline bool query_target_needs_all_dimensions(QUERY_TARGET *qt) {
  424. if(qt->request.options & RRDR_OPTION_PERCENTAGE)
  425. return true;
  426. return query_target_has_percentage_of_instance(qt);
  427. }
  428. static inline bool query_target_has_percentage_units(QUERY_TARGET *qt) {
  429. if(qt->window.time_group_method == RRDR_GROUPING_CV)
  430. return true;
  431. if((qt->request.options & RRDR_OPTION_PERCENTAGE) && !(qt->window.options & RRDR_OPTION_RETURN_RAW))
  432. return true;
  433. return query_target_has_percentage_of_instance(qt);
  434. }
  435. #endif // NETDATA_RRDCONTEXT_H