2019-09-01 10:03:04 +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
|
|
|
|
from monthdelta import monthdelta
|
2019-09-01 19:18:30 +00:00
|
|
|
from common.rocketchathelper import getAllChannels, getAllMessages, Connection
|
2019-09-01 11:12:30 +00:00
|
|
|
from common.savehelper import save
|
2019-09-04 22:52:47 +00:00
|
|
|
from common.charthelper import createElement, getColor, setTopicInfo, Topics, getTopic
|
2019-09-01 10:03:04 +00:00
|
|
|
|
2019-09-04 15:48:57 +00:00
|
|
|
begin = datetime.now()
|
|
|
|
end = datetime.now()
|
|
|
|
|
2019-09-01 10:03:04 +00:00
|
|
|
def main():
|
2019-09-04 15:48:57 +00:00
|
|
|
|
|
|
|
global begin, end
|
|
|
|
|
2019-09-01 10:50:21 +00:00
|
|
|
rocket = Connection()
|
2019-09-01 10:03:04 +00:00
|
|
|
|
|
|
|
labels = [None] * 12
|
|
|
|
messagesByChannel = []
|
|
|
|
usersByChannel = []
|
2019-09-04 22:52:47 +00:00
|
|
|
messagesDataTopic = {
|
|
|
|
Topics.GLOBAL: [0] * 12,
|
|
|
|
Topics.PROJECT: [0] * 12,
|
|
|
|
Topics.DEMOCRACY: [0] * 12,
|
|
|
|
Topics.ECOLOGY: [0] * 12,
|
|
|
|
Topics.TECHNOLOGY: [0] * 12,
|
2019-09-01 10:03:04 +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-01 10:03:04 +00:00
|
|
|
"labels": labels,
|
|
|
|
"messagesByChannel": messagesByChannel,
|
|
|
|
"usersByChannel": usersByChannel,
|
2019-09-04 22:52:47 +00:00
|
|
|
"messagesByTopic": [
|
|
|
|
createElement("global", getColor(), messagesDataTopic[Topics.GLOBAL]),
|
|
|
|
createElement("project", getColor(), messagesDataTopic[Topics.PROJECT]),
|
|
|
|
createElement("democratie", getColor(), messagesDataTopic[Topics.DEMOCRACY]),
|
|
|
|
createElement("ecologie", getColor(), messagesDataTopic[Topics.ECOLOGY]),
|
|
|
|
createElement("technologie", getColor(), messagesDataTopic[Topics.TECHNOLOGY])
|
2019-09-01 10:03:04 +00:00
|
|
|
],
|
|
|
|
"usersGlobal": usersGlobal
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueUserGlobal = [None] * 12
|
|
|
|
|
|
|
|
for channel in getAllChannels(rocket):
|
|
|
|
dataMess = []
|
|
|
|
dataUsers = []
|
|
|
|
print( channel['name'] )
|
|
|
|
begin = date - monthdelta(12)
|
|
|
|
end = begin + monthdelta(1)
|
|
|
|
|
2019-09-04 15:48:57 +00:00
|
|
|
messages = getAllMessages(rocket, channel['_id'], begindate= begin, enddate=date, count= channel['msgs'])
|
|
|
|
|
2019-09-04 22:52:47 +00:00
|
|
|
tsunamy = getTopic(channel)
|
2019-09-01 10:03:04 +00:00
|
|
|
|
|
|
|
for id in range(0, 12):
|
|
|
|
if uniqueUserGlobal[id] == None:
|
|
|
|
uniqueUserGlobal[id] = []
|
|
|
|
labels[id] = begin.strftime("%b %Y")
|
|
|
|
print(f"\t{labels[id]}")
|
2019-09-04 15:48:57 +00:00
|
|
|
|
|
|
|
resultMess = list(filter(lambda mess: begin < datetime.strptime(mess["ts"], "%Y-%m-%dT%H:%M:%S.%fZ") < end, messages))
|
2019-09-01 10:03:04 +00:00
|
|
|
length = len(resultMess)
|
|
|
|
dataMess.append(length)
|
|
|
|
|
|
|
|
if length > 0:
|
2019-09-04 22:52:47 +00:00
|
|
|
setTopicInfo(tsunamy, messagesDataTopic, id, length)
|
2019-09-01 10:03:04 +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 + monthdelta(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, 12):
|
|
|
|
uniqueUserGlobal[id] = len(set(uniqueUserGlobal[id]))
|
|
|
|
|
|
|
|
userGlobal = createElement( 'global', 'red', uniqueUserGlobal)
|
|
|
|
usersGlobal.append(userGlobal)
|
|
|
|
|
2019-09-01 15:09:12 +00:00
|
|
|
save(info, "chat_stat_monthly")
|
2019-09-01 10:03:04 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|