1from datetime import datetime, timedelta 2 3ERROR_STATE_DELTA_DEFAULT = timedelta(minutes=15) 4ERROR_STATE_DELTA_INSTALL = timedelta(minutes=5) 5ERROR_STATE_HOST_PLUGIN_FAILURE = timedelta(minutes=5) 6 7 8class ErrorState(object): 9 def __init__(self, min_timedelta=ERROR_STATE_DELTA_DEFAULT): 10 self.min_timedelta = min_timedelta 11 12 self.count = 0 13 self.timestamp = None 14 15 def incr(self): 16 if self.count == 0: 17 self.timestamp = datetime.utcnow() 18 19 self.count += 1 20 21 def reset(self): 22 self.count = 0 23 self.timestamp = None 24 25 def is_triggered(self): 26 if self.timestamp is None: 27 return False 28 29 delta = datetime.utcnow() - self.timestamp 30 if delta >= self.min_timedelta: 31 return True 32 33 return False 34 35 @property 36 def fail_time(self): 37 if self.timestamp is None: 38 return 'unknown' 39 40 delta = round((datetime.utcnow() - self.timestamp).seconds / 60.0, 2) 41 if delta < 60: 42 return '{0} min'.format(delta) 43 44 delta_hr = round(delta / 60.0, 2) 45 return '{0} hr'.format(delta_hr) 46