BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
EventCatalogue.cxx
Go to the documentation of this file.
1#include "EventCatalogue.h"
2
3#include "event_db_settings.h"
4#include "nlohmann/json.hpp"
5
6#include <fstream>
7#include <iostream>
8#include <map>
9#include <sstream>
10
12
13map<int, string> const Errors{{401, "Unauthorized Access (check user login and password)"},
14 {403, "Token invalid or expired"},
15 {404, "Requested Data not found"},
16 {409, "Incorrect Database Request (e.g. violating constraints)"},
17 {422, "Incorrect Input Request"},
18 {500, "Internal Server Error"},
19 {503, "Service Unavaialble"},
20 {504, "Server Timeout"}};
21
22string const GetErrorString(int error_id)
23{
24 auto it = Errors.find(error_id);
25 return it != Errors.end() ? it->second : "";
26}
27
29 : EventAttribute(attribute)
30 , EventMatchCriteria(condition)
31 , iValueType(0)
32{}
33
35 : EventAttribute(attribute)
36 , EventMatchCriteria(condition)
37 , iValueType(1)
38 , iValue(value)
39{}
40
41EventCondition::EventCondition(searchAttributes attribute, matchCriteria condition, unsigned int value)
42 : EventAttribute(attribute)
43 , EventMatchCriteria(condition)
44 , iValueType(2)
45 , uiValue(value)
46{}
47
49 : EventAttribute(attribute)
50 , EventMatchCriteria(condition)
51 , iValueType(3)
52 , dValue(value)
53{}
54
56 : EventAttribute(attribute)
57 , EventMatchCriteria(condition)
58 , iValueType(4)
59 , strValue(value)
60{}
61
62// private default constructor
68
69// Event Catalogue Constructor
71{
72 curl_global_init(CURL_GLOBAL_DEFAULT);
73 curlInstance = curl_easy_init();
74 if (!curlInstance) {
75 // cout<<"ERROR: CURL initialisation failed"<<endl;
76 throw runtime_error("CURL initialisation failed");
77 return;
78 }
79
81 EVENT_API_HOST + string("/") + EVENT_API_NAME + string("/v") + to_string(EventInfo::Version) + string("/");
83 if (VerboseLevel > 2)
84 cout << "DEBUG: ServiceUrl = " << ServiceUrl << ", ServiceAccount = " << ServiceAccount << endl;
85}
86
87// Event Catalogue Destructor
89{
90 curl_easy_cleanup(curlInstance);
91 curl_global_cleanup();
92}
93
94vector<unique_ptr<EventInfo>> EventCatalogue::GetEventList(vector<unique_ptr<EventCondition>> event_conditions)
95{
96 vector<unique_ptr<EventInfo>> event_array;
97 cout << "ERROR: EventCatalogue::GetEventList is not implemented yet" << endl;
98
99 return event_array;
100}
101
102unique_ptr<EventInfo> EventCatalogue::GetEventInfo(string storage_name, string file_path, int event_number)
103{
104 unique_ptr<EventInfo> event_info;
105 cout << "ERROR: EventCatalogue::GetEventInfo is not implemented yet" << endl;
106
107 return event_info;
108}
109
110int EventCatalogue::AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
111{
112 cout << "ERROR: EventCatalogue::AddEventInfo is not implemented yet" << endl;
113
114 return 0;
115}
116
117size_t response_callback(void* contents, size_t size, size_t nmemb, string* data)
118{
119 size_t realsize = size * nmemb;
120 try {
121 data->append((char*)contents, realsize);
122 } catch (std::bad_alloc& e) {
123 // handle memory problem
124 return 0;
125 }
126
127 return realsize;
128}
129
130static size_t header_callback(char* buffer, size_t size, size_t nitems, void* userdata)
131{
132 string* headers = (string*)userdata;
133 try {
134 headers->append(buffer, nitems * size);
135 } catch (std::bad_alloc& e) {
136 // handle memory problem
137 return 0;
138 }
139
140 return nitems * size;
141}
142
143int EventCatalogue::AddEventInfo(string json_file_path)
144{
145 ifstream json_file(json_file_path);
146 ostringstream sstr;
147 sstr << json_file.rdbuf();
148 string json_string = sstr.str();
149 if (VerboseLevel > 2)
150 cout << "DEBUG: json_string = " << json_string << endl;
151
152 string success_message =
153 "Event metadata have been written from JSON file '" + json_file_path + "' to the Event Catalogue";
154 int return_code = PostRequest(ServiceUrl + "event", json_string, success_message);
155
156 return return_code;
157}
158
159int EventCatalogue::UpdateEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
160{
161 cout << "ERROR: EventCatalogue::UpdateEventInfo is not implemented yet" << endl;
162
163 return 0;
164}
165
166int EventCatalogue::DeleteEventInfo(string storage_name, string file_path, int event_number)
167{
168 cout << "ERROR: EventCatalogue::DeleteEventInfo is not implemented yet" << endl;
169
170 return 0;
171}
172
173int EventCatalogue::CheckEventInfo(string storage_name, string file_path, int event_number)
174{
175 cout << "ERROR: EventCatalogue::CheckEventInfo is not implemented yet" << endl;
176
177 return 0;
178}
179
180int EventCatalogue::PrintEventInfo(string storage_name, string file_path, int event_number)
181{
182 cout << "ERROR: EventCatalogue::PrintEventInfo is not implemented yet" << endl;
183
184 return 0;
185}
186
187int EventCatalogue::AddStorageInfo(string storage_name)
188{
189 // write storage metadata to JSON element
190 // json storage_element;
191 // storage_element["storage_id"] = -1;
192 // storage_element["storage_name"] = storage_name;
193 string json_string = "{\"storage_id\": -1, \"storage_name\": \"" + storage_name + "\"}";
194 if (VerboseLevel > 2)
195 cout << "DEBUG: json_string = " << json_string << endl;
196
197 string success_message = "Storage information has been added for '" + storage_name + "' to the Event Catalogue";
198 int return_code = PostRequest(ServiceUrl + "storage", json_string, success_message);
199
200 return return_code;
201}
202
203int EventCatalogue::AddSoftwareInfo(string software_version)
204{
205 // write software version to JSON element
206 // json storage_element;
207 // storage_element["software_id"] = -1;
208 // storage_element["software_version"] = software_version;
209 string json_string = "{\"software_id\": -1, \"software_version\": \"" + software_version + "\"}";
210 if (VerboseLevel > 2)
211 cout << "ERROR: json_string = " << json_string << endl;
212
213 string success_message =
214 "Software version has been added for '" + software_version + "' version to the Event Catalogue";
215 int return_code = PostRequest(ServiceUrl + "software", json_string, success_message);
216
217 return return_code;
218}
219
220vector<unique_ptr<EventInfo>> EventCatalogue::EventCatalogueRequest(string rest_api_request)
221{
222 vector<unique_ptr<EventInfo>> event_array;
223 cout << "ERROR: EventCatalogue::EventCatalogueRequest is not implemented yet" << endl;
224
225 return event_array;
226}
227
228int EventCatalogue::EventCatalogueChecks(int& file_not_found, vector<unique_ptr<EventInfo>>& event_list)
229{
230 cout << "ERROR: EventCatalogue::EventCatalogueChecks is not implemented yet" << endl;
231
232 return 0;
233}
234
236{
237 cout << "ERROR: EventCatalogue::EventCatalogueStats is not implemented yet" << endl;
238
239 return 0;
240}
241
242int EventCatalogue::PostRequest(string RequestUrl, string json_string, string success_message)
243{
244 struct curl_slist* http_header = nullptr;
245 http_header = curl_slist_append(http_header, "Accept: */*");
246 http_header = curl_slist_append(http_header, "Content-Type: application/json");
247 http_header = curl_slist_append(http_header, "charset: utf-8");
248 curl_easy_setopt(curlInstance, CURLOPT_HTTPHEADER, http_header);
249 curl_easy_setopt(curlInstance, CURLOPT_URL, RequestUrl.c_str());
250 curl_easy_setopt(curlInstance, CURLOPT_USERPWD, ServiceAccount.c_str());
251
252 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPALIVE, 1);
253 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPIDLE, 30);
254 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPINTVL, 30);
255 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPCNT, 720);
256
257 curl_easy_setopt(curlInstance, CURLOPT_POST, 1);
258 curl_easy_setopt(curlInstance, CURLOPT_POSTFIELDS, json_string.c_str());
259
260 string response_string, header_string;
261 curl_easy_setopt(curlInstance, CURLOPT_WRITEFUNCTION, response_callback);
262 curl_easy_setopt(curlInstance, CURLOPT_WRITEDATA, &response_string);
263 curl_easy_setopt(curlInstance, CURLOPT_HEADERFUNCTION, header_callback);
264 curl_easy_setopt(curlInstance, CURLOPT_HEADERDATA, &header_string);
265
266 // write new event metadata to the Event Catalogue
267 int res = curl_easy_perform(curlInstance);
268 if (res) {
269 cout << "ERROR: sending command to '" << RequestUrl << "' failed with code = " << res << endl;
270 curl_slist_free_all(http_header);
271 return -1;
272 }
273 curl_slist_free_all(http_header);
274
275 long response_code;
276 curl_easy_getinfo(curlInstance, CURLINFO_RESPONSE_CODE, &response_code);
277 if (VerboseLevel > 2) {
278 double elapsed;
279 curl_easy_getinfo(curlInstance, CURLINFO_TOTAL_TIME, &elapsed);
280 char* url;
281 curl_easy_getinfo(curlInstance, CURLINFO_EFFECTIVE_URL, &url);
282 cout << "DEBUG: CURLINFO_TOTAL_TIME = " << elapsed << ", CURLINFO_EFFECTIVE_URL = " << string(url) << endl;
283 }
284 // check the result of the request
285 if ((response_code >= 200) && (response_code < 205)) {
286 if (VerboseLevel > 0)
287 cout << success_message << endl;
288 if (VerboseLevel > 1)
289 cout << "Request has been completed with the following response: " << response_string << endl;
290 } else {
291 string error_message = GetErrorString(response_code);
292 cout << "ERROR: Sending request to '" << RequestUrl << "' has been failed";
293 if (error_message != "")
294 cout << ": " << error_message << endl;
295 else
296 cout << " with code = " << response_code << endl;
297 if (VerboseLevel > 1)
298 cout << "Error Message:" << endl
299 << response_string << endl
300 << "Error Output Header:" << endl
301 << header_string << endl;
302 return -1 * response_code;
303 }
304
305 return 0;
306}
static size_t header_callback(char *buffer, size_t size, size_t nitems, void *userdata)
map< int, string > const Errors
size_t response_callback(void *contents, size_t size, size_t nmemb, string *data)
string const GetErrorString(int error_id)
virtual ~EventCatalogue()
int DeleteEventInfo(string storage_name, string file_path, int event_number)
Function to delete metadata for the event stored at the given location.
int AddSoftwareInfo(string software_version)
Function to add software version to the dictionary used by the Event Catalogue.
int UpdateEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
Function to update metadata of the event stored at the given location.
int PostRequest(string RequestUrl, string json_string, string success_message)
vector< unique_ptr< EventInfo > > EventCatalogueRequest(string rest_api_request)
Function for searching for event metadata in the Event Catalogue using REST API request as an HTTP(s)...
int AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
Function to add metadata for the event stored at the given location.
int CheckEventInfo(string storage_name, string file_path, int event_number)
Function to check metadata for the event stored at the given location.
int AddStorageInfo(string storage_name)
Function to add storage to the dictionary used by the Event Catalogue.
int EventCatalogueStats(int &record_count)
Function to collect summary statistics on the Event Catalogue.
int EventCatalogueChecks(int &file_not_found, vector< unique_ptr< EventInfo > > &event_list)
Function to perform a set of specific checks in the Event Catalogue.
vector< unique_ptr< EventInfo > > GetEventList(vector< unique_ptr< EventCondition > > event_conditions)
Function for searching for event metadata in the Event Catalogue using specified conditions.
int PrintEventInfo(string storage_name, string file_path, int event_number)
Function to print metadata for the event stored at the given location.
unique_ptr< EventInfo > GetEventInfo(string storage_name, string file_path, int event_number)
Function tp get metadata of the event stored at the given location.
matchCriteria EventMatchCriteria
searchAttributes
enumeration for attributes, which can be used in searching event metadata
searchAttributes EventAttribute
matchCriteria
enumeration for conditions, which can be used for matching with a specified value
unsigned int uiValue
Class presenting Event Info (Metadata) Structure.
static const int Version
structure (API) version
const char *const EVENT_API_HOST
const char *const EVENT_API_NAME
const char *const EVENT_API_PASSWORD
const char *const EVENT_API_USERNAME
basic_json< nlohmann::ordered_map > ordered_json
specialization that maintains the insertion order of object keys
Definition json.hpp:3374