// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later

#include "icingadb/icingadb.hpp"
#include "icingadb/redisconnection.hpp"
#include "base/configtype.hpp"
#include "base/configobject.hpp"
#include "base/defer.hpp"
#include "base/json.hpp"
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/shared.hpp"
#include "base/tlsutility.hpp"
#include "base/initialize.hpp"
#include "base/convert.hpp"
#include "base/array.hpp"
#include "base/exception.hpp"
#include "base/utility.hpp"
#include "icinga/command.hpp"
#include "icinga/customvarobject.hpp"
#include "icinga/dependency.hpp"
#include "icinga/service.hpp"
#include "icinga/hostgroup.hpp"
#include "icinga/servicegroup.hpp"
#include "icinga/usergroup.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/timeperiod.hpp"
#include "icinga/pluginutility.hpp"
#include "remote/zone.hpp"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iterator>
#include <map>
#include <mutex>
#include <set>
#include <utility>
#include <type_traits>

using namespace icinga;

INITIALIZE_ONCE(&IcingaDB::ConfigStaticInitialize);

// A list of all types for which we want to sync custom variables, along with their corresponding Redis key.
// They are sorted heuristically in the order they are most likely to be accessed, with the most commonly accessed
// ones first. This is not strictly required, but it should avoid some linear searching in the common case.
static constexpr std::pair<const Type::Ptr&, std::string_view> l_CustomVarKeys[] = {
	{Notification::TypeInstance, CONFIG_REDIS_KEY_PREFIX "notification:customvar"},
	{Service::TypeInstance, CONFIG_REDIS_KEY_PREFIX "service:customvar"},
	{Host::TypeInstance, CONFIG_REDIS_KEY_PREFIX "host:customvar"},
	{CheckCommand::TypeInstance, CONFIG_REDIS_KEY_PREFIX "checkcommand:customvar"},
	{ServiceGroup::TypeInstance, CONFIG_REDIS_KEY_PREFIX "servicegroup:customvar"},
	{HostGroup::TypeInstance, CONFIG_REDIS_KEY_PREFIX "hostgroup:customvar"},
	{User::TypeInstance, CONFIG_REDIS_KEY_PREFIX "user:customvar"},
	{UserGroup::TypeInstance, CONFIG_REDIS_KEY_PREFIX "usergroup:customvar"},
	{TimePeriod::TypeInstance, CONFIG_REDIS_KEY_PREFIX "timeperiod:customvar"},
	{NotificationCommand::TypeInstance, CONFIG_REDIS_KEY_PREFIX "notificationcommand:customvar"},
	{EventCommand::TypeInstance, CONFIG_REDIS_KEY_PREFIX "eventcommand:customvar"},
};

// A list of all serializable types that we want to index in Redis, along with their corresponding object and checksum Redis keys.
// They're sorted in the order they should be synced in the initial config sync.
static constexpr std::pair<const Type::Ptr&, IcingaDB::QueryArgPair> l_SyncableTypes[] = {
	// First sync the checkables to get their states ASAP.
	{Host::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "host", CHECKSUM_REDIS_KEY_PREFIX "host"}},
	{Service::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "service", CHECKSUM_REDIS_KEY_PREFIX "service"}},

	// Then sync them for similar reasons.
	{Downtime::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "downtime", CHECKSUM_REDIS_KEY_PREFIX "downtime"}},
	{Comment::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "comment", CHECKSUM_REDIS_KEY_PREFIX "comment"}},

	{HostGroup::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "hostgroup", CHECKSUM_REDIS_KEY_PREFIX "hostgroup"}},
	{ServiceGroup::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "servicegroup", CHECKSUM_REDIS_KEY_PREFIX "servicegroup"}},
	{CheckCommand::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "checkcommand", CHECKSUM_REDIS_KEY_PREFIX "checkcommand"}},
	{Endpoint::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "endpoint", CHECKSUM_REDIS_KEY_PREFIX "endpoint"}},
	{EventCommand::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "eventcommand", CHECKSUM_REDIS_KEY_PREFIX "eventcommand"}},
	{Notification::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "notification", CHECKSUM_REDIS_KEY_PREFIX "notification"}},
	{NotificationCommand::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "notificationcommand", CHECKSUM_REDIS_KEY_PREFIX "notificationcommand"}},
	{TimePeriod::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "timeperiod", CHECKSUM_REDIS_KEY_PREFIX "timeperiod"}},
	{User::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "user", CHECKSUM_REDIS_KEY_PREFIX "user"}},
	{UserGroup::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "usergroup", CHECKSUM_REDIS_KEY_PREFIX "usergroup"}},
	{Zone::TypeInstance, {CONFIG_REDIS_KEY_PREFIX "zone", CHECKSUM_REDIS_KEY_PREFIX "zone"}},
};

const IcingaDB::QueryArgPair& IcingaDB::GetSyncableTypeRedisKeys(const Type::Ptr& type)
{
	auto it = std::find_if(std::begin(l_SyncableTypes), std::end(l_SyncableTypes), [&type](const auto& pair) {
		return pair.first == type;
	});
	if (it == std::end(l_SyncableTypes)) {
		BOOST_THROW_EXCEPTION(std::invalid_argument{"Could not find Redis keys for type " + type->ToString()});
	}
	return it->second;
}

/**
 * Get all syncable types, along with their corresponding Redis keys as a list of pairs.
 *
 * The first element of the pair is the type, the second element is another pair containing the Redis
 * key for the object and the Redis key for the checksum.
 *
 * TODO: Once we upgrade to C++20, this should return a std::span instead to avoid the unnecessary copy.
 *
 * @return A list of all syncable types, along with their corresponding Redis keys.
 */
std::vector<IcingaDB::SyncableTypeInfo> IcingaDB::GetSyncableTypes()
{
	return {std::begin(l_SyncableTypes), std::end(l_SyncableTypes)};
}

void IcingaDB::ConfigStaticInitialize()
{
	/* triggered in ProcessCheckResult(), requires UpdateNextCheck() to be called before */
	Checkable::OnStateChange.connect([](const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr&) {
		IcingaDB::StateChangeHandler(checkable, cr, type);
	});

	Checkable::OnAcknowledgementSet.connect([](const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool, bool persistent, double changeTime, double expiry, const MessageOrigin::Ptr&) {
		AcknowledgementSetHandler(checkable, author, comment, type, persistent, changeTime, expiry);
	});
	Checkable::OnAcknowledgementCleared.connect([](const Checkable::Ptr& checkable, const String& removedBy, double changeTime, const MessageOrigin::Ptr&) {
		AcknowledgementClearedHandler(checkable, removedBy, changeTime);
	});

	Checkable::OnReachabilityChanged.connect([](const Checkable::Ptr& parent, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&) {
		// Icinga DB Web needs to know about the reachability of all children, not just the direct ones.
		// These might get updated with their next check result anyway, but we can't rely on that, since
		// they might not be actively checked or have a very high check interval.
		IcingaDB::ReachabilityChangeHandler(parent->GetAllChildren());
	});

	/* triggered on create, update and delete objects */
	ConfigObject::OnActiveChanged.connect([](const ConfigObject::Ptr& object, const Value&) {
		IcingaDB::VersionChangedHandler(object);
	});
	ConfigObject::OnVersionChanged.connect([](const ConfigObject::Ptr& object, const Value&) {
		IcingaDB::VersionChangedHandler(object);
	});

	DependencyGroup::OnChildRegistered.connect(&IcingaDB::DependencyGroupChildRegisteredHandler);
	DependencyGroup::OnChildRemoved.connect(&IcingaDB::DependencyGroupChildRemovedHandler);

	/* downtime start */
	Downtime::OnDowntimeTriggered.connect(&IcingaDB::DowntimeStartedHandler);
	/* fixed/flexible downtime end or remove */
	Downtime::OnDowntimeRemoved.connect(&IcingaDB::DowntimeRemovedHandler);

	Checkable::OnNotificationSentToAllUsers.connect([](
		const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
		const NotificationType& type, const CheckResult::Ptr& cr, const String& author, const String& text,
		const MessageOrigin::Ptr&
	) {
		IcingaDB::NotificationSentToAllUsersHandler(notification, checkable, users, type, cr, author, text);
	});

	Comment::OnCommentAdded.connect(&IcingaDB::CommentAddedHandler);
	Comment::OnCommentRemoved.connect(&IcingaDB::CommentRemovedHandler);

	Checkable::OnFlappingChange.connect(&IcingaDB::FlappingChangeHandler);

	Checkable::OnNewCheckResult.connect([](const Checkable::Ptr& checkable, const CheckResult::Ptr&, const MessageOrigin::Ptr&) {
		IcingaDB::NewCheckResultHandler(checkable);
	});

	Checkable::OnNextCheckChanged.connect([](const Checkable::Ptr& checkable, const Value&) {
		IcingaDB::NextCheckChangedHandler(checkable);
	});

	Service::OnHostProblemChanged.connect([](const Service::Ptr& service, const CheckResult::Ptr&, const MessageOrigin::Ptr&) {
		IcingaDB::HostProblemChangedHandler(service);
	});

	Notification::OnUsersRawChangedWithOldValue.connect([](const Notification::Ptr& notification, const Value& oldValues, const Value& newValues) {
		IcingaDB::NotificationUsersChangedHandler(notification, oldValues, newValues);
	});
	Notification::OnUserGroupsRawChangedWithOldValue.connect([](const Notification::Ptr& notification, const Value& oldValues, const Value& newValues) {
		IcingaDB::NotificationUserGroupsChangedHandler(notification, oldValues, newValues);
	});
	TimePeriod::OnRangesChangedWithOldValue.connect([](const TimePeriod::Ptr& timeperiod, const Value& oldValues, const Value& newValues) {
		IcingaDB::TimePeriodRangesChangedHandler(timeperiod, oldValues, newValues);
	});
	TimePeriod::OnIncludesChangedWithOldValue.connect([](const TimePeriod::Ptr& timeperiod, const Value& oldValues, const Value& newValues) {
		IcingaDB::TimePeriodIncludesChangedHandler(timeperiod, oldValues, newValues);
	});
	TimePeriod::OnExcludesChangedWithOldValue.connect([](const TimePeriod::Ptr& timeperiod, const Value& oldValues, const Value& newValues) {
		IcingaDB::TimePeriodExcludesChangedHandler(timeperiod, oldValues, newValues);
	});
	User::OnGroupsChangedWithOldValue.connect([](const User::Ptr& user, const Value& oldValues, const Value& newValues) {
		IcingaDB::UserGroupsChangedHandler(user, oldValues, newValues);
	});
	Host::OnGroupsChangedWithOldValue.connect([](const Host::Ptr& host, const Value& oldValues, const Value& newValues) {
		IcingaDB::HostGroupsChangedHandler(host, oldValues, newValues);
	});
	Service::OnGroupsChangedWithOldValue.connect([](const Service::Ptr& service, const Value& oldValues, const Value& newValues) {
		IcingaDB::ServiceGroupsChangedHandler(service, oldValues, newValues);
	});
	Command::OnEnvChangedWithOldValue.connect([](const Command::Ptr& command, const Value& oldValues, const Value& newValues) {
		IcingaDB::CommandEnvChangedHandler(command, oldValues, newValues);
	});
	Command::OnArgumentsChangedWithOldValue.connect([](const Command::Ptr& command, const Value& oldValues, const Value& newValues) {
		IcingaDB::CommandArgumentsChangedHandler(command, oldValues, newValues);
	});
	CustomVarObject::OnVarsChangedWithOldValue.connect([](const ConfigObject::Ptr& object, const Value& oldValues, const Value& newValues) {
		IcingaDB::CustomVarsChangedHandler(object, oldValues, newValues);
	});
}

