Class: Gzr::Commands::Dashboard::Cat

Inherits:
Gzr::Command show all
Includes:
Dashboard, FileHelper, Plan
Defined in:
lib/gzr/commands/dashboard/cat.rb

Instance Method Summary collapse

Methods included from Plan

#create_scheduled_plan, #delete_scheduled_plan, #query_all_scheduled_plans, #query_scheduled_plan, #query_scheduled_plans_for_dashboard, #query_scheduled_plans_for_look, #run_scheduled_plan, #update_scheduled_plan, #upsert_plan_for_dashboard, #upsert_plan_for_look, #upsert_plan_for_obj, #upsert_plans_for_dashboard, #upsert_plans_for_look, #upsert_plans_for_obj

Methods included from FileHelper

#read_file, #write_file

Methods included from Dashboard

#cat_dashboard, #create_dashboard, #create_dashboard_element, #create_dashboard_filter, #create_dashboard_layout, #delete_dashboard, #delete_dashboard_element, #delete_dashboard_filter, #delete_dashboard_layout, #get_all_dashboard_layout_components, #get_dashboard_layout, #import_lookml_dashboard, #query_dashboard, #search_dashboards_by_slug, #search_dashboards_by_title, #sync_lookml_dashboard, #trim_dashboard, #update_dashboard, #update_dashboard_element, #update_dashboard_filter, #update_dashboard_layout, #update_dashboard_layout_component

Methods included from Alert

#alert_notifications, #create_alert, #delete_alert, #follow_alert, #get_alert, #read_alert_notification, #search_alerts, #unfollow_alert, #update_alert, #update_alert_field

Methods inherited from Gzr::Command

#all_color_collections, #color_collection, #color_palette_lookup!, #create_merge_query, #create_query, #default_color_collection, #field_expressions_eval, #field_names, #find_color_palette_reference, #find_vis_config_reference, #get_auth, #get_user_by_id, #keys_to_keep, #keys_to_keep_internal, #merge_query, #pairs, #query, #render_csv, #rewrite_color_palette!, #run_inline_query, #update_color_palette!

Methods included from Session

#build_connection_hash, #login, #logout_all, #pastel, #read_token_data, #say_error, #say_ok, #say_warning, #sufficient_version?, #token_file, #update_auth, #with_session, #write_token_data

Constructor Details

#initialize(dashboard_id, options) ⇒ Cat

Returns a new instance of Cat.



37
38
39
40
41
# File 'lib/gzr/commands/dashboard/cat.rb', line 37

def initialize(dashboard_id,options)
  super()
  @dashboard_id = dashboard_id
  @options = options
end

Instance Method Details

#execute(*args, input: $stdin, output: $stdout) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gzr/commands/dashboard/cat.rb', line 43

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]

      max_row = 0
      data[:dashboard_layouts][0][:dashboard_layout_components].each_index do |i|
        component = data[:dashboard_layouts][0][:dashboard_layout_components][i]
        if component[:row] + component[:height] > max_row
          max_row = component[:row] + component[:height] + 4
        end
      end

      read_file(@options[:transform]) do |transform|
        i = 0
        transform[:dashboard_elements].collect! do |e|
          i += 1
          # there is no significance to 9900, we just need to prevent ID collisions
          e['id'] = 9900 + i
          data[:dashboard_elements] << e

          # when inserting an element into the top row, shift the other elements down according to the height of the inserted element
          if e[:position] === 'top'
            data[:dashboard_layouts][0][:dashboard_layout_components].each_index do |i|
              component = data[:dashboard_layouts][0][:dashboard_layout_components][i]
              component[:row] = component[:row] + e[:height]
            end
            row = '0'
          elsif e[:position] === 'bottom'
            row = max_row.to_s
          end

          column = '0'
          width = e[:width].to_s
          height = e[:height].to_s
          data[:dashboard_layouts][0][:dashboard_layout_components] << JSON.parse('{"id":' + e['id'].to_s + ',"dashboard_layout_id":1,"dashboard_element_id":' + e['id'].to_s + ''',"row":' + row + ',"column":' + column + ',"width":' + width + ',"height":' + height + ',"deleted":false,"element_title_hidden":false,"vis_type":"' + e[:type].to_s + '","can":{"index":true,"show":true,"create":true,"update":true,"destroy":true}}')
        end

        transform[:replacements].collect! do |e|
          replacements[e.keys.first.to_s] = e[e.keys[0]]
        end

      end
    end

    outputJSON = JSON.pretty_generate(data)

    replacements.each do |k,v|
      say_warning("Replaced #{k} with #{v}") if @options[:debug]
      outputJSON.gsub! k,v
    end

    file_name = if @options[:dir]
                  @options[:simple_filename] ? "Dashboard_#{data[:id]}.json" : "Dashboard_#{data[:id]}_#{data[:title]}.json"
                else
                  nil
                end
    write_file(file_name, @options[:dir], nil, output) do |f|
      f.puts outputJSON
    end
  end
end