pg_lookup_statistics_amount_by_bucket2.c (5220B)
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_amount_by_bucket2.c 18 * @brief Implementation of the lookup_statistics_amount_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_amount_by_bucket2.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Context used for TMH_PG_lookup_statistics_amount_by_bucket2(). 31 */ 32 struct LookupAmountStatisticsContext2 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb; 38 39 /** 40 * Closure for @a cb. 41 */ 42 void *cb_cls; 43 44 /** 45 * Did database result extraction fail? 46 */ 47 bool extract_failed; 48 49 /** 50 * Postgres context for array lookups 51 */ 52 struct PostgresClosure *pg; 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 LookupAmountStatisticsContext2 *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 lookup_statistics_amount_by_bucket_cb2 (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct LookupAmountStatisticsContext2 *tflc = cls; 70 struct PostgresClosure *pg = tflc->pg; 71 72 for (unsigned int i = 0; i < num_results; i++) 73 { 74 struct TALER_Amount *amounts; 75 size_t num_amounts; 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 TALER_PQ_result_spec_array_amount_with_currency (pg->conn, 81 "cumulative_amounts", 82 &num_amounts, 83 &amounts), 84 GNUNET_PQ_result_spec_end 85 }; 86 struct GNUNET_TIME_Timestamp bucket_start; 87 88 if (GNUNET_OK != 89 GNUNET_PQ_extract_result (result, 90 rs, 91 i)) 92 { 93 GNUNET_break (0); 94 tflc->extract_failed = true; 95 return; 96 } 97 98 bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch); 99 tflc->cb (tflc->cb_cls, 100 bucket_start, 101 num_amounts, 102 amounts); 103 GNUNET_PQ_cleanup_result (rs); 104 } 105 } 106 107 108 enum GNUNET_DB_QueryStatus 109 TMH_PG_lookup_statistics_amount_by_bucket2 ( 110 void *cls, 111 const char *instance_id, 112 const char *slug, 113 const char *granularity, 114 uint64_t counter, 115 TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb, 116 void *cb_cls) 117 { 118 struct PostgresClosure *pg = cls; 119 struct LookupAmountStatisticsContext2 context = { 120 .cb = cb, 121 .cb_cls = cb_cls, 122 /* Can be overwritten by the lookup_statistics_amount_by_bucket_cb2 */ 123 .extract_failed = false, 124 .pg = pg, 125 }; 126 struct GNUNET_PQ_QueryParam params[] = { 127 GNUNET_PQ_query_param_string (instance_id), 128 GNUNET_PQ_query_param_string (slug), 129 GNUNET_PQ_query_param_string (granularity), 130 GNUNET_PQ_query_param_uint64 (&counter), 131 GNUNET_PQ_query_param_end 132 }; 133 enum GNUNET_DB_QueryStatus qs; 134 135 check_connection (pg); 136 PREPARE (pg, 137 "lookup_statistics_amount_by_bucket2", 138 "SELECT" 139 " msba.bucket_start" 140 ",ARRAY_AGG (" 141 " ROW(msba.cumulative_value::INT8, msba.cumulative_frac::INT4, msba.curr)::taler_amount_currency" 142 " ) AS cumulative_amounts" 143 " FROM merchant_statistic_bucket_meta msbm" 144 " JOIN merchant_statistic_bucket_amount msba" 145 " USING (bmeta_serial_id)" 146 " JOIN merchant_instances mi" 147 " USING (merchant_serial)" 148 " WHERE mi.merchant_id=$1" 149 " AND msbm.slug=$2" 150 " AND msba.bucket_range=$3::TEXT::statistic_range" 151 " AND msbm.stype='amount'" 152 " GROUP BY msba.bucket_start" 153 " ORDER BY msba.bucket_start DESC" 154 " LIMIT $4;"); 155 qs = GNUNET_PQ_eval_prepared_multi_select ( 156 pg->conn, 157 "lookup_statistics_amount_by_bucket2", 158 params, 159 &lookup_statistics_amount_by_bucket_cb2, 160 &context); 161 /* If there was an error inside the cb, return a hard error. */ 162 if (context.extract_failed) 163 { 164 GNUNET_break (0); 165 return GNUNET_DB_STATUS_HARD_ERROR; 166 } 167 return qs; 168 }