void IcingaDB::UpdateAllConfigObjects()
{
	// This function performs an initial dump of all configuration objects into Redis, thus there are no
	// previously enqueued queries on m_RconWorker that we need to wait for. So, no Sync() call is necessary here.
	m_RconWorker->FireAndForgetQuery({"XADD", "icinga:schema", "MAXLEN", "1", "*", "version", "6"}, {}, true);

	Log(LogInformation, "IcingaDB") << "Starting initial config/status dump";
	double startTime = Utility::GetTime();

	SetOngoingDumpStart(startTime);

	Defer resetOngoingDumpStart ([this]() {
		SetOngoingDumpStart(0);
	});

	// Use a Workqueue to pack objects in parallel
	WorkQueue upq(25000, Configuration::Concurrency, LogNotice);
	upq.SetName("IcingaDB:ConfigDump");

	// Add a new type=* state=wip entry to the stream and remove all previous entries (MAXLEN 1).
	m_RconWorker->FireAndForgetQuery({"XADD", "icinga:dump", "MAXLEN", "1", "*", "key", "*", "state", "wip"});

	const std::vector<RedisConnection::QueryArg> globalKeys = {
		CONFIG_REDIS_KEY_PREFIX "customvar",
		CONFIG_REDIS_KEY_PREFIX "action:url",
		CONFIG_REDIS_KEY_PREFIX "notes:url",
		CONFIG_REDIS_KEY_PREFIX "icon:image",

		// These keys aren't tied to a specific Checkable type but apply to both "Host" and "Service" types,
		// and as such we've to make sure to clear them before we actually start dumping the actual objects.
		// This allows us to wait on both types to be dumped before we send a config dump done signal for those keys.
		CONFIG_REDIS_KEY_PREFIX "dependency:node",
		CONFIG_REDIS_KEY_PREFIX "dependency:edge",
		CONFIG_REDIS_KEY_PREFIX "dependency:edge:state",
		CONFIG_REDIS_KEY_PREFIX "redundancygroup",
		CONFIG_REDIS_KEY_PREFIX "redundancygroup:state",
	};
	DeleteKeys(m_RconWorker, globalKeys);
	DeleteKeys(m_RconWorker, {"icinga:nextupdate:host", "icinga:nextupdate:service"});
	m_RconWorker->Sync();

	Defer resetDumpedGlobals ([this]() {
		m_DumpedGlobals.CustomVar.Reset();
		m_DumpedGlobals.ActionUrl.Reset();
		m_DumpedGlobals.NotesUrl.Reset();
		m_DumpedGlobals.IconImage.Reset();
		m_DumpedGlobals.DependencyGroup.Reset();
	});

	upq.ParallelFor(l_SyncableTypes, false, [this](const SyncableTypeInfo& info) {
		// No structured binding is allowed here till C++20 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85889).
		Type::Ptr type;
		QueryArgPair redisKeyPair;
		std::tie(type, redisKeyPair) = info;
		ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
		if (!ctype)
			return;

		auto& rcon (m_Rcons.at(ctype));

		DeleteKeys(rcon, GetTypeOverwriteKeys(type));

		WorkQueue upqObjectType(25000, Configuration::Concurrency, LogNotice);
		upqObjectType.SetName("IcingaDB:ConfigDump:" + type->GetName().ToLower());

		std::map<String, String> redisCheckSums;

		upqObjectType.Enqueue([&rcon, &redisKeyPair, &redisCheckSums]() {
			String cursor = "0";

			do {
				Array::Ptr res = rcon->GetResultOfQuery({"HSCAN", redisKeyPair.ChecksumKey, cursor, "COUNT", "1000"});

				AddKvsToMap(res->Get(1), redisCheckSums);

				cursor = res->Get(0);
			} while (cursor != "0");
		});

		auto objectChunks (ChunkObjects(ctype->GetObjects(), 500));

		// Skimmed away attributes and checksums HMSETs' keys and values by Redis key.
		std::map<RedisConnection::QueryArg, RedisConnection::Queries> ourContentRaw {{redisKeyPair.ChecksumKey, {}}, {redisKeyPair.ObjectKey, {}}};
		std::mutex ourContentMutex;

		upqObjectType.ParallelFor(objectChunks, [&](decltype(objectChunks)::const_reference chunk) {
			std::map<RedisConnection::QueryArg, RedisConnection::Query> hMSets;

			auto skimObjects ([&]() {
				std::lock_guard<std::mutex> l (ourContentMutex);

				for (auto& kv : ourContentRaw) {
					auto pos (hMSets.find(kv.first));

					if (pos != hMSets.end()) {
						kv.second.emplace_back(std::move(pos->second));
						hMSets.erase(pos);
					}
				}
			});

			auto [configStateKey, checksumStateKey] = GetCheckableStateKeys(type);

			size_t bulkCounter = 0;
			for (const ConfigObject::Ptr& object : chunk) {
				if (type != object->GetReflectionType())
					continue;

				// If we encounter not yet activated objects, i.e. they are currently being loaded and are about to
				// be activated, but are still partially initialised, we want to exclude them from the config dump
				// before we end up in a nullptr deference and crash the Icinga 2 process. Should these excluded
				// objects later reach the activation process, they will be captured via the `OnActiveChanged` event
				// and processed in IcingaDB::VersionChangedHandler() as runtime updates.
				if (!object->IsActive()) {
					continue;
				}

				std::vector<Dictionary::Ptr> runtimeUpdates;
				CreateConfigUpdate(object, redisKeyPair, hMSets, runtimeUpdates, false);

				// Write out inital state for checkables
				if (!static_cast<std::string_view>(configStateKey).empty()) {
					String objectKey = GetObjectIdentifier(object);
					Dictionary::Ptr state = SerializeState(dynamic_pointer_cast<Checkable>(object));

					auto& states = hMSets[configStateKey];
					states.emplace_back(objectKey);
					states.emplace_back(JsonEncode(state));

					auto& statesChksms = hMSets[checksumStateKey];
					statesChksms.emplace_back(objectKey);
					statesChksms.emplace_back(JsonEncode(new Dictionary({{"checksum", HashValue(state)}})));
				}

				bulkCounter++;
				if (!(bulkCounter % 100)) {
					skimObjects();

					ExecuteRedisTransaction(rcon, hMSets, {});

					hMSets = decltype(hMSets)();
				}

				auto checkable (dynamic_pointer_cast<Checkable>(object));

				if (checkable && checkable->GetEnableActiveChecks()) {
					EnqueueConfigObject(checkable, icingadb::task_queue::NextUpdate);
				}
			}

			skimObjects();

			ExecuteRedisTransaction(rcon, hMSets, {});

			Log(LogNotice, "IcingaDB")
				<< "Dumped " << bulkCounter << " objects of " << type->ToString();
		});

		upqObjectType.Join();

		if (upqObjectType.HasExceptions()) {
			for (boost::exception_ptr exc : upqObjectType.GetExceptions()) {
				if (exc) {
					boost::rethrow_exception(exc);
				}
			}
		}

		std::map<RedisConnection::QueryArg, std::map<String, String>> ourContent;

		for (auto& source : ourContentRaw) {
			auto& dest (ourContent[source.first]);

			upqObjectType.Enqueue([&]() {
				for (auto& hMSet : source.second) {
					for (decltype(hMSet.size()) i = 0, stop = hMSet.size() - 1u; i < stop; i += 2u) {
						dest.emplace(std::move(hMSet[i]), std::move(hMSet[i + 1u]));
					}

					hMSet.clear();
				}

				source.second.clear();
			});
		}

		upqObjectType.Join();
		ourContentRaw.clear();

		auto& ourCheckSums (ourContent[redisKeyPair.ChecksumKey]);
		auto& ourObjects (ourContent[redisKeyPair.ObjectKey]);
		RedisConnection::Query setChecksum, setObject, delChecksum, delObject;

		auto redisCurrent (redisCheckSums.begin());
		auto redisEnd (redisCheckSums.end());
		auto ourCurrent (ourCheckSums.begin());
		auto ourEnd (ourCheckSums.end());

		auto flushSets ([&]() {
			auto affectedConfig (setObject.size() / 2u);

			setChecksum.insert(setChecksum.begin(), {"HMSET", redisKeyPair.ChecksumKey});
			setObject.insert(setObject.begin(), {"HMSET", redisKeyPair.ObjectKey});

			RedisConnection::Queries transaction;

			transaction.emplace_back(RedisConnection::Query{"MULTI"});
			transaction.emplace_back(std::move(setChecksum));
			transaction.emplace_back(std::move(setObject));
			transaction.emplace_back(RedisConnection::Query{"EXEC"});

			setChecksum.clear();
			setObject.clear();

			rcon->FireAndForgetQueries(std::move(transaction), {affectedConfig});
		});

		auto flushDels ([&]() {
			auto affectedConfig (delObject.size());

			delChecksum.insert(delChecksum.begin(), {"HDEL", redisKeyPair.ChecksumKey});
			delObject.insert(delObject.begin(), {"HDEL", redisKeyPair.ObjectKey});

			RedisConnection::Queries transaction;

			transaction.emplace_back(RedisConnection::Query{"MULTI"});
			transaction.emplace_back(std::move(delChecksum));
			transaction.emplace_back(std::move(delObject));
			transaction.emplace_back(RedisConnection::Query{"EXEC"});

			delChecksum.clear();
			delObject.clear();

			rcon->FireAndForgetQueries(std::move(transaction), {affectedConfig});
		});

		auto setOne ([&]() {
			setChecksum.emplace_back(ourCurrent->first);
			setChecksum.emplace_back(ourCurrent->second);
			setObject.emplace_back(ourCurrent->first);
			setObject.emplace_back(ourObjects[ourCurrent->first]);

			if (setChecksum.size() == 100u) {
				flushSets();
			}
		});

		auto delOne ([&]() {
			delChecksum.emplace_back(redisCurrent->first);
			delObject.emplace_back(redisCurrent->first);

			if (delChecksum.size() == 100u) {
				flushDels();
			}
		});

		for (;;) {
			if (redisCurrent == redisEnd) {
				for (; ourCurrent != ourEnd; ++ourCurrent) {
					setOne();
				}

				break;
			} else if (ourCurrent == ourEnd) {
				for (; redisCurrent != redisEnd; ++redisCurrent) {
					delOne();
				}

				break;
			} else if (redisCurrent->first < ourCurrent->first) {
				delOne();
				++redisCurrent;
			} else if (redisCurrent->first > ourCurrent->first) {
				setOne();
				++ourCurrent;
			} else {
				if (redisCurrent->second != ourCurrent->second) {
					setOne();
				}

				++redisCurrent;
				++ourCurrent;
			}
		}

		if (delChecksum.size()) {
			flushDels();
		}

		if (setChecksum.size()) {
			flushSets();
		}

		auto keys = GetTypeOverwriteKeys(type, true);
		keys.emplace_back(redisKeyPair.ObjectKey);
		for (auto& key : keys) {
			rcon->FireAndForgetQuery({"XADD", "icinga:dump", "*", "key", key, "state", "done"});
		}
		rcon->Sync();
	});

	upq.Join();

	if (upq.HasExceptions()) {
		for (boost::exception_ptr exc : upq.GetExceptions()) {
			try {
				if (exc) {
					boost::rethrow_exception(exc);
			}
			} catch(const std::exception& e) {
				Log(LogCritical, "IcingaDB")
						<< "Exception during ConfigDump: " << e.what();
			}
		}
	}

	for (auto& key : globalKeys) {
		m_RconWorker->FireAndForgetQuery({"XADD", "icinga:dump", "*", "key", key, "state", "done"});
	}

	m_RconWorker->FireAndForgetQuery({"XADD", "icinga:dump", "*", "key", "*", "state", "done"});

	// enqueue a callback that will notify us once all previous queries were executed and wait for this event
	std::promise<void> p;
	m_RconWorker->EnqueueCallback([&p](boost::asio::yield_context&) { p.set_value(); });
	p.get_future().wait();

	auto endTime (Utility::GetTime());
	auto took (endTime - startTime);

	SetLastdumpTook(took);
	SetLastdumpEnd(endTime);

	Log(LogInformation, "IcingaDB")
		<< "Initial config/status dump finished in " << took << " seconds.";
}

std::vector<std::vector<intrusive_ptr<ConfigObject>>> IcingaDB::ChunkObjects(std::vector<intrusive_ptr<ConfigObject>> objects, size_t chunkSize) {
	std::vector<std::vector<intrusive_ptr<ConfigObject>>> chunks;
	auto offset (objects.begin());
	auto end (objects.end());

	chunks.reserve((std::distance(offset, end) + chunkSize - 1) / chunkSize);

	while (static_cast<std::size_t>(std::distance(offset, end)) >= chunkSize) {
		auto until (offset + chunkSize);
		chunks.emplace_back(offset, until);
		offset = until;
	}

	if (offset != end) {
		chunks.emplace_back(offset, end);
	}

	return chunks;
}

void IcingaDB::DeleteKeys(const RedisConnection::Ptr& conn, const std::vector<RedisConnection::QueryArg>& keys) {
	RedisConnection::Query query = {"DEL"};
	for (auto& key : keys) {
		query.emplace_back(key);
	}

	conn->FireAndForgetQuery(std::move(query));
}

/**
 * Gets the Redis key for the custom variables of a given type.
 *
 * @param type The type for which to get the custom variable Redis key.
 * @return The corresponding customvar Redis key for the given type, or an empty string if the type does not have one.
 */
static RedisConnection::QueryArg GetRedisCustomVarKey(const Type::Ptr& type)
{
	auto it = std::find_if(std::begin(l_CustomVarKeys), std::end(l_CustomVarKeys), [&type](const auto& pair) {
		return pair.first == type;
	});
	if (it != std::end(l_CustomVarKeys)) {
		return it->second;
	}
	return {""};
}

std::vector<RedisConnection::QueryArg> IcingaDB::GetTypeOverwriteKeys(const Type::Ptr& type, bool skipChecksums)
{
	auto customvarKey = GetRedisCustomVarKey(type);
	if (static_cast<std::string_view>(customvarKey).empty()) {
		return {};
	}

	std::vector keys{customvarKey};
	if (Host::TypeInstance == type) {
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "hostgroup:member");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "host:state");
		if (!skipChecksums) {
			keys.emplace_back(CHECKSUM_REDIS_KEY_PREFIX "host:state");
		}
	} else if (Service::TypeInstance == type) {
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "servicegroup:member");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "service:state");
		if (!skipChecksums) {
			keys.emplace_back(CHECKSUM_REDIS_KEY_PREFIX "service:state");
		}
	} else if (User::TypeInstance == type) {
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "usergroup:member");
	} else if (TimePeriod::TypeInstance == type) {
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "timeperiod:override:include");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "timeperiod:override:exclude");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "timeperiod:range");
	} else if (Notification::TypeInstance == type) {
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "notification:user");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "notification:usergroup");
		keys.emplace_back(CONFIG_REDIS_KEY_PREFIX "notification:recipient");
	} else if (Command::TypeInstance->IsAssignableFrom(type)) {
		const auto& cmdRedisKeys = GetCmdEnvArgKeys(type);
		keys.emplace_back(cmdRedisKeys.ArgObjectKey);
		keys.emplace_back(cmdRedisKeys.EnvObjectKey);
		if (!skipChecksums) {
			keys.emplace_back(cmdRedisKeys.ArgChecksumKey);
			keys.emplace_back(cmdRedisKeys.EnvChecksumKey);
		}
	}

	return keys;
}

template<typename ConfigType>
static ConfigObject::Ptr GetObjectByName(const String& name)
{
	return ConfigObject::GetObject<ConfigType>(name);
}

