Skip to content

Commit

Permalink
Add hour grain to Sqlite (apache#4333)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored and mistercrunch committed Feb 7, 2018
1 parent ebc9470 commit 7ff9486
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions superset/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,12 @@ def load_long_lat_data():
"""Loading lat/long data from a csv file in the repo"""
with gzip.open(os.path.join(DATA_FOLDER, 'san_francisco.csv.gz')) as f:
pdf = pd.read_csv(f, encoding="utf-8")
pdf['date'] = datetime.datetime.now().date()
start = datetime.datetime.now().replace(
hour=0, minute=0, second=0, microsecond=0)
pdf['datetime'] = [
start + datetime.timedelta(hours=i * 24 / (len(pdf) - 1))
for i in range(len(pdf))
]
pdf['occupancy'] = [random.randint(1, 6) for _ in range(len(pdf))]
pdf['radius_miles'] = [random.uniform(1, 3) for _ in range(len(pdf))]
pdf['geohash'] = pdf[['LAT', 'LON']].apply(
Expand All @@ -1038,7 +1043,7 @@ def load_long_lat_data():
'region': String(50),
'postcode': Float(),
'id': String(100),
'date': Date(),
'datetime': DateTime(),
'occupancy': Float(),
'radius_miles': Float(),
'geohash': String(12),
Expand All @@ -1052,7 +1057,7 @@ def load_long_lat_data():
obj = db.session.query(TBL).filter_by(table_name='long_lat').first()
if not obj:
obj = TBL(table_name='long_lat')
obj.main_dttm_col = 'date'
obj.main_dttm_col = 'datetime'
obj.database = get_or_create_main_db()
db.session.merge(obj)
db.session.commit()
Expand Down
2 changes: 2 additions & 0 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ class SqliteEngineSpec(BaseEngineSpec):
engine = 'sqlite'
time_grains = (
Grain('Time Column', _('Time Column'), '{col}'),
Grain('hour', _('hour'),
"DATETIME(STRFTIME('%Y-%m-%dT%H:00:00', {col}))"),
Grain('day', _('day'), 'DATE({col})'),
Grain('week', _('week'),
"DATE({col}, -strftime('%w', {col}) || ' days')"),
Expand Down

0 comments on commit 7ff9486

Please sign in to comment.