<commit_before>/*
 * Copyright 2016 <NAME>, <EMAIL>
 * All rights reserved. Distributed under the terms of the MIT License.
 */

#include "database_functions.h"

#include <boost/log/trivial.hpp>
#include <string>

#include "type/uid.h"
#include "detail/raw_database_functions.h"

using namespace std;

namespace bash
{

namespace database
{

DatabaseFunctionsPtr DatabaseFunctions::Create(::database::detail::SQLiteWrapperInterfacePtr sqlite_wrapper,
                                               ::database::detail::GeneralDatabaseFunctionsInterfacePtr general_database_functions) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::Create: Function call";

  auto raw_database_functions = detail::RawDatabaseFunctions::Create(sqlite_wrapper);

  return Create(raw_database_functions, general_database_functions);
}

DatabaseFunctionsPtr DatabaseFunctions::Create(::bash::database::detail::RawDatabaseFunctionsInterfacePtr raw_database_functions,
                                               ::database::detail::GeneralDatabaseFunctionsInterfacePtr general_database_functions) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::Create: Function call";

  return DatabaseFunctionsPtr(new DatabaseFunctions(raw_database_functions, general_database_functions));
}

void DatabaseFunctions::CreateTables() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CreateTables: Function call";

  raw_database_functions_->CreateTables();
}

void DatabaseFunctions::AddSystemUser(type::UID uid) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddSystemUser: Function call";

  raw_database_functions_->AddSystemUser({uid});
}

::database::type::RowId DatabaseFunctions::GetSystemUserId(type::UID uid) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUserId: Function call";

  return raw_database_functions_->GetSystemUserId({uid});
}

::bash::database::detail::entity::SystemUser DatabaseFunctions::GetSystemUserById(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUserById: Function call";

  return raw_database_functions_->GetSystemUserById(id);
}

::database::type::RowIds DatabaseFunctions::GetSystemUsersIdsFromLogs(::database::type::RowId agent_name_id) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUsersIdsFromLogs: Function call";

  return raw_database_functions_->GetSystemUsersIdsFromLogs(agent_name_id);
}

void DatabaseFunctions::AddCommand(const ::bash::database::type::CommandName &command) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddCommand: Function call";

  return raw_database_functions_->AddCommand(command);
}

::database::type::RowId DatabaseFunctions::GetCommandId(const ::bash::database::type::CommandName &command) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetCommandId: Function call";

  return raw_database_functions_->GetCommandId(command);
}

::database::type::RowIds DatabaseFunctions::GetAllCommandsIds() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAllCommandsIds: Function call";

  return raw_database_functions_->GetAllCommandsIds();
}

::bash::database::type::CommandName DatabaseFunctions::GetCommandNameById(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetCommandNameById: Function call";

  return raw_database_functions_->GetCommandNameById(id);
}

void DatabaseFunctions::AddLog(const ::type::BashLogEntry &log_entry) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddLog: Function call";

  detail::entity::Log raw_log;
  raw_log.agent_name_id = general_database_functions_->GetAgentNameId(log_entry.agent_name);
  raw_log.date_id = general_database_functions_->GetDateId(log_entry.utc_time.GetDate());
  raw_log.time_id = general_database_functions_->GetTimeId(log_entry.utc_time.GetTime());
  raw_log.user_id = GetSystemUserId(log_entry.user_id);
  raw_log.command_id = raw_database_functions_->GetCommandId(log_entry.command);

  raw_database_functions_->AddLog(raw_log);
}

::database::type::RowsCount DatabaseFunctions::CountCommandsForDailySystemStatistic(::database::type::RowId agent_name_id,
                                                                                    ::database::type::RowId date_id,
                                                                                    ::database::type::RowId command_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CountCommandsForDailySystemStatistic: Function call";

  return raw_database_functions_->CountCommandsForDailySystemStatistic(agent_name_id, date_id, command_id);
}

