Import functions to import JSON representation of a dashboard

This commit is contained in:
Arik Fraimovich 2014-03-11 13:00:27 +02:00
parent f420c91909
commit 0f29506dda
4 changed files with 120 additions and 0 deletions

View File

@ -10,6 +10,7 @@ atfork.stdlib_fixer.fix_logging_module()
import logging
import time
from redash import settings, app, db, models, data_manager, __version__
from redash.import_export import manager as import_export_manager
from flask.ext.script import Manager, prompt_pass
manager = Manager(app)
@ -93,6 +94,7 @@ def delete(email):
manager.add_command("database", database_manager)
manager.add_command("users", users_manager)
manager.add_command("import", import_export_manager)
if __name__ == '__main__':
channel = logging.StreamHandler()

88
redash/import_export.py Normal file
View File

@ -0,0 +1,88 @@
import json
from redash import models
from flask.ext.script import Manager
def import_query_result(query_result):
query_result = models.QueryResult.create(data=json.dumps(query_result['data']),
query_hash=query_result['query_hash'],
retrieved_at=query_result['retrieved_at'],
query=query_result['query'],
runtime=query_result['runtime'])
return query_result
def import_query(user, query):
query_result = import_query_result(query['latest_query_data'])
new_query = models.Query.create(name=query['name'],
user=user,
ttl=-1,
query=query['query'],
query_hash=query['query_hash'],
description=query['description'],
latest_query_data=query_result)
return new_query
def import_visualization(user, visualization):
query = import_query(user, visualization['query'])
new_visualization = models.Visualization.create(name=visualization['name'],
description=visualization['description'],
type=visualization['type'],
options=json.dumps(visualization['options']),
query=query)
return new_visualization
def import_widget(dashboard, widget):
visualization = import_visualization(dashboard.user, widget['visualization'])
new_widget = models.Widget.create(dashboard=dashboard,
width=widget['width'],
options=json.dumps(widget['options']),
visualization=visualization)
return new_widget
def import_dashboard(user, dashboard):
"""
Imports dashboard along with widgets, visualizations and queries from another re:dash.
user - the user to associate all objects with.
dashboard - dashboard to import (can be result of loading a json output).
"""
new_dashboard = models.Dashboard.create(name=dashboard['name'],
slug=dashboard['slug'],
layout='[]',
user=user)
layout = []
for widgets in dashboard['widgets']:
row = []
for widget in widgets:
widget_id = import_widget(new_dashboard, widget).id
row.append(widget_id)
layout.append(row)
new_dashboard.layout = json.dumps(layout)
new_dashboard.save()
return new_dashboard
manager = Manager(help="import/export utilities")
@manager.command
def dashboard(filename, user_id):
user = models.User.get_by_id(user_id)
with open(filename) as f:
dashboard = json.loads(f.read())
import_dashboard(user, dashboard)

1
tests/flights.json Normal file

File diff suppressed because one or more lines are too long

29
tests/test_import.py Normal file
View File

@ -0,0 +1,29 @@
import json
from tests import BaseTestCase
from redash import models
from redash import import_export
from factories import user_factory
class ImportTest(BaseTestCase):
def setUp(self):
super(ImportTest, self).setUp()
with open('flights.json') as f:
self.dashboard = json.loads(f.read())
self.user = user_factory.create()
def test_imports_dashboard_correctly(self):
dashboard = import_export.import_dashboard(self.user, self.dashboard)
self.assertIsNotNone(dashboard)
self.assertEqual(dashboard.name, self.dashboard['name'])
self.assertEqual(dashboard.slug, self.dashboard['slug'])
self.assertEqual(dashboard.user, self.user)
self.assertEqual(dashboard.widgets.count(),
reduce(lambda s, row: s + len(row), self.dashboard['widgets'], 0))
self.assertEqual(models.Visualization.select().count(), dashboard.widgets.count())
self.assertEqual(models.Query.select().count(), dashboard.widgets.count())
self.assertEqual(models.QueryResult.select().count(), dashboard.widgets.count())