Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file src/backenddb/iterate_statistic_bucket_counters_by_range.c
18 : * @brief Implementation of the iterate_statistic_bucket_counters_by_range function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_statistic_bucket_counters_by_range.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Context used for TALER_MERCHANTDB_iterate_statistic_bucket_counters_by_range().
28 : */
29 : struct LookupCounterStatisticsContext2
30 : {
31 : /**
32 : * Function to call with the results.
33 : */
34 : TALER_MERCHANTDB_CounterByBucketRangeStatisticsCallback cb;
35 :
36 : /**
37 : * Closure for @a cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Did database result extraction fail?
43 : */
44 : bool extract_failed;
45 :
46 : /**
47 : * Postgres context for array lookups
48 : */
49 : struct TALER_MERCHANTDB_PostgresContext *pg;
50 :
51 : };
52 :
53 :
54 : /**
55 : * Function to be called with the results of a SELECT statement
56 : * that has returned @a num_results results about token families.
57 : *
58 : * @param[in,out] cls of type `struct LookupCounterStatisticsContext2 *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 1 : lookup_statistics_counter_by_bucket_cb2 (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 1 : struct LookupCounterStatisticsContext2 *tflc = cls;
68 1 : struct TALER_MERCHANTDB_PostgresContext *pg = tflc->pg;
69 :
70 1 : for (unsigned int i = 0; i < num_results; i++)
71 : {
72 : size_t num_slugs;
73 : char *slugs;
74 : size_t num_counters;
75 : uint64_t *counters;
76 : uint64_t bucket_start_epoch;
77 0 : struct GNUNET_PQ_ResultSpec rs[] = {
78 0 : GNUNET_PQ_result_spec_uint64 ("bucket_start",
79 : &bucket_start_epoch),
80 0 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
81 : "counters",
82 : &num_counters,
83 : &counters),
84 0 : GNUNET_PQ_result_spec_array_string (pg->conn,
85 : "slugs",
86 : &num_slugs,
87 : &slugs),
88 : GNUNET_PQ_result_spec_end
89 : };
90 : struct GNUNET_TIME_Timestamp bucket_start;
91 :
92 0 : if (GNUNET_OK !=
93 0 : GNUNET_PQ_extract_result (result,
94 : rs,
95 : i))
96 : {
97 0 : GNUNET_break (0);
98 0 : tflc->extract_failed = true;
99 0 : return;
100 : }
101 0 : if (num_slugs != num_counters)
102 : {
103 0 : GNUNET_break (0);
104 0 : tflc->extract_failed = true;
105 0 : GNUNET_PQ_cleanup_result (rs);
106 0 : return;
107 : }
108 0 : {
109 0 : const char *slugptrs[num_slugs];
110 0 : const char *pos = slugs;
111 :
112 0 : for (size_t j = 0; j<num_slugs; j++)
113 : {
114 0 : slugptrs[j] = pos;
115 0 : pos += strlen (pos) + 1;
116 : }
117 :
118 0 : bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
119 0 : tflc->cb (tflc->cb_cls,
120 : bucket_start,
121 : num_slugs,
122 : slugptrs,
123 : counters);
124 : }
125 0 : GNUNET_PQ_cleanup_result (rs);
126 : }
127 : }
128 :
129 :
130 : enum GNUNET_DB_QueryStatus
131 1 : TALER_MERCHANTDB_iterate_statistic_bucket_counters_by_range (
132 : struct TALER_MERCHANTDB_PostgresContext *pg,
133 : const char *instance_id,
134 : const char *prefix,
135 : const char *granularity,
136 : uint64_t counter,
137 : TALER_MERCHANTDB_CounterByBucketRangeStatisticsCallback cb,
138 : void *cb_cls)
139 : {
140 1 : struct LookupCounterStatisticsContext2 context = {
141 : .cb = cb,
142 : .cb_cls = cb_cls,
143 : /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb2 */
144 : .extract_failed = false,
145 : .pg = pg
146 : };
147 1 : struct GNUNET_PQ_QueryParam params[] = {
148 1 : GNUNET_PQ_query_param_string (prefix),
149 1 : GNUNET_PQ_query_param_string (granularity),
150 1 : GNUNET_PQ_query_param_uint64 (&counter),
151 : GNUNET_PQ_query_param_end
152 : };
153 : enum GNUNET_DB_QueryStatus qs;
154 :
155 1 : GNUNET_assert (NULL != pg->current_merchant_id);
156 1 : GNUNET_assert (0 == strcmp (instance_id,
157 : pg->current_merchant_id));
158 1 : TMH_PQ_prepare_anon (pg,
159 : "SELECT"
160 : " msbc.bucket_start"
161 : ",ARRAY_AGG(msbm.slug) AS slugs"
162 : ",ARRAY_AGG(msbc.cumulative_number) AS counters"
163 : " FROM merchant_statistic_bucket_counter msbc"
164 : " JOIN merchant_statistic_bucket_meta msbm"
165 : " USING (bmeta_serial_id)"
166 : " WHERE msbm.slug LIKE $1"
167 : " AND msbc.bucket_range=$2::TEXT::merchant.statistic_range"
168 : " AND msbm.stype = 'number'"
169 : " GROUP BY msbc.bucket_start"
170 : " ORDER BY msbc.bucket_start DESC"
171 : " LIMIT $3");
172 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
173 : pg->conn,
174 : "",
175 : params,
176 : &lookup_statistics_counter_by_bucket_cb2,
177 : &context);
178 : /* If there was an error inside the cb, return a hard error. */
179 1 : if (context.extract_failed)
180 : {
181 0 : GNUNET_break (0);
182 0 : return GNUNET_DB_STATUS_HARD_ERROR;
183 : }
184 1 : return qs;
185 : }
|