merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

pg_lookup_statistics_amount_by_interval.c (7223B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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 backenddb/pg_lookup_statistics_amount_by_interval.c
     18  * @brief Implementation of the lookup_statistics_amount_by_interval function for Postgres
     19  * @author Martin Schanzenbach
     20  */
     21 #include "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_lookup_statistics_amount_by_interval.h"
     26 #include "pg_helper.h"
     27 #include "taler_merchantdb_plugin.h"
     28 
     29 
     30 /**
     31  * Context used for TMH_PG_lookup_statistics_amount().
     32  */
     33 struct LookupAmountStatisticsContext
     34 {
     35   /**
     36    * Function to call with the results.
     37    */
     38   TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb;
     39 
     40   /**
     41    * Closure for @a cb.
     42    */
     43   void *cb_cls;
     44 
     45   /**
     46    * Did database result extraction fail?
     47    */
     48   bool extract_failed;
     49 
     50   /**
     51    * Description of statistic
     52    */
     53   char*description;
     54 };
     55 
     56 /**
     57  * Function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results about statistics.
     59  *
     60  * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 lookup_statistics_amount_by_interval_desc_cb (void *cls,
     66                                               PGresult *result,
     67                                               unsigned int num_results)
     68 {
     69   struct LookupAmountStatisticsContext *tflc = cls;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     char *description;
     74     struct GNUNET_PQ_ResultSpec rs[] = {
     75       GNUNET_PQ_result_spec_string ("description",
     76                                     &description),
     77       GNUNET_PQ_result_spec_end
     78     };
     79 
     80     if (GNUNET_OK !=
     81         GNUNET_PQ_extract_result (result,
     82                                   rs,
     83                                   i))
     84     {
     85       GNUNET_break (0);
     86       tflc->extract_failed = true;
     87       return;
     88     }
     89 
     90     tflc->description = GNUNET_strdup (description);
     91 
     92     GNUNET_PQ_cleanup_result (rs);
     93   }
     94 }
     95 
     96 
     97 /**
     98  * Function to be called with the results of a SELECT statement
     99  * that has returned @a num_results results about statistics.
    100  *
    101  * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
    102  * @param result the postgres result
    103  * @param num_results the number of results in @a result
    104  */
    105 static void
    106 lookup_statistics_amount_by_interval_cb (void *cls,
    107                                          PGresult *result,
    108                                          unsigned int num_results)
    109 {
    110   struct LookupAmountStatisticsContext *tflc = cls;
    111   struct TALER_Amount *amounts = NULL;
    112   char *resp_desc = NULL;
    113   uint64_t cur_interval_start_ago = UINT64_MAX;
    114   unsigned int amounts_len = 0;
    115 
    116   for (unsigned int i = 0; i < num_results; i++)
    117   {
    118     struct TALER_Amount cumulative_amount;
    119     uint64_t interval_start_ago;
    120     struct GNUNET_PQ_ResultSpec rs[] = {
    121       GNUNET_PQ_result_spec_uint64 ("range",
    122                                     &interval_start_ago),
    123       TALER_PQ_result_spec_amount_with_currency ("rvalue",
    124                                                  &cumulative_amount),
    125       GNUNET_PQ_result_spec_end
    126     };
    127 
    128     if (GNUNET_OK !=
    129         GNUNET_PQ_extract_result (result,
    130                                   rs,
    131                                   i))
    132     {
    133       GNUNET_break (0);
    134       tflc->extract_failed = true;
    135       return;
    136     }
    137 
    138     /* Call callback if the bucket changed */
    139     if ( (interval_start_ago != cur_interval_start_ago) &&
    140          (i > 0) )
    141     {
    142       struct GNUNET_TIME_Timestamp interval_start;
    143 
    144       interval_start = GNUNET_TIME_timestamp_get ();
    145       interval_start.abs_time.abs_value_us -= interval_start_ago * 1000 * 1000;
    146       tflc->cb (tflc->cb_cls,
    147                 resp_desc,
    148                 interval_start,
    149                 amounts_len,
    150                 amounts);
    151       GNUNET_array_grow (amounts,
    152                          amounts_len,
    153                          0);
    154       GNUNET_free (resp_desc);
    155     }
    156     cur_interval_start_ago = interval_start_ago;
    157     GNUNET_array_append (amounts,
    158                          amounts_len,
    159                          cumulative_amount);
    160     GNUNET_PQ_cleanup_result (rs);
    161   }
    162   if (0 != amounts_len)
    163   {
    164     struct GNUNET_TIME_Timestamp interval_start;
    165 
    166     interval_start = GNUNET_TIME_timestamp_from_s (cur_interval_start_ago);
    167     tflc->cb (tflc->cb_cls,
    168               resp_desc,
    169               interval_start,
    170               amounts_len,
    171               amounts);
    172     GNUNET_array_grow (amounts,
    173                        amounts_len,
    174                        0);
    175     GNUNET_free (resp_desc);
    176   }
    177 }
    178 
    179 
    180 enum GNUNET_DB_QueryStatus
    181 TMH_PG_lookup_statistics_amount_by_interval (
    182   void *cls,
    183   const char *instance_id,
    184   const char *slug,
    185   TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb,
    186   void *cb_cls)
    187 {
    188   struct PostgresClosure *pg = cls;
    189   struct LookupAmountStatisticsContext context = {
    190     .cb = cb,
    191     .cb_cls = cb_cls,
    192     /* Can be overwritten by the lookup_statistics_amount_by_interval_cb */
    193     .extract_failed = false,
    194     .description = NULL
    195   };
    196   struct GNUNET_PQ_QueryParam descParams[] = {
    197     GNUNET_PQ_query_param_string (slug),
    198     GNUNET_PQ_query_param_end
    199   };
    200   struct GNUNET_PQ_QueryParam params[] = {
    201     GNUNET_PQ_query_param_string (instance_id),
    202     GNUNET_PQ_query_param_string (slug),
    203     GNUNET_PQ_query_param_end
    204   };
    205   enum GNUNET_DB_QueryStatus qs;
    206 
    207   check_connection (pg);
    208   PREPARE (pg,
    209            "lookup_statistics_amount_by_interval_description",
    210            "SELECT description"
    211            " FROM merchant_statistic_interval_meta"
    212            " WHERE slug=$1 LIMIT 1");
    213   qs = GNUNET_PQ_eval_prepared_multi_select (
    214     pg->conn,
    215     "lookup_statistics_amount_by_interval_description",
    216     descParams,
    217     &lookup_statistics_amount_by_interval_desc_cb,
    218     &context);
    219   /* If there was an error inside the cb, return a hard error. */
    220   if (context.extract_failed)
    221   {
    222     GNUNET_break (0);
    223     return GNUNET_DB_STATUS_HARD_ERROR;
    224   }
    225   PREPARE (pg,
    226            "lookup_statistics_amount_by_interval",
    227            "SELECT *"
    228            " FROM merchant_statistic_interval_amount_get($2,$1)");
    229   qs = GNUNET_PQ_eval_prepared_multi_select (
    230     pg->conn,
    231     "lookup_statistics_amount_by_interval",
    232     params,
    233     &lookup_statistics_amount_by_interval_cb,
    234     &context);
    235   if (NULL != context.description)
    236     GNUNET_free (context.description);
    237   /* If there was an error inside the cb, return a hard error. */
    238   if (context.extract_failed)
    239   {
    240     GNUNET_break (0);
    241     return GNUNET_DB_STATUS_HARD_ERROR;
    242   }
    243   return qs;
    244 }