Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 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 :
18 : #include <gnunet/gnunet_util_lib.h>
19 : #include <gnunet/gnunet_json_lib.h>
20 : #include <jansson.h>
21 : #include <microhttpd.h>
22 : #include <pthread.h>
23 : #include "taler/taler_json_lib.h"
24 : #include "taler/taler_mhd_lib.h"
25 : #include "taler-auditor-httpd.h"
26 : #include "taler-auditor-httpd_get-monitoring-historic-reserve-summary.h"
27 : #include "auditor-database/preflight.h"
28 : #include "auditor-database/select_historic_reserve_revenue.h"
29 :
30 : /**
31 : * Add historic-reserve-summary to the list.
32 : *
33 : * @param[in,out] cls a `json_t *` array to extend
34 : * @param serial_id location of the @a dc in the database
35 : * @param start_time beginning of aggregated time interval
36 : * @param end_time end of aggregated time interval
37 : * @param reserve_profits total profits made
38 : *
39 : * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
40 : */
41 : static enum GNUNET_GenericReturnValue
42 0 : process_historic_reserve_summary (
43 : void *cls,
44 : uint64_t serial_id,
45 : struct GNUNET_TIME_Timestamp start_time,
46 : struct GNUNET_TIME_Timestamp end_time,
47 : const struct TALER_Amount *reserve_profits)
48 : {
49 0 : json_t *list = cls;
50 : json_t *obj;
51 :
52 0 : obj = GNUNET_JSON_PACK (
53 : GNUNET_JSON_pack_int64 ("row_id",
54 : serial_id),
55 : TALER_JSON_pack_time_abs_human ("start_date",
56 : start_time.abs_time),
57 : TALER_JSON_pack_time_abs_human ("end_date",
58 : end_time.abs_time),
59 : TALER_JSON_pack_amount ("reserve_profits",
60 : reserve_profits)
61 : );
62 0 : GNUNET_break (0 ==
63 : json_array_append_new (list,
64 : obj));
65 0 : return GNUNET_OK;
66 : }
67 :
68 :
69 : MHD_RESULT
70 0 : TAH_get_monitoring_historic_reserve_summary (
71 : struct TAH_RequestHandler *rh,
72 : struct MHD_Connection *connection,
73 : void **connection_cls,
74 : const char *upload_data,
75 : size_t *upload_data_size,
76 : const char *const args[])
77 : {
78 : json_t *ja;
79 : enum GNUNET_DB_QueryStatus qs;
80 0 : int64_t limit = -20;
81 : uint64_t offset;
82 :
83 : (void) rh;
84 : (void) connection_cls;
85 : (void) upload_data;
86 : (void) upload_data_size;
87 0 : if (GNUNET_SYSERR ==
88 0 : TALER_AUDITORDB_preflight (TAH_apg))
89 : {
90 0 : GNUNET_break (0);
91 0 : return TALER_MHD_reply_with_error (
92 : connection,
93 : MHD_HTTP_INTERNAL_SERVER_ERROR,
94 : TALER_EC_GENERIC_DB_SETUP_FAILED,
95 : NULL);
96 : }
97 0 : TALER_MHD_parse_request_snumber (connection,
98 : "limit",
99 : &limit);
100 0 : if (limit < 0)
101 0 : offset = INT64_MAX;
102 : else
103 0 : offset = 0;
104 0 : TALER_MHD_parse_request_number (connection,
105 : "offset",
106 : &offset);
107 0 : ja = json_array ();
108 0 : GNUNET_break (NULL != ja);
109 0 : qs = TALER_AUDITORDB_select_historic_reserve_revenue (
110 : TAH_apg,
111 : limit,
112 : offset,
113 : &process_historic_reserve_summary,
114 : ja);
115 :
116 0 : if (0 > qs)
117 : {
118 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
119 0 : json_decref (ja);
120 0 : TALER_LOG_WARNING (
121 : "Failed to handle GET /historic-reserve-summary");
122 0 : return TALER_MHD_reply_with_error (
123 : connection,
124 : MHD_HTTP_INTERNAL_SERVER_ERROR,
125 : TALER_EC_GENERIC_DB_FETCH_FAILED,
126 : "select_historic_reserve_revenue");
127 : }
128 0 : return TALER_MHD_REPLY_JSON_PACK (
129 : connection,
130 : MHD_HTTP_OK,
131 : GNUNET_JSON_pack_array_steal (
132 : "historic-reserve-summary",
133 : ja));
134 : }
|