generated from annakor98/2024-spring-ab-python-ads-HW-4
-
Notifications
You must be signed in to change notification settings - Fork 8
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
PR for hw4 #3
Open
Nimbleredsquirrel
wants to merge
8
commits into
central-university-dev:main
Choose a base branch
from
Nimbleredsquirrel:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
PR for hw4 #3
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5501937
Add files via upload
Nimbleredsquirrel 59a7922
Rename __init__.py to app/__init__.py
Nimbleredsquirrel 43bc522
Add files via upload
Nimbleredsquirrel ec0cff8
Add files via upload
Nimbleredsquirrel 425bc7d
Rename __init__.py to tests/__init__.py
Nimbleredsquirrel ec4da9b
Add files via upload
Nimbleredsquirrel 305153f
Delete Dockerfile
Nimbleredsquirrel 654a1d7
Update README.md
Nimbleredsquirrel 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 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,2 @@ | ||
MODEL_PATH = "model.pkl" | ||
DB_URL = "postgresql+psycopg2://postgres:postgres@postgres:5432/postgres" |
This file contains 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,3 @@ | ||
*.db | ||
*.env | ||
/venv |
This file contains 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,44 @@ | ||
.DEFAULT_GOAL := help | ||
PYTHONPATH = PYTHONPATH=./ | ||
TEST = $(PYTHONPATH) pytest --verbosity=2 --showlocals --log-level=DEBUG --strict-markers $(arg) | ||
CODE = app tests | ||
|
||
.PHONY: help | ||
help: ## Show this help | ||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | ||
|
||
.PHONY: test | ||
#test: ## Runs pytest with coverage | ||
#$(TEST) | ||
|
||
#.PHONY: test-fast | ||
#test-fast: ## Runs pytest with exitfirst | ||
# $(TEST) --exitfirst | ||
|
||
#.PHONY: test-failed | ||
#test-failed: ## Runs pytest from last-failed | ||
# $(TEST) --last-failed | ||
|
||
#.PHONY: test-cov | ||
#test-cov: ## Runs pytest with coverage report | ||
# $(TEST) --cov --cov-report html | ||
|
||
.PHONY: lint | ||
lint: ## Lint code | ||
black --check $(CODE) | ||
flake8 --jobs 4 --statistics --show-source --ignore E501,W503,E203 $(CODE) | ||
pylint $(CODE) | ||
mypy --namespace-packages --ignore-missing-imports --explicit-package-bases $(CODE) | ||
# pytest --dead-fixtures --dup-fixtures | ||
# safety check --full-report | ||
# bandit -c pyproject.toml -r $(CODE) | ||
poetry check | ||
|
||
.PHONY: format | ||
format: ## Formats all files | ||
autoflake --recursive --in-place --ignore-init-module-imports --remove-all-unused-imports $(CODE) | ||
isort $(CODE) | ||
black $(CODE) | ||
|
||
.PHONY: check | ||
check: format lint #test ## Format and lint code then run tests |
This file contains 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 |
---|---|---|
@@ -1,18 +1,9 @@ | ||
# 2024-spring-ab-python-ads-HW-4 | ||
## Интеграции и БД | ||
|
||
## Интеграции и БД + повторение прошлого семестра :) | ||
Здесь представлено решение задачи получения количества деревьев в определенном городе в определенном году с помощью Open Street Map. | ||
|
||
* Реализовать с помощью FastAPI приложение с эндпоинтом (POST), позволяющим получить количество деревьев в определенном городе в определенном году с помощью Open Street Map. | ||
API (см. example 3 здесь: https://wiki.openstreetmap.org/wiki/OSMPythonTools). Название города и страны + год должны передаваться в теле запроса. | ||
* Для обращения к Open Street Map API использовать библиотеку requests, НЕ OSMPythonTools. | ||
* Каждый успешный запрос должен логироваться в базу данных (postgresql). | ||
* Реализовать хранимую процедуру в базе данных на plpython, вычисляющую город, который встретился наибольшее количество раз в истории обращений к эндпоинту. | ||
|
||
Подсказка - Dockerfile для postgres с plpython лежит в репе. | ||
|
||
**Критерии оценки** | ||
* Реализован эндопинт - +2 балла | ||
* Используется библиотека requests - +1 балл | ||
* Реализован docker-compose с приложением и базой данных - +4 балла | ||
* Реализована хранимая процедура - +2 балла | ||
* Правильная структура репозитория - +1 балл | ||
* Реализовано с помощью FastAPI приложение с эндпоинтом (POST), позволяющим получить количество деревьев в определенном городе в определенном году с помощью Open Street Map. | ||
API. | ||
* Для обращения к Open Street Map API используется библиотека requests. | ||
* Каждый успешный запрос логируется в базу данных (postgresql). | ||
* Реализована хранимая процедуру в базе данных, вычисляющая город, который встретился наибольшее количество раз в истории обращений к эндпоинту. |
This file contains 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,12 @@ | ||
FROM python:3.11 as requirements-stage | ||
WORKDIR /tmp | ||
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org poetry | ||
COPY ./pyproject.toml ./poetry.lock* /tmp/ | ||
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes | ||
|
||
FROM python:3.11 | ||
WORKDIR /code | ||
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt | ||
RUN pip3 install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org -r /code/requirements.txt | ||
COPY ./app /code/app | ||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] |
This file contains 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 @@ | ||
"""init file.""" |
This file contains 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,16 @@ | ||
"""Create database attributes.""" | ||
|
||
import os | ||
|
||
from dotenv import load_dotenv | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import declarative_base, sessionmaker | ||
|
||
load_dotenv() | ||
|
||
|
||
engine = create_engine(os.getenv("DB_URL")) | ||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
Base = declarative_base() |
This file contains 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,47 @@ | ||
"""This is the fraud detector.""" | ||
|
||
from datetime import datetime | ||
from typing import Any | ||
|
||
from fastapi import Depends, FastAPI | ||
from sqlalchemy.orm import Session | ||
|
||
from . import models, schemas | ||
from .database import SessionLocal, engine | ||
from .model_predict import tree_count | ||
|
||
app = FastAPI() | ||
models.Base.metadata.create_all(bind=engine) | ||
|
||
|
||
def get_db(): | ||
"""Give database.""" | ||
db = SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
|
||
@app.get("/") | ||
async def root(): | ||
"""Hello message.""" | ||
return {"message": "Hello! This is an information application about trees."} | ||
|
||
|
||
@app.post("/information") | ||
async def post_information( | ||
buff: schemas.InputFeatures, db: Session = Depends(get_db) | ||
) -> int | Any: | ||
"""Get info.""" | ||
info = tree_count(buff.city, buff.year) | ||
new_detect = models.Trees( | ||
city=buff.city, | ||
country=buff.country, | ||
year=int(buff.year), | ||
trees_count=info, | ||
request_time=datetime.now(), | ||
) | ||
db.add(new_detect) | ||
db.commit() | ||
return info |
This file contains 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,20 @@ | ||
"""Model predictions.""" | ||
import requests | ||
|
||
|
||
def tree_count(town, year): | ||
"""Parser for OSM.""" | ||
url = r"http://overpass-api.de/api/interpreter" | ||
query = f"""[out:json][date:'{str(year)}-01-01T00:00:00Z'][maxsize:2000000000]; | ||
area["name:en"="{town}"]->.searchArea; | ||
(node["natural"="tree"](area.searchArea);); out count;""" | ||
result = requests.post(url, data=query, timeout=60) | ||
t = result.text | ||
cap = "total" | ||
for i in range(len(t)): | ||
if t[i : i + len(cap)] == cap: | ||
t = t[i + len(cap) + 4 :] | ||
for j in range(len(t)): # pylint: disable=C0200 | ||
if not t[j].isdigit(): | ||
return int(t[:j]) | ||
return 0 |
This file contains 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,18 @@ | ||
"""This program create template from database.""" | ||
|
||
from sqlalchemy import Column, DateTime, Integer, String | ||
|
||
from .database import Base | ||
|
||
|
||
class Trees(Base): # pylint: disable=R0903 | ||
"""Template from database.""" | ||
|
||
__tablename__ = "Trees" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше писать имена таблиц в нижнем регистре. |
||
|
||
id = Column(Integer, primary_key=True) | ||
city = Column(String, default="") | ||
country = Column(String, default="") | ||
year = Column(Integer, default=2000) | ||
trees_count = Column(Integer, default=0) | ||
request_time = Column(DateTime) |
This file contains 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,11 @@ | ||
"""All schemas.""" | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class InputFeatures(BaseModel): | ||
"""Input features.""" | ||
|
||
city: str = "" | ||
country: str = "" | ||
year: str = "" |
This file contains 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,20 @@ | ||
CREATE EXTENSION IF NOT EXISTS plpython3u; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем plpython3u, если используешь plpgsql ? |
||
|
||
CREATE TABLE post_information ( | ||
id SERIAL PRIMARY KEY, | ||
city VARCHAR(255), | ||
country VARCHAR(255), | ||
year INT, | ||
trees_count INT, | ||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE OR REPLACE FUNCTION most_frequent_city() RETURNS TABLE(city VARCHAR, queries_count bigint) AS $$ BEGIN RETURN QUERY | ||
SELECT t.city, | ||
COUNT(*) as queries_count | ||
FROM "Trees" t | ||
GROUP BY t.city | ||
ORDER BY queries_count DESC | ||
LIMIT 1; | ||
END; | ||
$$ LANGUAGE plpgsql; |
Binary file not shown.
This file contains 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 @@ | ||
version: "3.3" | ||
|
||
services: | ||
postgres: | ||
build: | ||
context: . | ||
dockerfile: pg.Dockerfile | ||
ports: | ||
- 5432:5432 | ||
volumes: | ||
- pgdata:/var/lib/postgresql/data/pgdata | ||
environment: | ||
- PGDATA=/var/lib/postgresql/data/pgdata | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_DB=postgres | ||
|
||
app: | ||
build: | ||
context: . | ||
dockerfile: app.Dockerfile | ||
ports: | ||
- 8000:80 | ||
environment: | ||
- DB_URL=postgresql+psycopg2://postgres:postgres@postgres:5432/postgres | ||
volumes: | ||
pgdata: |
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
FROM postgres:13.3 | ||
RUN apt update && apt install python3 python3-pip postgresql-plpython3-${PG_MAJOR} -y | ||
RUN echo 'CREATE EXTENSION IF NOT EXISTS plpython3u;' > /docker-entrypoint-initdb.d/py3.sql | ||
COPY ./database.sql /docker-entrypoint-initdb.d/py3.sql |
Oops, something went wrong.
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.
Тут стоит обрабатывать ошибки.