2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint>
|
2022-07-28 23:56:40 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h" // for tr_session_stats
|
2022-07-28 23:56:40 +00:00
|
|
|
|
|
|
|
// per-session data structure for bandwidth use statistics
|
|
|
|
class tr_stats
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tr_stats(std::string_view config_dir, time_t now)
|
|
|
|
: config_dir_{ config_dir }
|
|
|
|
, start_time_{ now }
|
|
|
|
{
|
|
|
|
single_.sessionCount = 1;
|
2023-05-06 04:11:05 +00:00
|
|
|
old_ = load_old_stats(config_dir_);
|
2022-07-28 23:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~tr_stats()
|
|
|
|
{
|
2023-05-05 20:33:23 +00:00
|
|
|
save();
|
2022-07-28 23:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
[[nodiscard]] tr_session_stats current() const;
|
|
|
|
|
|
|
|
[[nodiscard]] auto cumulative() const
|
|
|
|
{
|
|
|
|
return add(current(), old_);
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void add_uploaded(uint32_t n_bytes) noexcept
|
2022-07-28 23:56:40 +00:00
|
|
|
{
|
|
|
|
single_.uploadedBytes += n_bytes;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void add_downloaded(uint32_t n_bytes) noexcept
|
2022-07-28 23:56:40 +00:00
|
|
|
{
|
|
|
|
single_.downloadedBytes += n_bytes;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void add_file_created() noexcept
|
2022-07-28 23:56:40 +00:00
|
|
|
{
|
|
|
|
++single_.filesAdded;
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:33:23 +00:00
|
|
|
void save() const;
|
2022-07-28 23:56:40 +00:00
|
|
|
|
|
|
|
private:
|
2022-08-04 13:44:18 +00:00
|
|
|
static tr_session_stats add(tr_session_stats const& a, tr_session_stats const& b);
|
2022-07-28 23:56:40 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
static tr_session_stats load_old_stats(std::string_view config_dir);
|
2022-07-28 23:56:40 +00:00
|
|
|
|
|
|
|
std::string const config_dir_;
|
|
|
|
time_t start_time_;
|
|
|
|
|
|
|
|
static constexpr auto Zero = tr_session_stats{ TR_RATIO_NA, 0U, 0U, 0U, 0U, 0U };
|
|
|
|
tr_session_stats single_ = Zero;
|
|
|
|
tr_session_stats old_ = Zero;
|
|
|
|
};
|