diff --git a/.github/workflows/reset_database.yml b/.github/workflows/reset_database.yml new file mode 100644 index 0000000..9d9595e --- /dev/null +++ b/.github/workflows/reset_database.yml @@ -0,0 +1,32 @@ +name: reset + +on: + workflow_dispatch: + +jobs: + update: + name: reset database + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + cache: 'pip' + cache-dependency-path: requirements.txt + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Reset database and set inital recommendations + run: | + python create_schema.py + python update_database.py + env: + POSTGRES_USER: "${{ secrets.POSTGRES_USER }}" + POSTGRES_PASSWORD: "${{ secrets.POSTGRES_PASSWORD }}" + POSTGRES_HOST: "${{ secrets.POSTGRES_HOST }}" + POSTGRES_DB: "${{ secrets.POSTGRES_DB }}" diff --git a/.github/workflows/update_recommendations.yml b/.github/workflows/update_recommendations.yml index e882731..a09dc9a 100644 --- a/.github/workflows/update_recommendations.yml +++ b/.github/workflows/update_recommendations.yml @@ -5,7 +5,7 @@ on: jobs: update: - name: Update recommendations + name: update recommendations runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/create_schema.py b/create_schema.py index ae4b83d..ee24684 100644 --- a/create_schema.py +++ b/create_schema.py @@ -9,6 +9,7 @@ import bcrypt import pandas as pd +from dotenv import load_dotenv from sqlalchemy.sql import text from mangoleaf.connection import Connection @@ -74,7 +75,13 @@ def main(): dummy_password = bcrypt.hashpw(b"booksandmanga", bcrypt.gensalt()).decode("utf-8") passwords = [dummy_password] * len(user_id) df = pd.DataFrame( - dict(user_id=user_id, username=usernames, password=passwords, full_name=full_names) + dict( + user_id=user_id, + username=usernames, + password=passwords, + full_name=full_names, + registered="2024-07-23", + ) ) df.to_sql("users", db_engine, if_exists="append", index=False) @@ -95,4 +102,5 @@ def main(): if __name__ == "__main__": + load_dotenv(".streamlit/secrets.toml") main() diff --git a/mangoleaf/query.py b/mangoleaf/query.py index b97b03b..b9888d2 100644 --- a/mangoleaf/query.py +++ b/mangoleaf/query.py @@ -275,6 +275,29 @@ def next_user_id(): return int(user_id) + 1 if user_id is not None else 0 +def list_users_since(date): + """ + List all active users in the database + + Parameters + ---------- + date : str + Date to filter users from + + Returns + ------- + user_ids : pd.DataFrame + DataFrame with all active users + """ + query = """ + SELECT user_id FROM users + WHERE registered >= %(date)s + ORDER BY registered ASC + """ + user_ids = pd.read_sql(query, Connection().get(), params=dict(date=date)).user_id.to_list() + return user_ids + + def register_user(username, password): """ Register a new user in the database diff --git a/update_database.py b/update_database.py index 935e052..9583cdb 100644 --- a/update_database.py +++ b/update_database.py @@ -2,7 +2,9 @@ Update the dynamic data with the latest recommendations """ -from mangoleaf import Connection, recommend +from dotenv import load_dotenv + +from mangoleaf import Connection, query, recommend def update_database(users, n=40, count_threshold=50): @@ -23,15 +25,20 @@ def update_database(users, n=40, count_threshold=50): if __name__ == "__main__": + load_dotenv(".streamlit/secrets.toml") + # Selected users for user-based recommendations users = [ - # Mangas + # Manga example users 1002, 357, 2507, - # Books + # Book example users 114368, 95359, 104636, ] + new_users = query.list_users_since("2024-08-01") + users += new_users + update_database(users, 40, 50)