void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object,
	std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets, std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate)
{
	auto typeName = GetLowerCaseTypeNameDB(object);
	String objectKey = GetObjectIdentifier(object);
	String objectKeyName = typeName + "_id";

	Type::Ptr type = object->GetReflectionType();

	CustomVarObject::Ptr customVarObject = dynamic_pointer_cast<CustomVarObject>(object);

	if (customVarObject) {
		auto vars(SerializeVars(customVarObject->GetVars()));
		if (vars) {
			auto typeCvsKey = GetRedisCustomVarKey(type);
			auto& typeCvs (hMSets[typeCvsKey]);
			auto& allCvs (hMSets[CONFIG_REDIS_KEY_PREFIX "customvar"]);

			ObjectLock varsLock(vars);
			Array::Ptr varsArray(new Array);

			varsArray->Reserve(vars->GetLength());

			for (auto& kv : vars) {
				if (runtimeUpdate || m_DumpedGlobals.CustomVar.IsNew(kv.first)) {
					allCvs.emplace_back(kv.first);
					allCvs.emplace_back(JsonEncode(kv.second));

					if (runtimeUpdate) {
						AddObjectDataToRuntimeUpdates(runtimeUpdates, kv.first, CONFIG_REDIS_KEY_PREFIX "customvar", kv.second);
					}
				}

				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}});
				typeCvs.emplace_back(JsonEncode(data));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, std::string(typeCvsKey), data);
				}
			}
		}
	}

	if (type == Host::TypeInstance || type == Service::TypeInstance) {
		Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);

		String actionUrl = checkable->GetActionUrl();
		String notesUrl = checkable->GetNotesUrl();
		String iconImage = checkable->GetIconImage();
		if (!actionUrl.IsEmpty()) {
			auto& actionUrls (hMSets[CONFIG_REDIS_KEY_PREFIX "action:url"]);

			auto id (HashValue(new Array({m_EnvironmentId, actionUrl})));

			if (runtimeUpdate || m_DumpedGlobals.ActionUrl.IsNew(id)) {
				actionUrls.emplace_back(std::move(id));
				Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"action_url", actionUrl}});
				actionUrls.emplace_back(JsonEncode(data));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, static_cast<String>(actionUrls.at(actionUrls.size() - 2u)), CONFIG_REDIS_KEY_PREFIX "action:url", data);
				}
			}
		}
		if (!notesUrl.IsEmpty()) {
			auto& notesUrls (hMSets[CONFIG_REDIS_KEY_PREFIX "notes:url"]);

			auto id (HashValue(new Array({m_EnvironmentId, notesUrl})));

			if (runtimeUpdate || m_DumpedGlobals.NotesUrl.IsNew(id)) {
				notesUrls.emplace_back(std::move(id));
				Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"notes_url", notesUrl}});
				notesUrls.emplace_back(JsonEncode(data));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, static_cast<String>(notesUrls.at(notesUrls.size() - 2u)), CONFIG_REDIS_KEY_PREFIX "notes:url", data);
				}
			}
		}
		if (!iconImage.IsEmpty()) {
			auto& iconImages (hMSets[CONFIG_REDIS_KEY_PREFIX "icon:image"]);

			auto id (HashValue(new Array({m_EnvironmentId, iconImage})));

			if (runtimeUpdate || m_DumpedGlobals.IconImage.IsNew(id)) {
				iconImages.emplace_back(std::move(id));
				Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"icon_image", iconImage}});
				iconImages.emplace_back(JsonEncode(data));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, static_cast<String>(iconImages.at(iconImages.size() - 2u)), CONFIG_REDIS_KEY_PREFIX "icon:image", data);
				}
			}
		}

		Host::Ptr host;
		Service::Ptr service;
		tie(host, service) = GetHostService(checkable);

		ConfigObject::Ptr (*getGroup)(const String& name);
		Array::Ptr groups;
		if (service) {
			groups = service->GetGroups();
			getGroup = &::GetObjectByName<ServiceGroup>;
		} else {
			groups = host->GetGroups();
			getGroup = &::GetObjectByName<HostGroup>;
		}

		if (groups) {
			ObjectLock groupsLock(groups);
			Array::Ptr groupIds(new Array);

			groupIds->Reserve(groups->GetLength());

			std::string_view groupMemberKey;
			if (service) {
				groupMemberKey = CONFIG_REDIS_KEY_PREFIX "servicegroup:member";
			} else {
				groupMemberKey = CONFIG_REDIS_KEY_PREFIX "hostgroup:member";
			}
			auto& members (hMSets[groupMemberKey]);

			for (auto& group : groups) {
				auto groupObj ((*getGroup)(group));
				String groupId = GetObjectIdentifier(groupObj);
				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));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, std::string(groupMemberKey), data);
				}

				groupIds->Add(groupId);
			}
		}

		InsertCheckableDependencies(checkable, hMSets, runtimeUpdate ? &runtimeUpdates : nullptr);

		return;
	}

	if (type == TimePeriod::TypeInstance) {
		TimePeriod::Ptr timeperiod = static_pointer_cast<TimePeriod>(object);

		Dictionary::Ptr ranges = timeperiod->GetRanges();
		if (ranges) {
			ObjectLock rangesLock(ranges);
			Array::Ptr rangeIds(new Array);
			auto& typeRanges (hMSets[CONFIG_REDIS_KEY_PREFIX "timeperiod:range"]);

			rangeIds->Reserve(ranges->GetLength());

			for (auto& kv : ranges) {
				String rangeId = HashValue(new Array({m_EnvironmentId, kv.first, kv.second}));
				rangeIds->Add(rangeId);

				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));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "timeperiod:range", data);
				}
			}
		}

		Array::Ptr includes;
		ConfigObject::Ptr (*getInclude)(const String& name);
		includes = timeperiod->GetIncludes();
		getInclude = &::GetObjectByName<TimePeriod>;

		Array::Ptr includeChecksums = new Array();

		ObjectLock includesLock(includes);
		ObjectLock includeChecksumsLock(includeChecksums);

		includeChecksums->Reserve(includes->GetLength());


		auto& includs (hMSets[CONFIG_REDIS_KEY_PREFIX "timeperiod:override:include"]);
		for (auto include : includes) {
			auto includeTp ((*getInclude)(include.Get<String>()));
			String includeId = GetObjectIdentifier(includeTp);
			includeChecksums->Add(includeId);

			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));

			if (runtimeUpdate) {
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "timeperiod:override:include", data);
			}
		}

		Array::Ptr excludes;
		ConfigObject::Ptr (*getExclude)(const String& name);

		excludes = timeperiod->GetExcludes();
		getExclude = &::GetObjectByName<TimePeriod>;

		Array::Ptr excludeChecksums = new Array();

		ObjectLock excludesLock(excludes);
		ObjectLock excludeChecksumsLock(excludeChecksums);

		excludeChecksums->Reserve(excludes->GetLength());

		auto& excluds (hMSets[CONFIG_REDIS_KEY_PREFIX "timeperiod:override:exclude"]);

		for (auto exclude : excludes) {
			auto excludeTp ((*getExclude)(exclude.Get<String>()));
			String excludeId = GetObjectIdentifier(excludeTp);
			excludeChecksums->Add(excludeId);

			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));

			if (runtimeUpdate) {
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "timeperiod:override:exclude", data);
			}
		}

		return;
	}

	if (type == User::TypeInstance) {
		User::Ptr user = static_pointer_cast<User>(object);
		Array::Ptr groups = user->GetGroups();

		if (groups) {
			ObjectLock groupsLock(groups);
			Array::Ptr groupIds(new Array);

			groupIds->Reserve(groups->GetLength());

			auto& members (hMSets[CONFIG_REDIS_KEY_PREFIX "usergroup:member"]);
			auto& notificationRecipients (hMSets[CONFIG_REDIS_KEY_PREFIX "notification:recipient"]);

			for (auto& group : groups) {
				UserGroup::Ptr groupObj = UserGroup::GetByName(group);
				String groupId = GetObjectIdentifier(groupObj);
				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));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "usergroup:member", data);

					// Recipients are handled by notifications during initial dumps and only need to be handled here during runtime (e.g. User creation).
					for (auto& notification : groupObj->GetNotifications()) {
						String recipientId = HashValue(new Array({m_EnvironmentId, "usergroupuser", user->GetName(), groupObj->GetName(), notification->GetName()}));
						notificationRecipients.emplace_back(recipientId);
						Dictionary::Ptr recipientData = new Dictionary({{"notification_id", GetObjectIdentifier(notification)}, {"environment_id", m_EnvironmentId}, {"user_id", objectKey}, {"usergroup_id", groupId}});
						notificationRecipients.emplace_back(JsonEncode(recipientData));

						AddObjectDataToRuntimeUpdates(runtimeUpdates, recipientId, CONFIG_REDIS_KEY_PREFIX "notification:recipient", recipientData);
					}
				}

				groupIds->Add(groupId);
			}
		}

		return;
	}

	if (type == Notification::TypeInstance) {
		Notification::Ptr notification = static_pointer_cast<Notification>(object);

		std::set<User::Ptr> users = notification->GetUsers();
		Array::Ptr userIds = new Array();

		auto usergroups(notification->GetUserGroups());
		Array::Ptr usergroupIds = new Array();

		userIds->Reserve(users.size());

		auto& usrs (hMSets[CONFIG_REDIS_KEY_PREFIX "notification:user"]);
		auto& notificationRecipients (hMSets[CONFIG_REDIS_KEY_PREFIX "notification:recipient"]);

		for (auto& user : users) {
			String userId = GetObjectIdentifier(user);
			String id = HashValue(new Array({m_EnvironmentId, "user", user->GetName(), object->GetName()}));
			usrs.emplace_back(id);
			notificationRecipients.emplace_back(id);

			Dictionary::Ptr data = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"user_id", userId}});
			String dataJson = JsonEncode(data);
			usrs.emplace_back(dataJson);
			notificationRecipients.emplace_back(dataJson);

			if (runtimeUpdate) {
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "notification:user", data);
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "notification:recipient", data);
			}

			userIds->Add(userId);
		}

		usergroupIds->Reserve(usergroups.size());

		auto& groups (hMSets[CONFIG_REDIS_KEY_PREFIX "notification:usergroup"]);

		for (auto& usergroup : usergroups) {
			String usergroupId = GetObjectIdentifier(usergroup);
			String id = HashValue(new Array({m_EnvironmentId, "usergroup", usergroup->GetName(), object->GetName()}));
			groups.emplace_back(id);
			notificationRecipients.emplace_back(id);

			Dictionary::Ptr groupData = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"usergroup_id", usergroupId}});
			String groupDataJson = JsonEncode(groupData);
			groups.emplace_back(groupDataJson);
			notificationRecipients.emplace_back(groupDataJson);

			if (runtimeUpdate) {
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "notification:usergroup", groupData);
				AddObjectDataToRuntimeUpdates(runtimeUpdates, id, CONFIG_REDIS_KEY_PREFIX "notification:recipient", groupData);
			}

			for (const User::Ptr& user : usergroup->GetMembers()) {
				String userId = GetObjectIdentifier(user);
				String recipientId = HashValue(new Array({m_EnvironmentId, "usergroupuser", user->GetName(), usergroup->GetName(), notification->GetName()}));
				notificationRecipients.emplace_back(recipientId);
				Dictionary::Ptr userData = new Dictionary({{"notification_id", objectKey}, {"environment_id", m_EnvironmentId}, {"user_id", userId}, {"usergroup_id", usergroupId}});
				notificationRecipients.emplace_back(JsonEncode(userData));

				if (runtimeUpdate) {
					AddObjectDataToRuntimeUpdates(runtimeUpdates, recipientId, CONFIG_REDIS_KEY_PREFIX "notification:recipient", userData);
				}
			}

			usergroupIds->Add(usergroupId);
		}

		return;
	}

	if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance) {
		Command::Ptr command = static_pointer_cast<Command>(object);

		const auto& cmdRedisKeys = GetCmdEnvArgKeys(type);

		Dictionary::Ptr arguments = command->GetArguments();
		if (arguments) {
			ObjectLock argumentsLock(arguments);
			auto& typeArgs (hMSets[cmdRedisKeys.ArgObjectKey]);
			auto& argChksms (hMSets[cmdRedisKeys.ArgChecksumKey]);

			for (auto& kv : arguments) {
				Dictionary::Ptr values;
				if (kv.second.IsObjectType<Dictionary>()) {
					values = kv.second;
					values = values->ShallowClone();
				} else if (kv.second.IsObjectType<Array>()) {
					values = new Dictionary({{"value", JsonEncode(kv.second)}});
				} else {
					values = new Dictionary({{"value", kv.second}});
				}

				for (const char *attr : {"value", "set_if", "separator"}) {
					Value value;

					// Stringify if set.
					if (values->Get(attr, &value)) {
						switch (value.GetType()) {
							case ValueEmpty:
							case ValueString:
								break;
							case ValueObject:
								values->Set(attr, value.Get<Object::Ptr>()->ToString());
								break;
							default:
								values->Set(attr, JsonEncode(value));
						}
					}
				}

				for (const char *attr : {"repeat_key", "required", "skip_key"}) {
					Value value;

					// Boolify if set.
					if (values->Get(attr, &value)) {
						values->Set(attr, value.ToBool());
					}
				}

				{
					Value order;

					// Intify if set.
					if (values->Get("order", &order)) {
						values->Set("order", (int)order);
					}
				}

				values->Set(objectKeyName, objectKey);
				values->Set("argument_key", kv.first);
				values->Set("environment_id", m_EnvironmentId);

				String id = HashValue(new Array({m_EnvironmentId, kv.first, object->GetName()}));

				typeArgs.emplace_back(id);
				typeArgs.emplace_back(JsonEncode(values));

				argChksms.emplace_back(id);
				String checksum = HashValue(kv.second);
				argChksms.emplace_back(JsonEncode(new Dictionary({{"checksum", checksum}})));

				if (runtimeUpdate) {
					values->Set("checksum", checksum);
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, std::string(cmdRedisKeys.ArgObjectKey), values);
				}
			}
		}

		Dictionary::Ptr envvars = command->GetEnv();
		if (envvars) {
			ObjectLock envvarsLock(envvars);
			Array::Ptr envvarIds(new Array);
			auto& typeVars (hMSets[cmdRedisKeys.EnvObjectKey]);
			auto& varChksms (hMSets[cmdRedisKeys.EnvChecksumKey]);

			envvarIds->Reserve(envvars->GetLength());

			for (auto& kv : envvars) {
				Dictionary::Ptr values;
				if (kv.second.IsObjectType<Dictionary>()) {
					values = kv.second;
					values = values->ShallowClone();
				} else if (kv.second.IsObjectType<Array>()) {
					values = new Dictionary({{"value", JsonEncode(kv.second)}});
				} else {
					values = new Dictionary({{"value", kv.second}});
				}

				{
					Value value;

					// JsonEncode() the value if it's set.
					if (values->Get("value", &value)) {
						values->Set("value", JsonEncode(value));
					}
				}

				values->Set(objectKeyName, objectKey);
				values->Set("envvar_key", kv.first);
				values->Set("environment_id", m_EnvironmentId);

				String id = HashValue(new Array({m_EnvironmentId, kv.first, object->GetName()}));

				typeVars.emplace_back(id);
				typeVars.emplace_back(JsonEncode(values));

				varChksms.emplace_back(id);
				String checksum = HashValue(kv.second);
				varChksms.emplace_back(JsonEncode(new Dictionary({{"checksum", checksum}})));

				if (runtimeUpdate) {
					values->Set("checksum", checksum);
					AddObjectDataToRuntimeUpdates(runtimeUpdates, id, std::string(cmdRedisKeys.EnvObjectKey), values);
				}
			}
		}

		return;
	}
}

/**
 * Inserts the dependency data for a Checkable object into the given Redis HMSETs and runtime updates.
 *
 * This function is responsible for serializing the in memory representation Checkable dependencies into
 * Redis HMSETs and runtime updates (if any) according to the Icinga DB schema. The serialized data consists
 * of the following Redis HMSETs:
 * - dependency:node: Contains dependency node data representing each host, service, and redundancy group
 *   in the dependency graph.
 * - dependency:edge: Dependency edge information representing all connections between the nodes.
 * - redundancygroup: Redundancy group data representing all redundancy groups in the graph.
 * - redundancygroup:state: State information for redundancy groups.
 * - dependency:edge:state: State information for (each) dependency edge. Multiple edges may share the
 *	 same state.
 *
 * If the `onlyDependencyGroup` parameter is set, only dependencies from this group are processed. This is useful
 * when only a specific dependency group should be processed, e.g. during runtime updates. For initial config dumps,
 * it shouldn't be necessary to set the `runtimeUpdates` and `onlyDependencyGroup` parameters.
 *
 * @param checkable The checkable object to extract dependencies from.
 * @param hMSets The map of Redis HMSETs to insert the dependency data into.
 * @param runtimeUpdates If set, runtime updates are additionally added to this vector.
 * @param onlyDependencyGroup If set, only process dependency objects from this group.
 */
