Skip to content
This repository was archived by the owner on Apr 8, 2021. It is now read-only.

support for identifying users that acknowledge and resolve from Slack #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ source "https://rubygems.org"
gem "excon"
gem "pg"
gem "sequel"
gem "slack-api"
21 changes: 20 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
GEM
remote: https://rubygems.org/
specs:
eventmachine (1.2.2)
excon (0.40.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.10.1)
faraday (>= 0.7.4, < 1.0)
faye-websocket (0.10.6)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.5.1)
multi_json (1.12.1)
multipart-post (2.0.0)
pg (0.18.4)
sequel (4.37.0)
slack-api (1.4.0)
faraday (~> 0.11)
faraday_middleware (~> 0.10.0)
faye-websocket (~> 0.10.6)
multi_json (~> 1.0, >= 1.0.3)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby
Expand All @@ -12,6 +30,7 @@ DEPENDENCIES
excon
pg
sequel
slack-api

BUNDLED WITH
1.12.1
1.13.6
46 changes: 44 additions & 2 deletions pd2pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "time"
require "pg"
require "sequel"
require "slack"

# Ensure all data processing and storage is in UTC.
ENV["TZ"] = "UTC"
Expand Down Expand Up @@ -96,7 +97,11 @@ def convert_log_entry(le)
incident_id: le["incident"]["id"],
agent_type: le["agent"] && le["agent"]["type"],
agent_id: le["agent"] && le["agent"]["id"],
channel_id: le["channel"] && le["channel"]["channel"] && le["channel"]["channel"]["id"],
channel_name: le["channel"] && le["channel"]["channel"] && le["channel"]["channel"]["name"],
channel_type: le["channel"] && le["channel"]["type"],
channel_user_id: le["channel"] && le["channel"]["user"] && le["channel"]["user"]["id"],
channel_team_id: le["channel"] && le["channel"]["user"] && le["channel"]["team"]["id"],
user_id: le["user"] && le["user"]["id"],
notification_type: le["notification"] && le["notification"]["type"],
assigned_user_id: le["assigned_user"] && le["assigned_user"]["id"]
Expand All @@ -119,6 +124,36 @@ def convert_incident(i)
}
end

def refresh_slack_users()
slack_token = ENV["SLACK_TOKEN"]

if slack_token.to_s.empty?
log("refresh_slack_users.no_env SLACK_TOKEN environment variable is empty, skipping slack integration")
return
end

client = Slack::Client.new token: slack_token
records = client.users_list["members"].map{|m| {
id: m["id"],
name: m["name"],
real_name: m["real_name"]
}}

log("refresh_slack_users.update", total: records.length)
db.transaction do
table = db[:slack_users]
records.each do |record|
dataset = table.where(id: record[:id])
if dataset.empty?
table.insert(record)
else
dataset.update(record)
end
end
end
end


# Refresh database state for the given table by fetching all relevant
# values from the API. Yields each API value to a block that should
# convert the API value to a DB record for subsequent insertion /
Expand All @@ -134,7 +169,11 @@ def refresh_bulk(collection)
response = api.request(
:method => :get,
:path => "/api/v1/#{collection}",
:query => {"offset" => offset, "limit" => PAGINATION_LIMIT},
:query => {
"offset" => offset,
"limit" => PAGINATION_LIMIT,
"total" => true
},
:expects => [200]
)
data = JSON.parse(response.body)
Expand Down Expand Up @@ -193,7 +232,8 @@ def refresh_incremental(collection, query_params={})
"since" => since,
"until" => through,
"offset" => offset,
"limit" => PAGINATION_LIMIT
"limit" => PAGINATION_LIMIT,
"total" => true
}.merge(query_params),
:expects => [200]
)
Expand Down Expand Up @@ -225,6 +265,8 @@ def refresh_incremental(collection, query_params={})
def refresh
log("refresh.start")

refresh_slack_users()

refresh_bulk(:services) { |s| convert_service(s) }
refresh_bulk(:escalation_policies) { |ep| convert_escalation_policy(ep) }
refresh_bulk(:users) { |u| convert_user(u) }
Expand Down
10 changes: 10 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ create table log_entries (
incident_id varchar not null,
agent_type varchar,
agent_id varchar,
channel_id varchar,
channel_name varchar,
channel_type varchar,
channel_user_id varchar,
channel_team_id varchar,
user_id varchar,
notification_type varchar,
assigned_user_id varchar
Expand All @@ -42,5 +46,11 @@ create table users (
email varchar not null
);

create table slack_users (
id varchar primary key,
name varchar not null,
real_name varchar
);

-- Extension tablefunc enables crosstabs.
create extension tablefunc;