merchant

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

lookup_statistics_amount_by_bucket2.c (5372B)


      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_bucket2.c
     18  * @brief Implementation of the lookup_statistics_amount_by_bucket2 function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_statistics_amount_by_bucket2.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context used for TALER_MERCHANTDB_lookup_statistics_amount_by_bucket2().
     29  */
     30 struct LookupAmountStatisticsContext2
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb;
     36 
     37   /**
     38    * Closure for @a cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Did database result extraction fail?
     44    */
     45   bool extract_failed;
     46 
     47   /**
     48    * Postgres context for array lookups
     49    */
     50   struct TALER_MERCHANTDB_PostgresContext *pg;
     51 };
     52 
     53 
     54 /**
     55  * Function to be called with the results of a SELECT statement
     56  * that has returned @a num_results results about token families.
     57  *
     58  * @param[in,out] cls of type `struct LookupAmountStatisticsContext2 *`
     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_bucket_cb2 (void *cls,
     64                                         PGresult *result,
     65                                         unsigned int num_results)
     66 {
     67   struct LookupAmountStatisticsContext2 *tflc = cls;
     68   struct TALER_MERCHANTDB_PostgresContext *pg = tflc->pg;
     69 
     70   for (unsigned int i = 0; i < num_results; i++)
     71   {
     72     struct TALER_Amount *amounts;
     73     size_t num_amounts;
     74     uint64_t bucket_start_epoch;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_uint64 ("bucket_start",
     77                                     &bucket_start_epoch),
     78       TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
     79                                                        "merchant",
     80                                                        "cumulative_amounts",
     81                                                        &num_amounts,
     82                                                        &amounts),
     83       GNUNET_PQ_result_spec_end
     84     };
     85     struct GNUNET_TIME_Timestamp bucket_start;
     86 
     87     if (GNUNET_OK !=
     88         GNUNET_PQ_extract_result (result,
     89                                   rs,
     90                                   i))
     91     {
     92       GNUNET_break (0);
     93       tflc->extract_failed = true;
     94       return;
     95     }
     96 
     97     bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
     98     tflc->cb (tflc->cb_cls,
     99               bucket_start,
    100               num_amounts,
    101               amounts);
    102     GNUNET_PQ_cleanup_result (rs);
    103   }
    104 }
    105 
    106 
    107 enum GNUNET_DB_QueryStatus
    108 TALER_MERCHANTDB_lookup_statistics_amount_by_bucket2 (
    109   struct TALER_MERCHANTDB_PostgresContext *pg,
    110   const char *instance_id,
    111   const char *slug,
    112   const char *granularity,
    113   uint64_t counter,
    114   TALER_MERCHANTDB_AmountByBucketStatisticsCallback2 cb,
    115   void *cb_cls)
    116 {
    117   struct LookupAmountStatisticsContext2 context = {
    118     .cb = cb,
    119     .cb_cls = cb_cls,
    120     /* Can be overwritten by the lookup_statistics_amount_by_bucket_cb2 */
    121     .extract_failed = false,
    122     .pg = pg,
    123   };
    124   struct GNUNET_PQ_QueryParam params[] = {
    125     GNUNET_PQ_query_param_string (slug),
    126     GNUNET_PQ_query_param_string (granularity),
    127     GNUNET_PQ_query_param_uint64 (&counter),
    128     GNUNET_PQ_query_param_end
    129   };
    130   enum GNUNET_DB_QueryStatus qs;
    131 
    132   GNUNET_assert (NULL != pg->current_merchant_id);
    133   GNUNET_assert (0 == strcmp (instance_id,
    134                               pg->current_merchant_id));
    135   check_connection (pg);
    136   TMH_PQ_prepare_anon (pg,
    137                        "SELECT"
    138                        " msba.bucket_start"
    139                        ",ARRAY_AGG ("
    140                        "   ROW(msba.cumulative_value::INT8, msba.cumulative_frac::INT4, msba.curr)::merchant.taler_amount_currency"
    141                        " ) AS cumulative_amounts"
    142                        " FROM merchant_statistic_bucket_meta msbm"
    143                        " JOIN merchant_statistic_bucket_amount msba"
    144                        "   USING (bmeta_serial_id)"
    145                        " WHERE msbm.slug=$1"
    146                        "   AND msba.bucket_range=$2::TEXT::merchant.statistic_range"
    147                        "   AND msbm.stype='amount'"
    148                        " GROUP BY msba.bucket_start"
    149                        " ORDER BY msba.bucket_start DESC"
    150                        " LIMIT $3;");
    151   qs = GNUNET_PQ_eval_prepared_multi_select (
    152     pg->conn,
    153     "",
    154     params,
    155     &lookup_statistics_amount_by_bucket_cb2,
    156     &context);
    157   /* If there was an error inside the cb, return a hard error. */
    158   if (context.extract_failed)
    159   {
    160     GNUNET_break (0);
    161     return GNUNET_DB_STATUS_HARD_ERROR;
    162   }
    163   return qs;
    164 }