pg_lookup_statistics_counter_by_bucket2.c (5754B)
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 "taler/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 lookup_statistics_counter_by_bucket_cb2 (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct LookupCounterStatisticsContext2 *tflc = cls; 70 struct PostgresClosure *pg = tflc->pg; 71 72 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 struct GNUNET_PQ_ResultSpec rs[] = { 80 GNUNET_PQ_result_spec_uint64 ("bucket_start", 81 &bucket_start_epoch), 82 GNUNET_PQ_result_spec_array_uint64 (pg->conn, 83 "counters", 84 &num_counters, 85 &counters), 86 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 if (GNUNET_OK != 95 GNUNET_PQ_extract_result (result, 96 rs, 97 i)) 98 { 99 GNUNET_break (0); 100 tflc->extract_failed = true; 101 return; 102 } 103 if (num_slugs != num_counters) 104 { 105 GNUNET_break (0); 106 tflc->extract_failed = true; 107 GNUNET_PQ_cleanup_result (rs); 108 return; 109 } 110 { 111 const char *slugptrs[num_slugs]; 112 const char *pos = slugs; 113 114 for (size_t j = 0; j<num_slugs; j++) 115 { 116 slugptrs[j] = pos; 117 pos += strlen (pos) + 1; 118 } 119 120 bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch); 121 tflc->cb (tflc->cb_cls, 122 bucket_start, 123 num_slugs, 124 slugptrs, 125 counters); 126 } 127 GNUNET_PQ_cleanup_result (rs); 128 } 129 } 130 131 132 enum GNUNET_DB_QueryStatus 133 TMH_PG_lookup_statistics_counter_by_bucket2 ( 134 void *cls, 135 const char *instance_id, 136 const char *prefix, 137 const char *granularity, 138 uint64_t counter, 139 TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 cb, 140 void *cb_cls) 141 { 142 struct PostgresClosure *pg = cls; 143 struct LookupCounterStatisticsContext2 context = { 144 .cb = cb, 145 .cb_cls = cb_cls, 146 /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb2 */ 147 .extract_failed = false, 148 .pg = pg 149 }; 150 struct GNUNET_PQ_QueryParam params[] = { 151 GNUNET_PQ_query_param_string (instance_id), 152 GNUNET_PQ_query_param_string (prefix), 153 GNUNET_PQ_query_param_string (granularity), 154 GNUNET_PQ_query_param_uint64 (&counter), 155 GNUNET_PQ_query_param_end 156 }; 157 enum GNUNET_DB_QueryStatus qs; 158 159 check_connection (pg); 160 PREPARE (pg, 161 "lookup_statistics_counter_by_bucket2", 162 "SELECT" 163 " msbc.bucket_start" 164 ",ARRAY_AGG(msbm.slug) AS slugs" 165 ",ARRAY_AGG(msbc.cumulative_number) AS counters" 166 " FROM merchant_statistic_bucket_counter msbc" 167 " JOIN merchant_statistic_bucket_meta msbm" 168 " USING (bmeta_serial_id)" 169 " JOIN merchant_instances mi" 170 " USING (merchant_serial)" 171 " WHERE mi.merchant_id=$1" 172 " AND msbm.slug LIKE $2" 173 " AND msbc.bucket_range=$3::TEXT::statistic_range" 174 " AND msbm.stype = 'number'" 175 " GROUP BY msbc.bucket_start" 176 " ORDER BY msbc.bucket_start DESC" 177 " LIMIT $4"); 178 qs = GNUNET_PQ_eval_prepared_multi_select ( 179 pg->conn, 180 "lookup_statistics_counter_by_bucket2", 181 params, 182 &lookup_statistics_counter_by_bucket_cb2, 183 &context); 184 /* If there was an error inside the cb, return a hard error. */ 185 if (context.extract_failed) 186 { 187 GNUNET_break (0); 188 return GNUNET_DB_STATUS_HARD_ERROR; 189 } 190 return qs; 191 }