icinga2/lib/db_ido/timeperioddbobject.cpp
Yonas Habteab 91c7e60df8 Replace all existing copyright headers with SPDX headers
I've used the following command to replace the original copyright header
lines in a C-style comment block:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

For files that use shell-style comments (#) like CMakeLists.txt, I've
used this command:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

And for SQL files:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```
2026-02-04 14:00:05 +01:00

86 lines
2.3 KiB
C++

// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#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);
}
}
}