Beispiel #1
0
    def as_line(self, precision: Optional[str]) -> str:
        line = self.measurement

        if self.tags:
            line += ',' + ','.join('%s=%s' % (key, value)
                                   for key, value in self.tags.items())

        line += ' '
        fields = []
        for key, value in self.fields.items():
            if isinstance(value, (float, Decimal, bool)):
                value = str(value)
            elif isinstance(value, int):
                value = str(value) + 'i'
            else:
                value = quote_string(value)

            fields.append('%s=%s' % (key, value))

        line += ','.join(fields)
        if self.timestamp is not None:
            timestamp = self.timestamp
            if isinstance(timestamp, datetime):
                timestamp = convert_to_timestamp(timestamp, precision)

            line += ' %s' % timestamp

        return line
    def into(self, into: str) -> 'SelectQuery':
        """
        Set the INTO expression in the query.

        :param into: name of the measurement to write the query results into.
        :return: a new query

        """
        return SelectQuery(self._client, self._select, self._from, quote_string(into),
                           self._where, self._group_by, self._descending, self._limit,
                           self._offset, self._slimit, self._soffset, self._query_params)
Beispiel #3
0
    def query(self, select: Union[str, Iterable[str]],
              from_: Union[str, Iterable[str]], **kwargs) -> SelectQuery:
        """
        Create a query builder.

        To execute the query, call its :meth:`~asphalt.influxdb.query.SelectQuery.execute` method.

        :param select: expressions to select
        :param from_: measurements to select from
        :param kwargs: keyword arguments to pass to :class:`~asphalt.influxdb.query.SelectQuery`
        :return: a query builder object

        """
        assert check_argument_types()

        if not isinstance(select, str):
            select = ','.join(select)

        if isinstance(from_, str):
            from_ = quote_string(from_)
        else:
            from_ = ','.join(quote_string(item) for item in from_)

        return SelectQuery(self, select, from_, **kwargs)
    def where(self, *expressions: str,
              **equals: Union[str, float, int, Decimal, bool]) -> 'SelectQuery':
        """
        Augment or reset the WHERE clause in the query.

        With no arguments, the WHERE clause is reset. Otherwise, the expressions will be joined to
        the existing WHERE clause using the ``AND`` operator.

        :param expressions: raw InfluxQL expressions
        :param equals: key-value pairs which are transformed into expressions,
            quoting/transforming as necessary
        :return: a new query

        """
        equal_exprs = tuple('%s = %s' % (quote_string(key), transform_value(value))
                            for key, value in equals.items())
        expression = ' AND '.join(expressions + equal_exprs)
        if expression and self._where:
            expression = self._where + ' AND ' + expression

        return SelectQuery(self._client, self._select, self._from, self._into, expression,
                           self._group_by, self._descending, self._limit, self._offset,
                           self._slimit, self._soffset, self._query_params)
    def group_by(self, *tags: str) -> 'SelectQuery':
        """
        Augment or reset the GROUP BY clause in the query.

        If a ``*`` is the existing GROUP BY clause or among the given tags, it will be set as the
        sole GROUP BY clause and all other tags will be ignored as they would be redundant.

        With no arguments, the GROUP BY clause is reset. Otherwise, the expressions will be added
        to the existing GROUP BY clause.

        :param tags: tags on which to group
        :return: a new query

        """
        if '*' in (self._group_by,) + tags:
            expression = '*'
        else:
            expression = ','.join(quote_string(tag) for tag in tags)
            if expression and self._group_by:
                expression = self._group_by + ',' + expression

        return SelectQuery(self._client, self._select, self._from, self._into, self._where,
                           expression, self._descending, self._limit, self._offset, self._slimit,
                           self._soffset, self._query_params)
Beispiel #6
0
def test_quote_string(value, expected):
    assert quote_string(value) == expected