master
h 121 lines 4.05 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_QUERY_INTERNAL_H
4 #define NETDATA_QUERY_INTERNAL_H
5
6 #include "query.h"
7 #include "web/api/formatters/rrd2json.h"
8 #include "rrdr.h"
9
10 #define QUERY_PLAN_MIN_POINTS 10
11 #define POINTS_TO_EXPAND_QUERY 5
12
13 typedef struct query_point {
14 STORAGE_POINT sp;
15 NETDATA_DOUBLE value;
16 bool added;
17 #ifdef NETDATA_INTERNAL_CHECKS
18 size_t id;
19 #endif
20 } QUERY_POINT;
21
22 #ifdef NETDATA_INTERNAL_CHECKS
23 #define QUERY_POINT_EMPTY (QUERY_POINT){ \
24 .sp = STORAGE_POINT_UNSET, \
25 .value = NAN, \
26 .added = false, \
27 .id = 0, \
28 }
29 #else
30 #define QUERY_POINT_EMPTY (QUERY_POINT){ \
31 .sp = STORAGE_POINT_UNSET, \
32 .value = NAN, \
33 .added = false, \
34 }
35 #endif
36
37 #ifdef NETDATA_INTERNAL_CHECKS
38 #define query_point_set_id(point, point_id) (point).id = point_id
39 #else
40 #define query_point_set_id(point, point_id) debug_dummy()
41 #endif
42
43 typedef struct query_engine_ops {
44 // configuration
45 RRDR *r;
46 QUERY_METRIC *qm;
47 time_t view_update_every;
48 time_t query_granularity;
49 TIER_QUERY_FETCH tier_query_fetch;
50
51 // query planer
52 size_t current_plan;
53 time_t current_plan_expire_time;
54 time_t plan_expanded_after;
55 time_t plan_expanded_before;
56
57 // storage queries
58 size_t tier;
59 struct query_metric_tier *tier_ptr;
60 struct storage_engine_query_handle *seqh;
61
62 // aggregating points over time
63 size_t group_points_non_zero;
64 size_t group_points_added;
65 STORAGE_POINT group_point; // aggregates min, max, sum, count, anomaly count for each group point
66 STORAGE_POINT query_point; // aggregates min, max, sum, count, anomaly count across the whole query
67 RRDR_VALUE_FLAGS group_value_flags;
68
69 // statistics
70 size_t db_total_points_read;
71 size_t db_points_read_per_tier[RRD_STORAGE_TIERS];
72
73 struct {
74 time_t expanded_after;
75 time_t expanded_before;
76 struct storage_engine_query_handle handle;
77 bool initialized;
78 bool finalized;
79 } plans[QUERY_PLANS_MAX];
80
81 struct query_engine_ops *next;
82 } QUERY_ENGINE_OPS;
83
84 // query planner
85 #define query_plan_should_switch_plan(ops, now) ((now) >= (ops)->current_plan_expire_time)
86 bool query_planer_next_plan(QUERY_ENGINE_OPS *ops, time_t now, time_t last_point_end_time);
87 void query_planer_finalize_remaining_plans(QUERY_ENGINE_OPS *ops);
88 QUERY_ENGINE_OPS *rrd2rrdr_query_ops_prep(RRDR *r, size_t query_metric_id);
89 void rrd2rrdr_query_ops_release(QUERY_ENGINE_OPS *ops);
90 time_t query_target_min_update_every_for_tier(QUERY_TARGET *qt, size_t tier);
91 int query_plan_unittest(void);
92 void rrd2rrdr_query_ops_freeall(RRDR *r);
93
94 // query execution
95 void rrd2rrdr_query_execute(RRDR *r, size_t dim_id_in_rrdr, QUERY_ENGINE_OPS *ops);
96
97 // time aggregation
98 void time_grouping_add(RRDR *r, NETDATA_DOUBLE value, const RRDR_TIME_GROUPING add_flush);
99 NETDATA_DOUBLE time_grouping_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr, const RRDR_TIME_GROUPING add_flush);
100 void rrdr_set_grouping_function(RRDR *r, RRDR_TIME_GROUPING group_method);
101
102 // group by
103 struct group_by_label_key {
104 DICTIONARY *values;
105 };
106
107 void group_by_label_key_insert_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data);
108 void group_by_label_key_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused);
109 int rrdlabels_traversal_cb_to_group_by_label_key(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data);
110 void rrd2rrdr_set_timestamps(RRDR *r);
111 RRDR *rrd2rrdr_group_by_initialize(ONEWAYALLOC *owa, QUERY_TARGET *qt);
112 void rrdr2rrdr_group_by_calculate_percentage_of_group(RRDR *r);
113 void rrdr2rrdr_group_by_partial_trimming(RRDR *r);
114 void rrd2rrdr_group_by_add_metric(RRDR *r_dst, size_t d_dst, RRDR *r_tmp, size_t d_tmp,
115 RRDR_GROUP_BY_FUNCTION group_by_aggregate_function,
116 STORAGE_POINT *query_points, size_t pass);
117 void rrd2rrdr_convert_values_to_percentage_of_total(RRDR *r);
118 RRDR *rrd2rrdr_group_by_finalize(RRDR *r_tmp);
119 RRDR *rrd2rrdr_cardinality_limit(RRDR *r);
120
121 #endif //NETDATA_QUERY_INTERNAL_H