Module: Gzr::Plan

Instance Method Summary collapse

Instance Method Details

#create_scheduled_plan(plan) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/gzr/modules/plan.rb', line 107

def create_scheduled_plan(plan)
  begin
    data = @sdk.create_scheduled_plan(plan)
  rescue LookerSDK::Error => e
    say_error "Error creating scheduled_plan(#{JSON.pretty_generate(plan)})"
    say_error e.message
    raise
  end
  data
end

#delete_scheduled_plan(plan_id) ⇒ Object



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

def delete_scheduled_plan(plan_id)
  begin
    data = @sdk.delete_scheduled_plan(plan_id)
  rescue LookerSDK::ClientError => e
    say_error "Unable to delete scheduled_plan(#{plan_id})"
    say_error e.message
    raise
  end
  data
end

#query_all_scheduled_plans(user_id, fields = nil) ⇒ Object



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

def query_all_scheduled_plans(user_id,fields=nil)
  req = {}
  req[:all_users] = true if user_id == "all"
  req[:user_id] = user_id if user_id && !(user_id == "all")
  req[:fields] = fields if fields
  data = nil
  id = nil
  id = user_id unless user_id == "all"
  begin
    data = @sdk.all_scheduled_plans(req)
  rescue LookerSDK::ClientError => e
    say_error "Unable to get all_scheduled_plans(#{JSON.pretty_generate(req)})"
    say_error e.message
    raise
  end
  data
end

#query_scheduled_plan(plan_id, fields = nil) ⇒ Object



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

def query_scheduled_plan(plan_id,fields=nil)
  req = {}
  req[:fields] = fields if fields
  begin
    data = @sdk.scheduled_plan(plan_id,req)
  rescue LookerSDK::ClientError => e
    say_error "Unable to get scheduled_plan(#{plan_id},#{JSON.pretty_generate(req)})"
    say_error e.message
    raise
  end
  data
end

#query_scheduled_plans_for_dashboard(dashboard_id, user_id, fields = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gzr/modules/plan.rb', line 63

def query_scheduled_plans_for_dashboard(dashboard_id,user_id,fields=nil)
  req = {}
  req[:all_users] = true if user_id == "all"
  req[:user_id] = user_id if user_id && !(user_id == "all")
  req[:fields] = fields if fields
  data = nil
  begin
    data = @sdk.scheduled_plans_for_dashboard(dashboard_id,req)
    return nil if data.respond_to?(:message) && data.message == 'Not found'
  rescue LookerSDK::NotFound
    return nil
  rescue LookerSDK::ClientError => e
    say_error "Unable to get scheduled_plans_for_dashboard(#{dashboard_id},#{JSON.pretty_generate(req)})"
    say_error e.message
    raise
  end
  data.map! {|plan| plan.to_attrs}
  data
end

#query_scheduled_plans_for_look(look_id, user_id, fields = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gzr/modules/plan.rb', line 44

def query_scheduled_plans_for_look(look_id,user_id,fields=nil)
  req = {}
  req[:all_users] = true if user_id == "all"
  req[:user_id] = user_id if user_id && !(user_id == "all")
  req[:fields] = fields if fields
  data = nil
  begin
    data = @sdk.scheduled_plans_for_look(look_id,req)
    return nil if data.respond_to?(:message) && data.message == 'Not found'
  rescue LookerSDK::NotFound
    return nil
  rescue LookerSDK::ClientError => e
    say_error "Unable to get scheduled_plans_for_look(#{look_id},#{JSON.pretty_generate(req)})"
    say_error e.message
    raise
  end
  data
end

#run_scheduled_plan(plan) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/gzr/modules/plan.rb', line 129

def run_scheduled_plan(plan)
  begin
    data = @sdk.scheduled_plan_run_once(plan)
  rescue LookerSDK::Error => e
    say_error "Error executing scheduled_plan_run_once(#{JSON.pretty_generate(plan)})"
    say_error e.message
    raise
  end
  data
end

#update_scheduled_plan(plan_id, plan) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/gzr/modules/plan.rb', line 118

def update_scheduled_plan(plan_id,plan)
  begin
    data = @sdk.update_scheduled_plan(plan_id,plan)
  rescue LookerSDK::Error => e
    say_error "Error updating scheduled_plan(#{plan_id},#{JSON.pretty_generate(plan)})"
    say_error e.message
    raise
  end
  data
end

#upsert_plan_for_dashboard(dashboard_id, user_id, source_plan) ⇒ Object



155
156
157
158
# File 'lib/gzr/modules/plan.rb', line 155

