LCOV - code coverage report
Current view: top level - backenddb - iterate_statistic_bucket_amounts_by_range.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.8 % 29 24
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 2 2

            Line data    Source code
       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/iterate_statistic_bucket_amounts_by_range.c
      18              :  * @brief Implementation of the iterate_statistic_bucket_amounts_by_range function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "platform.h"
      22              : #include <taler/taler_pq_lib.h>
      23              : #include "merchant-database/iterate_statistic_bucket_amounts_by_range.h"
      24              : #include "helper.h"
      25              : 
      26              : 
      27              : /**
      28              :  * Context used for TALER_MERCHANTDB_iterate_statistic_bucket_amounts_by_range().
      29              :  */
      30              : struct LookupAmountStatisticsContext2
      31              : {
      32              :   /**
      33              :    * Function to call with the results.
      34              :    */
      35              :   TALER_MERCHANTDB_AmountByBucketRangeStatisticsCallback 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            1 : lookup_statistics_amount_by_bucket_cb2 (void *cls,
      64              :                                         PGresult *result,
      65              :                                         unsigned int num_results)
      66              : {
      67            1 :   struct LookupAmountStatisticsContext2 *tflc = cls;
      68            1 :   struct TALER_MERCHANTDB_PostgresContext *pg = tflc->pg;
      69              : 
      70            2 :   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            1 :     struct GNUNET_PQ_ResultSpec rs[] = {
      76            1 :       GNUNET_PQ_result_spec_uint64 ("bucket_start",
      77              :                                     &bucket_start_epoch),
      78            1 :       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            1 :     if (GNUNET_OK !=
      88            1 :         GNUNET_PQ_extract_result (result,
      89              :                                   rs,
      90              :                                   i))
      91              :     {
      92            0 :       GNUNET_break (0);
      93            0 :       tflc->extract_failed = true;
      94            0 :       return;
      95              :     }
      96              : 
      97            1 :     bucket_start = GNUNET_TIME_timestamp_from_s (bucket_start_epoch);
      98            1 :     tflc->cb (tflc->cb_cls,
      99              :               bucket_start,
     100              :               num_amounts,
     101              :               amounts);
     102            1 :     GNUNET_PQ_cleanup_result (rs);
     103              :   }
     104              : }
     105              : 
     106              : 
     107              : enum GNUNET_DB_QueryStatus
     108            1 : TALER_MERCHANTDB_iterate_statistic_bucket_amounts_by_range (
     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_AmountByBucketRangeStatisticsCallback cb,
     115              :   void *cb_cls)
     116              : {
     117            1 :   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            1 :   struct GNUNET_PQ_QueryParam params[] = {
     125            1 :     GNUNET_PQ_query_param_string (slug),
     126            1 :     GNUNET_PQ_query_param_string (granularity),
     127            1 :     GNUNET_PQ_query_param_uint64 (&counter),
     128              :     GNUNET_PQ_query_param_end
     129              :   };
     130              :   enum GNUNET_DB_QueryStatus qs;
     131              : 
     132            1 :   GNUNET_assert (NULL != pg->current_merchant_id);
     133            1 :   GNUNET_assert (0 == strcmp (instance_id,
     134              :                               pg->current_merchant_id));
     135            1 :   TMH_PQ_prepare_anon (pg,
     136              :                        "SELECT"
     137              :                        " msba.bucket_start"
     138              :                        ",ARRAY_AGG ("
     139              :                        "   ROW(msba.cumulative_value::INT8, msba.cumulative_frac::INT4, msba.curr)::merchant.taler_amount_currency"
     140              :                        " ) AS cumulative_amounts"
     141              :                        " FROM merchant_statistic_bucket_meta msbm"
     142              :                        " JOIN merchant_statistic_bucket_amount msba"
     143              :                        "   USING (bmeta_serial_id)"
     144              :                        " WHERE msbm.slug=$1"
     145              :                        "   AND msba.bucket_range=$2::TEXT::merchant.statistic_range"
     146              :                        "   AND msbm.stype='amount'"
     147              :                        " GROUP BY msba.bucket_start"
     148              :                        " ORDER BY msba.bucket_start DESC"
     149              :                        " LIMIT $3;");
     150            1 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     151              :     pg->conn,
     152              :     "",
     153              :     params,
     154              :     &lookup_statistics_amount_by_bucket_cb2,
     155              :     &context);
     156              :   /* If there was an error inside the cb, return a hard error. */
     157            1 :   if (context.extract_failed)
     158              :   {
     159            0 :     GNUNET_break (0);
     160            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     161              :   }
     162            1 :   return qs;
     163              : }
        

Generated by: LCOV version 2.0-1