2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2016-04-07 23:11:21 +02:00
|
|
|
from typing import Any, Dict, List
|
2013-12-26 19:20:35 +01:00
|
|
|
# This is hacky code to analyze data on our support stream. The main
|
|
|
|
# reusable bits are get_recent_messages and get_words.
|
|
|
|
|
|
|
|
import zulip
|
|
|
|
import re
|
|
|
|
import collections
|
|
|
|
|
2016-09-10 21:23:44 +02:00
|
|
|
def get_recent_messages(client, narrow_str, count=100):
|
|
|
|
# type: (zulip.Client, str, int) -> List[Dict[str, Any]]
|
|
|
|
narrow = [word.split(':') for word in narrow_str.split()]
|
2013-12-26 19:20:35 +01:00
|
|
|
req = {
|
|
|
|
'narrow': narrow,
|
|
|
|
'num_before': count,
|
|
|
|
'num_after': 0,
|
|
|
|
'anchor': 1000000000,
|
|
|
|
'apply_markdown': False
|
|
|
|
}
|
|
|
|
old_messages = client.do_api_query(req, zulip.API_VERSTRING + 'messages', method='GET')
|
|
|
|
if 'messages' not in old_messages:
|
|
|
|
return []
|
|
|
|
return old_messages['messages']
|
|
|
|
|
|
|
|
def get_words(content):
|
2016-09-10 21:23:44 +02:00
|
|
|
# type: (str) -> List[str]
|
2013-12-26 19:20:35 +01:00
|
|
|
regex = "[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+"
|
|
|
|
words = re.findall(regex, content, re.M)
|
|
|
|
words = [w.lower() for w in words]
|
|
|
|
# words = [w.rstrip('s') for w in words]
|
|
|
|
return words
|
|
|
|
|
|
|
|
def analyze_messages(msgs, word_count, email_count):
|
2016-09-10 21:23:44 +02:00
|
|
|
# type: (List[Dict[str, Any]], Dict[str, int], Dict[str, int]) -> None
|
2013-12-26 19:20:35 +01:00
|
|
|
for msg in msgs:
|
|
|
|
if False:
|
|
|
|
if ' ack' in msg['content']:
|
|
|
|
name = msg['sender_full_name'].split()[0]
|
2015-11-01 17:11:06 +01:00
|
|
|
print('ACK', name)
|
2013-12-26 19:20:35 +01:00
|
|
|
m = re.search('ticket (Z....).*email: (\S+).*~~~(.*)', msg['content'], re.M | re.S)
|
|
|
|
if m:
|
|
|
|
ticket, email, req = m.groups()
|
|
|
|
words = get_words(req)
|
|
|
|
for word in words:
|
|
|
|
word_count[word] += 1
|
|
|
|
email_count[email] += 1
|
|
|
|
if False:
|
2015-11-01 17:11:06 +01:00
|
|
|
print()
|
2013-12-26 19:20:35 +01:00
|
|
|
for k, v in msg.items():
|
2015-11-01 17:11:06 +01:00
|
|
|
print('%-20s: %s' % (k, v))
|
2013-12-26 19:20:35 +01:00
|
|
|
|
|
|
|
def generate_support_stats():
|
2016-09-10 21:23:44 +02:00
|
|
|
# type: () -> None
|
2013-12-26 19:20:35 +01:00
|
|
|
client = zulip.Client()
|
|
|
|
narrow = 'stream:support'
|
|
|
|
count = 2000
|
|
|
|
msgs = get_recent_messages(client, narrow, count)
|
2017-05-07 16:44:54 +02:00
|
|
|
msgs_by_topic = collections.defaultdict(list) # type: Dict[str, List[Dict[str, Any]]]
|
2013-12-26 19:20:35 +01:00
|
|
|
for msg in msgs:
|
|
|
|
topic = msg['subject']
|
|
|
|
msgs_by_topic[topic].append(msg)
|
|
|
|
|
2017-05-07 16:44:54 +02:00
|
|
|
word_count = collections.defaultdict(int) # type: Dict[str, int]
|
|
|
|
email_count = collections.defaultdict(int) # type: Dict[str, int]
|
2013-12-26 19:20:35 +01:00
|
|
|
|
|
|
|
if False:
|
|
|
|
for topic in msgs_by_topic:
|
|
|
|
msgs = msgs_by_topic[topic]
|
|
|
|
analyze_messages(msgs, word_count, email_count)
|
|
|
|
|
|
|
|
if True:
|
2016-01-25 01:27:18 +01:00
|
|
|
words = [w for w in word_count.keys() if word_count[w] >= 10 and len(w) >= 5]
|
2013-12-26 19:20:35 +01:00
|
|
|
words = sorted(words, key=lambda w: word_count[w], reverse=True)
|
|
|
|
for word in words:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(word, word_count[word])
|
2013-12-26 19:20:35 +01:00
|
|
|
|
|
|
|
if False:
|
2016-01-25 01:27:18 +01:00
|
|
|
emails = sorted(list(email_count.keys()),
|
|
|
|
key=lambda w: email_count[w], reverse=True)
|
2013-12-26 19:20:35 +01:00
|
|
|
for email in emails:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(email, email_count[email])
|
2013-12-26 19:20:35 +01:00
|
|
|
|
|
|
|
generate_support_stats()
|