diff --git a/Gemfile b/Gemfile index b71ddb6..938968d 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,4 @@ source "https://rubygems.org" gem "excon" gem "pg" gem "sequel" +gem "slack-api" diff --git a/Gemfile.lock b/Gemfile.lock index da258a2..9ca6f76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -12,6 +30,7 @@ DEPENDENCIES excon pg sequel + slack-api BUNDLED WITH - 1.12.1 + 1.13.6 diff --git a/pd2pg.rb b/pd2pg.rb index c78db83..85ee784 100644 --- a/pd2pg.rb +++ b/pd2pg.rb @@ -3,6 +3,7 @@ require "time" require "pg" require "sequel" +require "slack" # Ensure all data processing and storage is in UTC. ENV["TZ"] = "UTC" @@ -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"] @@ -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 / @@ -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) @@ -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] ) @@ -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) } diff --git a/schema.sql b/schema.sql index d0983a9..c5cc979 100644 --- a/schema.sql +++ b/schema.sql @@ -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 @@ -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;