diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index cdcfc205..141e630f 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ set(TEST_LIBRARIES if(NOT TIDE_ENABLE_REST_INTERFACE) set(EXCLUDE_FROM_TESTS core/JsonOptionsTests.cpp + core/LoggingUtilityTest.cpp core/RESTCommandTests.cpp ) endif() diff --git a/tests/cpp/core/LoggingUtilityTest.cpp b/tests/cpp/core/LoggingUtilityTest.cpp new file mode 100644 index 00000000..0b923b3f --- /dev/null +++ b/tests/cpp/core/LoggingUtilityTest.cpp @@ -0,0 +1,214 @@ +/*********************************************************************/ +/* Copyright (c) 2016, EPFL/Blue Brain Project */ +/* Pawel Podhajski */ +/* All rights reserved. */ +/* */ +/* Redistribution and use in source and binary forms, with or */ +/* without modification, are permitted provided that the following */ +/* conditions are met: */ +/* */ +/* 1. Redistributions of source code must retain the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer. */ +/* */ +/* 2. Redistributions in binary form must reproduce the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer in the documentation and/or other materials */ +/* provided with the distribution. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ +/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ +/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ +/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ +/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ +/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ +/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ +/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ +/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ +/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ +/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ +/* POSSIBILITY OF SUCH DAMAGE. */ +/* */ +/* The views and conclusions contained in the software and */ +/* documentation are those of the authors and should not be */ +/* interpreted as representing official policies, either expressed */ +/* or implied, of Ecole polytechnique federale de Lausanne. */ +/*********************************************************************/ + +#define BOOST_TEST_MODULE LoggingUtilityTest + +#include +#include +#include +namespace ut = boost::unit_test; + +#include "DisplayGroup.h" +#include "DummyContent.h" +#include "LoggingUtility.h" +#include "rest/RestLogger.h" + +#include + +namespace +{ +const QSize wallSize( 1000, 1000 ); +const std::string regexJson{ +R"(\{ + "event": \{ + "count": 2, + "last_event": "contentWindowAdded", + "last_event_date": "\d{4}-\d{2}-\d{2}\u\d{2}:\d{2}:\d{2}.\d{6}" + \}, + "window": \{ + "accumulated_count": 2, + "count": 2, + "date_set": "\d{4}-\d{2}-\d{2}\u\d{2}:\d{2}:\d{2}.\d{6}" + \} +\} +)" +}; +const std::string defaultJson{ +R"({ + "event": { + "count": 0, + "last_event": "", + "last_event_date": "" + }, + "window": { + "accumulated_count": 0, + "count": 0, + "date_set": "" + } +} +)" +}; +} + +BOOST_AUTO_TEST_CASE( testAccumulatedWindowCount ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + ContentWindowPtr window2 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded, + logger.get(), &LoggingUtility::contentWindowAdded ); + + BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 0); + displayGroup->addContentWindow( window1 ); + BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 1); + + displayGroup->addContentWindow( window2 ); + BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 2); + +} + +BOOST_AUTO_TEST_CASE( testWindowCount ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded, + logger.get(), &LoggingUtility::contentWindowAdded ); + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowRemoved, + logger.get(), &LoggingUtility::contentWindowRemoved ); + BOOST_CHECK( logger.get()->getWindowCount() == 0); + displayGroup->addContentWindow( window1 ); + BOOST_CHECK( logger.get()->getWindowCount() == 1); + displayGroup->removeContentWindow( window1 ); + BOOST_CHECK( logger.get()->getWindowCount() == 0); +} + +BOOST_AUTO_TEST_CASE( testWindowCountDecrement ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + + BOOST_CHECK( logger.get()->getWindowCount() == 0); + displayGroup->addContentWindow( window1 ); + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowRemoved, + logger.get(), &LoggingUtility::contentWindowRemoved ); + displayGroup->removeContentWindow( window1 ); + BOOST_CHECK( logger.get()->getWindowCount() == 0); +} + +BOOST_AUTO_TEST_CASE( testInteractionCounter ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + ContentWindowPtr window2 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + + BOOST_CHECK( logger.get()->getInteractionCount() == 0); + + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded, + logger.get(), &LoggingUtility::contentWindowAdded ); + + displayGroup->addContentWindow( window1 ); + displayGroup->focus( window1->getID()); + BOOST_CHECK( logger.get()->getInteractionCount() == 2); + +} + +BOOST_AUTO_TEST_CASE( testLastInteraction ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + BOOST_CHECK( logger.get()->getLastInteraction() == ""); + + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded, + logger.get(), &LoggingUtility::contentWindowAdded ); + + displayGroup->addContentWindow( window1 ); + displayGroup->focus( window1->getID()); + BOOST_CHECK( logger.get()->getLastInteraction() == "mode changed"); + +} + +BOOST_AUTO_TEST_CASE( testJsonOutput ) +{ + ContentPtr content( new DummyContent ); + DisplayGroupPtr displayGroup( new DisplayGroup( wallSize )); + + ContentWindowPtr window1 = boost::make_shared( content ); + ContentWindowPtr window2 = boost::make_shared( content ); + std::unique_ptr logger = make_unique(); + + QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded, + logger.get(), &LoggingUtility::contentWindowAdded ); + + std::unique_ptr logContent; + logContent.reset( new RestLogger( *(logger.get()) )); + BOOST_CHECK( logContent->toJSON() == defaultJson ); + + displayGroup->addContentWindow( window1 ); + displayGroup->addContentWindow( window2 ); + + std::string logText = logContent->toJSON(); + + boost::regex ip_regex(regexJson); + boost::sregex_iterator it(logText.begin(), logText.end(), ip_regex); + boost::sregex_iterator end; + + std::string outputJson; + for (; it != end; ++it) + outputJson.append( it->str()); + + BOOST_CHECK( logContent->toJSON() == outputJson ); + +} diff --git a/tide/core/CMakeLists.txt b/tide/core/CMakeLists.txt index 28666c04..52fbd9e6 100644 --- a/tide/core/CMakeLists.txt +++ b/tide/core/CMakeLists.txt @@ -59,6 +59,7 @@ list(APPEND TIDECORE_PUBLIC_HEADERS LayoutEngine.h LodTools.h log.h + LoggingUtility.h Markers.h MasterFromWallChannel.h MasterToWallChannel.h @@ -116,6 +117,7 @@ list(APPEND TIDECORE_SOURCES LayoutEngine.cpp LodTools.cpp log.cpp + LoggingUtility.cpp Markers.cpp MasterFromWallChannel.cpp MasterToWallChannel.cpp diff --git a/tide/core/LoggingUtility.cpp b/tide/core/LoggingUtility.cpp new file mode 100644 index 00000000..0902d559 --- /dev/null +++ b/tide/core/LoggingUtility.cpp @@ -0,0 +1,113 @@ + /*********************************************************************/ + /* Copyright (c) 2016, EPFL/Blue Brain Project */ + /* Pawel Podhajski */ + /* All rights reserved. */ + /* */ + /* Redistribution and use in source and binary forms, with or */ + /* without modification, are permitted provided that the following */ + /* conditions are met: */ + /* */ + /* 1. Redistributions of source code must retain the above */ + /* copyright notice, this list of conditions and the following */ + /* disclaimer. */ + /* */ + /* 2. Redistributions in binary form must reproduce the above */ + /* copyright notice, this list of conditions and the following */ + /* disclaimer in the documentation and/or other materials */ + /* provided with the distribution. */ + /* */ + /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ + /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ + /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ + /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ + /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ + /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ + /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ + /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ + /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ + /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ + /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ + /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ + /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ + /* POSSIBILITY OF SUCH DAMAGE. */ + /* */ + /* The views and conclusions contained in the software and */ + /* documentation are those of the authors and should not be */ + /* interpreted as representing official policies, either expressed */ + /* or implied, of Ecole polytechnique federale de Lausanne. */ + /*********************************************************************/ + +#include "LoggingUtility.h" +#include "ContentWindow.h" + +#include + +size_t LoggingUtility::getAccumulatedWindowCount() const +{ + return _windowCounterTotal; +}; + +const QString& LoggingUtility::getCounterModificationTime() const +{ + return _counterModificationTime; +}; + +int LoggingUtility::getInteractionCount() const +{ + return _interactionCounter; +}; + +const QString& LoggingUtility::getLastInteraction() const +{ + return _lastInteraction; +}; + +const QString& LoggingUtility::getLastInteractionTime() const +{ + return _lastInteractionTime; +}; + +size_t LoggingUtility::getWindowCount() const +{ + return _windowCounter; +}; + +void LoggingUtility::contentWindowAdded( ContentWindowPtr contentWindow ) +{ + connect( contentWindow.get(), &ContentWindow::stateChanged, + [this]() { _log( "state changed" ); }); + connect( contentWindow.get(), &ContentWindow::modeChanged, + [this]() { _log( "mode changed" ); }); + + ++_windowCounter; + ++_windowCounterTotal; + _counterModificationTime=_getTimeStamp(); + _log(__func__); +}; + +void LoggingUtility::contentWindowMovedToFront() +{ + _log(__func__); +}; + +void LoggingUtility::contentWindowRemoved() +{ + if (_windowCounter > 0) + --_windowCounter; + _counterModificationTime = _getTimeStamp(); + _log( __func__ ); +}; + +QString LoggingUtility::_getTimeStamp() const +{ + using namespace boost::posix_time; + ptime t = microsec_clock::local_time(); + return QString::fromStdString(to_iso_extended_string(t)); +}; + +void LoggingUtility::_log( const QString& s ) +{ + _lastInteraction = s; + _lastInteractionTime = _getTimeStamp(); + ++_interactionCounter; +}; diff --git a/tide/core/LoggingUtility.h b/tide/core/LoggingUtility.h new file mode 100644 index 00000000..66705fa0 --- /dev/null +++ b/tide/core/LoggingUtility.h @@ -0,0 +1,97 @@ +/*********************************************************************/ +/* Copyright (c) 2016, EPFL/Blue Brain Project */ +/* Pawel Podhajski */ +/* All rights reserved. */ +/* */ +/* Redistribution and use in source and binary forms, with or */ +/* without modification, are permitted provided that the following */ +/* conditions are met: */ +/* */ +/* 1. Redistributions of source code must retain the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer. */ +/* */ +/* 2. Redistributions in binary form must reproduce the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer in the documentation and/or other materials */ +/* provided with the distribution. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ +/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ +/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ +/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ +/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ +/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ +/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ +/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ +/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ +/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ +/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ +/* POSSIBILITY OF SUCH DAMAGE. */ +/* */ +/* The views and conclusions contained in the software and */ +/* documentation are those of the authors and should not be */ +/* interpreted as representing official policies, either expressed */ +/* or implied, of Ecole polytechnique federale de Lausanne. */ +/*********************************************************************/ + +#ifndef LOGGINGUTILITY_H +#define LOGGINGUTILITY_H + +#include + +#include "types.h" + +/** + * Provides information/statistics on application usage. + */ +class LoggingUtility : public QObject +{ + Q_OBJECT + +public: + + /** @return the number of open windows from the start. */ + size_t getAccumulatedWindowCount() const; + + /** @return the timestamp of opening/closing a window */ + const QString& getCounterModificationTime() const; + + /** @return the value of interaction counter. */ + int getInteractionCount() const; + + /** @return the name of last interaction */ + const QString& getLastInteraction() const; + + /** @return the timestamp of last interaction */ + const QString& getLastInteractionTime() const; + + /** @return the number of currently open windows. */ + size_t getWindowCount() const; + +public slots: + + /** Log the event, update the counters and update the timestamp of last interaction */ + void contentWindowAdded( ContentWindowPtr contentWindow ); + + /** Log the event, update the counters and update the timestamp of last interaction */ + void contentWindowMovedToFront(); + + /** Log the event, update the counters and update the timestamp of last interaction */ + void contentWindowRemoved(); + +private: + size_t _windowCounter = 0; + size_t _windowCounterTotal = 0; + QString _counterModificationTime; + QString _lastInteraction; + QString _lastInteractionTime; + size_t _interactionCounter = 0; + + QString _getTimeStamp() const; + void _log( const QString& s ); +}; + +#endif // LOGGINGUTILITY_H diff --git a/tide/master/CMakeLists.txt b/tide/master/CMakeLists.txt index bb30d46d..eb306cc7 100644 --- a/tide/master/CMakeLists.txt +++ b/tide/master/CMakeLists.txt @@ -88,12 +88,14 @@ if(TIDE_ENABLE_REST_INTERFACE) rest/JsonOptions.h rest/RestCommand.h rest/RestInterface.h + rest/RestLogger.h rest/StaticContent.h ) list(APPEND TIDEMASTER_SOURCES rest/JsonOptions.cpp rest/RestCommand.cpp rest/RestInterface.cpp + rest/RestLogger.cpp rest/StaticContent.cpp ) # Servus PUBLIC because servus::Serializable is a base class of public headers diff --git a/tide/master/MasterApplication.cpp b/tide/master/MasterApplication.cpp index 4e5ceb03..1684feb9 100644 --- a/tide/master/MasterApplication.cpp +++ b/tide/master/MasterApplication.cpp @@ -64,6 +64,7 @@ #if TIDE_ENABLE_REST_INTERFACE # include "ContentLoader.h" +# include "LoggingUtility.h" # include "rest/RestInterface.h" #endif @@ -313,6 +314,7 @@ void MasterApplication::_initRestInterface() { _restInterface = make_unique( config_->getWebServicePort(), masterWindow_->getOptions( )); + _logger = make_unique(); connect( _restInterface.get(), &RestInterface::browse, [this]( QString uri ) { @@ -336,5 +338,15 @@ void MasterApplication::_initRestInterface() connect( _restInterface.get(), &RestInterface::clear, [this]() { displayGroup_->clear(); }); + connect( displayGroup_.get(), &DisplayGroup::contentWindowAdded, + _logger.get(), &LoggingUtility::contentWindowAdded ); + + connect( displayGroup_.get(), &DisplayGroup::contentWindowRemoved, + _logger.get(), &LoggingUtility::contentWindowRemoved ); + + connect( displayGroup_.get(), &DisplayGroup::contentWindowMovedToFront, + _logger.get(), &LoggingUtility::contentWindowMovedToFront ); + + _restInterface.get()->setLogger( *(_logger.get()) ); } #endif diff --git a/tide/master/MasterApplication.h b/tide/master/MasterApplication.h index df63b389..040ad608 100644 --- a/tide/master/MasterApplication.h +++ b/tide/master/MasterApplication.h @@ -56,7 +56,7 @@ class PixelStreamWindowManager; class MasterConfiguration; class MultiTouchListener; class RestInterface; - +class LoggingUtility; /** * The main application for the Master process. */ @@ -111,6 +111,7 @@ class MasterApplication : public QApplication #if TIDE_ENABLE_REST_INTERFACE std::unique_ptr _restInterface; + std::unique_ptr _logger; void _initRestInterface(); #endif }; diff --git a/tide/master/rest/RestInterface.cpp b/tide/master/rest/RestInterface.cpp index 3a8e39a8..d9a75a2c 100644 --- a/tide/master/rest/RestInterface.cpp +++ b/tide/master/rest/RestInterface.cpp @@ -41,6 +41,7 @@ #include "JsonOptions.h" #include "RestCommand.h" +#include "RestLogger.h" #include "StaticContent.h" #include @@ -102,6 +103,7 @@ class RestInterface::Impl RestCommand loadCmd{ "tide::load" }; RestCommand saveCmd{ "tide::save" }; JsonOptions options; + std::unique_ptr logContent; }; RestInterface::RestInterface( const int port, OptionsPtr options ) @@ -131,3 +133,9 @@ RestInterface::RestInterface( const int port, OptionsPtr options ) } RestInterface::~RestInterface() {} + +void RestInterface::setLogger( const LoggingUtility& logger ) const +{ + _impl->logContent.reset( new RestLogger( logger )); + _impl->httpServer.register_( *(_impl->logContent.get()) ); +} diff --git a/tide/master/rest/RestInterface.h b/tide/master/rest/RestInterface.h index 4912609c..32d2c941 100644 --- a/tide/master/rest/RestInterface.h +++ b/tide/master/rest/RestInterface.h @@ -41,6 +41,8 @@ #define RESTINTERFACE_H #include "types.h" +#include "LoggingUtility.h" +#include "RestLogger.h" #include #include @@ -71,6 +73,8 @@ class RestInterface : public QObject /** Out-of-line destructor. */ ~RestInterface(); + void setLogger(const LoggingUtility& logger) const; + signals: /** Open a content. */ void open( QString uri ); diff --git a/tide/master/rest/RestLogger.cpp b/tide/master/rest/RestLogger.cpp new file mode 100644 index 00000000..a241ea4a --- /dev/null +++ b/tide/master/rest/RestLogger.cpp @@ -0,0 +1,77 @@ +/*********************************************************************/ +/* Copyright (c) 2016, EPFL/Blue Brain Project */ +/* Pawel Podhajski */ +/* All rights reserved. */ +/* */ +/* Redistribution and use in source and binary forms, with or */ +/* without modification, are permitted provided that the following */ +/* conditions are met: */ +/* */ +/* 1. Redistributions of source code must retain the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer. */ +/* */ +/* 2. Redistributions in binary form must reproduce the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer in the documentation and/or other materials */ +/* provided with the distribution. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ +/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ +/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ +/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ +/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ +/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ +/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ +/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ +/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ +/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ +/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ +/* POSSIBILITY OF SUCH DAMAGE. */ +/* */ +/* The views and conclusions contained in the software and */ +/* documentation are those of the authors and should not be */ +/* interpreted as representing official policies, either expressed */ +/* or implied, of Ecole polytechnique federale de Lausanne. */ +/*********************************************************************/ + +#include "RestLogger.h" + +#include +#include +#include +#include + + +RestLogger::RestLogger( const LoggingUtility& logger ) + : _logger( logger ) +{} + +std::string RestLogger::getTypeName() const +{ + return _name; +} + +std::string RestLogger::_toJSON() const +{ + std::string content; + + QJsonObject obj; + QJsonObject event; + event["last_event"] = _logger.getLastInteraction(); + event["last_event_date"] = _logger.getLastInteractionTime(); + event["count"] = static_cast( _logger.getInteractionCount() ); + obj["event"] = event; + QJsonObject window; + window["count"] = static_cast(_logger.getWindowCount()); + window["date_set"] = _logger.getCounterModificationTime(); + window["accumulated_count"] = static_cast( _logger.getAccumulatedWindowCount() ); + obj["window"] = window; + QJsonDocument doc(obj); + QString strJson( doc.toJson( QJsonDocument::Indented ) ); + content.append( strJson.toStdString() ); + + return content; +} diff --git a/tide/master/rest/RestLogger.h b/tide/master/rest/RestLogger.h new file mode 100644 index 00000000..e49e74d1 --- /dev/null +++ b/tide/master/rest/RestLogger.h @@ -0,0 +1,71 @@ +/*********************************************************************/ +/* Copyright (c) 2016, EPFL/Blue Brain Project */ +/* Pawel Podhajski */ +/* All rights reserved. */ +/* */ +/* Redistribution and use in source and binary forms, with or */ +/* without modification, are permitted provided that the following */ +/* conditions are met: */ +/* */ +/* 1. Redistributions of source code must retain the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer. */ +/* */ +/* 2. Redistributions in binary form must reproduce the above */ +/* copyright notice, this list of conditions and the following */ +/* disclaimer in the documentation and/or other materials */ +/* provided with the distribution. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */ +/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ +/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ +/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */ +/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */ +/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ +/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */ +/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */ +/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ +/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ +/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */ +/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ +/* POSSIBILITY OF SUCH DAMAGE. */ +/* */ +/* The views and conclusions contained in the software and */ +/* documentation are those of the authors and should not be */ +/* interpreted as representing official policies, either expressed */ +/* or implied, of Ecole polytechnique federale de Lausanne. */ +/*********************************************************************/ + +#ifndef RESTLOGGER_H +#define RESTLOGGER_H + +#include // base class + +#include "LoggingUtility.h" + +/** + * Exposes usage statistics content to ZeroEQ http server gathered by LoggingUtility. + */ +class RestLogger : public servus::Serializable +{ +public: + /** + * Construct dynamic content used by REST interface. + * + * @param logger LoggingUtility object which is exposed. + */ + RestLogger( const LoggingUtility& logger ); + + /** @return the string used as an endpoint by REST interface. */ + std::string getTypeName() const final; + +private: + + const std::string _name = "tide::stats"; + const LoggingUtility& _logger ; + + std::string _toJSON() const final; +}; + +#endif