lookup_statistics_counter_by_interval.c (6138B)
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/lookup_statistics_counter_by_interval.c 18 * @brief Implementation of the lookup_statistics_counter_by_interval function for Postgres 19 * @author Martin Schanzenbach 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_statistics_counter_by_interval.h" 24 #include "helper.h" 25 #include "merchantdb_lib.h" 26 27 28 /** 29 * Context used for TALER_MERCHANTDB_lookup_statistics_counter(). 30 */ 31 struct LookupCounterStatisticsContext 32 { 33 /** 34 * Function to call with the results. 35 */ 36 TALER_MERCHANTDB_CounterByIntervalStatisticsCallback 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 lookup_statistics_counter_by_interval_desc_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct LookupCounterStatisticsContext *tflc = cls; 68 69 for (unsigned int i = 0; i < num_results; i++) 70 { 71 char *description; 72 struct GNUNET_PQ_ResultSpec rs[] = { 73 GNUNET_PQ_result_spec_string ("description", 74 &description), 75 GNUNET_PQ_result_spec_end 76 }; 77 78 if (GNUNET_OK != 79 GNUNET_PQ_extract_result (result, 80 rs, 81 i)) 82 { 83 GNUNET_break (0); 84 tflc->extract_failed = true; 85 return; 86 } 87 88 tflc->description = GNUNET_strdup (description); 89 90 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 lookup_statistics_counter_by_interval_cb ( 105 void *cls, 106 PGresult *result, 107 unsigned int num_results) 108 { 109 struct LookupCounterStatisticsContext *tflc = cls; 110 111 for (unsigned int i = 0; i < num_results; i++) 112 { 113 uint64_t cumulative_number; 114 uint64_t interval_start_ago; 115 struct GNUNET_PQ_ResultSpec rs[] = { 116 GNUNET_PQ_result_spec_uint64 ("range", 117 &interval_start_ago), 118 GNUNET_PQ_result_spec_uint64 ("rvalue", 119 &cumulative_number), 120 GNUNET_PQ_result_spec_end 121 }; 122 struct GNUNET_TIME_Timestamp interval_start; 123 124 if (GNUNET_OK != 125 GNUNET_PQ_extract_result (result, 126 rs, 127 i)) 128 { 129 GNUNET_break (0); 130 tflc->extract_failed = true; 131 return; 132 } 133 134 interval_start = GNUNET_TIME_timestamp_get (); 135 interval_start.abs_time.abs_value_us -= interval_start_ago * 1000 * 1000; 136 tflc->cb (tflc->cb_cls, 137 tflc->description, 138 interval_start, 139 cumulative_number); 140 GNUNET_PQ_cleanup_result (rs); 141 } 142 } 143 144 145 enum GNUNET_DB_QueryStatus 146 TALER_MERCHANTDB_lookup_statistics_counter_by_interval ( 147 struct TALER_MERCHANTDB_PostgresContext *pg, 148 const char *instance_id, 149 const char *slug, 150 TALER_MERCHANTDB_CounterByIntervalStatisticsCallback cb, 151 void *cb_cls) 152 { 153 struct LookupCounterStatisticsContext context = { 154 .cb = cb, 155 .cb_cls = cb_cls, 156 /* Can be overwritten by the lookup_statistics_counter_by_interval_cb */ 157 .extract_failed = false, 158 .description = NULL 159 }; 160 struct GNUNET_PQ_QueryParam descParams[] = { 161 GNUNET_PQ_query_param_string (slug), 162 GNUNET_PQ_query_param_end 163 }; 164 struct GNUNET_PQ_QueryParam params[] = { 165 GNUNET_PQ_query_param_string (slug), 166 GNUNET_PQ_query_param_end 167 }; 168 enum GNUNET_DB_QueryStatus qs; 169 170 GNUNET_assert (NULL != pg->current_merchant_id); 171 GNUNET_assert (0 == strcmp (instance_id, 172 pg->current_merchant_id)); 173 check_connection (pg); 174 TMH_PQ_prepare_anon (pg, 175 "SELECT description" 176 " FROM merchant_statistic_interval_meta" 177 " WHERE slug=$1" 178 " LIMIT 1"); 179 qs = GNUNET_PQ_eval_prepared_multi_select ( 180 pg->conn, 181 "", 182 descParams, 183 &lookup_statistics_counter_by_interval_desc_cb, 184 &context); 185 /* If there was an error inside the cb, return a hard error. */ 186 if (context.extract_failed) 187 { 188 GNUNET_break (0); 189 return GNUNET_DB_STATUS_HARD_ERROR; 190 } 191 TMH_PQ_prepare_anon (pg, 192 "SELECT" 193 " range" 194 " ,rvalue" 195 " FROM merchant_statistic_interval_number_get($1)"); 196 qs = GNUNET_PQ_eval_prepared_multi_select ( 197 pg->conn, 198 "", 199 params, 200 &lookup_statistics_counter_by_interval_cb, 201 &context); 202 if (NULL != context.description) 203 GNUNET_free (context.description); 204 /* If there was an error inside the cb, return a hard error. */ 205 if (context.extract_failed) 206 { 207 GNUNET_break (0); 208 return GNUNET_DB_STATUS_HARD_ERROR; 209 } 210 return qs; 211 }