void IcingaDB::InsertCheckableDependencies(
	const Checkable::Ptr& checkable,
	std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets,
	std::vector<Dictionary::Ptr>* runtimeUpdates,
	const DependencyGroup::Ptr& onlyDependencyGroup
)
{
	// Only generate a dependency node event if the Checkable is actually part of some dependency graph.
	// That's, it either depends on other Checkables or others depend on it, and in both cases, we have
	// to at least generate a dependency node entry for it.
	if (!checkable->HasAnyDependencies()) {
		return;
	}

	// First and foremost, generate a dependency node entry for the provided Checkable object and insert it into
	// the HMSETs map and if set, the `runtimeUpdates` vector.
	auto [host, service] = GetHostService(checkable);
	auto checkableId(GetObjectIdentifier(checkable));
	{
		Dictionary::Ptr data(new Dictionary{{"environment_id", m_EnvironmentId}, {"host_id", GetObjectIdentifier(host)}});
		if (service) {
			data->Set("service_id", checkableId);
		}

		AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:node", checkableId, data);
		if (runtimeUpdates) {
			AddObjectDataToRuntimeUpdates(*runtimeUpdates, checkableId, CONFIG_REDIS_KEY_PREFIX "dependency:node", data);
		}
	}

	// If `onlyDependencyGroup` is provided, process the dependencies only from that group; otherwise,
	// retrieve all the dependency groups that the Checkable object is part of.
	std::vector<DependencyGroup::Ptr> dependencyGroups{onlyDependencyGroup};
	if (!onlyDependencyGroup) {
		dependencyGroups = checkable->GetDependencyGroups();
	}

	for (auto& dependencyGroup : dependencyGroups) {
		String edgeFromNodeId(checkableId);
		bool syncSharedEdgeState(false);

		if (!dependencyGroup->IsRedundancyGroup()) {
			// Non-redundant dependency groups are just placeholders and never get synced to Redis, thus just figure
			// out whether we have to sync the shared edge state. For runtime updates the states are sent via the
			// UpdateDependenciesState() method, thus we don't have to sync them here.
			syncSharedEdgeState = !runtimeUpdates && m_DumpedGlobals.DependencyGroup.IsNew(dependencyGroup->GetCompositeKey());
		} else {
			auto redundancyGroupId(HashValue(new Array{m_EnvironmentId, dependencyGroup->GetCompositeKey()}));
			dependencyGroup->SetIcingaDBIdentifier(redundancyGroupId);

			edgeFromNodeId = redundancyGroupId;

			// During the initial config sync, multiple children can depend on the same redundancy group, sync it only
			// the first time it is encountered. Though, if this is a runtime update, we have to re-serialize and sync
			// the redundancy group unconditionally, as we don't know whether it was already synced or the context that
			// triggered this update.
			if (runtimeUpdates || m_DumpedGlobals.DependencyGroup.IsNew(redundancyGroupId)) {
				Dictionary::Ptr groupData(new Dictionary{
					{"environment_id", m_EnvironmentId},
					{"display_name", dependencyGroup->GetRedundancyGroupName()},
				});
				// Set/refresh the redundancy group data in the Redis HMSETs (redundancy_group database table).
				AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "redundancygroup", redundancyGroupId, groupData);

				Dictionary::Ptr nodeData(new Dictionary{
					{"environment_id", m_EnvironmentId},
					{"redundancy_group_id", redundancyGroupId},
				});
				// Obviously, the redundancy group is part of some dependency chain, thus we have to generate
				// dependency node entry for it as well.
				AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:node", redundancyGroupId, nodeData);

				if (runtimeUpdates) {
					// Send the same data sent to the Redis HMSETs to the runtime updates stream as well.
					AddObjectDataToRuntimeUpdates(*runtimeUpdates, redundancyGroupId, CONFIG_REDIS_KEY_PREFIX "redundancygroup", groupData);
					AddObjectDataToRuntimeUpdates(*runtimeUpdates, redundancyGroupId, CONFIG_REDIS_KEY_PREFIX "dependency:node", nodeData);
				} else {
					syncSharedEdgeState = true;

					// Serialize and sync the redundancy group state information a) to the RedundancyGroupState and b)
					// to the DependencyEdgeState HMSETs. The latter is shared by all child Checkables of the current
					// redundancy group, and since they all depend on the redundancy group, the state of that group is
					// basically the state of the dependency edges between the children and the redundancy group.
					auto stateAttrs(SerializeRedundancyGroupState(checkable, dependencyGroup));
					AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "redundancygroup:state", redundancyGroupId, stateAttrs);
					AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", redundancyGroupId, Dictionary::Ptr(new Dictionary{
						{"id", redundancyGroupId},
						{"environment_id", m_EnvironmentId},
						{"failed", stateAttrs->Get("failed")},
					}));
				}
			}

			Dictionary::Ptr data(new Dictionary{
				{"environment_id", m_EnvironmentId},
				{"from_node_id", checkableId},
				{"to_node_id", redundancyGroupId},
				// All redundancy group members share the same state, thus use the group ID as a reference.
				{"dependency_edge_state_id", redundancyGroupId},
				{"display_name", dependencyGroup->GetRedundancyGroupName()},
			});

			// Generate a dependency edge entry representing the connection between the Checkable and the redundancy
			// group. This Checkable dependes on the redundancy group (is a child of it), thus the "dependency_edge_state_id"
			// is set to the redundancy group ID. Note that if this group has multiple children, they all will have the
			// same "dependency_edge_state_id" value.
			auto edgeId(HashValue(new Array{checkableId, redundancyGroupId}));
			AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge", edgeId, data);

			if (runtimeUpdates) {
				AddObjectDataToRuntimeUpdates(*runtimeUpdates, edgeId, CONFIG_REDIS_KEY_PREFIX "dependency:edge", data);
			}
		}

		auto dependencies(dependencyGroup->GetDependenciesForChild(checkable.get()));
		// Sort the dependencies by their parent Checkable object to ensure that all dependency objects that share the
		// same parent Checkable are placed next to each other in the container. See the while loop below for more info!
		std::sort(dependencies.begin(), dependencies.end(), [](const Dependency::Ptr& lhs, const Dependency::Ptr& rhs) {
			return lhs->GetParent() < rhs->GetParent();
		});

		// Traverse through each dependency objects within the current dependency group the provided Checkable depend
		// on and generate a dependency edge entry. The generated dependency edge "from_node_id" may vary depending on
		// whether the dependency group is a redundancy group or not. If it's a redundancy group, the "from_node_id"
		// will be the redundancy group ID; otherwise, it will be the current Checkable ID. However, the "to_node_id"
		// value will always be the parent Checkable ID of the dependency object.
		for (auto it(dependencies.begin()); it != dependencies.end(); /* no increment */) {
			auto dependency(*it);
			auto parent(dependency->GetParent());
			auto displayName(dependency->GetShortName());

			Dictionary::Ptr edgeStateAttrs(SerializeDependencyEdgeState(dependencyGroup, dependency));

			// In case there are multiple Dependency objects with the same parent, these are merged into a single edge
			// to prevent duplicate edges in the resulting graph. All objects with the same parent were placed next to
			// each other by the sort function above.
			//
			// Additionally, the iterator for the surrounding loop is incremented by this loop: after it has finished,
			// "it" will either point to the next dependency with a different parent or to the end of the container.
			while (++it != dependencies.end() && (*it)->GetParent() == parent) {
				displayName += ", " + (*it)->GetShortName();
				if (syncSharedEdgeState && edgeStateAttrs->Get("failed") == false) {
					edgeStateAttrs = SerializeDependencyEdgeState(dependencyGroup, *it);
				}
			}

			Dictionary::Ptr data(new Dictionary{
				{"environment_id", m_EnvironmentId},
				{"from_node_id", edgeFromNodeId},
				{"to_node_id", GetObjectIdentifier(parent)},
				{"dependency_edge_state_id", edgeStateAttrs->Get("id")},
				{"display_name", std::move(displayName)},
			});

			auto edgeId(HashValue(new Array{data->Get("from_node_id"), data->Get("to_node_id")}));
			AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge", edgeId, data);

			if (runtimeUpdates) {
				AddObjectDataToRuntimeUpdates(*runtimeUpdates, edgeId, CONFIG_REDIS_KEY_PREFIX "dependency:edge", data);
			} else if (syncSharedEdgeState) {
				AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", edgeStateAttrs->Get("id"), edgeStateAttrs);
			}
		}
	}
}

/**
 * Update the state information of a checkable in Redis.
 *
 * What is updated exactly depends on the mode parameter:
 *  - VolatileState: Update the volatile state information stored in icinga:host:state or icinga:service:state as well
 *    as the corresponding checksum stored in icinga:checksum:host:state or icinga:checksum:service:state.
 *  - RuntimeState: Write a runtime update to the icinga:runtime:state stream. It is up to the caller to ensure that
 *    identical volatile state information was already written before to avoid inconsistencies. This mode is only
 *    useful to upgrade a previous Volatile to a Full operation, otherwise Full should be used.
 *  - FullState: Perform an update of all state information in Redis, that is updating the volatile information and
 *    sending a corresponding runtime update so that this state update gets written through to the persistent database
 *    by a running icingadb process.
 *
 * @param checkable State of this checkable is updated in Redis
 * @param mode Mode of operation (DirtyBits:VolatileState, DirtyBits::RuntimeState, or DirtyBits::FullState)
 */
void IcingaDB::UpdateState(const Checkable::Ptr& checkable, uint32_t mode)
{
	Dictionary::Ptr stateAttrs = SerializeState(checkable);

	String checksum = HashValue(stateAttrs);

	auto [redisStateKey, redisChecksumKey] = GetCheckableStateKeys(checkable->GetReflectionType());
	if (mode & icingadb::task_queue::VolatileState) {
		String objectKey = GetObjectIdentifier(checkable);
		m_RconWorker->FireAndForgetQueries({
			{"HSET", redisStateKey, objectKey, JsonEncode(stateAttrs)},
			{"HSET", redisChecksumKey, objectKey, JsonEncode(new Dictionary({{"checksum", checksum}}))},
		});
	}

	if (mode & icingadb::task_queue::RuntimeState) {
		ObjectLock olock(stateAttrs);

		RedisConnection::Query streamadd({
			"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*",
			"runtime_type", "upsert",
			"redis_key", redisStateKey,
			"checksum", checksum,
		});

		for (const Dictionary::Pair& kv : stateAttrs) {
			streamadd.emplace_back(kv.first);
			streamadd.emplace_back(IcingaToStreamValue(kv.second));
		}

		m_RconWorker->FireAndForgetQuery(std::move(streamadd), {0, 1});
	}
}

/**
 * Update the dependency state information of the given checkable and its associated dependency groups in Redis.
 *
 * This function serializes the dependency state information of the provided Checkable object and its associated
 * DependencyGroup into Redis HMSETs and streams the state updates to the runtime state stream. It's intended to
 * be called by the background worker when processing runtime updates for Checkable objects that are part of some
 * dependency graph.
 *
 * @param checkable The Checkable you want to send the dependencies state update for
 * @param dependencyGroup The dependency group to process for the given checkable.
 */
