merchant

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

lookup_statistics_amount_by_interval.c (7170B)


      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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_statistics_amount_by_interval.h"
     26 #include "helper.h"
     27 #include "merchantdb_lib.h"
     28 
     29 
     30 /**
     31  * Context used for TALER_MERCHANTDB_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 (
    107   void *cls,
    108   PGresult *result,
    109   unsigned int num_results)
    110 {
    111   struct LookupAmountStatisticsContext *tflc = cls;
    112   struct TALER_Amount *amounts = NULL;
    113   char *resp_desc = NULL;
    114   uint64_t cur_interval_start_ago = UINT64_MAX;
    115   unsigned int amounts_len = 0;
    116 
    117   for (unsigned int i = 0; i < num_results; i++)
    118   {
    119     struct TALER_Amount cumulative_amount;
    120     uint64_t interval_start_ago;
    121     struct GNUNET_PQ_ResultSpec rs[] = {
    122       GNUNET_PQ_result_spec_uint64 ("range",
    123                                     &interval_start_ago),
    124       TALER_PQ_result_spec_amount_with_currency ("rvalue",
    125                                                  &cumulative_amount),
    126       GNUNET_PQ_result_spec_end
    127     };
    128 
    129     if (GNUNET_OK !=
    130         GNUNET_PQ_extract_result (result,
    131                                   rs,
    132                                   i))
    133     {
    134       GNUNET_break (0);
    135       tflc->extract_failed = true;
    136       return;
    137     }
    138 
    139     /* Call callback if the bucket changed */
    140     if ( (interval_start_ago != cur_interval_start_ago) &&
    141          (i > 0) )
    142     {
    143       struct GNUNET_TIME_Timestamp interval_start;
    144 
    145       interval_start = GNUNET_TIME_timestamp_get ();
    146       interval_start.abs_time.abs_value_us -= interval_start_ago * 1000 * 1000;
    147       tflc->cb (tflc->cb_cls,
    148                 resp_desc,
    149                 interval_start,
    150                 amounts_len,
    151                 amounts);
    152       GNUNET_array_grow (amounts,
    153                          amounts_len,
    154                          0);
    155       GNUNET_free (resp_desc);
    156     }
    157     cur_interval_start_ago = interval_start_ago;
    158     GNUNET_array_append (amounts,
    159                          amounts_len,
    160                          cumulative_amount);
    161     GNUNET_PQ_cleanup_result (rs);
    162   }
    163   if (0 != amounts_len)
    164   {
    165     struct GNUNET_TIME_Timestamp interval_start;
    166 
    167     interval_start = GNUNET_TIME_timestamp_from_s (cur_interval_start_ago);
    168     tflc->cb (tflc->cb_cls,
    169               resp_desc,
    170               interval_start,
    171               amounts_len,
    172               amounts);
    173     GNUNET_array_grow (amounts,
    174                        amounts_len,
    175                        0);
    176     GNUNET_free (resp_desc);
    177   }
    178 }
    179 
    180 
    181 enum GNUNET_DB_QueryStatus
    182 TALER_MERCHANTDB_lookup_statistics_amount_by_interval (
    183   struct TALER_MERCHANTDB_PostgresContext *pg,
    184   const char *instance_id,
    185   const char *slug,
    186   TALER_MERCHANTDB_AmountByIntervalStatisticsCallback cb,
    187   void *cb_cls)
    188 {
    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 }