Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmarieta committed Dec 15, 2023
0 parents commit dff1367
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
services:
db-test:
image: postgres:15.2
container_name: db-test
environment:
POSTGRES_USER: psql
POSTGRES_PASSWORD: pswd
POSTGRES_DB: db
ports:
- "5432:5432"
volumes:
- db-v:/var/lib/postgresql/datatmp
healthcheck:
# checking when the database is ready for the flask app to wait
test: ["CMD-SHELL", "pg_isready -U psql -d db"]
interval: 5s
timeout: 20s
retries: 5

flask-test:
build: ./flask
container_name: flask-test
ports:
- "5000:5000"
depends_on:
db-test:
# waiting for the database to be ready
condition: service_healthy
volumes:
- ./flask:/app

# to persist container volumes
volumes:
db-v:
14 changes: 14 additions & 0 deletions flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.9.16

RUN apt-get update && \
apt-get install -y postgresql-client-13 && \
apt-get install -y htop

WORKDIR /app

COPY . .
RUN pip install -r requirements.txt

EXPOSE 5000

CMD [ "python", "application.py"]
63 changes: 63 additions & 0 deletions flask/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os

db = SQLAlchemy()

application = Flask(__name__)
application.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://psql:pswd@db-test:5432/db'

db.init_app(application)

class EventParent(db.Model):

__tablename__ = 'EventParent'

id = db.Column(db.Integer, primary_key=True)
notebook_id = db.Column(db.String(100), nullable=False)
event_type = db.Column(db.String(32), nullable=False)
__mapper_args__ = {
'polymorphic_identity': 'EventParent',
'polymorphic_on': event_type
}

class Event(EventParent):

__tablename__ = 'Event'

id = db.Column(db.Integer, db.ForeignKey('EventParent.id'), primary_key=True)
col_1 = db.Column(db.String(200), nullable=False)

__mapper_args__ = {
'polymorphic_identity': 'Event'
}

nb_entries = 50000
notebook_id = 'postman_python'
content = "wngfiwrhguirehgiuerhgiuthiuthbirubntruituihthbrtwngfiwrhguirehgiuerhgiuthiuthbirubntruituihthbrtwngfiwrhguirehgiuerhgiuthiuthbirubntruituihthbrtwngfiwrhguirehgiuerhgiuthiuthbirubntruituihthbrtwng"

with application.app_context():
db.create_all()
# populate the table
if Event.query.first() is None :
for n in range(nb_entries) :
new_event = Event(
notebook_id=notebook_id,
col_1=content
)

db.session.add(new_event)
db.session.commit()

db.session.close()

@application.route('/listevents', methods=['GET'])
def list_events():
rows = db.session.query(Event).all()

nb_rows = len(rows)
return jsonify(f"Number of rows : {nb_rows}"), 200


if __name__ == "__main__":
application.run(debug=True, host="0.0.0.0")
53 changes: 53 additions & 0 deletions flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
alembic==1.10.4
APScheduler==3.10.4
attrs==23.1.0
boto3==1.28.35
botocore==1.31.35
certifi==2023.7.22
charset-normalizer==3.3.0
click==8.1.3
exceptiongroup==1.1.2
fastjsonschema==2.18.0
Flask==2.2.3
Flask-APScheduler==1.12.4
Flask-Cors==3.0.10
Flask-Login==0.6.3
Flask-Migrate==4.0.4
Flask-SQLAlchemy==3.0.3
greenlet==2.0.2
idna==3.4
importlib-metadata==6.3.0
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
jmespath==1.0.1
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
jupyter_core==5.3.1
Mako==1.2.4
MarkupSafe==2.1.2
memory-profiler==0.61.0
nbformat==5.9.2
packaging==23.1
platformdirs==3.10.0
pluggy==1.2.0
psutil==5.9.6
psycopg2-binary==2.9.6
pycryptodome==3.19.0
Pympler==1.0.1
pytest==7.4.0
python-dateutil==2.8.2
pytz==2023.3.post1
referencing==0.30.2
requests==2.31.0
rpds-py==0.9.2
s3transfer==0.6.2
six==1.16.0
SQLAlchemy==2.0.9
tomli==2.0.1
traitlets==5.9.0
typing_extensions==4.5.0
tzlocal==5.0.1
urllib3==1.26.16
Werkzeug==2.2.3
zipp==3.15.0

0 comments on commit dff1367

Please sign in to comment.