2019-09-02 19:36:59 +00:00
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
# toutes les chaines sont en unicode (même les docstrings)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
# from pprint import pprint
|
|
|
|
from rocketchat_API.rocketchat import RocketChat
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from common.rocketchathelper import getAllChannels, getAllMessages, Connection
|
|
|
|
from common.savehelper import save
|
2019-09-28 19:37:09 +00:00
|
|
|
from common.charthelper import createElement, getColor, setTopicsInfo, getTopics
|
2019-09-02 19:36:59 +00:00
|
|
|
|
2019-09-03 19:21:07 +00:00
|
|
|
begin = datetime.now()
|
|
|
|
end = datetime.now()
|
|
|
|
|
2019-09-02 19:36:59 +00:00
|
|
|
def main():
|
|
|
|
rocket = Connection()
|
|
|
|
nbElements = 30
|
|
|
|
|
2019-09-03 19:21:07 +00:00
|
|
|
global begin, end
|
|
|
|
|
2019-09-02 19:36:59 +00:00
|
|
|
labels = [None] * nbElements
|
|
|
|
messagesByChannel = []
|
|
|
|
usersByChannel = []
|
2019-09-28 19:37:09 +00:00
|
|
|
messagesByTopic = {}
|
2019-09-02 19:36:59 +00:00
|
|
|
usersGlobal = []
|
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
date = datetime(now.year, now.month, now.day, 0,0,0)
|
|
|
|
|
|
|
|
info = {
|
2019-09-04 22:52:47 +00:00
|
|
|
"updated": "{:02}/{:02}/{:04}".format(now.day, now.month, now.year),
|
2019-09-02 19:36:59 +00:00
|
|
|
"labels": labels,
|
|
|
|
"messagesByChannel": messagesByChannel,
|
|
|
|
"usersByChannel": usersByChannel,
|
2019-09-28 19:37:09 +00:00
|
|
|
"messagesByTopic": messagesByTopic,
|
2019-09-02 19:36:59 +00:00
|
|
|
"usersGlobal": usersGlobal
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueUserGlobal = [None] * nbElements
|
|
|
|
|
|
|
|
for channel in getAllChannels(rocket):
|
|
|
|
dataMess = []
|
|
|
|
dataUsers = []
|
|
|
|
print( channel['name'] )
|
|
|
|
begin = date - timedelta(nbElements)
|
2019-09-03 19:21:07 +00:00
|
|
|
end = begin + timedelta(nbElements)
|
2019-09-02 19:36:59 +00:00
|
|
|
|
2019-09-28 19:37:09 +00:00
|
|
|
topics = getTopics(channel)
|
|
|
|
print(topics)
|
2019-09-04 15:39:46 +00:00
|
|
|
messages = getAllMessages(rocket, channel['_id'], begindate= begin, enddate= end, count= channel["msgs"])
|
2019-09-03 19:21:07 +00:00
|
|
|
end = begin + timedelta(1)
|
|
|
|
|
2019-09-02 19:36:59 +00:00
|
|
|
for id in range(0, nbElements):
|
|
|
|
if uniqueUserGlobal[id] == None:
|
|
|
|
uniqueUserGlobal[id] = []
|
|
|
|
labels[id] = begin.strftime("%x")
|
2019-09-09 04:02:55 +00:00
|
|
|
print("\t{0}".format(labels[id]))
|
2019-09-03 19:21:07 +00:00
|
|
|
|
|
|
|
resultMess = list(filter(filter_message, messages))
|
2019-09-02 19:36:59 +00:00
|
|
|
length = len(resultMess)
|
|
|
|
dataMess.append(length)
|
|
|
|
|
|
|
|
if length > 0:
|
2019-09-28 19:37:09 +00:00
|
|
|
setTopicsInfo(topics, messagesByTopic, id, length)
|
2019-09-02 19:36:59 +00:00
|
|
|
|
|
|
|
users = []
|
|
|
|
for mess in resultMess:
|
|
|
|
users.append(mess['u']['_id'])
|
|
|
|
uniqueUserGlobal[id].append(mess['u']['_id'])
|
|
|
|
usersDistinct = set(users)
|
|
|
|
dataUsers.append(len(usersDistinct))
|
|
|
|
else:
|
|
|
|
dataUsers.append(0)
|
|
|
|
|
|
|
|
begin = end
|
|
|
|
end = begin + timedelta(1)
|
|
|
|
|
|
|
|
color = getColor()
|
|
|
|
messageByChannel = createElement(channel['name'], color,dataMess)
|
|
|
|
userByChannel = createElement( channel['name'], color,dataUsers)
|
|
|
|
|
|
|
|
messagesByChannel.append(messageByChannel)
|
|
|
|
usersByChannel.append(userByChannel)
|
|
|
|
|
|
|
|
for id in range(0, nbElements):
|
|
|
|
uniqueUserGlobal[id] = len(set(uniqueUserGlobal[id]))
|
|
|
|
|
|
|
|
userGlobal = createElement( 'global', 'red', uniqueUserGlobal)
|
|
|
|
usersGlobal.append(userGlobal)
|
|
|
|
|
|
|
|
save(info, "chat_stat_daily")
|
|
|
|
|
2019-09-03 19:21:07 +00:00
|
|
|
def filter_message(mess):
|
|
|
|
if 't' in mess:
|
|
|
|
return False
|
|
|
|
|
|
|
|
date = datetime.strptime(mess["ts"], "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
|
|
if date < begin:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if end < date:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-09-02 19:36:59 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|