void IcingaDB::UpdateDependenciesState(const Checkable::Ptr& checkable, const DependencyGroup::Ptr& dependencyGroup) const
{
	RedisConnection::Queries streamStates;
	auto addDependencyStateToStream([&streamStates](std::string_view redisKey, const Dictionary::Ptr& stateAttrs) {
		RedisConnection::Query xAdd{
			"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*", "runtime_type", "upsert",
			"redis_key", redisKey
		};
		ObjectLock olock(stateAttrs);
		for (auto& [key, value] : stateAttrs) {
			xAdd.emplace_back(key);
			xAdd.emplace_back(IcingaToStreamValue(value));
		}
		streamStates.emplace_back(std::move(xAdd));
	});

	std::map<RedisConnection::QueryArg, RedisConnection::Query> hMSets;
	auto dependencies(dependencyGroup->GetDependenciesForChild(checkable.get()));
	std::sort(dependencies.begin(), dependencies.end(), [](const Dependency::Ptr& lhs, const Dependency::Ptr& rhs) {
		return lhs->GetParent() < rhs->GetParent();
	});
	for (auto it(dependencies.begin()); it != dependencies.end(); /* no increment */) {
		const auto& dependency(*it);

		Dictionary::Ptr stateAttrs;
		// Note: The following loop is intended to cover some possible special cases but may not occur in practice
		// that often. That is, having two or more dependency objects that point to the same parent Checkable.
		// So, traverse all those duplicates and merge their relevant state information into a single edge.
		for (; it != dependencies.end() && (*it)->GetParent() == dependency->GetParent(); ++it) {
			if (!stateAttrs || stateAttrs->Get("failed") == false) {
				stateAttrs = SerializeDependencyEdgeState(dependencyGroup, *it);
			}
		}

		addDependencyStateToStream(CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", stateAttrs);
		AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", stateAttrs->Get("id"), stateAttrs);
	}

	if (dependencyGroup->IsRedundancyGroup()) {
		Dictionary::Ptr stateAttrs(SerializeRedundancyGroupState(checkable, dependencyGroup));

		Dictionary::Ptr sharedGroupState(stateAttrs->ShallowClone());
		sharedGroupState->Remove("redundancy_group_id");
		sharedGroupState->Remove("is_reachable");
		sharedGroupState->Remove("last_state_change");

		addDependencyStateToStream(CONFIG_REDIS_KEY_PREFIX "redundancygroup:state", stateAttrs);
		addDependencyStateToStream(CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", sharedGroupState);
		AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "redundancygroup:state", dependencyGroup->GetIcingaDBIdentifier(), stateAttrs);
		AddDataToHmSets(hMSets, CONFIG_REDIS_KEY_PREFIX "dependency:edge:state", dependencyGroup->GetIcingaDBIdentifier(), sharedGroupState);
	}

	if (!streamStates.empty()) {
		RedisConnection::Queries queries;
		for (auto& [redisKey, query] : hMSets) {
			query.insert(query.begin(), {"HSET", redisKey});
			queries.emplace_back(std::move(query));
		}

		m_RconWorker->FireAndForgetQueries(std::move(queries));
		m_RconWorker->FireAndForgetQueries(std::move(streamStates), {0, 1});
	}
}

void IcingaDB::AddObjectDataToRuntimeUpdates(std::vector<Dictionary::Ptr>& runtimeUpdates,
	String objectKey, String redisKey, const Dictionary::Ptr& data)
{
	Dictionary::Ptr dataClone = data->ShallowClone();
	dataClone->Set("id", std::move(objectKey));
	dataClone->Set("redis_key", std::move(redisKey));
	dataClone->Set("runtime_type", "upsert");
	runtimeUpdates.emplace_back(dataClone);
}

// Takes object and collects IcingaDB relevant attributes and computes checksums. Returns whether the object is relevant
// for IcingaDB.
bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& attributes)
{
	auto originalAttrs (object->GetOriginalAttributes());

	if (originalAttrs) {
		originalAttrs = originalAttrs->ShallowClone();
	}

	attributes->Set("name_checksum", SHA1(object->GetName()));
	attributes->Set("environment_id", m_EnvironmentId);
	attributes->Set("name", object->GetName());
	attributes->Set("original_attributes", originalAttrs);

	Zone::Ptr ObjectsZone;
	Type::Ptr type = object->GetReflectionType();

	if (type == Endpoint::TypeInstance) {
		ObjectsZone = static_cast<Endpoint*>(object.get())->GetZone();
	} else {
		ObjectsZone = static_pointer_cast<Zone>(object->GetZone());
	}

	if (ObjectsZone) {
		attributes->Set("zone_id", GetObjectIdentifier(ObjectsZone));
		attributes->Set("zone_name", ObjectsZone->GetName());
	}

	if (type == Endpoint::TypeInstance) {
		return true;
	}

	if (type == Zone::TypeInstance) {
		Zone::Ptr zone = static_pointer_cast<Zone>(object);

		attributes->Set("is_global", zone->GetGlobal());

		Zone::Ptr parent = zone->GetParent();
		if (parent) {
			attributes->Set("parent_id", GetObjectIdentifier(parent));
		}

		auto parentsRaw (zone->GetAllParentsRaw());
		attributes->Set("depth", parentsRaw.size());

		return true;
	}

	if (type == Host::TypeInstance || type == Service::TypeInstance) {
		Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
		auto checkTimeout (checkable->GetCheckTimeout());

		attributes->Set("checkcommand_name", checkable->GetCheckCommand()->GetName());
		attributes->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
		attributes->Set("check_timeout", checkTimeout.IsEmpty() ? checkable->GetCheckCommand()->GetTimeout() : (double)checkTimeout);
		attributes->Set("check_interval", checkable->GetCheckInterval());
		attributes->Set("check_retry_interval", checkable->GetRetryInterval());
		attributes->Set("active_checks_enabled", checkable->GetEnableActiveChecks());
		attributes->Set("passive_checks_enabled", checkable->GetEnablePassiveChecks());
		attributes->Set("event_handler_enabled", checkable->GetEnableEventHandler());
		attributes->Set("notifications_enabled", checkable->GetEnableNotifications());
		attributes->Set("flapping_enabled", checkable->GetEnableFlapping());
		attributes->Set("flapping_threshold_low", checkable->GetFlappingThresholdLow());
		attributes->Set("flapping_threshold_high", checkable->GetFlappingThresholdHigh());
		attributes->Set("perfdata_enabled", checkable->GetEnablePerfdata());
		attributes->Set("is_volatile", checkable->GetVolatile());
		attributes->Set("notes", checkable->GetNotes());
		attributes->Set("icon_image_alt", checkable->GetIconImageAlt());

		if (size_t totalChildren (checkable->GetAllChildrenCount()); totalChildren > 0) {
			// Only set the Redis key if the Checkable has actually some child dependencies.
			attributes->Set("total_children", totalChildren);
		}

		attributes->Set("checkcommand_id", GetObjectIdentifier(checkable->GetCheckCommand()));

		Endpoint::Ptr commandEndpoint = checkable->GetCommandEndpoint();
		if (commandEndpoint) {
			attributes->Set("command_endpoint_id", GetObjectIdentifier(commandEndpoint));
			attributes->Set("command_endpoint_name", commandEndpoint->GetName());
		}

		TimePeriod::Ptr timePeriod = checkable->GetCheckPeriod();
		if (timePeriod) {
			attributes->Set("check_timeperiod_id", GetObjectIdentifier(timePeriod));
			attributes->Set("check_timeperiod_name", timePeriod->GetName());
		}

		EventCommand::Ptr eventCommand = checkable->GetEventCommand();
		if (eventCommand) {
			attributes->Set("eventcommand_id", GetObjectIdentifier(eventCommand));
			attributes->Set("eventcommand_name", eventCommand->GetName());
		}

		String actionUrl = checkable->GetActionUrl();
		String notesUrl = checkable->GetNotesUrl();
		String iconImage = checkable->GetIconImage();
		if (!actionUrl.IsEmpty())
			attributes->Set("action_url_id", HashValue(new Array({m_EnvironmentId, actionUrl})));
		if (!notesUrl.IsEmpty())
			attributes->Set("notes_url_id", HashValue(new Array({m_EnvironmentId, notesUrl})));
		if (!iconImage.IsEmpty())
			attributes->Set("icon_image_id", HashValue(new Array({m_EnvironmentId, iconImage})));


		Host::Ptr host;
		Service::Ptr service;
		tie(host, service) = GetHostService(checkable);

		if (service) {
			attributes->Set("host_id", GetObjectIdentifier(service->GetHost()));
			attributes->Set("display_name", service->GetDisplayName());

			// Overwrite name here, `object->name` is 'HostName!ServiceName' but we only want the name of the Service
			attributes->Set("name", service->GetShortName());
		} else {
			attributes->Set("display_name", host->GetDisplayName());
			attributes->Set("address", host->GetAddress());
			attributes->Set("address6", host->GetAddress6());
		}

		return true;
	}

	if (type == User::TypeInstance) {
		User::Ptr user = static_pointer_cast<User>(object);

		attributes->Set("display_name", user->GetDisplayName());
		attributes->Set("email", user->GetEmail());
		attributes->Set("pager", user->GetPager());
		attributes->Set("notifications_enabled", user->GetEnableNotifications());
		attributes->Set("states", StateFilterToRedisValue(user->GetStateFilter()));
		attributes->Set("types", TypeFilterToRedisValue(user->GetTypeFilter()));

		if (user->GetPeriod())
			attributes->Set("timeperiod_id", GetObjectIdentifier(user->GetPeriod()));

		return true;
	}

	if (type == TimePeriod::TypeInstance) {
		TimePeriod::Ptr timeperiod = static_pointer_cast<TimePeriod>(object);

		attributes->Set("display_name", timeperiod->GetDisplayName());
		attributes->Set("prefer_includes", timeperiod->GetPreferIncludes());
		return true;
	}

	if (type == Notification::TypeInstance) {
		Notification::Ptr notification = static_pointer_cast<Notification>(object);

		Host::Ptr host;
		Service::Ptr service;

		tie(host, service) = GetHostService(notification->GetCheckable());

		attributes->Set("notificationcommand_id", GetObjectIdentifier(notification->GetCommand()));

		attributes->Set("host_id", GetObjectIdentifier(host));
		if (service)
			attributes->Set("service_id", GetObjectIdentifier(service));

		TimePeriod::Ptr timeperiod = notification->GetPeriod();
		if (timeperiod)
			attributes->Set("timeperiod_id", GetObjectIdentifier(timeperiod));

		if (notification->GetTimes()) {
			auto begin (notification->GetTimes()->Get("begin"));
			auto end (notification->GetTimes()->Get("end"));

			if (begin != Empty && (double)begin >= 0) {
				attributes->Set("times_begin", std::round((double)begin));
			}

			if (end != Empty && (double)end >= 0) {
				attributes->Set("times_end", std::round((double)end));
			}
		}

		attributes->Set("notification_interval", std::max(0.0, std::round(notification->GetInterval())));
		attributes->Set("states", StateFilterToRedisValue(notification->GetStateFilter()));
		attributes->Set("types", TypeFilterToRedisValue(notification->GetTypeFilter()));

		return true;
	}

	if (type == Comment::TypeInstance) {
		Comment::Ptr comment = static_pointer_cast<Comment>(object);

		attributes->Set("author", comment->GetAuthor());
		attributes->Set("text", comment->GetText());
		attributes->Set("entry_type", IcingaDB::CommentTypeToString(comment->GetEntryType()));
		attributes->Set("entry_time", TimestampToMilliseconds(comment->GetEntryTime()));
		attributes->Set("is_persistent", comment->GetPersistent());
		attributes->Set("is_sticky", comment->GetSticky());

		Host::Ptr host;
		Service::Ptr service;
		tie(host, service) = GetHostService(comment->GetCheckable());

		attributes->Set("host_id", GetObjectIdentifier(host));
		if (service) {
			attributes->Set("object_type", "service");
			attributes->Set("service_id", GetObjectIdentifier(service));
		} else
			attributes->Set("object_type", "host");

		auto expireTime (comment->GetExpireTime());

		if (expireTime > 0) {
			attributes->Set("expire_time", TimestampToMilliseconds(expireTime));
		}

		return true;
	}

	if (type == Downtime::TypeInstance) {
		Downtime::Ptr downtime = static_pointer_cast<Downtime>(object);

		attributes->Set("author", downtime->GetAuthor());
		attributes->Set("comment", downtime->GetComment());
		attributes->Set("entry_time", TimestampToMilliseconds(downtime->GetEntryTime()));
		attributes->Set("scheduled_start_time", TimestampToMilliseconds(downtime->GetStartTime()));
		attributes->Set("scheduled_end_time", TimestampToMilliseconds(downtime->GetEndTime()));
		attributes->Set("scheduled_duration", TimestampToMilliseconds(std::max(0.0, downtime->GetEndTime() - downtime->GetStartTime())));
		attributes->Set("flexible_duration", TimestampToMilliseconds(std::max(0.0, downtime->GetDuration())));
		attributes->Set("is_flexible", !downtime->GetFixed());
		attributes->Set("is_in_effect", downtime->IsInEffect());
		if (downtime->IsInEffect()) {
			attributes->Set("start_time", TimestampToMilliseconds(downtime->GetTriggerTime()));

			attributes->Set("end_time", TimestampToMilliseconds(
				downtime->GetFixed() ? downtime->GetEndTime() : (downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))
			));
		}

		auto duration = downtime->GetDuration();
		if (downtime->GetFixed()) {
			duration = downtime->GetEndTime() - downtime->GetStartTime();
		}
		attributes->Set("duration", TimestampToMilliseconds(std::max(0.0, duration)));

		Host::Ptr host;
		Service::Ptr service;
		tie(host, service) = GetHostService(downtime->GetCheckable());

		attributes->Set("host_id", GetObjectIdentifier(host));
		if (service) {
			attributes->Set("object_type", "service");
			attributes->Set("service_id", GetObjectIdentifier(service));
		} else
			attributes->Set("object_type", "host");

		auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy()));
		if (triggeredBy) {
			attributes->Set("triggered_by_id", GetObjectIdentifier(triggeredBy));
		}

		auto scheduledBy (downtime->GetScheduledBy());
		if (!scheduledBy.IsEmpty()) {
			attributes->Set("scheduled_by", scheduledBy);
		}

		auto parent (Downtime::GetByName(downtime->GetParent()));
		if (parent) {
			attributes->Set("parent_id", GetObjectIdentifier(parent));
		}

		return true;
	}

	if (type == UserGroup::TypeInstance) {
		UserGroup::Ptr userGroup = static_pointer_cast<UserGroup>(object);

		attributes->Set("display_name", userGroup->GetDisplayName());

		return true;
	}

	if (type == HostGroup::TypeInstance) {
		HostGroup::Ptr hostGroup = static_pointer_cast<HostGroup>(object);

		attributes->Set("display_name", hostGroup->GetDisplayName());

		return true;
	}

	if (type == ServiceGroup::TypeInstance) {
		ServiceGroup::Ptr serviceGroup = static_pointer_cast<ServiceGroup>(object);

		attributes->Set("display_name", serviceGroup->GetDisplayName());

		return true;
	}

	if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance) {
		Command::Ptr command = static_pointer_cast<Command>(object);

		attributes->Set("command", JsonEncode(command->GetCommandLine()));
		attributes->Set("timeout", std::max(0, command->GetTimeout()));

		return true;
	}

	return false;
}

/* Creates a config update with computed checksums etc.
 * Writes attributes, customVars and checksums into the respective supplied vectors. Adds two values to each vector
 * (if applicable), first the key then the value. To use in a Redis command the command (e.g. HSET) and the key (e.g.
 * icinga:config:object:downtime) need to be prepended. There is nothing to indicate success or failure.
 */
void
IcingaDB::CreateConfigUpdate(const ConfigObject::Ptr& object, const QueryArgPair& redisKeyPair,
	std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets, std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate)
{
	/* TODO: This isn't essentially correct as we don't keep track of config objects ourselves. This would avoid duplicated config updates at startup.
	if (!runtimeUpdate && m_ConfigDumpInProgress)
		return;
	*/

	Dictionary::Ptr attr = new Dictionary;

	if (!PrepareObject(object, attr))
		return;

	InsertObjectDependencies(object, hMSets, runtimeUpdates, runtimeUpdate);

	String objectKey = GetObjectIdentifier(object);
	auto& attrs (hMSets[redisKeyPair.ObjectKey]);
	auto& chksms (hMSets[redisKeyPair.ChecksumKey]);

	attrs.emplace_back(objectKey);
	attrs.emplace_back(JsonEncode(attr));

	String checksum = HashValue(attr);
	chksms.emplace_back(objectKey);
	chksms.emplace_back(JsonEncode(new Dictionary({{"checksum", checksum}})));

	/* Send an update event to subscribers. */
	if (runtimeUpdate) {
		attr->Set("checksum", checksum);
		AddObjectDataToRuntimeUpdates(runtimeUpdates, objectKey, std::string(redisKeyPair.ObjectKey), attr);
	}
}

