Javascriptを使ってredash API
実行環境はGAS。ライブラリ公開すれば、汎用性が高まるのでおすすめ。
取得したデータをspreadsheetに書き出すと便利。業務で使う場合は、結構おすすめ。
Poll for Fresh Query Results (including parameters)
参考ソース(python)
var httpOptions = {
method: "",
headers: {
Authorization: "",
Accept: 'application/json',
'Content-Type': 'application/json'
}
}
function setApiKey(api_key){
httpOptions.headers.Authorization = "Key " + api_key
}
// redash refresh query
function getRefreshQuery(redash_url, query_id){
var refreshUrl = redash_url + '/api/queries/' + query_id + '/refresh'
httpOptions.method = 'POST'
var res = UrlFetchApp.fetch(refreshUrl, httpOptions)
if(res.getResponseCode() !== 200){
Logger.log('Refresh Failed')
return 'Refresh Failed'
}
return JSON.parse(res)
}
// redash query result
function getQueryResult(redash_url, query_id, result_id){
var resultUrl = redash_url + '/api/queries/' + query_id + '/results/'+ result_id + '.json'
httpOptions.method = 'GET'
var res = UrlFetchApp.fetch(resultUrl, httpOptions)
var results = JSON.parse(res).query_result.data.rows
return results
}
// redash check status
function getQueryJobStatus(redash_url, job_id){
var jobcheckUrl = redash_url + '/api/jobs/' + job_id
httpOptions.method = 'GET'
var res = UrlFetchApp.fetch(jobcheckUrl, httpOptions)
return JSON.parse(res)
}