From 528609557cdf6ac6d551cbc846b6637f1b379d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:08:20 +0100 Subject: [PATCH 01/10] psql(users)!: add last_connection_at --- postgres-init/init.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/postgres-init/init.sql b/postgres-init/init.sql index c6b8a0fb..061b2705 100644 --- a/postgres-init/init.sql +++ b/postgres-init/init.sql @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS users ( country VARCHAR DEFAULT NULL, latitude DOUBLE PRECISION DEFAULT NULL, longitude DOUBLE PRECISION DEFAULT NULL, + last_connection_at TIMESTAMP WITH TIME ZONE DEFAULT now(), created_at TIMESTAMP WITH TIME ZONE DEFAULT now() ); From c37f694d027c0b939e0c592591ecc92cc43e6ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:08:50 +0100 Subject: [PATCH 02/10] feat(cliff): add psql -> PostgreSQL --- cliff.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/cliff.toml b/cliff.toml index 1e00ec9e..a0bd8122 100644 --- a/cliff.toml +++ b/cliff.toml @@ -55,6 +55,7 @@ commit_preprocessors = [ commit_parsers = [ { message = "^front", group = " Frontend" }, { message = "^back", group = " Backend" }, + { message = "^psql", group = " PostgreSQL" }, { message = "^db", group = " Database" }, { message = "^feat", group = " Features" }, { message = "^fix", group = " Bug Fixes" }, From 75302f983123ae01b46b8cf3769d4840ce0fb9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:16:52 +0100 Subject: [PATCH 03/10] db(last_connection): create get --- backend/matcha/db/last_connection.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 backend/matcha/db/last_connection.py diff --git a/backend/matcha/db/last_connection.py b/backend/matcha/db/last_connection.py new file mode 100644 index 00000000..fd753788 --- /dev/null +++ b/backend/matcha/db/last_connection.py @@ -0,0 +1,15 @@ +from matcha.db.utils import ( + db_fetchone, +) + + +def db_get_last_connection(id_user: int) -> str: + query = """ + SELECT last_connection_at + FROM users + WHERE + id = %s + ; + """ + + return db_fetchone(query, (id_user,))[0].timestamp() From 09ee645b615ad12622eedcf4448b10c86e041be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:17:06 +0100 Subject: [PATCH 04/10] back(profile): return last connection date --- backend/matcha/services/profile.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/matcha/services/profile.py b/backend/matcha/services/profile.py index 95f5b18d..cb113b5a 100644 --- a/backend/matcha/services/profile.py +++ b/backend/matcha/services/profile.py @@ -11,6 +11,8 @@ from matcha.db.visit import db_put_visit, db_get_visit +from matcha.db.last_connection import db_get_last_connection + from matcha.db.db import ( db_get_interests, db_set_user_profile_data, @@ -121,6 +123,7 @@ def services_profile(id_user, request): urlProfile=profile_url, isLiked=is_liked, connected=SocketManager.is_connected(user_db[0]), + lastConnection=db_get_last_connection(user_db[0]), ), 200, ) From 1ee47eb98acabd599d504ffafc631baa60fc0f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:25:56 +0100 Subject: [PATCH 05/10] style(socket): delete dead code --- backend/matcha/websocket/main_namespace.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/backend/matcha/websocket/main_namespace.py b/backend/matcha/websocket/main_namespace.py index d5f5e1d4..c0d4674d 100644 --- a/backend/matcha/websocket/main_namespace.py +++ b/backend/matcha/websocket/main_namespace.py @@ -7,14 +7,6 @@ from matcha.websocket.socket_manager import SocketManager -class ConnectedUser: - def __init__(self): - self.sid_userid = {} - - def insert(self, sid: str, user_id: int): - self.sid_userid.update({sid: user_id}) - - class MainNamespace(Namespace): def on_connect(self, auth): flaskprint(SocketManager()) From 7fbdea1611885c2346d397e9166523fe1d3f3d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:33:34 +0100 Subject: [PATCH 06/10] db(last_connection): update --- backend/matcha/db/last_connection.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/backend/matcha/db/last_connection.py b/backend/matcha/db/last_connection.py index fd753788..d8793f14 100644 --- a/backend/matcha/db/last_connection.py +++ b/backend/matcha/db/last_connection.py @@ -1,5 +1,6 @@ from matcha.db.utils import ( db_fetchone, + db_query, ) @@ -13,3 +14,19 @@ def db_get_last_connection(id_user: int) -> str: """ return db_fetchone(query, (id_user,))[0].timestamp() + + +def db_update_last_connection(id_user: int) -> str: + query = """ + UPDATE + users + SET + last_connection_at + = + GETDATE() + WHERE + id = %s + ; + """ + + return db_query(query, (id_user,)) From bb54b1fdc5094f7115ed9b305608d26a36fdeb61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:33:54 +0100 Subject: [PATCH 07/10] back(socket): update last connection on disconnect --- backend/matcha/websocket/main_namespace.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/matcha/websocket/main_namespace.py b/backend/matcha/websocket/main_namespace.py index c0d4674d..f51a3aa4 100644 --- a/backend/matcha/websocket/main_namespace.py +++ b/backend/matcha/websocket/main_namespace.py @@ -4,6 +4,8 @@ from matcha.utils import flaskprint +from matcha.db.last_connection import db_update_last_connection + from matcha.websocket.socket_manager import SocketManager @@ -33,6 +35,7 @@ def on_connect(self, auth): def on_disconnect(self): try: + db_update_last_connection(SocketManager.get_user_id(request.sid)) SocketManager.remove_session(request.sid) except Exception as e: flaskprint("Error handling message:" + str(e)) From 50d3f3a14359e93dcbb4fe69d19b2dc3a426d9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Tue, 17 Dec 2024 20:48:28 +0100 Subject: [PATCH 08/10] back(profile): update last connection on fetch --- backend/matcha/services/profile.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/matcha/services/profile.py b/backend/matcha/services/profile.py index cb113b5a..b898be58 100644 --- a/backend/matcha/services/profile.py +++ b/backend/matcha/services/profile.py @@ -7,6 +7,8 @@ from matcha.utils import check_request_json +from matcha.db.last_connection import db_update_last_connection + from matcha.db.pictures import db_get_user_images, db_get_url_profile from matcha.db.visit import db_put_visit, db_get_visit @@ -71,6 +73,8 @@ def services_profile(id_user, request): elif "error" in profile_picture: profile_url = url = profile_picture["error"] + db_update_last_connection(id_user) + return ( jsonify( username=user_db[0], From 6cd3a3a017394acc4b3e9ced2b53b9e14363d3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Wed, 18 Dec 2024 11:59:04 +0100 Subject: [PATCH 09/10] fix(last_connection): getdate does not exist ERROR: function getdate() does not exist at character 64 use the same now() function from the psql init --- backend/matcha/db/last_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/matcha/db/last_connection.py b/backend/matcha/db/last_connection.py index d8793f14..b4e4236a 100644 --- a/backend/matcha/db/last_connection.py +++ b/backend/matcha/db/last_connection.py @@ -23,7 +23,7 @@ def db_update_last_connection(id_user: int) -> str: SET last_connection_at = - GETDATE() + now() WHERE id = %s ; From c7c17d1699cdd622a89bf84ddfc304b4250ad74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Wed, 18 Dec 2024 12:00:25 +0100 Subject: [PATCH 10/10] fix(socket): variable name wasn't the same TODO add test on them web sockets --- backend/matcha/websocket/socket_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/matcha/websocket/socket_manager.py b/backend/matcha/websocket/socket_manager.py index 212ddbb5..838ee3c5 100644 --- a/backend/matcha/websocket/socket_manager.py +++ b/backend/matcha/websocket/socket_manager.py @@ -29,7 +29,7 @@ def add_session(cls, sid, user_id): @classmethod def remove_session(cls, sid): user_id = cls.sid_userid.pop(sid, None) - cls.userid_sid.pop(userid, None) + cls.userid_sid.pop(user_id, None) @classmethod def __str__(cls):