-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenricher.py
164 lines (147 loc) · 5.95 KB
/
enricher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env python
import collections
import datetime
import time
import channel
import config
import user
import utils
class Enricher(object):
def __init__(self, fake=False):
self.fake = fake
self.user = user.User(fake=self.fake)
self.channel = channel.Channel()
def get_channels(self, list_of_cid, report_start_date):
"""
Given a list of channel IDs and an yyyy-mm-dd str date,
returns a dictionary indexed by cid
where the value is another dictionary with
'name': user-friendly channel name
'new': True/False based on whether it was created during this period
'members': Count of members
"""
(y, m, d) = [int(x) for x in report_start_date.split("-")]
dt = datetime.datetime(y, m, d, 0, 0, 0)
report_start_ts = dt.timestamp()
entries = self.channel.batch_get_channel(list_of_cid)
ret = {}
for cid in entries:
entry = entries[cid]
name = entry['friendly_name']
created = int(entry['created'])
new = created > report_start_ts
members = entry.get("members", 0)
ret[cid] = {'name': name, 'new': new, 'members': members}
return ret
@staticmethod
def popular_messages(messages, cinfo, uinfo):
"""
given a list of lists where each list is
[reaction count, ts, cid, uid, tts]
convert to dict with 'count', 'dt', 'channel', 'cid', 'uid', 'user', 'url'
"""
ret = []
for message in messages:
if not isinstance(message, list):
continue
(reactions, ts, cid, uid, tts) = message
ret.append({
'count': reactions,
'dt': time.strftime("%m/%d/%Y %H:%M", time.localtime(int(float(ts)))),
'channel': cinfo[cid]['name'],
'user': uinfo[uid]['label'],
'uid': uid,
'cid': cid,
'url': utils.make_url(cid, ts, tts)
})
return ret
def enrich(self, report):
# Get the canonical list of USER ids we might refer to.
# That is all users who posted in all channels
channels = report['channel_user'].keys()
users = {} # We need a list, but this makes deduping easier
channel_list = []
# Collect list of userIDs we might refer to
for channel in channels:
channel_list.append(channel)
for user in report['channel_user'][channel]:
users[user] = 1
for uid in report['enriched_user']:
enriched = report['enriched_user'][uid]
for elem in ['reacted_to', 'reactions_from']:
reactors = enriched.get(elem, {}).keys()
for user in reactors:
users[user] = 1
users = list(users.keys())
channel_info = self.get_channels(channel_list, report['start_date'])
user_info = self.user.get_users(users)
report['user_info'] = user_info
report['channel_info'] = channel_info
reactions = report['reaction']
reactji = list(reactions.keys())
report['top_ten_reactions'] = reactji[0:10]
report['reacted_messages'] = Enricher.popular_messages(
report['reaction_count'], channel_info, user_info)
report['reacted_messages'] = report['reacted_messages'][0:10]
report['replied_messages'] = Enricher.popular_messages(
report['reply_count'], channel_info, user_info)
report['replied_messages'] = report['replied_messages'][0:10]
for cid in report.get('enriched_channel', {}):
for k in ['most_replied', 'most_reacted']:
report['enriched_channel'][cid][k] = Enricher.popular_messages(
report['enriched_channel'][cid][k], channel_info, user_info)
def user_enrich(self, report, uid):
self.enrich(report)
if uid not in report['user_info']:
report['user_info'][uid] = self.user.get_pretty(uid)
user = report['user_info'][uid]['label']
report['uid'] = uid
report['user'] = user
channel_list = []
for cid in report['channel_user']:
# print("Examining cid {}".format(cid))
channel = report['channel_user'][cid]
if uid not in channel:
continue
users = list(channel.keys())
users.sort(key=lambda x: channel[x][1])
users.reverse()
rank = 1
for i in users:
if i == uid:
break
rank += 1
cname = report['channel_info'][cid]['name']
channel_messages = report['channels'][cid][0]
channel_words = report['channels'][cid][1]
messages = channel[uid][0]
words = channel[uid][1]
if not words:
continue
percent_words = words * 100.0 / channel_words
percent_messages = messages * 100.0 / channel_messages
c = {
'slack_cid': cid,
'name': cname,
'rank': rank,
'words': words,
'messages': messages,
'percent': percent_words
}
channel_list.append(c)
channel_list.sort(key=lambda x: x['words'])
channel_list.reverse()
d = collections.OrderedDict()
for channel in channel_list:
name = channel['name']
d[name] = channel
report['enriched_channels'] = d
ci = report['channel_info']
ui = report['user_info']
report['reenriched_user'] = {}
for user in report['enriched_user']:
report['reenriched_user'][user] = {}
for t in ['reactions', 'replies']:
messages = Enricher.popular_messages(
report['enriched_user'][user][t], ci, ui)
report['reenriched_user'][user][t] = messages