From 0c55724caa90de4161c75bd12afb54bfce895172 Mon Sep 17 00:00:00 2001 From: Ekaropolus Date: Wed, 23 Jul 2025 01:39:45 -0600 Subject: [PATCH] add storage option wit neo4j and postgress DB --- pxy_meta_pages/services.py | 48 ++++++++++++++++++------------ pxy_meta_pages/storage_backends.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 pxy_meta_pages/storage_backends.py diff --git a/pxy_meta_pages/services.py b/pxy_meta_pages/services.py index da0d32e..eb84588 100644 --- a/pxy_meta_pages/services.py +++ b/pxy_meta_pages/services.py @@ -4,6 +4,7 @@ import logging from .models import FacebookPageAssistant from django.core.exceptions import ObjectDoesNotExist from pxy_neo4j.neo4j_connector import Neo4jDatabase +from .storage_backends import Neo4jStorage, PostgresStorage logger = logging.getLogger(__name__) @@ -18,6 +19,10 @@ class FacebookService: self.facebook_api_version = facebook_api_version self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}" self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection + self._storages = [ + Neo4jStorage(), # always log to Neo4j + PostgresStorage(), # always log to Postgres + ] def reply_to_comment(self, page_id, comment_id, message): """ @@ -106,10 +111,12 @@ class FacebookService: ) if shared: self._store_interaction( - page_id, - user_message=description or "Shared post comment", - bot_response=bot_response, - platform="Facebook" + page_id = page_id, + object_id = post_id, + parent_object_id = parent_id, + prompt = prompt, + bot_response = bot_response, + platform = "Facebook" ) # 6) Post on original post @@ -117,10 +124,12 @@ class FacebookService: orig = self._post_facebook_comment(parent_id, bot_response, page_access_token) if orig: self._store_interaction( - page_id, - user_message=description or "Original post comment", - bot_response=bot_response, - platform="Facebook (Original Post)" + page_id = page_id, + object_id = post_id, + parent_object_id = parent_id, + prompt = prompt, + bot_response = bot_response, + platform = "Facebook" ) return shared @@ -153,23 +162,24 @@ class FacebookService: def _store_interaction( self, page_id: str, - user_message: str, + object_id: str, + parent_object_id: str | None, + prompt: str, bot_response: str, platform: str ) -> None: """ - Encapsulates the repeated Neo4j store_interaction calls. + Dispatch this interaction to all registered storage backends. """ - try: - self.neo4j_db.store_interaction( - user_id = f"fb_bot_{page_id}", - bot_id = f"fb_bot_{page_id}", - user_message = user_message, - bot_response = bot_response, - platform = platform + for backend in self._storages: + backend.store( + page_id=page_id, + object_id=object_id, + parent_object_id=parent_object_id, + prompt=prompt, + bot_response=bot_response, + platform=platform ) - except Exception as e: - logger.error(f"Error logging interaction for page {page_id}: {e}") diff --git a/pxy_meta_pages/storage_backends.py b/pxy_meta_pages/storage_backends.py new file mode 100644 index 0000000..cddc18b --- /dev/null +++ b/pxy_meta_pages/storage_backends.py @@ -0,0 +1,46 @@ +from .models import BotInteraction, FacebookPageAssistant +from pxy_neo4j.neo4j_connector import Neo4jDatabase +import logging + +logger = logging.getLogger(__name__) + +class InteractionStorageBackend: + """Interface: implement `.store(...)`.""" + def store(self, *, page_id, object_id, parent_object_id, + prompt, bot_response, platform): + raise NotImplementedError + + +class Neo4jStorage(InteractionStorageBackend): + def __init__(self): + self.neo4j_db = Neo4jDatabase() + + def store(self, *, page_id, object_id, parent_object_id, + prompt, bot_response, platform): + try: + self.neo4j_db.store_interaction( + user_id = f"fb_bot_{page_id}", + bot_id = f"fb_bot_{page_id}", + user_message = prompt, + bot_response = bot_response, + platform = platform + ) + except Exception as e: + logger.error(f"[Neo4j] Error saving interaction: {e}") + + +class PostgresStorage(InteractionStorageBackend): + def store(self, *, page_id, object_id, parent_object_id, + prompt, bot_response, platform): + try: + page = FacebookPageAssistant.objects.get(page_id=page_id) + BotInteraction.objects.create( + page = page, + object_id = object_id or "", + parent_object_id = parent_object_id, + prompt = prompt, + bot_response = bot_response, + platform = platform + ) + except Exception as e: + logger.error(f"[Postgres] Error saving BotInteraction: {e}")