::database::type::RowsCount DatabaseFunctions::CountCommandsForUserDailyStatisticFromLogs(::database::type::RowId agent_name_id,
                                                                                          ::database::type::RowId date_id,
                                                                                          ::database::type::RowId user_id,
                                                                                          ::database::type::RowId command_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CountCommandsForUserDailyStatisticFromLogs: Function call";

  return raw_database_functions_->CountCommandsForUserDailyStatisticFromLogs(agent_name_id, date_id, user_id, command_id);
}

void DatabaseFunctions::AddDailySystemStatistic(const detail::entity::DailySystemStatistic &statistics) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddDailySystemStatistic: Function call";

  raw_database_functions_->AddDailySystemStatistic(statistics);
}

::database::type::RowIds DatabaseFunctions::GetDateIdsWithoutCreatedDailySystemStatistic(::database::type::RowId agent_name_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDateIdsWithoutCreatedDailySystemStatistic: Function call";

  return raw_database_functions_->GetDateIdsWithoutCreatedDailySystemStatistic(agent_name_id);
}

::database::type::RowIds DatabaseFunctions::GetAgentIdsWithoutConfiguration() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentIdsWithoutConfiguration: Function call";

  return raw_database_functions_->GetAgentIdsWithoutConfiguration();
}

::database::type::RowIds DatabaseFunctions::GetAgentsIdsWithConfiguration() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentsIdsWithConfigurations: Function call";

  return raw_database_functions_->GetAgentsIdsWithConfiguration();
}

void DatabaseFunctions::AddDailyUserStatistic(const ::bash::database::detail::entity::DailyUserStatistic &us) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddDailyUserStatistic: Function call";

  raw_database_functions_->AddDailyUserStatistic(us);
}

::database::entity::AgentNames DatabaseFunctions::GetAgentsWithExistingDailyUserStatistics() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentsWithExistingDailyUserStatistics: Function call";

  return raw_database_functions_->GetAgentsWithExistingDailyUserStatistics();
}

::database::type::RowId DatabaseFunctions::GetDailyUserStatisticId(::database::type::RowId agent_name_id,
                                                                   ::database::type::RowId user_id,
                                                                   ::database::type::RowId date_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDailyUserStatisticId: Function call";

  return raw_database_functions_->GetDailyUserStatisticId(agent_name_id, user_id, date_id);
}

void DatabaseFunctions::AddDailyUserCommandStatistic(const ::bash::database::detail::entity::DailyUserCommandStatistic &ucs) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDailyUserStatisticId: Function call";

  return raw_database_functions_->AddDailyUserCommandStatistic(ucs);
}

type::AnomalyDetectionConfigurations DatabaseFunctions::GetAnomalyDetectionConfigurations() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAnomalyDetectionConfigurations: Function call";

  auto raw_configs = raw_database_functions_->GetAnomalyDetectionConfigurations();

  type::AnomalyDetectionConfigurations configs;
  type::AnomalyDetectionConfiguration c;

  for (const auto &config : raw_configs) {
    c.id = config.id;
    c.agent_name_id = config.agent_name_id;
    c.begin_date = general_database_functions_->GetDateById(config.begin_date_id);
    c.end_date = general_database_functions_->GetDateById(config.end_date_id);

    configs.push_back(c);
  }

  return configs;
}

void DatabaseFunctions::RemoveAnomalyDetectionConfiguration(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::RemoveAnomalyDetectionConfiguration: Function call";

  raw_database_functions_->RemoveAnomalyDetectionConfiguration(id);
}

void DatabaseFunctions::AddAnomalyDetectionConfiguration(const type::AnomalyDetectionConfiguration &configuration) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddAnomalyDetectionConfiguration: Function call";<commit_msg>Copy changed value from configuration structure in DatabaseFunctions::GetAnomalyDetectionConfigurations<commit_after>/*
 * Copyright 2016 <NAME>, <EMAIL>
 * All rights reserved. Distributed under the terms of the MIT License.
 */

#include "database_functions.h"

#include <boost/log/trivial.hpp>
#include <string>

