libzypp  17.36.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 
19 using std::endl;
20 
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "Progress"
23 
25 namespace 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 
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  */
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 
134  ProgressData::value_type weight )
135  : _weight(weight),
136  _last_value(0),
137  _pd(pd)
138  {
139 
140  }
141 
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
ProgressData makeProgressData(const InputStream &input_r)
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Helper to create and pass std::istream.
Definition: inputstream.h:56
void min(value_type min_r)
Set new min value.
Definition: progressdata.h:204
long long value_type
Definition: progressdata.h:134
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:37
void max(value_type max_r)
Set new max value.
Definition: progressdata.h:208
bool report()
Send report if necessary.
Definition: progressdata.cc:33
std::streamoff size() const
Size of the input stream (informal).
Definition: inputstream.h:118
bool reportAlive() const
Definition: progressdata.h:316
void range(value_type max_r)
Set new [0,max].
Definition: progressdata.h:216
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
#define WAR
Definition: Logger.h:101
const std::string & name() const
Name of the std::istream.
Definition: inputstream.h:107
Maintain [min,max] and counter (value) for progress counting.
Definition: progressdata.h:131
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: progressdata.h:264
value_type val() const
Definition: progressdata.h:298
const std::string & name() const
Definition: progressdata.h:326
ProgressData::value_type _weight
Definition: progressdata.h:420
value_type reportValue() const
Definition: progressdata.h:322
bool tick()
Leave counter value unchanged (still alive).
Definition: progressdata.h:280
std::chrono::steady_clock::time_point TimePoint
Definition: progressdata.h:148
bool hasRange() const
Definition: progressdata.h:302
void name(const std::string &name_r)
Set counter name.
Definition: progressdata.h:225
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
ProgressData::value_type _last_value
Definition: progressdata.h:421
#define DBG
Definition: Logger.h:99
RWCOW_pointer< Data > _d
Pointer to data.
Definition: progressdata.h:346