From 423b4a4ff62c4475826a576be14412c881e6236a Mon Sep 17 00:00:00 2001 From: Redume Date: Mon, 7 Oct 2024 16:27:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=80?= =?UTF-8?q?=D0=BE=D1=83=D1=82=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B3=D1=80=D0=B0=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B8=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0=D0=BC=20(=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB=D1=8F,=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=81=D1=8F=D1=86,=20=D0=BA=D0=B2=D0=B0=D1=80?= =?UTF-8?q?=D1=82=D0=B0=D0=BB,=20=D0=B3=D0=BE=D0=B4).=20=D0=92=D1=8B=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B3=D1=80=D0=B0=D1=84=D0=B8=D0=BA=D0=B0=20=D0=B2=20=D0=BE?= =?UTF-8?q?=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D1=8E.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chart/chart.py | 81 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/chart/chart.py b/chart/chart.py index d273fb9..e873f58 100644 --- a/chart/chart.py +++ b/chart/chart.py @@ -3,10 +3,15 @@ import psycopg2 import uvicorn import yaml import matplotlib.pyplot as plt +import locale +import datetime +import dateutil.relativedelta + from fastapi import FastAPI, Response, status from psycopg2.extras import DictCursor from starlette.staticfiles import StaticFiles + app = FastAPI() config = yaml.safe_load(open('../config.yaml')) @@ -20,6 +25,9 @@ con = psycopg2.connect(host=config['database']['host'], cur = con.cursor(cursor_factory=DictCursor) con.autocommit = True +if not os.path.exists('../charts'): + os.mkdir('../charts') + app.mount('/static/charts', StaticFiles(directory='../charts/')) @@ -27,30 +35,66 @@ app.mount('/static/charts', StaticFiles(directory='../charts/')) async def get_chart(response: Response, from_currency: str, conv_currency: str, start_date: str, end_date: str): - """ - :param response: - :param from_currency: The currency to convert from - :type from_currency: str - :param conv_currency: The currency to be converted into - :type conv_currency: str - :param start_date: The start date of the schedule period - :type start_date: str - :param end_date: The last date of the schedule period - :type end_date: str - :return: - """ + + chart = await create_chart(from_currency, conv_currency, start_date, end_date) + + if not chart: + response.status_code = status.HTTP_404_NOT_FOUND + return {'message': 'No data found.', 'status_code': status.HTTP_404_NOT_FOUND} + + return {'message': f'./static/charts/{from_currency}-{conv_currency}.png', + 'status_code': status.HTTP_201_CREATED, + } + + +@app.get("/api/getChart/{period}") +async def get_chart_period(response: Response, from_currency: str, conv_currency: str, period: str): + if period not in ['week', 'month', 'quarter', 'year']: + response.status_code = status.HTTP_400_BAD_REQUEST + return {'message': 'Invalid period.', 'status_code': status.HTTP_400_BAD_REQUEST} + + days, month, years = 0, 0, 0 + + if period == 'week': + days = -7 + elif period == 'month': + month = -1 + elif period == 'quarter': + month = -3 + elif period == 'year': + years = -1 + + end_date = datetime.datetime.now() + start_date = end_date + dateutil.relativedelta.relativedelta(months=month, days=days, years=years) + + chart = await create_chart(from_currency, + conv_currency, + start_date.strftime('%Y-%m-%d'), + end_date.strftime('%Y-%m-%d') + ) + + if not chart: + response.status_code = status.HTTP_404_NOT_FOUND + return {'message': 'No data found.', 'status_code': status.HTTP_404_NOT_FOUND} + + return {'message': f'./static/charts/{from_currency}-{conv_currency}.png', + 'status_code': status.HTTP_201_CREATED, + } + + +async def create_chart(from_currency: str, conv_currency: str, start_date: str, end_date: str) -> bool: cur.execute('SELECT date, rate FROM currency WHERE (date BETWEEN %s AND %s) ' 'AND from_currency = %s AND conv_currency = %s ORDER BY date', [ start_date, end_date, from_currency.upper(), conv_currency.upper() ]) + con.commit() data = cur.fetchall() - if not data: - response.status_code = status.HTTP_404_NOT_FOUND - return {'message': 'No data found', 'status_code': status.HTTP_404_NOT_FOUND} + if not data or len(data) <= 1: + return False date, rate = [], [] @@ -71,15 +115,10 @@ async def get_chart(response: Response, fig = plt.gcf() fig.set_size_inches(18.5, 9.5) - if not os.path.exists('../charts'): - os.mkdir('../charts') - fig.savefig(f'../charts/{from_currency}-{conv_currency}.png') fig.clear() - return {'message': f'./static/charts/{from_currency}-{conv_currency}.png', - 'status_code': status.HTTP_201_CREATED, - } + return True if __name__ == '__main__':