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 backenddb/pg_lookup_statistics_amount_by_bucket2.c
18 : * @brief Implementation of the lookup_statistics_amount_by_bucket2 function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_error_codes.h>
23 : #include <taler/taler_dbevents.h>
24 : #include <taler/taler_pq_lib.h>
25 : #include "pg_lookup_statistics_amount_by_bucket2.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Context used for TMH_PG_lookup_statistics_amount_by_bucket2().
31 : */
32 : struct LookupAmountStatisticsContext2
33 : {
34 : /**
35 : * Function to call with the results.
36 : */
37 : TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb;
38 :
39 : /**
40 : * Closure for @a cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Did database result extraction fail?
46 : */
47 : bool extract_failed;
48 :
49 : /**
50 : * Postgres context for array lookups
51 : */
52 : struct PostgresClosure *pg;
53 : };
54 :
55 :
56 : /**
57 : * Function to be called with the results of a SELECT statement
58 : * that has returned @a num_results results about token families.
59 : *
60 : * @param[in,out] cls of type `struct LookupAmountStatisticsContext2 *`
61 : * @param result the postgres result
62 : * @param num_results the number of results in @a result
63 : */
64 : static void
65 0 : lookup_statistics_amount_by_bucket_cb2 (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 0 : struct LookupAmountStatisticsContext2 *tflc = cls;
70 0 : struct PostgresClosure *pg = tflc->pg;
71 :
72 0 : for (unsigned int i = 0; i < num_results; i++)
73 : {
74 : struct TALER_Amount *amounts;
75 : size_t num_amounts;
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 : TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
81 : "cumulative_amounts",
82 : &num_amounts,
83 : &amounts),
84 : GNUNET_PQ_result_spec_end
85 : };
86 : struct GNUNET_TIME_Timestamp bucket_start;
87 :
88 0 : if (GNUNET_OK !=
89 0 : GNUNET_PQ_extract_result (result,
90 : rs,
91 : i))
92 : {
93 0 : GNUNET_break (0);
94 0 : tflc->extract_failed = true;
95 0 : return;
96 : }
97 :
98 0 : bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
99 0 : tflc->cb (tflc->cb_cls,
100 : bucket_start,
101 : num_amounts,
102 : amounts);
103 0 : GNUNET_PQ_cleanup_result (rs);
104 : }
105 : }
106 :
107 :
108 : enum GNUNET_DB_QueryStatus
109 0 : TMH_PG_lookup_statistics_amount_by_bucket2 (
110 : void *cls,
111 : const char *instance_id,
112 : const char *slug,
113 : const char *granularity,
114 : uint64_t counter,
115 : TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb,
116 : void *cb_cls)
117 : {
118 0 : struct PostgresClosure *pg = cls;
119 0 : struct LookupAmountStatisticsContext2 context = {
120 : .cb = cb,
121 : .cb_cls = cb_cls,
122 : /* Can be overwritten by the lookup_statistics_amount_by_bucket_cb2 */
123 : .extract_failed = false,
124 : .pg = pg,
125 : };
126 0 : struct GNUNET_PQ_QueryParam params[] = {
127 0 : GNUNET_PQ_query_param_string (instance_id),
128 0 : GNUNET_PQ_query_param_string (slug),
129 0 : GNUNET_PQ_query_param_string (granularity),
130 0 : GNUNET_PQ_query_param_uint64 (&counter),
131 : GNUNET_PQ_query_param_end
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 0 : check_connection (pg);
136 0 : PREPARE (pg,
137 : "lookup_statistics_amount_by_bucket2",
138 : "SELECT"
139 : " bucket_start"
140 : " ARRAY_AGG("
141 : " ROW(cumulative_value, cumulative_frac, curr)::taler_amount_currency"
142 : ") AS cumulative_amounts"
143 : " FROM merchant_statistic_bucket_meta"
144 : " JOIN merchant_instances"
145 : " USING (merchant_serial)"
146 : " JOIN merchant_statistic_bucket_amount"
147 : " USING (bmeta_serial_id)"
148 : " WHERE merchant_instances.merchant_id=$1"
149 : " AND merchant_statistic_bucket_meta.slug=$2"
150 : " AND merchant_statistic_bucket_meta.bucket_range=$3::TEXT::bucket_range"
151 : " AND merchant_statistic_bucket_meta.stype='amount'"
152 : " GROUP BY bucket_start"
153 : " ORDER BY bucket_start DESC"
154 : " LIMIT $4;");
155 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
156 : pg->conn,
157 : "lookup_statistics_amount_by_bucket2",
158 : params,
159 : &lookup_statistics_amount_by_bucket_cb2,
160 : &context);
161 : /* If there was an error inside the cb, return a hard error. */
162 0 : if (context.extract_failed)
163 : {
164 0 : GNUNET_break (0);
165 0 : return GNUNET_DB_STATUS_HARD_ERROR;
166 : }
167 0 : return qs;
168 : }
|