]> granicus.if.org Git - icinga2/blob - lib/db_ido/timeperioddbobject.cpp
Merge pull request #7124 from Icinga/bugfix/namespace-thread-safe
[icinga2] / lib / db_ido / timeperioddbobject.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "db_ido/timeperioddbobject.hpp"
4 #include "db_ido/dbtype.hpp"
5 #include "db_ido/dbvalue.hpp"
6 #include "icinga/timeperiod.hpp"
7 #include "icinga/legacytimeperiod.hpp"
8 #include "base/utility.hpp"
9 #include "base/exception.hpp"
10 #include "base/objectlock.hpp"
11
12 using namespace icinga;
13
14 REGISTER_DBTYPE(TimePeriod, "timeperiod", DbObjectTypeTimePeriod, "timeperiod_object_id", TimePeriodDbObject);
15
16 TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
17         : DbObject(type, name1, name2)
18 { }
19
20 Dictionary::Ptr TimePeriodDbObject::GetConfigFields() const
21 {
22         TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
23
24         return new Dictionary({
25                 { "alias", tp->GetDisplayName() }
26         });
27 }
28
29 Dictionary::Ptr TimePeriodDbObject::GetStatusFields() const
30 {
31         return Empty;
32 }
33
34 void TimePeriodDbObject::OnConfigUpdateHeavy()
35 {
36         TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
37
38         DbQuery query_del1;
39         query_del1.Table = GetType()->GetTable() + "_timeranges";
40         query_del1.Type = DbQueryDelete;
41         query_del1.Category = DbCatConfig;
42         query_del1.WhereCriteria = new Dictionary({
43                 { "timeperiod_id", DbValue::FromObjectInsertID(tp) }
44         });
45         OnQuery(query_del1);
46
47         Dictionary::Ptr ranges = tp->GetRanges();
48
49         if (!ranges)
50                 return;
51
52         time_t refts = Utility::GetTime();
53         ObjectLock olock(ranges);
54         for (const Dictionary::Pair& kv : ranges) {
55                 int wday = LegacyTimePeriod::WeekdayFromString(kv.first);
56
57                 if (wday == -1)
58                         continue;
59
60                 tm reference = Utility::LocalTime(refts);
61
62                 Array::Ptr segments = new Array();
63                 LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
64
65                 ObjectLock olock(segments);
66                 for (const Value& vsegment : segments) {
67                         Dictionary::Ptr segment = vsegment;
68                         int begin = segment->Get("begin");
69                         int end = segment->Get("end");
70
71                         DbQuery query;
72                         query.Table = GetType()->GetTable() + "_timeranges";
73                         query.Type = DbQueryInsert;
74                         query.Category = DbCatConfig;
75                         query.Fields = new Dictionary({
76                                 { "instance_id", 0 }, /* DbConnection class fills in real ID */
77                                 { "timeperiod_id", DbValue::FromObjectInsertID(tp) },
78                                 { "day", wday },
79                                 { "start_sec", begin % 86400 },
80                                 { "end_sec", end % 86400 }
81                         });
82                         OnQuery(query);
83                 }
84         }
85 }