2019-06-08 20:08:21 +00:00
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
# toutes les chaines sont en unicode (même les docstrings)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
# from pprint import pprint
|
2019-06-08 20:08:21 +00:00
|
|
|
from rocketchat_API.rocketchat import RocketChat
|
|
|
|
import json
|
|
|
|
import dev_config as cfg
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
from datetime import datetime
|
|
|
|
from monthdelta import monthdelta
|
2019-07-04 19:15:35 +00:00
|
|
|
from common.channelhelper import getTsunamy, Tsunami, getAllChannels
|
2019-06-08 20:08:21 +00:00
|
|
|
|
|
|
|
def getColor():
|
|
|
|
r = random.randrange(255)
|
|
|
|
g = random.randrange(255)
|
|
|
|
b = random.randrange(255)
|
|
|
|
return 'rgb({:0},{:0},{:0})'.format(r,g,b)
|
|
|
|
|
2019-07-03 20:10:09 +00:00
|
|
|
def save(info):
|
|
|
|
# Récupération du répertoire racine du repo
|
|
|
|
rootFolder = os.path.join(os.path.dirname(__file__), '..')
|
|
|
|
# Répertoire pour stocker le fichier de sortie
|
|
|
|
dataFolder = os.path.join(rootFolder, 'public', 'data')
|
|
|
|
|
|
|
|
statsFilePath = os.path.abspath(
|
|
|
|
os.path.join(dataFolder, 'channelsstat.json'))
|
|
|
|
with open(statsFilePath, "w") as file_write:
|
|
|
|
json.dump(info, file_write)
|
|
|
|
|
|
|
|
def createElement(label, color, data) :
|
|
|
|
return {
|
|
|
|
"label": label,
|
|
|
|
"backgroundColor": color,
|
|
|
|
"data": data
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
def setTsunamyInfo(tsunamy, messagesDataTsunamy, id, length):
|
|
|
|
if tsunamy & Tsunami.GLOBAL:
|
|
|
|
messagesDataTsunamy[Tsunami.GLOBAL][id] += length
|
|
|
|
if tsunamy & Tsunami.PROJECT:
|
|
|
|
messagesDataTsunamy[Tsunami.PROJECT][id] += length
|
|
|
|
if tsunamy & Tsunami.DEMOCRACY:
|
|
|
|
messagesDataTsunamy[Tsunami.DEMOCRACY][id] += length
|
|
|
|
if tsunamy & Tsunami.ECOLOGY:
|
|
|
|
messagesDataTsunamy[Tsunami.ECOLOGY][id] += length
|
|
|
|
if tsunamy & Tsunami.TECHNOLOGY:
|
|
|
|
messagesDataTsunamy[Tsunami.TECHNOLOGY][id] += length
|
|
|
|
|
2019-07-03 20:10:09 +00:00
|
|
|
def main():
|
|
|
|
rocket = RocketChat(cfg.rocket['user'], cfg.rocket['password'],
|
2019-07-04 19:15:35 +00:00
|
|
|
server_url=cfg.rocket['server'])
|
|
|
|
|
2019-07-03 20:10:09 +00:00
|
|
|
labels = [None] * 12
|
|
|
|
messagesByChannel = []
|
|
|
|
usersByChannel = []
|
|
|
|
messagesDataTsunamy = {
|
2019-07-04 19:15:35 +00:00
|
|
|
Tsunami.GLOBAL: [0] * 12,
|
|
|
|
Tsunami.PROJECT: [0] * 12,
|
|
|
|
Tsunami.DEMOCRACY: [0] * 12,
|
|
|
|
Tsunami.ECOLOGY: [0] * 12,
|
|
|
|
Tsunami.TECHNOLOGY: [0] * 12,
|
2019-07-03 20:10:09 +00:00
|
|
|
}
|
|
|
|
usersGlobal = []
|
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
date = datetime(now.year, now.month, now.day, 0,0,0)
|
|
|
|
|
|
|
|
info = {
|
|
|
|
"updated": "updated {:02}/{:02}/{:04}".format(now.day, now.month, now.year),
|
|
|
|
"labels": labels,
|
|
|
|
"messagesByChannel": messagesByChannel,
|
|
|
|
"usersByChannel": usersByChannel,
|
|
|
|
"messagesByTsunamy": [
|
2019-07-04 19:15:35 +00:00
|
|
|
createElement("global", getColor(), messagesDataTsunamy[Tsunami.GLOBAL]),
|
|
|
|
createElement("project", getColor(), messagesDataTsunamy[Tsunami.PROJECT]),
|
|
|
|
createElement("democratie", getColor(), messagesDataTsunamy[Tsunami.DEMOCRACY]),
|
|
|
|
createElement("ecologie", getColor(), messagesDataTsunamy[Tsunami.ECOLOGY]),
|
|
|
|
createElement("technologie", getColor(), messagesDataTsunamy[Tsunami.TECHNOLOGY])
|
2019-07-03 20:10:09 +00:00
|
|
|
],
|
2019-06-27 19:19:19 +00:00
|
|
|
"usersGlobal": usersGlobal
|
2019-07-03 20:10:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
uniqueUserGlobal = [None] * 12
|
|
|
|
|
|
|
|
for channel in getAllChannels(rocket):
|
|
|
|
dataMess = []
|
|
|
|
dataUsers = []
|
|
|
|
print( channel['name'] )
|
|
|
|
begin = date - monthdelta(12)
|
|
|
|
end = begin + monthdelta(1)
|
|
|
|
|
|
|
|
tsunamy = getTsunamy(channel)
|
|
|
|
|
|
|
|
for id in range(0, 12):
|
|
|
|
if uniqueUserGlobal[id] == None:
|
|
|
|
uniqueUserGlobal[id] = []
|
|
|
|
labels[id] = begin.strftime("%b %Y")
|
|
|
|
print(f"\t{labels[id]}")
|
|
|
|
begindate = begin.isoformat()
|
|
|
|
enddate = end.isoformat()
|
|
|
|
|
|
|
|
resultMess = rocket.channels_history(channel['_id'], oldest= begindate, latest=enddate, count= 10000).json()
|
|
|
|
resultMess = list(filter(lambda mess: 't' not in mess, resultMess['messages']))
|
|
|
|
length = len(resultMess)
|
|
|
|
dataMess.append(length)
|
|
|
|
|
|
|
|
if length > 0:
|
|
|
|
setTsunamyInfo(tsunamy, messagesDataTsunamy, 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
|
2019-07-03 20:10:09 +00:00
|
|
|
end = begin + monthdelta(1)
|
2019-07-04 19:15:35 +00:00
|
|
|
|
|
|
|
color = getColor()
|
|
|
|
messageByChannel = createElement(channel['name'], color,dataMess)
|
|
|
|
userByChannel = createElement( channel['name'], color,dataUsers)
|
2019-06-27 19:19:19 +00:00
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
messagesByChannel.append(messageByChannel)
|
|
|
|
usersByChannel.append(userByChannel)
|
2019-07-03 20:10:09 +00:00
|
|
|
|
|
|
|
for id in range(0, 12):
|
2019-07-04 19:15:35 +00:00
|
|
|
uniqueUserGlobal[id] = len(set(uniqueUserGlobal[id]))
|
2019-07-03 20:10:09 +00:00
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
userGlobal = createElement( 'global', 'red', uniqueUserGlobal)
|
2019-07-03 20:10:09 +00:00
|
|
|
usersGlobal.append(userGlobal)
|
|
|
|
|
|
|
|
save(info)
|
2019-06-08 20:08:21 +00:00
|
|
|
|
2019-07-04 19:15:35 +00:00
|
|
|
|
|
|
|
|
2019-07-03 20:10:09 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|