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-by-count.c (4655B)


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