Skip to content

Commit

Permalink
Added tests and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
abraryaser02 committed May 10, 2024
1 parent 97d8e00 commit d92e9cf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 58 deletions.
68 changes: 37 additions & 31 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on: [push, pull_request]

jobs:
test:

runs-on: ubuntu-latest

services:
Expand All @@ -18,33 +17,40 @@ jobs:
- 5432:5432

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.12'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r services/backend/requirements.txt
- name: docker
run: |
docker-compose -f docker-compose-dev.yml up --build -d
sleep 20
- name: Run tests
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/backend_dev
DATABASE_TEST_URL: postgres://postgres:postgres@localhost:5432/backend_test
APP_SETTINGS: project.config.TestingConfig
PYTHONPATH: services/backend
run: |
cd services/backend
export APP_SETTINGS=project.config.TestingConfig
export PYTHONPATH=$(pwd)
export DATABASE_TEST_URL=postgresql://postgres:postgres@localhost:5435/backend_test
export DATABASE_URL=postgresql://postgres:postgres@localhost:5435/backend_dev
pytest
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.12'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r services/backend/requirements.txt
- name: Wait for Postgres
run: |
until pg_isready -h localhost -p 5432 -U postgres; do
echo "Waiting for postgres container..."
sleep 20
done
- name: Set up Docker
run: |
docker-compose -f docker-compose-dev.yml up --build -d
sleep 20
- name: Run tests
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/backend_dev
DATABASE_TEST_URL: postgres://postgres:postgres@localhost:5432/backend_test
APP_SETTINGS: project.config.TestingConfig
PYTHONPATH: services/backend
run: |
cd services/backend
export APP_SETTINGS=project.config.TestingConfig
export PYTHONPATH=$(pwd)
export DATABASE_TEST_URL=postgresql://postgres:postgres@localhost:5432/backend_test
export DATABASE_URL=postgresql://postgres:postgres@localhost:5432/backend_dev
pytest
27 changes: 11 additions & 16 deletions services/backend/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ def delete_event(event_id):
@app.route('/events_by_user/<int:user_id>', methods=['GET'])
def events_by_user(user_id):
query = """
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords
FROM events e
JOIN user_to_events ue ON ue.event_id = e.id_events
WHERE ue.user_id = :user_id
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords
FROM events e
JOIN user_to_events ue ON ue.event_id = e.id_events
WHERE ue.user_id = :user_id
"""
with engine.connect() as connection:
events = connection.execute(text(query), {'user_id': user_id}).fetchall()
Expand All @@ -226,14 +226,9 @@ def get_top_favorited_events():
try:
# SQL query to select the top 10 most favorited events
query = """
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords,
COUNT(ue.event_id) AS likes
FROM events e
LEFT JOIN user_to_events ue ON e.id_events = ue.event_id
GROUP BY e.id_events
SELECT * FROM mv_events_with_likes
ORDER BY likes DESC
LIMIT 10
LIMIT 10;
"""
with engine.connect() as connection:
# Execute the SQL query
Expand Down Expand Up @@ -269,6 +264,10 @@ def toggle_user_event():

if not user_id or not event_id:
return jsonify({'message': 'Missing user_id or event_id'}), 400

with engine.connect() as connection:
connection.execute(text("REFRESH MATERIALIZED VIEW mv_events_with_likes;"))
connection.commit()

with engine.connect() as connection:
# Check if both user and event exist
Expand Down Expand Up @@ -298,10 +297,6 @@ def toggle_user_event():
connection.commit()
return jsonify({'message': 'User added to event successfully', 'isFavorited': True}), 201

# @app.route(get favorites by event)
# view friends on about page

# most recent messages view, prev next toggle buttons

@app.route('/search', methods=['GET'])
def search():
Expand Down
44 changes: 33 additions & 11 deletions services/postgres/schema.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
SET max_parallel_maintenance_workers = 60;
SET max_parallel_workers = 60;
SET maintenance_work_mem TO '6GB';
SET max_wal_size = '4GB';
SET wal_buffers = '32MB';

-- Ensure the required extensions are installed
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS RUM;

-- Drop tables if they already exist
DROP TABLE IF EXISTS user_to_events;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS users;
DROP TABLE IF NOT EXISTS events;
DROP TABLE IF NOT EXISTS users;
DROP TABLE IF EXISTS fts_word;


-- Create the users table
CREATE TABLE users (
id_users BIGSERIAL PRIMARY KEY,
Expand Down Expand Up @@ -51,17 +52,38 @@ CREATE TABLE fts_word (
COPY fts_word(word) FROM '/docker-entrypoint-initdb.d/words_alpha.txt' WITH (FORMAT text);

-- Indexes for faster search
-- Create a composite index for login queries
CREATE INDEX idx_login ON users (email, password);
CREATE INDEX idx_users_email ON users (id_users, email);
CREATE INDEX idx_events_start_time ON events USING btree (start_time);
CREATE INDEX idx_events_name ON events USING rum (name);
CREATE INDEX idx_events_description ON events USING rum (description);

-- Index on email for user existence checks
CREATE INDEX idx_users_email ON users (email);

-- Index for events based on start time for ordering and filtering
CREATE INDEX idx_upcoming_events ON events(start_time) WHERE start_time > NOW();

-- Index for events based on the event ID
CREATE INDEX idx_events_id_events ON events (id_events);

--Indexes on user_to_events for join operations
CREATE INDEX idx_user_to_events_user_event ON user_to_events(user_id, event_id);

CREATE MATERIALIZED VIEW mv_events_with_likes AS
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords,
COUNT(ue.event_id) AS likes
FROM events e
LEFT JOIN user_to_events ue ON e.id_events = ue.event_id
GROUP BY e.id_events;

CREATE INDEX idx_mv_events_with_likes ON mv_events_with_likes(likes DESC);

-- Full-text search index
CREATE INDEX idx_events_fti ON events USING rum (tsv);
CREATE INDEX idx_fts_word ON fts_word USING rum (word);
CREATE INDEX idx_events_fti ON events USING rum (tsv rum_tsvector_ops);

-- Trigger to update tsvector
-- Index for fts_word for spelling suggestions
CREATE INDEX idx_fts_word_rum ON fts_word USING rum (word rum_trgm_ops);

-- Create the function and trigger to update tsvector
CREATE OR REPLACE FUNCTION update_tsvector() RETURNS trigger AS $$
BEGIN
NEW.tsv := to_tsvector('english', NEW.name || ' ' || NEW.description);
Expand All @@ -73,6 +95,7 @@ CREATE TRIGGER trigger_update_tsvector
BEFORE INSERT OR UPDATE ON events
FOR EACH ROW EXECUTE FUNCTION update_tsvector();

-- Function for spelling suggestions
CREATE OR REPLACE FUNCTION get_spelling_suggestions(query TEXT)
RETURNS TABLE(suggestion TEXT) AS $$
BEGIN
Expand All @@ -88,5 +111,4 @@ BEGIN
END;
$$ LANGUAGE plpgsql;


COMMIT;

0 comments on commit d92e9cf

Please sign in to comment.