libzypp: zypp/media/detail/DownloadProgressTracker.cc Source File

libzypp 17.36.6

DownloadProgressTracker.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "DownloadProgressTracker.h"
10
11#include <algorithm>
12
13 namespace zypp::internal {
14
15 void ProgressTracker::updateStats(double dltotal, double dlnow)
16{
17 clock::time_point now = clock::now();
18
19 if ( !_timeStart )
20 _timeStart = _timeLast = now;
21
22 // If called without args (0.0), recompute based on the last values seen
23 if ( dltotal && dltotal != _dnlTotal )
24 _dnlTotal = dltotal;
25
26 if ( dlnow && dlnow != _dnlNow ) {
27 _dnlNow = dlnow;
28 }
29
30 // percentage:
31 if ( _dnlTotal )
32 _dnlPercent = int(_dnlNow * 100 / _dnlTotal);
33
34 // download rates:
35 _drateTotal = _dnlNow / std::max( std::chrono::duration_cast<std::chrono::seconds>(now - *_timeStart).count(), int64_t(1) );
36
37 if ( _timeLast < now )
38 {
39 const auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - *_timeLast).count();
40 if ( duration >= 1 ) {
41 _drateLast = (_dnlNow - _dnlLast) / duration;
42 // start new period
43 _timeLast = now;
44 _dnlLast = _dnlNow;
45 }
46 }
47 else if ( _timeStart == _timeLast )
48 _drateLast = _drateTotal;
49}
50
51}
DownloadProgressTracker.h
zypp::internal
Definition DownloadProgressTracker.cc:13
zypp::internal::ProgressTracker::_timeStart
std::optional< clock::time_point > _timeStart
Start total stats.
Definition DownloadProgressTracker.h:21
zypp::internal::ProgressTracker::_dnlTotal
double _dnlTotal
Bytes to download or 0 if unknown.
Definition DownloadProgressTracker.h:24
zypp::internal::ProgressTracker::_drateLast
double _drateLast
Download rate in last period.
Definition DownloadProgressTracker.h:31
zypp::internal::ProgressTracker::_timeLast
std::optional< clock::time_point > _timeLast
Start last period(~1sec)
Definition DownloadProgressTracker.h:22
zypp::internal::ProgressTracker::_drateTotal
double _drateTotal
Download rate so far.
Definition DownloadProgressTracker.h:30
zypp::internal::ProgressTracker::_dnlLast
double _dnlLast
Bytes downloaded at period start.
Definition DownloadProgressTracker.h:25
zypp::internal::ProgressTracker::_dnlNow
double _dnlNow
Bytes downloaded now.
Definition DownloadProgressTracker.h:26
zypp::internal::ProgressTracker::_dnlPercent
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
Definition DownloadProgressTracker.h:28
zypp::internal::ProgressTracker::updateStats
void updateStats(double dltotal=0.0, double dlnow=0.0)
Definition DownloadProgressTracker.cc:15