2022-07-28 23:56:40 +00:00
|
|
|
// This file Copyright 2007-2022 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
|
|
|
|
|
|
|
#include "transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "stats.h"
|
2022-03-26 03:39:08 +00:00
|
|
|
#include "tr-strbuf.h"
|
2022-07-28 23:56:40 +00:00
|
|
|
#include "utils.h" // for tr_getRatio(), tr_time()
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_session_stats tr_stats::loadOldStats(std::string_view config_dir)
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto ret = tr_session_stats{};
|
2007-11-26 18:55:36 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
auto top = tr_variant{};
|
2022-07-28 23:56:40 +00:00
|
|
|
auto filename = tr_pathbuf{ config_dir, "/stats.json"sv };
|
2022-03-26 03:39:08 +00:00
|
|
|
bool loaded = tr_variantFromFile(&top, TR_VARIANT_PARSE_JSON, filename.sv(), nullptr);
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!loaded)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-26 03:39:08 +00:00
|
|
|
// maybe the user just upgraded from an old version of Transmission
|
|
|
|
// that was still using stats.benc
|
2022-07-28 23:56:40 +00:00
|
|
|
filename.assign(config_dir, "/stats.benc");
|
2022-03-26 03:39:08 +00:00
|
|
|
loaded = tr_variantFromFile(&top, TR_VARIANT_PARSE_BENC, filename.sv(), nullptr);
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (loaded)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
auto i = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_downloaded_bytes, &i))
|
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
ret.downloadedBytes = (uint64_t)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_files_added, &i))
|
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
ret.filesAdded = (uint64_t)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_seconds_active, &i))
|
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
ret.secondsActive = (uint64_t)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_session_count, &i))
|
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
ret.sessionCount = (uint64_t)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_uploaded_bytes, &i))
|
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
ret.uploadedBytes = (uint64_t)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantFree(&top);
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
2022-07-28 23:56:40 +00:00
|
|
|
|
|
|
|
return ret;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
void tr_stats::save() const
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const saveme = cumulative();
|
|
|
|
auto const filename = tr_pathbuf{ config_dir_, "/stats.json"sv };
|
2021-10-23 15:43:15 +00:00
|
|
|
auto top = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&top, 5);
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_variantDictAddInt(&top, TR_KEY_downloaded_bytes, saveme.downloadedBytes);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_files_added, saveme.filesAdded);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_seconds_active, saveme.secondsActive);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_session_count, saveme.sessionCount);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_uploaded_bytes, saveme.uploadedBytes);
|
|
|
|
tr_variantToFile(&top, TR_VARIANT_FMT_JSON, filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
void tr_stats::clear()
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
single_ = old_ = Zero;
|
|
|
|
is_dirty_ = true;
|
|
|
|
start_time_ = tr_time();
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
[[nodiscard]] tr_session_stats tr_stats::current() const
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto ret = single_;
|
|
|
|
ret.secondsActive = time(nullptr) - start_time_;
|
|
|
|
ret.ratio = tr_getRatio(ret.uploadedBytes, ret.downloadedBytes);
|
|
|
|
return ret;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
2022-08-04 13:44:18 +00:00
|
|
|
|
|
|
|
tr_session_stats tr_stats::add(tr_session_stats const& a, tr_session_stats const& b)
|
|
|
|
{
|
|
|
|
auto ret = tr_session_stats{};
|
|
|
|
ret.uploadedBytes = a.uploadedBytes + b.uploadedBytes;
|
|
|
|
ret.downloadedBytes = a.downloadedBytes + b.downloadedBytes;
|
|
|
|
ret.filesAdded = a.filesAdded + b.filesAdded;
|
|
|
|
ret.sessionCount = a.sessionCount + b.sessionCount;
|
|
|
|
ret.secondsActive = a.secondsActive + b.secondsActive;
|
|
|
|
ret.ratio = tr_getRatio(ret.uploadedBytes, ret.downloadedBytes);
|
|
|
|
return ret;
|
|
|
|
}
|