From 0b9317a5bfcec2b3a87085a4dfa81b638e92177c Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 2 Nov 2021 16:42:27 +0100 Subject: [PATCH] IcingaDB: Remove GetObjectIdentifiersWithoutEnv() Having the command type be a part of the command ID isn't needed anywhere. Removing this simplifies the way we generate IDs in general, because we don't need Prepend() anymore. The command type was only needed to prevent ID collisions within the command_envvar and command_argument tables. Those tables have since been separated into {check,event,notification}command_envvar and {check,event,notification}command_argument tables. --- lib/icingadb/icingadb-objects.cpp | 26 +++++++++++++------------- lib/icingadb/icingadb-utility.cpp | 14 ++------------ lib/icingadb/icingadb.hpp | 20 -------------------- 3 files changed, 15 insertions(+), 45 deletions(-) diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index a98d1dc80..66daf6f22 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -629,7 +629,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S } } - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(kv.first, GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, kv.first, object->GetName()})); typeCvs.emplace_back(id); Dictionary::Ptr data = new Dictionary({{objectKeyName, objectKey}, {"environment_id", m_EnvironmentId}, {"customvar_id", kv.first}}); @@ -719,7 +719,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S for (auto& group : groups) { auto groupObj ((*getGroup)(group)); String groupId = GetObjectIdentifier(groupObj); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(GetObjectIdentifiersWithoutEnv(groupObj), GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, groupObj->GetName(), object->GetName()})); members.emplace_back(id); Dictionary::Ptr data = new Dictionary({{objectKeyName, objectKey}, {"environment_id", m_EnvironmentId}, {typeName + "group_id", groupId}}); members.emplace_back(JsonEncode(data)); @@ -750,7 +750,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S String rangeId = HashValue(new Array({m_EnvironmentId, kv.first, kv.second})); rangeIds->Add(rangeId); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(kv.first, Prepend(kv.second, GetObjectIdentifiersWithoutEnv(object)))))); + String id = HashValue(new Array({m_EnvironmentId, kv.first, kv.second, object->GetName()})); typeRanges.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"timeperiod_id", objectKey}, {"range_key", kv.first}, {"range_value", kv.second}}); typeRanges.emplace_back(JsonEncode(data)); @@ -780,7 +780,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S String includeId = GetObjectIdentifier(includeTp); includeChecksums->Add(includeId); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(GetObjectIdentifiersWithoutEnv(includeTp), GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, includeTp->GetName(), object->GetName()})); includs.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"timeperiod_id", objectKey}, {"include_id", includeId}}); includs.emplace_back(JsonEncode(data)); @@ -810,7 +810,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S String excludeId = GetObjectIdentifier(excludeTp); excludeChecksums->Add(excludeId); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(GetObjectIdentifiersWithoutEnv(excludeTp), GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, excludeTp->GetName(), object->GetName()})); excluds.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"timeperiod_id", objectKey}, {"exclude_id", excludeId}}); excluds.emplace_back(JsonEncode(data)); @@ -843,7 +843,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S for (auto& group : groups) { auto groupObj ((*getGroup)(group)); String groupId = GetObjectIdentifier(groupObj); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(GetObjectIdentifiersWithoutEnv(groupObj), GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, groupObj->GetName(), object->GetName()})); members.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"user_id", objectKey}, {"environment_id", m_EnvironmentId}, {"usergroup_id", groupId}}); members.emplace_back(JsonEncode(data)); @@ -878,7 +878,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S for (auto& user : users) { String userId = GetObjectIdentifier(user); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(GetObjectIdentifiersWithoutEnv(user), GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, user->GetName(), object->GetName()})); usrs.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"user_id", userId}}); usrs.emplace_back(JsonEncode(data)); @@ -901,7 +901,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S auto groupMembers = usergroup->GetMembers(); std::copy(groupMembers.begin(), groupMembers.end(), std::inserter(allUsers, allUsers.begin())); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend("usergroup", Prepend(GetObjectIdentifiersWithoutEnv(usergroup), GetObjectIdentifiersWithoutEnv(object)))))); + String id = HashValue(new Array({m_EnvironmentId, "usergroup", usergroup->GetName(), object->GetName()})); groups.emplace_back(id); Dictionary::Ptr groupData = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"usergroup_id", usergroupId}}); groups.emplace_back(JsonEncode(groupData)); @@ -920,7 +920,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S for (auto& user : allUsers) { String userId = GetObjectIdentifier(user); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend("user", Prepend(GetObjectIdentifiersWithoutEnv(user), GetObjectIdentifiersWithoutEnv(object)))))); + String id = HashValue(new Array({m_EnvironmentId, "user", user->GetName(), object->GetName()})); notificationRecipients.emplace_back(id); Dictionary::Ptr data = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"user_id", userId}}); notificationRecipients.emplace_back(JsonEncode(data)); @@ -974,7 +974,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S values->Set("argument_key", kv.first); values->Set("environment_id", m_EnvironmentId); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(kv.first, GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, kv.first, object->GetName()})); typeArgs.emplace_back(id); typeArgs.emplace_back(JsonEncode(values)); @@ -1023,7 +1023,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S values->Set("envvar_key", kv.first); values->Set("environment_id", m_EnvironmentId); - String id = HashValue(new Array(Prepend(m_EnvironmentId, Prepend(kv.first, GetObjectIdentifiersWithoutEnv(object))))); + String id = HashValue(new Array({m_EnvironmentId, kv.first, object->GetName()})); typeVars.emplace_back(id); typeVars.emplace_back(JsonEncode(values)); @@ -1543,7 +1543,7 @@ void IcingaDB::SendStateChange(const ConfigObject::Ptr& object, const CheckResul auto eventTime (cr->GetExecutionEnd()); auto eventTs (TimestampToMilliseconds(eventTime)); - Array::Ptr rawId = new Array(Prepend(m_EnvironmentId, GetObjectIdentifiersWithoutEnv(object))); + Array::Ptr rawId = new Array({m_EnvironmentId, object->GetName()}); rawId->Add(eventTs); std::vector xAdd ({ @@ -1623,7 +1623,7 @@ void IcingaDB::SendSentNotification( auto usersAmount (users.size()); auto sendTs (TimestampToMilliseconds(sendTime)); - Array::Ptr rawId = new Array(Prepend(m_EnvironmentId, GetObjectIdentifiersWithoutEnv(notification))); + Array::Ptr rawId = new Array({m_EnvironmentId, notification->GetName()}); rawId->Add(GetNotificationTypeByEnum(type)); rawId->Add(sendTs); diff --git a/lib/icingadb/icingadb-utility.cpp b/lib/icingadb/icingadb-utility.cpp index 769b4b5aa..dbb7fa818 100644 --- a/lib/icingadb/icingadb-utility.cpp +++ b/lib/icingadb/icingadb-utility.cpp @@ -60,19 +60,9 @@ String IcingaDB::FormatCommandLine(const Value& commandLine) return result; } -ArrayData IcingaDB::GetObjectIdentifiersWithoutEnv(const ConfigObject::Ptr& object) -{ - Type::Ptr type = object->GetReflectionType(); - - if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance) - return {type->GetName(), object->GetName()}; - else - return {object->GetName()}; -} - String IcingaDB::GetObjectIdentifier(const ConfigObject::Ptr& object) { - return HashValue(new Array(Prepend(m_EnvironmentId, GetObjectIdentifiersWithoutEnv(object)))); + return HashValue(new Array({m_EnvironmentId, object->GetName()})); } /** @@ -82,7 +72,7 @@ String IcingaDB::GetObjectIdentifier(const ConfigObject::Ptr& object) */ String IcingaDB::CalcEventID(const char* eventType, const ConfigObject::Ptr& object, double eventTime, NotificationType nt) { - Array::Ptr rawId = new Array(GetObjectIdentifiersWithoutEnv(object)); + Array::Ptr rawId = new Array({object->GetName()}); rawId->Insert(0, m_EnvironmentId); rawId->Insert(1, eventType); diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index fba1a2857..168bd2f00 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -106,7 +106,6 @@ private: static long long TimestampToMilliseconds(double timestamp); static String IcingaToStreamValue(const Value& value); - static ArrayData GetObjectIdentifiersWithoutEnv(const ConfigObject::Ptr& object); static String GetObjectIdentifier(const ConfigObject::Ptr& object); static String CalcEventID(const char* eventType, const ConfigObject::Ptr& object, double eventTime = 0, NotificationType nt = NotificationType(0)); static Dictionary::Ptr SerializeVars(const CustomVarObject::Ptr& object); @@ -141,25 +140,6 @@ private: void ExceptionHandler(boost::exception_ptr exp); - template - static inline - std::vector Prepend(std::vector&& needle, std::vector&& haystack) - { - for (auto& hay : haystack) { - needle.emplace_back(std::move(hay)); - } - - return std::move(needle); - } - - template - static inline - std::vector Prepend(Needle&& needle, std::vector&& haystack) - { - haystack.emplace(haystack.begin(), std::forward(needle)); - return std::move(haystack); - } - static std::vector GetTypes(); Timer::Ptr m_StatsTimer;