Skip to content

Commit

Permalink
Run queries with no cached result in public dashboards (re getredash#220
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Allen Short committed Dec 12, 2017
1 parent a30f347 commit 546a7b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
24 changes: 14 additions & 10 deletions redash/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import json
from funcy import project
from redash import models
from redash.handlers.query_results import run_query_sync


def public_widget(widget):

res = {
'id': widget.id,
'width': widget.width,
Expand All @@ -18,8 +20,15 @@ def public_widget(widget):
'created_at': widget.created_at
}

if widget.visualization and widget.visualization.id:
query_data = models.QueryResult.query.get(widget.visualization.query_rel.latest_query_data_id).to_dict()
if (widget.visualization and widget.visualization.id and
widget.visualization.query_rel is not None):
q = widget.visualization.query_rel
# make sure the widget's query has a latest_query_data_id that is
# not null so public dashboards work
if (q.latest_query_data_id is None):
run_query_sync(q.data_source, {}, q.query_text)

query_data = q.to_dict()
res['visualization'] = {
'type': widget.visualization.type,
'name': widget.visualization.name,
Expand All @@ -28,10 +37,10 @@ def public_widget(widget):
'updated_at': widget.visualization.updated_at,
'created_at': widget.visualization.created_at,
'query': {
'id': widget.visualization.query_rel.id,
'id': q.id,
'query': ' ', # workaround, as otherwise the query data won't be loaded.
'name': widget.visualization.query_rel.name,
'description': widget.visualization.query_rel.description,
'name': q.name,
'description': q.description,
'options': {},
'latest_query_data': query_data
}
Expand All @@ -51,11 +60,6 @@ def public_dashboard(dashboard):

widgets = {w.id: public_widget(w) for w in widget_list}

# make sure all widgets' query's have a last_query_data_id that is not null so public dashboards work
for w in widgets:
if not hasattr(w, 'latest_query_data'):
models.Query.query.filter(models.Query.id == widgets[w]['visualization']['query']['id']).first()

widgets_layout = []
for row in dashboard_dict['layout']:
new_row = []
Expand Down
12 changes: 12 additions & 0 deletions tests/handlers/test_embed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import mock

from tests import BaseTestCase
from redash.models import db
from redash.query_runner.pg import PostgreSQL


class TestEmbedVisualization(BaseTestCase):
Expand Down Expand Up @@ -97,6 +100,15 @@ def test_inactive_token(self):
res = self.make_request('get', '/api/dashboards/public/{}'.format(api_key.api_key), user=False, is_json=False)
self.assertEqual(res.status_code, 404)

def test_dashboard_widgets(self):
dashboard = self.factory.create_dashboard()
w1 = self.factory.create_widget(dashboard=dashboard)
w2 = self.factory.create_widget(dashboard=dashboard, visualization=None, text="a text box")
api_key = self.factory.create_api_key(object=dashboard)
with mock.patch.object(PostgreSQL, "run_query") as qr:
qr.return_value = ("[1, 2]", None)
res = self.make_request('get', '/api/dashboards/public/{}'.format(api_key.api_key), user=False, is_json=False)
self.assertEqual(res.status_code, 200)
# Not relevant for now, as tokens in api_keys table are only created for dashboards. Once this changes, we should
# add this test.
# def test_token_doesnt_belong_to_dashboard(self):
Expand Down

0 comments on commit 546a7b5

Please sign in to comment.