Skip to content

Instantly share code, notes, and snippets.

@tsg
Created July 3, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsg/23534eb1822d1e84947731c38194be1f to your computer and use it in GitHub Desktop.
Save tsg/23534eb1822d1e84947731c38194be1f to your computer and use it in GitHub Desktop.
Split Kibana ndjson into search/visualization/dashboard as expected by the package registry
import json
import sys
import os
# create dest directories
def ensure_directories(dest_dir):
def ensure_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
ensure_dir(os.path.join(dest_dir, "dashboard"))
ensure_dir(os.path.join(dest_dir, "search"))
ensure_dir(os.path.join(dest_dir, "visualization"))
if __name__ == "__main__":
source_file = sys.argv[1]
dest_dir = sys.argv[2]
ensure_directories(dest_dir)
with open(source_file, "r") as fread:
for line in fread:
saved_object = json.loads(line)
type_ = saved_object.get("type")
id_ = saved_object.get("id")
if type_ == "dashboard":
fname = os.path.join(dest_dir, "dashboard", id_ + ".json")
elif type_ == "search":
fname = os.path.join(dest_dir, "search", id_ + ".json")
elif type_ == "visualization":
fname = os.path.join(dest_dir, "visualization", id_ + ".json")
else:
continue
with open(fname, "w") as fwrite:
json.dump(saved_object, fwrite, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment