Skip to content

Commit

Permalink
feat: randomize a specific alert by alert_id
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker committed Jun 27, 2024
1 parent 99d30f1 commit 0e9cf40
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/gzr/commands/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ def ls(*)
end
end

desc 'randomize', 'Randomize the scheduled alerts on a server'
desc 'randomize [ALERT_ID]', 'Randomize the scheduled alerts on a server'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :window, type: :numeric, default: 60,
desc: 'Length of window'
method_option :all, type: :boolean,
desc: 'Randomize all alerts regardless of owner'
def randomize(*)
def randomize(alert_id=nil)
if options[:help]
invoke :help, ['randomize']
else
require_relative 'alert/randomize'
Gzr::Commands::Alert::Randomize.new(options).execute
Gzr::Commands::Alert::Randomize.new(alert_id,options).execute
end
end

Expand Down
54 changes: 34 additions & 20 deletions lib/gzr/commands/alert/randomize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class Randomize < Gzr::Command
include Gzr::Alert
include Gzr::User
include Gzr::Cron
def initialize(options)
def initialize(alert_id,options)
super()
@alert_id = alert_id
@options = options
end

Expand All @@ -50,32 +51,45 @@ def execute(input: $stdin, output: $stdout)
with_session do
@me ||= query_me("id")

req = {}
req[:disabled] = false
req[:all_owners] = @options[:all] unless @options[:all].nil?
alerts = search_alerts(**req)
begin
say_ok "No alerts found"
return nil
end unless alerts && alerts.length > 0

alerts.each do |alert|
crontab = alert[:cron]
if crontab == ""
say_warning("skipping alert #{alert[:id]} with no cron")
next
if @alert_id
alert = get_alert(@alert_id)
if alert
randomize_alert(alert,window)
else
say_warning("Alert #{@alert_id} not found")
end
crontab = randomize_cron(crontab, window)
else
req = {}
req[:disabled] = false
req[:all_owners] = @options[:all] unless @options[:all].nil?
alerts = search_alerts(**req)
begin
alert[:cron] = crontab
update_alert(alert[:id], alert)
rescue LookerSDK::UnprocessableEntity => e
say_warning("Skipping invalid entry")
say_ok "No alerts found"
return nil
end unless alerts && alerts.length > 0

alerts.each do |alert|
randomize_alert(alert,window)
end
end

end
end

def randomize_alert(alert,window=60)
crontab = alert[:cron]
if crontab == ""
say_warning("skipping alert #{alert[:id]} with no cron")
return
end
crontab = randomize_cron(crontab, window)
begin
alert[:cron] = crontab
update_alert(alert[:id], alert)
rescue LookerSDK::UnprocessableEntity => e
say_warning("Skipping invalid entry")
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/modules/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def randomize_cron(crontab, window=60)
hour = 0
end
cronfields[0] = minute
cronfields[1] = hour
cronfields[1] = hour if /^[[:digit:]]+$/.match? cronfields[1]
return cronfields.join(' ')
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/alert/randomize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
output = `gzr alert help randomize`
expected_output = <<-OUT
Usage:
gzr alert randomize
gzr alert randomize [ALERT_ID]
Options:
-h, [--help], [--no-help] # Display usage information
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
gzr alert import FILE [DASHBOARD_ELEMENT_ID] # Import an alert from a file
gzr alert ls # list alerts
gzr alert notifications # Get notifications
gzr alert randomize # Randomize the scheduled alerts on a server
gzr alert randomize [ALERT_ID] # Randomize the scheduled alerts on a server
gzr alert read NOTIFICATION_ID # Read notification id
gzr alert rm ALERT_ID # Delete the alert given by ALERT_ID
gzr alert threshold ALERT_ID THRESHOLD # Change the threshold of the alert given by ALERT_ID
Expand Down

0 comments on commit 0e9cf40

Please sign in to comment.