portail/scripts/generate_chat_stat_daily.py

124 lines
No EOL
3.4 KiB
Python

# 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
from common.charthelper import createElement, getColor, setTopicInfo, getTopic, Topics
begin = datetime.now()
end = datetime.now()
def main():
rocket = Connection()
nbElements = 30
global begin, end
labels = [None] * nbElements
messagesByChannel = []
usersByChannel = []
messagesDataTopic = {
Topics.GLOBAL: [0] * nbElements,
Topics.PROJECT: [0] * nbElements,
Topics.DEMOCRACY: [0] * nbElements,
Topics.ECOLOGY: [0] * nbElements,
Topics.TECHNOLOGY: [0] * nbElements,
}
usersGlobal = []
now = datetime.now()
date = datetime(now.year, now.month, now.day, 0,0,0)
info = {
"updated": "{:02}/{:02}/{:04}".format(now.day, now.month, now.year),
"labels": labels,
"messagesByChannel": messagesByChannel,
"usersByChannel": usersByChannel,
"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])
],
"usersGlobal": usersGlobal
}
uniqueUserGlobal = [None] * nbElements
for channel in getAllChannels(rocket):
dataMess = []
dataUsers = []
print( channel['name'] )
begin = date - timedelta(nbElements)
end = begin + timedelta(nbElements)
tsunamy = getTopic(channel)
messages = getAllMessages(rocket, channel['_id'], begindate= begin, enddate= end, count= channel["msgs"])
end = begin + timedelta(1)
for id in range(0, nbElements):
if uniqueUserGlobal[id] == None:
uniqueUserGlobal[id] = []
labels[id] = begin.strftime("%x")
print("\t{0}".format(labels[id]))
resultMess = list(filter(filter_message, messages))
length = len(resultMess)
dataMess.append(length)
if length > 0:
setTopicInfo(tsunamy, messagesDataTopic, id, length)
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")
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
if __name__ == "__main__":
main()