Skip to content

Commit

Permalink
Make progressbar display 10 second average download speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sude- committed Apr 27, 2015
1 parent 43ee082 commit 21e6c61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <json/json.h>
#include <ctime>
#include <fstream>
#include <deque>

class Timer
{
Expand Down Expand Up @@ -72,6 +73,7 @@ class Downloader
Timer timer;
Config config;
ProgressBar* progressbar;
std::deque< std::pair<time_t, size_t> > TimeAndSize;
protected:
private:
CURLcode downloadFile(const std::string& url, const std::string& filepath, const std::string& xml_data = std::string(), const std::string& gamename = std::string());
Expand Down
15 changes: 15 additions & 0 deletions src/downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d

CURLcode Downloader::beginDownload()
{
this->TimeAndSize.clear();
this->timer.reset();
CURLcode result = curl_easy_perform(curlhandle);
this->resume_position = 0;
Expand Down Expand Up @@ -1734,6 +1735,20 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
{
downloader->timer.reset();
int iTermWidth = Util::getTerminalWidth();

// 10 second average download speed
// Don't use static value of 10 seconds because update interval depends on when and how often progress callback is called
downloader->TimeAndSize.push_back(std::make_pair(time(NULL), static_cast<size_t>(dlnow)));
if (downloader->TimeAndSize.size() > 100) // 100 * 100ms = 10s
{
downloader->TimeAndSize.pop_front();
time_t time_first = downloader->TimeAndSize.front().first;
size_t size_first = downloader->TimeAndSize.front().second;
time_t time_last = downloader->TimeAndSize.back().first;
size_t size_last = downloader->TimeAndSize.back().second;
rate = (size_last - size_first) / static_cast<double>((time_last - time_first));
}

bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate)));
std::stringstream eta_ss;
if (eta.hours() > 23)
Expand Down

0 comments on commit 21e6c61

Please sign in to comment.