From 56ae875861ab260b80a030f50c4aff9f9dc8fff0 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:32:39 +0200 Subject: Adding upstream version 2.14.2. Signed-off-by: Daniel Baumann --- lib/db_ido/timeperioddbobject.cpp | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/db_ido/timeperioddbobject.cpp (limited to 'lib/db_ido/timeperioddbobject.cpp') diff --git a/lib/db_ido/timeperioddbobject.cpp b/lib/db_ido/timeperioddbobject.cpp new file mode 100644 index 0000000..98997f5 --- /dev/null +++ b/lib/db_ido/timeperioddbobject.cpp @@ -0,0 +1,85 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#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" + +using namespace icinga; + +REGISTER_DBTYPE(TimePeriod, "timeperiod", DbObjectTypeTimePeriod, "timeperiod_object_id", TimePeriodDbObject); + +TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) +{ } + +Dictionary::Ptr TimePeriodDbObject::GetConfigFields() const +{ + TimePeriod::Ptr tp = static_pointer_cast(GetObject()); + + return new Dictionary({ + { "alias", tp->GetDisplayName() } + }); +} + +Dictionary::Ptr TimePeriodDbObject::GetStatusFields() const +{ + return Empty; +} + +void TimePeriodDbObject::OnConfigUpdateHeavy() +{ + TimePeriod::Ptr tp = static_pointer_cast(GetObject()); + + DbQuery query_del1; + query_del1.Table = GetType()->GetTable() + "_timeranges"; + query_del1.Type = DbQueryDelete; + query_del1.Category = DbCatConfig; + query_del1.WhereCriteria = new Dictionary({ + { "timeperiod_id", DbValue::FromObjectInsertID(tp) } + }); + OnQuery(query_del1); + + Dictionary::Ptr ranges = tp->GetRanges(); + + if (!ranges) + return; + + time_t refts = Utility::GetTime(); + ObjectLock olock(ranges); + for (const Dictionary::Pair& kv : ranges) { + int wday = LegacyTimePeriod::WeekdayFromString(kv.first); + + if (wday == -1) + continue; + + tm reference = Utility::LocalTime(refts); + + Array::Ptr segments = new Array(); + LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments); + + ObjectLock olock(segments); + for (const Value& vsegment : segments) { + Dictionary::Ptr segment = vsegment; + int begin = segment->Get("begin"); + int end = segment->Get("end"); + + DbQuery query; + query.Table = GetType()->GetTable() + "_timeranges"; + query.Type = DbQueryInsert; + query.Category = DbCatConfig; + 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 } + }); + OnQuery(query); + } + } +} -- cgit v1.2.3