Skip to content

Instantly share code, notes, and snippets.

@y-yoi
Created April 16, 2019 04:18
Show Gist options
  • Save y-yoi/059006cbab4437ad6eca94c1cf497ccf to your computer and use it in GitHub Desktop.
Save y-yoi/059006cbab4437ad6eca94c1cf497ccf to your computer and use it in GitHub Desktop.
Redash query export/import command
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
# 指定された場所の.sqlファイルを開き、
# 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