taler-auditor-httpd_get-monitoring-early-aggregation.c (4270B)
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 #include <gnunet/gnunet_util_lib.h> 17 #include <gnunet/gnunet_json_lib.h> 18 #include <jansson.h> 19 #include <microhttpd.h> 20 #include <pthread.h> 21 #include "taler/taler_json_lib.h" 22 #include "taler/taler_mhd_lib.h" 23 #include "taler-auditor-httpd.h" 24 #include "taler-auditor-httpd_get-monitoring-early-aggregation.h" 25 #include "auditor-database/preflight.h" 26 #include "auditor-database/select_early_aggregations.h" 27 28 29 /** 30 * Add early aggregation record to the list. 31 * 32 * @param[in,out] cls a `json_t *` array to extend 33 * @param ea early aggregation record 34 * @return #GNUNET_OK 35 */ 36 static enum GNUNET_GenericReturnValue 37 add_early_aggregation ( 38 void *cls, 39 const struct TALER_AUDITORDB_EarlyAggregation *ea) 40 { 41 json_t *list = cls; 42 json_t *obj; 43 44 obj = GNUNET_JSON_PACK ( 45 GNUNET_JSON_pack_uint64 ("row_id", 46 ea->row_id), 47 GNUNET_JSON_pack_uint64 ("batch_deposit_serial_id", 48 ea->batch_deposit_serial_id), 49 GNUNET_JSON_pack_uint64 ("tracking_serial_id", 50 ea->tracking_serial_id), 51 TALER_JSON_pack_amount ("balance", 52 &ea->total), 53 GNUNET_JSON_pack_bool ("suppressed", 54 ea->suppressed) 55 ); 56 GNUNET_break (0 == 57 json_array_append_new (list, 58 obj)); 59 return GNUNET_OK; 60 } 61 62 63 MHD_RESULT 64 TAH_get_monitoring_early_aggregation ( 65 struct TAH_RequestHandler *rh, 66 struct MHD_Connection *connection, 67 void **connection_cls, 68 const char *upload_data, 69 size_t *upload_data_size, 70 const char *const args[]) 71 { 72 json_t *ja; 73 enum GNUNET_DB_QueryStatus qs; 74 int64_t limit = -20; 75 uint64_t offset; 76 bool return_suppressed = false; 77 78 if (GNUNET_SYSERR == 79 TALER_AUDITORDB_preflight (TAH_apg)) 80 { 81 GNUNET_break (0); 82 return TALER_MHD_reply_with_error (connection, 83 MHD_HTTP_INTERNAL_SERVER_ERROR, 84 TALER_EC_GENERIC_DB_SETUP_FAILED, 85 NULL); 86 } 87 TALER_MHD_parse_request_snumber (connection, 88 "limit", 89 &limit); 90 if (limit < 0) 91 offset = INT64_MAX; 92 else 93 offset = 0; 94 TALER_MHD_parse_request_number (connection, 95 "offset", 96 &offset); 97 { 98 const char *ret_s = MHD_lookup_connection_value (connection, 99 MHD_GET_ARGUMENT_KIND, 100 "return_suppressed"); 101 if ( (NULL != ret_s) && 102 (0 == strcmp (ret_s, 103 "true")) ) 104 { 105 return_suppressed = true; 106 } 107 } 108 ja = json_array (); 109 GNUNET_break (NULL != ja); 110 qs = TALER_AUDITORDB_select_early_aggregations ( 111 TAH_apg, 112 limit, 113 offset, 114 return_suppressed, 115 &add_early_aggregation, 116 ja); 117 if (0 > qs) 118 { 119 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 120 json_decref (ja); 121 TALER_LOG_WARNING ( 122 "Failed to handle GET /early-aggregation in database\n"); 123 return TALER_MHD_reply_with_error (connection, 124 MHD_HTTP_INTERNAL_SERVER_ERROR, 125 TALER_EC_GENERIC_DB_FETCH_FAILED, 126 "select_early_aggregations"); 127 } 128 return TALER_MHD_REPLY_JSON_PACK ( 129 connection, 130 MHD_HTTP_OK, 131 GNUNET_JSON_pack_array_steal ("early_aggregations", 132 ja)); 133 }