Skip to content

Commit

Permalink
Added time features
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Mancevice committed Aug 10, 2016
1 parent 934a1aa commit 39327d3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 30 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ flux.query(Widgets)
```


### Query Ad Hoc Measurement

```python
# SELECT * from /.*/;
flux.query(influxalchemy.Measurement.new("/.*/"))
```


### Select Fields of Measurement

```python
Expand Down Expand Up @@ -84,3 +92,25 @@ flux.query(Widgets).filter(clause1 & clause2)
# SELECT * FROM widgets WHERE tag1 = 'fizz' OR tag2 = 'buzz';
flux.query(Widgets).filter(clause1 | clause2)
```


### Group Bys

```python
# SELECT * FROM widgets GROUP BY time(1d);
flux.query(Widgets).group_by("time(1d)")

# SELECT * FROM widgets GROUP BY tag1;
flux.query(Widgets).group_by(Widgets.tag1)
```


### Time

```python
# SELECT * FROM widgets WHERE (time > now() - 7d);
flux.query(Widgets).filter(Widgets.time > "now() - 7d")

# SELECT * FROM widgets WHERE time >= '2016-01-01' AND time <= now() - 7d;
flux.query(Widgets).filter(Widgets.time.between("'2016-01-01'", "now() - 7d"))
```
20 changes: 18 additions & 2 deletions influxalchemy/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

class MetaMeasurement(type):
def __getattr__(self, name):
return Tag(name, self)
if name == "time":
return Time(name, self)
else:
return Tag(name, self)

def __str__(self):
try:
Expand Down Expand Up @@ -69,12 +72,25 @@ def notlike(self, other):
return TagExp.nk(self, other)


class Time(Tag):
def between(self, start, end, startinc=True, endinc=True):
if startinc is True:
startexp = TagExp.ge(self, start)
else:
startexp = TagExp.gt(self, start)
if endinc is True:
endexp = TagExp.le(self, end)
else:
endexp = TagExp.lt(self, end)
return startexp & endexp


class TagExp(object):
def __init__(self, left, op, right):
self._left = left
self._op = op
lits = [operations.LK, operations.NK, operations.AND, operations.OR]
if self._op in lits:
if self._op in lits or isinstance(left, Time):
self._right = right
else:
self._right = repr(right)
Expand Down
38 changes: 26 additions & 12 deletions influxalchemy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@

import itertools

import influxdb
from . import measurement


class InfluxDBQuery(object):
def __init__(self, entities, client, *expressions):
def __init__(self, entities, client, expressions=None, groupby=None):
self._entities = entities
self._client = client
self._expressions = expressions
self._expressions = expressions or ()
self._groupby = groupby

def __str__(self):
select = ", ".join(self._select)
from_ = self._from
where = " AND ".join(self._where)
if any(where):
return "SELECT %s FROM %s WHERE %s;" % (select, from_, where)
iql = "SELECT %s FROM %s WHERE %s" % (select, from_, where)
else:
return "SELECT %s FROM %s;" % (select, from_)
iql = "SELECT %s FROM %s" % (select, from_)
if self._groupby is not None:
iql += " GROUP BY %s" % self._groupby
return "%s;" % iql

def __repr__(self):
return str(self)

def execute(self):
return self._client.bind.query(str(self))

def filter(self, *expressions):
expressions = self._expressions + expressions
return InfluxDBQuery(self._entities, self._client, *expressions)
return InfluxDBQuery(self._entities, self._client, expressions)

def filter_by(self, **kwargs):
expressions = self._expressions
for key, val in kwargs.items():
expressions += measurement.TagExp.eq(key, val),
return InfluxDBQuery(self._entities, self._client, *expressions)

def execute(self):
return self._client.bind.query(str(self))
def group_by(self, groupby):
return InfluxDBQuery(
self._entities, self._client, self._expressions, groupby)

@property
def measurement(self):
Expand All @@ -43,16 +52,21 @@ def measurement(self):

@property
def _select(self):
selects = []
for ent in self._entities:
# Entity is a Tag
if isinstance(ent, measurement.Tag):
yield str(ent)
selects.append(str(ent))
# Entity is a Measurement
else:
for tag in self._client.tags(ent):
yield tag
for field in self._client.fields(ent):
yield field
try:
for tag in self._client.tags(ent):
selects.append(tag)
for field in self._client.fields(ent):
selects.append(field)
except influxdb.exceptions.InfluxDBClientError:
selects = ["*"]
return selects

@property
def _from(self):
Expand Down
Loading

0 comments on commit 39327d3

Please sign in to comment.