Module: Gzr::Look

Instance Method Summary collapse

Instance Method Details

#cat_look(look_id) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gzr/modules/look.rb', line 195

def cat_look(look_id)
  data = query_look(look_id)
  find_vis_config_reference(data) do |vis_config|
    find_color_palette_reference(vis_config) do |o,default_colors|
      rewrite_color_palette!(o,default_colors)
    end
  end

  data[:scheduled_plans] = query_scheduled_plans_for_look(@look_id,"all") if @options[:plans]
  data
end

#create_fetch_query(source_query) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gzr/modules/look.rb', line 164

def create_fetch_query(source_query)
  new_query = source_query.select do |k,v|
    (keys_to_keep('create_query') - [:client_id]).include? k
  end
  find_vis_config_reference(new_query) do |vis_config|
    find_color_palette_reference(vis_config) do |o,default_colors|
      update_color_palette!(o,default_colors)
    end
  end
  return create_query(new_query)
end

#create_look(look) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/gzr/modules/look.rb', line 72

def create_look(look)
  begin
    look[:public] = false unless look[:public]
    @sdk.create_look(look).to_attrs
  rescue LookerSDK::Error => e
    say_error "Error creating look(#{JSON.pretty_generate(look)})"
    say_error e
    raise
  end
end

#create_merge_result(merge_result) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gzr/modules/look.rb', line 176

def create_merge_result(merge_result)
  new_merge_result = merge_result.select do |k,v|
    (keys_to_keep('create_merge_query') - [:client_id,:source_queries]).include? k
  end
  new_merge_result[:source_queries] = merge_result[:source_queries].map do |query|
    new_query = {}
    new_query[:query_id] = create_fetch_query(query[:query]).id
    new_query[:name] = query[:name]
    new_query[:merge_fields] = query[:merge_fields]
    new_query
  end
  find_vis_config_reference(new_merge_result) do |vis_config|
    find_color_palette_reference(vis_config) do |o,default_colors|
      update_color_palette!(o,default_colors)
    end
  end
  return create_merge_query(new_merge_result)
end

#delete_look(look_id) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/gzr/modules/look.rb', line 97

def delete_look(look_id)
  begin
    @sdk.delete_look(look_id)
  rescue LookerSDK::Error => e
    say_error "Error deleting look(#{look_id})"
    say_error e
    raise
  end
end

#query_look(look_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gzr/modules/look.rb', line 26

def query_look(look_id)
  begin
    @sdk.look(look_id).to_attrs
  rescue LookerSDK::NotFound => e
      say_error "look(#{look_id}) not found"
      say_error e
      raise
  rescue LookerSDK::Error => e
      say_error "Error querying look(#{look_id})"
      say_error e
      raise
  end
end

#search_looks_by_slug(slug, folder_id = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gzr/modules/look.rb', line 40

def search_looks_by_slug(slug, folder_id=nil)
  data = []
  begin
    req = { :slug => slug }
    req[:folder_id] = folder_id if folder_id
    data = @sdk.search_looks(req).collect { |l| l.to_attrs }
    req[:deleted] = true
    data = @sdk.search_looks(req).collect { |l| l.to_attrs } if data.empty?
  rescue LookerSDK::Error => e
    say_error "Error search_looks_by_slug(#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
  data
end

#search_looks_by_title(title, folder_id = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gzr/modules/look.rb', line 56

def search_looks_by_title(title, folder_id=nil)
  data = []
  begin
    req = { :title => title }
    req[:folder_id] = folder_id if folder_id
    data = @sdk.search_looks(req).collect { |l| l.to_attrs }
    req[:deleted] = true
    data = @sdk.search_looks(req).collect { |l| l.to_attrs } if data.empty?
  rescue LookerSDK::Error => e
    say_error "Error search_looks_by_title(#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
  data
end

#trim_look(data) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/gzr/modules/look.rb', line 207

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

#update_look(id, look) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gzr/modules/look.rb', line 83

def update_look(id,look)
  begin
    @sdk.update_look(id,look).to_attrs
  rescue LookerSDK::NotFound => e
    say_error "look(#{id}) not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Error updating look(#{id},#{JSON.pretty_generate(look)})"
    say_error e
    raise
  end
end

#upsert_look(user_id, query_id, folder_id, source, output: $stdout) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gzr/modules/look.rb', line 107

def upsert_look(user_id, query_id, folder_id, source, output: $stdout)
  # try to find look by slug in target folder
  existing_look = search_looks_by_slug(source[:slug], folder_id).fetch(0,nil) if source[:slug]
  # check for look of same title in target folder
  title_used = search_looks_by_title(source[:title], folder_id).fetch(0,nil)

  # If there is no match by slug in target folder or no slug given, then we match by title
  existing_look ||= title_used

  # same_title is now a flag indicating that there is already a look in the same folder with
  # that title, and it is the one we are updating.
  same_title = (title_used&.fetch(:id,nil) == existing_look&.fetch(:id,nil))

  # check if the slug is used by any look
  slug_used = search_looks_by_slug(source[:slug]).fetch(0,nil) if source[:slug]

  # same_slug is now a flag indicating that there is already a look with
  # that slug, but it is the one we are updating.
  same_slug = (slug_used&.fetch(:id,nil) == existing_look&.fetch(:id,nil))

  if slug_used && !same_slug then
    say_warning "slug #{slug_used[:slug]} already used for look #{slug_used[:title]} in folder #{slug_used[:folder_id]}", output: output
    say_warning("That look is in the 'Trash' but not fully deleted yet", output: output) if slug_used[:deleted]
    say_warning "look will be imported with new slug", output: output
  end

  if existing_look then
    if title_used && !same_title then
      raise Gzr::CLI::Error, "Look #{source[:title]} already exists in folder #{folder_id}\nDelete it before trying to upate another Look to have that title."
    end
    raise Gzr::CLI::Error, "Look #{existing_look[:title]} with slug #{existing_look[:slug]} already exists in folder #{folder_id}\nUse --force if you want to overwrite it" unless @options[:force]
    say_ok "Modifying existing Look #{existing_look[:id]} #{existing_look[:title]} in folder #{folder_id}", output: output
    new_look = source.select do |k,v|
      (keys_to_keep('update_look') - [:space_id,:folder_id,:user_id,:query_id,:slug]).include? k
    end
    new_look[:slug] = source[:slug] if source[:slug] && !slug_used
    new_look[:deleted] = false if existing_look[:deleted]
    new_look[:query_id] = query_id
    return update_look(existing_look[:id],new_look)
  else
    new_look = source.select do |k,v|
      (keys_to_keep('create_look') - [:space_id,:folder_id,:user_id,:query_id,:slug]).include? k
    end
    new_look[:slug] = source[:slug] unless slug_used
    new_look[:query_id] = query_id
    new_look[:user_id] = user_id
    new_look[:folder_id] = folder_id

    find_vis_config_reference(new_look) do |vis_config|
      find_color_palette_reference(vis_config) do |o,default_colors|
        update_color_palette!(o,default_colors)
      end
    end
    return create_look(new_look)
  end
end