Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions caravel/migrations/versions/ad4d656d92bc_add_avg_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Add avg() to default metrics

Revision ID: ad4d656d92bc
Revises: b46fa1b0b39e
Create Date: 2016-10-25 10:16:39.871078

"""

# revision identifiers, used by Alembic.
revision = 'ad4d656d92bc'
down_revision = '7e3ddad2a00b'

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('columns', sa.Column('avg', sa.Boolean(), nullable=True))
op.add_column('table_columns', sa.Column('avg', sa.Boolean(), nullable=True))

def downgrade():
with op.batch_alter_table('columns') as batch_op:
batch_op.drop_column('avg')
with op.batch_alter_table('table_columns') as batch_op:
batch_op.drop_column('avg')
24 changes: 23 additions & 1 deletion caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ def fetch_metadata(self):
dbcol.groupby = dbcol.is_string
dbcol.filterable = dbcol.is_string
dbcol.sum = dbcol.isnum
dbcol.avg = dbcol.isnum
dbcol.is_dttm = dbcol.is_time

db.session.merge(self)
Expand All @@ -1186,6 +1187,13 @@ def fetch_metadata(self):
metric_type='sum',
expression="SUM({})".format(quoted)
))
if dbcol.avg:
metrics.append(M(
metric_name='avg__' + dbcol.column_name,
verbose_name='avg__' + dbcol.column_name,
metric_type='avg',
expression="AVG({})".format(quoted)
))
if dbcol.max:
metrics.append(M(
metric_name='max__' + dbcol.column_name,
Expand Down Expand Up @@ -1366,6 +1374,7 @@ class TableColumn(Model, AuditMixinNullable, ImportMixin):
groupby = Column(Boolean, default=False)
count_distinct = Column(Boolean, default=False)
sum = Column(Boolean, default=False)
avg = Column(Boolean, default=False)
max = Column(Boolean, default=False)
min = Column(Boolean, default=False)
filterable = Column(Boolean, default=False)
Expand All @@ -1379,7 +1388,7 @@ class TableColumn(Model, AuditMixinNullable, ImportMixin):
str_types = ('VARCHAR', 'STRING', 'CHAR')
export_fields = (
'table_id', 'column_name', 'verbose_name', 'is_dttm', 'is_active',
'type', 'groupby', 'count_distinct', 'sum', 'max', 'min',
'type', 'groupby', 'count_distinct', 'sum', 'avg', 'max', 'min',
'filterable', 'expression', 'description', 'python_date_format',
'database_expression'
)
Expand Down Expand Up @@ -2137,6 +2146,7 @@ class DruidColumn(Model, AuditMixinNullable):
groupby = Column(Boolean, default=False)
count_distinct = Column(Boolean, default=False)
sum = Column(Boolean, default=False)
avg = Column(Boolean, default=False)
max = Column(Boolean, default=False)
min = Column(Boolean, default=False)
filterable = Column(Boolean, default=False)
Expand Down Expand Up @@ -2175,6 +2185,18 @@ def generate_metrics(self):
json=json.dumps({
'type': mt, 'name': name, 'fieldName': self.column_name})
))

if self.avg and self.isnum:
mt = corrected_type.lower() + 'Avg'
name = 'avg__' + self.column_name
metrics.append(DruidMetric(
metric_name=name,
metric_type='avg',
verbose_name='AVG({})'.format(self.column_name),
json=json.dumps({
'type': mt, 'name': name, 'fieldName': self.column_name})
))

if self.min and self.isnum:
mt = corrected_type.lower() + 'Min'
name = 'min__' + self.column_name
Expand Down