Fix broken forecast trend attribute in IQVIA (#23454)

This commit is contained in:
Aaron Bach 2019-04-27 09:17:57 -06:00 committed by Andrew Sayre
parent c174b83f54
commit bdc95e76d0

View file

@ -46,6 +46,7 @@ RATING_MAPPING = [{
'maximum': 12
}]
TREND_FLAT = 'Flat'
TREND_INCREASING = 'Increasing'
TREND_SUBSIDING = 'Subsiding'
@ -76,17 +77,18 @@ async def async_setup_platform(
def calculate_trend(indices):
"""Calculate the "moving average" of a set of indices."""
def moving_average(data, samples):
"""Determine the "moving average" (http://tinyurl.com/yaereb3c)."""
ret = np.cumsum(data, dtype=float)
ret[samples:] = ret[samples:] - ret[:-samples]
return ret[samples - 1:] / samples
index_range = np.arange(0, len(indices))
index_array = np.array(indices)
linear_fit = np.polyfit(index_range, index_array, 1)
slope = round(linear_fit[0], 2)
increasing = np.all(np.diff(moving_average(np.array(indices), 4)) > 0)
if increasing:
if slope > 0:
return TREND_INCREASING
return TREND_SUBSIDING
if slope < 0:
return TREND_SUBSIDING
return TREND_FLAT
class ForecastSensor(IQVIAEntity):