taler-auditor-httpd_get-monitoring-emergency-by-count.c (4748B)
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 #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-emergency-by-count.h" 25 #define TALER_AUDITORDB_EMERGENCIES_BY_COUNT_RESULT_CLOSURE json_t 26 #include "auditor-database/iterate_emergencies_by_count.h" 27 #include "auditor-database/preflight.h" 28 29 /** 30 * Add emergency-by-count to the list. 31 * 32 * @param[in,out] list a `json_t *` array to extend 33 * @param dc struct of inconsistencies 34 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 35 */ 36 static enum GNUNET_GenericReturnValue 37 process_emergency_by_count ( 38 json_t *list, 39 const struct TALER_AUDITORDB_EmergenciesByCount *dc) 40 { 41 json_t *obj; 42 43 obj = GNUNET_JSON_PACK ( 44 GNUNET_JSON_pack_uint64 ("row_id", 45 dc->row_id), 46 GNUNET_JSON_pack_data_auto ("h_denom_pub", 47 &dc->denompub_h), 48 GNUNET_JSON_pack_int64 ("num_issued", 49 dc->num_issued), 50 GNUNET_JSON_pack_int64 ("num_known", 51 dc->num_known), 52 TALER_JSON_pack_amount ("risk", 53 &dc->risk), 54 GNUNET_JSON_pack_timestamp ("start", 55 GNUNET_TIME_absolute_to_timestamp ( 56 dc->start)), 57 GNUNET_JSON_pack_timestamp ("deposit_end", 58 GNUNET_TIME_absolute_to_timestamp ( 59 dc->deposit_end)), 60 TALER_JSON_pack_amount ("value", 61 &dc->value), 62 GNUNET_JSON_pack_bool ("suppressed", 63 dc->suppressed) 64 ); 65 GNUNET_break (0 == 66 json_array_append_new (list, 67 obj)); 68 return GNUNET_OK; 69 } 70 71 72 enum MHD_Result 73 TAH_get_monitoring_emergency_by_count ( 74 struct TAH_RequestHandler *rh, 75 struct MHD_Connection *connection, 76 void **connection_cls, 77 const char *upload_data, 78 size_t *upload_data_size, 79 const char *const args[]) 80 { 81 json_t *ja; 82 enum GNUNET_DB_QueryStatus qs; 83 int64_t limit = -20; 84 uint64_t offset; 85 bool return_suppressed = false; 86 87 (void) rh; 88 (void) connection_cls; 89 (void) upload_data; 90 (void) upload_data_size; 91 if (GNUNET_SYSERR == 92 TALER_AUDITORDB_preflight (TAH_apg)) 93 { 94 GNUNET_break (0); 95 return TALER_MHD_reply_with_error (connection, 96 MHD_HTTP_INTERNAL_SERVER_ERROR, 97 TALER_EC_GENERIC_DB_SETUP_FAILED, 98 NULL); 99 } 100 TAH_PARSE_REQUEST_LIMIT (connection, 101 "limit", 102 &limit); 103 if (limit < 0) 104 offset = TAH_SAFE_UINT64_MAX; 105 else 106 offset = 0; 107 TAH_PARSE_SAFE_REQUEST_NUMBER (connection, 108 "offset", 109 &offset); 110 { 111 const char *ret_s 112 = MHD_lookup_connection_value (connection, 113 MHD_GET_ARGUMENT_KIND, 114 "return_suppressed"); 115 if (ret_s != NULL && strcmp (ret_s, "true") == 0) 116 { 117 return_suppressed = true; 118 } 119 } 120 121 ja = json_array (); 122 GNUNET_break (NULL != ja); 123 qs = TALER_AUDITORDB_iterate_emergencies_by_count ( 124 TAH_apg, 125 limit, 126 offset, 127 return_suppressed, 128 &process_emergency_by_count, 129 ja); 130 if (0 > qs) 131 { 132 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 133 json_decref (ja); 134 TALER_LOG_WARNING ( 135 "Failed to handle GET /emergency-by-count\n"); 136 return TALER_MHD_reply_with_error (connection, 137 MHD_HTTP_INTERNAL_SERVER_ERROR, 138 TALER_EC_GENERIC_DB_FETCH_FAILED, 139 "emergency-by-count"); 140 } 141 return TAH_reply_json_records (connection, 142 ja); 143 }