diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index 79fbd477..f6f56449 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -11,6 +11,7 @@ import sys import time import uuid +from datetime import datetime 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) 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 #### 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']) 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): chat = Chats.Chat(chat_id, subtype) if not chat.exists(): diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index 1a53ef8f..6d8d2091 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -296,3 +296,18 @@ def objects_user_account(): 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']), 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]) + + + diff --git a/var/www/templates/chats_explorer/user_account.html b/var/www/templates/chats_explorer/user_account.html index 49a3380b..188779b2 100644 --- a/var/www/templates/chats_explorer/user_account.html +++ b/var/www/templates/chats_explorer/user_account.html @@ -39,6 +39,9 @@ {% endwith %} {% if meta['chats'] %} +

User All Messages:

+
+

User Chats:

{% for meta_chats in meta['chats'] %}
@@ -69,6 +72,11 @@ {# {% 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(){ if($('#nav_menu').is(':visible')){ $('#nav_menu').hide();