merchant

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

lookup_statistics_counter_by_bucket2.c (5813B)


      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_counter_by_bucket2.c
     18  * @brief Implementation of the lookup_statistics_counter_by_bucket2 function for Postgres
     19  * @author Christian Grothoff
     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_counter_by_bucket2.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Context used for TALER_MERCHANTDB_lookup_statistics_counter_by_bucket2().
     30  */
     31 struct LookupCounterStatisticsContext2
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 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    * Postgres context for array lookups
     50    */
     51   struct TALER_MERCHANTDB_PostgresContext *pg;
     52 
     53 };
     54 
     55 
     56 /**
     57  * Function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results about token families.
     59  *
     60  * @param[in,out] cls of type `struct LookupCounterStatisticsContext2 *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 lookup_statistics_counter_by_bucket_cb2 (void *cls,
     66                                          PGresult *result,
     67                                          unsigned int num_results)
     68 {
     69   struct LookupCounterStatisticsContext2 *tflc = cls;
     70   struct TALER_MERCHANTDB_PostgresContext *pg = tflc->pg;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     size_t num_slugs;
     75     char *slugs;
     76     size_t num_counters;
     77     uint64_t *counters;
     78     uint64_t bucket_start_epoch;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_uint64 ("bucket_start",
     81                                     &bucket_start_epoch),
     82       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
     83                                           "counters",
     84                                           &num_counters,
     85                                           &counters),
     86       GNUNET_PQ_result_spec_array_string (pg->conn,
     87                                           "slugs",
     88                                           &num_slugs,
     89                                           &slugs),
     90       GNUNET_PQ_result_spec_end
     91     };
     92     struct GNUNET_TIME_Timestamp bucket_start;
     93 
     94     if (GNUNET_OK !=
     95         GNUNET_PQ_extract_result (result,
     96                                   rs,
     97                                   i))
     98     {
     99       GNUNET_break (0);
    100       tflc->extract_failed = true;
    101       return;
    102     }
    103     if (num_slugs != num_counters)
    104     {
    105       GNUNET_break (0);
    106       tflc->extract_failed = true;
    107       GNUNET_PQ_cleanup_result (rs);
    108       return;
    109     }
    110     {
    111       const char *slugptrs[num_slugs];
    112       const char *pos = slugs;
    113 
    114       for (size_t j = 0; j<num_slugs; j++)
    115       {
    116         slugptrs[j] = pos;
    117         pos += strlen (pos) + 1;
    118       }
    119 
    120       bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
    121       tflc->cb (tflc->cb_cls,
    122                 bucket_start,
    123                 num_slugs,
    124                 slugptrs,
    125                 counters);
    126     }
    127     GNUNET_PQ_cleanup_result (rs);
    128   }
    129 }
    130 
    131 
    132 enum GNUNET_DB_QueryStatus
    133 TALER_MERCHANTDB_lookup_statistics_counter_by_bucket2 (
    134   struct TALER_MERCHANTDB_PostgresContext *pg,
    135   const char *instance_id,
    136   const char *prefix,
    137   const char *granularity,
    138   uint64_t counter,
    139   TALER_MERCHANTDB_CounterByBucketStatisticsCallback2 cb,
    140   void *cb_cls)
    141 {
    142   struct LookupCounterStatisticsContext2 context = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     /* Can be overwritten by the lookup_statistics_counter_by_bucket_cb2 */
    146     .extract_failed = false,
    147     .pg = pg
    148   };
    149   struct GNUNET_PQ_QueryParam params[] = {
    150     GNUNET_PQ_query_param_string (instance_id),
    151     GNUNET_PQ_query_param_string (prefix),
    152     GNUNET_PQ_query_param_string (granularity),
    153     GNUNET_PQ_query_param_uint64 (&counter),
    154     GNUNET_PQ_query_param_end
    155   };
    156   enum GNUNET_DB_QueryStatus qs;
    157 
    158   check_connection (pg);
    159   PREPARE (pg,
    160            "lookup_statistics_counter_by_bucket2",
    161            "SELECT"
    162            " msbc.bucket_start"
    163            ",ARRAY_AGG(msbm.slug) AS slugs"
    164            ",ARRAY_AGG(msbc.cumulative_number) AS counters"
    165            "  FROM merchant_statistic_bucket_counter msbc"
    166            "  JOIN merchant_statistic_bucket_meta msbm"
    167            "    USING (bmeta_serial_id)"
    168            "  JOIN merchant_instances mi"
    169            "    USING (merchant_serial)"
    170            " WHERE mi.merchant_id=$1"
    171            "   AND msbm.slug LIKE $2"
    172            "   AND msbc.bucket_range=$3::TEXT::statistic_range"
    173            "   AND msbm.stype = 'number'"
    174            " GROUP BY msbc.bucket_start"
    175            " ORDER BY msbc.bucket_start DESC"
    176            " LIMIT $4");
    177   qs = GNUNET_PQ_eval_prepared_multi_select (
    178     pg->conn,
    179     "lookup_statistics_counter_by_bucket2",
    180     params,
    181     &lookup_statistics_counter_by_bucket_cb2,
    182     &context);
    183   /* If there was an error inside the cb, return a hard error. */
    184   if (context.extract_failed)
    185   {
    186     GNUNET_break (0);
    187     return GNUNET_DB_STATUS_HARD_ERROR;
    188   }
    189   return qs;
    190 }