<commit_before>/*
** Copyright 2011-2015 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : <EMAIL>
*/

#include <algorithm>
#include <sstream>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/graphite/query.hh"
#include "com/centreon/broker/logging/logging.hh"

using namespace com::centreon::broker;
using namespace com::centreon::broker::graphite;

std::ostream& operator<<(std::ostream& in, QString const& string) {
  in << string.toStdString();
  return (in);
}

/**
 *  Constructor.
 *
 *  @param[in] naming_scheme  The naming scheme to use.
 *  @param[in] type           The type of the query: metric or status.
 *  @param[in] cache          The macro cache.
 */
query::query(
        std::string const& naming_scheme,
        data_type type,
        macro_cache const& cache) :
  _naming_scheme_index(0),
  _type(type),
  _cache(&cache) {
  _compile_naming_scheme(naming_scheme, type);
}

/**
 *  Copy operator.
 *
 *  @param[in] q  The object to copy.
 */
query::query(query const& q)
  : _compiled_naming_scheme(q._compiled_naming_scheme),
    _compiled_getters(q._compiled_getters),
    _type(q._type),
    _cache(q._cache){}

/**
 *  Destructor
 */
query::~query() {}

/**
 *  Assignment operator.
 *
 *  @param[in] q  The object to copy.
 *
 *  @return       A reference to this object.
 */
query& query::operator=(query const& q) {
  if (this != &q) {
    _compiled_naming_scheme = q._compiled_naming_scheme;
    _compiled_getters = q._compiled_getters;
    _type = q._type;
    _cache = q._cache;
  }
  return (*this);
}

/**
 *  Generate the query for a metric.
 *
 *  @param[in] me  The metric.
 *
 *  @return  The query for a metric.
 */
std::string query::generate_metric(storage::metric const& me) {
  if (_type != metric)
    throw (exceptions::msg()
           << "graphite: attempt to generate metric"
              " with a query of the bad type");
  _naming_scheme_index = 0;
  std::ostringstream iss;
  try {
    for (std::vector<void (query::*)(io::data const&, std::ostream&)>::const_iterator
           it(_compiled_getters.begin()),
           end(_compiled_getters.end());
         it != end;
         ++it)
      (this->**it)(me, iss);
  } catch (std::exception const& e) {
    logging::error(logging::medium)
      << "graphite: couldn't generate query for metric "
      << me.metric_id << ":" << e.what();
    return ("");
  }

  iss << (" ") << me.value << " " << me.ctime << "\n";

  return (iss.str());
}

/**
 *  Generate the query for a status.
 *
 *  @param[in] st  The status.
 *
 *  @return  The query for a status.
 */
std::string query::generate_status(storage::status const& st) {
  if (_type != status)
    throw (exceptions::msg()
           << "graphite: attempt to generate status"
              " with a query of the bad type");
  _naming_scheme_index = 0;
  std::ostringstream iss;
  try {
    for (std::vector<void (query::*)(io::data const&, std::ostream&)>::const_iterator
           it(_compiled_getters.begin()),
           end(_compiled_getters.end());
         it != end;
         ++it)
      (this->**it)(st, iss);
  } catch (std::exception const& e) {
    logging::error(logging::medium)
      << "graphite: couldn't generate query for status "
      << st.index_id << ":" << e.what();
    return ("");
  }

  iss << (" ") << st.state << " " << st.ctime << "\n";

  return (iss.str());
}

/**
 *  Compile a naming scheme.
 *
 *  @param[in] naming_scheme  The naming scheme to compile.
 *  @param[in] type           The type of this query.
 */
