merchant

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

lookup_statistics_amount_by_interval.c (7085B)


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