21 #include "lookup_estimator.h"
23 #include <aspect/logging.h>
24 #include <config/yaml.h>
25 #include <core/threading/mutex.h>
26 #include <core/threading/mutex_locker.h>
28 #include <bsoncxx/builder/basic/document.hpp>
29 #include <bsoncxx/builder/basic/kvp.hpp>
30 #include <bsoncxx/exception/exception.hpp>
32 #include <mongocxx/client.hpp>
33 #include <mongocxx/exception/operation_exception.hpp>
51 const std::string & cfg_prefix,
54 mongo_connection_manager_(mongo_connection_manager),
56 fully_match_args_(config_, cfg_prefix_,
"fully-match-args", true),
57 include_failures_(config, cfg_prefix_,
"include-failures", false),
59 config_->get_string_or_default((std::string(cfg_prefix_) +
"/instance").c_str(),
"default")),
61 config_->get_string_or_default((std::string(cfg_prefix_) +
"/database").c_str(),
"skills")),
62 collection_(config_->get_string_or_default((std::string(cfg_prefix_) +
"/collection").c_str(),
65 mongodb_client_lookup_ = mongo_connection_manager_->
create_client(instance_);
67 "Using instance %s, database %s, collection %s",
81 using bsoncxx::builder::basic::document;
82 using bsoncxx::builder::basic::kvp;
84 document query = get_skill_query(skill);
85 query.append(kvp(
"outcome",
static_cast<int>(SkillerInterface::SkillStatusEnum::S_FINAL)));
86 bsoncxx::stdx::optional<bsoncxx::document::value> found_entry =
87 mongodb_client_lookup_->database(database_)[collection_].find_one(query.view());
88 return found_entry.has_value();
89 }
catch (mongocxx::operation_exception &e) {
91 std::string(
"Error trying to lookup " + skill.
skill_name +
"\n Exception: " + e.what());
92 logger_->
log_error(logger_name_,
"%s", error.c_str());
100 using bsoncxx::builder::basic::document;
101 using bsoncxx::builder::basic::kvp;
104 document query = get_skill_query(skill);
106 query.append(kvp(
"outcome",
static_cast<int>(SkillerInterface::SkillStatusEnum::S_FINAL)));
108 mongocxx::pipeline p{};
109 p.match(query.view());
114 outcome_ = SkillerInterface::SkillStatusEnum::S_FINAL;
120 query.append(kvp(
"outcome", (
int)SkillerInterface::SkillStatusEnum::S_FINAL));
122 mongocxx::cursor sample_cursor =
123 mongodb_client_lookup_->database(database_)[collection_].aggregate(p);
124 auto doc = *(sample_cursor.begin());
126 switch (doc[duration_field_].get_value().type()) {
127 case bsoncxx::type::k_double:
128 res =
static_cast<float>(doc[duration_field_].get_double().value);
130 case bsoncxx::type::k_int32:
131 res =
static_cast<float>(doc[duration_field_].get_int32().value);
135 + bsoncxx::to_string(doc[duration_field_].get_value().type())
136 +
" when looking up skill exec duration.")
139 error_ = doc[
"error"].get_utf8().value.to_string();
142 }
catch (mongocxx::operation_exception &e) {
144 std::string(
"Error for lookup of " + skill.
skill_name +
"\n Exception: " + e.what());
145 logger_->
log_error(logger_name_,
"%s", error.c_str());
150 std::pair<SkillerInterface::SkillStatusEnum, std::string>
153 return make_pair(outcome_, error_);
156 bsoncxx::builder::basic::document
157 LookupEstimator::get_skill_query(
const Skill &skill)
const
159 using bsoncxx::builder::basic::document;
160 using bsoncxx::builder::basic::kvp;
163 document query = document();
164 query.append(kvp(skill_name_field_, skill.
skill_name));
166 for (
const auto &skill_arg : skill.
skill_args) {
167 query.append(kvp(
"args." + skill_arg.first, skill_arg.second));
171 query.append(kvp(
"args." + skill_arg.first, skill_arg.second));
Interface for configuration handling.
Base class for exceptions in Fawkes.
A structured representation of a skill.
std::string skill_name
The name of the skill.
std::unordered_map< std::string, std::string > skill_args
A map of the skill's argument keys to argument values.
An abstract estimator for the execution time of a skill.
std::map< std::string, Skill >::const_iterator active_whitelist_entry_
Points to the whitelist entry that matches the skill to execute.
const std::map< std::string, Skill > whitelist_
Whitelist of skills that the estimator is allowed to process.
T get_property(const Property< T > &property) const
Get the current property value for active_whitelist_entry_.
const float speed_
Config estimator-specific speedup factor.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
LookupEstimator(MongoDBConnCreator *mongo_connection_manager, Configuration *config, const std::string &cfg_prefix, Logger *logger)
Constructor.
float get_execution_time(const Skill &skill) override
Get the estimated execution time for the given skill string.
bool can_provide_exec_time(const Skill &skill) const override
Check if this estimator can give an estimate for a given skill.
std::pair< SkillerInterface::SkillStatusEnum, std::string > execute(const Skill &skill) override
Let the estimator know that we are executing this skill, so it can apply possible side effects.
Interface for a MongoDB connection creator.
virtual mongocxx::client * create_client(const std::string &config_name="")=0
Create a new MongoDB client.
SkillStatusEnum
This determines the current status of skill execution.
Fawkes library namespace.