void query::_compile_naming_scheme(
              std::string const& naming_scheme,
              data_type type) {
  (void)type;
  size_t found_macro = 0;
  size_t end_macro = 0;

  while ((found_macro = naming_scheme.find_first_of('$', found_macro))
           != std::string::npos) {<commit_msg>Graphite: bump error priority for empty queries.<commit_after>/*
** Copyright 2011-2015 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : <EMAIL>
*/

#include <algorithm>
#include <sstream>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/graphite/query.hh"
#include "com/centreon/broker/logging/logging.hh"

using namespace com::centreon::broker;
using namespace com::centreon::broker::graphite;

std::ostream& operator<<(std::ostream& in, QString const& string) {
  in << string.toStdString();
  return (in);
}

/**
 *  Constructor.
 *
 *  @param[in] naming_scheme  The naming scheme to use.
 *  @param[in] type           The type of the query: metric or status.
 *  @param[in] cache          The macro cache.
 */
query::query(
        std::string const& naming_scheme,
        data_type type,
        macro_cache const& cache) :
  _naming_scheme_index(0),
  _type(type),
  _cache(&cache) {
  _compile_naming_scheme(naming_scheme, type);
}

/**
 *  Copy operator.
 *
 *  @param[in] q  The object to copy.
 */
query::query(query const& q)
  : _compiled_naming_scheme(q._compiled_naming_scheme),
    _compiled_getters(q._compiled_getters),
    _type(q._type),
    _cache(q._cache){}

/**
 *  Destructor
 */
query::~query() {}

/**
 *  Assignment operator.
 *
 *  @param[in] q  The object to copy.
 *
 *  @return       A reference to this object.
 */
query& query::operator=(query const& q) {
  if (this != &q) {
    _compiled_naming_scheme = q._compiled_naming_scheme;
    _compiled_getters = q._compiled_getters;
    _type = q._type;
    _cache = q._cache;
  }
  return (*this);
}

/**
 *  Generate the query for a metric.
 *
 *  @param[in] me  The metric.
 *
 *  @return  The query for a metric.
 */
std::string query::generate_metric(storage::metric const& me) {
  if (_type != metric)
    throw (exceptions::msg()
           << "graphite: attempt to generate metric"
              " with a query of the bad type");
  _naming_scheme_index = 0;
  std::ostringstream iss;
  try {
    for (std::vector<void (query::*)(io::data const&, std::ostream&)>::const_iterator
           it(_compiled_getters.begin()),
           end(_compiled_getters.end());
         it != end;
         ++it)
      (this->**it)(me, iss);
  } catch (std::exception const& e) {
    logging::error(logging::high)
      << "graphite: couldn't generate query for metric "
      << me.metric_id << ":" << e.what();
    return ("");
  }

  iss << (" ") << me.value << " " << me.ctime << "\n";

  return (iss.str());
}

/**
 *  Generate the query for a status.
 *
 *  @param[in] st  The status.
 *
 *  @return  The query for a status.
 */
std::string query::generate_status(storage::status const& st) {
  if (_type != status)
    throw (exceptions::msg()
           << "graphite: attempt to generate status"
              " with a query of the bad type");
  _naming_scheme_index = 0;
  std::ostringstream iss;
  try {
    for (std::vector<void (query::*)(io::data const&, std::ostream&)>::const_iterator
           it(_compiled_getters.begin()),
           end(_compiled_getters.end());
         it != end;
         ++it)
      (this->**it)(st, iss);
  } catch (std::exception const& e) {
    logging::error(logging::high)
      << "graphite: couldn't generate query for status "
      << st.index_id << ":" << e.what();
    return ("");
  }

  iss << (" ") << st.state << " " << st.ctime << "\n";

  return (iss.str());
}

/**
 *  Compile a naming scheme.
 *
 *  @param[in] naming_scheme  The naming scheme to compile.
 *  @param[in] type           The type of this query.
 */
void query::_compile_naming_scheme(
              std::string const& naming_scheme,
              data_type type) {
  (void)type;
  size_t found_macro = 0;
  size_t end_macro = 0;

  while ((found_macro = naming_scheme.find_first_of('$', found_macro))
           != std::string::npos) {
{'fraction_non_alphanumeric': 0.1520774046670461, 'fraction_numerical': 0.0038702333523050656, 'mean_word_length': 3.08271375464684, 'pattern_counts': {'":': 4, '<': 101, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '771340', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2830, 'n_words': 770}