示例#1
0
    def common(req, q):
        # process metric name
        name = req.get_param('name')
        if name and name.strip():
            q.append({'match': {'name': name.strip()}})

        # handle start and end time
        try:
            st = req.get_param('start_time')
            st = tu.parse_isotime(st) if st else ParamUtil._default_st()
            st = st.timetuple()
            et = req.get_param('end_time')
            et = tu.parse_isotime(et) if et else ParamUtil._default_et()
            et = et.timetuple()
            q.append({'range': {'timestamp': {'lt': time.mktime(et),
                                              'gte': time.mktime(st)}}})
        except Exception:
            return False

        # handle dimensions
        dimensions = req.get_param('dimensions')
        matches = []

        def _handle_pair(pair):
            param = pair.split(':')
            if len(param) == 2 and param[0] and param[1]:
                key = param[0].strip()
                value = param[1].strip()
                # in case that the value is numeric
                try:
                    value = float(param[1].strip())
                except Exception:
                    # The value is not numeric, so use as is.
                    pass
                matches.append({'match': {'dimensions.' + key: value}})

        if dimensions:
            map(_handle_pair, dimensions.split(','))
            q += matches

        return True
示例#2
0
    def common(req, q):
        # process metric name
        name = req.get_param('name')
        if name and name.strip():
            q.append({'match': {'name': name.strip()}})

        # handle start and end time
        try:
            st = req.get_param('start_time')
            st = tu.parse_isotime(st) if st else ParamUtil._default_st()
            st = st.timetuple()
            et = req.get_param('end_time')
            et = tu.parse_isotime(et) if et else ParamUtil._default_et()
            et = et.timetuple()
            q.append({'range': {'timestamp': {'lt': time.mktime(et),
                                              'gte': time.mktime(st)}}})
        except Exception:
            return False

        # handle dimensions
        dimensions = req.get_param('dimensions')
        matches = []

        def _handle_pair(pair):
            param = pair.split(':')
            if len(param) == 2 and param[0] and param[1]:
                key = param[0].strip()
                value = param[1].strip()
                # in case that the value is numeric
                try:
                    value = float(param[1].strip())
                except Exception:
                    # The value is not numeric, so use as is.
                    pass
                matches.append({'match': {'dimensions.' + key: value}})

        if dimensions:
            map(_handle_pair, dimensions.split(','))
            q += matches

        return True
    def update_sub_expr_state(self, expr, expr_data):
        def _update_metrics():
            """Delete metrics not in period."""
            data_list = expr_data['data'][expr.fmtd_sub_expr_str]['metrics']
            start_time = t_now - (float(expr.period) + 2) * int(expr.periods)
            while (len(data_list) != 0
                   and data_list[0]['timestamp'] < start_time):
                data_list.popleft()

        def _update_state():
            """Update state of a sub expr."""
            data_sub = expr_data['data'][expr.fmtd_sub_expr_str]
            data_list = data_sub['metrics']
            if len(data_list) == 0:
                data_sub['state'] = 'UNDETERMINED'
            else:
                period = float(expr.period)
                periods = int(expr.periods)
                right = t_now
                left = right - period
                temp_data = []
                value_in_periods = []
                for i in range(len(data_list) - 1, -1, -1):
                    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 = []
                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(None)
                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)))

        t_now = tu.iso8601_from_timestamp(tu.utcnow_ts())
        t_now = tu.parse_isotime(t_now).timetuple()
        t_now = time.mktime(t_now)
        _update_metrics()
        _update_state()
 def _add_metrics():
     temp = None
     if self.match_by:
         q_name = self.get_matched_data_queue_name(data)
         if q_name:
             temp = self.expr_data_queue[q_name]
     else:
         if None not in self.expr_data_queue:
             self.create_data_item(None)
         temp = self.expr_data_queue[None]
     if temp:
         data_list = temp['data'][expr.fmtd_sub_expr_str]
         data_list['metrics'].append(
             {'value': float(data['value']),
              'timestamp':
                  time.mktime(tu.parse_isotime(
                      data['timestamp']).timetuple())})