Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dttm format #506

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
439b7a1
time format minor features added
axeisghost May 21, 2016
cec723a
add description for datetime format input
May 23, 2016
404a2fd
db version bug walkaround
yxjames May 23, 2016
4336f83
removed unecessary comments and fixed minor bug
axeisghost May 23, 2016
34b6278
Merge branch 'master' into dttm_format
joshwalters May 24, 2016
eba364d
fixed code style
axeisghost May 24, 2016
2ff5cc1
minor fix
axeisghost May 24, 2016
5816d01
fixed missing time format column in DruidDatasource
axeisghost May 24, 2016
7360fd2
Merge branch 'master' into dttm_format
joshwalters May 24, 2016
6897c38
Update models.py
joshwalters May 24, 2016
adcafad
Merge branch 'master' into dttm_format
joshwalters May 24, 2016
7218303
Revert "Update models.py"
joshwalters May 24, 2016
e91507a
removed timestamp_format from druid and removed try catch in migration
axeisghost Jun 2, 2016
c610dc0
Using spaces, not tabs
joshwalters Jun 2, 2016
55bf3e0
Merge branch 'master' of https://github.com/airbnb/caravel into dttm_…
axeisghost Jun 2, 2016
53d7b24
get the most updated migration and add the migration on the head of it
axeisghost Jun 2, 2016
09d0b9e
remove vscode setting file
axeisghost Jun 2, 2016
669cc77
Merge branch 'master' into dttm_format
joshwalters Jun 3, 2016
d8552b0
Merge branch 'master' into dttm_format
yxjames Jun 3, 2016
7a6310e
Merge branch 'master' into dttm_format
joshwalters Jun 6, 2016
9c5419a
Merge branch 'master' into dttm_format
joshwalters Jun 8, 2016
646542a
Merge branch 'master' into dttm_format
joshwalters Jun 9, 2016
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add d timestamp format for tables

Revision ID: 422116f431b7
Revises: 956a063c52b3
Create Date: 2016-05-20 19:11:57.218062

"""

# revision identifiers, used by Alembic.
revision = '422116f431b7'
down_revision = '956a063c52b3'

from alembic import op
import sqlalchemy as sa


def upgrade():
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

img
failing would result in a broken app, let's fail early in the migration script if anywhere

op.add_column('datasources', sa.Column('timestamp_format', sa.String(length=256), nullable=True))
op.add_column('tables', sa.Column('timestamp_format', sa.String(length=256), nullable=True))
except Exception:
pass


def downgrade():
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, no try

op.drop_column('tables', 'timestamp_format')
op.drop_column('datasources', 'timestamp_format')
except Exception:
pass
18 changes: 11 additions & 7 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,11 @@ def grains(self):
if self.sqlalchemy_uri.startswith(db_type):
return grains

def dttm_converter(self, dttm):
def dttm_converter(self, dttm, tf=None):
"""Returns a string that the database flavor understands as a date"""
default = "'{}'".format(dttm.strftime('%Y-%m-%d %H:%M:%S.%f'))
if tf is None or tf == '':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pythonesquier:
tf = tf or '%Y-%m-%d %H:%M:%S.%f'

tf = '%Y-%m-%d %H:%M:%S.%f'
default = "'{}'".format(dttm.strftime(tf))
iso = dttm.isoformat()
d = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this previous approach had shortcomings to start with. It assumed that the casting required could be defined on a per-database basis where it cannot since even within a single table you could represent dates in different fashion (epoch, string, native DATETIME, DATE or TIMESTAMP types...).

'mssql': "CONVERT(DATETIME, '{}', 126)".format(iso), #untested
Expand Down Expand Up @@ -508,6 +510,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
offset = Column(Integer, default=0)
cache_timeout = Column(Integer)
schema = Column(String(255))
timestamp_format = Column(String(256))

baselink = "tablemodelview"

Expand Down Expand Up @@ -657,18 +660,18 @@ def query( # sqla
select_exprs += [timestamp_grain]
groupby_exprs += [timestamp_grain]

tf = '%Y-%m-%d %H:%M:%S.%f'
tf = self.timestamp_format
time_filter = [
timestamp >= text(self.database.dttm_converter(from_dttm)),
timestamp <= text(self.database.dttm_converter(to_dttm)),
timestamp >= text(self.database.dttm_converter(from_dttm, tf)),
timestamp <= text(self.database.dttm_converter(to_dttm, tf)),
]
inner_time_filter = copy(time_filter)
if inner_from_dttm:
inner_time_filter[0] = timestamp >= text(
self.database.dttm_converter(inner_from_dttm))
self.database.dttm_converter(inner_from_dttm, tf))
if inner_to_dttm:
inner_time_filter[1] = timestamp <= text(
self.database.dttm_converter(inner_to_dttm))
self.database.dttm_converter(inner_to_dttm, tf))
else:
inner_time_filter = []

Expand Down Expand Up @@ -961,6 +964,7 @@ class DruidDatasource(Model, AuditMixinNullable, Queryable):
'DruidCluster', backref='datasources', foreign_keys=[cluster_name])
offset = Column(Integer, default=0)
cache_timeout = Column(Integer)
timestamp_format = Column(String(256))

@property
def metrics_combo(self):
Expand Down
7 changes: 5 additions & 2 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,11 @@ class TableModelView(CaravelModelView, DeleteMixin): # noqa
'table_link', 'database', 'sql_link', 'is_featured',
'changed_by_', 'changed_on_']
add_columns = [
'table_name', 'database', 'schema',
'table_name', 'database', 'schema', 'timestamp_format',
'default_endpoint', 'offset', 'cache_timeout']
edit_columns = [
'table_name', 'is_featured', 'database', 'schema', 'description', 'owner',
'table_name', 'is_featured', 'database', 'schema',
'timestamp_format', 'description', 'owner',
'main_dttm_col', 'default_endpoint', 'offset', 'cache_timeout']
related_views = [TableColumnInlineView, SqlMetricInlineView]
base_order = ('changed_on', 'desc')
Expand All @@ -299,6 +300,8 @@ class TableModelView(CaravelModelView, DeleteMixin): # noqa
'schema': (
"Schema, as used only in some databases like Postgres, Redshift "
"and DB2"),
'timestamp_format': (
"Use default(%Y-%m-%d %H:%M:%S.%f) if empty"),
'description': Markup(
"Supports <a href='https://daringfireball.net/projects/markdown/'>"
"markdown</a>"),
Expand Down
4 changes: 3 additions & 1 deletion caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ def get_df(self, query_obj=None):
self.results = None

# The datasource here can be different backend but the interface is common
timestamp_format = self.datasource.timestamp_format
self.results = self.datasource.query(**query_obj)
self.query = self.results.query
df = self.results.df
if df is None or df.empty:
raise Exception("No data, review your incantations!")
else:
if 'timestamp' in df.columns:
df.timestamp = pd.to_datetime(df.timestamp, utc=False)
df.timestamp = pd.to_datetime(
df.timestamp, utc=False, format=timestamp_format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this here as opposed to relying on pandas's to_datetime magic, but it breaks down if we need to use a database side CAST or TO_DATE of some form

if self.datasource.offset:
df.timestamp += timedelta(hours=self.datasource.offset)
df = df.fillna(0)
Expand Down