Class: Gzr::Commands::Dashboard::Import
Instance Method Summary
collapse
-
#copy_result_maker_filterables(new_element) ⇒ Object
-
#execute(input: $stdin, output: $stdout) ⇒ Object
-
#initialize(file, dest_space_id, options) ⇒ Import
constructor
A new instance of Import.
-
#process_dashboard_element(dash_elem) ⇒ Object
-
#sync_dashboard(source, target_space_id) ⇒ Object
-
#sync_dashboard_element(new_element, existing_element, dashboard_id) ⇒ Object
-
#sync_dashboard_filter(new_filter, existing_filter, dashboard_id) ⇒ Object
-
#sync_dashboard_layout(dashboard_id, new_layout, existing_layout) ⇒ Object
-
#sync_dashboard_layout_component(source, target, elem_table) ⇒ Object
Methods included from FileHelper
#read_file, #write_file
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 User
#query_all_users, #query_me, #query_user, #search_users, #update_user
Methods included from Look
#create_fetch_query, #create_look, #create_merge_result, #delete_look, #query_look, #search_looks_by_slug, #search_looks_by_title, #update_look, #upsert_look
Methods included from Dashboard
#create_dashboard, #create_dashboard_element, #create_dashboard_filter, #create_dashboard_layout, #delete_dashboard, #delete_dashboard_element, #delete_dashboard_filter, #delete_dashboard_layout, #query_dashboard, #search_dashboards_by_slug, #search_dashboards_by_title, #update_dashboard, #update_dashboard_element, #update_dashboard_filter, #update_dashboard_layout, #update_dashboard_layout_component
#create_merge_query, #create_query, #field_expression, #field_names, #keys_to_keep, #merge_query, #pairs, #query, #render_csv, #run_inline_query
Methods included from Session
#build_connection_hash, #login, #logout_all, #pastel, #say_error, #say_ok, #say_warning, #v3_1_available?, #with_session
Constructor Details
#initialize(file, dest_space_id, options) ⇒ Import
Returns a new instance of Import.
41
42
43
44
45
46
|
# File 'lib/gzr/commands/dashboard/import.rb', line 41
def initialize(file, dest_space_id, options)
super()
@file = file
@dest_space_id = dest_space_id
@options = options
end
|
Instance Method Details
#copy_result_maker_filterables(new_element) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/gzr/commands/dashboard/import.rb', line 150
def copy_result_maker_filterables(new_element)
return nil unless new_element[:result_maker]
if new_element[:result_maker].fetch(:filterables,[]).length > 0
result_maker = { :filterables => [] }
new_element[:result_maker][:filterables].each do |filterable|
result_maker[:filterables] << filterable.select do |k,v|
true unless [:can].include? k
end
end
return result_maker
end
nil
end
|
#execute(input: $stdin, output: $stdout) ⇒ Object
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
|
# File 'lib/gzr/commands/dashboard/import.rb', line 48
def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session("3.1") do
@me ||= query_me("id")
read_file(@file) do |data|
dashboard = sync_dashboard(data,@dest_space_id)
source_filters = data[:dashboard_filters].sort { |a,b| a[:row] <=> b[:row] }
existing_filters = dashboard.dashboard_filters.sort { |a,b| a.row <=> b.row }
existing_filters.collect! do |e|
matches_by_name_title = source_filters.select { |s| s[:row] != e.row && (s[:title] == e.title || s[:name] == e.name) }
if matches_by_name_title.length > 0
delete_dashboard_filter(e.id)
nil
else
e
end
end
pairs(source_filters,existing_filters,dashboard.id) do |source,target,id|
say_warning "Synching dashboard filter for dashboard #{id}" if @options[:debug]
sync_dashboard_filter(source,target,id)
end
elem_table = pairs(data[:dashboard_elements],dashboard.dashboard_elements,dashboard.id) do |source,target,id|
sync_dashboard_element(source,target,id)
end
source_dashboard_layouts = data[:dashboard_layouts].sort_by { |v| (v[:active] ? 0 : 1) }
existing_dashboard_layouts = dashboard.dashboard_layouts.sort_by { |v| (v.active ? 0 : 1) }
pairs(source_dashboard_layouts,existing_dashboard_layouts) do |s,t|
sync_dashboard_layout(dashboard.id,s,t) do |s,t|
sync_dashboard_layout_component(s,t,elem_table)
end
end
upsert_plans_for_dashboard(dashboard.id,@me.id,data[:scheduled_plans]) if data[:scheduled_plans]
output.puts "Imported dashboard #{dashboard.id}" unless @options[:plain]
output.puts dashboard.id if @options[:plain]
end
end
end
|
#process_dashboard_element(dash_elem) ⇒ Object
196
197
198
199
200
201
|
# File 'lib/gzr/commands/dashboard/import.rb', line 196
def process_dashboard_element(dash_elem)
return [create_fetch_query(dash_elem[:query]).id, nil, nil] if dash_elem[:query]
return [nil, upsert_look(@me.id, create_fetch_query(dash_elem[:look][:query]).id, @dest_space_id, dash_elem[:look]).id, nil] if dash_elem[:look]
return [nil,nil,create_merge_result(dash_elem[:merge_result]).id] if dash_elem[:merge_result]
[nil,nil,nil]
end
|
#sync_dashboard(source, target_space_id) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/gzr/commands/dashboard/import.rb', line 92
def sync_dashboard(source,target_space_id)
existing_dashboard = search_dashboards_by_title(source[:title], target_space_id).fetch(0,nil)
slug_used = search_dashboards_by_slug(source[:slug]).fetch(0,nil) if source[:slug]
if slug_used then
if existing_dashboard then
if !(existing_dashboard.space_id == slug_used.space_id && existing_dashboard.title == slug_used.title) then
say_warning "slug #{slug_used.slug} already used for dashboard #{slug_used.title} in space #{slug_used.space_id}"
say_warning "dashboard will be imported with new slug"
end
else
say_warning "slug #{slug_used.slug} already used for dashboard #{slug_used.title} in space #{slug_used.space_id}"
say_warning "dashboard will be imported with new slug"
end
end
if existing_dashboard then
if @options[:force] then
say_ok "Modifying existing dashboard #{existing_dashboard.id} #{existing_dashboard[:title]} in space #{target_space_id}"
new_dash = source.select do |k,v|
(keys_to_keep('update_dashboard') - [:space_id,:user_id,:title,:slug]).include? k
end
new_dash[:slug] = source[:slug] unless slug_used
return update_dashboard(existing_dashboard.id,new_dash)
else
raise Gzr::CLI::Error, "Dashboard #{source[:title]} already exists in space #{target_space_id}\nUse --force if you want to overwrite it"
end
else
new_dash = source.select do |k,v|
(keys_to_keep('create_dashboard') - [:space_id,:user_id,:slug]).include? k
end
new_dash[:slug] = source[:slug] unless slug_used
new_dash[:space_id] = target_space_id
new_dash[:user_id] = @me.id
return create_dashboard(new_dash)
end
end
|
#sync_dashboard_element(new_element, existing_element, dashboard_id) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/gzr/commands/dashboard/import.rb', line 164
def sync_dashboard_element(new_element,existing_element,dashboard_id)
if new_element && !existing_element then
element = new_element.select do |k,v|
(keys_to_keep('create_dashboard_element') - [:dashboard_id, :look_id, :query_id, :merge_result_id]).include? k
end
(element[:query_id],element[:look_id],element[:merge_result_id]) = process_dashboard_element(new_element)
say_warning "Creating dashboard element #{element.inspect}" if @options[:debug]
element[:dashboard_id] = dashboard_id
result_maker = copy_result_maker_filterables(new_element)
element[:result_maker] = result_maker if result_maker
return [new_element[:id], create_dashboard_element(element).id]
end
if existing_element && new_element then
element = keys_to_keep('update_dashboard_element').collect do |e|
[e,nil]
end.to_h
element.merge!( new_element.select do |k,v|
(keys_to_keep('update_dashboard_element') - [:dashboard_id, :look_id, :query_id, :merge_result_id]).include? k
end
)
(element[:query_id],element[:look_id],element[:merge_result_id]) = process_dashboard_element(new_element)
say_warning "Updating dashboard element #{existing_element.id}" if @options[:debug]
result_maker = copy_result_maker_filterables(new_element)
element[:result_maker] = result_maker if result_maker
return [new_element[:id], update_dashboard_element(existing_element.id,element).id]
end
say_warning "Deleting dashboard element #{existing_element.id}" if @options[:debug]
delete_dashboard_element(existing_element.id)
return [nil,existing_element.id]
end
|
#sync_dashboard_filter(new_filter, existing_filter, dashboard_id) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/gzr/commands/dashboard/import.rb', line 130
def sync_dashboard_filter(new_filter,existing_filter,dashboard_id)
if new_filter && !existing_filter then
filter = new_filter.select do |k,v|
(keys_to_keep('create_dashboard_filter') + [:row]).include? k
end
filter[:dashboard_id] = dashboard_id
say_warning "Creating filter" if @options[:debug]
return create_dashboard_filter(filter)
end
if existing_filter && new_filter then
filter = new_filter.select do |k,v|
(keys_to_keep('update_dashboard_filter') + [:row]).include? k
end
say_warning "Updating filter #{existing_filter.id}" if @options[:debug]
return update_dashboard_filter(existing_filter.id,filter)
end
say_warning "Deleting filter #{existing_filter.id}" if @options[:debug]
return delete_dashboard_filter(existing_filter.id)
end
|
#sync_dashboard_layout(dashboard_id, new_layout, existing_layout) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/gzr/commands/dashboard/import.rb', line 203
def sync_dashboard_layout(dashboard_id,new_layout,existing_layout)
layout_obj = nil
if new_layout && !existing_layout then
layout = new_layout.select do |k,v|
(keys_to_keep('create_dashboard_layout') - [:dashboard_id]).include? k
end
layout[:dashboard_id] = dashboard_id
say_warning "Creating dashboard layout #{layout}" if @options[:debug]
layout_obj = create_dashboard_layout(layout)
end
if new_layout && existing_layout then
layout = new_layout.select do |k,v|
(keys_to_keep('update_dashboard_layout') - [:dashboard_id]).include? k
end
say_warning "Updating dashboard layout #{existing_layout.id}" if @options[:debug]
layout_obj = update_dashboard_layout(existing_layout.id,layout)
end
if !new_layout && existing_layout then
say_warning "Deleting dashboard layout #{existing_layout.id}" if @options[:debug]
delete_dashboard_layout(existing_layout.id)
end
return unless layout_obj
layout_components = new_layout[:dashboard_layout_components].zip(layout_obj.dashboard_layout_components)
return layout_components unless block_given?
layout_components.each { |s,t| yield(s,t) }
end
|
#sync_dashboard_layout_component(source, target, elem_table) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/gzr/commands/dashboard/import.rb', line 239
def sync_dashboard_layout_component(source, target, elem_table)
component = keys_to_keep('update_dashboard_layout_component').collect do |e|
[e,nil]
end.to_h
component[:dashboard_layout_id] = target.dashboard_layout_id
component.merge!(source.select do |k,v|
(keys_to_keep('update_dashboard_layout_component') - [:id,:dashboard_layout_id]).include? k
end)
component[:dashboard_element_id] = elem_table.assoc(source[:dashboard_element_id])[1]
say_warning "Updating dashboard layout component #{target.id}" if @options[:debug]
update_dashboard_layout_component(target.id,component)
end
|