2014-02-04 14:11:48 +00:00
|
|
|
import json
|
|
|
|
import settings
|
|
|
|
from data.models import *
|
|
|
|
|
|
|
|
# first run:
|
|
|
|
|
|
|
|
# CREATE TABLE "visualizations" (
|
|
|
|
# "id" serial NOT NULL PRIMARY KEY,
|
|
|
|
# "type" varchar(100) NOT NULL,
|
|
|
|
# "query_id" integer NOT NULL REFERENCES "queries" ("id") DEFERRABLE INITIALLY DEFERRED,
|
|
|
|
# "name" varchar(255) NOT NULL,
|
|
|
|
# "description" varchar(4096),
|
|
|
|
# "options" text NOT NULL
|
|
|
|
# )
|
|
|
|
# ;
|
|
|
|
|
|
|
|
# ALTER TABLE widgets ADD COLUMN "visualization_id" integer REFERENCES "visualizations" ("id") DEFERRABLE INITIALLY DEFERRED;
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-02-05 15:54:08 +00:00
|
|
|
default_options = {"series": {"type": "bar"}}
|
|
|
|
|
2014-02-06 14:35:29 +00:00
|
|
|
# create 'table' visualization for all queries
|
|
|
|
print 'creating TABLE visualizations ...'
|
|
|
|
for query in Query.objects.all():
|
|
|
|
vis = Visualization(query=query, name="Table",
|
|
|
|
description=query.description,
|
|
|
|
type="TABLE", options="{}")
|
|
|
|
vis.save()
|
|
|
|
|
|
|
|
|
|
|
|
# create 'cohort' visualization for all queries named with 'cohort'
|
|
|
|
print 'creating COHORT visualizations ...'
|
2014-02-04 14:11:48 +00:00
|
|
|
for query in Query.objects.filter(name__icontains="cohort"):
|
2014-02-06 14:35:29 +00:00
|
|
|
vis = Visualization(query=query, name="Cohort",
|
2014-02-04 14:11:48 +00:00
|
|
|
description=query.description,
|
|
|
|
type="COHORT", options="{}")
|
|
|
|
vis.save()
|
|
|
|
|
|
|
|
|
2014-02-06 14:35:29 +00:00
|
|
|
# create visualization for every widget (unless it already exists)
|
|
|
|
print 'migrating Widgets -> Visualizations ...'
|
2014-02-04 14:11:48 +00:00
|
|
|
for widget in Widget.objects.all():
|
2014-02-06 14:35:29 +00:00
|
|
|
print 'processing widget %d:' % widget.id,
|
2014-02-04 14:11:48 +00:00
|
|
|
query = widget.query
|
|
|
|
vis_type = widget.type.upper()
|
|
|
|
|
|
|
|
vis = query.visualizations.filter(type=vis_type)
|
|
|
|
if vis:
|
2014-02-06 14:35:29 +00:00
|
|
|
print 'visualization exists'
|
2014-02-04 14:11:48 +00:00
|
|
|
widget.visualization = vis[0]
|
|
|
|
widget.save()
|
|
|
|
|
|
|
|
else:
|
2014-02-06 14:35:29 +00:00
|
|
|
vis_name = widget.type.title()
|
|
|
|
|
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-06 14:35:29 +00:00
|
|
|
vis = Visualization(query=query, name=vis_name,
|
|
|
|
description=query.description,
|
|
|
|
type=vis_type, options=vis_options)
|
|
|
|
|
|
|
|
print 'created visualization %s' % vis_type
|
2014-02-04 14:11:48 +00:00
|
|
|
vis.save()
|
|
|
|
widget.visualization = vis
|
|
|
|
widget.save()
|