Skip to content

Commit

Permalink
Merge b968a3b into 9f58004
Browse files Browse the repository at this point in the history
  • Loading branch information
twheys committed Jul 6, 2018
2 parents 9f58004 + b968a3b commit ea34a00
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
42 changes: 27 additions & 15 deletions fireant/slicer/queries/builder.py
Expand Up @@ -34,6 +34,8 @@ def __init__(self, slicer, table):
self._dimensions = []
self._filters = []
self._references = []
self._limit = None
self._offset = None

@immutable
def filter(self, *filters):
Expand All @@ -43,6 +45,22 @@ def filter(self, *filters):
"""
self._filters += filters

@immutable
def limit(self, limit):
"""
:param limit:
A limit on the number of database rows returned.
"""
self._limit = limit

@immutable
def offset(self, offset):
"""
:param offset:
A offset on the number of database rows returned.
"""
self._offset = offset

@property
def query(self):
"""
Expand Down Expand Up @@ -143,22 +161,18 @@ def query(self):
for (term, orientation) in orders:
query = query.orderby(term, order=orientation)

return query
return query.limit(self._limit).offset(self._offset)

def fetch(self, limit=None, offset=None, hint=None) -> Iterable[Dict]:
def fetch(self, hint=None) -> Iterable[Dict]:
"""
Fetch the data for this query and transform it into the widgets.
:param limit:
A limit on the number of database rows returned.
:param offset:
A offset on the number of database rows returned.
:param hint:
A query hint label used with database vendors which support it. Adds a label comment to the query.
:return:
A list of dict (JSON) objects containing the widget configurations.
"""
query = self.query.limit(limit).offset(offset)
query = self.query
if hint and hasattr(query, 'hint') and callable(query.hint):
query = query.hint(hint)

Expand Down Expand Up @@ -216,16 +230,14 @@ def query(self):
base_table=self.table,
joins=self.slicer.joins,
dimensions=self._dimensions,
filters=self._filters)
filters=self._filters) \
.limit(self._limit) \
.offset(self._offset)

def fetch(self, limit=None, offset=None, hint=None, force_include=()) -> pd.Series:
def fetch(self, hint=None, force_include=()) -> pd.Series:
"""
Fetch the data for this query and transform it into the widgets.
:param limit:
A limit on the number of database rows returned.
:param offset:
A offset on the number of database rows returned.
:param force_include:
A list of dimension values to include in the result set. This can be used to avoid having necessary results
cut off due to the pagination. These results will be returned at the head of the results.
Expand All @@ -248,8 +260,8 @@ def fetch(self, limit=None, offset=None, hint=None, force_include=()) -> pd.Seri
# Ensure that these values are included
query = query.orderby(include, order=Order.desc)

# Add ordering and pagination
query = query.orderby(definition).limit(limit).offset(offset)
# Order by the dimension definition that the choices are for
query = query.orderby(definition)

data = fetch_data(self.slicer.database,
str(query),
Expand Down
12 changes: 8 additions & 4 deletions fireant/tests/slicer/queries/test_builder.py
Expand Up @@ -2235,7 +2235,8 @@ def test_set_limit(self, mock_fetch_data: Mock):
slicer.data \
.widget(f.DataTablesJS(slicer.metrics.votes)) \
.dimension(slicer.dimensions.timestamp) \
.fetch(limit=20)
.limit(20) \
.fetch()

mock_fetch_data.assert_called_once_with(ANY,
'SELECT '
Expand All @@ -2250,7 +2251,8 @@ def test_set_offset(self, mock_fetch_data: Mock):
slicer.data \
.widget(f.DataTablesJS(slicer.metrics.votes)) \
.dimension(slicer.dimensions.timestamp) \
.fetch(offset=20)
.offset(20) \
.fetch()

mock_fetch_data.assert_called_once_with(ANY,
'SELECT '
Expand All @@ -2266,7 +2268,9 @@ def test_set_limit_and_offset(self, mock_fetch_data: Mock):
slicer.data \
.widget(f.DataTablesJS(slicer.metrics.votes)) \
.dimension(slicer.dimensions.timestamp) \
.fetch(limit=20, offset=20)
.limit(20) \
.offset(30) \
.fetch()

mock_fetch_data.assert_called_once_with(ANY,
'SELECT '
Expand All @@ -2276,7 +2280,7 @@ def test_set_limit_and_offset(self, mock_fetch_data: Mock):
'GROUP BY "$timestamp" '
'ORDER BY "$timestamp" '
'LIMIT 20 '
'OFFSET 20',
'OFFSET 30',
dimensions=DimensionMatcher(slicer.dimensions.timestamp))


Expand Down

0 comments on commit ea34a00

Please sign in to comment.