libzypp: zypp-core/ui/progressdata.cc Source File

libzypp 17.38.6

progressdata.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <zypp-core/base/Logger.h>
14#include <zypp-core/base/InputStream>
15#include <zypp-core/base/String.h>
16
17#include <zypp-core/ui/ProgressData>
18
19using std::endl;
20
21#undef ZYPP_BASE_LOGGER_LOGGROUP
22#define ZYPP_BASE_LOGGER_LOGGROUP "Progress"
23
25namespace zypp
26{
27
29 //
30 // METHOD NAME : ProgressData::report
31 // METHOD TYPE : void
32 //
33 bool ProgressData::report()
34 {
35 bool forceReport { _d->_state != RUN }; // bsc#1206949: force reporting the INIT||END states
36
37 static constexpr std::chrono::milliseconds minfequency { 1000 };
38 static constexpr std::chrono::milliseconds maxfequency { 100 };
39 Data::TimePoint now { Data::TimePoint::clock::now() };
40 Data::TimePoint::duration elapsed { now - _d->_last_send };
41 if ( not forceReport && elapsed < maxfequency ) {
42 return true; // skip report, continue per default
43 }
44
45 // compute value and check whether to report it
46 if ( hasRange() )
47 {
48 value_type newVal = _d->_val * 100 / ( _d->_max - _d->_min );
49
50 if ( elapsed > minfequency
51 || newVal != _d->_last_val
52 || forceReport )
53 {
54 _d->_last_val = newVal;
55 _d->_last_send = now;
56 }
57 else
58 return true; // skip report, continue per default
59 }
60 else
61 {
62 if ( elapsed > minfequency || forceReport )
63 {
64 _d->_last_val = _d->_val;
65 _d->_last_send = now;
66 }
67 else
68 return true; // skip report, continue per default
69 }
70
71 // now send report
72 if ( _d->_state == INIT )
73 {
74 _d->_state = RUN;
75 DBG << str::form( "{#%u|%s} START", numericId(), name().c_str() ) << endl;
76 }
77 // XXX << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(), _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
78
79 if ( _d->_receiver )
80 {
81 if ( ! _d->_receiver( *this ) )
82 {
83 if ( _d->_state != END )
84 {
85 WAR << "User request to ABORT pending action. "
86 << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(),
87 _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
88 }
89 return false; // aborted by user
90 }
91 }
92
93 if ( _d->_state == END )
94 {
95 DBG << str::form( "{#%u|%s} END", numericId(), name().c_str() ) << endl;
96 }
97
98 return true; // continue per default
99 }
100
101 /******************************************************************
102 **
103 ** FUNCTION NAME : operator<<
104 ** FUNCTION TYPE : std::ostream &
105 */
106 std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
107 {
108 if ( obj.hasRange() )
109 {
110 return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
111 obj.numericId(), obj.name().c_str(),
112 obj.min(), obj.max(), obj.val(), obj.reportValue() );
113 }
114 return str << str::form( "{%u|%s}[-,-](%lld)",
115 obj.numericId(), obj.name().c_str(),
116 obj.val() );
117 }
118
119 /******************************************************************
120 **
121 ** FUNCTION NAME : operator<<
122 ** FUNCTION TYPE : std::ostream &
123 */
124 ProgressData makeProgressData( const InputStream & input_r )
125 {
126 ProgressData ret;
127 ret.name( input_r.name() );
128 if ( input_r.size() > 0 )
129 ret.range( input_r.size() );
130 return ret;
131 }
132
133 CombinedProgressData::CombinedProgressData( ProgressData &pd,
134 ProgressData::value_type weight )
135 : _weight(weight),
136 _last_value(0),
137 _pd(pd)
138 {
139
140 }
141
142 bool CombinedProgressData::operator()( const ProgressData &progress )
143 {
144 if ( progress.reportAlive() || _pd.reportAlive() || ( _weight == 0 ) )
145 return _pd.tick();
146
147 // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
148 float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
149 // how much the subtask affects the parent task ie: 0,1
150 float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
151 // real increment of the parent task
152 float real_increment = parent_factor*increment;
153 _last_value = progress.val();
154 return _pd.incr( (ProgressData::value_type)( (_pd.max()-_pd.min()) * real_increment) );
155 }
156
158} // namespace zypp
Logger.h
DBG
#define DBG
Definition Logger.h:102
WAR
#define WAR
Definition Logger.h:104
String.h
zypp::CombinedProgressData::_pd
ProgressData & _pd
Definition progressdata.h:422
zypp::CombinedProgressData::operator()
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
Definition progressdata.cc:142
zypp::CombinedProgressData::_weight
ProgressData::value_type _weight
Definition progressdata.h:420
zypp::CombinedProgressData::CombinedProgressData
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
Definition progressdata.cc:133
zypp::CombinedProgressData::_last_value
ProgressData::value_type _last_value
Definition progressdata.h:421
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::name
const std::string & name() const
Name of the std::istream.
Definition inputstream.h:107
zypp::InputStream::size
std::streamoff size() const
Size of the input stream (informal).
Definition inputstream.h:118
zypp::ProgressData::Data::TimePoint
std::chrono::steady_clock::time_point TimePoint
Definition progressdata.h:148
zypp::ProgressData
Maintain [min,max] and counter (value) for progress counting.
Definition progressdata.h:132
zypp::ProgressData::value_type
long long value_type
Definition progressdata.h:134
zypp::ProgressData::reportValue
value_type reportValue() const
Definition progressdata.h:322
zypp::ProgressData::reportAlive
bool reportAlive() const
Definition progressdata.h:316
zypp::ProgressData::name
void name(const std::string &name_r)
Set counter name.
Definition progressdata.h:225
zypp::ProgressData::report
bool report()
Send report if necessary.
Definition progressdata.cc:33
zypp::ProgressData::RUN
@ RUN
Definition progressdata.h:143
zypp::ProgressData::END
@ END
Definition progressdata.h:143
zypp::ProgressData::INIT
@ INIT
Definition progressdata.h:143
zypp::ProgressData::max
void max(value_type max_r)
Set new max value.
Definition progressdata.h:208
zypp::ProgressData::min
void min(value_type min_r)
Set new min value.
Definition progressdata.h:204
zypp::ProgressData::name
const std::string & name() const
Definition progressdata.h:326
zypp::ProgressData::hasRange
bool hasRange() const
Definition progressdata.h:302
zypp::ProgressData::val
value_type val() const
Definition progressdata.h:298
zypp::ProgressData::_d
RWCOW_pointer< Data > _d
Pointer to data.
Definition progressdata.h:346
zypp::ProgressData::range
void range(value_type max_r)
Set new [0,max].
Definition progressdata.h:216
str
String related utilities and Regular expression matching.
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::makeProgressData
ProgressData makeProgressData(const InputStream &input_r)
relates: ProgressData Setup from InputStream.
Definition progressdata.cc:124
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::base::ProvideNumericId< ProgressData, unsigned >::numericId
unsigned numericId() const
Definition providenumericid.h:49