PerSystDB.h 4.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//================================================================================
// Name        : PerSystDB.h
// Author      : Carla Guillen
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Template implementing features to use Units in Operators.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

#ifndef ANALYTICS_OPERATORS_PERSYSTSQL_PERSYSTDB_H_
#define ANALYTICS_OPERATORS_PERSYSTSQL_PERSYSTDB_H_

31
#include "mariadb/mysql.h"
32
#include "../../../common/include/logging.h"
33
34
35
#include <vector>
#include <string>
#include <map>
36
#include <mutex>
37
38

struct Aggregate_info_t {
39
40
41
42
43
44
45
	std::string job_id_db;
	unsigned int timestamp;
	unsigned int property_type_id;
	unsigned int num_of_observations;
	float average;
	std::vector<float> quantiles;
	float severity_average;
46
47
};

48
class PerSystDB {
49

50
51
52
53
54
private:
	struct Job_info_t {
		std::string job_id_db;
		unsigned long long last_seen_timestamp;
	};
55
56

public:
57
58
59
	enum Rotation_t {
		EVERY_YEAR, EVERY_MONTH, EVERY_XDAYS //number of days must be provided
	};
60
61

protected:
62
63
64
65

	PerSystDB();
	virtual ~PerSystDB();

66
67
68
69
70
71
	MYSQL *_mysql;
	Rotation_t _rotation;
	unsigned int _every_x_days; //ignored except when EVERY_XDAYS is chosen
	unsigned long long _end_aggregate_timestamp;
	std::string _current_table_suffix;
	boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
72

73
74
	static PerSystDB * instance;
	static std::mutex mut;
75
	bool _initialized;
76
77
78
79
80
81
82
83
	std::map<std::string, Job_info_t> _jobCache;
	const std::size_t JOB_CACHE_MAX_SIZE = 10000;

	/** print error.
	 * Prints the mysql error message. If connection is gone (Error 2006) then we also close the connection.
	 * Please check with isInitialized() to initialize it again.
	 */
	void print_error();
84
85
	bool getCurrentSuffixAggregateTable(std::string & new_suffix);
	bool createNewAggregate(std::string& new_suffix);
86
87
	void getNewDates(const std::string& last_end_timestamp,	std::string & begin_timestamp, std::string & end_timestamp);
	void addJobToCache(std::string &job_id_string, std::string & job_id_db);
88

89
public:
Carla Guillen's avatar
Carla Guillen committed
90

91
92
93
94
95
96
97
98
99
	/**
	 * Connect to database.
	 */
	bool initializeConnection(const std::string & host,	const std::string & user, const std::string & password,
			const std::string & database_name, Rotation_t rotation, int port = 3306, unsigned int every_x_days = 0);

	bool isInitialized(){
		return _initialized;
	}
100

101
102
103
104
105
106
107
108
109
110
111
	/**
	 * Disconnect
	 */
	bool finalizeConnection();

	/**
	 * Check if job_id (db) exist. If map empty it doesn't exist/job not found is not yet on accounting.
	 * @param job_id_strings	job id strings including array jobid.
	 * @param job_id_map    job_id_string to job_id (db) map
	 */
	bool getDBJobIDs(std::vector<std::string> & job_id_strings,	std::map<std::string, std::string>& job_id_map);
Carla Guillen's avatar
Carla Guillen committed
112

113
114
115
	/**
	 * Insert job in the accounting table.
	 */
116
	bool insertIntoJob(const std::string& job_id_string, const std::string& uid, int & job_id_db, const std::string & suffix);
117
118
119
120

	/**
	 * Insert performance data into the aggregate table (Aggregate_<suffix>
	 */
Carla Guillen's avatar
Carla Guillen committed
121
	bool insertInAggregateTable(const std::string& suffix, Aggregate_info_t & agg_info);
122
123
124
125

	/**
	 * Update the last suffix in the Accounting table
	 */
Carla Guillen's avatar
Carla Guillen committed
126
	bool updateJobsLastSuffix(std::map<std::string, std::string>& job_map, std::string & suffix);
127
128
129
130

	/**
	 * Get the next or the current table suffix
	 */
Carla Guillen's avatar
Carla Guillen committed
131
	bool getTableSuffix(std::string & table_suffix);
132
133
134
135

	/**
	 * Singleton object. Get here your instance!
	 */
136
	static PerSystDB * getInstance();
137
138
139
};


140

141
#endif /* ANALYTICS_OPERATORS_PERSYSTSQL_PERSYSTDB_H_ */