crazy-toads.github.io/script/getstats.py

150 lines
4.1 KiB
Python
Raw Normal View History

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
from pprint import pprint
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
from common.channelhelper import getTsunamy
from common.channelhelper import Tsunami
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)
rocket = RocketChat(cfg.rocket['user'], cfg.rocket['password'],
server_url='https://coa.crapaud-fou.org')
index = 0
2019-06-08 20:32:24 +00:00
labels = [None] * 12
2019-06-08 20:08:21 +00:00
messagesByChannel = []
messagesByTsunamy = []
2019-06-08 21:21:30 +00:00
usersByChannel = []
messagesDataTsunamy = {
"global": [0] * 12,
"project": [0] * 12,
"democraty": [0] * 12,
"ecology": [0] * 12,
"technology": [0] * 12,
}
2019-06-08 20:08:21 +00:00
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": [{
"label": "global",
"backgroundColor": getColor(),
"data": messagesDataTsunamy['global']
},
{
"label": "projet",
"backgroundColor": getColor(),
"data": messagesDataTsunamy['project']
},
{
"label": "democratie",
"backgroundColor": getColor(),
"data": messagesDataTsunamy['democraty']
},
{
"label": "ecologie",
"backgroundColor": getColor(),
"data": messagesDataTsunamy['ecology']
},
{
"label": "technologie",
"backgroundColor": getColor(),
"data": messagesDataTsunamy['technology']
}]
}
2019-06-08 20:08:21 +00:00
while True:
channels = rocket.channels_list(offset=index).json()
totalChannels = channels['total']
2019-06-08 20:08:21 +00:00
for channel in channels['channels']:
2019-06-08 21:21:30 +00:00
dataMess = []
dataUsers = []
2019-06-08 20:08:21 +00:00
pprint( channel['name'] )
begin = date - monthdelta(12)
end = begin + monthdelta(1)
tsunamy = getTsunamy(channel)
2019-06-08 20:08:21 +00:00
for id in range(0, 12):
2019-06-08 20:32:24 +00:00
labels[id] = begin.strftime("%b %Y")
2019-06-08 20:08:21 +00:00
begindate = begin.isoformat()
enddate = end.isoformat()
resultMess = rocket.channels_history(channel['_id'], oldest= begindate, latest=enddate, count= 10000).json()
2019-06-16 09:02:24 +00:00
resultMess = list(filter(lambda mess: 't' not in mess, resultMess['messages']))
lenght = len(resultMess)
2019-06-08 21:21:30 +00:00
dataMess.append(lenght)
if lenght > 0:
if tsunamy & Tsunami.GLOBAL:
messagesDataTsunamy['global'][id] += lenght
if tsunamy & Tsunami.PROJECT:
messagesDataTsunamy['project'][id] += lenght
if tsunamy & Tsunami.DEMOCRACY:
messagesDataTsunamy['democraty'][id] += lenght
if tsunamy & Tsunami.ECOLOGY:
messagesDataTsunamy['ecology'][id] += lenght
if tsunamy & Tsunami.TECHNOLOGY:
messagesDataTsunamy['technology'][id] += lenght
2019-06-08 21:37:05 +00:00
users = []
2019-06-16 09:02:24 +00:00
for mess in resultMess:
2019-06-08 21:21:30 +00:00
users.append(mess['u']['_id'])
usersDistinct = set(users)
dataUsers.append(len(usersDistinct))
else:
dataUsers.append(0)
2019-06-08 20:08:21 +00:00
begin = end
end = begin + monthdelta(1)
2019-06-08 21:38:43 +00:00
color = getColor()
2019-06-08 20:08:21 +00:00
messageByChannel = {
"label": channel['name'],
2019-06-08 21:38:43 +00:00
"backgroundColor": color,
2019-06-08 21:21:30 +00:00
"data": dataMess
}
userByChannel = {
"label": channel['name'],
2019-06-08 21:38:43 +00:00
"backgroundColor": color,
2019-06-08 21:21:30 +00:00
"data": dataUsers
2019-06-08 20:08:21 +00:00
}
messagesByChannel.append(messageByChannel)
2019-06-08 21:21:30 +00:00
usersByChannel.append(userByChannel)
2019-06-08 20:08:21 +00:00
if channels['count'] + channels['offset'] >= channels['total']:
break
index += channels['count']
# 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')
2019-06-08 20:32:24 +00:00
2019-06-08 20:08:21 +00:00
statsFilePath = os.path.abspath(
2019-06-09 13:08:42 +00:00
os.path.join(dataFolder, 'channelsstat.json'))
2019-06-08 20:08:21 +00:00
with open(statsFilePath, "w") as file_write:
2019-06-08 20:32:24 +00:00
json.dump(info, file_write)
2019-06-08 20:08:21 +00:00