Commit a788c1a7 authored by 陈涛's avatar 陈涛

增加执行补充净值记录接口

parent 3b49a589
import datetime
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from motor.core import AgnosticCollection from motor.core import AgnosticCollection
from dependencies import get_nav_collect from dependencies import get_nav_collect
from model import SortParams, FilterTime, Page, PageResponse from model import SortParams, FilterTime, Page, PageResponse, Response
from model.fund import StakingFundNav from model.fund import StakingFundNav
from service.nav import calculate_nav
router = APIRouter() router = APIRouter()
...@@ -26,3 +29,13 @@ async def nav( ...@@ -26,3 +29,13 @@ async def nav(
result = await cursor.to_list(length=None) result = await cursor.to_list(length=None)
response = PageResponse[StakingFundNav](data=result, **page.dict(), total=count) response = PageResponse[StakingFundNav](data=result, **page.dict(), total=count)
return response return response
@router.post("/{fund_id}/", response_model=Response[StakingFundNav], summary="插入净值记录")
async def insert_nav(
fund_id: str,
calc_time: datetime.datetime,
):
fund_nav = await calculate_nav(fund_id=fund_id, calc_time=calc_time)
response = Response[StakingFundNav](data=fund_nav)
return response
...@@ -11,9 +11,13 @@ from service.beacon import BeaconChaService ...@@ -11,9 +11,13 @@ from service.beacon import BeaconChaService
from service.price import get_price from service.price import get_price
async def calculate_nav(fund_id, calc_time=None, beach_service=BeaconChaService()): async def calculate_nav(fund_id, calc_time: datetime.datetime = None, beach_service=BeaconChaService()):
# todo 测试 # todo 测试
calc_time = datetime.datetime.utcnow().replace(minute=0, second=0, microsecond=0, tzinfo=pytz.UTC) calc_time = calc_time.replace(minute=0, second=0, microsecond=0,
tzinfo=pytz.UTC) if calc_time else datetime.datetime.utcnow().replace(minute=0,
second=0,
microsecond=0,
tzinfo=pytz.UTC)
from main import app from main import app
fund_collect = get_fund_collect(app.state.mongodb_manager) fund_collect = get_fund_collect(app.state.mongodb_manager)
logger.info(f'[定时任务开始执行] [计算净值] {fund_id} ') logger.info(f'[定时任务开始执行] [计算净值] {fund_id} ')
...@@ -70,7 +74,12 @@ async def calculate_nav(fund_id, calc_time=None, beach_service=BeaconChaService( ...@@ -70,7 +74,12 @@ async def calculate_nav(fund_id, calc_time=None, beach_service=BeaconChaService(
net_value = round(value / total_fund_share, 2) if total_fund_share > 0 else fund_data["base_nav"] net_value = round(value / total_fund_share, 2) if total_fund_share > 0 else fund_data["base_nav"]
await fund_collect.update_one({'id': fund_id}, {"$set": {"nva": net_value}}) await fund_collect.update_one({'id': fund_id}, {"$set": {"nva": net_value}})
nav_collect = get_nav_collect(app.state.mongodb_manager) nav_collect = get_nav_collect(app.state.mongodb_manager)
date = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC)
date = calc_time.replace(hour=0, minute=0, second=0, microsecond=0,
tzinfo=pytz.UTC) if calc_time else datetime.datetime.utcnow().replace(hour=0, minute=0,
second=0,
microsecond=0,
tzinfo=pytz.UTC)
if fund_data["settlement_time"] == "00:00": if fund_data["settlement_time"] == "00:00":
date -= datetime.timedelta(days=1) date -= datetime.timedelta(days=1)
nav_record = await nav_collect.find_one({"fund_id": fund_id, "record_date": date}) nav_record = await nav_collect.find_one({"fund_id": fund_id, "record_date": date})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment