Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pg:incidents #81

Merged
merged 1 commit into from
May 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/heroku/client/heroku_postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def maintenance_info
def maintenance_run
http_post "#{resource_name}/maintenance"
end

def incidents
http_get "#{resource_name}/incidents"
end
end
54 changes: 54 additions & 0 deletions lib/heroku/command/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,32 @@ def calls
puts exec_sql(sql)
end

# pg:incidents [DATABASE]
#
# show recents incidents.
#
def incidents
db = shift_argument
attachment = generate_resolver.resolve(db, "DATABASE_URL")
validate_arguments!

incidents = hpg_client(attachment).incidents
if incidents.empty?
output_with_bang("No incidents found for this database.")
elsif incidents.is_a?(Hash) && incidents.has_key?(:message)
output_with_bang(incidents[:message])
else
styled_header(attachment.display_name)
incidents.each do |incident|
display "\n==== #{time_format(incident[:created_at])}"
incident[:duration] = format_duration(incident[:created_at], incident[:updated_at])
incident[:duration] += " (ongoing)" if incident[:ongoing]
incident.reject! { |k,_| [:id, :updated_at, :created_at, :ongoing].include?(k) }
styled_hash(Hash[incident.map {|k, v| [humanize(k), v] }])
end
end
end

private
def pg_stat_statement?
return false if version.to_f < 9.1
Expand Down Expand Up @@ -756,6 +782,34 @@ def track_extra(command)
def in_maintenance?(app)
api.get_app_maintenance(app).body['maintenance']
end

def time_format(time)
Time.parse(time).getutc.strftime("%Y-%m-%d %H:%M %Z")
end

def format_duration(start, stop)
start = Time.parse(start)
stop = Time.parse(stop)

seconds = (stop - start).to_i
minutes = seconds / 60
hours = minutes / 60
days = hours / 24

if days > 0
"#{days} days #{hours % 24} hours"
elsif hours > 0
"#{hours} hours #{minutes % 60} minutes"
elsif minutes > 0
"#{minutes} minutes #{seconds % 60} seconds"
elsif seconds >= 0
"#{seconds} seconds"
end
end

def humanize(key)
key.to_s.gsub(/_/, ' ').split(" ").map(&:capitalize).join(" ")
end
end


Expand Down