Merge branch 'update_chat_info' into 'master'

Update chat info

See merge request crapaud-fou/portail!1
This commit is contained in:
Mickael (MckMonster) 2019-08-18 21:42:19 +00:00
commit 925ac0b5e3
7 changed files with 136 additions and 32 deletions

5
.gitignore vendored
View file

@ -1,3 +1,6 @@
# site/data/*.json
!site/data/*.sample.json
config
config
scripts/dev_config.py
.vscode
scripts/__pycache__

View file

@ -1,4 +1,4 @@
image: alpine
image: frolvlad/alpine-python3
pages:
stage: deploy

View file

@ -1,9 +1,10 @@
#!/bin/sh
pip3 install -r ./scripts/requirements.txt
# idees
cp ./site/data/idees.sample.json ./site/data/idees.json
# chat
cp ./site/data/chat.sample.json ./site/data/chat.json
python3 ./scripts/generate_chat_info.py
# wiki
# wiki

View file

@ -0,0 +1,123 @@
from rocketchat_API.rocketchat import RocketChat
import os
import json
from datetime import datetime
from monthdelta import monthdelta
def main():
print("Start chat info generation")
crapauds_total = 0
crapauds_recent = 0
channels_total = 0
channels_recent = 0
channels_list = []
messages_total = 0
messages_recent = 0
recent_date = datetime.now() - monthdelta()
rocket = RocketChat(None, None, auth_token= os.environ['ROCKETCHAT_AUTH'], user_id= os.environ['ROCKETCHAT_USERID'], server_url=os.environ['ROCKETCHAT_SERVER'])
print("Check users")
users = rocket.users_list().json()
crapauds_total = users["total"]
print("Check channels")
channels = getAllChannels(rocket)
channels_total = len(channels)
users = []
for channel in channels:
messages_total += int(channel["msgs"])
date = channel["lm"]
channel_date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
if channel_date > recent_date:
channels_recent += 1
print("Check messages for channels {}".format(channel['name']))
messages = rocket.channels_history(channel["_id"], oldest= recent_date, count= 10000).json()
if messages["success"]:
nbMessages = len(messages["messages"])
messages_recent += nbMessages
channels_list.append((channel["name"], nbMessages))
users.extend(map(lambda message: message["u"]["_id"], messages["messages"]))
else:
print("Error : {}".format(messages["error"]))
# Get the channels with the most number of message
channels_list.sort(key=lambda channel: channel[1], reverse= True)
channels_recentlist = list(map(lambda channel: channel[0], channels_list))
crapauds_recent = len(set(users))
info = {
"crapauds": {
"total": crapauds_total,
"recent": crapauds_recent
},
"canaux": {
"total": channels_total,
"recent": channels_recent,
# Get only the 10 first channels
"liste": channels_recentlist[0:10]
},
"messages": {
"total": messages_total,
"recent": messages_recent
}
}
save(info)
print("End chat info generation")
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, 'site', 'data')
statsFilePath = os.path.abspath(
os.path.join(dataFolder, 'chat.json'))
with open(statsFilePath, "w") as file_write:
json.dump(info, file_write)
def getAllChannels(rocket):
index = 0
allChannels = []
while True:
channels = rocket.channels_list(offset = index).json()
allChannels.extend([ channel for channel in channels['channels'] if 'archived' not in channel])
if channels['count'] + channels['offset'] >= channels['total']:
break
index += channels['count']
return allChannels
def getAllUsers(rocket):
index = 0
allUsers = []
while True:
users = rocket.users_list(offset = index).json()
allUsers.extend(users["users"])
if users['count'] + users['offset'] >= users['total']:
break
index += users['count']
return allUsers
def getAllMessages(rocket, roomid, begindate):
index = 0
allMessages = []
while True:
messages = rocket.channels_history(roomid, offset = index, oldest= begindate).json()
allMessages.extend(messages["m"])
if messages['count'] + messages['offset'] >= messages['total']:
break
index += messages['count']
return allMessages
if __name__ == "__main__":
main()

2
scripts/requirements.txt Normal file
View file

@ -0,0 +1,2 @@
rocketchat_API==0.6.34
MonthDelta==0.9.1

View file

@ -1,26 +1 @@
{
"crapauds": {
"total": 2300,
"recent": 200
},
"canaux": {
"total": 183,
"recent": 23,
"liste": [
"general",
"semourais",
"cohorte-brest",
"accueil",
"right-to-repair",
"cohorte-34",
"cercle-neurodiversité",
"cercle-permaculture",
"no-gafam",
"Le LED"
]
},
"messages": {
"total": 23400,
"recent": 892
}
}
{"crapauds": {"total": 2365, "recent": 181}, "canaux": {"total": 177, "recent": 99, "liste": ["semourais", "accueil", "general", "right-to-repair", "wiki-crapaud-fou", "cohorte-loire", "reflexion-sur-l-argent", "le-led", "empowerment", "libre"]}, "messages": {"total": 186095, "recent": 13043}}

View file

@ -143,12 +143,12 @@
<div class="recent">
<div><span class="nombre crapauds">0</span> crapauds actifs</div>
<div><span class="nombre canaux">0</span> canaux actifs</div>
<div><span class="nombre messages">0</span> nouveaux messages</div>
<div><span class="nombre messages">0</span> messages</div>
</div>
</div>
</div>
<div>
<div>Les canaux les plus actifs sur 30 jours</div>
<div>Les 10 canaux les plus actifs sur 30 jours</div>
<div>
<ul class="list">
</ul>