libzypp 17.31.23
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 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 else if ( _d->_state == END )
93 {
94 DBG << str::form( "{#%u|%s} END", numericId(), name().c_str() ) << endl;
95 }
96
97 return true; // continue per default
98 }
99
100 /******************************************************************
101 **
102 ** FUNCTION NAME : operator<<
103 ** FUNCTION TYPE : std::ostream &
104 */
105 std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
106 {
107 if ( obj.hasRange() )
108 {
109 return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
110 obj.numericId(), obj.name().c_str(),
111 obj.min(), obj.max(), obj.val(), obj.reportValue() );
112 }
113 return str << str::form( "{%u|%s}[-,-](%lld)",
114 obj.numericId(), obj.name().c_str(),
115 obj.val() );
116 }
117
118 /******************************************************************
119 **
120 ** FUNCTION NAME : operator<<
121 ** FUNCTION TYPE : std::ostream &
122 */
124 {
125 ProgressData ret;
126 ret.name( input_r.name() );
127 if ( input_r.size() > 0 )
128 ret.range( input_r.size() );
129 return ret;
130 }
131
134 : _weight(weight),
135 _last_value(0),
136 _pd(pd)
137 {
138
139 }
140
142 {
143 if ( progress.reportAlive() || ( _weight == 0 ) )
144 return _pd.tick();
145
146 // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
147 float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
148 // how much the subtask affects the parent task ie: 0,1
149 float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
150 // real increment of the parent task
151 float real_increment = parent_factor*increment;
152 _last_value = progress.val();
153 return _pd.incr( (int)( (_pd.max()-_pd.min()) * real_increment) );
154 }
155
157} // 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