LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_statistics_counter_by_interval.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.0 % 50 40
Test Date: 2025-11-06 19:31:41 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 backenddb/pg_lookup_statistics_counter_by_interval.c
      18              :  * @brief Implementation of the lookup_statistics_counter_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 "pg_lookup_statistics_counter_by_interval.h"
      26              : #include "pg_helper.h"
      27              : #include "taler_merchantdb_plugin.h"
      28              : 
      29              : 
      30              : /**
      31              :  * Context used for TMH_PG_lookup_statistics_counter().
      32              :  */
      33              : struct LookupCounterStatisticsContext
      34              : {
      35              :   /**
      36              :    * Function to call with the results.
      37              :    */
      38              :   TALER_MERCHANTDB_CounterByIntervalStatisticsCallback 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            4 : lookup_statistics_counter_by_interval_desc_cb (void *cls,
      66              :                                                PGresult *result,
      67              :                                                unsigned int num_results)
      68              : {
      69            4 :   struct LookupCounterStatisticsContext *tflc = cls;
      70              : 
      71            8 :   for (unsigned int i = 0; i < num_results; i++)
      72              :   {
      73              :     char *description;
      74            4 :     struct GNUNET_PQ_ResultSpec rs[] = {
      75            4 :       GNUNET_PQ_result_spec_string ("description",
      76              :                                     &description),
      77              :       GNUNET_PQ_result_spec_end
      78              :     };
      79              : 
      80            4 :     if (GNUNET_OK !=
      81            4 :         GNUNET_PQ_extract_result (result,
      82              :                                   rs,
      83              :                                   i))
      84              :     {
      85            0 :       GNUNET_break (0);
      86            0 :       tflc->extract_failed = true;
      87            0 :       return;
      88              :     }
      89              : 
      90            4 :     tflc->description = GNUNET_strdup (description);
      91              : 
      92            4 :     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            4 : lookup_statistics_counter_by_interval_cb (void *cls,
     107              :                                           PGresult *result,
     108              :                                           unsigned int num_results)
     109              : {
     110            4 :   struct LookupCounterStatisticsContext *tflc = cls;
     111              : 
     112           10 :   for (unsigned int i = 0; i < num_results; i++)
     113              :   {
     114              :     uint64_t cumulative_number;
     115              :     uint64_t interval_start_ago;
     116            6 :     struct GNUNET_PQ_ResultSpec rs[] = {
     117            6 :       GNUNET_PQ_result_spec_uint64 ("range",
     118              :                                     &interval_start_ago),
     119            6 :       GNUNET_PQ_result_spec_uint64 ("rvalue",
     120              :                                     &cumulative_number),
     121              :       GNUNET_PQ_result_spec_end
     122              :     };
     123              :     struct GNUNET_TIME_Timestamp interval_start;
     124              : 
     125            6 :     if (GNUNET_OK !=
     126            6 :         GNUNET_PQ_extract_result (result,
     127              :                                   rs,
     128              :                                   i))
     129              :     {
     130            0 :       GNUNET_break (0);
     131            0 :       tflc->extract_failed = true;
     132            0 :       return;
     133              :     }
     134              : 
     135            6 :     interval_start = GNUNET_TIME_timestamp_get ();
     136            6 :     interval_start.abs_time.abs_value_us -= interval_start_ago * 1000 * 1000;
     137            6 :     tflc->cb (tflc->cb_cls,
     138            6 :               tflc->description,
     139              :               interval_start,
     140              :               cumulative_number);
     141            6 :     GNUNET_PQ_cleanup_result (rs);
     142              :   }
     143              : }
     144              : 
     145              : 
     146              : enum GNUNET_DB_QueryStatus
     147            4 : TMH_PG_lookup_statistics_counter_by_interval (
     148              :   void *cls,
     149              :   const char *instance_id,
     150              :   const char *slug,
     151              :   TALER_MERCHANTDB_CounterByIntervalStatisticsCallback cb,
     152              :   void *cb_cls)
     153              : {
     154            4 :   struct PostgresClosure *pg = cls;
     155            4 :   struct LookupCounterStatisticsContext context = {
     156              :     .cb = cb,
     157              :     .cb_cls = cb_cls,
     158              :     /* Can be overwritten by the lookup_statistics_counter_by_interval_cb */
     159              :     .extract_failed = false,
     160              :     .description = NULL
     161              :   };
     162            4 :   struct GNUNET_PQ_QueryParam descParams[] = {
     163            4 :     GNUNET_PQ_query_param_string (slug),
     164              :     GNUNET_PQ_query_param_end
     165              :   };
     166            4 :   struct GNUNET_PQ_QueryParam params[] = {
     167            4 :     GNUNET_PQ_query_param_string (instance_id),
     168            4 :     GNUNET_PQ_query_param_string (slug),
     169              :     GNUNET_PQ_query_param_end
     170              :   };
     171              :   enum GNUNET_DB_QueryStatus qs;
     172              : 
     173            4 :   check_connection (pg);
     174            4 :   PREPARE (pg,
     175              :            "lookup_statistics_counter_by_interval_description",
     176              :            "SELECT description"
     177              :            " FROM merchant_statistic_interval_meta"
     178              :            " WHERE slug=$1 LIMIT 1");
     179            4 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     180              :     pg->conn,
     181              :     "lookup_statistics_counter_by_interval_description",
     182              :     descParams,
     183              :     &lookup_statistics_counter_by_interval_desc_cb,
     184              :     &context);
     185              :   /* If there was an error inside the cb, return a hard error. */
     186            4 :   if (context.extract_failed)
     187              :   {
     188            0 :     GNUNET_break (0);
     189            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     190              :   }
     191            4 :   PREPARE (pg,
     192              :            "lookup_statistics_counter_by_interval",
     193              :            "SELECT *"
     194              :            " FROM merchant_statistic_interval_number_get($2,$1)");
     195            4 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     196              :     pg->conn,
     197              :     "lookup_statistics_counter_by_interval",
     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