def _get_prev_states(endpoint): prev_states = endpoint.p_prev_states oldest_state = [] output = NO_DATA if len(prev_states) > 1: oldest_state = prev_states.pop(0) current_state = prev_states.pop() elif len(prev_states) == 1: current_state = oldest_state = prev_states.pop() else: return output output = 'First seen: ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( oldest_state[1])) + ' (' + duration(oldest_state[1]) + ') and put into state: ' + oldest_state[0] + '\n' last_state = oldest_state for state in prev_states: delay = delta(state[1], last_state[1])[0] if delay == 'just now': delay = 'immediately' else: delay += ' later' output += 'then ' + delay + ' it changed into state: ' + state[0] + \ ' (' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(state[1])) + ')\n' last_state = state output += 'Finally it was last seen: ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( current_state[1])) + ' (' + duration(current_state[1]) + ')' return output
def _get_prev_state(endpoint): # TODO needs to be rewritten now that prev_state is no longer a history of state changes prev_state = endpoint.p_prev_state oldest_state = [] output = NO_DATA if prev_state: current_state = oldest_state = prev_state else: return output output = 'First seen: ' + time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( oldest_state[1])) + ' (' + duration( oldest_state[1] ) + ') and put into state: ' + oldest_state[0] + '\n' delay = delta(prev_state[1], oldest_state[1])[0] if delay == 'just now': delay = 'immediately' else: delay += ' later' output += 'then ' + delay + ' it changed into state: ' + prev_state[0] + \ ' (' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(prev_state[1])) + ')\n' output += 'Finally it was last seen: ' + time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( current_state[1])) + ' (' + duration(current_state[1]) + ')' return output
def get_report_template(report_version: str, default_template: str = "index.md"): """ Retrieve and return the most appropriate template based on the chaostoolkit-lib version used when running the experiment. """ env = Environment(loader=PackageLoader('chaosreport', 'template')) env.filters["pretty_date"] = lambda d: str( maya.MayaDT.from_datetime(dateparser.parse(d))) env.globals["pretty_duration"] = lambda d0, d1: date.delta( dateparser.parse(d0), dateparser.parse(d1), words=False)[0] if not report_version: return env.get_template(default_template) templates = [] for name in env.list_templates(["md"]): if name in ["index.md", "header.md"]: continue _, _, v = name.split('_') v, _ = v.rsplit('.md', 1) templates.append((semver.parse_version_info(v), name)) templates = sorted(templates, key=lambda vinfo: vinfo[0]) report_version = report_version.replace('rc1', '-rc1') for (vinfo, name) in templates: if semver.match( report_version, "<={v}".format(v=semver.format_version(**vinfo._asdict()))): return env.get_template(name) # none of the old versions matched, we can use the latest template return env.get_template(default_template)
def training_summary(self): if self.load(TRAINING_FLUID_RESPONSE_SENT_KEY, None) is False: start_weight = self.load(TRAINING_START_WEIGHT_KEY) weigh_in = self.last_weigh_in() if weigh_in["when"] != start_weight["when"]: start_time = self.load(TRAINING_START_TIME_KEY) end_time = self.load(TRAINING_END_TIME_KEY) time = "who knows how" if start_time and end_time: time = delta(start_time, end_time)[0] pounds_difference = weigh_in["weight"] - start_weight["weight"] ounces = pounds_difference * 16 self.say("Last training was %s long, with %s oz fluid loss. Drink up!" % (time, ounces)) self.clear(TRAINING_START_TIME_KEY) self.clear(TRAINING_END_TIME_KEY) self.save(TRAINING_FLUID_RESPONSE_SENT_KEY, True)
def training_summary(self): if self.load(TRAINING_FLUID_RESPONSE_SENT_KEY, None) is False: start_weight = self.load(TRAINING_START_WEIGHT_KEY) weigh_in = self.last_weigh_in() if weigh_in["when"] != start_weight["when"]: start_time = self.load(TRAINING_START_TIME_KEY) end_time = self.load(TRAINING_END_TIME_KEY) time = "who knows how" if start_time and end_time: time = delta(start_time, end_time)[0] pounds_difference = weigh_in["weight"] - start_weight["weight"] ounces = pounds_difference * 16 self.say( "Last training was %s long, with %s oz fluid loss. Drink up!" % (time, ounces)) self.clear(TRAINING_START_TIME_KEY) self.clear(TRAINING_END_TIME_KEY) self.save(TRAINING_FLUID_RESPONSE_SENT_KEY, True)
def update_metrics(n): if r.exists('session_end') and r.exists('timestamp'): start_datetime = datetime.datetime.fromtimestamp( int(r.get('session_start'))) end_datetime = datetime.datetime.fromtimestamp( int(r.get('session_end'))) speed_readings = [float(i) for i in r.lrange('bike_mph', 0, -1)] resistance_readings = [int(i) for i in r.lrange('resistance', 0, -1)] heart_readings = [float(i) for i in r.lrange('heart_bpm', 0, -1)] if len(speed_readings) > 0 and len(heart_readings) > 0: return [ html.H5('Last Session Duration: {}'.format( date.delta(start_datetime, end_datetime)[0]), className='card-text'), html.Br(), html.P('Session Average Bike Speed: {0:0.2f} MPH'.format( sum(speed_readings) / len(speed_readings)), className='card-text'), html.P('Session Max Bike Speed: {0:0.2f} MPH'.format( max(speed_readings)), className='card-text'), html.Br(), html.P('Session Average Resistance: {0:0.2f}'.format( sum(resistance_readings) / len(resistance_readings)), className='card-text'), html.P('Session Max Resistance: {:d}'.format( max(resistance_readings)), className='card-text'), html.Br(), html.P('Session Average Heart Rate: {0:0.2f} BPM'.format( sum(heart_readings) / len(heart_readings)), className='card-text'), html.P('Session Max Heart Rate: {0:0.2f} BPM'.format( max(heart_readings)), className='card-text') ], 'Last session ended: {}'.format(date.duration(end_datetime)) elif r.exists('session_start') and r.exists('timestamp'): start_datetime = datetime.datetime.fromtimestamp( int(r.get('session_start'))) return [ html.H5('Current session started: {}'.format( date.duration(start_datetime)), className='card-title'), html.P('Current Bike Speed: {0:0.2f} MPH'.format( float(r.lindex('bike_mph', 0))), className='card-text'), html.P('Current Resistance: {:d}'.format( int(r.lindex('resistance', 0))), className='card-text'), html.P('Current Heart Rate: {0:0.2f} BPM'.format( float(r.lindex('heart_bpm', 0))), className='card-text'), ], 'Last Update: {}'.format( datetime.datetime.fromtimestamp(int(r.lindex('timestamp', 0))).strftime('%c')) return [ html.P('Waiting to receive data from bike...', className='card-text', style={'fontStyle': 'italic'}) ], [""]
def delta(value1, value2): '''Wrapper for :func:`natural.date.delta`''' return date.delta(value1, value2)