-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from openchatai/feat/analytics
Feat/analytics
- Loading branch information
Showing
5 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from shared.models.opencopilot_db.analytics import Analytics | ||
from sqlalchemy.orm import sessionmaker | ||
from shared.models.opencopilot_db import engine | ||
|
||
def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, logs: str = ""): | ||
Session = sessionmaker(bind=engine) | ||
session = Session() | ||
|
||
# Fetch the existing record, if any | ||
existing_record = session.query(Analytics).filter_by(chatbot_id=chatbot_id).first() | ||
|
||
if existing_record: | ||
# Increment existing values | ||
existing_record.successful_operations += successful_operations | ||
existing_record.total_operations += total_operations | ||
if logs: | ||
existing_record.logs = logs | ||
print("Analytics record updated for chatbot_id:", chatbot_id) | ||
else: | ||
# Create a new record | ||
record_to_upsert = Analytics( | ||
chatbot_id=chatbot_id, | ||
successful_operations=successful_operations, | ||
total_operations=total_operations, | ||
logs=logs | ||
) | ||
session.add(record_to_upsert) | ||
print("Analytics record inserted for chatbot_id:", chatbot_id) | ||
|
||
session.commit() | ||
session.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from sqlalchemy import Column, Integer, String, ForeignKey | ||
from .get_declarative_base import Base | ||
from .database_setup import engine | ||
from .chatbot import Chatbot | ||
class Analytics(Base): | ||
__tablename__ = 'analytics' | ||
|
||
chatbot_id = Column(String(36), primary_key=True) | ||
chatbot_id_fk = Column(String(36), ForeignKey(Chatbot.id), nullable=True) | ||
successful_operations = Column(Integer) | ||
total_operations = Column(Integer) | ||
logs= Column(String(4096), nullable=True) | ||
|
||
def __init__(self, chatbot_id, successful_operations, total_operations, logs=None): | ||
self.chatbot_id = chatbot_id | ||
self.successful_operations = successful_operations | ||
self.total_operations = total_operations | ||
self.logs = logs | ||
|
||
Base.metadata.create_all(engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters