lookup_statistics_counter_by_bucket2.c (5814B)
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_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_pq_lib.h> 23 #include "merchant-database/lookup_statistics_counter_by_bucket2.h" 24 #include "helper.h" 25 26 /** 27 * Context used for TALER_MERCHANTDB_lookup_statistics_counter_by_bucket2(). 28 */ 29 struct LookupCounterStatisticsContext2 30 { 31 /** 32 * Function to call with the results. 33 */ 34 TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 cb; 35 36 /** 37 * Closure for @a cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Did database result extraction fail? 43 */ 44 bool extract_failed; 45 46 /** 47 * Postgres context for array lookups 48 */ 49 struct TALER_MERCHANTDB_PostgresContext *pg; 50 51 }; 52 53 54 /** 55 * Function to be called with the results of a SELECT statement 56 * that has returned @a num_results results about token families. 57 * 58 * @param[in,out] cls of type `struct LookupCounterStatisticsContext2 *` 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_bucket_cb2 (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct LookupCounterStatisticsContext2 *tflc = cls; 68 struct TALER_MERCHANTDB_PostgresContext *pg = tflc->pg; 69 70 for (unsigned int i = 0; i < num_results; i++) 71 { 72 size_t num_slugs; 73 char *slugs; 74 size_t num_counters; 75 uint64_t *counters; 76 uint64_t bucket_start_epoch; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("bucket_start", 79 &bucket_start_epoch), 80 GNUNET_PQ_result_spec_array_uint64 (pg->conn, 81 "counters", 82 &num_counters, 83 &counters), 84 GNUNET_PQ_result_spec_array_string (pg->conn, 85 "slugs", 86 &num_slugs, 87 &slugs), 88 GNUNET_PQ_result_spec_end 89 }; 90 struct GNUNET_TIME_Timestamp bucket_start; 91 92 if (GNUNET_OK != 93 GNUNET_PQ_extract_result (result, 94 rs, 95 i)) 96 { 97 GNUNET_break (0); 98 tflc->extract_failed = true; 99 return; 100 } 101 if (num_slugs != num_counters) 102 { 103 GNUNET_break (0); 104 tflc->extract_failed = true; 105 GNUNET_PQ_cleanup_result (rs); 106 return; 107 } 108 { 109 const char *slugptrs[num_slugs]; 110 const char *pos = slugs; 111 112 for (size_t j = 0; j<num_slugs; j++) 113 { 114 slugptrs[j] = pos; 115 pos += strlen (pos) + 1; 116 } 117 118 bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch); 119 tflc->cb (tflc->cb_cls, 120 bucket_start, 121 num_slugs, 122 slugptrs, 123 counters); 124 } 125 GNUNET_PQ_cleanup_result (rs); 126 } 127 } 128 129 130 enum GNUNET_DB_QueryStatus 131 TALER_MERCHANTDB_lookup_statistics_counter_by_bucket2 ( 132 struct TALER_MERCHANTDB_PostgresContext *pg, 133 const char *instance_id, 134 const char *prefix, 135 const char *granularity, 136 uint64_t counter, 137 TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 cb, 138 void *cb_cls) 139 { 140 struct LookupCounterStatisticsContext2 context = { 141 .cb = cb, 142 .cb_cls = cb_cls, 143 /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb2 */ 144 .extract_failed = false, 145 .pg = pg 146 }; 147 struct GNUNET_PQ_QueryParam params[] = { 148 GNUNET_PQ_query_param_string (prefix), 149 GNUNET_PQ_query_param_string (granularity), 150 GNUNET_PQ_query_param_uint64 (&counter), 151 GNUNET_PQ_query_param_end 152 }; 153 enum GNUNET_DB_QueryStatus qs; 154 155 GNUNET_assert (NULL != pg->current_merchant_id); 156 GNUNET_assert (0 == strcmp (instance_id, 157 pg->current_merchant_id)); 158 check_connection (pg); 159 TMH_PQ_prepare_anon (pg, 160 "SELECT" 161 " msbc.bucket_start" 162 ",ARRAY_AGG(msbm.slug) AS slugs" 163 ",ARRAY_AGG(msbc.cumulative_number) AS counters" 164 " FROM merchant_statistic_bucket_counter msbc" 165 " JOIN merchant_statistic_bucket_meta msbm" 166 " USING (bmeta_serial_id)" 167 " WHERE msbm.slug LIKE $1" 168 " AND msbc.bucket_range=$2::TEXT::merchant.statistic_range" 169 " AND msbm.stype = 'number'" 170 " GROUP BY msbc.bucket_start" 171 " ORDER BY msbc.bucket_start DESC" 172 " LIMIT $3"); 173 qs = GNUNET_PQ_eval_prepared_multi_select ( 174 pg->conn, 175 "", 176 params, 177 &lookup_statistics_counter_by_bucket_cb2, 178 &context); 179 /* If there was an error inside the cb, return a hard error. */ 180 if (context.extract_failed) 181 { 182 GNUNET_break (0); 183 return GNUNET_DB_STATUS_HARD_ERROR; 184 } 185 return qs; 186 }