From e884dc0c322a3647f134c827ea96d59e7efb01d3 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 5 Aug 2020 00:14:57 +0200 Subject: [PATCH] Added tagging system + API to controller --- src/controller/controller_api.cpp | 79 ++++++++++++++++++++++++ src/controller/controller_statistics.cpp | 2 + 2 files changed, 81 insertions(+) diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index c376fd82..a96f193e 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -746,6 +746,85 @@ void Controller::handleAPICommands(JSON::Value &Request, JSON::Value &Response){ } } + // Tag handling + if (Request.isMember("tags") && Request["tags"].isArray()){ + //Set entire tag list to new given list, sorted and de-duplicated + Controller::Storage["tags"].shrink(0); + std::set newTags; + jsonForEach(Request["tags"], tag){ + std::string val = tag->asString(); + if (val.size()){newTags.insert(val);} + } + //Set new tags + for (std::set::iterator it = newTags.begin(); it != newTags.end(); ++it){ + Controller::Storage["tags"].append(*it); + } + } + if (Request.isMember("add_tag")){ + //Add given tag(s) to existing list + std::set newTags; + //Get current tags + if (Controller::Storage.isMember("tags") && Controller::Storage["tags"].isArray() && Controller::Storage["tags"].size()){ + jsonForEach(Controller::Storage["tags"], tag){ + std::string val = tag->asString(); + if (val.size()){newTags.insert(val);} + } + } + Controller::Storage["tags"].shrink(0); + if (Request["add_tag"].isString()){ + newTags.insert(Request["add_tag"].asStringRef()); + }else if (Request["add_tag"].isArray()){ + jsonForEach(Request["add_tag"], tag){ + std::string val = tag->asString(); + if (val.size()){newTags.insert(val);} + } + }else if (Request["add_tag"].isObject()){ + jsonForEach(Request["add_tag"], tag){ + std::string val = tag.key(); + if (val.size()){newTags.insert(val);} + } + } + //Set new tags + for (std::set::iterator it = newTags.begin(); it != newTags.end(); ++it){ + Controller::Storage["tags"].append(*it); + } + } + if (Request.isMember("remove_tag")){ + //Remove given tag(s) from existing list + std::set newTags; + //Get current tags + if (Controller::Storage.isMember("tags") && Controller::Storage["tags"].isArray() && Controller::Storage["tags"].size()){ + jsonForEach(Controller::Storage["tags"], tag){ + std::string val = tag->asString(); + if (val.size()){newTags.insert(val);} + } + } + Controller::Storage["tags"].shrink(0); + if (Request["remove_tag"].isString()){ + newTags.erase(Request["remove_tag"].asStringRef()); + }else if (Request["remove_tag"].isArray()){ + jsonForEach(Request["remove_tag"], tag){ + newTags.erase(tag->asString()); + } + }else if (Request["remove_tag"].isObject()){ + jsonForEach(Request["remove_tag"], tag){ + newTags.erase(tag.key()); + } + } + //Set new tags + for (std::set::iterator it = newTags.begin(); it != newTags.end(); ++it){ + Controller::Storage["tags"].append(*it); + } + } + if (Request.isMember("tags") || Request.isMember("add_tag") || Request.isMember("remove_tag")){ + if (Controller::Storage.isMember("tags") && Controller::Storage["tags"].isArray()){ + Response["tags"] = Controller::Storage["tags"]; + }else{ + Response["tags"].append(""); + Response["tags"].shrink(0); + } + } + if (Request.isMember("capabilities")){ Controller::checkCapable(capabilities); Response["capabilities"] = capabilities; diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index d85ee84d..08ad861f 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -1902,6 +1902,8 @@ void Controller::handlePrometheus(HTTP::Parser &H, Socket::Connection &conn, int { tthread::lock_guard guard(Controller::configMutex); + // add tags, if any + if (Storage.isMember("tags") && Storage["tags"].isArray() && Storage["tags"].size()){resp["tags"] = Storage["tags"];} // Loop over connectors const JSON::Value &caps = capabilities["connectors"]; jsonForEachConst(Storage["config"]["protocols"], prtcl){