-
Notifications
You must be signed in to change notification settings - Fork 21
WEB-3381 | backend setup for DBM #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Version control | ||
| .git | ||
|
|
||
| # Compiled Python bytecode | ||
| **/__pycache__ | ||
| **/*.pyc | ||
| **/*.pyo | ||
|
|
||
| # Compiled extensions | ||
| **/*.pyd | ||
| **/*.so | ||
|
|
||
| # coverage.py | ||
| .coverage | ||
| .coverage.* | ||
| htmlcov | ||
|
|
||
| # Cached files | ||
| .cache | ||
| .mypy_cache | ||
| .hypothesis | ||
| .pytest_cache | ||
|
|
||
| # Virtualenvs and builds | ||
| build/ | ||
| dist/ | ||
| .env | ||
| .venv | ||
| env/ | ||
| venv/ | ||
| ENV/ | ||
| env.bak/ | ||
| venv.bak/ | ||
|
|
||
| # Docker | ||
| Dockerfile* | ||
| .dockerignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # syntax = docker/dockerfile:1.2 | ||
| # ^ This enables the new BuildKit stable syntax which can be | ||
| # run with the DOCKER_BUILDKIT=1 environment variable in your | ||
| # docker build command (see build.sh) | ||
| FROM python:3.9.6-slim-buster | ||
|
|
||
| # Update, upgrade, and cleanup debian packages | ||
| RUN export DEBIAN_FRONTEND=noninteractive && \ | ||
| apt-get update && \ | ||
| apt-get upgrade --yes && \ | ||
| apt-get install --yes build-essential libpq-dev && \ | ||
| apt-get clean && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Copy over app | ||
| WORKDIR /app | ||
| COPY . . | ||
|
|
||
| # Install dependencies via pip and avoid caching build artifacts | ||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| # Set default Flask app and development environment | ||
| ENV FLASK_APP=dbm.py | ||
|
|
||
| # Start the app using ddtrace so we have profiling and tracing | ||
| ENTRYPOINT ["ddtrace-run"] | ||
| CMD gunicorn --bind 0.0.0.0:7578 dbm:app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from flask import Flask | ||
| from models import Items, db | ||
| from faker import Faker | ||
| import random | ||
| import os | ||
|
|
||
| fake = Faker() | ||
| DB_USERNAME = os.environ['POSTGRES_USER'] | ||
| DB_PASSWORD = os.environ['POSTGRES_PASSWORD'] | ||
| DB_HOST = os.environ['POSTGRES_HOST'] | ||
|
|
||
|
|
||
| def create_app(): | ||
| """Create a Flask application""" | ||
| app = Flask(__name__) | ||
| app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + \ | ||
| DB_USERNAME + ':' + DB_PASSWORD + '@' + DB_HOST + '/' + DB_USERNAME | ||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
|
|
||
| db.init_app(app) | ||
| initialize_database(app, db) | ||
| return app | ||
|
|
||
|
|
||
| def initialize_database(app, db): | ||
| """Drop and restore database in a consistent state""" | ||
| app.logger.info('Running DB Init for DBM') | ||
| with app.app_context(): | ||
| db.drop_all() | ||
| db.create_all() | ||
| for i in range(15000): | ||
| newItem = Items( | ||
| fake.sentence(), | ||
| random.randint(1, 7000), | ||
| fake.image_url(), | ||
| random.randint(1, 10) | ||
| ) | ||
| db.session.add(newItem) | ||
| i+1 | ||
| db.session.commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from flask import jsonify | ||
| from flask_cors import CORS | ||
| from bootstrap import create_app | ||
| from models import db | ||
| import random | ||
| import os | ||
|
|
||
| DB_USERNAME = os.environ['POSTGRES_USER'] | ||
| DB_PASSWORD = os.environ['POSTGRES_PASSWORD'] | ||
| DB_HOST = os.environ['POSTGRES_HOST'] | ||
|
|
||
| DB_URL = 'postgresql://' + \ | ||
| DB_USERNAME + ':' + DB_PASSWORD + '@' + DB_HOST + '/' + DB_USERNAME | ||
|
|
||
| app = create_app() | ||
| app.config.update( | ||
| DEBUG=True, | ||
| SECRET_KEY="secret_sauce", | ||
| ) | ||
|
|
||
| CORS(app) | ||
| engine = db.create_engine(DB_URL) | ||
|
|
||
| @app.route("/get-item", methods=["GET"]) | ||
| def product_ticker(): | ||
| query = db.text(f'SELECT * FROM items WHERE order_count::int > {random.randint(1, 7000)};') | ||
| app.logger.info(engine) | ||
| try: | ||
| app.logger.info('Connecting to db') | ||
| with engine.begin() as conn: | ||
| results = conn.execute(query).fetchall() | ||
| if results: | ||
| app.logger.info('Results found, parsing single item') | ||
| result = random.choice(results) | ||
| item_response = { | ||
| 'id': result.id, | ||
| 'description': result.description, | ||
| 'last_hour': result.last_hour, | ||
| 'order_count': result.order_count, | ||
| 'image_url': result.image_url | ||
| } | ||
| return jsonify(item_response) | ||
| except: | ||
| app.logger.error("An error occurred while getting items.") | ||
| err = jsonify({'error': 'Internal Server Error'}) | ||
| err.status_code = 500 | ||
| return err | ||
|
|
||
| if __name__ == "__main__": | ||
| app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from flask_sqlalchemy import SQLAlchemy | ||
|
|
||
| db = SQLAlchemy() | ||
|
|
||
|
|
||
| class Items(db.Model): | ||
| __tablename__ = 'items' | ||
| id = db.Column(db.Integer, primary_key=True) | ||
| description = db.Column(db.String(128)) | ||
| order_count = db.Column(db.String(64)) | ||
| last_hour = db.Column(db.String(64)) | ||
| image_url = db.Column(db.String(64)) | ||
|
|
||
| def __init__(self, description, order_count, image_url, last_hour): | ||
| self.description = description | ||
| self.order_count = order_count | ||
| self.last_hour = last_hour | ||
| self.image_url = image_url | ||
|
|
||
| def serialize(self): | ||
| return { | ||
| 'id': self.id, | ||
| 'description': self.description, | ||
| 'order_count': self.order_count, | ||
| 'last_hour': self.last_hour, | ||
| 'image_url': self.image_url | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| certifi==2020.11.8 | ||
| chardet==3.0.4 | ||
| click==8.0 | ||
| ddtrace==1.7.2 | ||
| Flask==2.2.2 | ||
| Flask-Login==0.6.2 | ||
| Flask-WTF==1.0.1 | ||
| Flask-Cors==3.0.10 | ||
| Flask-SQLAlchemy==3.0.2 | ||
| idna==2.10 | ||
| intervaltree==3.1.0 | ||
| itsdangerous==2.0 | ||
| Jinja2==3.0 | ||
| MarkupSafe==2.1.1 | ||
| nose==1.3.7 | ||
| protobuf==3.14.0 | ||
| requests==2.25.1 | ||
| six==1.15.0 | ||
| sortedcontainers==2.3.0 | ||
| SQLAlchemy==1.4.42 | ||
| psycopg2-binary | ||
| tenacity==6.2.0 | ||
| urllib3==1.26.5 | ||
| gunicorn==20.1.0 | ||
| Faker==18.3.2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a temp file or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly not sure, wasn't going to commit it but then noticed that there was a ton of these files. I'm not exactly sure what they're for and if we even need them (in the repo at least).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yea, I see what you mean. Maybe we should try deleting them, restarting the app locally, and see if it blows up or if the backend will auto-generate them. And then add that /storage dir to .gitignore
We try that separately from this PR, doesn't have to happen here