import uuid import datetime from app.config import TaskEnv 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 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 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 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