Example #1
0
 def _update_state():
     """Update state of a sub expr."""
     data_sub = expr_data['data'][expr.fmtd_sub_expr_str]
     data_list = data_sub['metrics']
     period = float(expr.period)
     periods = int(expr.periods)
     right = t_now
     left = right - period
     temp_data = []
     value_in_periods = []
     i = len(data_list) - 1
     while i >= 0:
         if data_list[i]['timestamp'] >= left:
             temp_data.append(float(data_list[i]['value']))
         else:
             value = calculator.calc_value(expr.normalized_func,
                                           temp_data)
             value_in_periods.append(value)
             right = left
             left = right - period
             temp_data = []
             i += 1
         i -= 1
     value = calculator.calc_value(expr.normalized_func, temp_data)
     value_in_periods.append(value)
     for i in range(len(value_in_periods), periods, 1):
         value_in_periods.append(
             calculator.calc_value(expr.normalized_func, []))
     expr_data['data'][expr.fmtd_sub_expr_str]['values'] = (
         value_in_periods)
     expr_data['data'][expr.fmtd_sub_expr_str]['state'] = (
         calculator.compare_thresh(value_in_periods,
                                   expr.normalized_operator,
                                   float(expr.threshold)))
Example #2
0
 def _update_state():
     """Update state of a sub expr."""
     data_sub = expr_data["data"][expr.fmtd_sub_expr_str]
     data_list = data_sub["metrics"]
     period = float(expr.period)
     periods = int(expr.periods)
     right = t_now
     left = right - period
     temp_data = []
     value_in_periods = []
     i = len(data_list) - 1
     while i >= 0:
         if data_list[i]["timestamp"] >= left:
             temp_data.append(float(data_list[i]["value"]))
         else:
             value = calculator.calc_value(expr.normalized_func, temp_data)
             value_in_periods.append(value)
             right = left
             left = right - period
             temp_data = []
             i += 1
         i -= 1
     value = calculator.calc_value(expr.normalized_func, temp_data)
     value_in_periods.append(value)
     for i in range(len(value_in_periods), periods, 1):
         value_in_periods.append(calculator.calc_value(expr.normalized_func, []))
     expr_data["data"][expr.fmtd_sub_expr_str]["values"] = value_in_periods
     expr_data["data"][expr.fmtd_sub_expr_str]["state"] = calculator.compare_thresh(
         value_in_periods, expr.normalized_operator, float(expr.threshold)
     )
 def test_calc_value(self):
     self.assertEqual(0, calculator.calc_value('MAX', [0]))
     data = []
     self.assertEqual(None, calculator.calc_value('MAX', data))
     random.seed(time.time())
     for i in range(0, 30, 1):
         data.append(random.uniform(0, 1000))
     self.assertEqual(max(data), calculator.calc_value('MAX', data))
     self.assertEqual(sum(data), calculator.calc_value('SUM', data))
     self.assertEqual(len(data), calculator.calc_value('COUNT', data))
     self.assertEqual(min(data), calculator.calc_value('MIN', data))
     self.assertEqual(sum(data) / len(data),
                      calculator.calc_value('AVG', data))
 def test_calc_value(self):
     self.assertEqual(0, calculator.calc_value('MAX', [0]))
     data = []
     self.assertEqual(None, calculator.calc_value('MAX', data))
     random.seed(time.time())
     for i in range(0, 30, 1):
         data.append(random.uniform(0, 1000))
     self.assertEqual(max(data), calculator.calc_value('MAX', data))
     self.assertEqual(sum(data), calculator.calc_value('SUM', data))
     self.assertEqual(len(data), calculator.calc_value('COUNT', data))
     self.assertEqual(min(data), calculator.calc_value('MIN', data))
     self.assertEqual(
         sum(data) / len(data), calculator.calc_value('AVG', data))