LCOV - code coverage report
Current view: top level - backenddb - iterate_statistic_interval_counters.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 79.6 % 49 39
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 3 3

            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_interval_counters.c
      18              :  * @brief Implementation of the iterate_statistic_interval_counters function for Postgres
      19              :  * @author Martin Schanzenbach
      20              :  */
      21              : #include "platform.h"
      22              : #include <taler/taler_pq_lib.h>
      23              : #include "merchant-database/iterate_statistic_interval_counters.h"
      24              : #include "helper.h"
      25              : #include "merchantdb_lib.h"
      26              : 
      27              : 
      28              : /**
      29              :  * Context used for TALER_MERCHANTDB_iterate_statistic_interval_counters().
      30              :  */
      31              : struct LookupCounterStatisticsContext
      32              : {
      33              :   /**
      34              :    * Function to call with the results.
      35              :    */
      36              :   TALER_MERCHANTDB_CounterByIntervalStatisticsCallback 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            4 : lookup_statistics_counter_by_interval_desc_cb (void *cls,
      64              :                                                PGresult *result,
      65              :                                                unsigned int num_results)
      66              : {
      67            4 :   struct LookupCounterStatisticsContext *tflc = cls;
      68              : 
      69            8 :   for (unsigned int i = 0; i < num_results; i++)
      70              :   {
      71              :     char *description;
      72            4 :     struct GNUNET_PQ_ResultSpec rs[] = {
      73            4 :       GNUNET_PQ_result_spec_string ("description",
      74              :                                     &description),
      75              :       GNUNET_PQ_result_spec_end
      76              :     };
      77              : 
      78            4 :     if (GNUNET_OK !=
      79            4 :         GNUNET_PQ_extract_result (result,
      80              :                                   rs,
      81              :                                   i))
      82              :     {
      83            0 :       GNUNET_break (0);
      84            0 :       tflc->extract_failed = true;
      85            0 :       return;
      86              :     }
      87              : 
      88            4 :     tflc->description = GNUNET_strdup (description);
      89              : 
      90            4 :     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            4 : lookup_statistics_counter_by_interval_cb (
     105              :   void *cls,
     106              :   PGresult *result,
     107              :   unsigned int num_results)
     108              : {
     109            4 :   struct LookupCounterStatisticsContext *tflc = cls;
     110              : 
     111           10 :   for (unsigned int i = 0; i < num_results; i++)
     112              :   {
     113              :     uint64_t cumulative_number;
     114              :     uint64_t interval_start_ago;
     115            6 :     struct GNUNET_PQ_ResultSpec rs[] = {
     116            6 :       GNUNET_PQ_result_spec_uint64 ("range",
     117              :                                     &interval_start_ago),
     118            6 :       GNUNET_PQ_result_spec_uint64 ("rvalue",
     119              :                                     &cumulative_number),
     120              :       GNUNET_PQ_result_spec_end
     121              :     };
     122              :     struct GNUNET_TIME_Timestamp interval_start;
     123              : 
     124            6 :     if (GNUNET_OK !=
     125            6 :         GNUNET_PQ_extract_result (result,
     126              :                                   rs,
     127              :                                   i))
     128              :     {
     129            0 :       GNUNET_break (0);
     130            0 :       tflc->extract_failed = true;
     131            0 :       return;
     132              :     }
     133              : 
     134            6 :     interval_start = GNUNET_TIME_timestamp_get ();
     135            6 :     interval_start.abs_time.abs_value_us -= interval_start_ago * 1000 * 1000;
     136            6 :     tflc->cb (tflc->cb_cls,
     137            6 :               tflc->description,
     138              :               interval_start,
     139              :               cumulative_number);
     140            6 :     GNUNET_PQ_cleanup_result (rs);
     141              :   }
     142              : }
     143              : 
     144              : 
     145              : enum GNUNET_DB_QueryStatus
     146            4 : TALER_MERCHANTDB_iterate_statistic_interval_counters (
     147              :   struct TALER_MERCHANTDB_PostgresContext *pg,
     148              :   const char *instance_id,
     149              :   const char *slug,
     150              :   TALER_MERCHANTDB_CounterByIntervalStatisticsCallback cb,
     151              :   void *cb_cls)
     152              : {
     153            4 :   struct LookupCounterStatisticsContext context = {
     154              :     .cb = cb,
     155              :     .cb_cls = cb_cls,
     156              :     /* Can be overwritten by the lookup_statistics_counter_by_interval_cb */
     157              :     .extract_failed = false,
     158              :     .description = NULL
     159              :   };
     160            4 :   struct GNUNET_PQ_QueryParam descParams[] = {
     161            4 :     GNUNET_PQ_query_param_string (slug),
     162              :     GNUNET_PQ_query_param_end
     163              :   };
     164            4 :   struct GNUNET_PQ_QueryParam params[] = {
     165            4 :     GNUNET_PQ_query_param_string (slug),
     166              :     GNUNET_PQ_query_param_end
     167              :   };
     168              :   enum GNUNET_DB_QueryStatus qs;
     169              : 
     170            4 :   GNUNET_assert (NULL != pg->current_merchant_id);
     171            4 :   GNUNET_assert (0 == strcmp (instance_id,
     172              :                               pg->current_merchant_id));
     173            4 :   TMH_PQ_prepare_anon (pg,
     174              :                        "SELECT description"
     175              :                        "  FROM merchant_statistic_interval_meta"
     176              :                        " WHERE slug=$1"
     177              :                        " LIMIT 1");
     178            4 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     179              :     pg->conn,
     180              :     "",
     181              :     descParams,
     182              :     &lookup_statistics_counter_by_interval_desc_cb,
     183              :     &context);
     184              :   /* If there was an error inside the cb, return a hard error. */
     185            4 :   if (context.extract_failed)
     186              :   {
     187            0 :     GNUNET_break (0);
     188            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     189              :   }
     190            4 :   TMH_PQ_prepare_anon (pg,
     191              :                        "SELECT"
     192              :                        "  range"
     193              :                        " ,rvalue"
     194              :                        "  FROM merchant_statistic_interval_number_get($1)");
     195            4 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     196              :     pg->conn,
     197              :     "",
     198              :     params,
     199              :     &lookup_statistics_counter_by_interval_cb,
     200              :     &context);
     201            4 :   if (NULL != context.description)
     202            4 :     GNUNET_free (context.description);
     203              :   /* If there was an error inside the cb, return a hard error. */
     204            4 :   if (context.extract_failed)
     205              :   {
     206            0 :     GNUNET_break (0);
     207            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     208              :   }
     209            4 :   return qs;
     210              : }
        

Generated by: LCOV version 2.0-1