Make generic version of batcher function (#3822)

This commit is contained in:
Kasia
2022-06-17 20:52:20 +02:00
committed by GitHub
parent a83c139154
commit f3f340335f
10 changed files with 316 additions and 259 deletions
@@ -10,6 +10,7 @@
#include "providers/twitch/api/Helix.hpp"
#include "singletons/Toasts.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
#include "widgets/Window.hpp"
#ifdef Q_OS_WIN
@@ -35,15 +36,6 @@ void NotificationController::initialize(Settings &settings, Paths &paths)
this->channelMap[Platform::Twitch].delayedItemsChanged.connect([this] {
this->twitchSetting_.setValue(this->channelMap[Platform::Twitch].raw());
});
/*
for (const QString &channelName : this->mixerSetting_.getValue()) {
this->channelMap[Platform::Mixer].appendItem(channelName);
}
this->channelMap[Platform::Mixer].delayedItemsChanged.connect([this] {
this->mixerSetting_.setValue(
this->channelMap[Platform::Mixer]);
});*/
liveStatusTimer_ = new QTimer();
@@ -128,40 +120,12 @@ NotificationModel *NotificationController::createModel(QObject *parent,
return model;
}
namespace {
// TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing
std::vector<QStringList> getChannelsInBatches(QStringList channels)
{
constexpr int batchSize = 100;
int batchCount = (channels.size() / batchSize) + 1;
std::vector<QStringList> batches;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
QStringList batch;
// I hate you, msvc
int last = (std::min)(batchSize, channels.size() - batchSize * i);
for (int j = 0; j < last; j++)
{
batch.push_back(channels.at(j + (batchSize * i)));
}
batches.emplace_back(batch);
}
return batches;
}
} // namespace
void NotificationController::fetchFakeChannels()
{
qCDebug(chatterinoNotification) << "fetching fake channels";
QStringList channels;
for (std::vector<int>::size_type i = 0;
i != channelMap[Platform::Twitch].raw().size(); i++)
i < channelMap[Platform::Twitch].raw().size(); i++)
{
auto chan = getApp()->twitch->getChannelOrEmpty(
channelMap[Platform::Twitch].raw()[i]);
@@ -170,10 +134,11 @@ void NotificationController::fetchFakeChannels()
channels.push_back(channelMap[Platform::Twitch].raw()[i]);
}
}
for (const auto &batch : getChannelsInBatches(channels))
for (const auto &batch : splitListIntoBatches(channels))
{
getHelix()->fetchStreams(
QStringList(), batch,
{}, batch,
[batch, this](std::vector<HelixStream> streams) {
std::unordered_set<QString> liveStreams;
for (const auto &stream : streams)
@@ -15,7 +15,6 @@ class NotificationModel;
enum class Platform : uint8_t {
Twitch, // 0
// Mixer, // 1
};
class NotificationController final : public Singleton, private QObject
@@ -49,10 +48,6 @@ private:
ChatterinoSetting<std::vector<QString>> twitchSetting_ = {
"/notifications/twitch"};
/*
ChatterinoSetting<std::vector<QString>> mixerSetting_ = {
"/notifications/mixer"};
*/
};
} // namespace chatterino
+2 -26
View File
@@ -17,36 +17,12 @@
#include "providers/twitch/TwitchUser.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "singletons/Emotes.hpp"
#include "util/Helpers.hpp"
#include "util/QStringHash.hpp"
#include "util/RapidjsonHelpers.hpp"
namespace chatterino {
std::vector<QStringList> getEmoteSetBatches(QStringList emoteSetKeys)
{
// splitting emoteSetKeys to batches of 100, because Ivr API endpoint accepts a maximum of 100 emotesets at once
constexpr int batchSize = 100;
int batchCount = (emoteSetKeys.size() / batchSize) + 1;
std::vector<QStringList> batches;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
QStringList batch;
int last = std::min(batchSize, emoteSetKeys.size() - batchSize * i);
for (int j = 0; j < last; j++)
{
batch.push_back(emoteSetKeys.at(j + (batchSize * i)));
}
batches.emplace_back(batch);
}
return batches;
}
TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken,
const QString &oauthClient, const QString &userID)
: Account(ProviderId::Twitch)
@@ -268,7 +244,7 @@ void TwitchAccount::loadUserstateEmotes(std::weak_ptr<Channel> weakChannel)
}
// requesting emotes
auto batches = getEmoteSetBatches(newEmoteSetKeys);
auto batches = splitListIntoBatches(newEmoteSetKeys);
for (int i = 0; i < batches.size(); i++)
{
qCDebug(chatterinoTwitch)
+2 -30
View File
@@ -15,6 +15,7 @@
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
#include "util/Helpers.hpp"
#include "util/PostToThread.hpp"
#include <QMetaEnum>
@@ -26,35 +27,6 @@ using namespace std::chrono_literals;
namespace chatterino {
namespace {
// TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing
template <class T>
std::vector<T> getChannelsInBatches(T channels)
{
constexpr int batchSize = 100;
int batchCount = (channels.size() / batchSize) + 1;
std::vector<T> batches;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
T batch;
// I hate you, msvc
int last = (std::min)(batchSize, channels.size() - batchSize * i);
for (int j = 0; j < last; j++)
{
batch.push_back(channels.at(j + (batchSize * i)));
}
batches.emplace_back(batch);
}
return batches;
}
} // namespace
TwitchIrcServer::TwitchIrcServer()
: whispersChannel(new Channel("/whispers", Channel::Type::TwitchWhispers))
, mentionsChannel(new Channel("/mentions", Channel::Type::TwitchMentions))
@@ -344,7 +316,7 @@ void TwitchIrcServer::bulkRefreshLiveStatus()
});
// iterate over batches of channel IDs
for (const auto &batch : getChannelsInBatches(twitchChans->keys()))
for (const auto &batch : splitListIntoBatches(twitchChans->keys()))
{
getHelix()->fetchStreams(
batch, {},
+30
View File
@@ -3,6 +3,8 @@
#include <QColor>
#include <QString>
#include <cmath>
namespace chatterino {
/**
@@ -37,4 +39,32 @@ QColor getRandomColor(const QString &userId);
QString formatUserMention(const QString &userName, bool isFirstWord,
bool mentionUsersWithComma);
template <typename T>
std::vector<T> splitListIntoBatches(const T &list, int batchSize = 100)
{
std::vector<T> batches;
int batchCount = std::ceil(static_cast<double>(list.size()) / batchSize);
batches.reserve(batchCount);
auto it = list.cbegin();
for (int j = 0; j < batchCount; j++)
{
T batch;
for (int i = 0; i < batchSize && it != list.end(); i++)
{
batch.append(*it);
it++;
}
if (batch.empty())
{
break;
}
batches.emplace_back(std::move(batch));
}
return batches;
}
} // namespace chatterino