Skip to content

Commit

Permalink
Merge pull request #51 from mistercrunch/offset
Browse files Browse the repository at this point in the history
Adding timezone offset as a datasource param
  • Loading branch information
mistercrunch committed Oct 20, 2015
2 parents cdb32a1 + a7fca30 commit 8b67fa1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
@@ -0,0 +1,24 @@
"""TZ offsets in data sources
Revision ID: 2929af7925ed
Revises: 1e2841a4128
Create Date: 2015-10-19 20:54:00.565633
"""

# revision identifiers, used by Alembic.
revision = '2929af7925ed'
down_revision = '1e2841a4128'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

def upgrade():
op.add_column('datasources', sa.Column('offset', sa.Integer(), nullable=True))
op.add_column('tables', sa.Column('offset', sa.Integer(), nullable=True))


def downgrade():
op.drop_column('tables', 'offset')
op.drop_column('datasources', 'offset')
7 changes: 4 additions & 3 deletions panoramix/models.py
Expand Up @@ -11,9 +11,8 @@
from sqlalchemy import Table
from sqlalchemy import create_engine, MetaData, desc, select, and_
from sqlalchemy.orm import relationship
from sqlalchemy.sql import table, literal_column, text, column
from sqlalchemy.sql import table, literal_column, text
from sqlalchemy.sql.elements import ColumnClause
from flask import request

from copy import deepcopy, copy
from collections import namedtuple
Expand Down Expand Up @@ -212,6 +211,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
database_id = Column(Integer, ForeignKey('dbs.id'), nullable=False)
database = relationship(
'Database', backref='tables', foreign_keys=[database_id])
offset = Column(Integer, default=0)

baselink = "tableview"

Expand Down Expand Up @@ -637,7 +637,7 @@ def refresh_datasources(self):
Datasource.sync_to_db(datasource, self)


class Datasource(Model, AuditMixin, Queryable):
class Datasource(Model, AuditMixinNullable, Queryable):
type = "druid"

baselink = "datasourcemodelview"
Expand All @@ -655,6 +655,7 @@ class Datasource(Model, AuditMixin, Queryable):
String(250), ForeignKey('clusters.cluster_name'))
cluster = relationship(
'Cluster', backref='datasources', foreign_keys=[cluster_name])
offset = Column(Integer, default=0)

@property
def metrics_combo(self):
Expand Down
16 changes: 12 additions & 4 deletions panoramix/views.py
Expand Up @@ -126,10 +126,14 @@ class DatabaseView(PanoramixModelView, DeleteMixin):
class TableView(PanoramixModelView, DeleteMixin):
datamodel = SQLAInterface(models.SqlaTable)
list_columns = ['table_link', 'database']
add_columns = ['table_name', 'database', 'default_endpoint']
add_columns = ['table_name', 'database', 'default_endpoint', 'offset']
edit_columns = [
'table_name', 'database', 'main_dttm_col', 'default_endpoint']
'table_name', 'database', 'main_dttm_col', 'default_endpoint',
'offset']
related_views = [TableColumnInlineView, SqlMetricInlineView]
description_columns = {
'offset': "Timezone offset (in hours) for this datasource"
}

def post_add(self, table):
try:
Expand Down Expand Up @@ -219,13 +223,17 @@ class DashboardModelView(PanoramixModelView, DeleteMixin):
class DatasourceModelView(PanoramixModelView, DeleteMixin):
datamodel = SQLAInterface(models.Datasource)
list_columns = [
'datasource_link', 'cluster', 'owner', 'is_featured', 'is_hidden']
'datasource_link', 'cluster', 'owner', 'is_featured', 'is_hidden',
'offset']
related_views = [ColumnInlineView, MetricInlineView]
edit_columns = [
'datasource_name', 'cluster', 'description', 'owner',
'is_featured', 'is_hidden', 'default_endpoint']
'is_featured', 'is_hidden', 'default_endpoint', 'offset']
page_size = 100
base_order = ('datasource_name', 'asc')
description_columns = {
'offset': "Timezone offset (in hours) for this datasource"
}

def post_add(self, datasource):
datasource.generate_metrics()
Expand Down
5 changes: 3 additions & 2 deletions panoramix/viz.py
@@ -1,6 +1,5 @@
from collections import OrderedDict, defaultdict
from copy import copy
from datetime import datetime
from datetime import datetime, timedelta
import json
import uuid

Expand Down Expand Up @@ -110,6 +109,8 @@ def get_df(self, query_obj=None):
else:
if 'timestamp' in df.columns:
df.timestamp = pd.to_datetime(df.timestamp)
if self.datasource.offset:
df.timestamp += timedelta(hours=self.datasource.offset)
return df

@property
Expand Down

0 comments on commit 8b67fa1

Please sign in to comment.