merchant

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

lookup_statistics_amount_by_interval.c (7051B)


      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   uint64_t cur_interval_start_ago = UINT64_MAX;
    112   unsigned int amounts_len = 0;
    113 
    114   for (unsigned int i = 0; i < num_results; i++)
    115   {
    116     struct TALER_Amount cumulative_amount;
    117     uint64_t interval_start_ago;
    118     struct GNUNET_PQ_ResultSpec rs[] = {
    119       GNUNET_PQ_result_spec_uint64 ("range",
    120                                     &interval_start_ago),
    121       TALER_PQ_result_spec_amount_with_currency ("rvalue",
    122                                                  &cumulative_amount),
    123       GNUNET_PQ_result_spec_end
    124     };
    125 
    126     if (GNUNET_OK !=
    127         GNUNET_PQ_extract_result (result,
    128                                   rs,
    129                                   i))
    130     {
    131       GNUNET_break (0);
    132       tflc->extract_failed = true;
    133       return;
    134     }
    135 
    136     /* Call callback if the bucket changed */
    137     if ( (interval_start_ago != cur_interval_start_ago) &&
    138          (i > 0) )
    139     {
    140       struct GNUNET_TIME_Timestamp interval_start;
    141 
    142       interval_start = GNUNET_TIME_timestamp_get ();
    143       interval_start.abs_time.abs_value_us -= cur_interval_start_ago * 1000 * 1000;
    144       tflc->cb (tflc->cb_cls,
    145                 tflc->description,
    146                 interval_start,
    147                 amounts_len,
    148                 amounts);
    149       GNUNET_array_grow (amounts,
    150                          amounts_len,
    151                          0);
    152     }
    153     cur_interval_start_ago = interval_start_ago;
    154     GNUNET_array_append (amounts,
    155                          amounts_len,
    156                          cumulative_amount);
    157     GNUNET_PQ_cleanup_result (rs);
    158   }
    159   if (0 != amounts_len)
    160   {
    161     struct GNUNET_TIME_Timestamp interval_start;
    162 
    163     interval_start = GNUNET_TIME_timestamp_get ();
    164     interval_start.abs_time.abs_value_us -= cur_interval_start_ago * 1000 * 1000;
    165     tflc->cb (tflc->cb_cls,
    166               tflc->description,
    167               interval_start,
    168               amounts_len,
    169               amounts);
    170     GNUNET_array_grow (amounts,
    171                        amounts_len,
    172                        0);
    173   }
    174 }
    175 
    176 
    177 enum GNUNET_DB_QueryStatus
    178 TALER_MERCHANTDB_lookup_statistics_amount_by_interval (
    179   struct TALER_MERCHANTDB_PostgresContext *pg,
    180   const char *instance_id,
    181   const char *slug,
    182   TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb,
    183   void *cb_cls)
    184 {
    185   struct LookupAmountStatisticsContext context = {
    186     .cb = cb,
    187     .cb_cls = cb_cls,
    188     /* Can be overwritten by the lookup_statistics_amount_by_interval_cb */
    189     .extract_failed = false,
    190     .description = NULL
    191   };
    192   struct GNUNET_PQ_QueryParam descParams[] = {
    193     GNUNET_PQ_query_param_string (slug),
    194     GNUNET_PQ_query_param_end
    195   };
    196   struct GNUNET_PQ_QueryParam params[] = {
    197     GNUNET_PQ_query_param_string (slug),
    198     GNUNET_PQ_query_param_end
    199   };
    200   enum GNUNET_DB_QueryStatus qs;
    201 
    202   GNUNET_assert (NULL != pg->current_merchant_id);
    203   GNUNET_assert (0 == strcmp (instance_id,
    204                               pg->current_merchant_id));
    205   TMH_PQ_prepare_anon (pg,
    206                        "SELECT description"
    207                        " FROM merchant_statistic_interval_meta"
    208                        " WHERE slug=$1 LIMIT 1");
    209   qs = GNUNET_PQ_eval_prepared_multi_select (
    210     pg->conn,
    211     "",
    212     descParams,
    213     &lookup_statistics_amount_by_interval_desc_cb,
    214     &context);
    215   /* If there was an error inside the cb, return a hard error. */
    216   if (context.extract_failed)
    217   {
    218     GNUNET_break (0);
    219     return GNUNET_DB_STATUS_HARD_ERROR;
    220   }
    221   TMH_PQ_prepare_anon (pg,
    222                        "SELECT *"
    223                        " FROM merchant_statistic_interval_amount_get($1)");
    224   qs = GNUNET_PQ_eval_prepared_multi_select (
    225     pg->conn,
    226     "",
    227     params,
    228     &lookup_statistics_amount_by_interval_cb,
    229     &context);
    230   if (NULL != context.description)
    231     GNUNET_free (context.description);
    232   /* If there was an error inside the cb, return a hard error. */
    233   if (context.extract_failed)
    234   {
    235     GNUNET_break (0);
    236     return GNUNET_DB_STATUS_HARD_ERROR;
    237   }
    238   return qs;
    239 }