2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileStats.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-26 18:38:27 +02:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
2020-06-26 01:24:13 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::storageStatisticsFast> FileStatsFast::get_storage_statistics_fast_object() const {
|
2019-04-26 00:47:25 +02:00
|
|
|
return make_tl_object<td_api::storageStatisticsFast>(size, count, database_size, language_pack_database_size,
|
|
|
|
log_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStats::add(StatByType &by_type, FileType file_type, int64 size) {
|
|
|
|
auto pos = static_cast<size_t>(file_type);
|
2021-09-26 11:44:18 +02:00
|
|
|
CHECK(pos < stat_by_type_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
by_type[pos].size += size;
|
|
|
|
by_type[pos].cnt++;
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:55:08 +01:00
|
|
|
void FileStats::add_impl(const FullFileInfo &info) {
|
2021-09-26 11:44:18 +02:00
|
|
|
if (split_by_owner_dialog_id_) {
|
|
|
|
add(stat_by_owner_dialog_id_[info.owner_dialog_id], info.file_type, info.size);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-09-26 11:44:18 +02:00
|
|
|
add(stat_by_type_, info.file_type, info.size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-03-02 00:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStats::add_copy(const FullFileInfo &info) {
|
|
|
|
add_impl(info);
|
2021-09-26 11:44:18 +02:00
|
|
|
if (need_all_files_) {
|
|
|
|
all_files_.push_back(info);
|
2020-03-02 00:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileStats::add(FullFileInfo &&info) {
|
|
|
|
add_impl(info);
|
2021-09-26 11:44:18 +02:00
|
|
|
if (need_all_files_) {
|
|
|
|
all_files_.push_back(std::move(info));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 11:44:18 +02:00
|
|
|
FileTypeStat FileStats::get_nontemp_stat(const FileStats::StatByType &by_type) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileTypeStat stat;
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (FileType(i) != FileType::Temp) {
|
|
|
|
stat.size += by_type[i].size;
|
|
|
|
stat.cnt += by_type[i].cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stat;
|
|
|
|
}
|
2020-03-02 09:59:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileTypeStat FileStats::get_total_nontemp_stat() const {
|
2021-09-26 11:44:18 +02:00
|
|
|
if (!split_by_owner_dialog_id_) {
|
|
|
|
return get_nontemp_stat(stat_by_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
FileTypeStat stat;
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto &dialog : stat_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto tmp = get_nontemp_stat(dialog.second);
|
|
|
|
stat.size += tmp.size;
|
|
|
|
stat.cnt += tmp.cnt;
|
|
|
|
}
|
|
|
|
return stat;
|
|
|
|
}
|
2020-03-02 09:59:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileStats::apply_dialog_limit(int32 limit) {
|
|
|
|
if (limit == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-26 11:44:18 +02:00
|
|
|
if (!split_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<int64, DialogId>> dialogs;
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto &dialog : stat_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!dialog.first.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int64 size = 0;
|
|
|
|
for (auto &it : dialog.second) {
|
|
|
|
size += it.size;
|
|
|
|
}
|
2020-07-13 22:20:16 +02:00
|
|
|
dialogs.emplace_back(size, dialog.first);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
size_t prefix = dialogs.size();
|
|
|
|
if (prefix > static_cast<size_t>(limit)) {
|
|
|
|
prefix = static_cast<size_t>(limit);
|
|
|
|
}
|
|
|
|
std::partial_sort(dialogs.begin(), dialogs.begin() + prefix, dialogs.end(),
|
|
|
|
[](const auto &x, const auto &y) { return x.first > y.first; });
|
|
|
|
dialogs.resize(prefix);
|
|
|
|
|
2021-09-26 18:38:27 +02:00
|
|
|
apply_dialog_ids(transform(dialogs, [](const auto &dialog) { return dialog.second; }));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-26 18:38:27 +02:00
|
|
|
void FileStats::apply_dialog_ids(const vector<DialogId> &dialog_ids) {
|
|
|
|
std::unordered_set<DialogId, DialogIdHash> all_dialogs(dialog_ids.begin(), dialog_ids.end());
|
2018-12-31 20:04:05 +01:00
|
|
|
StatByType other_stats;
|
|
|
|
bool other_flag = false;
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto it = stat_by_owner_dialog_id_.begin(); it != stat_by_owner_dialog_id_.end();) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (all_dialogs.count(it->first)) {
|
2018-04-23 00:14:49 +02:00
|
|
|
++it;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other_stats[i].size += it->second[i].size;
|
|
|
|
other_stats[i].cnt += it->second[i].cnt;
|
|
|
|
}
|
|
|
|
other_flag = true;
|
2021-09-26 11:44:18 +02:00
|
|
|
it = stat_by_owner_dialog_id_.erase(it);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (other_flag) {
|
2021-09-26 18:38:27 +02:00
|
|
|
DialogId other_dialog_id; // prevents MSVC warning C4709: comma operator within array index expression
|
2021-09-26 11:44:18 +02:00
|
|
|
stat_by_owner_dialog_id_[other_dialog_id] = other_stats;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 11:44:18 +02:00
|
|
|
td_api::object_ptr<td_api::storageStatisticsByChat> FileStats::get_storage_statistics_by_chat_object(
|
|
|
|
DialogId dialog_id, const FileStats::StatByType &stat_by_type_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto stats = make_tl_object<td_api::storageStatisticsByChat>(dialog_id.get(), 0, 0, Auto());
|
2020-06-22 07:43:00 +02:00
|
|
|
FileStats::StatByType aggregated_stats;
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto file_type = narrow_cast<size_t>(get_main_file_type(static_cast<FileType>(i)));
|
2021-09-26 11:44:18 +02:00
|
|
|
aggregated_stats[file_type].size += stat_by_type_[i].size;
|
|
|
|
aggregated_stats[file_type].cnt += stat_by_type_[i].cnt;
|
2020-06-22 07:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
|
|
|
auto size = aggregated_stats[i].size;
|
|
|
|
auto cnt = aggregated_stats[i].cnt;
|
2018-04-27 10:00:56 +02:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
auto file_type = static_cast<FileType>(i);
|
2018-04-27 10:00:56 +02:00
|
|
|
stats->size_ += size;
|
|
|
|
stats->count_ += cnt;
|
|
|
|
stats->by_file_type_.push_back(
|
2020-06-22 03:28:03 +02:00
|
|
|
make_tl_object<td_api::storageStatisticsByFileType>(get_file_type_object(file_type), size, cnt));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::storageStatistics> FileStats::get_storage_statistics_object() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto stats = make_tl_object<td_api::storageStatistics>(0, 0, Auto());
|
2021-09-26 11:44:18 +02:00
|
|
|
if (!split_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
stats->by_chat_.reserve(1);
|
2021-09-26 11:44:18 +02:00
|
|
|
stats->by_chat_.push_back(get_storage_statistics_by_chat_object(DialogId(), stat_by_type_));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-09-26 11:44:18 +02:00
|
|
|
stats->by_chat_.reserve(stat_by_owner_dialog_id_.size());
|
|
|
|
for (auto &by_dialog : stat_by_owner_dialog_id_) {
|
2020-06-22 03:28:03 +02:00
|
|
|
stats->by_chat_.push_back(get_storage_statistics_by_chat_object(by_dialog.first, by_dialog.second));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
std::sort(stats->by_chat_.begin(), stats->by_chat_.end(), [](const auto &x, const auto &y) {
|
|
|
|
if (x->chat_id_ == 0 || y->chat_id_ == 0) {
|
|
|
|
return (x->chat_id_ == 0) < (y->chat_id_ == 0);
|
|
|
|
}
|
|
|
|
return x->size_ > y->size_;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (const auto &by_dialog : stats->by_chat_) {
|
|
|
|
stats->size_ += by_dialog->size_;
|
|
|
|
stats->count_ += by_dialog->count_;
|
|
|
|
}
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2021-09-26 11:44:18 +02:00
|
|
|
vector<DialogId> FileStats::get_dialog_ids() const {
|
|
|
|
vector<DialogId> res;
|
|
|
|
if (!split_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
2021-09-26 11:44:18 +02:00
|
|
|
res.reserve(stat_by_owner_dialog_id_.size());
|
|
|
|
for (auto &by_dialog : stat_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (by_dialog.first.is_valid()) {
|
|
|
|
res.push_back(by_dialog.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-09-26 11:44:18 +02:00
|
|
|
vector<FullFileInfo> FileStats::get_all_files() {
|
|
|
|
return std::move(all_files_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringBuilder &operator<<(StringBuilder &sb, const FileTypeStat &stat) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return sb << tag("size", format::as_size(stat.size)) << tag("count", stat.cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats) {
|
2021-09-26 11:44:18 +02:00
|
|
|
if (!file_stats.split_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileTypeStat total_stat;
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto &type_stat : file_stats.stat_by_type_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
total_stat.size += type_stat.size;
|
|
|
|
total_stat.cnt += type_stat.cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb << "[FileStat " << tag("total", total_stat);
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
2021-09-26 11:44:18 +02:00
|
|
|
sb << tag(get_file_type_name(FileType(i)), file_stats.stat_by_type_[i]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
sb << "]";
|
|
|
|
} else {
|
|
|
|
{
|
|
|
|
FileTypeStat total_stat;
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto &by_type : file_stats.stat_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &type_stat : by_type.second) {
|
|
|
|
total_stat.size += type_stat.size;
|
|
|
|
total_stat.cnt += type_stat.cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb << "[FileStat " << tag("total", total_stat);
|
|
|
|
}
|
2021-09-26 11:44:18 +02:00
|
|
|
for (auto &by_type : file_stats.stat_by_owner_dialog_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileTypeStat dialog_stat;
|
|
|
|
for (auto &type_stat : by_type.second) {
|
|
|
|
dialog_stat.size += type_stat.size;
|
|
|
|
dialog_stat.cnt += type_stat.cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb << "[FileStat " << tag("owner_dialog_id", by_type.first) << tag("total", dialog_stat);
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
2019-01-19 23:59:37 +01:00
|
|
|
sb << tag(get_file_type_name(FileType(i)), by_type.second[i]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
sb << "]";
|
|
|
|
}
|
|
|
|
sb << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
}
|
2020-03-02 09:59:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|