summaryrefslogtreecommitdiffstats
path: root/lib/db_ido/timeperioddbobject.cpp
blob: 98997f527837cdfb7433ef698b9d7add4c6c4a6a (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<TimePeriod>(GetObject());

	return new Dictionary({
		{ "alias", tp->GetDisplayName() }
	});
}

Dictionary::Ptr TimePeriodDbObject::GetStatusFields() const
{
	return Empty;
}

void TimePeriodDbObject::OnConfigUpdateHeavy()
{
	TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(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);
		}
	}
}