From 7bf0fe8992a3da7bcf469c98221da73363a0c2a4 Mon Sep 17 00:00:00 2001 From: terrtia Date: Mon, 13 Nov 2023 14:10:24 +0100 Subject: [PATCH] chg: [chats] add heatmap nb week messages by hour --- bin/lib/chats_viewer.py | 8 + bin/lib/objects/abstract_chat_object.py | 27 ++ bin/packages/Date.py | 14 + var/www/blueprints/chats_explorer.py | 12 + .../templates/chats_explorer/chat_viewer.html | 333 ++++++++++++++++++ var/www/update_thirdparty.sh | 2 +- 6 files changed, 395 insertions(+), 1 deletion(-) diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index a3ad11c6..bae98988 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -313,6 +313,14 @@ def api_get_chat(chat_id, chat_instance_uuid): meta['messages'], meta['tags_messages'] = chat.get_messages() return meta, 200 +def api_get_nb_message_by_week(chat_id, chat_instance_uuid): + chat = Chats.Chat(chat_id, chat_instance_uuid) + if not chat.exists(): + return {"status": "error", "reason": "Unknown chat"}, 404 + week = chat.get_nb_message_this_week() + # week = chat.get_nb_message_by_week('20231109') + return week, 200 + def api_get_subchannel(chat_id, chat_instance_uuid): subchannel = ChatSubChannels.ChatSubChannel(chat_id, chat_instance_uuid) if not subchannel.exists(): diff --git a/bin/lib/objects/abstract_chat_object.py b/bin/lib/objects/abstract_chat_object.py index 188f1c5a..d1645913 100755 --- a/bin/lib/objects/abstract_chat_object.py +++ b/bin/lib/objects/abstract_chat_object.py @@ -8,6 +8,7 @@ Base Class for AIL Objects ################################## import os import sys +import time from abc import ABC from datetime import datetime @@ -21,6 +22,7 @@ from lib.objects.abstract_subtype_object import AbstractSubtypeObject from lib.ail_core import get_object_all_subtypes, zscan_iter ################ from lib.ConfigLoader import ConfigLoader from lib.objects import Messages +from packages import Date # from lib.data_retention_engine import update_obj_date @@ -141,6 +143,30 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): def get_last_message(self): return r_object.zrevrange(f'messages:{self.type}:{self.subtype}:{self.id}', 0, 0) + def get_nb_message_by_hours(self, date_day, nb_day): + hours = [] + # start=0, end=23 + timestamp = time.mktime(datetime.strptime(date_day, "%Y%m%d").timetuple()) + for i in range(24): + timestamp_end = timestamp + 3600 + nb_messages = r_object.zcount(f'messages:{self.type}:{self.subtype}:{self.id}', timestamp, timestamp_end) + timestamp = timestamp_end + hours.append({'date': f'{date_day[0:4]}-{date_day[4:6]}-{date_day[6:8]}', 'day': nb_day, 'hour': i, 'count': nb_messages}) + return hours + + def get_nb_message_by_week(self, date_day): + date_day = Date.get_date_week_by_date(date_day) + week_messages = [] + i = 0 + for date in Date.daterange_add_days(date_day, 6): + week_messages = week_messages + self.get_nb_message_by_hours(date, i) + i += 1 + return week_messages + + def get_nb_message_this_week(self): + week_date = Date.get_current_week_day() + return self.get_nb_message_by_week(week_date) + def get_message_meta(self, message, timestamp=None): # TODO handle file message message = Messages.Message(message[9:]) meta = message.get_meta(options={'content', 'link', 'parent', 'parent_meta', 'user-account'}, timestamp=timestamp) @@ -205,6 +231,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): self.add_obj_children(obj_global_id, mess_id) + # get_messages_meta ???? # TODO move me to abstract subtype diff --git a/bin/packages/Date.py b/bin/packages/Date.py index 49bf38eb..6e58fb91 100644 --- a/bin/packages/Date.py +++ b/bin/packages/Date.py @@ -85,11 +85,25 @@ def get_today_date_str(separator=False): else: return datetime.date.today().strftime("%Y%m%d") +def get_current_week_day(): + dt = datetime.date.today() + start = dt - datetime.timedelta(days=dt.weekday()) + return start.strftime("%Y%m%d") + +def get_date_week_by_date(date): + dt = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) + start = dt - datetime.timedelta(days=dt.weekday()) + return start.strftime("%Y%m%d") + def date_add_day(date, num_day=1): new_date = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) + datetime.timedelta(num_day) new_date = str(new_date).replace('-', '') return new_date +def daterange_add_days(date, nb_days): + end_date = date_add_day(date, num_day=nb_days) + return get_daterange(date, end_date) + def date_substract_day(date, num_day=1): new_date = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) - datetime.timedelta(num_day) new_date = str(new_date).replace('-', '') diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index 55cba38c..1a6dfba7 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -87,6 +87,18 @@ def chats_explorer_chat(): chat = chat[0] return render_template('chat_viewer.html', chat=chat, bootstrap_label=bootstrap_label) +@chats_explorer.route("chats/explorer/messages/stats/week", methods=['GET']) +@login_required +@login_read_only +def chats_explorer_messages_stats_week(): + chat_id = request.args.get('id') + instance_uuid = request.args.get('uuid') + week = chats_viewer.api_get_nb_message_by_week(chat_id, instance_uuid) + if week[1] != 200: + return create_json_response(week[0], week[1]) + else: + return jsonify(week[0]) + @chats_explorer.route("/chats/explorer/subchannel", methods=['GET']) @login_required @login_read_only diff --git a/var/www/templates/chats_explorer/chat_viewer.html b/var/www/templates/chats_explorer/chat_viewer.html index 8010b508..fed8676b 100644 --- a/var/www/templates/chats_explorer/chat_viewer.html +++ b/var/www/templates/chats_explorer/chat_viewer.html @@ -16,6 +16,7 @@ +