Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abraryaser02 committed May 12, 2024
1 parent b78d451 commit 1c81fb6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 143 deletions.
23 changes: 21 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

services:
postgres:
image: postgres:13
image: custom-postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -19,6 +19,25 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build custom PostgreSQL image
run: |
docker build -t custom-postgres:latest -f services/postgres/Dockerfile .
- name: Build backend image
run: |
docker build -t backend-dev:latest -f services/backend/Dockerfile-dev .
- name: Set up Python
uses: actions/setup-python@v2
with:
Expand All @@ -36,7 +55,7 @@ jobs:
sleep 20
done
- name: Set up Docker
- name: Set up Docker Compose
run: |
docker-compose -f docker-compose-dev.yml up --build -d
sleep 20
Expand Down
177 changes: 36 additions & 141 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,162 +1,57 @@
# Welcome to p-5cevents!

## Endpoints
# Events Final Project

#### Generate a test event (refresh page to create multiple random events)
This project is a scalable web application for managing and displaying events globally. It is built with a React frontend and a Python backend, and uses PostgreSQL for data storage. Docker is used to containerize the application for easy deployment and scalability.

`http://localhost:5001/test_event`
## Project Structure

#### Get all events
- **services/backend**: Contains the backend API built with Python.
- **services/client/my-app**: Contains the React frontend application.
- **postgres**: Contains the Dockerfile and SQL schema for the PostgreSQL database.

`http://localhost:5001/all_events`
## Getting Started

#### Get event by id (ex. 1)
### Prerequisites

`http://localhost:5001/get_event/id`
- Docker
- Docker Compose

#### Get event attribute (ex. name)
### Running the Application

`http://localhost:5001/get_event/id/name`
1. **Clone the repository:**
```sh
git clone <repository-url>
cd Events_final_project_2
```

Attributes
2. **Build and run the containers:**
```sh
docker-compose -f docker-compose-dev.yml up --build
```

name, description, location, time, organization
3. **Access the application:**
- The frontend will be available at `http://localhost:3000`
- The backend API will be available at `http://localhost:5001`

#### Delete an event
### Stopping the Application

`http://localhost:5001/delete_event/id`
`
To stop the running containers, use:

## Overview

p-5cevents is a full-stack web app with a Flask backend running on a Postgres database with React on the front-end. We will be dockerizing our app as we build it and have it production-ready using Gunicorn, Nginx, and DigitalOcean.

## Process

Currently, the app is at its basic stages. We have a small flask API endpoint set at `http://localhost:5001/users/ping` which displays
```
{
"message": "pong!",
"status": "success"
}
```
on your webpage!

To run docker and see this state-of-the-art website in use, follow this step:

```
docker-compose -f docker-compose-dev.yml up -d --build
```sh
docker-compose -f docker-compose-dev.yml down
```
The beauty of Docker is that you do not need to download any dependencies on your local machine for the website to pop up on your browser. You can run this command to bring up the Docker container and build the images (which in our case is users-db and users) and everything should work!

To recreate the database you can run the following command:
## Database Indexing and Scalability

```
docker-compose -f docker-compose-dev.yml \
run backend python manage.py recreate-db
```
This command recreates our PostgreSQL database using `manage.py`, where we can define custom CLI (Command Line Interface) using Flask-CLI. In short, you can create your own fun commands and tell docker what to do! In this case, we are just telling it to re-create our database for us.

## More Database fun!

You can run the following command after recreating the database. This should connect you to Postgres' command line interface **psql**!
```
$ docker exec -ti $(docker ps -aqf "name=users-db") psql -U postgres
### Indexes

# \c users_dev
You are now connected to database "users_dev" as user "postgres".
Efficient indexing is crucial for optimizing query performance, especially when dealing with large datasets. This project leverages PostgreSQL's powerful indexing capabilities:
# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
- **B-tree Indexes**: Used on primary keys, foreign keys, and other frequently queried columns. B-tree indexes are optimal for equality and range queries.
- **Full-text Search Indexes**: Implemented using PostgreSQL's `GIN` (Generalized Inverted Index) or `RUM` (Rum Universal Index) indexes to enable fast full-text searches on event descriptions and titles. This allows for efficient searching through large text fields.
- **Composite Indexes**: Created on multiple columns to support complex queries, ensuring that the database can quickly locate rows that match multiple criteria.
- **Partial Indexes**: Applied to subsets of data to optimize queries that frequently access a particular subset of rows.

# \q
```
If you are versed in SQL, you can also play around with it! # indicates that we are inside the psql shell.
After you login using the previous command, you can play around with psql!
### SQL Optimization and Scalability

```
postgres=# \c users_dev
You are now connected to database "users_dev" as user "postgres".
users_dev=# select * from users;
id | username | email | active
----+----------+-------+--------
(0 rows)
users_dev=# INSERT INTO users (username, email, active) VALUES ('abrar', '[email protected]', true);
INSERT 0 1
users_dev=# select * from users;
id | username | email | active
----+----------+------------------+--------
1 | abrar | [email protected] | t
(1 row)
users_dev=# INSERT INTO users (username, email, active) VALUES
users_dev-# ('sae', '[email protected]', true),
users_dev-# ('asya', '[email protected]', true),
users_dev-# ('yunju', '[email protected]', true),
users_dev-# ('dylan', '[email protected]', true),
users_dev-# ('oncel', '[email protected]', true),
users_dev-# ('landen', '[email protected]', true),
users_dev-# ('david', '[email protected]', true),
users_dev-# ('sadhvi', '[email protected]', true),
users_dev-# ('sumi', '[email protected]', true);
INSERT 0 9
users_dev=# select * from users;
id | username | email | active
----+----------+-------------------+--------
1 | abrar | [email protected] | t
2 | sae | [email protected] | t
3 | asya | [email protected] | t
4 | yunju | [email protected] | t
5 | dylan | [email protected] | t
6 | oncel | [email protected] | t
7 | landen | [email protected] | t
8 | david | [email protected] | t
9 | sadhvi | [email protected] | t
10 | sumi | [email protected] | t
(10 rows)
// inserting a value by mistake
users_dev=# INSERT INTO users (username, email, active) VALUES ('abrar', '[email protected]', true);
INSERT 0 1
users_dev=# select * from users;
id | username | email | active
----+----------+-------------------+--------
1 | abrar | [email protected] | t
2 | sae | [email protected] | t
3 | asya | [email protected] | t
4 | yunju | [email protected] | t
5 | dylan | [email protected] | t
6 | oncel | [email protected] | t
7 | landen | [email protected] | t
8 | david | [email protected] | t
9 | sadhvi | [email protected] | t
10 | sumi | [email protected] | t
11 | abrar | [email protected] | t
(11 rows)
// no worries, we can always delete it
users_dev=# DELETE from users WHERE username = 'abrar' and id = '11';
DELETE 1
users_dev=# select * from users;
id | username | email | active
----+----------+-------------------+--------
1 | abrar | [email protected] | t
2 | sae | [email protected] | t
3 | asya | [email protected] | t
4 | yunju | [email protected] | t
5 | dylan | [email protected] | t
6 | oncel | [email protected] | t
7 | landen | [email protected] | t
8 | david | [email protected] | t
9 | sadhvi | [email protected] | t
10 | sumi | [email protected] | t
(10 rows)
```
The backend of this project utilizes SQLAlchemy Core for interacting with the PostgreSQL database. SQLAlchemy Core provides a high level of flexibility and performance by allowing the construction of complex SQL queries programmatically.

0 comments on commit 1c81fb6

Please sign in to comment.