Skip to content

Instantly share code, notes, and snippets.

@ccombe
Forked from y-yoi/query_export.py
Last active April 2, 2024 06:44
Show Gist options
  • Save ccombe/f729b1cb8b9e2a3923f7e9d3a55b4b54 to your computer and use it in GitHub Desktop.
Save ccombe/f729b1cb8b9e2a3923f7e9d3a55b4b54 to your computer and use it in GitHub Desktop.
Redash query export/import command

Setup (Assumes Python 3.6+ installed)

  • recommend using venv or similar to keep package installs clean
  • todo: move to requirements.txt in the future to keep thing more contained
$ pip install click requests redashAPI.client

Usage for exporting

  • confirmed - this works
$ python query_export.py --redash-url "https://app.redash.io/<slug>" --api-key "<api-key>"

Usage for importing

  • todo, script needs to run from the folder in which the sql queries are found
  • missing the dashboard side of things (how to export and downnload)

the below isn't fully tested yet, simply using the same logic as the exporter to start then writing up the readme instructions from there

$ python query_import.py --redash-url "https://app.redash.io/<slug>" --api-key "<api-key>"
import click
import requests
template = u"""/*
Name: {name}
Description: {description}
Data source: {data_source}
Created By: {created_by}
Last Update At: {last_updated_at}
*/
{query}"""
def get_queries(url, api_key):
queries = []
headers = {'Authorization': 'Key {}'.format(api_key)}
path = "{}/api/queries".format(url)
has_more = True
page = 1
while has_more:
response = requests.get(path, headers=headers, params={'page': page}).json()
queries.extend(response['results'])
has_more = page * response['page_size'] + 1 <= response['count']
page += 1
return queries
def save_queries(queries):
for query in queries:
filename = 'query_{}.sql'.format(query['id'])
with open(filename, 'w') as f:
content = template.format(name=query['name'],
description=query['description'],
data_source=query['data_source_id'],
created_by=query['user']['name'],
last_updated_at=query['updated_at'],
query=query['query'])
#f.write(content.encode('utf-8'))
print(query)
f.write(content)
@click.command()
@click.option('--redash-url')
@click.option('--api-key', help="API Key")
def main(redash_url, api_key):
queries = get_queries(redash_url, api_key)
save_queries(queries)
if __name__ == '__main__':
main()
#pip3 install click redash-api-client
import click
import re
import glob
import json
from redashAPI.client import RedashAPIClient
def post_query(redash, contents):
#headers = {'Authorization': 'Key {}'.format(api_key), "Content-Type": "application/json"}
#path = "{}/api/queries".format(url)
#response = requests.post(path, headers=headers,
# params={
response = redash.create_query(
contents["name"],
contents["datasource"],
contents["query"],
False
)
return response
# Name: (.)+
# Description: (.*)
# Data source: (.+)
# Created By: (.*)
def open_queries(redash):
for sql in glob.glob('*.sql'):
line=""
with open(sql) as f:
line += f.read()
#print(f.read())
print(line)
contents = {}
contents["query"] = line
search = re.search("Name: (.+)", line)
contents["name"] = search.group(1)
search = re.search("Description: (.+)", line)
contents["description"] = search.group(1)
search = re.search("Data source: (.+)", line)
contents["datasource"] = search.group(1)
search = re.search("Created By: (.+)", line)
contents["createdby"] = search.group(1)
print(contents)
print(post_query(redash, contents))
print("---")
@click.command()
@click.option('--redash-url')
@click.option('--api-key', help="API Key")
def main(redash_url, api_key):
redash = RedashAPIClient(api_key, redash_url)
open_queries(redash)
#queries = get_queries(redash_url, api_key)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment