From d41a1cf1d45db826b06005af5ca42952c521b223 Mon Sep 17 00:00:00 2001 From: Cedric Caffy <cedric.caffy@cern.ch> Date: Thu, 19 Mar 2020 11:53:13 +0100 Subject: [PATCH] Refactored Statistics get, update, save with reusable code --- statistics/CMakeLists.txt | 7 +- ...ater.cpp => DatabaseStatisticsService.cpp} | 60 +++++++------ ...tter.hpp => DatabaseStatisticsService.hpp} | 34 ++++---- .../DatabaseStatisticsServiceFactory.cpp | 44 ++++++++++ .../DatabaseStatisticsServiceFactory.hpp | 30 +++++++ statistics/MySQLStatisticsService.cpp | 33 ++++++++ statistics/MySQLStatisticsService.hpp | 37 ++++++++ statistics/StatisticsGetter.cpp | 84 ------------------- statistics/StatisticsSaveCmd.cpp | 8 +- statistics/StatisticsService.cpp | 33 ++++++++ statistics/StatisticsService.hpp | 43 ++++++++++ statistics/StatisticsServiceFactory.cpp | 29 +++++++ ...dater.hpp => StatisticsServiceFactory.hpp} | 30 ++----- statistics/StatisticsUpdateCmd.cpp | 9 +- 14 files changed, 321 insertions(+), 160 deletions(-) rename statistics/{StatisticsUpdater.cpp => DatabaseStatisticsService.cpp} (67%) rename statistics/{StatisticsGetter.hpp => DatabaseStatisticsService.hpp} (57%) create mode 100644 statistics/DatabaseStatisticsServiceFactory.cpp create mode 100644 statistics/DatabaseStatisticsServiceFactory.hpp create mode 100644 statistics/MySQLStatisticsService.cpp create mode 100644 statistics/MySQLStatisticsService.hpp delete mode 100644 statistics/StatisticsGetter.cpp create mode 100644 statistics/StatisticsService.cpp create mode 100644 statistics/StatisticsService.hpp create mode 100644 statistics/StatisticsServiceFactory.cpp rename statistics/{StatisticsUpdater.hpp => StatisticsServiceFactory.hpp} (55%) diff --git a/statistics/CMakeLists.txt b/statistics/CMakeLists.txt index 8068ed4c38..a468c9af7a 100644 --- a/statistics/CMakeLists.txt +++ b/statistics/CMakeLists.txt @@ -47,10 +47,13 @@ set (STATISTICS_LIB_SRC_FILES StatisticsSchema.cpp MysqlStatisticsSchema.cpp StatisticsSchemaFactory.cpp - StatisticsUpdater.cpp - StatisticsGetter.cpp Statistics.cpp FileStatistics.cpp + StatisticsService.cpp + DatabaseStatisticsService.cpp + MySQLStatisticsService.cpp + DatabaseStatisticsServiceFactory.cpp + StatisticsServiceFactory.cpp ) add_library (ctastatistics SHARED diff --git a/statistics/StatisticsUpdater.cpp b/statistics/DatabaseStatisticsService.cpp similarity index 67% rename from statistics/StatisticsUpdater.cpp rename to statistics/DatabaseStatisticsService.cpp index 62d7f808a6..fe548cd032 100644 --- a/statistics/StatisticsUpdater.cpp +++ b/statistics/DatabaseStatisticsService.cpp @@ -1,6 +1,6 @@ -/** +/* * The CERN Tape Archive (CTA) project - * Copyright © 2018 CERN + * Copyright (C) 2019 CERN * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,18 +16,18 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "StatisticsUpdater.hpp" -#include "rdbms/Login.hpp" +#include "DatabaseStatisticsService.hpp" + namespace cta { namespace statistics { -StatisticsUpdater::StatisticsUpdater(cta::rdbms::Conn &conn):m_conn(conn) { +DatabaseStatisticsService::DatabaseStatisticsService(cta::rdbms::Conn & databaseConnection):m_conn(databaseConnection) { } -StatisticsUpdater::~StatisticsUpdater() { +DatabaseStatisticsService::~DatabaseStatisticsService() { } -void StatisticsUpdater::updateTapeStatistics() { +void DatabaseStatisticsService::update(){ const char * const sql = "UPDATE TAPE TAPE_TO_UPDATE SET" "(" @@ -92,27 +92,39 @@ void StatisticsUpdater::updateTapeStatistics() { } } -uint64_t StatisticsUpdater::getNbUpdatedTapes() { - return m_nbUpdatedTapes; +void DatabaseStatisticsService::save(const cta::statistics::Statistics& statistics) { + } -std::unique_ptr<StatisticsUpdater> TapeStatisticsUpdaterFactory::create(cta::rdbms::Login::DbType dbType, cta::rdbms::Conn& conn){ - typedef cta::rdbms::Login::DbType DbType; - std::unique_ptr<StatisticsUpdater> ret; - switch(dbType){ - case DbType::DBTYPE_IN_MEMORY: - case DbType::DBTYPE_SQLITE: - case DbType::DBTYPE_MYSQL: - throw cta::exception::Exception("In TapeStatisticsUpdaterFactory::create(), the "+cta::rdbms::Login::dbTypeToString(dbType)+" database type is not supported."); - case DbType::DBTYPE_ORACLE: - case DbType::DBTYPE_POSTGRESQL: - ret.reset(new StatisticsUpdater(conn)); - return std::move(ret); - default: - throw cta::exception::Exception("In TapeStatisticsUpdaterFactory::create(), unknown database type."); +std::unique_ptr<cta::statistics::Statistics> DatabaseStatisticsService::get() { + const char * const sql = + "SELECT " + "VIRTUAL_ORGANIZATION_NAME AS VO," + "SUM(NB_MASTER_FILES) AS TOTAL_MASTER_FILES_VO," + "SUM(MASTER_DATA_IN_BYTES) AS TOTAL_MASTER_DATA_BYTES_VO," + "SUM(NB_COPY_NB_1) AS TOTAL_NB_COPY_1_VO," + "SUM(COPY_NB_1_IN_BYTES) AS TOTAL_NB_COPY_1_BYTES_VO," + "SUM(NB_COPY_NB_GT_1) AS TOTAL_NB_COPY_NB_GT_1_VO," + "SUM(COPY_NB_GT_1_IN_BYTES) AS TOTAL_COPY_NB_GT_1_IN_BYTES_VO " + "FROM " + "TAPE " + "INNER JOIN " + "TAPE_POOL ON TAPE_POOL.TAPE_POOL_ID = TAPE.TAPE_POOL_ID " + "INNER JOIN " + "VIRTUAL_ORGANIZATION ON TAPE_POOL.VIRTUAL_ORGANIZATION_ID = VIRTUAL_ORGANIZATION.VIRTUAL_ORGANIZATION_ID " + "GROUP BY VIRTUAL_ORGANIZATION_NAME"; + try { + auto stmt = m_conn.createStmt(sql); + auto rset = stmt.executeQuery(); + //Build the Statitistics with the result set and return them + Statistics::Builder builder(rset); + return builder.build(); + } catch(cta::exception::Exception &ex) { + ex.getMessage().str(std::string(__PRETTY_FUNCTION__) + ": " + ex.getMessage().str()); + throw; } } -}} +}} \ No newline at end of file diff --git a/statistics/StatisticsGetter.hpp b/statistics/DatabaseStatisticsService.hpp similarity index 57% rename from statistics/StatisticsGetter.hpp rename to statistics/DatabaseStatisticsService.hpp index 749155cf0a..5483c22490 100644 --- a/statistics/StatisticsGetter.hpp +++ b/statistics/DatabaseStatisticsService.hpp @@ -15,32 +15,28 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + #pragma once #include "rdbms/Conn.hpp" #include "rdbms/Login.hpp" #include "Statistics.hpp" +#include "StatisticsService.hpp" -namespace cta { -namespace statistics{ - - /** - * This class interogates the database and returns the statistics - * @param connection the connection to the database - */ -class StatisticsGetter { -public: - StatisticsGetter(cta::rdbms::Conn& connection); - virtual ~StatisticsGetter(); - StatisticsGetter &operator=(const StatisticsGetter& ) = delete; - virtual std::unique_ptr<Statistics> getAllStatistics() const; -private: - cta::rdbms::Conn & m_conn; -}; - -class StatisticsGetterFactory { +namespace cta { namespace statistics { + +class DatabaseStatisticsService: public StatisticsService { public: - static std::unique_ptr<StatisticsGetter> create(cta::rdbms::Conn &conn, cta::rdbms::Login::DbType dbType); + DatabaseStatisticsService(cta::rdbms::Conn &databaseConnection); + DatabaseStatisticsService(const DatabaseStatisticsService& orig) = delete; + DatabaseStatisticsService & operator=(const DatabaseStatisticsService &) = delete; + virtual ~DatabaseStatisticsService(); + + virtual void update() override; + virtual void save(const cta::statistics::Statistics &statistics) override; + virtual std::unique_ptr<cta::statistics::Statistics> get() override; +protected: + cta::rdbms::Conn & m_conn; }; }} diff --git a/statistics/DatabaseStatisticsServiceFactory.cpp b/statistics/DatabaseStatisticsServiceFactory.cpp new file mode 100644 index 0000000000..ff9f13e4df --- /dev/null +++ b/statistics/DatabaseStatisticsServiceFactory.cpp @@ -0,0 +1,44 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "DatabaseStatisticsServiceFactory.hpp" +#include "MySQLStatisticsService.hpp" + +namespace cta { +namespace statistics { +std::unique_ptr<DatabaseStatisticsService> DatabaseStatisticsServiceFactory::create(cta::rdbms::Conn& databaseConnection, cta::rdbms::Login::DbType dbType) { + typedef cta::rdbms::Login::DbType DbType; + std::unique_ptr<DatabaseStatisticsService> ret; + switch(dbType){ + case DbType::DBTYPE_MYSQL: + ret.reset(new MySQLStatisticsService(databaseConnection)); + return std::move(ret); + case DbType::DBTYPE_IN_MEMORY: + case DbType::DBTYPE_SQLITE: + case DbType::DBTYPE_ORACLE: + case DbType::DBTYPE_POSTGRESQL: + ret.reset(new DatabaseStatisticsService(databaseConnection)); + return std::move(ret); + default: + throw cta::exception::Exception("In DatabaseStatisticsServiceFactory::create(), unknown database type."); + } +} + + +}} \ No newline at end of file diff --git a/statistics/DatabaseStatisticsServiceFactory.hpp b/statistics/DatabaseStatisticsServiceFactory.hpp new file mode 100644 index 0000000000..897d0568e0 --- /dev/null +++ b/statistics/DatabaseStatisticsServiceFactory.hpp @@ -0,0 +1,30 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "DatabaseStatisticsService.hpp" +namespace cta { +namespace statistics { + +class DatabaseStatisticsServiceFactory { +public: + static std::unique_ptr<DatabaseStatisticsService> create(cta::rdbms::Conn &databaseConnection, cta::rdbms::Login::DbType dbType); +}; + +}} \ No newline at end of file diff --git a/statistics/MySQLStatisticsService.cpp b/statistics/MySQLStatisticsService.cpp new file mode 100644 index 0000000000..854b44992d --- /dev/null +++ b/statistics/MySQLStatisticsService.cpp @@ -0,0 +1,33 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "MySQLStatisticsService.hpp" + +namespace cta { namespace statistics { +MySQLStatisticsService::MySQLStatisticsService(cta::rdbms::Conn & databaseConnection):DatabaseStatisticsService(databaseConnection) { +} + +MySQLStatisticsService::~MySQLStatisticsService() { +} + +void MySQLStatisticsService::update() { + throw cta::exception::Exception("In MySQLStatisticsService::update() cannot update tape statistics because it is not implemented for MySQL databases"); +} + + +}} \ No newline at end of file diff --git a/statistics/MySQLStatisticsService.hpp b/statistics/MySQLStatisticsService.hpp new file mode 100644 index 0000000000..b7e621437e --- /dev/null +++ b/statistics/MySQLStatisticsService.hpp @@ -0,0 +1,37 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#pragma once +#include "DatabaseStatisticsService.hpp" + +namespace cta { namespace statistics { + +class MySQLStatisticsService: public DatabaseStatisticsService { +public: + MySQLStatisticsService(cta::rdbms::Conn & databaseConnection); + MySQLStatisticsService(const MySQLStatisticsService& orig) = delete; + MySQLStatisticsService & operator=(const MySQLStatisticsService &) = delete; + virtual ~MySQLStatisticsService(); + + void update() override; +private: + +}; + +}} diff --git a/statistics/StatisticsGetter.cpp b/statistics/StatisticsGetter.cpp deleted file mode 100644 index 2b88447179..0000000000 --- a/statistics/StatisticsGetter.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * The CERN Tape Archive (CTA) project - * Copyright (C) 2019 CERN - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "StatisticsGetter.hpp" -#include "rdbms/Login.hpp" -#include "common/make_unique.hpp" - -namespace cta { -namespace statistics{ - - StatisticsGetter::StatisticsGetter(cta::rdbms::Conn& connection):m_conn(connection) { - - } - - - StatisticsGetter::~StatisticsGetter() { - } - - std::unique_ptr<Statistics> StatisticsGetter::getAllStatistics() const { - const char * const sql = - "SELECT " - "VIRTUAL_ORGANIZATION_NAME AS VO," - "SUM(NB_MASTER_FILES) AS TOTAL_MASTER_FILES_VO," - "SUM(MASTER_DATA_IN_BYTES) AS TOTAL_MASTER_DATA_BYTES_VO," - "SUM(NB_COPY_NB_1) AS TOTAL_NB_COPY_1_VO," - "SUM(COPY_NB_1_IN_BYTES) AS TOTAL_NB_COPY_1_BYTES_VO," - "SUM(NB_COPY_NB_GT_1) AS TOTAL_NB_COPY_NB_GT_1_VO," - "SUM(COPY_NB_GT_1_IN_BYTES) AS TOTAL_COPY_NB_GT_1_IN_BYTES_VO " - "FROM " - "TAPE " - "INNER JOIN " - "TAPE_POOL ON TAPE_POOL.TAPE_POOL_ID = TAPE.TAPE_POOL_ID " - "INNER JOIN " - "VIRTUAL_ORGANIZATION ON TAPE_POOL.VIRTUAL_ORGANIZATION_ID = VIRTUAL_ORGANIZATION.VIRTUAL_ORGANIZATION_ID " - "GROUP BY VIRTUAL_ORGANIZATION_NAME"; - try { - auto stmt = m_conn.createStmt(sql); - auto rset = stmt.executeQuery(); - //Build the Statitistics with the result set and return them - Statistics::Builder builder(rset); - return builder.build(); - } catch(cta::exception::Exception &ex) { - ex.getMessage().str(std::string(__PRETTY_FUNCTION__) + ": " + ex.getMessage().str()); - throw; - } - } - - std::unique_ptr<StatisticsGetter> StatisticsGetterFactory::create(cta::rdbms::Conn& conn, cta::rdbms::Login::DbType dbType) { - typedef cta::rdbms::Login::DbType DbType; - std::unique_ptr<StatisticsGetter> ret; - switch(dbType){ - case DbType::DBTYPE_IN_MEMORY: - case DbType::DBTYPE_SQLITE: - case DbType::DBTYPE_MYSQL: - case DbType::DBTYPE_ORACLE: - case DbType::DBTYPE_POSTGRESQL: - ret = cta::make_unique<StatisticsGetter>(conn); - break; - case DbType::DBTYPE_NONE: - throw cta::exception::Exception("In StatisticsGetterFactory::create(): DBTYPE_NONE provided."); - default: - throw cta::exception::Exception("In StatisticsGetterFactory::create(): Unknown database type"); - } - return std::move(ret); - } - -}} - - diff --git a/statistics/StatisticsSaveCmd.cpp b/statistics/StatisticsSaveCmd.cpp index b94a9b630a..3af7dbef9f 100644 --- a/statistics/StatisticsSaveCmd.cpp +++ b/statistics/StatisticsSaveCmd.cpp @@ -21,7 +21,9 @@ #include "StatisticsSaveCmd.hpp" #include "catalogue/SchemaChecker.hpp" #include "StatisticsSchemaFactory.hpp" -#include "StatisticsGetter.hpp" +#include "StatisticsService.hpp" +#include "StatisticsServiceFactory.hpp" + #include <algorithm> namespace cta { @@ -111,8 +113,8 @@ int StatisticsSaveCmd::exceptionThrowingMain(const int argc, char *const *const statisticsChecker->compareTablesLocatedInSchema(); //Compute the statistics - std::unique_ptr<StatisticsGetter> statisticsGetter = StatisticsGetterFactory::create(catalogueConn,loginCatalogue.dbType); - std::unique_ptr<Statistics> statistics = statisticsGetter->getAllStatistics(); + std::unique_ptr<StatisticsService> service = StatisticsServiceFactory::create(catalogueConn,loginCatalogue.dbType); + std::unique_ptr<Statistics> statistics = service->get(); std::cout << *statistics << std::endl; //Insert them into the statistics database return EXIT_SUCCESS; diff --git a/statistics/StatisticsService.cpp b/statistics/StatisticsService.cpp new file mode 100644 index 0000000000..11644a9775 --- /dev/null +++ b/statistics/StatisticsService.cpp @@ -0,0 +1,33 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "StatisticsService.hpp" + +namespace cta { namespace statistics { + +StatisticsService::StatisticsService() { +} + +StatisticsService::~StatisticsService() { +} + +uint64_t StatisticsService::getNbUpdatedTapes() { + return m_nbUpdatedTapes; +} + +}} \ No newline at end of file diff --git a/statistics/StatisticsService.hpp b/statistics/StatisticsService.hpp new file mode 100644 index 0000000000..1287936f33 --- /dev/null +++ b/statistics/StatisticsService.hpp @@ -0,0 +1,43 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "Statistics.hpp" + + +namespace cta { namespace statistics { + +class StatisticsService { +public: + StatisticsService(); + StatisticsService(const StatisticsService& orig) = delete; + StatisticsService & operator=(const StatisticsService &) = delete; + virtual ~StatisticsService(); + + virtual void update() = 0; + virtual void save(const cta::statistics::Statistics &statistics) = 0; + virtual std::unique_ptr<cta::statistics::Statistics> get() = 0; + uint64_t getNbUpdatedTapes(); + +protected: + uint64_t m_nbUpdatedTapes = 0; +}; + +}} + diff --git a/statistics/StatisticsServiceFactory.cpp b/statistics/StatisticsServiceFactory.cpp new file mode 100644 index 0000000000..78c1bd4923 --- /dev/null +++ b/statistics/StatisticsServiceFactory.cpp @@ -0,0 +1,29 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "StatisticsServiceFactory.hpp" +#include "DatabaseStatisticsServiceFactory.hpp" + +namespace cta { namespace statistics { + +std::unique_ptr<StatisticsService> StatisticsServiceFactory::create(cta::rdbms::Conn& connection, cta::rdbms::Login::DbType dbType) { + return DatabaseStatisticsServiceFactory::create(connection,dbType); +} + +}} \ No newline at end of file diff --git a/statistics/StatisticsUpdater.hpp b/statistics/StatisticsServiceFactory.hpp similarity index 55% rename from statistics/StatisticsUpdater.hpp rename to statistics/StatisticsServiceFactory.hpp index ab8bbe45ea..3870f4dc0d 100644 --- a/statistics/StatisticsUpdater.hpp +++ b/statistics/StatisticsServiceFactory.hpp @@ -1,6 +1,6 @@ -/** +/* * The CERN Tape Archive (CTA) project - * Copyright © 2018 CERN + * Copyright (C) 2019 CERN * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,35 +16,17 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#pragma once +#pragma once #include "rdbms/Conn.hpp" #include "rdbms/Login.hpp" +#include "StatisticsService.hpp" namespace cta { namespace statistics { - -class StatisticsUpdater { -public: - StatisticsUpdater(cta::rdbms::Conn &conn); - virtual void updateTapeStatistics(); - virtual uint64_t getNbUpdatedTapes(); - virtual ~StatisticsUpdater(); -protected: - cta::rdbms::Conn &m_conn; - uint64_t m_nbUpdatedTapes = 0; -}; - -//TODO if necessary -class MySQLStatisticsUpdater: public StatisticsUpdater { +class StatisticsServiceFactory { public: - MySQLStatisticsUpdater(cta::rdbms::Conn &conn); - virtual ~MySQLStatisticsUpdater(); - void updateTapeStatistics() override; -}; + static std::unique_ptr<StatisticsService> create(cta::rdbms::Conn &connection, cta::rdbms::Login::DbType dbType); -class TapeStatisticsUpdaterFactory { -public: - static std::unique_ptr<StatisticsUpdater> create(cta::rdbms::Login::DbType dbType, cta::rdbms::Conn &conn); }; }} \ No newline at end of file diff --git a/statistics/StatisticsUpdateCmd.cpp b/statistics/StatisticsUpdateCmd.cpp index 77b79af099..39293bf555 100644 --- a/statistics/StatisticsUpdateCmd.cpp +++ b/statistics/StatisticsUpdateCmd.cpp @@ -22,7 +22,8 @@ #include "catalogue/SchemaChecker.hpp" #include "statistics/StatisticsUpdateCmdLineArgs.hpp" #include "common/Timer.hpp" -#include "StatisticsUpdater.hpp" +#include "StatisticsService.hpp" +#include "StatisticsServiceFactory.hpp" #include <cstdlib> namespace cta { @@ -71,11 +72,11 @@ int StatisticsUpdateCmd::exceptionThrowingMain(const int argc, char *const *cons return EXIT_FAILURE; } - std::unique_ptr<StatisticsUpdater> updater = TapeStatisticsUpdaterFactory::create(loginCatalogue.dbType,catalogueConn); + std::unique_ptr<StatisticsService> service = StatisticsServiceFactory::create(catalogueConn,loginCatalogue.dbType); std::cout<<"Updating tape statistics in the catalogue..."<<std::endl; cta::utils::Timer t; - updater->updateTapeStatistics(); - std::cout<<"Updated catalogue tape statistics in "<<t.secs()<<", "<<updater->getNbUpdatedTapes()<<" tape(s) have been updated"<<std::endl; + service->update(); + std::cout<<"Updated catalogue tape statistics in "<<t.secs()<<", "<<service->getNbUpdatedTapes()<<" tape(s) have been updated"<<std::endl; return EXIT_SUCCESS; } -- GitLab