From 801742d466e4701dd09414ddca7de09a7ae5bbb8 Mon Sep 17 00:00:00 2001 From: shahzeydyfah Date: Thu, 13 Jun 2024 20:13:47 +0330 Subject: [PATCH] fix:last record --- app/models/monito_physical_progress.py | 6 ++++ app/repositories/monitor_physical_progress.py | 30 +++++++++++++++---- app/repositories/monitor_rial_progress.py | 11 ++++--- app/urls/monitor_physical.py | 6 ++-- app/urls/monitor_rial.py | 5 ++-- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/app/models/monito_physical_progress.py b/app/models/monito_physical_progress.py index 9580826..ad72bf5 100644 --- a/app/models/monito_physical_progress.py +++ b/app/models/monito_physical_progress.py @@ -2,6 +2,7 @@ import datetime import uuid from typing import Optional +import pydantic from pydantic import Field from core.models.base import BaseModel @@ -18,3 +19,8 @@ class PhysicalProgressModel(BaseModel): last_month:Optional[int] = Field(description='واقعی ماه اخیر') last_three_month:Optional[int] = Field(description='واقعی ماه اخیر') +class LastRecord(pydantic.BaseModel): + project_id:Optional[uuid.UUID] = Field(description="کد شناسایی پروژه در این فیلد قرار می گیرد") + date_at:Optional[datetime.datetime] = Field(description='تاریخ وارد شده') + + diff --git a/app/repositories/monitor_physical_progress.py b/app/repositories/monitor_physical_progress.py index e38f0c4..91511b5 100644 --- a/app/repositories/monitor_physical_progress.py +++ b/app/repositories/monitor_physical_progress.py @@ -1,7 +1,8 @@ import uuid +import datetime from app.config import TaskEnv -from app.models.monito_physical_progress import PhysicalProgressModel +from app.models.monito_physical_progress import PhysicalProgressModel, LastRecord from core.repositories.base import BaseRepository from core.service.mongo_db import use_collection from core.utils.crudify import mongo_inserter @@ -9,18 +10,37 @@ from app.repositories.project import ProjectRepository COLLECTION = TaskEnv.CLN_MONITOR_PYSICAL MODEL = PhysicalProgressModel + + class PhysicalProgressRepository(BaseRepository): def __init__(self): super().__init__(collection=TaskEnv.CLN_MONITOR_PYSICAL, model=PhysicalProgressModel) - async def last_record(self,project_id:uuid.UUID): - last = await use_collection(TaskEnv.CLN_MONITOR_PYSICAL).find({'project_id':project_id}).sort("$natural", -1).to_list(None) + async def search_last(self, connection: str, _inp: LastRecord, first: int, last: int): + m_first = _inp.date_at - datetime.timedelta(days=first) + print(m_first) + m_end = _inp.date_at - datetime.timedelta(days=last) + print(m_end) + last = await use_collection(connection).find({ + 'project_id': _inp.project_id, + 'date_at': {'$gte': m_first, '$lte': m_end} + }).sort("$natural", -1).to_list(None) if last: return last[0] else: return {} - async def create(self,_inp:MODEL): - create = await mongo_inserter(_inp,COLLECTION,getter=self.get) + async def last_record(self, _inp: LastRecord): + send = {'month': {}, 'three_month': {}} + last = await self.search_last(TaskEnv.CLN_MONITOR_PYSICAL, _inp, 35, 25) + if last: + send['month'] = last + three_month = await self.search_last(TaskEnv.CLN_MONITOR_PYSICAL, _inp, 95, 85) + if three_month: + send['three_month'] = three_month + return send + + async def create(self, _inp: MODEL): + create = await mongo_inserter(_inp, COLLECTION, getter=self.get) await ProjectRepository().set_sts_two_for_monitor(_inp.project_id) return create diff --git a/app/repositories/monitor_rial_progress.py b/app/repositories/monitor_rial_progress.py index 740fa95..923037f 100644 --- a/app/repositories/monitor_rial_progress.py +++ b/app/repositories/monitor_rial_progress.py @@ -1,7 +1,9 @@ import uuid from app.config import TaskEnv +from app.models.monito_physical_progress import LastRecord from app.models.monitor_rial_progress import RialProgressModel +from app.repositories.monitor_physical_progress import PhysicalProgressRepository from app.repositories.project import ProjectRepository from core.repositories.base import BaseRepository from core.service.mongo_db import use_collection @@ -12,12 +14,9 @@ class RialProgressRepository(BaseRepository): def __init__(self): super().__init__(collection=TaskEnv.CLN_MONITOR_RIAL, model=RialProgressModel) - async def last_record(self, project_id:uuid.UUID): - last = await use_collection(TaskEnv.CLN_MONITOR_RIAL).find({'project_id':project_id}).sort("$natural", -1).to_list(None) - if last: - return last[0] - else: - return {} + async def last_record(self, _inp: LastRecord): + last = await PhysicalProgressRepository().search_last(TaskEnv.CLN_MONITOR_RIAL, _inp, 35, 25) + return last async def create(self,_inp:RialProgressModel): create = await mongo_inserter(_inp,TaskEnv.CLN_MONITOR_RIAL,getter=self.get) diff --git a/app/urls/monitor_physical.py b/app/urls/monitor_physical.py index 809813e..071b31b 100644 --- a/app/urls/monitor_physical.py +++ b/app/urls/monitor_physical.py @@ -2,7 +2,7 @@ from uuid import UUID from fastapi import Body -from app.models.monito_physical_progress import PhysicalProgressModel +from app.models.monito_physical_progress import PhysicalProgressModel, LastRecord from app.repositories.monitor_physical_progress import PhysicalProgressRepository from core.app import app from core.utils.router import generate_router, shrink @@ -19,9 +19,9 @@ repo = PhysicalProgressRepository() def create(coding: ThisModel = Body(...)): pass -@router.post('/last/{project_id}', response_model=ThisModel, tags=_tag_base, description='پیشرفت فیزیکی') +@router.post('/last', response_model=list[dict], tags=_tag_base, description='پیشرفت فیزیکی') @shrink(repo.last_record) -def last_record(project_id:UUID): +def last_record(last:LastRecord): pass @router.put('/{_id}', response_model=ThisModel, tags=_tag_base, description='پیشرفت فیزیکی') diff --git a/app/urls/monitor_rial.py b/app/urls/monitor_rial.py index f4a2bb5..a0335d2 100644 --- a/app/urls/monitor_rial.py +++ b/app/urls/monitor_rial.py @@ -2,6 +2,7 @@ from uuid import UUID from fastapi import Body +from app.models.monito_physical_progress import LastRecord from app.models.monitor_rial_progress import RialProgressModel from app.repositories.monitor_rial_progress import RialProgressRepository from core.app import app @@ -19,9 +20,9 @@ repo = RialProgressRepository() def create(coding: ThisModel = Body(...)): pass -@router.post('/last/{project_id}', response_model=ThisModel, tags=_tag_base, description='پیشرفت ریالی') +@router.post('/last', response_model=ThisModel, tags=_tag_base, description='پیشرفت ریالی') @shrink(repo.last_record) -def last_record(project_id:UUID): +def last_record(last:LastRecord): pass @router.put('/{_id}', response_model=ThisModel, tags=_tag_base, description='پیشرفت ریالی')