From 87dc61917166e082bdad46ceb3b1fb70030cf386 Mon Sep 17 00:00:00 2001 From: terrtia Date: Tue, 5 Mar 2024 10:56:36 +0100 Subject: [PATCH] chg: [chats] add heatmap nb messages/hour for all messages --- bin/lib/chats_viewer.py | 11 ++++-- bin/lib/objects/Messages.py | 4 +-- bin/lib/objects/abstract_chat_object.py | 36 ++++++++++++++++--- var/www/blueprints/chats_explorer.py | 18 ++++++++-- .../chats_explorer/SubChannelMessages.html | 18 ++++++++++ .../templates/chats_explorer/chat_viewer.html | 16 +++++++-- 6 files changed, 90 insertions(+), 13 deletions(-) diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index cac6050b..0ec42772 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -346,14 +346,21 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page) return meta, 200 -def api_get_nb_message_by_week(chat_id, chat_instance_uuid): - chat = Chats.Chat(chat_id, chat_instance_uuid) +def api_get_nb_message_by_week(chat_type, chat_instance_uuid, chat_id): + chat = get_obj_chat(chat_type, chat_instance_uuid, chat_id) 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_nb_week_messages(chat_type, chat_instance_uuid, chat_id): + chat = get_obj_chat(chat_type, chat_instance_uuid, chat_id) + if not chat.exists(): + return {"status": "error", "reason": "Unknown chat"}, 404 + week = chat.get_nb_week_messages() + return week, 200 + def api_get_chat_participants(chat_type, chat_subtype, chat_id): if chat_type not in ['chat', 'chat-subchannel', 'chat-thread']: return {"status": "error", "reason": "Unknown chat type"}, 400 diff --git a/bin/lib/objects/Messages.py b/bin/lib/objects/Messages.py index e01143ec..56281e69 100755 --- a/bin/lib/objects/Messages.py +++ b/bin/lib/objects/Messages.py @@ -89,7 +89,7 @@ class Message(AbstractObject): def get_date(self): timestamp = self.get_timestamp() - return datetime.fromtimestamp(float(timestamp)).strftime('%Y%m%d') + return datetime.utcfromtimestamp(float(timestamp)).strftime('%Y%m%d') def get_timestamp(self): dirs = self.id.split('/') @@ -250,7 +250,7 @@ class Message(AbstractObject): timestamp = self.get_timestamp() else: timestamp = float(timestamp) - timestamp = datetime.fromtimestamp(float(timestamp)) + timestamp = datetime.utcfromtimestamp(float(timestamp)) meta['date'] = timestamp.strftime('%Y/%m/%d') meta['hour'] = timestamp.strftime('%H:%M:%S') meta['full_date'] = timestamp.isoformat(' ') diff --git a/bin/lib/objects/abstract_chat_object.py b/bin/lib/objects/abstract_chat_object.py index 79e4361e..9674d735 100755 --- a/bin/lib/objects/abstract_chat_object.py +++ b/bin/lib/objects/abstract_chat_object.py @@ -51,8 +51,6 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): # get useraccount / username # get users ? # timeline name ???? - # info - # created # last imported/updated # TODO get instance @@ -97,7 +95,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): def get_created_at(self, date=False): created_at = self._get_field('created_at') if date and created_at: - created_at = datetime.fromtimestamp(float(created_at)) + created_at = datetime.utcfromtimestamp(float(created_at)) created_at = created_at.isoformat(' ') return created_at @@ -176,7 +174,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): 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()) + timestamp = time.mktime(datetime.strptime(date_day, "%Y%m%d").utctimetuple()) 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) @@ -197,6 +195,34 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): week_date = Date.get_current_week_day() return self.get_nb_message_by_week(week_date) + def get_nb_week_messages(self): + week = {} + # Init + for day in ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']: + week[day] = {} + for i in range(24): + week[day][i] = 0 + + # chat + for mess_t in r_object.zrange(f'messages:{self.type}:{self.subtype}:{self.id}', 0, -1, withscores=True): + timestamp = datetime.utcfromtimestamp(float(mess_t[1])) + date_name = timestamp.strftime('%a') + week[date_name][timestamp.hour] += 1 + + subchannels = self.get_subchannels() + for gid in subchannels: + for mess_t in r_object.zrange(f'messages:{gid}', 0, -1, withscores=True): + timestamp = datetime.utcfromtimestamp(float(mess_t[1])) + 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 + def get_message_meta(self, message, timestamp=None, translation_target='', options=None): # TODO handle file message message = Messages.Message(message[9:]) if not options: @@ -222,7 +248,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): mess, pagination = self._get_messages(nb=nb, page=page) for message in mess: timestamp = message[1] - date_day = datetime.fromtimestamp(timestamp).strftime('%Y/%m/%d') + date_day = datetime.utcfromtimestamp(timestamp).strftime('%Y/%m/%d') if date_day != curr_date: messages[date_day] = [] curr_date = date_day diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index f20a59c0..fc0d539a 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -98,9 +98,23 @@ def chats_explorer_chat(): @login_required @login_read_only def chats_explorer_messages_stats_week(): + chat_type = request.args.get('type') + instance_uuid = request.args.get('subtype') 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) + week = chats_viewer.api_get_nb_message_by_week(chat_type, instance_uuid, chat_id) + if week[1] != 200: + return create_json_response(week[0], week[1]) + else: + return jsonify(week[0]) + +@chats_explorer.route("chats/explorer/messages/stats/week/all", methods=['GET']) +@login_required +@login_read_only +def chats_explorer_messages_stats_week_all(): + chat_type = request.args.get('type') + instance_uuid = request.args.get('subtype') + chat_id = request.args.get('id') + week = chats_viewer.api_get_nb_week_messages(chat_type, instance_uuid, chat_id) # TODO SELECT DATE if week[1] != 200: return create_json_response(week[0], week[1]) else: diff --git a/var/www/templates/chats_explorer/SubChannelMessages.html b/var/www/templates/chats_explorer/SubChannelMessages.html index 5eb6153b..4f5a98da 100644 --- a/var/www/templates/chats_explorer/SubChannelMessages.html +++ b/var/www/templates/chats_explorer/SubChannelMessages.html @@ -21,6 +21,7 @@ #} +