pg_lookup_statistics_counter_by_bucket.c (5243B)
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_bucket.c 18 * @brief Implementation of the lookup_statistics_counter_by_bucket function for Postgres 19 * @author Martin Schanzenbach 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_bucket.h" 26 #include "pg_helper.h" 27 #include "taler_merchantdb_plugin.h" 28 29 30 /** 31 * Context used for TMH_PG_lookup_statistics_counter(). 32 */ 33 struct LookupCounterStatisticsContext 34 { 35 /** 36 * Function to call with the results. 37 */ 38 TALER_MERCHANTDB_CounterByBucketStatisticsCallback cb; 39 40 /** 41 * Closure for @a cb. 42 */ 43 void *cb_cls; 44 45 /** 46 * Did database result extraction fail? 47 */ 48 bool extract_failed; 49 }; 50 51 52 /** 53 * Function to be called with the results of a SELECT statement 54 * that has returned @a num_results results about token families. 55 * 56 * @param[in,out] cls of type `struct LookupTokenFamiliesContext *` 57 * @param result the postgres result 58 * @param num_results the number of results in @a result 59 */ 60 static void 61 lookup_statistics_counter_by_bucket_cb (void *cls, 62 PGresult *result, 63 unsigned int num_results) 64 { 65 struct LookupCounterStatisticsContext *tflc = cls; 66 67 for (unsigned int i = 0; i < num_results; i++) 68 { 69 char *description; 70 char *bucket_range; 71 uint64_t cumulative_number; 72 uint64_t bucket_start_epoch; 73 uint64_t bucket_end_epoch; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_string ("description", 76 &description), 77 GNUNET_PQ_result_spec_uint64 ("bucket_start", 78 &bucket_start_epoch), 79 GNUNET_PQ_result_spec_uint64 ("bucket_end", 80 &bucket_end_epoch), 81 GNUNET_PQ_result_spec_string ("bucket_range", 82 &bucket_range), 83 GNUNET_PQ_result_spec_uint64 ("cumulative_number", 84 &cumulative_number), 85 GNUNET_PQ_result_spec_end 86 }; 87 struct GNUNET_TIME_Timestamp bucket_start; 88 struct GNUNET_TIME_Timestamp bucket_end; 89 90 if (GNUNET_OK != 91 GNUNET_PQ_extract_result (result, 92 rs, 93 i)) 94 { 95 GNUNET_break (0); 96 tflc->extract_failed = true; 97 return; 98 } 99 100 bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch); 101 bucket_end = GNUNET_TIME_timestamp_from_s (bucket_end_epoch); 102 tflc->cb (tflc->cb_cls, 103 description, 104 bucket_start, 105 bucket_end, 106 bucket_range, 107 cumulative_number); 108 GNUNET_PQ_cleanup_result (rs); 109 } 110 } 111 112 113 enum GNUNET_DB_QueryStatus 114 TMH_PG_lookup_statistics_counter_by_bucket ( 115 void *cls, 116 const char *instance_id, 117 const char *slug, 118 TALER_MERCHANTDB_CounterByBucketStatisticsCallback cb, 119 void *cb_cls) 120 { 121 struct PostgresClosure *pg = cls; 122 struct LookupCounterStatisticsContext context = { 123 .cb = cb, 124 .cb_cls = cb_cls, 125 /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb */ 126 .extract_failed = false, 127 }; 128 struct GNUNET_PQ_QueryParam params[] = { 129 GNUNET_PQ_query_param_string (instance_id), 130 GNUNET_PQ_query_param_string (slug), 131 GNUNET_PQ_query_param_end 132 }; 133 enum GNUNET_DB_QueryStatus qs; 134 135 check_connection (pg); 136 PREPARE (pg, 137 "lookup_statistics_counter_by_bucket", 138 "SELECT" 139 " description" 140 ",bucket_start" 141 ",bucket_range::TEXT" 142 ",merchant_statistics_bucket_end(bucket_start, bucket_range) AS bucket_end" 143 ",cumulative_number" 144 " FROM merchant_statistic_bucket_counter" 145 " JOIN merchant_statistic_bucket_meta" 146 " USING (bmeta_serial_id)" 147 " JOIN merchant_instances" 148 " USING (merchant_serial)" 149 " WHERE merchant_instances.merchant_id=$1" 150 " AND " 151 " merchant_statistic_bucket_meta.slug=$2" 152 " AND " 153 " merchant_statistic_bucket_meta.stype = 'number'"); 154 qs = GNUNET_PQ_eval_prepared_multi_select ( 155 pg->conn, 156 "lookup_statistics_counter_by_bucket", 157 params, 158 &lookup_statistics_counter_by_bucket_cb, 159 &context); 160 /* If there was an error inside the cb, return a hard error. */ 161 if (context.extract_failed) 162 { 163 GNUNET_break (0); 164 return GNUNET_DB_STATUS_HARD_ERROR; 165 } 166 return qs; 167 }