Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024, 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 exchangedb/select_aml_statistics.c
18 : * @brief Implementation of the select_aml_statistics function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_aml_statistics.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #get_statistics_cb().
28 : */
29 : struct GetStatisticsContext
30 : {
31 : /**
32 : * Function to call per result.
33 : */
34 : TALER_EXCHANGEDB_AmlStatisticsCallback cb;
35 :
36 : /**
37 : * Closure for @e cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_EXCHANGEDB_PostgresContext *pg;
45 :
46 : /**
47 : * Flag set to #GNUNET_OK as long as everything is fine.
48 : */
49 : enum GNUNET_GenericReturnValue status;
50 :
51 : };
52 :
53 :
54 : /**
55 : * Invoke the callback for each result.
56 : *
57 : * @param cls a `struct GetStatisticsContext *`
58 : * @param result SQL result
59 : * @param num_results number of rows in @a result
60 : */
61 : static void
62 0 : get_statistics_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 0 : struct GetStatisticsContext *ctx = cls;
67 :
68 0 : for (unsigned int i = 0; i < num_results; i++)
69 : {
70 : uint64_t val;
71 : char *name;
72 0 : struct GNUNET_PQ_ResultSpec rs[] = {
73 0 : GNUNET_PQ_result_spec_string ("name",
74 : &name),
75 0 : GNUNET_PQ_result_spec_uint64 ("value",
76 : &val),
77 : GNUNET_PQ_result_spec_end
78 : };
79 :
80 0 : if (GNUNET_OK !=
81 0 : GNUNET_PQ_extract_result (result,
82 : rs,
83 : i))
84 : {
85 0 : GNUNET_break (0);
86 0 : ctx->status = GNUNET_SYSERR;
87 0 : return;
88 : }
89 0 : ctx->cb (ctx->cb_cls,
90 : name,
91 : val);
92 0 : GNUNET_PQ_cleanup_result (rs);
93 : }
94 : }
95 :
96 :
97 : enum GNUNET_DB_QueryStatus
98 0 : TALER_EXCHANGEDB_select_aml_statistics (
99 : struct TALER_EXCHANGEDB_PostgresContext *pg,
100 : size_t num_names,
101 : const char *names[static num_names],
102 : struct GNUNET_TIME_Timestamp start_date,
103 : struct GNUNET_TIME_Timestamp end_date,
104 : TALER_EXCHANGEDB_AmlStatisticsCallback cb,
105 : void *cb_cls)
106 0 : {
107 0 : struct GetStatisticsContext ctx = {
108 : .cb = cb,
109 : .cb_cls = cb_cls,
110 : .pg = pg,
111 : .status = GNUNET_OK
112 : };
113 0 : struct GNUNET_PQ_QueryParam params[] = {
114 0 : GNUNET_PQ_query_param_array_ptrs_string (num_names,
115 : names,
116 : pg->conn),
117 0 : GNUNET_PQ_query_param_timestamp (&start_date),
118 0 : GNUNET_PQ_query_param_timestamp (&end_date),
119 : GNUNET_PQ_query_param_end
120 : };
121 : enum GNUNET_DB_QueryStatus qs;
122 :
123 0 : PREPARE (pg,
124 : "select_aml_statistics",
125 : "SELECT "
126 : " event_type AS name"
127 : ",COUNT(*) AS value"
128 : " FROM kyc_events"
129 : " WHERE event_type = ANY ($1)"
130 : " AND event_timestamp >= $2"
131 : " AND event_timestamp < $3"
132 : " GROUP BY event_type;");
133 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
134 : "select_aml_statistics",
135 : params,
136 : &get_statistics_cb,
137 : &ctx);
138 0 : GNUNET_PQ_cleanup_query_params_closures (params);
139 0 : if (GNUNET_OK != ctx.status)
140 0 : return GNUNET_DB_STATUS_HARD_ERROR;
141 0 : return qs;
142 : }
|