| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "jsonwrap.h" |
| 4 | #include "jsonwrap-internal.h" |
| 5 | |
| 6 | size_t query_target_summary_contexts_v2(BUFFER *wb, QUERY_TARGET *qt, const char *key, struct summary_total_counts *totals) { |
| 7 | buffer_json_member_add_array(wb, key); |
| 8 | DICTIONARY *dict = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE); |
| 9 | |
| 10 | struct { |
| 11 | STORAGE_POINT query_points; |
| 12 | QUERY_INSTANCES_COUNTS instances; |
| 13 | QUERY_METRICS_COUNTS metrics; |
| 14 | QUERY_ALERTS_COUNTS alerts; |
| 15 | } *z; |
| 16 | |
| 17 | for (long c = 0; c < (long) qt->contexts.used; c++) { |
| 18 | QUERY_CONTEXT *qc = query_context(qt, c); |
| 19 | |
| 20 | z = dictionary_set(dict, rrdcontext_acquired_id(qc->rca), NULL, sizeof(*z)); |
| 21 | |
| 22 | z->instances.selected += qc->instances.selected; |
| 23 | z->instances.excluded += qc->instances.selected; |
| 24 | z->instances.queried += qc->instances.queried; |
| 25 | z->instances.failed += qc->instances.failed; |
| 26 | |
| 27 | z->metrics.selected += qc->metrics.selected; |
| 28 | z->metrics.excluded += qc->metrics.excluded; |
| 29 | z->metrics.queried += qc->metrics.queried; |
| 30 | z->metrics.failed += qc->metrics.failed; |
| 31 | |
| 32 | z->alerts.clear += qc->alerts.clear; |
| 33 | z->alerts.warning += qc->alerts.warning; |
| 34 | z->alerts.critical += qc->alerts.critical; |
| 35 | |
| 36 | storage_point_merge_to(z->query_points, qc->query_points); |
| 37 | } |
| 38 | |
| 39 | size_t unique_contexts = dictionary_entries(dict); |
| 40 | dfe_start_read(dict, z) { |
| 41 | buffer_json_add_array_item_object(wb); |
| 42 | buffer_json_member_add_string(wb, "id", z_dfe.name); |
| 43 | |
| 44 | // Only include detailed statistics if MINIMAL_STATS option is not set |
| 45 | if (!(qt->window.options & RRDR_OPTION_MINIMAL_STATS)) { |
| 46 | query_target_instance_counts(wb, &z->instances); |
| 47 | query_target_metric_counts(wb, &z->metrics); |
| 48 | query_target_alerts_counts(wb, &z->alerts, NULL, false); |
| 49 | } |
| 50 | |
| 51 | query_target_points_statistics(wb, qt, &z->query_points); |
| 52 | buffer_json_object_close(wb); |
| 53 | |
| 54 | aggregate_into_summary_totals(totals, &z->metrics); |
| 55 | } |
| 56 | dfe_done(z); |
| 57 | buffer_json_array_close(wb); |
| 58 | dictionary_destroy(dict); |
| 59 | |
| 60 | return unique_contexts; |
| 61 | } |