1st refacto

This commit is contained in:
Mickael 2019-07-03 22:10:09 +02:00
parent 20157b2cd3
commit 2c188c4ccf

View file

@ -20,145 +20,125 @@ def getColor():
b = random.randrange(255) b = random.randrange(255)
return 'rgb({:0},{:0},{:0})'.format(r,g,b) return 'rgb({:0},{:0},{:0})'.format(r,g,b)
rocket = RocketChat(cfg.rocket['user'], cfg.rocket['password'], def save(info):
server_url='https://coa.crapaud-fou.org') # Récupération du répertoire racine du repo
index = 0 rootFolder = os.path.join(os.path.dirname(__file__), '..')
labels = [None] * 12 # Répertoire pour stocker le fichier de sortie
messagesByChannel = [] dataFolder = os.path.join(rootFolder, 'public', 'data')
messagesByTsunamy = []
usersByChannel = []
messagesDataTsunamy = {
"global": [0] * 12,
"project": [0] * 12,
"democraty": [0] * 12,
"ecology": [0] * 12,
"technology": [0] * 12,
}
usersGlobal = []
now = datetime.now() statsFilePath = os.path.abspath(
date = datetime(now.year, now.month, now.day, 0,0,0) os.path.join(dataFolder, 'channelsstat.json'))
with open(statsFilePath, "w") as file_write:
json.dump(info, file_write)
info = { def createElement(label, color, data) :
"updated": "updated {:02}/{:02}/{:04}".format(now.day, now.month, now.year), return {
"labels": labels, "label": label,
"messagesByChannel": messagesByChannel, "backgroundColor": color,
"usersByChannel": usersByChannel, "data": data
"messagesByTsunamy": [{ }
"label": "global",
"backgroundColor": getColor(), def main():
"data": messagesDataTsunamy['global'] rocket = RocketChat(cfg.rocket['user'], cfg.rocket['password'],
}, server_url='https://coa.crapaud-fou.org')
{ index = 0
"label": "projet", labels = [None] * 12
"backgroundColor": getColor(), messagesByChannel = []
"data": messagesDataTsunamy['project'] usersByChannel = []
}, messagesDataTsunamy = {
{ "global": [0] * 12,
"label": "democratie", "project": [0] * 12,
"backgroundColor": getColor(), "democraty": [0] * 12,
"data": messagesDataTsunamy['democraty'] "ecology": [0] * 12,
}, "technology": [0] * 12,
{ }
"label": "ecologie", usersGlobal = []
"backgroundColor": getColor(),
"data": messagesDataTsunamy['ecology'] now = datetime.now()
}, date = datetime(now.year, now.month, now.day, 0,0,0)
{
"label": "technologie", info = {
"backgroundColor": getColor(), "updated": "updated {:02}/{:02}/{:04}".format(now.day, now.month, now.year),
"data": messagesDataTsunamy['technology'] "labels": labels,
}], "messagesByChannel": messagesByChannel,
"usersByChannel": usersByChannel,
"messagesByTsunamy": [
createElement("global", getColor(), messagesDataTsunamy['global']),
createElement("project", getColor(), messagesDataTsunamy['project']),
createElement("democratie", getColor(), messagesDataTsunamy['democraty']),
createElement("ecologie", getColor(), messagesDataTsunamy['ecology']),
createElement("technologie", getColor(), messagesDataTsunamy['technology'])
],
"usersGlobal": usersGlobal "usersGlobal": usersGlobal
} }
usersTest = [None] * 12 usersTest = [None] * 12
while True: while True:
channels = rocket.channels_list(offset=index).json() channels = rocket.channels_list(offset=index).json()
totalChannels = channels['total']
for channel in channels['channels']:
for channel in channels['channels']: dataMess = []
dataMess = [] dataUsers = []
dataUsers = [] pprint( channel['name'] )
pprint( channel['name'] ) begin = date - monthdelta(12)
begin = date - monthdelta(12)
end = begin + monthdelta(1)
tsunamy = getTsunamy(channel)
for id in range(0, 12):
if usersTest[id] == None:
usersTest[id] = []
labels[id] = begin.strftime("%b %Y")
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']))
lenght = len(resultMess)
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
users = []
for mess in resultMess:
users.append(mess['u']['_id'])
usersTest[id].append(mess['u']['_id'])
usersDistinct = set(users)
dataUsers.append(len(usersDistinct))
else:
dataUsers.append(0)
begin = end
end = begin + monthdelta(1) end = begin + monthdelta(1)
color = getColor()
messageByChannel = {
"label": channel['name'],
"backgroundColor": color,
"data": dataMess
}
userByChannel = { tsunamy = getTsunamy(channel)
"label": channel['name'],
"backgroundColor": color,
"data": dataUsers
}
messagesByChannel.append(messageByChannel) for id in range(0, 12):
usersByChannel.append(userByChannel) if usersTest[id] == None:
usersTest[id] = []
labels[id] = begin.strftime("%b %Y")
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']))
lenght = len(resultMess)
dataMess.append(lenght)
if channels['count'] + channels['offset'] >= channels['total']: if lenght > 0:
break if tsunamy & Tsunami.GLOBAL:
index += channels['count'] 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
for id in range(0, 12): users = []
usersTest[id] = len(set(usersTest[id])) for mess in resultMess:
users.append(mess['u']['_id'])
usersTest[id].append(mess['u']['_id'])
usersDistinct = set(users)
dataUsers.append(len(usersDistinct))
else:
dataUsers.append(0)
userGlobal = { begin = end
"label": 'global', end = begin + monthdelta(1)
"backgroundColor": 'red',
"data": usersTest color = getColor()
} messageByChannel = createElement(channel['name'], color,dataMess)
usersGlobal.append(userGlobal) userByChannel = createElement( channel['name'], color,dataUsers)
# Récupération du répertoire racine du repo messagesByChannel.append(messageByChannel)
rootFolder = os.path.join(os.path.dirname(__file__), '..') usersByChannel.append(userByChannel)
# Répertoire pour stocker le fichier de sortie
dataFolder = os.path.join(rootFolder, 'public', 'data')
statsFilePath = os.path.abspath( if channels['count'] + channels['offset'] >= channels['total']:
os.path.join(dataFolder, 'channelsstat.json')) break
with open(statsFilePath, "w") as file_write: index += channels['count']
json.dump(info, file_write)
for id in range(0, 12):
usersTest[id] = len(set(usersTest[id]))
userGlobal = createElement( 'global', 'red', usersTest)
usersGlobal.append(userGlobal)
save(info)
if __name__ == "__main__":
main()