Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-importing query/dashboard when origin updated. #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ packages = [

[tool.poetry.dependencies]
python = "^3.6"
python-dateutil = "2.8.1"
requests = "^2.22.0"
click = "^7.0"

Expand Down
4 changes: 3 additions & 1 deletion redash_toolbelt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ def create_data_source(self, name, _type, options):

return self._post("api/data_sources", json=payload)

def dashboard(self, slug):
def dashboard(self, slug, legacy=False):
"""GET api/dashboards/{slug}"""
if legacy:
return self._get("api/dashboards/{}?legacy=1".format(slug)).json()
return self._get("api/dashboards/{}".format(slug)).json()

def create_query(self, query_json):
Expand Down
39 changes: 33 additions & 6 deletions redash_toolbelt/examples/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys, os, json, logging, textwrap
import click
from redash_toolbelt import Redash
from dateutil import parser

logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
logging.getLogger("requests").setLevel("ERROR")
Expand Down Expand Up @@ -388,9 +389,19 @@ def import_query_subroutine(query):

data_source_id = DATA_SOURCES.get(query["data_source_id"])

should_update_query = False
if origin_id in meta["queries"] or str(origin_id) in meta["queries"]:
print("Query {} - SKIP - was already imported".format(origin_id))
return
destination_id = meta["queries"][query["id"]]
dest_query = dest_client.get_query(destination_id)
origin_updated_at = parser.parse(query["updated_at"])
dest_updated_at = parser.parse(dest_query["updated_at"])

if origin_updated_at > dest_updated_at:
should_update_query = True
print("Query {} - Re-importing - Destination is out-to-date.".format(origin_id))
else:
print("Query {} - SKIP - was already imported".format(origin_id))
return

if data_source_id is None:
print(
Expand Down Expand Up @@ -424,7 +435,11 @@ def import_query_subroutine(query):
user_client = Redash(DESTINATION, user_api_key)

try:
response = user_client.create_query(data)
if should_update_query:
print("Updating query %s - %s OK" % (origin_id, destination_id))
response = user_client.update_query(destination_id, data)
else:
response = user_client.create_query(data)
except Exception as e:
print("Query {} - FAIL - {}".format(origin_id, e))
return
Expand Down Expand Up @@ -552,9 +567,18 @@ def import_dashboards(orig_client, dest_client):
dashboards = sorted(dashboards, key=lambda x: x.get("created_at", 0))

for dashboard in dashboards:
should_update_query = False
if dashboard["slug"] in meta["dashboards"]:
print("Dashboard `{}` - SKIP - Already imported".format(dashboard["slug"]))
continue
dashboard_slug = meta["dashboards"][dashboard["slug"]]
dest_dashboard = dest_client.dashboard(dashboard_slug, True)
origin_updated_at = parser.parse(dashboard["updated_at"])
dest_updated_at = parser.parse(dest_dashboard["updated_at"])
if origin_updated_at > dest_updated_at:
should_update_query = True
print("Dashboard `{}` - Re-importing origin".format(dashboard_slug))
else:
print("Dashboard `{}` - SKIP - Already imported".format(dashboard["slug"]))
continue

print(" importing: {}".format(dashboard["slug"]))

Expand All @@ -573,7 +597,10 @@ def import_dashboards(orig_client, dest_client):

user_client = Redash(DESTINATION, user_api_key)

new_dashboard = user_client.create_dashboard(d["name"])
if should_update_query:
new_dashboard = dest_dashboard
else:
new_dashboard = user_client.create_dashboard(d["name"])

new_dash_id = new_dashboard["id"]

Expand Down