chg: [user-account] add heatmap nb user messages

ocr
terrtia 2024-03-28 14:59:26 +01:00
parent f37111fe2b
commit 414b5af277
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
3 changed files with 58 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import sys
import time import time
import uuid import uuid
from datetime import datetime
sys.path.append(os.environ['AIL_BIN']) sys.path.append(os.environ['AIL_BIN'])
################################## ##################################
@ -397,6 +398,33 @@ def get_user_account_chats_meta(user_id, chats, subchannels):
meta.append(chat_meta) meta.append(chat_meta)
return meta return meta
def get_user_account_nb_all_week_messages(user_id, chats, subchannels):
week = {}
# Init
for day in ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']:
week[day] = {}
for i in range(24):
week[day][i] = 0
# chats
for chat_g_id in chats:
c_subtype, c_id = chat_g_id.split(':', 1)
chat = Chats.Chat(c_id, c_subtype)
for message in chat.get_user_messages(user_id):
timestamp = message.split('/', 2)[1]
timestamp = datetime.utcfromtimestamp(float(timestamp))
date_name = timestamp.strftime('%a')
week[date_name][timestamp.hour] += 1
stats = []
nb_day = 0
for day in week:
for hour in week[day]:
stats.append({'date': day, 'day': nb_day, 'hour': hour, 'count': week[day][hour]})
nb_day += 1
return stats
#### FIX #### #### FIX ####
def fix_correlations_subchannel_message(): def fix_correlations_subchannel_message():
@ -535,6 +563,13 @@ def api_get_user_account(user_id, instance_uuid, translation_target=None):
meta['chats'] = get_user_account_chats_meta(user_id, meta['chats'], meta['subchannels']) meta['chats'] = get_user_account_chats_meta(user_id, meta['chats'], meta['subchannels'])
return meta, 200 return meta, 200
def api_get_user_account_nb_all_week_messages(user_id, instance_uuid):
user_account = UsersAccount.UserAccount(user_id, instance_uuid)
if not user_account.exists():
return {"status": "error", "reason": "Unknown user-account"}, 404
week = get_user_account_nb_all_week_messages(user_account.id, user_account.get_chats(), user_account.get_chat_subchannels())
return week, 200
def api_chat_messages(subtype, chat_id): def api_chat_messages(subtype, chat_id):
chat = Chats.Chat(chat_id, subtype) chat = Chats.Chat(chat_id, subtype)
if not chat.exists(): if not chat.exists():

View File

@ -296,3 +296,18 @@ def objects_user_account():
return render_template('user_account.html', meta=user_account, bootstrap_label=bootstrap_label, return render_template('user_account.html', meta=user_account, bootstrap_label=bootstrap_label,
ail_tags=Tag.get_modal_add_tags(user_account['id'], user_account['type'], user_account['subtype']), ail_tags=Tag.get_modal_add_tags(user_account['id'], user_account['type'], user_account['subtype']),
translation_languages=languages, translation_target=target) translation_languages=languages, translation_target=target)
@chats_explorer.route("objects/user-account/messages/stats/week/all", methods=['GET'])
@login_required
@login_read_only
def user_account_messages_stats_week_all():
instance_uuid = request.args.get('subtype')
user_id = request.args.get('id')
week = chats_viewer.api_get_user_account_nb_all_week_messages(user_id, instance_uuid)
if week[1] != 200:
return create_json_response(week[0], week[1])
else:
return jsonify(week[0])

View File

@ -39,6 +39,9 @@
{% endwith %} {% endwith %}
{% if meta['chats'] %} {% if meta['chats'] %}
<h4 class="mx-5 mt-2 text-secondary">User All Messages:</h4>
<div id="heatmapweekhourall"></div>
<h4>User Chats:</h4> <h4>User Chats:</h4>
{% for meta_chats in meta['chats'] %} {% for meta_chats in meta['chats'] %}
<div class="my-2"> <div class="my-2">
@ -69,6 +72,11 @@
{# {% endif %}#} {# {% endif %}#}
}); });
d3.json("{{ url_for('chats_explorer.user_account_messages_stats_week_all') }}?subtype={{ meta['subtype'] }}&id={{ meta['id'] }}")
.then(function(data) {
create_heatmap_week_hour('#heatmapweekhourall', data);
})
function toggle_sidebar(){ function toggle_sidebar(){
if($('#nav_menu').is(':visible')){ if($('#nav_menu').is(':visible')){
$('#nav_menu').hide(); $('#nav_menu').hide();