Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
dcdb
dcdb
Commits
014b619f
Commit
014b619f
authored
Sep 02, 2019
by
Carla Guillen Carias
Browse files
Using boost gregorian and posix_time libraries instead of chrono
parent
3d72b599
Changes
4
Hide whitespace changes
Inline
Side-by-side
analytics/operators/persystsql/PerSystDB.cpp
View file @
014b619f
...
...
@@ -33,13 +33,16 @@
#include
<vector>
#include
<map>
#include
<sys/types.h>
#include
<chrono>
// chrono::system_clock
#include
<ctime>
// localtime
#include
<iomanip>
// put_time
#include
<sys/types.h>
// uid_t
#include
<pwd.h>
// getpwuid
#include
"../../../common/include/timestamp.h"
#include
"PerSystDB.h"
#include
"boost/date_time/gregorian/gregorian.hpp"
#include
"boost/date_time/local_time/local_time.hpp"
#include
"boost/date_time/posix_time/posix_time.hpp"
class
SQLResult
{
private:
...
...
@@ -67,17 +70,24 @@ void PerSystDB::print_error(){
//mysql_close(mysql);
}
PerSystDB
::
PerSystDB
(
MYSQL
*
mysql
,
Rotation_t
rotation
,
unsigned
int
every_x_days
)
:
_mysql
(
mysql
),
_rotation
(
rotation
),
_every_x_days
(
every_x_days
),
_internal_connection
(
false
),
_aggregate_timestamp
(
0
)
{
PerSystDB
::
PerSystDB
(
MYSQL
*
mysql
,
Rotation_t
rotation
,
unsigned
int
every_x_days
)
:
_mysql
(
mysql
),
_rotation
(
rotation
),
_every_x_days
(
every_x_days
),
_internal_connection
(
false
),
_end_aggregate_timestamp
(
0
)
{
}
PerSystDB
::
PerSystDB
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
password
,
const
std
::
string
&
database_name
,
int
port
,
Rotation_t
rotation
,
unsigned
int
every_x_days
)
:
_rotation
(
rotation
),
_every_x_days
(
every_x_days
),
_internal_connection
(
true
),
_aggregate_timestamp
(
0
)
{
PerSystDB
::
PerSystDB
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
password
,
const
std
::
string
&
database_name
,
int
port
,
Rotation_t
rotation
,
unsigned
int
every_x_days
)
:
_rotation
(
rotation
),
_every_x_days
(
every_x_days
),
_internal_connection
(
true
),
_end_aggregate_timestamp
(
0
)
{
_mysql
=
mysql_init
(
NULL
);
if
(
!
mysql_real_connect
(
_mysql
,
host
.
c_str
(),
user
.
c_str
(),
password
.
c_str
(),
database_name
.
c_str
(),
port
,
NULL
,
0
)){
print_error
();
}
}
PerSystDB
::~
PerSystDB
(){
if
(
_internal_connection
){
mysql_close
(
_mysql
);
...
...
@@ -117,37 +127,48 @@ bool PerSystDB::getDBJobIDs(std::vector<std::string> & job_id_strings, std::map<
bool
PerSystDB
::
getCurrentSuffixAggregateTable
(
std
::
string
&
suffix
){
auto
now
=
std
::
chrono
::
system_clock
::
now
();
auto
in_time_t
=
std
::
chrono
::
system_clock
::
to_time_t
(
now
);
auto
date_time
=
std
::
put_time
(
std
::
localtime
(
&
in_time_t
),
"%Y-%m-%d %H:%M:%S"
);
if
(
_end_aggregate_timestamp
){
auto
now_uts
=
getTimestamp
();
if
(
now_uts
<
_end_aggregate_timestamp
)
{
//suffix found, don't do anything
suffix
=
_current_table_suffix
;
return
true
;
}
}
auto
right_now
=
boost
::
posix_time
::
second_clock
::
local_time
();
auto
date_time
=
boost
::
posix_time
::
to_iso_extended_string
(
right_now
);
std
::
replace
(
date_time
.
begin
(),
date_time
.
end
(),
'T'
,
' '
);
std
::
stringstream
build_query
;
build_query
<<
"SELECT suffix FROM SuffixToAggregateTable WHERE begin_timestamp <
\'
"
;
build_query
<<
"SELECT suffix
, UNIX_TIMESTAMP(end_timestamp)
FROM SuffixToAggregateTable WHERE begin_timestamp <
\'
"
;
build_query
<<
date_time
<<
"
\'
AND end_timestamp >
\'
"
<<
date_time
<<
"
\'
"
;
auto
query
=
build_query
.
str
();
#if DEBUG
std
::
cout
<<
query
<<
std
::
endl
;
#endif
std
::
cout
<<
query
<<
std
::
endl
;
#endif
if
(
mysql_real_query
(
_mysql
,
query
.
c_str
(),
query
.
size
())){
print_error
();
return
false
;
print_error
();
return
false
;
}
SQLResult
result
(
_mysql
);
if
(
result
.
get
()){
MYSQL_ROW
row
;
while
(
row
=
result
.
fetch_row
()){
if
(
row
[
0
]){
suffix
=
std
::
string
(
row
[
0
]);
return
true
;
}
else
{
return
false
;
}
}
MYSQL_ROW
row
;
while
(
row
=
result
.
fetch_row
()){
if
(
row
[
0
]
&&
row
[
1
]){
suffix
=
std
::
string
(
row
[
0
]);
std
::
string
row1
(
row
[
1
]);
_end_aggregate_timestamp
=
std
::
stoull
(
row1
)
*
1e9
;
return
true
;
}
else
{
return
false
;
}
}
}
return
false
;
}
bool
PerSystDB
::
insertIntoJob
(
const
std
::
string
&
job_id_string
,
unsigned
long
long
uid
,
int
&
job_id_db
,
const
std
::
string
&
suffix
){
std
::
stringstream
build_insert
;
build_insert
<<
"INSERT INTO Accounting (job_id_string, user, aggregate_first_suffix, aggregate_last_suffix) VALUES (
\'
"
<<
job_id_string
<<
"
\'
,
\'
"
;
...
...
@@ -211,13 +232,9 @@ bool PerSystDB::createNewAggregate(std::string& new_suffix){
}
}
if
(
end_timestamp
.
size
()
==
0
){
auto
now
=
std
::
chrono
::
system_clock
::
now
();
auto
in_time_t
=
std
::
chrono
::
system_clock
::
to_time_t
(
now
);
auto
datetime
=
std
::
put_time
(
std
::
localtime
(
&
in_time_t
),
"%Y-%m-%d %H:%M:%S"
);
std
::
stringstream
dt
;
dt
<<
datetime
;
end_timestamp
=
dt
.
str
().
substr
(
0
,
11
);
end_timestamp
=
end_timestamp
+
"00:00:00"
;
boost
::
gregorian
::
date
today
=
boost
::
gregorian
::
day_clock
::
local_day
();
auto
today_iso
=
to_iso_extended_string
(
today
);
end_timestamp
=
today_iso
+
" 00:00:00"
;
}
int
new_suff
=
std
::
stoi
(
last_suffix
)
+
1
;
...
...
@@ -285,38 +302,40 @@ bool PerSystDB::getTableSuffix(std::string & table_suffix){
return
true
;
}
void
PerSystDB
::
getNewDates
(
const
std
::
string
&
last_end_timestamp
,
std
::
string
&
begin_timestamp
,
std
::
string
&
end_timestamp
){
using
days
=
std
::
chrono
::
duration
<
int
,
std
::
ratio_multiply
<
std
::
ratio
<
24
>
,
std
::
chrono
::
hours
::
period
>>
;
using
years
=
std
::
chrono
::
duration
<
int
,
std
::
ratio_multiply
<
std
::
ratio
<
146097
,
400
>
,
days
::
period
>>
;
using
months
=
std
::
chrono
::
duration
<
int
,
std
::
ratio_divide
<
years
::
period
,
std
::
ratio
<
12
>>>
;
void
PerSystDB
::
getNewDates
(
const
std
::
string
&
last_end_timestamp
,
std
::
string
&
begin_timestamp
,
std
::
string
&
end_timestamp
)
{
begin_timestamp
=
last_end_timestamp
;
std
::
tm
tm
=
{
};
std
::
stringstream
ss
(
last_end_timestamp
);
ss
>>
std
::
get_time
(
&
tm
,
"%Y-%m-%d %H:%M:%S"
);
auto
tp
=
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
boost
::
gregorian
::
date
d
=
boost
::
gregorian
::
date_from_tm
(
tm
);
switch
(
_rotation
)
{
case
EVERY_YEAR
:
tp
+=
years
{
1
}
;
break
;
case
EVERY_MONTH
:
tp
+=
months
{
1
}
;
break
;
case
EVERY_XDAYS
:
tp
+=
days
{
_every_x_days
}
;
break
;
default:
tp
+=
months
{
1
}
;
break
;
case
EVERY_YEAR
:
d
+=
boost
::
gregorian
::
months
(
12
)
;
break
;
case
EVERY_MONTH
:
d
+=
boost
::
gregorian
::
months
(
1
)
;
break
;
case
EVERY_XDAYS
:
d
+=
boost
::
gregorian
::
days
(
_every_x_days
)
;
break
;
default:
d
+=
boost
::
gregorian
::
months
(
1
)
;
break
;
};
auto
in_time_t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
auto
date_time
=
std
::
put_time
(
std
::
localtime
(
&
in_time_t
),
"%Y-%m-%d %H:%M:%S"
);
std
::
stringstream
build_date
;
build_date
<<
date_time
;
end_timestamp
=
build_date
.
str
();
boost
::
posix_time
::
ptime
epoch
(
boost
::
gregorian
::
date
(
1970
,
1
,
1
));
boost
::
posix_time
::
ptime
end_ts
(
d
);
boost
::
posix_time
::
time_duration
diff
=
end_ts
-
epoch
;
_end_aggregate_timestamp
=
diff
.
total_seconds
()
*
1e9
;
end_timestamp
=
to_iso_extended_string
(
d
)
+
" 00:00:00"
;
#if DEBUG
std
::
cout
<<
"boost_end_timestamp ="
<<
_end_aggregate_timestamp
<<
std
::
endl
;
std
::
cout
<<
"boost_end_aggregate_timestamp="
<<
end_timestamp
<<
std
::
endl
;
#endif
}
analytics/operators/persystsql/PerSystDB.h
View file @
014b619f
...
...
@@ -58,9 +58,10 @@ protected:
Rotation_t
_rotation
;
unsigned
int
_every_x_days
;
//ignored except when EVERY_XDAYS is chosen
bool
_internal_connection
;
unsigned
long
long
_aggregate_timestamp
;
unsigned
long
long
_end
_aggregate_timestamp
;
std
::
string
_current_table_suffix
;
void
print_error
();
public:
PerSystDB
(
MYSQL
*
mysql
,
Rotation_t
rotation
,
unsigned
int
every_x_days
=
0
);
...
...
@@ -83,4 +84,5 @@ public:
};
#endif
/* ANALYTICS_OPERATORS_PERSYSTSQL_PERSYSTDB_H_ */
analytics/operators/persystsql/PerSystSqlOperator.cpp
View file @
014b619f
...
...
@@ -25,20 +25,24 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//================================================================================
#include
"PerSystSqlOperator.h"
#include
<boost/log/sources/record_ostream.hpp>
#include
<boost/log/trivial.hpp>
#include
<boost/log/utility/formatting_ostream.hpp>
#include
<boost/parameter/keyword.hpp>
#include
<stddef.h>
#include
<
sys/types.
h>
#include
<
cmat
h>
#include
<cstdint>
#include
<memory>
#include
<string>
#include
"../../../common/include/logging.h"
#include
"../../../common/include/sensorbase.h"
#include
"../../../common/include/timestamp.h"
#include
"../../includes/CommonStatistics.h"
#include
"../../includes/QueryEngine.h"
#include
"
PerSystSqlOperator
.h"
#include
"
../../includes/UnitTemplate
.h"
PerSystSqlOperator
::
PerSystSqlOperator
(
const
std
::
string
&
name
)
:
OperatorTemplate
(
name
),
JobOperatorTemplate
(
name
),
_number_of_even_quantiles
(
0
),
...
...
@@ -102,7 +106,7 @@ void PerSystSqlOperator::compute_internal(U_Ptr unit, vector<reading_t> buffer)
reading
.
value
=
computeObs
(
buffer
);
break
;
case
AggregatorSensorBase
::
AVG_SEV
:
reading
.
value
=
computeSeverityAverage
(
buffer
);
reading
.
value
=
computeSeverityAverage
(
buffer
);
//ToDo an appropriate cast needed here
break
;
default:
LOG
(
warning
)
<<
_name
<<
": Encountered unknown operation!"
;
...
...
@@ -188,6 +192,40 @@ float severity_memory(float metric, float threshold, float max_memory){
return
severity
;
}
int64_t
PerSystSqlOperator
::
computeSeverityAverage
(
vector
<
reading_t
>
&
buffer
){
return
0
;
float
PerSystSqlOperator
::
computeSeverityAverage
(
vector
<
reading_t
>
&
buffer
){
std
::
vector
<
float
>
severities
;
switch
(
_severity_formula
)
{
case
(
FORMULA1
):
for
(
auto
reading
:
buffer
){
auto
severity
=
severity_formula1
(
reading
.
value
,
_severity_threshold
,
_severity_exponent
);
severities
.
push_back
(
severity
);
}
break
;
case
(
FORMULA2
):
for
(
auto
reading
:
buffer
){
auto
severity
=
severity_formula2
(
reading
.
value
,
_severity_threshold
,
_severity_exponent
);
severities
.
push_back
(
severity
);
}
break
;
case
(
FORMULA3
):
for
(
auto
reading
:
buffer
){
auto
severity
=
severity_formula3
(
reading
.
value
,
_severity_threshold
,
_severity_exponent
);
severities
.
push_back
(
severity
);
}
break
;
case
(
MEMORY_FORMULA
):
for
(
auto
reading
:
buffer
){
auto
severity
=
severity_memory
(
reading
.
value
,
_severity_threshold
,
_severity_max_memory
);
severities
.
push_back
(
severity
);
}
break
;
case
(
NOFORMULA
):
default:
return
0.0
;
break
;
}
if
(
severities
.
size
()){
return
(
std
::
accumulate
(
severities
.
begin
(),
severities
.
end
(),
0.0
f
)
/
severities
.
size
());
}
return
0.0
;
}
analytics/operators/persystsql/PerSystSqlOperator.h
View file @
014b619f
...
...
@@ -82,7 +82,7 @@ private:
protected:
virtual
void
compute
(
U_Ptr
unit
)
override
;
void
compute_internal
(
U_Ptr
unit
,
vector
<
reading_t
>
buffer
);
int64_
t
computeSeverityAverage
(
vector
<
reading_t
>
&
buffer
);
floa
t
computeSeverityAverage
(
vector
<
reading_t
>
&
buffer
);
std
::
vector
<
AggregatorSBPtr
>
_quantileSensors
;
unsigned
int
_number_of_even_quantiles
;
Formula
_severity_formula
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment