2014-02-04 14:11:48 +00:00
|
|
|
import json
|
2014-02-09 13:14:46 +00:00
|
|
|
from playhouse.migrate import Migrator
|
|
|
|
from redash import db
|
2014-02-05 10:20:16 +00:00
|
|
|
from redash import models
|
2014-02-04 14:11:48 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-02-05 15:54:08 +00:00
|
|
|
default_options = {"series": {"type": "bar"}}
|
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
db.connect_db()
|
|
|
|
|
|
|
|
if not models.Visualization.table_exists():
|
|
|
|
print "Creating visualization table..."
|
|
|
|
models.Visualization.create_table()
|
2014-02-06 14:35:29 +00:00
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
with db.database.transaction():
|
|
|
|
migrator = Migrator(db.database)
|
|
|
|
print "Adding visualization_id to widgets:"
|
2014-02-09 14:40:26 +00:00
|
|
|
field = models.Widget.visualization
|
|
|
|
field.null = True
|
2014-02-09 13:14:46 +00:00
|
|
|
migrator.add_column(models.Widget, models.Widget.visualization, 'visualization_id')
|
2014-02-06 14:35:29 +00:00
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
print 'Creating TABLE visualizations for all queries...'
|
|
|
|
for query in models.Query.select():
|
|
|
|
vis = models.Visualization(query=query, name="Table",
|
2014-02-09 14:40:26 +00:00
|
|
|
description=query.description or "",
|
2014-02-09 13:14:46 +00:00
|
|
|
type="TABLE", options="{}")
|
2014-02-04 14:11:48 +00:00
|
|
|
vis.save()
|
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
print 'Creating COHORT visualizations for all queries named like %cohort%...'
|
2014-02-09 14:40:26 +00:00
|
|
|
for query in models.Query.select().where(models.Query.name ** "%cohort%"):
|
2014-02-09 13:14:46 +00:00
|
|
|
vis = models.Visualization(query=query, name="Cohort",
|
2014-02-09 14:40:26 +00:00
|
|
|
description=query.description or "",
|
2014-02-09 13:14:46 +00:00
|
|
|
type="COHORT", options="{}")
|
|
|
|
vis.save()
|
2014-02-04 14:11:48 +00:00
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
print 'Create visualization for all widgets (unless exists already):'
|
|
|
|
for widget in models.Widget.select():
|
|
|
|
print 'Processing widget id: %d:' % widget.id
|
2014-02-04 14:11:48 +00:00
|
|
|
vis_type = widget.type.upper()
|
2014-02-09 13:14:46 +00:00
|
|
|
if vis_type == 'GRID':
|
|
|
|
vis_type = 'TABLE'
|
2014-02-04 14:11:48 +00:00
|
|
|
|
2014-02-09 14:40:26 +00:00
|
|
|
query = models.Query.get_by_id(widget.query_id)
|
|
|
|
vis = query.visualizations.where(models.Visualization.type == vis_type).first()
|
2014-02-04 14:11:48 +00:00
|
|
|
if vis:
|
2014-02-09 13:14:46 +00:00
|
|
|
print '... visualization type (%s) found.' % vis_type
|
2014-02-09 14:40:26 +00:00
|
|
|
widget.visualization = vis
|
2014-02-04 14:11:48 +00:00
|
|
|
widget.save()
|
|
|
|
else:
|
2014-02-09 13:14:46 +00:00
|
|
|
vis_name = vis_type.title()
|
2014-02-06 14:35:29 +00:00
|
|
|
|
2014-02-04 14:11:48 +00:00
|
|
|
options = json.loads(widget.options)
|
2014-02-05 15:54:08 +00:00
|
|
|
vis_options = {"series": options} if options else default_options
|
2014-02-04 14:11:48 +00:00
|
|
|
vis_options = json.dumps(vis_options)
|
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
vis = models.Visualization(query=query, name=vis_name,
|
2014-02-09 14:40:26 +00:00
|
|
|
description=query.description or "",
|
2014-02-09 13:14:46 +00:00
|
|
|
type=vis_type, options=vis_options)
|
2014-02-06 14:35:29 +00:00
|
|
|
|
2014-02-09 13:14:46 +00:00
|
|
|
print '... Created visualization for type: %s' % vis_type
|
2014-02-04 14:11:48 +00:00
|
|
|
vis.save()
|
|
|
|
widget.visualization = vis
|
2014-02-09 13:14:46 +00:00
|
|
|
widget.save()
|
|
|
|
|
2014-02-09 14:40:26 +00:00
|
|
|
with db.database.transaction():
|
|
|
|
migrator = Migrator(db.database)
|
|
|
|
print "Setting visualization_id as not null..."
|
|
|
|
migrator.set_nullable(models.Widget, models.Widget.visualization, False)
|
|
|
|
|
|
|
|
db.close_db(None)
|