icinga2/lib/db_ido/timeperioddbobject.cpp

103 lines
3.8 KiB
C++
Raw Normal View History

2013-07-31 09:14:58 +02:00
/******************************************************************************
* Icinga 2 *
2016-01-12 08:29:59 +01:00
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
2013-07-31 09:14:58 +02:00
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
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
#include <boost/foreach.hpp>
using namespace icinga;
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
{ }
Dictionary::Ptr TimePeriodDbObject::GetConfigFields(void) const
{
Dictionary::Ptr fields = new Dictionary();
TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
2013-07-31 09:14:58 +02:00
fields->Set("alias", tp->GetDisplayName());
2013-07-31 09:14:58 +02:00
return fields;
}
Dictionary::Ptr TimePeriodDbObject::GetStatusFields(void) const
{
return Empty;
}
2013-08-09 10:39:03 +02:00
void TimePeriodDbObject::OnConfigUpdateHeavy(void)
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;
query_del1.Category = DbCatConfig;
query_del1.WhereCriteria = new Dictionary();
2013-08-09 10:39:03 +02:00
query_del1.WhereCriteria->Set("timeperiod_id", DbValue::FromObjectInsertID(tp));
OnQuery(query_del1);
Dictionary::Ptr ranges = tp->GetRanges();
2013-08-09 10:39:03 +02:00
if (!ranges)
return;
time_t refts = Utility::GetTime();
ObjectLock olock(ranges);
BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
int wday = LegacyTimePeriod::WeekdayFromString(kv.first);
2013-08-09 10:39:03 +02:00
if (wday == -1)
continue;
tm reference = Utility::LocalTime(refts);
2013-08-09 10:39:03 +02:00
Array::Ptr segments = new Array();
LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
2013-09-12 10:30:28 +02:00
ObjectLock olock(segments);
BOOST_FOREACH(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();
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query.Fields->Set("timeperiod_id", DbValue::FromObjectInsertID(tp));
query.Fields->Set("day", wday);
query.Fields->Set("start_sec", begin % 86400);
query.Fields->Set("end_sec", end % 86400);
OnQuery(query);
}
2013-08-09 10:39:03 +02:00
}
}