From 659916694861de4082e9be1a3c6d0b508b57771f Mon Sep 17 00:00:00 2001 From: Aleksandr Kariakin Date: Fri, 12 Apr 2024 14:36:44 +0800 Subject: [PATCH] Alembic --- README.md | 54 ++++++++----------- alembic.ini | 4 +- alembic/README | 1 - alembic/README.md | 8 +++ alembic/env.py | 14 ++++- ..._12_1354-713caec2649e_initial_migration.py | 30 +++++++++++ ...2_1357-4ae5c037da0f_add_page_data_table.py | 43 +++++++++++++++ ...f61fa_add_bookmarked_conversation_table.py | 38 +++++++++++++ ...8-ce3fb8b3d191_add_qa_interaction_table.py | 44 +++++++++++++++ ...59-0278f7c21985_add_quiz_question_table.py | 38 +++++++++++++ ..._1359-dccb3577feb5_add_space_info_table.py | 36 +++++++++++++ ..._1400-59b9eced0668_add_user_score_table.py | 38 +++++++++++++ ..._12_1404-209d7088ea81_add_index_columns.py | 32 +++++++++++ compose.yml | 40 -------------- content/database/.gitignore | 2 - database/bookmarked_conversation_manager.py | 24 ++++----- database/database.py | 10 ---- docker-compose.yml | 2 +- interactions/vectorize_and_store.py | 11 ---- models/bookmarked_conversation.py | 5 +- models/page_data.py | 8 +-- models/qa_interaction.py | 10 ++-- models/quiz_question.py | 5 +- models/space_info.py | 2 - models/user_score.py | 2 - slack/event_consumer.py | 5 +- slack/reaction_manager.py | 10 ++-- 27 files changed, 377 insertions(+), 139 deletions(-) delete mode 100644 alembic/README create mode 100644 alembic/README.md create mode 100644 alembic/versions/2024_04_12_1354-713caec2649e_initial_migration.py create mode 100644 alembic/versions/2024_04_12_1357-4ae5c037da0f_add_page_data_table.py create mode 100644 alembic/versions/2024_04_12_1358-c3363bdf61fa_add_bookmarked_conversation_table.py create mode 100644 alembic/versions/2024_04_12_1358-ce3fb8b3d191_add_qa_interaction_table.py create mode 100644 alembic/versions/2024_04_12_1359-0278f7c21985_add_quiz_question_table.py create mode 100644 alembic/versions/2024_04_12_1359-dccb3577feb5_add_space_info_table.py create mode 100644 alembic/versions/2024_04_12_1400-59b9eced0668_add_user_score_table.py create mode 100644 alembic/versions/2024_04_12_1404-209d7088ea81_add_index_columns.py delete mode 100644 compose.yml delete mode 100644 content/database/.gitignore diff --git a/README.md b/README.md index 66b1c6ea..8d6b636c 100644 --- a/README.md +++ b/README.md @@ -118,61 +118,51 @@ The version that `asdf` will use for the current project is declared in `.tool-v 2. Follow the instructions in the files to fill in the values -### Run Nur via shell +### Run Top-Assist ```` -git clone https://github.com/MDGrey33/Nur.git -cd Nur +git clone https://github.com/toptal/top-assist +cd top-assist poetry install -poetry run python main.py +PYTHONPATH=. poetry run python main.py ```` -`poetry run python ./main.py` (for the menu operations) +`PYTHONPATH=. poetry run python main.py` (for the menu operations) -`poetry run python ./api/endpoint.py` (API / uvicorn web server) +`PYTHONPATH=. poetry run python api/endpoint.py` (API / uvicorn web server) Refer to the ./documentation/slack_app_manifest_installation_guide.md for slack bot setup -`poetry run python ./slack/bot.py` (slack bot stream listener) +`PYTHONPATH=. poetry run python slack/bot.py` (slack bot stream listener) -### Run Nur via Docker +### Run DBs -We will use docker compose to build 3 containers of Nur. Each one is used to run a separate part of the app: -* `nur-manager` will create a container allowing us to use `main.py` functionality. It is meant to be interactive (read further for details). -* `nur-web` starts the web application. -* `nur-slack` starts the slack integration script. +Top Assist uses two databases: `postgresql` and `chroma`. +Please be sure that you set all the required environment variables in the `.env` file in `# Database` section. +To run the databases, you can use docker-compose -First off, run the composer script: ``` -git clone https://github.com/MDGrey33/Nur.git -cd Nur docker composer up +poetry run alembic upgrade head # to setup PostgreSQL database ``` -Upon successful completion, you have three containers running, all of them sharing a common volume mounted at `/content` within the containers. -`nur-web` and `nur-slack` should be fully operational already. If you wish to run commands in Nur's cli interface, you will need to attach a shell to the `nur-manager` container in interactive mode, and enter the management environment: +### DB Migrations +1) Import the model in `alembic/env.py` and run: ``` -./bin/manage +poetry run alembic revision --autogenerate -m "Add my new model" ``` +Alembic will detect the changes in the models/tables and create a new migration file. +Be aware that it can remove empty indexes/fields. +If you need to have control over the migration you can remove the `--autogenerate` flag and write the migration manually. -If the above works, you should see Nur's manager command prompt - the same thing you should see when running `./main.py` locally. -Bear in mind that the Dockerized version uses a shared volume named `nur_shared_content`. This is bootstrapped during the first installation, but will persist until it is manually removed. - -## Add a new data model using Alembic - - -```warp-runnable-command -alembic revision -m "create account table" -# Import the model in /nurai/migrations/env.py -# Create a new migration -poetry run alembic revision --autogenerate -m "Initialize the db" -# Apply the migration -poetry run alembic upgrade head -``` +2) Apply the migration: +`poetry run alembic upgrade head` +3) To rollback last N migrations run: +`poetry run alembic downgrade -N` ## Network traffic diff --git a/alembic.ini b/alembic.ini index 3a229aa4..7f8be8d9 100644 --- a/alembic.ini +++ b/alembic.ini @@ -60,8 +60,8 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne # are written from script.py.mako # output_encoding = utf-8 -# Custom: Add DB URL -sqlalchemy.url = configuration.DB_URL +# Custom: Replaced in alembic/env.py +sqlalchemy.url = driver://user:pass@localhost/dbname [post_write_hooks] diff --git a/alembic/README b/alembic/README deleted file mode 100644 index 98e4f9c4..00000000 --- a/alembic/README +++ /dev/null @@ -1 +0,0 @@ -Generic single-database configuration. \ No newline at end of file diff --git a/alembic/README.md b/alembic/README.md new file mode 100644 index 00000000..93645b1c --- /dev/null +++ b/alembic/README.md @@ -0,0 +1,8 @@ +### Alembic documentation +https://alembic.sqlalchemy.org/en/latest/ + +### Cookbook +https://alembic.sqlalchemy.org/en/latest/cookbook.html#rudimental-schema-level-multi-tenancy-for-postgresql-databases + +Notes: +- we set `DB_URL` in `.env` file and rewrite it in `alembic/env.py` file (`alembic.ini` file is not used of security reasons) diff --git a/alembic/env.py b/alembic/env.py index e1e144cf..b27b58cd 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -2,6 +2,16 @@ from sqlalchemy import engine_from_config from sqlalchemy import pool +from configuration import DB_URL +from database.database import Base + +# Import all models to ensure they are registered with the metadata +from models.page_data import PageData +from models.bookmarked_conversation import BookmarkedConversation +from models.qa_interaction import QAInteraction +from models.quiz_question import QuizQuestion +from models.space_info import SpaceInfo +from models.user_score import UserScore from alembic import context @@ -9,6 +19,9 @@ # access to the values within the .ini file in use. config = context.config +# Custom: Change DB URL to the one in the configuration +config.set_main_option("sqlalchemy.url", DB_URL) + # Interpret the config file for Python logging. # This line sets up loggers basically. if config.config_file_name is not None: @@ -16,7 +29,6 @@ # add your model's MetaData object here # for 'autogenerate' support -from database.database import Base target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, diff --git a/alembic/versions/2024_04_12_1354-713caec2649e_initial_migration.py b/alembic/versions/2024_04_12_1354-713caec2649e_initial_migration.py new file mode 100644 index 00000000..1f5a4fd2 --- /dev/null +++ b/alembic/versions/2024_04_12_1354-713caec2649e_initial_migration.py @@ -0,0 +1,30 @@ +"""Initial migration + +Revision ID: 713caec2649e +Revises: +Create Date: 2024-04-12 13:54:12.213839 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '713caec2649e' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1357-4ae5c037da0f_add_page_data_table.py b/alembic/versions/2024_04_12_1357-4ae5c037da0f_add_page_data_table.py new file mode 100644 index 00000000..64939de9 --- /dev/null +++ b/alembic/versions/2024_04_12_1357-4ae5c037da0f_add_page_data_table.py @@ -0,0 +1,43 @@ +"""Add page_data table + +Revision ID: 4ae5c037da0f +Revises: 713caec2649e +Create Date: 2024-04-12 13:57:09.418241 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '4ae5c037da0f' +down_revision: Union[str, None] = '713caec2649e' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('page_data', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('page_id', sa.String(), nullable=True), + sa.Column('space_key', sa.String(), nullable=True), + sa.Column('title', sa.String(), nullable=True), + sa.Column('author', sa.String(), nullable=True), + sa.Column('createdDate', sa.DateTime(), nullable=True), + sa.Column('lastUpdated', sa.DateTime(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('comments', sa.Text(), nullable=True), + sa.Column('last_embedded', sa.DateTime(), nullable=True), + sa.Column('embed', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('page_data') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1358-c3363bdf61fa_add_bookmarked_conversation_table.py b/alembic/versions/2024_04_12_1358-c3363bdf61fa_add_bookmarked_conversation_table.py new file mode 100644 index 00000000..0e4f162c --- /dev/null +++ b/alembic/versions/2024_04_12_1358-c3363bdf61fa_add_bookmarked_conversation_table.py @@ -0,0 +1,38 @@ +"""Add bookmarked_conversation table + +Revision ID: c3363bdf61fa +Revises: 4ae5c037da0f +Create Date: 2024-04-12 13:58:12.798686 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'c3363bdf61fa' +down_revision: Union[str, None] = '4ae5c037da0f' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('bookmarked_conversations', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('title', sa.Text(), nullable=True), + sa.Column('body', sa.Text(), nullable=True), + sa.Column('thread_id', sa.String(), nullable=True), + sa.Column('bookmarked_on_slack', sa.DateTime(), nullable=True), + sa.Column('posted_on_confluence', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('bookmarked_conversations') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1358-ce3fb8b3d191_add_qa_interaction_table.py b/alembic/versions/2024_04_12_1358-ce3fb8b3d191_add_qa_interaction_table.py new file mode 100644 index 00000000..38ff7884 --- /dev/null +++ b/alembic/versions/2024_04_12_1358-ce3fb8b3d191_add_qa_interaction_table.py @@ -0,0 +1,44 @@ +"""Add qa_interaction table + +Revision ID: ce3fb8b3d191 +Revises: c3363bdf61fa +Create Date: 2024-04-12 13:58:46.916722 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'ce3fb8b3d191' +down_revision: Union[str, None] = 'c3363bdf61fa' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('qa_interactions', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('question_text', sa.Text(), nullable=True), + sa.Column('thread_id', sa.String(), nullable=True), + sa.Column('assistant_thread_id', sa.String(), nullable=True), + sa.Column('answer_text', sa.Text(), nullable=True), + sa.Column('channel_id', sa.String(), nullable=True), + sa.Column('slack_user_id', sa.String(), nullable=True), + sa.Column('question_timestamp', sa.DateTime(), nullable=True), + sa.Column('answer_timestamp', sa.DateTime(), nullable=True), + sa.Column('comments', sa.Text(), nullable=True), + sa.Column('last_embedded', sa.DateTime(), nullable=True), + sa.Column('embed', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('qa_interactions') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1359-0278f7c21985_add_quiz_question_table.py b/alembic/versions/2024_04_12_1359-0278f7c21985_add_quiz_question_table.py new file mode 100644 index 00000000..1c612207 --- /dev/null +++ b/alembic/versions/2024_04_12_1359-0278f7c21985_add_quiz_question_table.py @@ -0,0 +1,38 @@ +"""Add quiz_question table + +Revision ID: 0278f7c21985 +Revises: ce3fb8b3d191 +Create Date: 2024-04-12 13:59:10.846548 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '0278f7c21985' +down_revision: Union[str, None] = 'ce3fb8b3d191' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('quiz_questions', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('question_text', sa.Text(), nullable=True), + sa.Column('thread_id', sa.String(), nullable=True), + sa.Column('summary', sa.Text(), nullable=True), + sa.Column('posted_on_slack', sa.DateTime(), nullable=True), + sa.Column('posted_on_confluence', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('quiz_questions') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1359-dccb3577feb5_add_space_info_table.py b/alembic/versions/2024_04_12_1359-dccb3577feb5_add_space_info_table.py new file mode 100644 index 00000000..04aa9ae7 --- /dev/null +++ b/alembic/versions/2024_04_12_1359-dccb3577feb5_add_space_info_table.py @@ -0,0 +1,36 @@ +"""Add space_info table + +Revision ID: dccb3577feb5 +Revises: 0278f7c21985 +Create Date: 2024-04-12 13:59:35.003367 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'dccb3577feb5' +down_revision: Union[str, None] = '0278f7c21985' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('space_info', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('space_key', sa.String(), nullable=False), + sa.Column('space_name', sa.String(), nullable=False), + sa.Column('last_import_date', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('space_info') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1400-59b9eced0668_add_user_score_table.py b/alembic/versions/2024_04_12_1400-59b9eced0668_add_user_score_table.py new file mode 100644 index 00000000..a760237c --- /dev/null +++ b/alembic/versions/2024_04_12_1400-59b9eced0668_add_user_score_table.py @@ -0,0 +1,38 @@ +"""Add user_score table + +Revision ID: 59b9eced0668 +Revises: dccb3577feb5 +Create Date: 2024-04-12 14:00:04.186969 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '59b9eced0668' +down_revision: Union[str, None] = 'dccb3577feb5' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('user_scores', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('slack_user_id', sa.String(), nullable=False), + sa.Column('seeker_score', sa.Integer(), nullable=True), + sa.Column('revealer_score', sa.Integer(), nullable=True), + sa.Column('luminary_score', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('slack_user_id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('user_scores') + # ### end Alembic commands ### diff --git a/alembic/versions/2024_04_12_1404-209d7088ea81_add_index_columns.py b/alembic/versions/2024_04_12_1404-209d7088ea81_add_index_columns.py new file mode 100644 index 00000000..6ac7213b --- /dev/null +++ b/alembic/versions/2024_04_12_1404-209d7088ea81_add_index_columns.py @@ -0,0 +1,32 @@ +"""Add index columns + +Revision ID: 209d7088ea81 +Revises: 59b9eced0668 +Create Date: 2024-04-12 14:04:01.691616 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '209d7088ea81' +down_revision: Union[str, None] = '59b9eced0668' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.create_index('ix_page_data_page_id', 'page_data', ['page_id'], unique=True) + op.create_index('ix_bookmarked_conversations_thread_id', 'bookmarked_conversations', ['thread_id'], unique=False) + op.create_index('ix_qa_interactions_thread_id', 'qa_interactions', ['thread_id'], unique=False) + op.create_index('ix_quiz_questions_thread_id', 'quiz_questions', ['thread_id'], unique=False) + + +def downgrade() -> None: + op.drop_index('ix_page_data_page_id', 'page_data') + op.drop_index('ix_bookmarked_conversations_thread_id', 'bookmarked_conversations') + op.drop_index('ix_qa_interactions_thread_id', 'qa_interactions') + op.drop_index('ix_quiz_questions_thread_id', 'quiz_questions') diff --git a/compose.yml b/compose.yml deleted file mode 100644 index 5306447d..00000000 --- a/compose.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: nur - -services: - web: - build: - context: . - dockerfile: Dockerfile - command: - - poetry - - run - - python - - api/endpoint.py - ports: - - "8000:8000" - volumes: - - shared_content:/app/content - environment: - - NUR_API_HOST=web - - NUR_API_PORT=8000 - - slack: - depends_on: - - web - build: - context: . - dockerfile: Dockerfile - command: - - poetry - - run - - python - - slack/bot.py - volumes: - - shared_content:/app/content - environment: - - NUR_API_HOST=web - - NUR_API_PORT=8000 - -volumes: - shared_content: - \ No newline at end of file diff --git a/content/database/.gitignore b/content/database/.gitignore deleted file mode 100644 index c96a04f0..00000000 --- a/content/database/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file diff --git a/database/bookmarked_conversation_manager.py b/database/bookmarked_conversation_manager.py index be603dc1..6f9613a5 100644 --- a/database/bookmarked_conversation_manager.py +++ b/database/bookmarked_conversation_manager.py @@ -2,21 +2,15 @@ from datetime import datetime, timezone -class BookmarkedConversationManager: - def __init__(self, session): - self.session = session +def add_bookmarked_conversation(session, title, body, thread_id): + new_conversation = BookmarkedConversation(title=title, body=body, thread_id=thread_id) + session.add(new_conversation) + session.commit() - def add_bookmarked_conversation(self, title, body, thread_id): - BookmarkedConversation().create_or_update(self.session, - title=title, - body=body, - thread_id=thread_id) - def update_posted_on_confluence(self, thread_id): - BookmarkedConversation().create_or_update(self.session, - thread_id=thread_id, - posted_on_confluence=datetime.now(timezone.utc)) +def update_posted_on_confluence(session, thread_id): + conversation = session.query(BookmarkedConversation).filter_by(thread_id=thread_id).first() + if conversation: + conversation.posted_on_confluence = datetime.now(timezone.utc) + session.commit() print(f"Updated conversation with thread ID {thread_id} with timestamp") - - def get_unposted_conversations(self): - return self.session.query(BookmarkedConversation).filter_by(posted_on_confluence=None).all() diff --git a/database/database.py b/database/database.py index eba43456..10650d97 100644 --- a/database/database.py +++ b/database/database.py @@ -2,21 +2,11 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from configuration import DB_URL -# from models.bookmarked_conversation import BookmarkedConversation -# from models.page_data import PageData -# from models.qa_interaction import QAInteraction -# from models.quiz_question import QuizQuestion -# from models.space_info import SpaceInfo -# from models.user_score import UserScore from contextlib import contextmanager engine = create_engine(DB_URL) SessionLocal = sessionmaker(autocommit=False, autoflush=True, bind=engine) -# TODO: Extract and uncomment -# for model in [QAInteraction, SpaceInfo, PageData, BookmarkedConversation, QuizQuestion, UserScore]: -# model.metadata.create_all(engine) -from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() @contextmanager diff --git a/docker-compose.yml b/docker-compose.yml index dc42ce85..07244986 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: volumes: - chroma-data:/chroma/.chroma/index ports: - - 8000:8000 + - "8000:8000" networks: - net diff --git a/interactions/vectorize_and_store.py b/interactions/vectorize_and_store.py index 2ef7b91d..036c851e 100644 --- a/interactions/vectorize_and_store.py +++ b/interactions/vectorize_and_store.py @@ -10,17 +10,6 @@ from configuration import interaction_embeds_endpoint import requests -# TODO: Remove -# def get_qna_interactions_from_database(db_session): -# """ -# Fetch all Q&A interactions from the database. -# -# Returns: -# list: A list of QAInteraction objects. -# """ -# return QAInteractionManager(db_session).get_qa_interactions() - - def get_qna_interactions_without_embeds(db_session): """ diff --git a/models/bookmarked_conversation.py b/models/bookmarked_conversation.py index 37d452d1..9ae529a8 100644 --- a/models/bookmarked_conversation.py +++ b/models/bookmarked_conversation.py @@ -1,11 +1,12 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from datetime import datetime, timezone from sqlalchemy import Column, Integer, String, Text, DateTime class BookmarkedConversation(Base): + """ + SQLAlchemy model for storing bookmarked conversations. + """ __tablename__ = 'bookmarked_conversations' id = Column(Integer, primary_key=True) diff --git a/models/page_data.py b/models/page_data.py index 8b2412fd..ee601cbe 100644 --- a/models/page_data.py +++ b/models/page_data.py @@ -1,9 +1,5 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from sqlalchemy import Column, Integer, String, Text, DateTime -import json # TODO Can we switch to sqlalchemy JSON type? - class PageData(Base): @@ -20,7 +16,7 @@ class PageData(Base): createdDate = Column(DateTime) lastUpdated = Column(DateTime) content = Column(Text) - comments = Column(Text, default=json.dumps([])) + comments = Column(Text) last_embedded = Column(DateTime) - embed = Column(Text, default=json.dumps([])) + embed = Column(Text) diff --git a/models/qa_interaction.py b/models/qa_interaction.py index 977c324d..884e7582 100644 --- a/models/qa_interaction.py +++ b/models/qa_interaction.py @@ -1,11 +1,11 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from sqlalchemy import Column, Integer, String, Text, DateTime -import json # TODO Can we switch to sqlalchemy JSON type? class QAInteraction(Base): + """ + SQLAlchemy model for storing QA interactions. + """ __tablename__ = 'qa_interactions' id = Column(Integer, primary_key=True) @@ -17,6 +17,6 @@ class QAInteraction(Base): slack_user_id = Column(String) question_timestamp = Column(DateTime) answer_timestamp = Column(DateTime) - comments = Column(Text, default=json.dumps([])) + comments = Column(Text) last_embedded = Column(DateTime) - embed = Column(Text, default=json.dumps([])) + embed = Column(Text) diff --git a/models/quiz_question.py b/models/quiz_question.py index c44f8519..2e8f0e3a 100644 --- a/models/quiz_question.py +++ b/models/quiz_question.py @@ -1,10 +1,11 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from sqlalchemy import Column, Integer, String, Text, DateTime class QuizQuestion(Base): + """ + SQLAlchemy model for storing quiz questions. + """ __tablename__ = 'quiz_questions' id = Column(Integer, primary_key=True) diff --git a/models/space_info.py b/models/space_info.py index ee41701a..3e6f9fe2 100644 --- a/models/space_info.py +++ b/models/space_info.py @@ -1,6 +1,4 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from sqlalchemy import Column, Integer, String, DateTime diff --git a/models/user_score.py b/models/user_score.py index e39cef91..03104be3 100644 --- a/models/user_score.py +++ b/models/user_score.py @@ -1,6 +1,4 @@ from database.database import Base -from sqlalchemy.ext.declarative import declarative_base -# Base = declarative_base() from sqlalchemy import Column, Integer, String diff --git a/slack/event_consumer.py b/slack/event_consumer.py index 5481a062..4b28cb9b 100644 --- a/slack/event_consumer.py +++ b/slack/event_consumer.py @@ -77,7 +77,9 @@ def process_question(self, question_event: QuestionEvent): # TODO: Refactor thi if response_text: print(f"Response from assistant: {response_text}\n") try: - self.add_question_and_response_to_database(question_event, response_text, assistant_thread_id=assistant_thread_id) + self.add_question_and_response_to_database(question_event, + response_text, + assistant_thread_id=assistant_thread_id) try: ScoreManager(self.session).add_or_update_score(slack_user_id=question_event.user, category='seeker') print(f"Score updated for user {question_event.user}") @@ -136,6 +138,7 @@ def process_question(question_event: QuestionEvent): with get_db_session() as session: EventConsumer(session).process_question(question_event) + # TODO: Move session call to method level def process_feedback(feedback_event: FeedbackEvent): """Directly processes a feedback event without using the queue.""" diff --git a/slack/reaction_manager.py b/slack/reaction_manager.py index 6d251612..e3d767c5 100644 --- a/slack/reaction_manager.py +++ b/slack/reaction_manager.py @@ -6,7 +6,7 @@ from database.score_manager import ScoreManager from slack.event_consumer import get_user_name_from_id from slack_sdk.errors import SlackApiError -from database.bookmarked_conversation_manager import BookmarkedConversationManager +from database.bookmarked_conversation_manager import add_bookmarked_conversation, update_posted_on_confluence from slack.message_manager import get_message_replies from database.database import get_db_session @@ -132,11 +132,13 @@ def process_bookmark_added_event(slack_web_client, event): body = "\n".join([message.get("text", "") for message in bookmarked_conversation_messages[1:]]) with get_db_session() as session: - bookmarked_conversation_manager = BookmarkedConversationManager(session) - bookmarked_conversation_manager.add_bookmarked_conversation(title=title, body=body, thread_id=item_ts) + add_bookmarked_conversation(session, + title=title, + body=body, + thread_id=item_ts) print(f"Bookmarked conversation added to the database: {title}") create_page_on_confluence(title, body) - bookmarked_conversation_manager.update_posted_on_confluence(item_ts) + update_posted_on_confluence(session, item_ts) else: print("No messages found for the bookmarked conversation.")