def upsert_plan_for_dashboard(dashboard_id,user_id,source_plan)
  existing_plans = query_scheduled_plans_for_dashboard(dashboard_id,"all")
  upsert_plan_for_obj(user_id,source_plan,existing_plans) { |p| p[:dashboard_id] = dashboard_id }
end

#upsert_plan_for_look(look_id, user_id, source_plan) ⇒ Object



150
151
152
153
# File 'lib/gzr/modules/plan.rb', line 150

def upsert_plan_for_look(look_id,user_id,source_plan)
  existing_plans = query_scheduled_plans_for_look(look_id,"all")
  upsert_plan_for_obj(user_id,source_plan,existing_plans) { |p| p[:look_id] = look_id }
end

#upsert_plan_for_obj(user_id, source_plan, existing_plans) ⇒ Object



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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/gzr/modules/plan.rb', line 169

def upsert_plan_for_obj(user_id, source_plan, existing_plans)
  matches = existing_plans.select { |p| p.name == source_plan[:name] && user_id == p.user_id }
  if matches.length > 0 then
    say_ok "Modifying existing plan #{matches.first.id} #{matches.first.name}"
    plan = keys_to_keep('update_scheduled_plan').collect do |e|
      [e,nil]
    end.to_h

    plan.merge!( source_plan.select do |k,v|
      (keys_to_keep('update_scheduled_plan') - [:plan_id,:look_id,:dashboard_id,:user_id,:dashboard_filters,:lookml_dashboard_id,:scheduled_plan_destination]).include? k
    end)
    plan[:scheduled_plan_destination] = source_plan[:scheduled_plan_destination].collect do |p|
      p.reject do |k,v|
        [:id,:scheduled_plan_id,:looker_recipient,:can].include? k
      end
    end
    plan[:user_id] = user_id
    plan[:enabled] = true if @options[:enabled]
    plan[:enabled] = false if @options[:disabled]

    plan[:enabled] = false if plan[:enabled].nil?
    plan[:require_results] = false if plan[:require_results].nil?
    plan[:require_no_results] = false if plan[:require_no_results].nil?
    plan[:require_change] = false if plan[:require_change].nil?
    plan[:send_all_results] = false if plan[:send_all_results].nil?
    plan[:run_once] = false if plan[:run_once].nil?
    plan[:include_links] = false if plan[:include_links].nil?
    yield plan
    update_scheduled_plan(matches.first.id,plan)
  else
    plan = source_plan.select do |k,v|
      (keys_to_keep('create_scheduled_plan') - [:plan_id,:dashboard_id,:user_id,:dashboard_filters,:lookml_dashboard_id,:scheduled_plan_destination]).include? k
    end
    plan[:scheduled_plan_destination] = source_plan[:scheduled_plan_destination].collect do |p|
      p.reject do |k,v|
        [:id,:scheduled_plan_id,:looker_recipient,:can].include? k
      end
    end

    yield plan
    plan[:enabled] = true if @options[:enabled]
    plan[:enabled] = false if @options[:disabled]

    plan[:enabled] = false if plan[:enabled].nil?
    plan[:require_results] = false if plan[:require_results].nil?
    plan[:require_no_results] = false if plan[:require_no_results].nil?
    plan[:require_change] = false if plan[:require_change].nil?
    plan[:send_all_results] = false if plan[:send_all_results].nil?
    plan[:run_once] = false if plan[:run_once].nil?
    plan[:include_links] = false if plan[:include_links].nil?
    create_scheduled_plan(plan)
  end
end

#upsert_plans_for_dashboard(dashboard_id, user_id, source_plans) ⇒ Object



145
146
147
148
# File 'lib/gzr/modules/plan.rb', line 145

def upsert_plans_for_dashboard(dashboard_id,user_id,source_plans)
  existing_plans = query_scheduled_plans_for_dashboard(dashboard_id,"all")
  upsert_plans_for_obj(user_id,source_plans,existing_plans) { |p| p[:dashboard_id] = dashboard_id }
end

#upsert_plans_for_look(look_id, user_id, source_plans) ⇒ Object



140
141
142
143
# File 'lib/gzr/modules/plan.rb', line 140

def upsert_plans_for_look(look_id,user_id,source_plans)
  existing_plans = query_scheduled_plans_for_look(look_id,"all")
  upsert_plans_for_obj(user_id,source_plans,existing_plans) { |p| p[:look_id] = look_id }
end

#upsert_plans_for_obj(user_id, source_plans, existing_plans, &block) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/gzr/modules/plan.rb', line 160

def upsert_plans_for_obj(user_id,source_plans,existing_plans, &block)
  plans = nil
  source_plans.collect do |source_plan|
    plans = upsert_plan_for_obj(user_id, source_plan, existing_plans, &block)
  end
  return nil unless plans.length > 0
  plans.first
end