Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --trim option for dashboard cat, look cat, and folder export commands #178

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/gzr/commands/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def mv(dashboard_id, target_folder_id)
method_option :transform, type: :string,
desc: 'Fully-qualified path to a JSON file describing the transformations to apply'
method_option :simple_filename, type: :boolean,
desc: 'Use simple filename for output (Dashboard_<id>.json)'
desc: 'Use simple filename for output (Dashboard_<id>.json)'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
drstrangelooker marked this conversation as resolved.
Show resolved Hide resolved
def cat(dashboard_id)
if options[:help]
invoke :help, ['cat']
Expand Down
1 change: 1 addition & 0 deletions lib/gzr/commands/dashboard/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def execute(*args, input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session do
data = cat_dashboard(@dashboard_id)
data = trim_dashboard(data) if @options[:trim]

replacements = {}
if @options[:transform]
Expand Down
2 changes: 2 additions & 0 deletions lib/gzr/commands/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def top(*)
desc: 'Display usage information'
method_option :plans, type: :boolean,
desc: 'Include scheduled plans'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
method_option :dir, type: :string, default: '.',
desc: 'Directory to store output tree'
method_option :tar, type: :string,
Expand Down
2 changes: 2 additions & 0 deletions lib/gzr/commands/folder/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ def process_folder(folder_id, base, rel_path = nil)
end
folder[:looks].each do |l|
look = cat_look(l[:id])
look = trim_look(look) if @options[:trim]
write_file("Look_#{look[:id]}_#{look[:title]}.json", base, path) do |f|
f.write JSON.pretty_generate(look)
end
end
folder[:dashboards].each do |d|
data = cat_dashboard(d[:id])
data = trim_dashboard(data) if @options[:trim]
write_file("Dashboard_#{data[:id]}_#{data[:title]}.json", base, path) do |f|
f.write JSON.pretty_generate(data)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/gzr/commands/look.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def import(file,dest_folder_id)
desc: 'Include scheduled plans'
method_option :simple_filename, type: :boolean,
desc: 'Use simple filename for output (Look_<id>.json)'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
def cat(look_id)
if options[:help]
invoke :help, ['cat']
Expand Down
1 change: 1 addition & 0 deletions lib/gzr/commands/look/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session do
data = cat_look(@look_id)
data = trim_look(data) if @options[:trim]
file_name = if @options[:dir]
@options[:simple_filename] ? "Look_#{data[:id]}.json" : "Look_#{data[:id]}_#{data[:title]}.json"
else
Expand Down
67 changes: 67 additions & 0 deletions lib/gzr/modules/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,72 @@ def cat_dashboard(dashboard_id)
data[:scheduled_plans] = query_scheduled_plans_for_dashboard(@dashboard_id,"all") if @options[:plans]
data
end

def trim_dashboard(data)
trimmed = data.select do |k,v|
(keys_to_keep('update_dashboard') + [:id,:dashboard_elements,:dashboard_filters,:dashboard_layouts,:scheduled_plans]).include? k
end

trimmed[:dashboard_elements] = data[:dashboard_elements].map do |de|
new_de = de.select do |k,v|
(keys_to_keep('update_dashboard_element') + [:id,:look,:query,:merge_result]).include? k
end
if de[:look]
new_de[:look] = de[:look].select do |k,v|
(keys_to_keep('update_look') + [:id,:query]).include? k
end
if de[:look][:query]
new_de[:look][:query] = de[:look][:query].select do |k,v|
(keys_to_keep('create_query') + [:id]).include? k
end
end
end
if de[:query]
new_de[:query] = de[:query].select do |k,v|
(keys_to_keep('create_query') + [:id]).include? k
end
end
if de[:merge_result]
new_de[:merge_result] = de[:merge_result].select do |k,v|
(keys_to_keep('update_merge_query') + [:id]).include? k
end
new_de[:merge_result][:source_queries] = de[:merge_result][:source_queries].map do |sq|
sq.select do |k,v|
(keys_to_keep('create_query') + [:id]).include? k
end
end
end
new_de
end

trimmed[:dashboard_filters] = data[:dashboard_filters].map do |df|
new_df = df.select do |k,v|
keys_to_keep('update_dashboard_filter').include? k
end
new_df
end

trimmed[:dashboard_layouts] = data[:dashboard_layouts].map do |dl|
new_dl = dl.select do |k,v|
(keys_to_keep('update_dashboard_layout') + [:id]).include? k
end
if dl[:dashboard_layout_components]
new_dl[:dashboard_layout_components] = dl[:dashboard_layout_components].map do |dlc|
dlc.select do |k,v|
(keys_to_keep('update_dashboard_layout_component') + [:id]).include? k
end
end
end
new_dl
end

trimmed[:scheduled_plans] = data[:scheduled_plans].map do |sp|
sp.select do |k,v|
(keys_to_keep('create_scheduled_plan') + [:id]).include? k
end
end if data[:scheduled_plans]

trimmed
end
end
end
19 changes: 18 additions & 1 deletion lib/gzr/modules/look.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,25 @@ def cat_look(look_id)
end
end

data[:scheduled_plans] = query_scheduled_plans_for_look(@look_id,"all")&.to_attrs if @options[:plans]
data[:scheduled_plans] = query_scheduled_plans_for_look(@look_id,"all").map { |e| e.to_attrs } if @options[:plans]
data
end

def trim_look(data)
trimmed = data.select do |k,v|
(keys_to_keep('update_look') + [:id,:query]).include? k
end
trimmed[:query] = data[:query].select do |k,v|
(keys_to_keep('create_query') + [:id]).include? k
end

trimmed[:scheduled_plans] = data[:scheduled_plans].map do |sp|
sp.select do |k,v|
(keys_to_keep('create_scheduled_plan') + [:id]).include? k
end
end if data[:scheduled_plans]

trimmed
end
end
end
1 change: 1 addition & 0 deletions spec/integration/dashboard/cat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
[--plans], [--no-plans] # Include scheduled plans
[--transform=TRANSFORM] # Fully-qualified path to a JSON file describing the transformations to apply
[--simple-filename], [--no-simple-filename] # Use simple filename for output (Dashboard_<id>.json)
[--trim], [--no-trim] # Trim output to minimal set of fields for later import

Output the JSON representation of a dashboard to the screen or a file
OUT
Expand Down
1 change: 1 addition & 0 deletions spec/integration/folder/export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Options:
-h, [--help], [--no-help] # Display usage information
[--plans], [--no-plans] # Include scheduled plans
[--trim], [--no-trim] # Trim output to minimal set of fields for later import
[--dir=DIR] # Directory to store output tree
# Default: .
[--tar=TAR] # Tar file to store output
Expand Down
1 change: 1 addition & 0 deletions spec/integration/look/cat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[--dir=DIR] # Directory to store output file
[--plans], [--no-plans] # Include scheduled plans
[--simple-filename], [--no-simple-filename] # Use simple filename for output (Look_<id>.json)
[--trim], [--no-trim] # Trim output to minimal set of fields for later import

Output the JSON representation of a look to the screen or a file
OUT
Expand Down