void IcingaDB::SendConfigDelete(const ConfigObject::Ptr& object)
{
	if (!m_RconWorker || !m_RconWorker->IsConnected())
		return;

	if (auto customVarObject = dynamic_pointer_cast<CustomVarObject>(object); customVarObject) {
		SendCustomVarsChanged(object, customVarObject->GetVars(), nullptr);
	}

	Type::Ptr type = object->GetReflectionType();
	if (type == Host::TypeInstance || type == Service::TypeInstance) {
		Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);

		Host::Ptr host;
		Service::Ptr service;
		tie(host, service) = GetHostService(checkable);

		auto [configStateKey, checksumStateKey] = GetCheckableStateKeys(checkable->GetReflectionType());
		EnqueueRelationsDeletion(GetObjectIdentifier(checkable), {{configStateKey, checksumStateKey}});
		EnqueueConfigObject(object, icingadb::task_queue::ConfigDelete | icingadb::task_queue::NextUpdate); // Send also ZREM for next update

		if (service) {
			SendGroupsChanged<ServiceGroup>(checkable, service->GetGroups(), nullptr);
		} else {
			SendGroupsChanged<HostGroup>(checkable, host->GetGroups(), nullptr);
		}

		return;
	}

	EnqueueConfigObject(object, icingadb::task_queue::ConfigDelete);

	if (type == TimePeriod::TypeInstance) {
		TimePeriod::Ptr timeperiod = static_pointer_cast<TimePeriod>(object);
		SendTimePeriodRangesChanged(timeperiod, timeperiod->GetRanges(), nullptr);
		SendTimePeriodIncludesChanged(timeperiod, timeperiod->GetIncludes(), nullptr);
		SendTimePeriodExcludesChanged(timeperiod, timeperiod->GetExcludes(), nullptr);
		return;
	}

	if (type == User::TypeInstance) {
		User::Ptr user = static_pointer_cast<User>(object);
		SendGroupsChanged<UserGroup>(user, user->GetGroups(), nullptr);
		return;
	}

	if (type == Notification::TypeInstance) {
		Notification::Ptr notification = static_pointer_cast<Notification>(object);
		SendNotificationUsersChanged(notification, notification->GetUsersRaw(), nullptr);
		SendNotificationUserGroupsChanged(notification, notification->GetUserGroupsRaw(), nullptr);
		return;
	}

	if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance) {
		Command::Ptr command = static_pointer_cast<Command>(object);
		const auto& cmdRedisKeys = GetCmdEnvArgKeys(command->GetReflectionType());
		SendCommandArgumentsChanged(command, cmdRedisKeys, command->GetArguments(), nullptr);
		SendCommandEnvChanged(command, cmdRedisKeys, command->GetEnv(), nullptr);
		return;
	}
}

static inline
unsigned short GetPreviousState(const Checkable::Ptr& checkable, const Service::Ptr& service, StateType type)
{
	auto phs ((type == StateTypeHard ? checkable->GetLastHardStatesRaw() : checkable->GetLastSoftStatesRaw()) % 100u);

	if (service || phs == ServicePending) {
		return phs;
	} else {
		return Host::CalculateState(ServiceState(phs));
	}
}

