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