Module: Gzr::Plan

Instance Method Summary collapse

Instance Method Details

#create_scheduled_plan(plan) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/gzr/modules/plan.rb', line 102

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

#delete_scheduled_plan(plan_id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gzr/modules/plan.rb', line 88

def delete_scheduled_plan(plan_id)
  begin
    @sdk.delete_scheduled_plan(plan_id)
  rescue LookerSDK::NotFound
    say_error "delete_scheduled_plan(#{plan_id}) not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Unable to delete scheduled_plan(#{plan_id})"
    say_error e
    raise
  end
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
# 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
  begin
    @sdk.all_scheduled_plans(req).collect { |p| p.to_attrs }
  rescue LookerSDK::NotFound => e
    return []
  rescue LookerSDK::Error => e
    say_error "Unable to get all_scheduled_plans(#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

#query_scheduled_plan(plan_id, fields = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gzr/modules/plan.rb', line 74

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

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



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

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
  begin
    @sdk.scheduled_plans_for_dashboard(dashboard_id,req).collect { |p| p.to_attrs }
  rescue LookerSDK::NotFound
    return []
  rescue LookerSDK::Error => e
    say_error "Unable to get scheduled_plans_for_dashboard(#{dashboard_id},#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

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



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

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
  begin
    @sdk.scheduled_plans_for_look(look_id,req).collect { |p| p.to_attrs }
  rescue LookerSDK::NotFound => e
    return []
  rescue LookerSDK::Error => e
    say_error "Unable to get scheduled_plans_for_look(#{look_id},#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

#run_scheduled_plan(plan) ⇒ Object



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

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

#update_scheduled_plan(plan_id, plan) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gzr/modules/plan.rb', line 112

def update_scheduled_plan(plan_id,plan)
  begin
    @sdk.update_scheduled_plan(plan_id,plan).to_attrs
  rescue LookerSDK::NotFound
    say_error "update_scheduled_plan(#{plan_id},#{JSON.pretty_generate(plan)}) not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Error updating scheduled_plan(#{plan_id},#{JSON.pretty_generate(plan)})"
    say_error e
    raise
  end
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