2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-07-31 09:14:58 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/timeperioddbobject.hpp"
|
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "icinga/timeperiod.hpp"
|
|
|
|
#include "icinga/legacytimeperiod.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2013-07-31 09:14:58 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-02 17:12:07 +02:00
|
|
|
REGISTER_DBTYPE(TimePeriod, "timeperiod", DbObjectTypeTimePeriod, "timeperiod_object_id", TimePeriodDbObject);
|
2013-07-31 09:14:58 +02:00
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
2013-07-31 09:14:58 +02:00
|
|
|
{ }
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr TimePeriodDbObject::GetConfigFields() const
|
2013-07-31 09:14:58 +02:00
|
|
|
{
|
2013-08-05 10:06:19 +02:00
|
|
|
TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
|
2013-07-31 09:14:58 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "alias", tp->GetDisplayName() }
|
|
|
|
});
|
2013-07-31 09:14:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr TimePeriodDbObject::GetStatusFields() const
|
2013-07-31 09:14:58 +02:00
|
|
|
{
|
|
|
|
return Empty;
|
|
|
|
}
|
2013-08-09 10:39:03 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void TimePeriodDbObject::OnConfigUpdateHeavy()
|
2013-08-09 10:39:03 +02:00
|
|
|
{
|
|
|
|
TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
|
|
|
|
|
|
|
|
DbQuery query_del1;
|
|
|
|
query_del1.Table = GetType()->GetTable() + "_timeranges";
|
|
|
|
query_del1.Type = DbQueryDelete;
|
2013-10-29 15:54:43 +01:00
|
|
|
query_del1.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
query_del1.WhereCriteria = new Dictionary({
|
|
|
|
{ "timeperiod_id", DbValue::FromObjectInsertID(tp) }
|
|
|
|
});
|
2013-08-09 10:39:03 +02:00
|
|
|
OnQuery(query_del1);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Dictionary::Ptr ranges = tp->GetRanges();
|
2013-08-09 10:39:03 +02:00
|
|
|
|
|
|
|
if (!ranges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
ObjectLock olock(ranges);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : ranges) {
|
2013-11-30 17:42:50 +01:00
|
|
|
int wday = LegacyTimePeriod::WeekdayFromString(kv.first);
|
2013-08-09 10:39:03 +02:00
|
|
|
|
|
|
|
if (wday == -1)
|
|
|
|
continue;
|
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
tm reference = Utility::LocalTime(refts);
|
2013-08-09 10:39:03 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr segments = new Array();
|
2013-11-30 17:42:50 +01:00
|
|
|
LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
|
2013-09-09 15:54:19 +02:00
|
|
|
|
2013-09-12 10:30:28 +02:00
|
|
|
ObjectLock olock(segments);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& vsegment : segments) {
|
2013-09-09 15:54:19 +02:00
|
|
|
Dictionary::Ptr segment = vsegment;
|
|
|
|
int begin = segment->Get("begin");
|
|
|
|
int end = segment->Get("end");
|
|
|
|
|
|
|
|
DbQuery query;
|
|
|
|
query.Table = GetType()->GetTable() + "_timeranges";
|
|
|
|
query.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
query.Fields = new Dictionary({
|
|
|
|
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
|
|
|
|
{ "timeperiod_id", DbValue::FromObjectInsertID(tp) },
|
|
|
|
{ "day", wday },
|
|
|
|
{ "start_sec", begin % 86400 },
|
|
|
|
{ "end_sec", end % 86400 }
|
|
|
|
});
|
2013-09-09 15:54:19 +02:00
|
|
|
OnQuery(query);
|
|
|
|
}
|
2013-08-09 10:39:03 +02:00
|
|
|
}
|
|
|
|
}
|