-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3176fe0
commit f367d8e
Showing
5 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2024 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
# frozen_string_literal: true | ||
|
||
require_relative '../../command' | ||
require_relative '../../modules/alert' | ||
require_relative '../../modules/user' | ||
require_relative '../../modules/cron' | ||
|
||
module Gzr | ||
module Commands | ||
class Alert | ||
class Randomize < Gzr::Command | ||
include Gzr::Alert | ||
include Gzr::User | ||
include Gzr::Cron | ||
def initialize(options) | ||
super() | ||
@options = options | ||
end | ||
|
||
def execute(input: $stdin, output: $stdout) | ||
say_warning(@options) if @options[:debug] | ||
|
||
window = @options[:window] | ||
if window < 1 or window > 60 | ||
say_error("window must be between 1 and 60") | ||
raise Gzr::CLI::Error.new() | ||
end | ||
|
||
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 | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2024 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
RSpec.describe "`gzr alert randomize` command", type: :cli do | ||
it "executes `gzr alert help randomize` command successfully" do | ||
output = `gzr alert help randomize` | ||
expected_output = <<-OUT | ||
Usage: | ||
gzr alert randomize | ||
Options: | ||
-h, [--help], [--no-help] # Display usage information | ||
[--window=N] # Length of window | ||
# Default: 60 | ||
[--all], [--no-all] # Randomize all alerts regardless of owner | ||
Randomize the scheduled alerts on a server | ||
OUT | ||
|
||
expect(output).to eq(expected_output) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2024 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
RSpec.describe "`gzr alert` command", type: :cli do | ||
it "executes `gzr help alert` command successfully" do | ||
output = `gzr help alert` | ||
expected_output = <<-OUT | ||
Commands: | ||
gzr alert cat ALERT_ID # Output json information about an alert to screen or file | ||
gzr alert chown ALERT_ID OWNER_ID # Change the owner of the alert given by ALERT_ID to OWNER_ID | ||
gzr alert disable ALERT_ID REASON # Disable the alert given by ALERT_ID | ||
gzr alert enable ALERT_ID # Enable the alert given by ALERT_ID | ||
gzr alert follow ALERT_ID # Start following the alert given by ALERT_ID | ||
gzr alert help [COMMAND] # Describe subcommands or one specific subcommand | ||
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 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 | ||
gzr alert unfollow ALERT_ID # Stop following the alert given by ALERT_ID | ||
OUT | ||
|
||
expect(output).to eq(expected_output) | ||
end | ||
end |