void IcingaDB::SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{
	if (!GetActive()) {
		return;
	}

	Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
	if (!checkable)
		return;

	if (!cr)
		return;

	Host::Ptr host;
	Service::Ptr service;

	tie(host, service) = GetHostService(checkable);

	EnqueueConfigObject(checkable, icingadb::task_queue::RuntimeState);

	int hard_state{};
	if (!cr) {
		hard_state = ServicePending;
	} else if (service) {
		hard_state = service->GetLastHardState();
	} else {
		hard_state = host->GetLastHardState();
	}

	int soft_state{};
	if (!cr) {
		soft_state = ServicePending;
	} else if (service) {
		soft_state = cr->GetState();
	} else {
		soft_state = Host::CalculateState(cr->GetState());
	}

	auto eventTime (cr->GetExecutionEnd());
	auto eventTs (TimestampToMilliseconds(eventTime));

	Array::Ptr rawId = new Array({m_EnvironmentId, object->GetName()});
	rawId->Add(eventTs);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:state", "*",
		"id", HashValue(rawId),
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"state_type", Checkable::StateTypeToString(type).ToLower(),
		"soft_state", Convert::ToString(soft_state),
		"hard_state", Convert::ToString(hard_state),
		"check_attempt", Convert::ToString(checkable->GetCheckAttempt()),
		"previous_soft_state", Convert::ToString(GetPreviousState(checkable, service, StateTypeSoft)),
		"previous_hard_state", Convert::ToString(GetPreviousState(checkable, service, StateTypeHard)),
		"max_check_attempts", Convert::ToString(checkable->GetMaxCheckAttempts()),
		"event_time", Convert::ToString(eventTs),
		"event_id", CalcEventID("state_change", object, eventTime),
		"event_type", "state_change"
	});

	if (cr) {
		auto output (cr->GetOutput());
		auto pos (output.Find("\n"));

		if (pos != String::NPos) {
			auto longOutput (output.SubStr(pos + 1u));
			output.erase(output.Begin() + pos, output.End());

			xAdd.emplace_back("long_output");
			xAdd.emplace_back(Utility::ValidateUTF8(std::move(longOutput)));
		}

		xAdd.emplace_back("output");
		xAdd.emplace_back(Utility::ValidateUTF8(std::move(output)));
		xAdd.emplace_back("check_source");
		xAdd.emplace_back(cr->GetCheckSource());
		xAdd.emplace_back("scheduling_source");
		xAdd.emplace_back(cr->GetSchedulingSource());
	}

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendSentNotification(
	const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
	NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text, double sendTime
)
{
	if (!GetActive()) {
		return;
	}

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	auto finalText = text;
	if (finalText == "" && cr) {
		finalText = cr->GetOutput();
	}

	auto usersAmount (users.size());
	auto sendTs (TimestampToMilliseconds(sendTime));

	auto notificationTypeStr(GetNotificationTypeByEnum(type));
	Array::Ptr rawId = new Array({m_EnvironmentId, notification->GetName()});
	rawId->Add(notificationTypeStr);
	rawId->Add(sendTs);

	auto notificationHistoryId (HashValue(rawId));

	int state{};
	if (!cr) {
		state = ServicePending;
	} else if (service) {
		state = cr->GetState();
	} else {
		state = Host::CalculateState(cr->GetState());
	}

	int previous_hard_state{};
	if (!cr) {
		previous_hard_state = ServicePending;
	} else if (service) {
		previous_hard_state = cr->GetPreviousHardState();
	} else {
		previous_hard_state = Host::CalculateState(cr->GetPreviousHardState());
	}

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:notification", "*",
		"id", notificationHistoryId,
		"environment_id", m_EnvironmentId,
		"notification_id", GetObjectIdentifier(notification),
		"host_id", GetObjectIdentifier(host),
		"type", notificationTypeStr,
		"state", Convert::ToString(state),
		"previous_hard_state", Convert::ToString(previous_hard_state),
		"author", Utility::ValidateUTF8(author),
		"text", Utility::ValidateUTF8(finalText),
		"users_notified", Convert::ToString(usersAmount),
		"send_time", Convert::ToString(sendTs),
		"event_id", CalcEventID("notification", notification, sendTime, type),
		"event_type", "notification"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	if (!users.empty()) {
		Array::Ptr users_notified = new Array();
		for (const User::Ptr& user : users) {
			users_notified->Add(GetObjectIdentifier(user));
		}
		xAdd.emplace_back("users_notified_ids");
		xAdd.emplace_back(JsonEncode(users_notified));
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime)
{
	if (!GetActive()) {
		return;
	}

	EnqueueConfigObject(downtime, icingadb::task_queue::ConfigUpdate);

	auto checkable (downtime->GetCheckable());
	auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy()));

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	/* Update checkable state as in_downtime may have changed. */
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:downtime", "*",
		"downtime_id", GetObjectIdentifier(downtime),
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"entry_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEntryTime())),
		"author", Utility::ValidateUTF8(downtime->GetAuthor()),
		"comment", Utility::ValidateUTF8(downtime->GetComment()),
		"is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()),
		"flexible_duration", Convert::ToString(TimestampToMilliseconds(std::max(0.0, downtime->GetDuration()))),
		"scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())),
		"scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())),
		"has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()),
		"trigger_time", Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())),
		"event_id", CalcEventID("downtime_start", downtime),
		"event_type", "downtime_start"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	if (triggeredBy) {
		xAdd.emplace_back("triggered_by_id");
		xAdd.emplace_back(GetObjectIdentifier(triggeredBy));
	}

	if (downtime->GetFixed()) {
		xAdd.emplace_back("start_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())));
		xAdd.emplace_back("end_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())));
	} else {
		xAdd.emplace_back("start_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())));
		xAdd.emplace_back("end_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))));
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	auto parent (Downtime::GetByName(downtime->GetParent()));

	if (parent) {
		xAdd.emplace_back("parent_id");
		xAdd.emplace_back(GetObjectIdentifier(parent));
	}

	auto scheduledBy (downtime->GetScheduledBy());

	if (!scheduledBy.IsEmpty()) {
		xAdd.emplace_back("scheduled_by");
		xAdd.emplace_back(scheduledBy);
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime)
{
	if (!GetActive()) {
		return;
	}

	auto checkable (downtime->GetCheckable());
	auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy()));

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	// Downtime never got triggered (didn't send "downtime_start") so we don't want to send "downtime_end"
	if (downtime->GetTriggerTime() == 0)
		return;

	/* Update checkable state as in_downtime may have changed. */
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:downtime", "*",
		"downtime_id", GetObjectIdentifier(downtime),
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"entry_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEntryTime())),
		"author", Utility::ValidateUTF8(downtime->GetAuthor()),
		"cancelled_by", Utility::ValidateUTF8(downtime->GetRemovedBy()),
		"comment", Utility::ValidateUTF8(downtime->GetComment()),
		"is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()),
		"flexible_duration", Convert::ToString(TimestampToMilliseconds(std::max(0.0, downtime->GetDuration()))),
		"scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())),
		"scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())),
		"has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()),
		"trigger_time", Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())),
		"event_id", CalcEventID("downtime_end", downtime),
		"event_type", "downtime_end"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	if (triggeredBy) {
		xAdd.emplace_back("triggered_by_id");
		xAdd.emplace_back(GetObjectIdentifier(triggeredBy));
	}

	if (downtime->GetWasCancelled()) {
		xAdd.emplace_back("cancel_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetRemoveTime())));
	}

	if (downtime->GetFixed()) {
		xAdd.emplace_back("start_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())));
		xAdd.emplace_back("end_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())));
	} else {
		xAdd.emplace_back("start_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())));
		xAdd.emplace_back("end_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))));
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	auto parent (Downtime::GetByName(downtime->GetParent()));

	if (parent) {
		xAdd.emplace_back("parent_id");
		xAdd.emplace_back(GetObjectIdentifier(parent));
	}

	auto scheduledBy (downtime->GetScheduledBy());

	if (!scheduledBy.IsEmpty()) {
		xAdd.emplace_back("scheduled_by");
		xAdd.emplace_back(scheduledBy);
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendAddedComment(const Comment::Ptr& comment)
{
	if (comment->GetEntryType() != CommentUser || !GetActive())
		return;

	auto checkable (comment->GetCheckable());

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	// Update the checkable state to so that the "last_comment_id" is correctly reflected.
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:comment", "*",
		"comment_id", GetObjectIdentifier(comment),
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"entry_time", Convert::ToString(TimestampToMilliseconds(comment->GetEntryTime())),
		"author", Utility::ValidateUTF8(comment->GetAuthor()),
		"comment", Utility::ValidateUTF8(comment->GetText()),
		"entry_type", IcingaDB::CommentTypeToString(comment->GetEntryType()),
		"is_persistent", Convert::ToString((unsigned short)comment->GetPersistent()),
		"is_sticky", Convert::ToString((unsigned short)comment->GetSticky()),
		"event_id", CalcEventID("comment_add", comment),
		"event_type", "comment_add"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	{
		auto expireTime (comment->GetExpireTime());

		if (expireTime > 0) {
			xAdd.emplace_back("expire_time");
			xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(expireTime)));
		}
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendRemovedComment(const Comment::Ptr& comment)
{
	if (comment->GetEntryType() != CommentUser || !GetActive()) {
		return;
	}

	double removeTime = comment->GetRemoveTime();
	bool wasRemoved = removeTime > 0;

	double expireTime = comment->GetExpireTime();
	bool hasExpireTime = expireTime > 0;
	bool isExpired = hasExpireTime && expireTime <= Utility::GetTime();

	if (!wasRemoved && !isExpired) {
		/* The comment object disappeared for no apparent reason, most likely because it simply was deleted instead
		 * of using the proper remove-comment API action. In this case, information that should normally be set is
		 * missing and a proper history event cannot be generated.
		 */
		return;
	}

	auto checkable (comment->GetCheckable());

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	// Update the checkable state to so that the "last_comment_id" is correctly reflected.
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:comment", "*",
		"comment_id", GetObjectIdentifier(comment),
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"entry_time", Convert::ToString(TimestampToMilliseconds(comment->GetEntryTime())),
		"author", Utility::ValidateUTF8(comment->GetAuthor()),
		"comment", Utility::ValidateUTF8(comment->GetText()),
		"entry_type", IcingaDB::CommentTypeToString(comment->GetEntryType()),
		"is_persistent", Convert::ToString((unsigned short)comment->GetPersistent()),
		"is_sticky", Convert::ToString((unsigned short)comment->GetSticky()),
		"event_id", CalcEventID("comment_remove", comment),
		"event_type", "comment_remove"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	if (wasRemoved) {
		xAdd.emplace_back("remove_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(removeTime)));
		xAdd.emplace_back("has_been_removed");
		xAdd.emplace_back("1");
		xAdd.emplace_back("removed_by");
		xAdd.emplace_back(Utility::ValidateUTF8(comment->GetRemovedBy()));
	} else {
		xAdd.emplace_back("has_been_removed");
		xAdd.emplace_back("0");
	}

	if (hasExpireTime) {
		xAdd.emplace_back("expire_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(expireTime)));
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendFlappingChange(const Checkable::Ptr& checkable, double changeTime, double flappingLastChange)
{
	if (!GetActive()) {
		return;
	}

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:flapping", "*",
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"flapping_threshold_low", Convert::ToString(checkable->GetFlappingThresholdLow()),
		"flapping_threshold_high", Convert::ToString(checkable->GetFlappingThresholdHigh())
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	double startTime;

	if (checkable->IsFlapping()) {
		startTime = changeTime;

		xAdd.emplace_back("event_type");
		xAdd.emplace_back("flapping_start");
		xAdd.emplace_back("percent_state_change_start");
		xAdd.emplace_back(Convert::ToString(checkable->GetFlappingCurrent()));
	} else {
		startTime = flappingLastChange;

		xAdd.emplace_back("event_type");
		xAdd.emplace_back("flapping_end");
		xAdd.emplace_back("end_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(changeTime)));
		xAdd.emplace_back("percent_state_change_end");
		xAdd.emplace_back(Convert::ToString(checkable->GetFlappingCurrent()));
	}

	long long startTs = TimestampToMilliseconds(startTime);

	xAdd.emplace_back("start_time");
	xAdd.emplace_back(Convert::ToString(startTs));
	xAdd.emplace_back("event_id");
	xAdd.emplace_back(CalcEventID(checkable->IsFlapping() ? "flapping_start" : "flapping_end", checkable, startTime));
	xAdd.emplace_back("id");
	xAdd.emplace_back(HashValue(new Array({m_EnvironmentId, checkable->GetName(), startTs})));

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendAcknowledgementSet(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry)
{
	if (!GetActive()) {
		return;
	}

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	/* Update checkable state as is_acknowledged may have changed. */
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:acknowledgement", "*",
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"event_type", "ack_set",
		"author", author,
		"comment", comment,
		"is_sticky", Convert::ToString((unsigned short)(type == AcknowledgementSticky)),
		"is_persistent", Convert::ToString((unsigned short)persistent)
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	if (expiry > 0) {
		xAdd.emplace_back("expire_time");
		xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(expiry)));
	}

	long long setTs = TimestampToMilliseconds(changeTime);

	xAdd.emplace_back("set_time");
	xAdd.emplace_back(Convert::ToString(setTs));
	xAdd.emplace_back("event_id");
	xAdd.emplace_back(CalcEventID("ack_set", checkable, changeTime));
	xAdd.emplace_back("id");
	xAdd.emplace_back(HashValue(new Array({m_EnvironmentId, checkable->GetName(), setTs})));

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::SendAcknowledgementCleared(const Checkable::Ptr& checkable, const String& removedBy, double changeTime, double ackLastChange)
{
	if (!GetActive()) {
		return;
	}

	Host::Ptr host;
	Service::Ptr service;
	tie(host, service) = GetHostService(checkable);

	/* Update checkable state as is_acknowledged may have changed. */
	EnqueueConfigObject(checkable, icingadb::task_queue::FullState);

	RedisConnection::Query xAdd ({
		"XADD", "icinga:history:stream:acknowledgement", "*",
		"environment_id", m_EnvironmentId,
		"host_id", GetObjectIdentifier(host),
		"clear_time", Convert::ToString(TimestampToMilliseconds(changeTime)),
		"event_type", "ack_clear"
	});

	if (service) {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("service");
		xAdd.emplace_back("service_id");
		xAdd.emplace_back(GetObjectIdentifier(checkable));
	} else {
		xAdd.emplace_back("object_type");
		xAdd.emplace_back("host");
	}

	auto endpoint (Endpoint::GetLocalEndpoint());

	if (endpoint) {
		xAdd.emplace_back("endpoint_id");
		xAdd.emplace_back(GetObjectIdentifier(endpoint));
	}

	long long setTs = TimestampToMilliseconds(ackLastChange);

	xAdd.emplace_back("set_time");
	xAdd.emplace_back(Convert::ToString(setTs));
	xAdd.emplace_back("event_id");
	xAdd.emplace_back(CalcEventID("ack_clear", checkable, ackLastChange));
	xAdd.emplace_back("id");
	xAdd.emplace_back(HashValue(new Array({m_EnvironmentId, checkable->GetName(), setTs})));

	if (!removedBy.IsEmpty()) {
		xAdd.emplace_back("cleared_by");
		xAdd.emplace_back(removedBy);
	}

	m_HistoryBulker.ProduceOne(std::move(xAdd));
}

void IcingaDB::ForwardHistoryEntries()
{
	using clock = std::chrono::steady_clock;

	const std::chrono::seconds logInterval (10);
	auto nextLog (clock::now() + logInterval);

	auto logPeriodically ([this, logInterval, &nextLog]() {
		if (clock::now() > nextLog) {
			nextLog += logInterval;

			auto size (m_HistoryBulker.Size());

			Log(size > m_HistoryBulker.GetBulkSize() ? LogInformation : LogNotice, "IcingaDB")
				<< "Pending history queries: " << size;
		}
	});

	for (;;) {
		logPeriodically();

		auto haystack (m_HistoryBulker.ConsumeMany());

		if (haystack.empty()) {
			if (!GetActive()) {
				break;
			}

			continue;
		}

		uintmax_t attempts = 0;

		auto logFailure ([&haystack, &attempts](const char* err = nullptr) {
			Log msg (LogNotice, "IcingaDB");

			msg << "history: " << haystack.size() << " queries failed temporarily (attempt #" << ++attempts << ")";

			if (err) {
				msg << ": " << err;
			}
		});

		for (;;) {
			logPeriodically();

			if (m_Rcon && m_Rcon->IsConnected()) {
				try {
					m_Rcon->GetResultsOfQueries(haystack, {0, 0, haystack.size()});
					break;
				} catch (const std::exception& ex) {
					logFailure(ex.what());
				} catch (...) {
					logFailure();
				}
			} else {
				logFailure("not connected to Redis");
			}

			if (!GetActive()) {
				Log(LogCritical, "IcingaDB") << "history: " << haystack.size() << " queries failed (attempt #" << attempts
					<< ") while we're about to shut down. Giving up and discarding additional "
					<< m_HistoryBulker.Size() << " queued history queries.";

				return;
			}

			Utility::Sleep(2);
		}
	}
}

void IcingaDB::SendNotificationUsersChanged(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<Value> deletedUsers = GetArrayDeletedValues(oldValues, newValues);

	for (const auto& userName : deletedUsers) {
		String id = HashValue(new Array({m_EnvironmentId, "user", userName, notification->GetName()}));
		EnqueueRelationsDeletion(id,{
			{CONFIG_REDIS_KEY_PREFIX "notification:user", ""},
			{CONFIG_REDIS_KEY_PREFIX "notification:recipient", ""},
		});
	}
}

void IcingaDB::SendNotificationUserGroupsChanged(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<Value> deletedUserGroups = GetArrayDeletedValues(oldValues, newValues);

	for (const auto& userGroupName : deletedUserGroups) {
		UserGroup::Ptr userGroup = UserGroup::GetByName(userGroupName);
		String id = HashValue(new Array({m_EnvironmentId, "usergroup", userGroupName, notification->GetName()}));
		EnqueueRelationsDeletion(id, {
			{CONFIG_REDIS_KEY_PREFIX "notification:usergroup", ""},
			{CONFIG_REDIS_KEY_PREFIX "notification:recipient", ""}
		});

		for (const User::Ptr& user : userGroup->GetMembers()) {
			String userId = HashValue(new Array({m_EnvironmentId, "usergroupuser", user->GetName(), userGroupName, notification->GetName()}));
			EnqueueRelationsDeletion(userId, {{CONFIG_REDIS_KEY_PREFIX "notification:recipient", ""}});
		}
	}
}

void IcingaDB::SendTimePeriodRangesChanged(const TimePeriod::Ptr& timeperiod, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<String> deletedKeys = GetDictionaryDeletedKeys(oldValues, newValues);

	for (const auto& rangeKey : deletedKeys) {
		String id = HashValue(new Array({m_EnvironmentId, rangeKey, oldValues->Get(rangeKey), timeperiod->GetName()}));
		EnqueueRelationsDeletion(id, {{CONFIG_REDIS_KEY_PREFIX "timeperiod:range", ""}});
	}
}

void IcingaDB::SendTimePeriodIncludesChanged(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<Value> deletedIncludes = GetArrayDeletedValues(oldValues, newValues);

	for (const auto& includeName : deletedIncludes) {
		String id = HashValue(new Array({m_EnvironmentId, includeName, timeperiod->GetName()}));
		EnqueueRelationsDeletion(id, {{CONFIG_REDIS_KEY_PREFIX "timeperiod:override:include", ""}});
	}
}

void IcingaDB::SendTimePeriodExcludesChanged(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<Value> deletedExcludes = GetArrayDeletedValues(oldValues, newValues);

	for (const auto& excludeName : deletedExcludes) {
		String id = HashValue(new Array({m_EnvironmentId, excludeName, timeperiod->GetName()}));
		EnqueueRelationsDeletion(id, {{CONFIG_REDIS_KEY_PREFIX "timeperiod:override:exclude", ""}});
	}
}

template<typename T>
void IcingaDB::SendGroupsChanged(const ConfigObject::Ptr& object, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	std::vector<Value> deletedGroups = GetArrayDeletedValues(oldValues, newValues);
	std::string_view keyType;
	if constexpr (std::is_same_v<T, UserGroup>) {
		keyType = CONFIG_REDIS_KEY_PREFIX "usergroup:member";
	} else if constexpr (std::is_same_v<T, HostGroup>) {
		keyType = CONFIG_REDIS_KEY_PREFIX "hostgroup:member";
	} else {
		static_assert(std::is_same_v<T, ServiceGroup>, "IcingaDB::SendGroupsChanged<T>: T must be UserGroup, HostGroup or ServiceGroup");
		keyType = CONFIG_REDIS_KEY_PREFIX "servicegroup:member";
	}

	for (const auto& groupName : deletedGroups) {
		typename T::Ptr group = ConfigObject::GetObject<T>(groupName);
		String id = HashValue(new Array({m_EnvironmentId, group->GetName(), object->GetName()}));
		EnqueueRelationsDeletion(id, {{keyType, ""}});

		if constexpr (std::is_same_v<T, UserGroup>) {
			UserGroup::Ptr userGroup = dynamic_pointer_cast<UserGroup>(group);

			for (const auto& notification : userGroup->GetNotifications()) {
				String userId = HashValue(new Array({m_EnvironmentId, "usergroupuser", object->GetName(), groupName, notification->GetName()}));
				EnqueueRelationsDeletion(userId, {{CONFIG_REDIS_KEY_PREFIX "notification:recipient", ""}});
			}
		}
	}
}

void IcingaDB::SendCommandEnvChanged(
	const ConfigObject::Ptr& command,
	const CmdArgEnvRedisKeys& cmdRedisKeys,
	const Dictionary::Ptr& oldValues,
	const Dictionary::Ptr& newValues
)
{
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	for (const auto& envvarKey : GetDictionaryDeletedKeys(oldValues, newValues)) {
		String id = HashValue(new Array({m_EnvironmentId, envvarKey, command->GetName()}));
		EnqueueRelationsDeletion(id, {{cmdRedisKeys.EnvObjectKey, cmdRedisKeys.EnvChecksumKey}});
	}
}

void IcingaDB::SendCommandArgumentsChanged(
	const ConfigObject::Ptr& command,
	const CmdArgEnvRedisKeys& cmdRedisKeys,
	const Dictionary::Ptr& oldValues,
	const Dictionary::Ptr& newValues
)
{
	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	for (const auto& argumentKey : GetDictionaryDeletedKeys(oldValues, newValues)) {
		String id = HashValue(new Array({m_EnvironmentId, argumentKey, command->GetName()}));
		EnqueueRelationsDeletion(id, {{cmdRedisKeys.ArgObjectKey, cmdRedisKeys.ArgChecksumKey}});
	}
}

void IcingaDB::SendCustomVarsChanged(const ConfigObject::Ptr& object, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	const auto& type = object->GetReflectionType();
	std::string_view customvarKey = GetRedisCustomVarKey(type);
	if (customvarKey.empty()) {
		return;
	}

	if (!m_RconWorker || !m_RconWorker->IsConnected() || oldValues == newValues) {
		return;
	}

	Dictionary::Ptr oldVars = SerializeVars(oldValues);
	Dictionary::Ptr newVars = SerializeVars(newValues);

	for (const auto& varId : GetDictionaryDeletedKeys(oldVars, newVars)) {
		String id = HashValue(new Array({m_EnvironmentId, varId, object->GetName()}));
		EnqueueRelationsDeletion(id, {{customvarKey, ""}});
	}
}

Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)
{
	Dictionary::Ptr attrs = new Dictionary();

	Host::Ptr host;
	Service::Ptr service;

	tie(host, service) = GetHostService(checkable);

	String id = GetObjectIdentifier(checkable);

	/*
	 * As there is a 1:1 relationship between host and host state, the host ID ('host_id')
	 * is also used as the host state ID ('id'). These are duplicated to 1) avoid having
	 * special handling for this in Icinga DB and 2) to have both a primary key and a foreign key
	 * in the SQL database in the end. In the database 'host_id' ends up as foreign key 'host_state.host_id'
	 * referring to 'host.id' while 'id' ends up as the primary key 'host_state.id'. This also applies for service.
	 */
	attrs->Set("id", id);
	attrs->Set("environment_id", m_EnvironmentId);
	attrs->Set("state_type", Checkable::StateTypeToString(checkable->HasBeenChecked() ? checkable->GetStateType() : StateTypeHard).ToLower());

	// TODO: last_hard/soft_state should be "previous".
	if (service) {
		attrs->Set("service_id", id);
		auto state = service->HasBeenChecked() ? service->GetState() : ServicePending;
		attrs->Set("soft_state", state);
		attrs->Set("hard_state", service->HasBeenChecked() ? service->GetLastHardState() : ServicePending);
		attrs->Set("severity", service->GetSeverity());
		attrs->Set("host_id", GetObjectIdentifier(host));
	} else {
		attrs->Set("host_id", id);
		auto state = host->HasBeenChecked() ? host->GetState() : HostPending;
		attrs->Set("soft_state", state);
		attrs->Set("hard_state", host->HasBeenChecked() ? host->GetLastHardState() : HostPending);
		attrs->Set("severity", host->GetSeverity());
	}

	attrs->Set("previous_soft_state", GetPreviousState(checkable, service, StateTypeSoft));
	attrs->Set("previous_hard_state", GetPreviousState(checkable, service, StateTypeHard));
	attrs->Set("check_attempt", checkable->GetCheckAttempt());

	attrs->Set("is_active", checkable->IsActive());
	attrs->Set("affects_children", checkable->AffectsChildren());

	CheckResult::Ptr cr = checkable->GetLastCheckResult();

	if (cr) {
		String rawOutput = cr->GetOutput();
		if (!rawOutput.IsEmpty()) {
			size_t lineBreak = rawOutput.Find("\n");
			String output = rawOutput.SubStr(0, lineBreak);
			if (!output.IsEmpty())
				attrs->Set("output", rawOutput.SubStr(0, lineBreak));

			if (lineBreak > 0 && lineBreak != String::NPos) {
				String longOutput = rawOutput.SubStr(lineBreak+1, rawOutput.GetLength());
				if (!longOutput.IsEmpty())
					attrs->Set("long_output", longOutput);
			}
		}

		String perfData = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
		if (!perfData.IsEmpty())
			attrs->Set("performance_data", perfData);

		String normedPerfData = PluginUtility::FormatPerfdata(cr->GetPerformanceData(), true);
		if (!normedPerfData.IsEmpty())
			attrs->Set("normalized_performance_data", normedPerfData);

		if (!cr->GetCommand().IsEmpty())
			attrs->Set("check_commandline", FormatCommandLine(cr->GetCommand()));
		attrs->Set("execution_time", std::min((long long)UINT32_MAX, TimestampToMilliseconds(fmax(0.0, cr->CalculateExecutionTime()))));
		attrs->Set("latency", std::min((long long)UINT32_MAX, TimestampToMilliseconds(cr->CalculateLatency())));
		attrs->Set("check_source", cr->GetCheckSource());
		attrs->Set("scheduling_source", cr->GetSchedulingSource());
	}

	attrs->Set("is_problem", checkable->GetProblem());
	attrs->Set("is_handled", checkable->GetHandled());
	attrs->Set("is_reachable", checkable->IsReachable());
	attrs->Set("is_flapping", checkable->IsFlapping());

	attrs->Set("is_acknowledged", checkable->IsAcknowledged());
	attrs->Set("is_sticky_acknowledgement", checkable->GetAcknowledgement() == AcknowledgementSticky);

	if (checkable->IsAcknowledged()) {
		Timestamp entry = 0;
		Comment::Ptr AckComment;
		for (const Comment::Ptr& c : checkable->GetComments()) {
			if (c->GetEntryType() == CommentAcknowledgement) {
				if (c->GetEntryTime() > entry) {
					entry = c->GetEntryTime();
					AckComment = c;
				}
			}
		}
		if (AckComment != nullptr) {
			attrs->Set("acknowledgement_comment_id", GetObjectIdentifier(AckComment));
		}
	}

	{
		auto lastComment (checkable->GetLastComment());

		if (lastComment) {
			attrs->Set("last_comment_id", GetObjectIdentifier(lastComment));
		}
	}

	attrs->Set("in_downtime", checkable->IsInDowntime());

	if (checkable->GetCheckTimeout().IsEmpty())
		attrs->Set("check_timeout", TimestampToMilliseconds(checkable->GetCheckCommand()->GetTimeout()));
	else
		attrs->Set("check_timeout", TimestampToMilliseconds(checkable->GetCheckTimeout()));

	long long lastCheck = TimestampToMilliseconds(checkable->GetLastCheck());
	if (lastCheck > 0)
		attrs->Set("last_update", lastCheck);

	attrs->Set("last_state_change", TimestampToMilliseconds(checkable->GetLastStateChange()));
	attrs->Set("next_check", TimestampToMilliseconds(checkable->GetNextCheck()));
	attrs->Set("next_update", TimestampToMilliseconds(checkable->GetNextUpdate()));

	return attrs;
}

void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendStateChange(object, cr, type);
	}
}

void IcingaDB::ReachabilityChangeHandler(const std::set<Checkable::Ptr>& children)
{
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		for (auto& checkable : children) {
			rw->EnqueueConfigObject(checkable, icingadb::task_queue::FullState);
			for (const auto& dependencyGroup : checkable->GetDependencyGroups()) {
				rw->EnqueueDependencyGroupStateUpdate(dependencyGroup);
			}
		}
	}
}

void IcingaDB::VersionChangedHandler(const ConfigObject::Ptr& object)
{
	Type::Ptr type = object->GetReflectionType();
	auto it = std::find_if(std::begin(l_SyncableTypes), std::end(l_SyncableTypes), [&type](const auto& pair) {
		return pair.first == type;
	});
	if (it == std::end(l_SyncableTypes)) {
		return;
	}

	if (object->IsActive()) {
		for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
			// A runtime config change triggers also a full state update as well as next update event.
			rw->EnqueueConfigObject(object, icingadb::task_queue::ConfigUpdate | icingadb::task_queue::FullState | icingadb::task_queue::NextUpdate);
		}
	} else if (!object->IsActive() && object->GetExtension("ConfigObjectDeleted")) { // same as in apilistener-configsync.cpp
		for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
			rw->SendConfigDelete(object);
		}
	}
}

void IcingaDB::DowntimeStartedHandler(const Downtime::Ptr& downtime)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendStartedDowntime(downtime);
	}
}

void IcingaDB::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendRemovedDowntime(downtime);
	}
}

void IcingaDB::NotificationSentToAllUsersHandler(
	const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
	NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text
)
{
	auto rws (ConfigType::GetObjectsByType<IcingaDB>());
	auto sendTime (notification->GetLastNotification());

	if (!rws.empty()) {
		for (auto& rw : rws) {
			rw->SendSentNotification(notification, checkable, users, type, cr, author, text, sendTime);
		}
	}
}

void IcingaDB::CommentAddedHandler(const Comment::Ptr& comment)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendAddedComment(comment);
	}
}

void IcingaDB::CommentRemovedHandler(const Comment::Ptr& comment)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendRemovedComment(comment);
	}
}

