BmnRoot
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
63EventCondition::EventCondition()
64 : EventAttribute(kError)
65 , EventMatchCriteria(kEqual)
66 , iValueType(0)
67{}
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
80 ServiceUrl =
81 EVENT_API_HOST + string("/") + EVENT_API_NAME + string("/v") + to_string(EventInfo::Version) + string("/");
82 ServiceAccount = EVENT_API_USERNAME + string(":") + EVENT_API_PASSWORD;
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
100vector<unique_ptr<EventInfo>> EventCatalogue::GetEventList(vector<unique_ptr<EventCondition>> event_conditions)
101{
102 vector<unique_ptr<EventInfo>> event_array;
103 cout << "ERROR: EventCatalogue::GetEventList is not implemented yet" << endl;
104
105 return event_array;
106}
107
115unique_ptr<EventInfo> EventCatalogue::GetEventInfo(string storage_name, string file_path, int event_number)
116{
117 unique_ptr<EventInfo> event_info;
118 cout << "ERROR: EventCatalogue::GetEventInfo is not implemented yet" << endl;
119
120 return event_info;
121}
122
132int EventCatalogue::AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
133{
134 cout << "ERROR: EventCatalogue::AddEventInfo is not implemented yet" << endl;
135
136 return 0;
137}
138
139size_t response_callback(void* contents, size_t size, size_t nmemb, string* data)
140{
141 size_t realsize = size * nmemb;
142 try {
143 data->append((char*)contents, realsize);
144 } catch (std::bad_alloc& e) {
145 // handle memory problem
146 return 0;
147 }
148
149 return realsize;
150}
151
152static size_t header_callback(char* buffer, size_t size, size_t nitems, void* userdata)
153{
154 string* headers = (string*)userdata;
155 try {
156 headers->append(buffer, nitems * size);
157 } catch (std::bad_alloc& e) {
158 // handle memory problem
159 return 0;
160 }
161
162 return nitems * size;
163}
164
170int EventCatalogue::AddEventInfo(string json_file_path)
171{
172 ifstream json_file(json_file_path);
173 ostringstream sstr;
174 sstr << json_file.rdbuf();
175 string json_string = sstr.str();
176 if (VerboseLevel > 2)
177 cout << "DEBUG: json_string = " << json_string << endl;
178
179 string success_message =
180 "Event metadata have been written from JSON file '" + json_file_path + "' to the Event Catalogue";
181 int return_code = PostRequest(ServiceUrl + "event", json_string, success_message);
182
183 return return_code;
184}
185
195int EventCatalogue::UpdateEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
196{
197 cout << "ERROR: EventCatalogue::UpdateEventInfo is not implemented yet" << endl;
198
199 return 0;
200}
201
210int EventCatalogue::DeleteEventInfo(string storage_name, string file_path, int event_number)
211{
212 cout << "ERROR: EventCatalogue::DeleteEventInfo is not implemented yet" << endl;
213
214 return 0;
215}
216
225int EventCatalogue::CheckEventInfo(string storage_name, string file_path, int event_number)
226{
227 cout << "ERROR: EventCatalogue::CheckEventInfo is not implemented yet" << endl;
228
229 return 0;
230}
231
240int EventCatalogue::PrintEventInfo(string storage_name, string file_path, int event_number)
241{
242 cout << "ERROR: EventCatalogue::PrintEventInfo is not implemented yet" << endl;
243
244 return 0;
245}
246
252int EventCatalogue::AddStorageInfo(string storage_name)
253{
254 // write storage metadata to JSON element
255 // json storage_element;
256 // storage_element["storage_id"] = -1;
257 // storage_element["storage_name"] = storage_name;
258 string json_string = "{\"storage_id\": -1, \"storage_name\": \"" + storage_name + "\"}";
259 if (VerboseLevel > 2)
260 cout << "DEBUG: json_string = " << json_string << endl;
261
262 string success_message = "Storage information has been added for '" + storage_name + "' to the Event Catalogue";
263 int return_code = PostRequest(ServiceUrl + "storage", json_string, success_message);
264
265 return return_code;
266}
267
273int EventCatalogue::AddSoftwareInfo(string software_version)
274{
275 // write software version to JSON element
276 // json storage_element;
277 // storage_element["software_id"] = -1;
278 // storage_element["software_version"] = software_version;
279 string json_string = "{\"software_id\": -1, \"software_version\": \"" + software_version + "\"}";
280 if (VerboseLevel > 2)
281 cout << "ERROR: json_string = " << json_string << endl;
282
283 string success_message =
284 "Software version has been added for '" + software_version + "' version to the Event Catalogue";
285 int return_code = PostRequest(ServiceUrl + "software", json_string, success_message);
286
287 return return_code;
288}
289
296vector<unique_ptr<EventInfo>> EventCatalogue::EventCatalogueRequest(string rest_api_request)
297{
298 vector<unique_ptr<EventInfo>> event_array;
299 cout << "ERROR: EventCatalogue::EventCatalogueRequest is not implemented yet" << endl;
300
301 return event_array;
302}
303
310int EventCatalogue::EventCatalogueChecks(int& file_not_found, vector<unique_ptr<EventInfo>>& event_list)
311{
312 cout << "ERROR: EventCatalogue::EventCatalogueChecks is not implemented yet" << endl;
313
314 return 0;
315}
316
323{
324 cout << "ERROR: EventCatalogue::EventCatalogueStats is not implemented yet" << endl;
325
326 return 0;
327}
328
329int EventCatalogue::PostRequest(string RequestUrl, string json_string, string success_message)
330{
331 struct curl_slist* http_header = nullptr;
332 http_header = curl_slist_append(http_header, "Accept: */*");
333 http_header = curl_slist_append(http_header, "Content-Type: application/json");
334 http_header = curl_slist_append(http_header, "charset: utf-8");
335 curl_easy_setopt(curlInstance, CURLOPT_HTTPHEADER, http_header);
336 curl_easy_setopt(curlInstance, CURLOPT_URL, RequestUrl.c_str());
337 curl_easy_setopt(curlInstance, CURLOPT_USERPWD, ServiceAccount.c_str());
338
339 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPALIVE, 1);
340 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPIDLE, 30);
341 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPINTVL, 30);
342 // curl_easy_setopt(curlInstance, CURLOPT_TCP_KEEPCNT, 720);
343
344 curl_easy_setopt(curlInstance, CURLOPT_POST, 1);
345 curl_easy_setopt(curlInstance, CURLOPT_POSTFIELDS, json_string.c_str());
346
347 string response_string, header_string;
348 curl_easy_setopt(curlInstance, CURLOPT_WRITEFUNCTION, response_callback);
349 curl_easy_setopt(curlInstance, CURLOPT_WRITEDATA, &response_string);
350 curl_easy_setopt(curlInstance, CURLOPT_HEADERFUNCTION, header_callback);
351 curl_easy_setopt(curlInstance, CURLOPT_HEADERDATA, &header_string);
352
353 // write new event metadata to the Event Catalogue
354 int res = curl_easy_perform(curlInstance);
355 if (res) {
356 cout << "ERROR: sending command to '" << RequestUrl << "' failed with code = " << res << endl;
357 curl_slist_free_all(http_header);
358 return -1;
359 }
360 curl_slist_free_all(http_header);
361
362 long response_code;
363 curl_easy_getinfo(curlInstance, CURLINFO_RESPONSE_CODE, &response_code);
364 if (VerboseLevel > 2) {
365 double elapsed;
366 curl_easy_getinfo(curlInstance, CURLINFO_TOTAL_TIME, &elapsed);
367 char* url;
368 curl_easy_getinfo(curlInstance, CURLINFO_EFFECTIVE_URL, &url);
369 cout << "DEBUG: CURLINFO_TOTAL_TIME = " << elapsed << ", CURLINFO_EFFECTIVE_URL = " << string(url) << endl;
370 }
371 // check the result of the request
372 if ((response_code >= 200) && (response_code < 205)) {
373 if (VerboseLevel > 0)
374 cout << success_message << endl;
375 if (VerboseLevel > 1)
376 cout << "Request has been completed with the following response: " << response_string << endl;
377 } else {
378 string error_message = GetErrorString(response_code);
379 cout << "ERROR: Sending request to '" << RequestUrl << "' has been failed";
380 if (error_message != "")
381 cout << ": " << error_message << endl;
382 else
383 cout << " with code = " << response_code << endl;
384 if (VerboseLevel > 1)
385 cout << "Error Message:" << endl
386 << response_string << endl
387 << "Error Output Header:" << endl
388 << header_string << endl;
389 return -1 * response_code;
390 }
391
392 return 0;
393}
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)
int AddSoftwareInfo(string software_version)
int UpdateEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
vector< unique_ptr< EventInfo > > EventCatalogueRequest(string rest_api_request)
int AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
int CheckEventInfo(string storage_name, string file_path, int event_number)
int AddStorageInfo(string storage_name)
int EventCatalogueStats(int &record_count)
int EventCatalogueChecks(int &file_not_found, vector< unique_ptr< EventInfo > > &event_list)
vector< unique_ptr< EventInfo > > GetEventList(vector< unique_ptr< EventCondition > > event_conditions)
int PrintEventInfo(string storage_name, string file_path, int event_number)
unique_ptr< EventInfo > GetEventInfo(string storage_name, string file_path, int event_number)
searchAttributes
enumeration for attributes, which can be used in searching event metadata
matchCriteria
enumeration for conditions, which can be used for matching with a specified value
EventCondition(searchAttributes attribute, matchCriteria condition)
Class presenting Event Info (Metadata) Structure.
static const int Version
structure (API) version
a class to store JSON values
Definition json.hpp:17282
iterator find(KeyT &&key)
find an element in a JSON object
Definition json.hpp:19600
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:3346