diff --git a/server/internal/entities/include/MetricStat.hpp b/server/internal/entities/include/MetricStat.hpp index 8880d309e749016560783b99c37c7bfe0e46c206..69ca521af87ae843ab0c7d46016b69252b8199f2 100644 --- a/server/internal/entities/include/MetricStat.hpp +++ b/server/internal/entities/include/MetricStat.hpp @@ -1,8 +1,52 @@ -// -// Created by qwert on 03.05.2023. -// - #ifndef SOURCEDOUT_METRICSTAT_HPP #define SOURCEDOUT_METRICSTAT_HPP +#include + +class MetricStat { +public: + MetricStat(size_t solutionId, float textBasedRes, float tokenBasedRes, float treeBasedRes, bool verdict, + float meanRes); + + MetricStat(size_t id, size_t solutionId, float textBasedRes, float tokenBasedRes, float treeBasedRes, bool verdict, + float meanRes); + + [[nodiscard]] size_t getId() const; + + void setId(size_t id); + + [[nodiscard]] size_t getSolutionId() const; + + void setSolutionId(size_t solutionId); + + [[nodiscard]] float getTextBasedRes() const; + + void setTextBasedRes(float textBasedRes); + + [[nodiscard]] float getTokenBasedRes() const; + + void setTokenBasedRes(float tokenBasedRes); + + [[nodiscard]] float getTreeBasedRes() const; + + void setTreeBasedRes(float treeBasedRes); + + [[nodiscard]] bool isVerdict() const; + + void setVerdict(bool verdict); + + [[nodiscard]] float getMeanRes() const; + + void setMeanRes(float meanRes); + +private: + size_t id; + size_t solution_id; + float text_based_res; + float token_based_res; + float tree_based_res; + bool verdict; + float mean_res; +}; + #endif //SOURCEDOUT_METRICSTAT_HPP diff --git a/server/internal/entities/include/Solution.hpp b/server/internal/entities/include/Solution.hpp index dcfa255607b382b079f22877977fa8ff54c24cee..5c3d313094c434fd0ef04abffad30e64619aa86e 100644 --- a/server/internal/entities/include/Solution.hpp +++ b/server/internal/entities/include/Solution.hpp @@ -41,6 +41,12 @@ public: void setResult(const std::string &result); + void setId(size_t id); + + bool operator==(const Solution &rhs) const; + + bool operator!=(const Solution &rhs) const; + private: size_t id; std::string send_date; diff --git a/server/internal/entities/include/Task.hpp b/server/internal/entities/include/Task.hpp index a9832ec37d3ce44954f833b50abe00d9232adb93..99d28e625298d9c4b5a19d2de3391433ebf1e422 100644 --- a/server/internal/entities/include/Task.hpp +++ b/server/internal/entities/include/Task.hpp @@ -17,5 +17,11 @@ public: [[nodiscard]] const std::string &getDescription() const; void setDescription(const std::string &description); + + void setId(size_t id); + + bool operator==(const Task &rhs) const; + + bool operator!=(const Task &rhs) const; }; #endif //SOURCEDOUT_TASK_HPP diff --git a/server/internal/entities/include/User.hpp b/server/internal/entities/include/User.hpp index 23a2f137e9ff5c1c5c68c3cd54854f02e7a63af0..d593cf95f1ba4606d0cdbeab42d98935c1e37b96 100644 --- a/server/internal/entities/include/User.hpp +++ b/server/internal/entities/include/User.hpp @@ -32,6 +32,12 @@ public: [[nodiscard]] size_t getId() const; friend std::ostream &operator<<(std::ostream &os, const User &user); + + void setId(size_t id); + + bool operator==(const User &rhs) const; + + bool operator!=(const User &rhs) const; }; #endif //SOURCEDOUT_USER_HPP diff --git a/server/internal/entities/src/MetricStat.cpp b/server/internal/entities/src/MetricStat.cpp index 6427df1b1864a1b339d03b6e5fc74afee6fc180b..268dde6376e86edb5572dc30182d49fc8e78c3a5 100644 --- a/server/internal/entities/src/MetricStat.cpp +++ b/server/internal/entities/src/MetricStat.cpp @@ -1,3 +1,68 @@ -// -// Created by qwert on 03.05.2023. -// + +#include "MetricStat.hpp" + +MetricStat::MetricStat(unsigned long solutionId, float textBasedRes, float tokenBasedRes, float treeBasedRes, + bool verdict, float meanRes) : id(0), solution_id(solutionId), + text_based_res(textBasedRes), token_based_res(tokenBasedRes), + tree_based_res(treeBasedRes), verdict(verdict), mean_res(meanRes) {} + +MetricStat::MetricStat(size_t id, size_t solutionId, float textBasedRes, float tokenBasedRes, float treeBasedRes, + bool verdict, float meanRes) : id(id), solution_id(solutionId), text_based_res(textBasedRes), + token_based_res(tokenBasedRes), tree_based_res(treeBasedRes), + verdict(verdict), mean_res(meanRes) {} + +size_t MetricStat::getId() const { + return id; +} + +void MetricStat::setId(size_t id_) { + id = id_; +} + +size_t MetricStat::getSolutionId() const { + return solution_id; +} + +void MetricStat::setSolutionId(size_t solutionId) { + solution_id = solutionId; +} + +float MetricStat::getTextBasedRes() const { + return text_based_res; +} + +void MetricStat::setTextBasedRes(float textBasedRes) { + text_based_res = textBasedRes; +} + +float MetricStat::getTokenBasedRes() const { + return token_based_res; +} + +void MetricStat::setTokenBasedRes(float tokenBasedRes) { + token_based_res = tokenBasedRes; +} + +float MetricStat::getTreeBasedRes() const { + return tree_based_res; +} + +void MetricStat::setTreeBasedRes(float treeBasedRes) { + tree_based_res = treeBasedRes; +} + +bool MetricStat::isVerdict() const { + return verdict; +} + +void MetricStat::setVerdict(bool verdict_) { + verdict = verdict_; +} + +float MetricStat::getMeanRes() const { + return mean_res; +} + +void MetricStat::setMeanRes(float meanRes) { + mean_res = meanRes; +} diff --git a/server/internal/entities/src/Solution.cpp b/server/internal/entities/src/Solution.cpp index dd8a542a7bf1c09e5c7ff7b081dd1a50bb991f22..331f7e9aa88adbb2b9d5b5ec809836811c61daab 100644 --- a/server/internal/entities/src/Solution.cpp +++ b/server/internal/entities/src/Solution.cpp @@ -15,6 +15,9 @@ size_t Solution::getId() const { return id; } +void Solution::setId(size_t id_) { + id = id_; +} const std::string &Solution::getSendDate() const { return send_date; @@ -71,3 +74,11 @@ const std::string &Solution::getResult() const { void Solution::setResult(const std::string &result_) { Solution::result = result_; } + +bool Solution::operator==(const Solution &rhs) const { + return id == rhs.id; +} + +bool Solution::operator!=(const Solution &rhs) const { + return !(rhs == *this); +} diff --git a/server/internal/entities/src/Task.cpp b/server/internal/entities/src/Task.cpp index 99e0fe634f4a3d28f78cd11511352f6b90616680..56de902680f2d157e8e86dcc0a8dfe85e4b836eb 100644 --- a/server/internal/entities/src/Task.cpp +++ b/server/internal/entities/src/Task.cpp @@ -16,6 +16,18 @@ const std::string &Task::getDescription() const { return description; } +void Task::setId(size_t id_) { + id = id_; +} + void Task::setDescription(const std::string &description_) { Task::description = description_; } + +bool Task::operator==(const Task &rhs) const { + return id == rhs.id; +} + +bool Task::operator!=(const Task &rhs) const { + return !(rhs == *this); +} diff --git a/server/internal/entities/src/User.cpp b/server/internal/entities/src/User.cpp index c048f8f0b920614ef9e17cba52de56c2b614cafe..b6f54d7f5596cd1bf0c114d6002d95c462938101 100644 --- a/server/internal/entities/src/User.cpp +++ b/server/internal/entities/src/User.cpp @@ -12,6 +12,10 @@ const std::string &User::getLogin() const { return login; } +void User::setId(size_t id_) { + id = id_; +} + void User::setLogin(const std::string &login_) { User::login = login_; } @@ -41,3 +45,11 @@ std::ostream &operator<<(std::ostream &os, const User &user) { << user.username; return os; } + +bool User::operator==(const User &rhs) const { + return id == rhs.id; +} + +bool User::operator!=(const User &rhs) const { + return !(rhs == *this); +} diff --git a/server/internal/repository/include/MetricRepository.hpp b/server/internal/repository/include/MetricRepository.hpp index ce611ba040793bc6025c496c855548523a661255..4ece7fea65e98b8cb5671cb49116962846b0a702 100644 --- a/server/internal/repository/include/MetricRepository.hpp +++ b/server/internal/repository/include/MetricRepository.hpp @@ -1,8 +1,29 @@ -// -// Created by qwert on 03.05.2023. -// - #ifndef SOURCEDOUT_METRICREPOSITORY_HPP #define SOURCEDOUT_METRICREPOSITORY_HPP +#include "IMetricRepository.hpp" +#include "dbManager.hpp" +#include + + +using namespace pqxx; + +class MetricRepository : IMetricRepository { +public: + MetricStat getById(size_t id) override; + + size_t storeMetric(MetricStat metric) override; + + void updateMetric(MetricStat metric) override; + + void deleteMetric(MetricStat metric) override; + + void deleteMetricById(size_t id) override; + +private: + std::shared_ptr manager; + + static MetricStat makeMetric(const result::const_iterator &c); +}; + #endif //SOURCEDOUT_METRICREPOSITORY_HPP diff --git a/server/internal/repository/include/SolutionRepository.hpp b/server/internal/repository/include/SolutionRepository.hpp index 5513cbc1f2a390a81edf5ce8c7ab2e5b86df8aba..f0b1c883fe82d0fecdaacb67c9d6ba451c59462a 100644 --- a/server/internal/repository/include/SolutionRepository.hpp +++ b/server/internal/repository/include/SolutionRepository.hpp @@ -11,6 +11,7 @@ using namespace pqxx; class SolutionRepository : ISolutionRepository { +public: Solution getSolutionById(size_t id) override; std::vector getSolutionsBySenderId(size_t sender_id) override; @@ -30,4 +31,4 @@ private: std::shared_ptr manager; }; -#endif //SOURCEDOUT_SOLUTIONREPOSITORY_HPP +#endif diff --git a/server/internal/repository/src/MetricRepository.cpp b/server/internal/repository/src/MetricRepository.cpp index 6427df1b1864a1b339d03b6e5fc74afee6fc180b..0e726616ee68867fe46179713e230bc93a874424 100644 --- a/server/internal/repository/src/MetricRepository.cpp +++ b/server/internal/repository/src/MetricRepository.cpp @@ -1,3 +1,85 @@ -// -// Created by qwert on 03.05.2023. -// +#include "MetricStat.hpp" +#include "MetricRepository.hpp" +#include + +MetricStat MetricRepository::getById(size_t id) { + try { + auto c = manager->connection(); + std::string sql = "SELECT * FROM metricStat WHERE id=" + std::to_string(id); + nontransaction n(*c); + result r(n.exec(sql)); + manager->freeConnection(c); + return makeMetric(r.begin()); + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + throw e; + } +} + + +size_t MetricRepository::storeMetric(MetricStat metric) { + try { + auto c = manager->connection(); + + std::string sql = ( + boost::format( + "INSERT INTO metricStat (solution_id, text_based_res, token_based_res, tree_based_res, verdict, mean_res) " \ + "VALUES ('%s', '%s', '%s', '%s', '%s', '%s') RETURNING id; ") % metric.getSolutionId() % + metric.getTextBasedRes() % metric.getTokenBasedRes() % metric.getTreeBasedRes() % metric.isVerdict() % + metric.getMeanRes()).str(); + work w(*c); + row r = (w.exec1(sql)); + w.commit(); + manager->freeConnection(c); + return r["id"].as(); + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + throw e; + } +} + +void MetricRepository::updateMetric(MetricStat metric) { + try { + auto c = manager->connection(); + + std::string sql = (boost::format( + "UPDATE metricStat SET solution_id = '%s', text_based_res = '%s', token_based_res = '%s', tree_based_res = '%s', verdict = '%s', mean_res = '%s';") + % metric.getSolutionId() % metric.getTextBasedRes() % metric.getTokenBasedRes() % + metric.getTreeBasedRes() % metric.isVerdict() % metric.getMeanRes()).str(); + work w(*c); + w.exec(sql); + manager->freeConnection(c); + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + throw e; + } +} + +void MetricRepository::deleteMetric(MetricStat metric) { + deleteMetricById(metric.getId()); +} + +void MetricRepository::deleteMetricById(size_t id) { + try { + auto c = manager->connection(); + std::string sql = "DELETE FROM metricStat WHERE id=" + std::to_string(id); + work w(*c); + w.exec(sql); + w.commit(); + manager->freeConnection(c); + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + throw e; + } +} + +MetricStat MetricRepository::makeMetric(const result::const_iterator &c) { + return {c.at(c.column_number("id")).as(), + c.at(c.column_number("solution_id")).as(), + c.at(c.column_number("text_based_res")).as(), + c.at(c.column_number("token_based_res")).as(), + c.at(c.column_number("tree_based_res")).as(), + c.at(c.column_number("verdict")).as(), + c.at(c.column_number("mean_res")).as() + }; +} diff --git a/server/internal/repository/src/SolutionRepository.cpp b/server/internal/repository/src/SolutionRepository.cpp index 3c7d0cdafd9dcb138c6752e88ce99abd46a9cb29..3288158770e1a7d4b47726799ac26cd8175aa411 100644 --- a/server/internal/repository/src/SolutionRepository.cpp +++ b/server/internal/repository/src/SolutionRepository.cpp @@ -3,12 +3,13 @@ #include #include "Solution.hpp" #include "SolutionRepository.hpp" + using namespace pqxx; -Solution SolutionRepository::getSolutionById(size_t id) { +Solution SolutionRepository::getSolutionById(size_t id) { try { auto c = manager->connection(); - std::string sql = "SELECT * FROM Users WHERE id=" + std::to_string(id); + std::string sql = "SELECT * FROM solutions WHERE id=" + std::to_string(id); nontransaction n(*c); result r(n.exec(sql)); manager->freeConnection(c); @@ -19,7 +20,7 @@ Solution SolutionRepository::getSolutionById(size_t id) { } } -std::vector SolutionRepository::getSolutionsBySenderId(size_t sender_id) { +std::vector SolutionRepository::getSolutionsBySenderId(size_t sender_id) { try { auto c = manager->connection(); std::string sql = "SELECT * FROM solutions WHERE sender_id=" + std::to_string(sender_id); @@ -27,7 +28,7 @@ std::vector SolutionRepository::getSolutionsBySenderId(size_t sender_i result r(n.exec(sql)); std::vector solutions; manager->freeConnection(c); - for(result::const_iterator k = r.begin(); k != r.end(); ++k) + for (result::const_iterator k = r.begin(); k != r.end(); ++k) solutions.push_back(makeSolution(k)); return solutions; } catch (const std::exception &e) { @@ -36,7 +37,7 @@ std::vector SolutionRepository::getSolutionsBySenderId(size_t sender_i } } -std::vector SolutionRepository::getSolutionsByTaskId(size_t task_id) { +std::vector SolutionRepository::getSolutionsByTaskId(size_t task_id) { try { auto c = manager->connection(); std::string sql = "SELECT * FROM solutions WHERE task_id=" + std::to_string(task_id); @@ -44,7 +45,7 @@ std::vector SolutionRepository::getSolutionsByTaskId(size_t task_id) result r(n.exec(sql)); std::vector solutions; manager->freeConnection(c); - for(result::const_iterator k = r.begin(); k != r.end(); ++k) + for (result::const_iterator k = r.begin(); k != r.end(); ++k) solutions.push_back(makeSolution(k)); return solutions; } catch (const std::exception &e) { @@ -53,12 +54,15 @@ std::vector SolutionRepository::getSolutionsByTaskId(size_t task_id) } } -size_t SolutionRepository::storeSolution(Solution solution) { +size_t SolutionRepository::storeSolution(Solution solution) { try { auto c = manager->connection(); - std::string sql = (boost::format("INSERT INTO solutions (send_date,sender_id, source, task_id, result, tokens, astTree) " \ - "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s') RETURNING id; ") % solution.getSendDate() % solution.getSenderId() % solution.getSource() % solution.getTaskId() % solution.getResult() % solution.getTokens() % solution.getAstTree()).str(); + std::string sql = ( + boost::format("INSERT INTO solutions (send_date,sender_id, source, task_id, result, tokens, astTree) " \ + "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s') RETURNING id; ") % solution.getSendDate() % + solution.getSenderId() % solution.getSource() % solution.getTaskId() % solution.getResult() % + solution.getTokens() % solution.getAstTree()).str(); work w(*c); row r = (w.exec1(sql)); w.commit(); @@ -70,12 +74,15 @@ size_t SolutionRepository::storeSolution(Solution solution) { } } -void SolutionRepository::updateSolution(Solution solution) { +void SolutionRepository::updateSolution(Solution solution) { try { auto c = manager->connection(); - std::string sql = (boost::format("UPDATE solutions SET send_date = '%s', sender_id = '%s', source = '%s', task_id = '%s', result = '%s', tokens = '%s', astTree = '%s';") - % solution.getSendDate() % solution.getSenderId() % solution.getSource() % solution.getTaskId() % solution.getResult() % solution.getTokens() % solution.getAstTree()).str(); + std::string sql = (boost::format( + "UPDATE solutions SET send_date = '%s', sender_id = '%s', source = '%s', task_id = '%s', result = '%s', tokens = '%s', astTree = '%s';") + % solution.getSendDate() % solution.getSenderId() % solution.getSource() % + solution.getTaskId() % solution.getResult() % solution.getTokens() % + solution.getAstTree()).str(); work w(*c); w.exec(sql); manager->freeConnection(c); @@ -85,7 +92,7 @@ void SolutionRepository::updateSolution(Solution solution) { } } -void SolutionRepository::deleteSolutionById(size_t id) { +void SolutionRepository::deleteSolutionById(size_t id) { try { auto c = manager->connection(); std::string sql = "DELETE FROM solutions WHERE id=" + std::to_string(id); @@ -99,12 +106,12 @@ void SolutionRepository::deleteSolutionById(size_t id) { } } -void SolutionRepository::deleteSolution(Solution solution) { +void SolutionRepository::deleteSolution(Solution solution) { deleteSolutionById(solution.getId()); } -Solution SolutionRepository::makeSolution(const result::const_iterator& c){ +Solution SolutionRepository::makeSolution(const result::const_iterator &c) { return {c.at(c.column_number("id")).as(), c.at(c.column_number("send_date")).as(), c.at(c.column_number("sender_id")).as(), diff --git a/server/internal/repository/virtual/IMetricRepository.hpp b/server/internal/repository/virtual/IMetricRepository.hpp index 2c4fcff2be1eb6761287aab47a5b6f50fe1e16e5..f0911b9183dd5a1ff0638cc2f7329bcea8b4f943 100644 --- a/server/internal/repository/virtual/IMetricRepository.hpp +++ b/server/internal/repository/virtual/IMetricRepository.hpp @@ -1,8 +1,21 @@ -// -// Created by qwert on 03.05.2023. -// - #ifndef SOURCEDOUT_IMETRICREPOSITORY_HPP #define SOURCEDOUT_IMETRICREPOSITORY_HPP + +#include +#include "MetricStat.hpp" + +class IMetricRepository { +public: + virtual MetricStat getById(size_t id) = 0; + + virtual size_t storeMetric(MetricStat metric) = 0; + + virtual void updateMetric(MetricStat metric) = 0; + + virtual void deleteMetric(MetricStat metric) = 0; + + virtual void deleteMetricById(size_t id) = 0; +}; + #endif //SOURCEDOUT_IMETRICREPOSITORY_HPP