Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Firebase Realtime Database storage functionality in tracker store #8681

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

Thirunayan22
Copy link

Proposed changes:

  • Added Firebase storage functionality addressed in issue Update tracker store to enable storing messages in Firebase Realtime Database #8642 to tracker_stores.py
  • Created FirebaseRealtimeTrackerStore class to to track and store conversation history in a firebase real-time database instance
  • Updated _create_from_endpoint_config method to connect a Firebase Realtime Database instance through endpoints.yml
  • Updated dependencies to include Firebase Admin SDK

Status (please check what you already did):

  • added some tests for the functionality
  • updated the documentation
  • updated the changelog (please check changelog for instructions)
  • reformat files using black (please check Readme for instructions)

@CLAassistant
Copy link

CLAassistant commented May 13, 2021

CLA assistant check
All committers have signed the CLA.

@sara-tagger
Copy link
Collaborator

Thanks for submitting a pull request 🚀 @amn41 will take a look at it as soon as possible ✨

@sara-tagger sara-tagger requested a review from amn41 May 13, 2021 06:00
@stale
Copy link

stale bot commented Apr 16, 2022

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 16, 2022
@ancalita ancalita removed the request for review from amn41 May 18, 2022 12:18
@stale stale bot removed the stale label May 18, 2022
Copy link
Member

@ancalita ancalita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thirunayan22 thank you for your contribution 💯 and my apologies for getting back so late.
I've left some comments that would be best addressed once you can pull the latest changes from main and resolve any conflicts (considering the long time passed, perhaps the best course of action would be to replicate this contribution in a fresh new PR targeting the latest main).

Please note that we also require rigorous unit test coverage, as well as a changelog entry to give details of your improvement.

self.database_url = database_url
self.db_storage_path = db_storage_path

self.cred = credentials.Certificate(self.key_file_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.cred = credentials.Certificate(self.key_file_path)
self.credentials = credentials.Certificate(self.key_file_path)

self.db_storage_path = db_storage_path

self.cred = credentials.Certificate(self.key_file_path)
init_app = firebase_admin.initialize_app(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would store this as an instance attribute:

Suggested change
init_app = firebase_admin.initialize_app(
self.db = firebase_admin.initialize_app(

serialised_tracker = FirebaseRealtimeTrackerStore.serialise_tracker(tracker)

user_message_id = f"user_{int(time())}"
user_message = tracker.latest_message.text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check that the latest message which is of type Optional[UserUttered] is not None:

Suggested change
user_message = tracker.latest_message.text
user_message = tracker.latest_message
if user_message is not None:
message_text = user_message.text
else:
message_text = None

bot_message_id = f"bot_{int(time())}"
bot_message = self.get_latest_bot_message(serialised_tracker)

if bot_message != None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if bot_message != None:
if bot_message is not None:

self.push_conversation_db(
ref_path=f"{self.db_storage_path}/{tracker.sender_id}",
message_id=user_message_id,
message=user_message,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method call should be extracted outside this if statement, by checking first that message_text is not None, because regardless of whether there is a bot response, the user message should be saved, right?

"""
Supporting multiple outputs by bot dispatcher
"""
serialised_tracker = json.loads(serialised_tracker)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serialised_tracker = json.loads(serialised_tracker)
deserialised_tracker = json.loads(serialised_tracker)

You could also re-use the deserialise_tracker method?

else:
return None

def push_conversation_db(self, ref_path, message_id, message) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type annotations for args, please add.

def push_conversation_db(self, ref_path, message_id, message) -> None:

ref = db.reference(ref_path)
ref.update({message_id: message})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar at all with the firebase_admin library, but I have a hunch a call to push ref to the firebase db is missing here? Please investigate and add a few unit tests to check your assumptions.

@@ -1257,6 +1349,14 @@ def _create_from_endpoint_config(
tracker_store = DynamoTrackerStore(
domain=domain, event_broker=event_broker, **endpoint_config.kwargs
)
elif endpoint_config.type.lower() == "firebase-realtime":
tracker_store = FirebaseRealtimeTrackerStore(
database_url=endpoint_config.url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please maintain the order of args, first should be domain, followed by database_url, then event_broker etc.

domain: Domain,
firebase_key_file_path: Text,
db_storage_path: Text,
database_url: Text,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using the parameter name host instead of database_url to maintain code consistency.

@Thirunayan22
Copy link
Author

Hi @ancalita , no worries, and thanks a lot for reviewing the PR 💯 , I was also waiting for the feedback on this. As you noted, I will make the changes and get back to you.
Thank you

@tmbo tmbo requested a review from a team as a code owner September 12, 2022 19:48
@tmbo tmbo requested review from sanchariGr and removed request for a team September 12, 2022 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants