示例#1
0
def diff(redash_url, api_key, in_file):

    if in_file is None:
        click.echo('No file provided')
        return
    server = redash.Redash(redash_url, api_key)
    old_queries = server.Get_Queries()
    old_queries = server.Get_Full_Queries(old_queries)
    old_sorted_queries = sort_queries(old_queries)

    old_stream = StringIO()
    yaml.scalarstring.walk_tree(old_sorted_queries)
    yaml.dump(old_sorted_queries, old_stream, Dumper=yaml.RoundTripDumper)

    new_queries = read_yaml(in_file)
    new_sorted_queries = sort_queries(new_queries)

    new_stream = StringIO()
    yaml.scalarstring.walk_tree(new_sorted_queries)
    yaml.dump(new_sorted_queries, new_stream, Dumper=yaml.RoundTripDumper)

    # diff = difflib.ndiff(old_stream.getvalue().strip().splitlines(),new_stream.getvalue().strip().splitlines())
    diff = difflib.HtmlDiff().make_file(
        old_stream.getvalue().strip().splitlines(),
        new_stream.getvalue().strip().splitlines(), "test.html")
    sys.stdout.writelines(diff)
示例#2
0
def dashboards(redash_url, api_key, out_file):
    if out_file is None:
        click.echo('No out file provided')
        return
    server = redash.Redash(redash_url, api_key)
    dashboards = server.Get_Dashboards()

    save_yaml(dashboards, out_file)
示例#3
0
def dump(redash_url, api_key, out_file):
    if out_file is None:
        click.echo('No out file provided')
        return
    server = redash.Redash(redash_url, api_key)
    queries = server.Get_Queries()
    queries = server.Get_Full_Queries(queries)

    save_yaml(queries, out_file)
示例#4
0
def archive(redash_url, api_key, in_file):

    if in_file is None:
        click.echo('No file provided')
        return
    server = redash.Redash(redash_url, api_key)
    server_queries = server.Get_Queries(True)

    new = read_yaml(in_file)
    server.Archive_Missing_Queries(server_queries, new)
示例#5
0
def push(redash_url, api_key, in_file):

    if in_file is None:
        click.echo('No file provided')
        return
    server = redash.Redash(redash_url, api_key)
    old_queries = server.Get_Queries()
    old_queries = server.Get_Full_Queries(old_queries)

    new = read_yaml(in_file)
    server.Put_Queries(old_queries, new)
示例#6
0
def users(redash_url, api_key, in_file):
    if in_file is None:
        click.echo('No file provided')
        return

    users = []
    with open(in_file) as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:
            user = {
                'name': row[0] + ' ' + row[1],
                'email': row[2]
            }  # TODO validation
            users.append(user)

    server = redash.Redash(redash_url, api_key)

    dashboards = server.Create_Users(users)
示例#7
0
def dump(redash_url, api_key, out_file, split_file, out_path,
         include_dashboards):
    if split_file:
        if out_path is None:
            click.echo("No out path provided")
            return
    else:
        if out_file is None:
            click.echo("No out file provided")
            return

    server = redash.Redash(redash_url, api_key)
    queries = server.Get_Queries()
    queries = server.Get_Full_Queries(queries)

    if split_file:
        queries_path = os.path.join(out_path, "queries")
        if not os.path.exists(queries_path):
            os.makedirs(queries_path)

        for item in queries:
            save_yaml(
                item,
                os.path.join(
                    queries_path,
                    "%s-%s.yaml" % (item["id"], slugify(item["name"]))),
            )

        if include_dashboards:
            dashboards_path = os.path.join(out_path, "dashboards")
            if not os.path.exists(dashboards_path):
                os.makedirs(dashboards_path)

            for item in server.Get_Dashboards():
                save_yaml(
                    item,
                    os.path.join(
                        dashboards_path,
                        "%s-%s.yaml" % (item["id"], slugify(item["name"])),
                    ),
                )

    else:
        save_yaml(queries, out_file)
示例#8
0
def dump(redash_url, api_key, out_file, split_file, out_path,
         include_dashboards, id, auto_redpush_id):
    if split_file:
        if out_path is None:
            click.echo("No out path provided")
            return
    else:
        if out_file is None:
            click.echo("No out file provided")
            return

    server = redash.Redash(redash_url, api_key)

    if id:
        queries = server.Get_Queries()
        full_queries = server.Get_Full_Query_By_ID(id)
    else:
        queries = server.Get_Queries()
        full_queries = server.Get_Full_Queries(queries)

    if not full_queries:
        print("Queries not found, exit")
        return

    if auto_redpush_id:
        redpush_ids_list = [
            query["redpush_id"] for query in queries if "redpush_id" in query
        ]
        if not redpush_ids_list:
            max_redpush_id = 0
        else:
            max_redpush_id = server.getMaxOfList(redpush_ids_list)

        for query in full_queries:
            if "redpush_id" not in query:
                max_redpush_id += 1
                query["redpush_id"] = max_redpush_id

                visualizations = query["visualizations"]
                visualizations_ids_list = [
                    element["redpush_id"] for element in visualizations
                    if "redpush_id" in element
                ]
                element_redpush_id = 0
                if not element_redpush_id:
                    element_redpush_id = 0
                else:
                    element_redpush_id = server.getMaxOfList(
                        visualizations_ids_list)

                for element in visualizations:
                    if "redpush_id" not in element:
                        element_redpush_id += 1
                        element["redpush_id"] = element_redpush_id

        full_queries = sort_queries(full_queries)

    if split_file:
        queries_path = os.path.join(out_path, "queries")
        if not os.path.exists(queries_path):
            os.makedirs(queries_path)

        for item in full_queries:
            save_yaml(
                item,
                os.path.join(
                    queries_path,
                    "%s-%s.yaml" % (item["id"], slugify(item["name"]))),
            )

        if include_dashboards:
            dashboards_path = os.path.join(out_path, "dashboards")
            if not os.path.exists(dashboards_path):
                os.makedirs(dashboards_path)

            for item in server.Get_Dashboards():
                save_yaml(
                    item,
                    os.path.join(
                        dashboards_path,
                        "%s-%s.yaml" % (item["id"], slugify(item["name"])),
                    ),
                )

    else:
        save_yaml(full_queries, out_file)