示例#1
0
        def execute(self) -> QueryResult[Dict[str, List[float]]]:
            self._statement = self._statement.order_by(
                desc(SQLCompositeMetric.timestamp))
            if self._max_count:
                query = self._session.query(SQLCompositeMetric.id).order_by(desc(SQLCompositeMetric.timestamp)).\
                    limit(self._max_count)
                self._statement = self._statement.filter(
                    SQLCompositeMetric.id.in_([r.id for r in query.all()
                                               ]))  # MySQL forces the .all()
            sql_result: List[SQLCompositeMetric] = self._statement.all()
            result: QueryResult[Dict[str, List[float]]] = QueryResult()
            result.metric_data = {}  # correction on default type/value
            by_id = OrderedDict()
            for name, value, timestamp, uuid, group_id in reversed(sql_result):
                alias = aliased(SQLMetadata, name='metadata_field_' + name)
                if group_id not in by_id:
                    query = self._session.query(SQLMetadataSet).filter(
                        SQLMetadataSet.uuid == uuid)
                    for metadata_name, (metadata_value,
                                        op) in self._metadata_filter.items():
                        if op == MetricStore.Comparison.EQUAL:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value == metadata_value)
                        elif op == MetricStore.Comparison.NOT_EQUAL:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value != metadata_value)
                        elif op == MetricStore.Comparison.LESS_THAN:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value < metadata_value)
                        elif op == MetricStore.Comparison.GREATER_THAN:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value > metadata_value)
                        elif op == MetricStore.Comparison.LESS_THAN_OR_EQUAL:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value <= metadata_value)
                        elif op == MetricStore.Comparison.GREATER_THAN_OR_EQUAL:
                            query = query.filter(alias.name == metadata_name,
                                                 alias.value >= metadata_value)
                        else:
                            raise ValueError(f"Invalid operations: {op}")
                    try:
                        sql_metadata = query.one()
                        metadata = Metadata({})
                        for r in sql_metadata.data:
                            metadata.values[r.name] = r.value
                        by_id[group_id] = timestamp, metadata, {name: value}
                    except NoResultFound:
                        pass
                else:
                    by_id[group_id][2][name] = value
            for (timestamp, metadata, metrics_table) in by_id.values():
                result.timestamps.append(timestamp)
                result.metadata.append(metadata)
                for name, value in metrics_table.items():
                    result.metric_data.setdefault(name, [])
                    result.metric_data[name].append(value)

            return result
示例#2
0
 def execute(self) -> QueryResult[MetricDataClassT]:
     # we order timestamps for query in descending order to filter out "the top" which are the newest items
     self._statement = self._statement.order_by(
         desc(SQLCompositeMetric.timestamp))
     if self._max_count:
         self._statement = self._statement.limit(self._max_count)
     sql_result: List[SQLCompositeMetric] = self._statement.all()
     result: QueryResult[MetricDataClassT] = QueryResult()
     for item in reversed(
             sql_result
     ):  # order timestamps from oldest to newest when returning to client
         flattened: Dict[str, float] = {}
         for child in item.children:
             flattened[child.name] = child.value
         metadata = Metadata({
             data.name: data.value
             for data in item.metrics_metadata.data
         })
         result.metadata.append(metadata)
         result.timestamps.append(item.timestamp)
         result.metric_data.append(
             CompositeMetric.from_flattened(flattened).to_dataclass(
                 self._type))
     return result