exchange

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

pg_select_aml_statistics.c (4050B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024, 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 /**
     17  * @file exchangedb/pg_select_aml_statistics.c
     18  * @brief Implementation of the select_aml_statistics function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_select_aml_statistics.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #get_statistics_cb().
     31  */
     32 struct GetStatisticsContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   TALER_EXCHANGEDB_AmlStatisticsCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Plugin context.
     46    */
     47   struct PostgresClosure *pg;
     48 
     49   /**
     50    * Flag set to #GNUNET_OK as long as everything is fine.
     51    */
     52   enum GNUNET_GenericReturnValue status;
     53 
     54 };
     55 
     56 
     57 /**
     58  * Invoke the callback for each result.
     59  *
     60  * @param cls a `struct GetStatisticsContext *`
     61  * @param result SQL result
     62  * @param num_results number of rows in @a result
     63  */
     64 static void
     65 get_statistics_cb (void *cls,
     66                    PGresult *result,
     67                    unsigned int num_results)
     68 {
     69   struct GetStatisticsContext *ctx = cls;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     uint64_t val;
     74     char *name;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_string ("name",
     77                                     &name),
     78       GNUNET_PQ_result_spec_uint64 ("value",
     79                                     &val),
     80       GNUNET_PQ_result_spec_end
     81     };
     82 
     83     if (GNUNET_OK !=
     84         GNUNET_PQ_extract_result (result,
     85                                   rs,
     86                                   i))
     87     {
     88       GNUNET_break (0);
     89       ctx->status = GNUNET_SYSERR;
     90       return;
     91     }
     92     ctx->cb (ctx->cb_cls,
     93              name,
     94              val);
     95     GNUNET_PQ_cleanup_result (rs);
     96   }
     97 }
     98 
     99 
    100 enum GNUNET_DB_QueryStatus
    101 TEH_PG_select_aml_statistics (
    102   void *cls,
    103   size_t num_names,
    104   const char *names[static num_names],
    105   struct GNUNET_TIME_Timestamp start_date,
    106   struct GNUNET_TIME_Timestamp end_date,
    107   TALER_EXCHANGEDB_AmlStatisticsCallback cb,
    108   void *cb_cls)
    109 {
    110   struct PostgresClosure *pg = cls;
    111   struct GetStatisticsContext ctx = {
    112     .cb = cb,
    113     .cb_cls = cb_cls,
    114     .pg = pg,
    115     .status = GNUNET_OK
    116   };
    117   struct GNUNET_PQ_QueryParam params[] = {
    118     GNUNET_PQ_query_param_array_ptrs_string (num_names,
    119                                              names,
    120                                              pg->conn),
    121     GNUNET_PQ_query_param_timestamp (&start_date),
    122     GNUNET_PQ_query_param_timestamp (&end_date),
    123     GNUNET_PQ_query_param_end
    124   };
    125   enum GNUNET_DB_QueryStatus qs;
    126 
    127   PREPARE (pg,
    128            "select_aml_statistics",
    129            "SELECT "
    130            " event_type AS name"
    131            ",COUNT(*) AS value"
    132            " FROM kyc_events"
    133            " WHERE event_type = ANY ($1)"
    134            "   AND event_timestamp >= $2"
    135            "   AND event_timestamp < $3"
    136            " GROUP BY event_type;");
    137   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    138                                              "select_aml_statistics",
    139                                              params,
    140                                              &get_statistics_cb,
    141                                              &ctx);
    142   GNUNET_PQ_cleanup_query_params_closures (params);
    143   if (GNUNET_OK != ctx.status)
    144     return GNUNET_DB_STATUS_HARD_ERROR;
    145   return qs;
    146 }