void IcingaDB::FlappingChangeHandler(const Checkable::Ptr& checkable, double changeTime)
{
	auto flappingLastChange (checkable->GetFlappingLastChange());

	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendFlappingChange(checkable, changeTime, flappingLastChange);
	}
}

void IcingaDB::NewCheckResultHandler(const Checkable::Ptr& checkable)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->EnqueueConfigObject(checkable, icingadb::task_queue::VolatileState);
	}
}

void IcingaDB::NextCheckChangedHandler(const Checkable::Ptr& checkable)
{
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->EnqueueConfigObject(checkable, icingadb::task_queue::VolatileState | icingadb::task_queue::NextUpdate);
	}
}

void IcingaDB::DependencyGroupChildRegisteredHandler(const Checkable::Ptr& child, const DependencyGroup::Ptr& dependencyGroup)
{
	for (const auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->EnqueueConfigObject(child, icingadb::task_queue::FullState); // Child requires a full state update.
		rw->EnqueueDependencyChildRegistered(dependencyGroup, child);
		rw->EnqueueDependencyGroupStateUpdate(dependencyGroup);

		std::set<Checkable::Ptr> parents;
		dependencyGroup->LoadParents(parents);
		for (const auto& parent : parents) {
			// The total_children and affects_children columns might now have different outcome, so update the parent
			// Checkable as well. The grandparent Checkable may still have wrong numbers of total children, though it's
			// not worth traversing the whole tree way up and sending config updates for each one of them, as the next
			// Redis config dump is going to fix it anyway.
			rw->EnqueueConfigObject(parent, icingadb::task_queue::ConfigUpdate | icingadb::task_queue::FullState);
		}
	}
}

void IcingaDB::DependencyGroupChildRemovedHandler(const DependencyGroup::Ptr& dependencyGroup, const std::vector<Dependency::Ptr>& dependencies, bool removeGroup)
{
	for (const auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->EnqueueDependencyChildRemoved(dependencyGroup, dependencies, removeGroup);
	}
}

void IcingaDB::HostProblemChangedHandler(const Service::Ptr& service) {
	for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		/* Host state changes affect is_handled and severity of services. */
		rw->EnqueueConfigObject(service, icingadb::task_queue::FullState);
	}
}

void IcingaDB::AcknowledgementSetHandler(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry)
{
	auto rws (ConfigType::GetObjectsByType<IcingaDB>());

	if (!rws.empty()) {
		for (auto& rw : rws) {
			rw->SendAcknowledgementSet(checkable, author, comment, type, persistent, changeTime, expiry);
		}
	}
}

void IcingaDB::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const String& removedBy, double changeTime)
{
	auto rws (ConfigType::GetObjectsByType<IcingaDB>());

	if (!rws.empty()) {
		auto rb (Shared<String>::Make(removedBy));
		auto ackLastChange (checkable->GetAcknowledgementLastChange());

		for (auto& rw : rws) {
			rw->SendAcknowledgementCleared(checkable, *rb, changeTime, ackLastChange);
		}
	}
}

void IcingaDB::NotificationUsersChangedHandler(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendNotificationUsersChanged(notification, oldValues, newValues);
	}
}

void IcingaDB::NotificationUserGroupsChangedHandler(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendNotificationUserGroupsChanged(notification, oldValues, newValues);
	}
}

void IcingaDB::TimePeriodRangesChangedHandler(const TimePeriod::Ptr& timeperiod, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendTimePeriodRangesChanged(timeperiod, oldValues, newValues);
	}
}

void IcingaDB::TimePeriodIncludesChangedHandler(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendTimePeriodIncludesChanged(timeperiod, oldValues, newValues);
	}
}

void IcingaDB::TimePeriodExcludesChangedHandler(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendTimePeriodExcludesChanged(timeperiod, oldValues, newValues);
	}
}

void IcingaDB::UserGroupsChangedHandler(const User::Ptr& user, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendGroupsChanged<UserGroup>(user, oldValues, newValues);
	}
}

void IcingaDB::HostGroupsChangedHandler(const Host::Ptr& host, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendGroupsChanged<HostGroup>(host, oldValues, newValues);
	}
}

void IcingaDB::ServiceGroupsChangedHandler(const Service::Ptr& service, const Array::Ptr& oldValues, const Array::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendGroupsChanged<ServiceGroup>(service, oldValues, newValues);
	}
}

void IcingaDB::CommandEnvChangedHandler(const Command::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	const auto& cmdRedisKeys = GetCmdEnvArgKeys(command->GetReflectionType());
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendCommandEnvChanged(command, cmdRedisKeys, oldValues, newValues);
	}
}

void IcingaDB::CommandArgumentsChangedHandler(const Command::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	const auto& cmdRedisKeys = GetCmdEnvArgKeys(command->GetReflectionType());
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendCommandArgumentsChanged(command, cmdRedisKeys, oldValues, newValues);
	}
}

void IcingaDB::CustomVarsChangedHandler(const ConfigObject::Ptr& object, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues) {
	for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
		rw->SendCustomVarsChanged(object, oldValues, newValues);
	}
}

void IcingaDB::DeleteRelationship(const String& id, RedisConnection::QueryArg redisObjKey, RedisConnection::QueryArg redisChecksumKey) {
	Log(LogNotice, "IcingaDB")
		<< "Deleting relationship '" << static_cast<std::string_view>(redisObjKey) << " -> '" << id << "'";

	RedisConnection::Queries queries;

	if (!static_cast<std::string_view>(redisChecksumKey).empty()) {
		queries.push_back({"HDEL", std::move(redisChecksumKey), id});
	}

	queries.push_back({"HDEL", redisObjKey, id});
	queries.push_back({
		"XADD", "icinga:runtime", "MAXLEN", "~", "1000000", "*",
		"redis_key", std::move(redisObjKey), "id", id, "runtime_type", "delete"
	});

	m_RconWorker->FireAndForgetQueries(queries);
}

void IcingaDB::DeleteState(const String& id, RedisConnection::QueryArg redisObjKey, RedisConnection::QueryArg redisChecksumKey) const
{
	Log(LogNotice, "IcingaDB")
		<< "Deleting state '" << static_cast<std::string_view>(redisObjKey) << "' -> " << std::quoted(id.CStr());

	RedisConnection::Queries hdels = {{"HDEL", std::move(redisObjKey), id}};
	if (!static_cast<std::string_view>(redisChecksumKey).empty()) {
		hdels.push_back({"HDEL", std::move(redisChecksumKey), id});
	}

	m_RconWorker->FireAndForgetQueries(std::move(hdels));
	// TODO: This is currently purposefully commented out due to how Icinga DB (Go) handles runtime state
	//       upsert and delete events. See https://github.com/Icinga/icingadb/pull/894 for more details.
	/*m_Rcon->FireAndForgetQueries({{
		"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*",
		"redis_key", redisKey, "id", id, "runtime_type", "delete"
	}}, Prio::RuntimeStateStream, {0, 1});*/
}

/**
 * Add the provided data to the provided map of HMSET queries.
 *
 * @param hMSets The map of HMSETs to add the provided data to.
 * @param redisKey The Redis key to which the HMSET query should be added.
 * @param id The config object identifier to be added as the first field of the HMSET query.
 * @param data The actual data to be added.
 */
void IcingaDB::AddDataToHmSets(
	std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets,
	const RedisConnection::QueryArg& redisKey,
	const String& id,
	const Dictionary::Ptr& data
)
{
	RedisConnection::Query& query = hMSets[redisKey];
	query.emplace_back(id);
	query.emplace_back(JsonEncode(data));
}

/**
 * Execute the provided HMSET values and runtime updates in a single Redis transaction on the provided Redis connection.
 *
 * The HMSETs should just contain the necessary key value pairs to be set in Redis, i.e, without the HMSET command
 * itself. This function will then go through each of the map keys and prepend the HMSET command when transforming the
 * map into valid Redis queries. Likewise, the runtime updates should just contain the key value pairs to be streamed
 * to the icinga:runtime pipeline, and this function will generate a XADD query for each one of the vector elements.
 *
 * @param rcon The Redis connection to execute the transaction on.
 * @param hMSets A map of Redis keys and their respective HMSET values.
 * @param runtimeUpdates A list of dictionaries to be sent to the icinga:runtime stream.
 */
void IcingaDB::ExecuteRedisTransaction(const RedisConnection::Ptr& rcon,
	std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets, const std::vector<Dictionary::Ptr>& runtimeUpdates)
{
	RedisConnection::Queries transaction{{"MULTI"}};
	for (auto& [redisKey, query] : hMSets) {
		if (!query.empty()) {
			query.insert(query.begin(), {"HSET", redisKey});
			transaction.emplace_back(std::move(query));
		}
	}

	for (auto& attrs : runtimeUpdates) {
		RedisConnection::Query xAdd{"XADD", "icinga:runtime", "MAXLEN", "~", "1000000", "*"};

		ObjectLock olock(attrs);
		for (auto& [key, value] : attrs) {
			if (auto streamVal(IcingaToStreamValue(value)); !streamVal.IsEmpty()) {
				xAdd.emplace_back(key);
				xAdd.emplace_back(std::move(streamVal));
			}
		}

		transaction.emplace_back(std::move(xAdd));
	}

	if (transaction.size() > 1) {
		transaction.emplace_back(RedisConnection::Query{"EXEC"});
		if (!runtimeUpdates.empty()) {
			rcon->FireAndForgetQueries(std::move(transaction), {1});
		} else {
			// This is likely triggered by the initial Redis config dump, so a) we don't need to record the number of
			// affected objects and b) we don't really know how many objects are going to be affected by this tx.
			rcon->FireAndForgetQueries(std::move(transaction));
		}
	}
}
