lookup_statistics_amount_by_bucket2.c (5279B)
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_amount_by_bucket2.c 18 * @brief Implementation of the lookup_statistics_amount_by_bucket2 function for Postgres 19 * @author Christian Grothoff 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 "merchant-database/lookup_statistics_amount_by_bucket2.h" 26 #include "helper.h" 27 28 29 /** 30 * Context used for TALER_MERCHANTDB_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 TALER_MERCHANTDB_PostgresContext *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 TALER_MERCHANTDB_PostgresContext *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 TALER_MERCHANTDB_lookup_statistics_amount_by_bucket2 ( 110 struct TALER_MERCHANTDB_PostgresContext *pg, 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 LookupAmountStatisticsContext2 context = { 119 .cb = cb, 120 .cb_cls = cb_cls, 121 /* Can be overwritten by the lookup_statistics_amount_by_bucket_cb2 */ 122 .extract_failed = false, 123 .pg = pg, 124 }; 125 struct GNUNET_PQ_QueryParam params[] = { 126 GNUNET_PQ_query_param_string (instance_id), 127 GNUNET_PQ_query_param_string (slug), 128 GNUNET_PQ_query_param_string (granularity), 129 GNUNET_PQ_query_param_uint64 (&counter), 130 GNUNET_PQ_query_param_end 131 }; 132 enum GNUNET_DB_QueryStatus qs; 133 134 check_connection (pg); 135 PREPARE (pg, 136 "lookup_statistics_amount_by_bucket2", 137 "SELECT" 138 " msba.bucket_start" 139 ",ARRAY_AGG (" 140 " ROW(msba.cumulative_value::INT8, msba.cumulative_frac::INT4, msba.curr)::taler_amount_currency" 141 " ) AS cumulative_amounts" 142 " FROM merchant_statistic_bucket_meta msbm" 143 " JOIN merchant_statistic_bucket_amount msba" 144 " USING (bmeta_serial_id)" 145 " JOIN merchant_instances mi" 146 " USING (merchant_serial)" 147 " WHERE mi.merchant_id=$1" 148 " AND msbm.slug=$2" 149 " AND msba.bucket_range=$3::TEXT::statistic_range" 150 " AND msbm.stype='amount'" 151 " GROUP BY msba.bucket_start" 152 " ORDER BY msba.bucket_start DESC" 153 " LIMIT $4;"); 154 qs = GNUNET_PQ_eval_prepared_multi_select ( 155 pg->conn, 156 "lookup_statistics_amount_by_bucket2", 157 params, 158 &lookup_statistics_amount_by_bucket_cb2, 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 }