mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 12:43:12 +03:00
Теперь создается уникальное имя графика. Подправил статус коды
This commit is contained in:
parent
4e1a307e17
commit
1dabe437ce
1 changed files with 31 additions and 15 deletions
|
@ -1,13 +1,18 @@
|
||||||
import os
|
import os
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import dateutil.relativedelta
|
import dateutil.relativedelta
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
from fastapi import FastAPI, Response, status
|
from fastapi import FastAPI, Response, status
|
||||||
from psycopg2.extras import DictCursor
|
from psycopg2.extras import DictCursor
|
||||||
|
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
from middleware.plausible_analytics import PlausibleAnalytics
|
from middleware.plausible_analytics import PlausibleAnalytics
|
||||||
|
|
||||||
|
@ -29,26 +34,23 @@ if not os.path.exists('../charts'):
|
||||||
os.mkdir('../charts')
|
os.mkdir('../charts')
|
||||||
|
|
||||||
app.mount('/static/charts', StaticFiles(directory='../charts/'))
|
app.mount('/static/charts', StaticFiles(directory='../charts/'))
|
||||||
|
|
||||||
app.middleware('http')(PlausibleAnalytics())
|
app.middleware('http')(PlausibleAnalytics())
|
||||||
|
|
||||||
@app.get("/api/getChart/")
|
@app.get("/api/getChart/", status_code=status.HTTP_201_CREATED)
|
||||||
async def get_chart(response: Response,
|
async def get_chart(response: Response, from_currency: str, conv_currency: str, start_date: str, end_date: str):
|
||||||
from_currency: str, conv_currency: str,
|
|
||||||
start_date: str, end_date: str):
|
|
||||||
|
|
||||||
chart = await create_chart(from_currency, conv_currency, start_date, end_date)
|
chart = await create_chart(from_currency, conv_currency, start_date, end_date)
|
||||||
|
|
||||||
if not chart:
|
if not chart:
|
||||||
response.status_code = status.HTTP_404_NOT_FOUND
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return {'message': 'No data found.', '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',
|
return {
|
||||||
'status_code': status.HTTP_201_CREATED,
|
'message': f'./static/charts/{chart}.png',
|
||||||
|
'status_code': status.HTTP_201_CREATED,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/getChart/{period}")
|
@app.get("/api/getChart/{period}", status_code=status.HTTP_201_CREATED)
|
||||||
async def get_chart_period(response: Response, from_currency: str, conv_currency: str, period: str):
|
async def get_chart_period(response: Response, from_currency: str, conv_currency: str, period: str):
|
||||||
if period not in ['week', 'month', 'quarter', 'year']:
|
if period not in ['week', 'month', 'quarter', 'year']:
|
||||||
response.status_code = status.HTTP_400_BAD_REQUEST
|
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
@ -78,12 +80,13 @@ async def get_chart_period(response: Response, from_currency: str, conv_currency
|
||||||
response.status_code = status.HTTP_404_NOT_FOUND
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return {'message': 'No data found.', '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',
|
return {
|
||||||
'status_code': status.HTTP_201_CREATED,
|
'message': f'./static/charts/{chart}.png',
|
||||||
|
'status_code': status.HTTP_201_CREATED,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def create_chart(from_currency: str, conv_currency: str, start_date: str, end_date: str) -> bool:
|
async def create_chart(from_currency: str, conv_currency: str, start_date: str, end_date: str) -> (str, bool):
|
||||||
cur.execute('SELECT date, rate FROM currency WHERE (date BETWEEN %s AND %s) '
|
cur.execute('SELECT date, rate FROM currency WHERE (date BETWEEN %s AND %s) '
|
||||||
'AND from_currency = %s AND conv_currency = %s ORDER BY date',
|
'AND from_currency = %s AND conv_currency = %s ORDER BY date',
|
||||||
[
|
[
|
||||||
|
@ -95,7 +98,7 @@ async def create_chart(from_currency: str, conv_currency: str, start_date: str,
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
|
|
||||||
if not data or len(data) <= 1:
|
if not data or len(data) <= 1:
|
||||||
return False
|
return
|
||||||
|
|
||||||
date, rate = [], []
|
date, rate = [], []
|
||||||
|
|
||||||
|
@ -116,10 +119,23 @@ async def create_chart(from_currency: str, conv_currency: str, start_date: str,
|
||||||
fig = plt.gcf()
|
fig = plt.gcf()
|
||||||
fig.set_size_inches(18.5, 9.5)
|
fig.set_size_inches(18.5, 9.5)
|
||||||
|
|
||||||
fig.savefig(f'../charts/{from_currency}-{conv_currency}.png')
|
name = await generate_unique_name(
|
||||||
|
f'{from_currency.upper()}_{conv_currency.upper()}',
|
||||||
|
datetime.datetime.now()
|
||||||
|
)
|
||||||
|
|
||||||
|
fig.savefig(f'../charts/{name}.png')
|
||||||
fig.clear()
|
fig.clear()
|
||||||
|
|
||||||
return True
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_unique_name(currency_pair: str, date: datetime) -> str:
|
||||||
|
date_str = date.strftime("%Y%m%d")
|
||||||
|
random_suffix = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
||||||
|
unique_name = f"{currency_pair}_{date_str}_{random_suffix}"
|
||||||
|
|
||||||
|
return unique_name
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Reference in a new issue