Skip to content

Commit

Permalink
Improve GitHub automatic updates
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Aug 4, 2024
1 parent 69e9cee commit 3229ab1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/reset_database.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
2 changes: 1 addition & 1 deletion .github/workflows/update_recommendations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

jobs:
update:
name: Update recommendations
name: update recommendations
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down
10 changes: 9 additions & 1 deletion create_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -95,4 +102,5 @@ def main():


if __name__ == "__main__":
load_dotenv(".streamlit/secrets.toml")
main()
23 changes: 23 additions & 0 deletions mangoleaf/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions update_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit 3229ab1

Please sign in to comment.