def test_get_average(backlog_min, timeseries): store = {} # type: Dict[str, Tuple[float, float, float]] for idx, (this_time, this_value, expected_average) in enumerate(timeseries): avg = get_average( store, "foo", this_time, this_value, backlog_min, ) assert avg == expected_average, "at [%r]: got %r expected %r" % (idx, avg, expected_average)
def _check_trend( value_store, temp: float, params: TrendComputeDict, output_unit: str, crit_temp: Optional[float], crit_temp_lower: Optional[float], unique_name: str, ) -> Generator[Result, None, None]: trend_range_min = params["period"] this_time = time.time() # current rate since last check rate = get_rate( value_store=value_store, key="temp.%s.delta" % unique_name, time=this_time, value=temp, ) # average trend, initialized with initial temperature value on first check rate_avg = get_average( value_store=value_store, key="temp.%s.trend" % unique_name, time=this_time, value=rate, backlog_minutes=trend_range_min, ) trend = rate_avg * trend_range_min * 60.0 levels_upper_trend = _validate_levels(params.get('trend_levels')) levels_lower_trend = _validate_levels(params.get('trend_levels_lower')) if levels_lower_trend is not None: # GUI representation of this parameter is labelled 'temperature decrease'; the user may input this # as a positive or negative value levels_lower_trend = (abs(levels_lower_trend[0]) * -1, abs(levels_lower_trend[1]) * -1) yield from check_levels( value=trend, levels_upper=levels_upper_trend, levels_lower=levels_lower_trend, label='Temperature trend', render_func=lambda trend: render_temp( trend, output_unit, relative=True, sign=True, ) + temp_unitsym[output_unit] + ' per ' + str(trend_range_min) + ' min', ) if "trend_timeleft" not in params: return limit = crit_temp if trend > 0 else crit_temp_lower if limit is None: # crit levels may not be set return # compute time until temperature limit is reached warn_timeleft_min, crit_timeleft_min = params["trend_timeleft"] if warn_timeleft_min is None or crit_timeleft_min is None: levels_timeleft_sec = None else: levels_timeleft_sec = (warn_timeleft_min * 60.0, crit_timeleft_min * 60.0) diff_to_limit = limit - temp seconds_left = float(diff_to_limit / rate_avg) yield from check_levels( value=seconds_left, levels_lower=levels_timeleft_sec, render_func=timespan, label='Time until temperature limit reached', )