Skip to content
This repository was archived by the owner on May 12, 2023. It is now read-only.

Commit

Permalink
Schedules are back baby!
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmckenzie committed Jul 27, 2015
1 parent a751d92 commit 7d529e7
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pagerduty/clt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
require 'pagerduty/clt/incident_list'
require 'pagerduty/clt/escalation_policy'
require 'pagerduty/clt/escalation_policies'
require 'pagerduty/clt/schedule'
require 'pagerduty/clt/schedules'
require 'pagerduty/clt/cli'

module Pagerduty
Expand Down
13 changes: 13 additions & 0 deletions lib/pagerduty/clt/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ def execute
end
end

class SchedulesCommand < AbstractCommand
option [ '-q', '--query' ], 'QUERY', 'Query'

def execute
options = { query: query }

schedules = Schedules.new.where(options)
table = Formatters::Schedules::Table.new(schedules).render
puts table if table
end
end

class OncallCommand < AbstractCommand
option [ '-q', '--query' ], 'QUERY', 'Query'

Expand All @@ -91,6 +103,7 @@ def execute
class MainCommand < AbstractCommand
# subcommand %w(c console), 'Run a console', ConsoleCommand
subcommand %w(o oncall), 'Who is currently on call', OncallCommand
subcommand %w(s schedules), 'Schedules', SchedulesCommand
subcommand %w(l list), 'List incidents needing attention (triggered + acknowledged)', ListNeedingAttentionCommand
subcommand %w(a ack acknowledge), 'Acknowledge incidents', AcknowledgeCommand
subcommand %w(r resolve), 'Resolve incidents', ResolveCommand
Expand Down
1 change: 1 addition & 0 deletions lib/pagerduty/clt/formatters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'pagerduty/clt/formatters/terminal_table'
require 'pagerduty/clt/formatters/incidents/table'
require 'pagerduty/clt/formatters/on_call/table'
require 'pagerduty/clt/formatters/schedules/table'

module Pagerduty
module CLT
Expand Down
39 changes: 39 additions & 0 deletions lib/pagerduty/clt/formatters/schedules/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Pagerduty
module CLT
module Formatters
module Schedules
class Table

def initialize(schedules)
@schedules = schedules
end

def render
return nil if schedules.empty?
puts TerminalTable.new(headings: columns, rows: rows, max_width: 140).render
end

private

attr_reader :schedules

def columns
{
name: { label: 'Name', max_width: 35 },
people: { label: 'People', max_width: -1 }
}
end

def rows
schedules.map do |schedule|
{
name: schedule.name,
people: schedule.users.map(&:name).join(', ')
}
end
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/pagerduty/clt/path_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ def users_path(id)
'users/%s' % [ id ]
end

def schedules_path
'schedules'
end

def schedule_path(id)
'%s/%s' % [ schedules_path, id ]
end

def schedule_users_path(id)
'%s/%s/users' % [ schedules_path, id ]
end

def escalation_policies_path
'escalation_policies/on_call'
end
Expand Down
34 changes: 34 additions & 0 deletions lib/pagerduty/clt/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Pagerduty
module CLT
class Schedule

include PathHelper # FIXME
extend PathHelper # FIXME

def initialize(raw)
@raw = raw
end

def id
@id ||= raw.id
end

def name
@name ||= raw.name
end

def users
@users ||= begin
path = schedule_users_path(id)
options = { since: Time.now.strftime('%Y-%m-%d'), until: (Time.now + 7 * 86_400).strftime('%Y-%m-%d') }
$connection.get(path, options).users.map { |raw_user| User.new(raw_user) }
end
end

private

attr_reader :raw

end
end
end
38 changes: 38 additions & 0 deletions lib/pagerduty/clt/schedules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Pagerduty
module CLT
class Schedules

include Base
include PathHelper # FIXME

def all
where
end

def where(query: nil)
get(query)
end

private

def get(query)
schedules = []
options = {}
options[:query] = query if query

jobs = []
response = $connection.get(schedules_path)
response.schedules.each_with_index do |raw_schedule_summary, i|
jobs << Thread.new do
raw_schedule_detailed = $connection.get(schedule_path(raw_schedule_summary.id), options)
schedules[i] = Schedule.new(raw_schedule_detailed.schedule)
end
end

jobs.each(&:join)
schedules
end

end
end
end

0 comments on commit 7d529e7

Please sign in to comment.