#include "type/uid.h"
#include "detail/raw_database_functions.h"

using namespace std;

namespace bash
{

namespace database
{

DatabaseFunctionsPtr DatabaseFunctions::Create(::database::detail::SQLiteWrapperInterfacePtr sqlite_wrapper,
                                               ::database::detail::GeneralDatabaseFunctionsInterfacePtr general_database_functions) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::Create: Function call";

  auto raw_database_functions = detail::RawDatabaseFunctions::Create(sqlite_wrapper);

  return Create(raw_database_functions, general_database_functions);
}

DatabaseFunctionsPtr DatabaseFunctions::Create(::bash::database::detail::RawDatabaseFunctionsInterfacePtr raw_database_functions,
                                               ::database::detail::GeneralDatabaseFunctionsInterfacePtr general_database_functions) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::Create: Function call";

  return DatabaseFunctionsPtr(new DatabaseFunctions(raw_database_functions, general_database_functions));
}

void DatabaseFunctions::CreateTables() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CreateTables: Function call";

  raw_database_functions_->CreateTables();
}

void DatabaseFunctions::AddSystemUser(type::UID uid) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddSystemUser: Function call";

  raw_database_functions_->AddSystemUser({uid});
}

::database::type::RowId DatabaseFunctions::GetSystemUserId(type::UID uid) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUserId: Function call";

  return raw_database_functions_->GetSystemUserId({uid});
}

::bash::database::detail::entity::SystemUser DatabaseFunctions::GetSystemUserById(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUserById: Function call";

  return raw_database_functions_->GetSystemUserById(id);
}

::database::type::RowIds DatabaseFunctions::GetSystemUsersIdsFromLogs(::database::type::RowId agent_name_id) {
  BOOST_LOG_TRIVIAL(debug) << "database::DatabaseFunctions::GetSystemUsersIdsFromLogs: Function call";

  return raw_database_functions_->GetSystemUsersIdsFromLogs(agent_name_id);
}

void DatabaseFunctions::AddCommand(const ::bash::database::type::CommandName &command) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddCommand: Function call";

  return raw_database_functions_->AddCommand(command);
}

::database::type::RowId DatabaseFunctions::GetCommandId(const ::bash::database::type::CommandName &command) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetCommandId: Function call";

  return raw_database_functions_->GetCommandId(command);
}

::database::type::RowIds DatabaseFunctions::GetAllCommandsIds() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAllCommandsIds: Function call";

  return raw_database_functions_->GetAllCommandsIds();
}

::bash::database::type::CommandName DatabaseFunctions::GetCommandNameById(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetCommandNameById: Function call";

  return raw_database_functions_->GetCommandNameById(id);
}

void DatabaseFunctions::AddLog(const ::type::BashLogEntry &log_entry) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddLog: Function call";

  detail::entity::Log raw_log;
  raw_log.agent_name_id = general_database_functions_->GetAgentNameId(log_entry.agent_name);
  raw_log.date_id = general_database_functions_->GetDateId(log_entry.utc_time.GetDate());
  raw_log.time_id = general_database_functions_->GetTimeId(log_entry.utc_time.GetTime());
  raw_log.user_id = GetSystemUserId(log_entry.user_id);
  raw_log.command_id = raw_database_functions_->GetCommandId(log_entry.command);

  raw_database_functions_->AddLog(raw_log);
}

::database::type::RowsCount DatabaseFunctions::CountCommandsForDailySystemStatistic(::database::type::RowId agent_name_id,
                                                                                    ::database::type::RowId date_id,
                                                                                    ::database::type::RowId command_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CountCommandsForDailySystemStatistic: Function call";

  return raw_database_functions_->CountCommandsForDailySystemStatistic(agent_name_id, date_id, command_id);
}

