1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ProgressData.cc
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/InputStream.h"
15 #include "zypp/base/String.h"
17 #include "zypp/ProgressData.h"
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "Progress"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 // METHOD NAME : ProgressData::report
33 bool ProgressData::report()
35 Date now = Date::now();
37 // compute value and check whether to report it
40 value_type newVal = _d->_val * 100 / ( _d->_max - _d->_min );
42 if ( newVal - _d->_last_val > 10
43 || now - _d->_last_send > 1
44 || ( _d->_last_val == 0 && newVal > 0 )
45 || ( newVal == 100 && _d->_last_val != 100 )
46 || ( newVal != 100 && _d->_last_val == 100 )
47 || _d->_state != RUN /*INIT||END*/ )
49 _d->_last_val = newVal;
53 return true; // skip report, continue per default
57 if ( now - _d->_last_send > 1 || _d->_state != RUN /*INIT||END*/ )
59 _d->_last_val = _d->_val;
63 return true; // skip report, continue per default
67 if ( _d->_state == INIT )
71 // XXX << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(), _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
75 if ( ! _d->_receiver( *this ) )
77 if ( _d->_state != END )
79 WAR << "User request to ABORT pending action. "
80 << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(),
81 _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
83 return false; // aborted by user
86 else if ( _d->_state == END )
88 DBG << str::form( "{#%u|%s}END", numericId(), name().c_str() ) << endl;
91 return true; // continue per default
94 /******************************************************************
96 ** FUNCTION NAME : operator<<
97 ** FUNCTION TYPE : std::ostream &
99 std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
101 if ( obj.hasRange() )
103 return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
104 obj.numericId(), obj.name().c_str(),
105 obj.min(), obj.max(), obj.val(), obj.reportValue() );
107 return str << str::form( "{%u|%s}[-,-](%lld)",
108 obj.numericId(), obj.name().c_str(),
112 /******************************************************************
114 ** FUNCTION NAME : operator<<
115 ** FUNCTION TYPE : std::ostream &
117 ProgressData makeProgressData( const InputStream & input_r )
120 ret.name( input_r.name() );
121 if ( input_r.size() > 0 )
122 ret.range( input_r.size() );
126 CombinedProgressData::CombinedProgressData( ProgressData &pd,
127 ProgressData::value_type weight )
135 bool CombinedProgressData::operator()( const ProgressData &progress )
137 if ( progress.reportAlive() || ( _weight == 0 ) )
140 // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
141 float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
142 // how much the subtask affects the parent task ie: 0,1
143 float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
144 // real increment of the parent task
145 float real_increment = parent_factor*increment;
146 _last_value = progress.val();
147 return _pd.incr( (int)( (_pd.max()-_pd.min()) * real_increment) );
150 /////////////////////////////////////////////////////////////////
152 ///////////////////////////////////////////////////////////////////