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_interval_amounts.c
18 : * @brief Implementation of the iterate_statistic_interval_amounts function for Postgres
19 : * @author Martin Schanzenbach
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_statistic_interval_amounts.h"
24 : #include "helper.h"
25 : #include "merchantdb_lib.h"
26 :
27 :
28 : /**
29 : * Context used for TALER_MERCHANTDB_iterate_statistic_interval_amounts().
30 : */
31 : struct LookupAmountStatisticsContext
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb;
37 :
38 : /**
39 : * Closure for @a cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Did database result extraction fail?
45 : */
46 : bool extract_failed;
47 :
48 : /**
49 : * Description of statistic
50 : */
51 : char*description;
52 : };
53 :
54 : /**
55 : * Function to be called with the results of a SELECT statement
56 : * that has returned @a num_results results about statistics.
57 : *
58 : * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 2 : lookup_statistics_amount_by_interval_desc_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 2 : struct LookupAmountStatisticsContext *tflc = cls;
68 :
69 2 : for (unsigned int i = 0; i < num_results; i++)
70 : {
71 : char *description;
72 0 : struct GNUNET_PQ_ResultSpec rs[] = {
73 0 : GNUNET_PQ_result_spec_string ("description",
74 : &description),
75 : GNUNET_PQ_result_spec_end
76 : };
77 :
78 0 : if (GNUNET_OK !=
79 0 : GNUNET_PQ_extract_result (result,
80 : rs,
81 : i))
82 : {
83 0 : GNUNET_break (0);
84 0 : tflc->extract_failed = true;
85 0 : return;
86 : }
87 :
88 0 : tflc->description = GNUNET_strdup (description);
89 :
90 0 : GNUNET_PQ_cleanup_result (rs);
91 : }
92 : }
93 :
94 :
95 : /**
96 : * Function to be called with the results of a SELECT statement
97 : * that has returned @a num_results results about statistics.
98 : *
99 : * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
100 : * @param result the postgres result
101 : * @param num_results the number of results in @a result
102 : */
103 : static void
104 2 : lookup_statistics_amount_by_interval_cb (
105 : void *cls,
106 : PGresult *result,
107 : unsigned int num_results)
108 : {
109 2 : struct LookupAmountStatisticsContext *tflc = cls;
110 2 : struct TALER_Amount *amounts = NULL;
111 2 : uint64_t cur_interval_start_ago = UINT64_MAX;
112 2 : unsigned int amounts_len = 0;
113 :
114 2 : for (unsigned int i = 0; i < num_results; i++)
115 : {
116 : struct TALER_Amount cumulative_amount;
117 : uint64_t interval_start_ago;
118 0 : struct GNUNET_PQ_ResultSpec rs[] = {
119 0 : GNUNET_PQ_result_spec_uint64 ("range",
120 : &interval_start_ago),
121 0 : TALER_PQ_result_spec_amount_with_currency ("rvalue",
122 : &cumulative_amount),
123 : GNUNET_PQ_result_spec_end
124 : };
125 :
126 0 : if (GNUNET_OK !=
127 0 : GNUNET_PQ_extract_result (result,
128 : rs,
129 : i))
130 : {
131 0 : GNUNET_break (0);
132 0 : tflc->extract_failed = true;
133 0 : return;
134 : }
135 :
136 : /* Call callback if the bucket changed */
137 0 : if ( (interval_start_ago != cur_interval_start_ago) &&
138 : (i > 0) )
139 : {
140 : struct GNUNET_TIME_Timestamp interval_start;
141 :
142 0 : interval_start = GNUNET_TIME_timestamp_get ();
143 0 : interval_start.abs_time.abs_value_us -= cur_interval_start_ago * 1000 * 1000;
144 0 : tflc->cb (tflc->cb_cls,
145 0 : tflc->description,
146 : interval_start,
147 : amounts_len,
148 : amounts);
149 0 : GNUNET_array_grow (amounts,
150 : amounts_len,
151 : 0);
152 : }
153 0 : cur_interval_start_ago = interval_start_ago;
154 0 : GNUNET_array_append (amounts,
155 : amounts_len,
156 : cumulative_amount);
157 0 : GNUNET_PQ_cleanup_result (rs);
158 : }
159 2 : if (0 != amounts_len)
160 : {
161 : struct GNUNET_TIME_Timestamp interval_start;
162 :
163 0 : interval_start = GNUNET_TIME_timestamp_get ();
164 0 : interval_start.abs_time.abs_value_us -= cur_interval_start_ago * 1000 * 1000;
165 0 : tflc->cb (tflc->cb_cls,
166 0 : tflc->description,
167 : interval_start,
168 : amounts_len,
169 : amounts);
170 0 : GNUNET_array_grow (amounts,
171 : amounts_len,
172 : 0);
173 : }
174 : }
175 :
176 :
177 : enum GNUNET_DB_QueryStatus
178 2 : TALER_MERCHANTDB_iterate_statistic_interval_amounts (
179 : struct TALER_MERCHANTDB_PostgresContext *pg,
180 : const char *instance_id,
181 : const char *slug,
182 : TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb,
183 : void *cb_cls)
184 : {
185 2 : struct LookupAmountStatisticsContext context = {
186 : .cb = cb,
187 : .cb_cls = cb_cls,
188 : /* Can be overwritten by the lookup_statistics_amount_by_interval_cb */
189 : .extract_failed = false,
190 : .description = NULL
191 : };
192 2 : struct GNUNET_PQ_QueryParam descParams[] = {
193 2 : GNUNET_PQ_query_param_string (slug),
194 : GNUNET_PQ_query_param_end
195 : };
196 2 : struct GNUNET_PQ_QueryParam params[] = {
197 2 : GNUNET_PQ_query_param_string (slug),
198 : GNUNET_PQ_query_param_end
199 : };
200 : enum GNUNET_DB_QueryStatus qs;
201 :
202 2 : GNUNET_assert (NULL != pg->current_merchant_id);
203 2 : GNUNET_assert (0 == strcmp (instance_id,
204 : pg->current_merchant_id));
205 2 : TMH_PQ_prepare_anon (pg,
206 : "SELECT description"
207 : " FROM merchant_statistic_interval_meta"
208 : " WHERE slug=$1 LIMIT 1");
209 2 : qs = GNUNET_PQ_eval_prepared_multi_select (
210 : pg->conn,
211 : "",
212 : descParams,
213 : &lookup_statistics_amount_by_interval_desc_cb,
214 : &context);
215 : /* If there was an error inside the cb, return a hard error. */
216 2 : if (context.extract_failed)
217 : {
218 0 : GNUNET_break (0);
219 0 : return GNUNET_DB_STATUS_HARD_ERROR;
220 : }
221 2 : TMH_PQ_prepare_anon (pg,
222 : "SELECT *"
223 : " FROM merchant_statistic_interval_amount_get($1)");
224 2 : qs = GNUNET_PQ_eval_prepared_multi_select (
225 : pg->conn,
226 : "",
227 : params,
228 : &lookup_statistics_amount_by_interval_cb,
229 : &context);
230 2 : if (NULL != context.description)
231 0 : GNUNET_free (context.description);
232 : /* If there was an error inside the cb, return a hard error. */
233 2 : if (context.extract_failed)
234 : {
235 0 : GNUNET_break (0);
236 0 : return GNUNET_DB_STATUS_HARD_ERROR;
237 : }
238 2 : return qs;
239 : }
|