diff --git a/CMakeLists.txt b/CMakeLists.txt index a57fe873629c847fa7dde32d44aa8403cbefaca7..a932d139e6c9050bb4736c04be180e9709dd0cb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -334,6 +334,7 @@ SET( libringclient_LIB_SRCS src/private/threadworker.cpp src/mime.cpp src/smartinfohub.cpp + src/usage_statistics.cpp #Extension src/extensions/presencecollectionextension.cpp diff --git a/src/usage_statistics.cpp b/src/usage_statistics.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14cf81535f89375d08cfc6a18b3f54046995e005 --- /dev/null +++ b/src/usage_statistics.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** + * Copyright (C) 2017 by Savoir-faire Linux * + * Author : Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> * + * Author : Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>* + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser 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 "usage_statistics.h" + +//Qt +#include <QDebug> + +//Accessors + +unsigned +UsageStatistics::totalCount() const { + return m_TotalSeconds; +} + +unsigned +UsageStatistics::totalSeconds() const { + return m_TotalSeconds; +} + +unsigned +UsageStatistics::lastWeekCount() const { + return m_LastWeekCount; +} + +unsigned +UsageStatistics::lastTrimCount() const { + return m_LastTrimCount; +} + +time_t +UsageStatistics::lastUsed() const { + return m_LastUsed; +} + +bool +UsageStatistics::haveCalled() const { + return m_HaveCalled; +} + +//Mutators + +void +UsageStatistics::setHaveCalled() { + m_HaveCalled = true; +} + +/// \brief Update usage using a time range. +/// +/// All values are updated using given <tt>[start, stop]</tt> time range. +/// \a start and \a stop are given in seconds. +/// +/// \param start starting time of usage +/// \param stop ending time of usage, must be greater than \a start +void +UsageStatistics::update(const time_t& start, const time_t& stop) { + ++m_TotalCount; + setLastUsed(start); + m_TotalSeconds += stop - start; + time_t now; + ::time(&now); + if (now - 3600*24*7 < stop) + ++m_LastWeekCount; + if (now - 3600*24*7*15 < stop) + ++m_LastTrimCount; +} + +/// \brief Use this method to update lastUsed time by a new time only if sooner. +/// +/// \return \a true if the update has been effective. +bool +UsageStatistics::setLastUsed(time_t new_time) { + if (new_time > m_LastUsed) { + m_LastUsed = new_time; + return true; + } + return false; +} + +UsageStatistics& UsageStatistics::operator+=(const UsageStatistics& rhs) { + m_TotalCount += rhs.m_TotalCount; + m_LastWeekCount += rhs.m_LastWeekCount; + m_LastTrimCount += rhs.m_LastTrimCount; + m_HaveCalled += rhs.m_TotalCount; // '+=' mean '|=' here + setLastUsed(rhs.m_LastUsed); + return *this; +} diff --git a/src/usage_statistics.h b/src/usage_statistics.h index 7a030643425e357f5a2c237c31e3fff468499cf1..2e3867c0412722e6246494ee14b6933c811e6971 100644 --- a/src/usage_statistics.h +++ b/src/usage_statistics.h @@ -28,35 +28,21 @@ class UsageStatistics public: //Accessors - unsigned totalCount() const { - return m_TotalSeconds; - } + unsigned totalCount() const; - unsigned totalSeconds() const { - return m_TotalSeconds; - } + unsigned totalSeconds() const; - unsigned lastWeekCount() const { - return m_LastWeekCount; - } + unsigned lastWeekCount() const; - unsigned lastTrimCount() const { - return m_LastTrimCount; - } + unsigned lastTrimCount() const; - time_t lastUsed() const { - return m_LastUsed; - } + time_t lastUsed() const; - bool haveCalled() const { - return m_HaveCalled; - } + bool haveCalled() const; //Mutators - void setHaveCalled() { - m_HaveCalled = true; - } + void setHaveCalled(); /// \brief Update usage using a time range. /// @@ -65,40 +51,17 @@ public: /// /// \param start starting time of usage /// \param stop ending time of usage, must be greater than \a start - void update(const time_t& start, const time_t& stop) { - ++m_TotalCount; - setLastUsed(start); - m_TotalSeconds += stop - start; - time_t now; - ::time(&now); - if (now - 3600*24*7 < stop) - ++m_LastWeekCount; - if (now - 3600*24*7*15 < stop) - ++m_LastTrimCount; - } + void update(const time_t& start, const time_t& stop); /// \brief Use this method to update lastUsed time by a new time only if sooner. /// /// \return \a true if the update has been effective. - bool setLastUsed(time_t time) { - if (m_LastUsed < time) { - m_LastUsed = time; - return true; - } - return false; - } + bool setLastUsed(time_t new_time); /// \brief Inplace incrementation of current values by values from another UsageStatistics instance. /// /// \note \a lastUsed time is not incremented but updated using setLastUsed(). - UsageStatistics& operator+=(const UsageStatistics& rhs) { - m_TotalCount += rhs.m_TotalCount; - m_LastWeekCount += rhs.m_LastWeekCount; - m_LastTrimCount += rhs.m_LastTrimCount; - m_HaveCalled += rhs.m_TotalCount; // '+=' mean '|=' here - setLastUsed(rhs.m_LastUsed); - return *this; - } + UsageStatistics& operator+=(const UsageStatistics& rhs); private: unsigned m_TotalCount {0}; ///< cummulated usage in number of time