Commit a7d082d7 authored by Jacob Schatz's avatar Jacob Schatz

Get dimension groups working with date

parent 854db744
from pypika import Field, functions as fn
from pypika.terms import Function
class DateTrunc(Function):
def __init__(self, part, term, alias=None):
super(DateTrunc, self).__init__('DATE_TRUNC', part, term, alias=alias)
class Date():
def __init__(self, timeframe, table, name):
self.sql = None
field = Field(name, table=table)
if timeframe == 'date*':
alias = '{}_date'.format((field.get_sql().replace('_', '')))
self.sql = fn.Date(field, alias=alias)
elif timeframe == 'month*':
alias = '{}_month'.format((field.get_sql().replace('_', '')))
self.sql = fn.ToChar(DateTrunc('month', field), 'YYYY-MM', alias=alias)
elif timeframe == 'week*':
alias = '{}_week'.format((field.get_sql().replace('_', '')))
self.sql = fn.ToChar(DateTrunc('week', field), 'YYYY-MM-DD', alias=alias)
elif timeframe == 'year*':
fields.append('EXTRACT(YEAR FROM {} )::integer AS "{}_year"'\
.format(name, name))
...@@ -2,6 +2,7 @@ import re ...@@ -2,6 +2,7 @@ import re
from sqlalchemy import String, cast from sqlalchemy import String, cast
from .substitution import Substitution from .substitution import Substitution
from .aggregate import Aggregate from .aggregate import Aggregate
from .date import Date
from models.data import View, Dimension, DimensionGroup, Measure, Join from models.data import View, Dimension, DimensionGroup, Measure, Join
from pypika import Query, Table, Field from pypika import Query, Table, Field
...@@ -33,7 +34,9 @@ class SqlHelper(): ...@@ -33,7 +34,9 @@ class SqlHelper():
dimensions = list(filter(lambda x: x.name in incoming_dimensions, view.dimensions)) dimensions = list(filter(lambda x: x.name in incoming_dimensions, view.dimensions))
measures = list(filter(lambda x: x.name in incoming_measures, view.measures)) measures = list(filter(lambda x: x.name in incoming_measures, view.measures))
table = self.table(base_table, explore.name) table = self.table(base_table, explore.name)
dimension_groups = self.dimension_groups(view_name, incoming_dimension_groups, table)
dimensions = self.dimensions(dimensions, table) dimensions = self.dimensions(dimensions, table)
dimensions = dimensions + dimension_groups
measures = self.measures(measures, table) measures = self.measures(measures, table)
return self.get_query(from_=table, dimensions=dimensions, measures=measures) return self.get_query(from_=table, dimensions=dimensions, measures=measures)
...@@ -57,6 +60,21 @@ class SqlHelper(): ...@@ -57,6 +60,21 @@ class SqlHelper():
aggregate = Aggregate(m, table) aggregate = Aggregate(m, table)
return aggregate.sql return aggregate.sql
def dimension_groups(self, view_name, dimension_groups, table):
fields = []
for dimension_group in dimension_groups:
dimension_group_queried = DimensionGroup.query\
.join(View, DimensionGroup.view_id == View.id)\
.filter(View.name == view_name)\
.filter(DimensionGroup.name == dimension_group['name'])\
.first()
(_table, name) = dimension_group_queried.table_column_name.split('.')
for timeframe in dimension_group['timeframes']:
d = Date(timeframe, table, name)
fields.append(d.sql)
return fields
def get_query(self, from_, dimensions, measures): def get_query(self, from_, dimensions, measures):
select = dimensions + measures select = dimensions + measures
q = Query.from_(from_).select(*select).groupby(*dimensions) q = Query.from_(from_).select(*select).groupby(*dimensions)
......
...@@ -217,7 +217,6 @@ const actions = { ...@@ -217,7 +217,6 @@ const actions = {
.filter(j => !!(j.dimensions || j.measures)); .filter(j => !!(j.dimensions || j.measures));
let order = null; let order = null;
const dimensionGroups = baseView const dimensionGroups = baseView
.dimension_groups .dimension_groups
.map(dg => ({ .map(dg => ({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment