Beispiel #1
0
    def _generate_series(self, fields: dict,
                         intervals: List[datetime]) -> dict:

        series = {}
        totals = {}
        for field, (operation, metric_name) in fields.items():
            try:
                metric = self._metrics[metric_name]
            except KeyError:
                raise InvalidParams(f"Unknown metric '{metric_name}'")

            if operation not in metric["operations"]:
                raise InvalidParams(
                    f"Invalid operation '{operation}' for metric '{metric_name}'"
                )

            mu = 1000 * random.random()
            series[field] = [random.normalvariate(mu, 50) for _ in intervals]

            if operation == "count_unique":
                series[field] = list(map(int, series[field]))

            totals[field] = self._operations[operation](series[field])

        return {
            "totals": totals,
            "series": series,
        }
Beispiel #2
0
    def get_tag_values(self, project: Project, metric_name: str,
                       tag_name: str) -> Dict[str, str]:
        # Return same tag names for every metric for now:
        if metric_name not in self._metrics:
            raise InvalidParams(f"Unknown metric '{metric_name}'")

        try:
            return self._tags[tag_name]
        except KeyError:
            raise InvalidParams(
                f"Unknown tag '{tag_name}' for metric '{metric_name}'")
Beispiel #3
0
    def _verify_query(self, query: QueryDefinition):
        if not query.query:
            return

        filter_ = parse_query(query.query)
        for conditions in filter_["or"]:
            for tag_name, tag_value in conditions["and"]:
                if tag_name not in self._tags:
                    raise InvalidParams(f"Unknown tag '{tag_name}'")
                if tag_value not in self._tags[tag_name]:
                    raise InvalidParams(
                        f"Unknown tag value '{tag_value}' for tag '{tag_name}'"
                    )
Beispiel #4
0
    def _validate_metric_names(cls, metric_names):
        unknown_metric_names = set(metric_names) - _METRICS.keys()
        if unknown_metric_names:
            raise InvalidParams(
                f"Unknown metrics '{', '.join(unknown_metric_names)}'")

        return metric_names
Beispiel #5
0
def _get_metric(metric_name: str) -> dict:
    try:
        metric = _METRICS[metric_name]
    except KeyError:
        raise InvalidParams(f"Unknown metric '{metric_name}'")

    return metric
Beispiel #6
0
    def _get_metric(cls, metric_name: str) -> dict:
        try:
            metric = cls._metrics[metric_name]
        except KeyError:
            raise InvalidParams(f"Unknown metric '{metric_name}'")

        return metric
Beispiel #7
0
    def get_series(self, query: QueryDefinition) -> dict:
        """ Get time series for the given query """

        intervals = list(query.get_intervals())

        self._verify_query(query)

        for tag_name in query.groupby:
            if tag_name not in self._tags:
                raise InvalidParams(f"Unknown tag '{tag_name}'")
        tags = [[(tag_name, tag_value) for tag_value in self._tags[tag_name]]
                for tag_name in query.groupby]

        return {
            "start":
            query.start,
            "end":
            query.end,
            "query":
            query.query,
            "intervals":
            intervals,
            "groups": [
                dict(
                    by={
                        tag_name: tag_value
                        for tag_name, tag_value in combination
                    },
                    **self._generate_series(query.fields, intervals),
                ) for combination in itertools.product(*tags)
            ] if tags else
            [dict(by={}, **self._generate_series(query.fields, intervals))],
        }
Beispiel #8
0
def parse_tag(tag_string: str) -> Tuple[str, str]:
    try:
        name, value = tag_string.split(":")
    except ValueError:
        raise InvalidParams(f"Expected something like 'foo:\"bar\"' for tag, got '{tag_string}'")

    return (verify_tag_name(name), value.strip('"'))
Beispiel #9
0
    def _get_tag_values(cls, metric_name: str, tag_name: str) -> List[str]:
        metric = _get_metric(metric_name)
        try:
            tags = metric["tags"][tag_name]
        except KeyError:
            raise InvalidParams(f"Unknown tag '{tag_name}'")

        return tags
Beispiel #10
0
    def get_single_metric(self, project: Project, metric_name: str) -> dict:
        """Get metadata for a single metric, without tag values"""
        try:
            metric = _METRICS[metric_name]
        except KeyError:
            raise InvalidParams()

        return dict(
            name=metric_name,
            **{
                # Only return metric names
                key: (sorted(value.keys()) if key == "tags" else value)
                for key, value in metric.items()
            },
        )
Beispiel #11
0
def verify_tag_name(name: str) -> str:

    if not TAG_REGEX.match(name):
        raise InvalidParams(f"Invalid tag name: '{name}'")

    return name