exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-auditor-httpd_get-monitoring-emergency.c (4598B)


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