taler-auditor-httpd_get-monitoring-historic-denomination-revenue.c (4352B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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 18 #include <gnunet/gnunet_util_lib.h> 19 #include <gnunet/gnunet_json_lib.h> 20 #include <jansson.h> 21 #include <microhttpd.h> 22 #include <pthread.h> 23 #include "taler/taler_json_lib.h" 24 #include "taler/taler_mhd_lib.h" 25 #include "taler-auditor-httpd.h" 26 #include "taler-auditor-httpd_get-monitoring-historic-denomination-revenue.h" 27 #include "auditor-database/preflight.h" 28 #include "auditor-database/select_historic_denom_revenue.h" 29 30 31 /** 32 * Add historic-denomination-revenue to the list. 33 * 34 * @param[in,out] cls a `json_t *` array to extend 35 * @param serial_id location of the @a dc in the database 36 * @param denom_pub_hash public key hash of the denomination 37 * @param revenue_timestamp when was the revenue effective 38 * @param revenue_balance how much in profit did we make from this denomination 39 * @param loss_balance how much loss did we make from this denomination 40 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 41 */ 42 static enum GNUNET_GenericReturnValue 43 process_historic_denomination_revenue ( 44 void *cls, 45 uint64_t serial_id, 46 const struct TALER_DenominationHashP *denom_pub_hash, 47 struct GNUNET_TIME_Timestamp revenue_timestamp, 48 const struct TALER_Amount *revenue_balance, 49 const struct TALER_Amount *loss_balance) 50 { 51 json_t *list = cls; 52 json_t *obj; 53 54 obj = GNUNET_JSON_PACK ( 55 GNUNET_JSON_pack_uint64 ("serial_id", 56 serial_id), 57 GNUNET_JSON_pack_data_auto ("denom_pub_hash", 58 denom_pub_hash), 59 TALER_JSON_pack_time_abs_human ("revenue_timestamp", 60 revenue_timestamp.abs_time), 61 TALER_JSON_pack_amount ("revenue_balance", 62 revenue_balance), 63 TALER_JSON_pack_amount ("loss_balance", 64 loss_balance) 65 ); 66 GNUNET_break (0 == 67 json_array_append_new (list, 68 obj)); 69 70 71 return GNUNET_OK; 72 } 73 74 75 MHD_RESULT 76 TAH_get_monitoring_historic_denomination_revenue ( 77 struct TAH_RequestHandler *rh, 78 struct MHD_Connection *connection, 79 void **connection_cls, 80 const char *upload_data, 81 size_t *upload_data_size, 82 const char *const args[]) 83 { 84 json_t *ja; 85 int64_t limit = -20; 86 uint64_t offset; 87 enum GNUNET_DB_QueryStatus qs; 88 89 (void) rh; 90 (void) connection_cls; 91 (void) upload_data; 92 (void) upload_data_size; 93 if (GNUNET_SYSERR == 94 TALER_AUDITORDB_preflight (TAH_apg)) 95 { 96 GNUNET_break (0); 97 return TALER_MHD_reply_with_error ( 98 connection, 99 MHD_HTTP_INTERNAL_SERVER_ERROR, 100 TALER_EC_GENERIC_DB_SETUP_FAILED, 101 NULL); 102 } 103 TALER_MHD_parse_request_snumber (connection, 104 "limit", 105 &limit); 106 if (limit < 0) 107 offset = INT64_MAX; 108 else 109 offset = 0; 110 TALER_MHD_parse_request_number (connection, 111 "offset", 112 &offset); 113 ja = json_array (); 114 GNUNET_break (NULL != ja); 115 qs = TALER_AUDITORDB_select_historic_denom_revenue ( 116 TAH_apg, 117 limit, 118 offset, 119 &process_historic_denomination_revenue, 120 ja); 121 if (0 > qs) 122 { 123 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 124 json_decref (ja); 125 TALER_LOG_WARNING ( 126 "Failed to handle GET /historic-denomination-revenue"); 127 return TALER_MHD_reply_with_error ( 128 connection, 129 MHD_HTTP_INTERNAL_SERVER_ERROR, 130 TALER_EC_GENERIC_DB_FETCH_FAILED, 131 "select_historic_denom_revenue"); 132 } 133 return TALER_MHD_REPLY_JSON_PACK ( 134 connection, 135 MHD_HTTP_OK, 136 GNUNET_JSON_pack_array_steal ( 137 "historic-denomination-revenue", 138 ja)); 139 }