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_counter_by_bucket2.c
18 : * @brief Implementation of the lookup_statistics_counter_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_counter_by_bucket2.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Context used for TMH_PG_lookup_statistics_counter_by_bucket2().
30 : */
31 : struct LookupCounterStatisticsContext2
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 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 : * Postgres context for array lookups
50 : */
51 : struct PostgresClosure *pg;
52 :
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 LookupCounterStatisticsContext2 *`
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_counter_by_bucket_cb2 (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 0 : struct LookupCounterStatisticsContext2 *tflc = cls;
70 0 : struct PostgresClosure *pg = tflc->pg;
71 :
72 0 : for (unsigned int i = 0; i < num_results; i++)
73 : {
74 : size_t num_slugs;
75 : char *slugs;
76 : size_t num_counters;
77 : uint64_t *counters;
78 : uint64_t bucket_start_epoch;
79 0 : struct GNUNET_PQ_ResultSpec rs[] = {
80 0 : GNUNET_PQ_result_spec_uint64 ("bucket_start",
81 : &bucket_start_epoch),
82 0 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
83 : "counters",
84 : &num_counters,
85 : &counters),
86 0 : GNUNET_PQ_result_spec_array_string (pg->conn,
87 : "slugs",
88 : &num_slugs,
89 : &slugs),
90 : GNUNET_PQ_result_spec_end
91 : };
92 : struct GNUNET_TIME_Timestamp bucket_start;
93 :
94 0 : if (GNUNET_OK !=
95 0 : GNUNET_PQ_extract_result (result,
96 : rs,
97 : i))
98 : {
99 0 : GNUNET_break (0);
100 0 : tflc->extract_failed = true;
101 0 : return;
102 : }
103 0 : if (num_slugs != num_counters)
104 : {
105 0 : GNUNET_break (0);
106 0 : tflc->extract_failed = true;
107 0 : GNUNET_PQ_cleanup_result (rs);
108 0 : return;
109 : }
110 :
111 0 : bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
112 0 : tflc->cb (tflc->cb_cls,
113 : bucket_start,
114 : num_slugs,
115 : (const char **) slugs,
116 : counters);
117 0 : GNUNET_PQ_cleanup_result (rs);
118 : }
119 : }
120 :
121 :
122 : enum GNUNET_DB_QueryStatus
123 0 : TMH_PG_lookup_statistics_counter_by_bucket2 (
124 : void *cls,
125 : const char *instance_id,
126 : const char *prefix,
127 : const char *granularity,
128 : uint64_t counter,
129 : TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 cb,
130 : void *cb_cls)
131 : {
132 0 : struct PostgresClosure *pg = cls;
133 0 : struct LookupCounterStatisticsContext2 context = {
134 : .cb = cb,
135 : .cb_cls = cb_cls,
136 : /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb2 */
137 : .extract_failed = false,
138 : .pg = pg
139 : };
140 0 : struct GNUNET_PQ_QueryParam params[] = {
141 0 : GNUNET_PQ_query_param_string (instance_id),
142 0 : GNUNET_PQ_query_param_string (prefix),
143 0 : GNUNET_PQ_query_param_string (granularity),
144 0 : GNUNET_PQ_query_param_uint64 (&counter),
145 : GNUNET_PQ_query_param_end
146 : };
147 : enum GNUNET_DB_QueryStatus qs;
148 :
149 0 : check_connection (pg);
150 0 : PREPARE (pg,
151 : "lookup_statistics_counter_by_bucket2",
152 : "SELECT"
153 : " bucket_start"
154 : " ARRAY_AGG(slug) AS slugs"
155 : " ARRAY_AGG(cumulative_number) AS counters"
156 : " FROM merchant_statistic_bucket_counter"
157 : " JOIN merchant_statistic_bucket_meta"
158 : " USING (bmeta_serial_id)"
159 : " JOIN merchant_instances"
160 : " USING (merchant_serial)"
161 : " WHERE merchant_instances.merchant_id=$1"
162 : " AND merchant_statistic_bucket_meta.slug LIKE $1 || '%'"
163 : " AND merchant_statistic_bucket_meta.bucket_range=$3::TEXT::bucket_range"
164 : " AND merchant_statistic_bucket_meta.stype = 'number'"
165 : " GROUP BY bucket_start"
166 : " ORDER BY bucket_start DESC"
167 : " LIMIT $4");
168 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
169 : pg->conn,
170 : "lookup_statistics_counter_by_bucket2",
171 : params,
172 : &lookup_statistics_counter_by_bucket_cb2,
173 : &context);
174 : /* If there was an error inside the cb, return a hard error. */
175 0 : if (context.extract_failed)
176 : {
177 0 : GNUNET_break (0);
178 0 : return GNUNET_DB_STATUS_HARD_ERROR;
179 : }
180 0 : return qs;
181 : }
|