::database::type::RowsCount DatabaseFunctions::CountCommandsForUserDailyStatisticFromLogs(::database::type::RowId agent_name_id,
                                                                                          ::database::type::RowId date_id,
                                                                                          ::database::type::RowId user_id,
                                                                                          ::database::type::RowId command_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::CountCommandsForUserDailyStatisticFromLogs: Function call";

  return raw_database_functions_->CountCommandsForUserDailyStatisticFromLogs(agent_name_id, date_id, user_id, command_id);
}

void DatabaseFunctions::AddDailySystemStatistic(const detail::entity::DailySystemStatistic &statistics) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddDailySystemStatistic: Function call";

  raw_database_functions_->AddDailySystemStatistic(statistics);
}

::database::type::RowIds DatabaseFunctions::GetDateIdsWithoutCreatedDailySystemStatistic(::database::type::RowId agent_name_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDateIdsWithoutCreatedDailySystemStatistic: Function call";

  return raw_database_functions_->GetDateIdsWithoutCreatedDailySystemStatistic(agent_name_id);
}

::database::type::RowIds DatabaseFunctions::GetAgentIdsWithoutConfiguration() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentIdsWithoutConfiguration: Function call";

  return raw_database_functions_->GetAgentIdsWithoutConfiguration();
}

::database::type::RowIds DatabaseFunctions::GetAgentsIdsWithConfiguration() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentsIdsWithConfigurations: Function call";

  return raw_database_functions_->GetAgentsIdsWithConfiguration();
}

void DatabaseFunctions::AddDailyUserStatistic(const ::bash::database::detail::entity::DailyUserStatistic &us) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddDailyUserStatistic: Function call";

  raw_database_functions_->AddDailyUserStatistic(us);
}

::database::entity::AgentNames DatabaseFunctions::GetAgentsWithExistingDailyUserStatistics() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAgentsWithExistingDailyUserStatistics: Function call";

  return raw_database_functions_->GetAgentsWithExistingDailyUserStatistics();
}

::database::type::RowId DatabaseFunctions::GetDailyUserStatisticId(::database::type::RowId agent_name_id,
                                                                   ::database::type::RowId user_id,
                                                                   ::database::type::RowId date_id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDailyUserStatisticId: Function call";

  return raw_database_functions_->GetDailyUserStatisticId(agent_name_id, user_id, date_id);
}

void DatabaseFunctions::AddDailyUserCommandStatistic(const ::bash::database::detail::entity::DailyUserCommandStatistic &ucs) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetDailyUserStatisticId: Function call";

  return raw_database_functions_->AddDailyUserCommandStatistic(ucs);
}

type::AnomalyDetectionConfigurations DatabaseFunctions::GetAnomalyDetectionConfigurations() {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::GetAnomalyDetectionConfigurations: Function call";

  auto raw_configs = raw_database_functions_->GetAnomalyDetectionConfigurations();

  type::AnomalyDetectionConfigurations configs;
  type::AnomalyDetectionConfiguration c;

  for (const auto &config : raw_configs) {
    c.id = config.id;
    c.agent_name_id = config.agent_name_id;
    c.begin_date = general_database_functions_->GetDateById(config.begin_date_id);
    c.end_date = general_database_functions_->GetDateById(config.end_date_id);
    c.changed = config.changed;

    configs.push_back(c);
  }

  return configs;
}

void DatabaseFunctions::RemoveAnomalyDetectionConfiguration(::database::type::RowId id) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::RemoveAnomalyDetectionConfiguration: Function call";

  raw_database_functions_->RemoveAnomalyDetectionConfiguration(id);
}

void DatabaseFunctions::AddAnomalyDetectionConfiguration(const type::AnomalyDetectionConfiguration &configuration) {
  BOOST_LOG_TRIVIAL(debug) << "bash::database::DatabaseFunctions::AddAnomalyDetectionConfiguration: Function call";
{'fraction_non_alphanumeric': 0.11086142322097378, 'fraction_numerical': 0.00042803638309256285, 'mean_word_length': 5.9302929180571, 'pattern_counts': {'":': 0, '<': 111, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6194544', 'n_tokens_mistral': 5176, 'n_tokens_neox': 4938, 'n_words': 638}