Skip to content

Commit

Permalink
feat: randomize a specific plan by plan_id
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker committed Jun 27, 2024
1 parent cbdf5eb commit 99d30f1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
8 changes: 4 additions & 4 deletions lib/gzr/commands/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def cat(plan_id)
desc 'ls', 'List the scheduled plans on a server'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :fields, type: :string, default: 'id,enabled,name,user(id,display_name),look_id,dashboard_id,lookml_dashboard_id',
method_option :fields, type: :string, default: 'id,enabled,name,user(id,display_name),look_id,dashboard_id,lookml_dashboard_id,crontab',
desc: 'Fields to display'
method_option :disabled, type: :boolean,
desc: 'Retrieve disable plans'
Expand All @@ -145,19 +145,19 @@ def ls(*)
end
end

desc 'randomize', 'Randomize the scheduled plans on a server'
desc 'randomize [PLAN_ID]', 'Randomize the scheduled plans 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 plans regardless of owner'
def randomize(*)
def randomize(plan_id = nil)
if options[:help]
invoke :help, ['randomize']
else
require_relative 'plan/randomize'
Gzr::Commands::Plan::Randomize.new(options).execute
Gzr::Commands::Plan::Randomize.new(plan_id, options).execute
end
end
end
Expand Down
38 changes: 26 additions & 12 deletions lib/gzr/commands/plan/randomize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class Randomize < Gzr::Command
include Gzr::Plan
include Gzr::User
include Gzr::Cron
def initialize(options)
def initialize(plan_id,options)
super()
@plan_id = plan_id
@options = options
end

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

plans = query_all_scheduled_plans( @options[:all]?'all':@me[:id] )
plans.each do |plan|
crontab = plan[:crontab]
if crontab == ""
say_warning("skipping plan #{plan[:id]} with no crontab")
next
if @plan_id
plan = query_scheduled_plan(@plan_id)
if plan
randomize_plan(plan,window)
else
say_warning("Plan #{@plan_id} not found")
end
crontab = randomize_cron(crontab, window)
begin
update_scheduled_plan(plan[:id], { crontab: crontab })
rescue LookerSDK::UnprocessableEntity => e
say_warning("Skipping invalid entry")
else
plans = query_all_scheduled_plans( @options[:all]?'all':@me[:id] )
plans.each do |plan|
randomize_plan(plan,window)
end
end
end
end

def randomize_plan(plan,window=60)
crontab = plan[:crontab]
if crontab == ""
say_warning("skipping plan #{plan[:id]} with no crontab")
return
end
crontab = randomize_cron(crontab, window)
begin
update_scheduled_plan(plan[:id], { crontab: crontab })
rescue LookerSDK::UnprocessableEntity => e
say_warning("Skipping invalid entry")
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/plan/ls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Options:
-h, [--help], [--no-help] # Display usage information
[--fields=FIELDS] # Fields to display
# Default: id,enabled,name,user(id,display_name),look_id,dashboard_id,lookml_dashboard_id
# Default: id,enabled,name,user(id,display_name),look_id,dashboard_id,lookml_dashboard_id,crontab
[--disabled], [--no-disabled] # Retrieve disable plans
[--plain], [--no-plain] # print without any extra formatting
[--csv], [--no-csv] # output in csv format per RFC4180
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/plan/randomize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
output = `gzr plan help randomize`
expected_output = <<-OUT
Usage:
gzr plan randomize
gzr plan randomize [PLAN_ID]
Options:
-h, [--help], [--no-help] # Display usage information
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
gzr plan help [COMMAND] # Describe subcommands or one specific subcommand
gzr plan import PLAN_FILE OBJ_TYPE OBJ_ID # Import a plan from a file
gzr plan ls # List the scheduled plans on a server
gzr plan randomize # Randomize the scheduled plans on a server
gzr plan randomize [PLAN_ID] # Randomize the scheduled plans on a server
gzr plan rm PLAN_ID # Delete a scheduled plan
gzr plan runit PLAN_ID # Execute a saved plan immediately
Expand Down

0 comments on commit 99d30f1

Please sign in to comment.