轶哥

📚 Having fun with AI Agent. Always learning.

    Redash刷新整个仪表盘API
    •   更新:2021-03-10 18:38:37
    •   首发:2021-03-10 18:38:37
    •   教程
    •   4144

    转眼间使用Redash已经两年多了,redash给我的感觉就是省心省事。基础需求都能实现,用户体验很好,升级版本也容易,二次开发也方便简单。

    只不过有两个需求redash没有实现:

    • API刷新整个仪表盘
    • 导出仪表盘中所有的Widget数据为Excel文件

    第二个需求,我在《Redash导出仪表盘中所有数据为Excel文件》一文进行了实现说明。

    需求分析

    redash.png

    Refresh按钮可以设置定时刷新,但是某些情况下我们需要根据条件触发刷新。

    或者在内网部署,通过iframe将Share的链接嵌入到OA系统。分享链接不具备刷新功能。

    刷新整个仪表盘

    官方API文档中就有关于刷新整个仪表盘的说明,指向了这个文件:https://github.com/getredash/redash-toolbelt/blob/master/redash_toolbelt/examples/refresh_dashboard.py

    执行上面的代码需要安装Poetry,可以参考《Poetry快速安装教程》。

    安装poetry后,安装依赖:

    poetry install
    

    然后可以执行:

    poetry run python3 redash_toolbelt/examples/refresh_dashboard.py https://example.com API_Key slug
    

    实现对相应slug的仪表盘所有query进行刷新。

    refresh_dashboard.py代码默认的刷新周期需要手动进行修改:

    params = {
      "created_at": {
        # p.get("name"): fill_dynamic_val(todays_dates, p)
        # for p in qry["options"].get("parameters", [])
        "start": str(start) + " 00:00:00",
        "end": str(end) + " 23:59:59"
      }
    }
    

    封装命令为API

    from flask import Flask
    import subprocess, json
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def welcome():
        output = subprocess.check_output(["poetry run python3 redash_toolbelt/examples/refresh_dashboard.py https://example.com API_Key slug"], shell = True)
        out = output.decode()
        return json.dumps({"out": out},ensure_ascii=False)
    
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=5001)
    

    访问http://127.0.0.1:5001即可实现对指定仪表盘进行刷新。

    这个实现方法过于简单除暴,只适用于临时使用,正式实现还是参考redash_toolbelt代码来写吧。

    打赏
    交流区

    暂无内容

    尚未登陆
    发布
      上一篇 (Redash导出仪表盘中所有数据为Excel文件)
    下一篇 (Poetry快速安装教程)  

    评论回复提醒