예제 #1
0
 def get_aggregate_function(self, column_name, metric_name, service=None):
     """
     Returns string with metric value column name surrounded by aggregate function
     based on metric type (which tells how to interpret value - is it a counter,
     rate or something else).
     """
     metric = Metric.get_for(metric_name, service=service)
     if not metric:
         raise ValueError("Invalid metric {}".format(metric_name))
     f = metric.get_aggregate_name()
     return f or column_name
예제 #2
0
    def set_metric_values(self, metric_name, column_name,
                          requests, service, **metric_values):
        metric = Metric.get_for(metric_name, service=service)
        q = requests

        def _key(v):
            return v['value']

        # we need list of three items:
        #  * value - numeric value for given metric
        #  * label - label value to be used
        #  * samples count - number of samples for a metric
        if metric.is_rate:
            row = requests.aggregate(value=models.Avg(column_name))
            row['samples'] = requests.count()
            row['label'] = Metric.TYPE_RATE
            q = [row]

        elif metric.is_count:
            q = []
            values = requests.distinct(
                column_name).values_list(column_name, flat=True)
            for v in values:
                rqs = requests.filter(**{column_name: v})
                row = rqs.aggregate(
                    value=models.Sum(column_name),
                    samples=models.Count(column_name)
                )
                row['label'] = v
                q.append(row)
            q.sort(key=_key)
            q.reverse()

        elif metric.is_value:
            q = []
            is_user_metric = column_name == "user_identifier"
            if is_user_metric:
                values = requests.distinct(
                    column_name).values_list(column_name, "user_username")
            else:
                values = requests.distinct(
                    column_name).values_list(column_name, flat=True)
            for v in values:
                value = v
                if is_user_metric:
                    value = v[0]
                rqs = requests.filter(**{column_name: value})
                row = rqs.aggregate(
                    value=models.Count(column_name),
                    samples=models.Count(column_name)
                )
                row['label'] = v
                q.append(row)
            q.sort(key=_key)
            q.reverse()

        elif metric.is_value_numeric:
            q = []
            row = requests.aggregate(value=models.Max(column_name),
                                     samples=models.Count(column_name))
            row['label'] = Metric.TYPE_VALUE_NUMERIC
            q.append(row)

        else:
            raise ValueError("Unsupported metric type: {}".format(metric.type))
        rows = q[:100]
        metric_values.update({'metric': metric_name, 'service': service})
        for row in rows:
            label = row['label']
            value = row['value']
            samples = row['samples']
            metric_values.update({'value': value or 0,
                                  'label': label,
                                  'samples_count': samples,
                                  'value_raw': value or 0,
                                  'value_num': value if isinstance(value, (int, float, long, Decimal,)) else None})
            # log.debug(MetricValue.add(**metric_values))

            print MetricValue.add(**metric_values)