feat: profit chart in statistics
This commit is contained in:
		
							
								
								
									
										1
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								main.py
									
									
									
									
									
								
							@@ -45,6 +45,7 @@ routers_list = [
 | 
				
			|||||||
    routers.time_tracking_router,
 | 
					    routers.time_tracking_router,
 | 
				
			||||||
    routers.billing_router,
 | 
					    routers.billing_router,
 | 
				
			||||||
    routers.task_router,
 | 
					    routers.task_router,
 | 
				
			||||||
 | 
					    routers.statistics_router,
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
for router in routers_list:
 | 
					for router in routers_list:
 | 
				
			||||||
    app.include_router(router)
 | 
					    app.include_router(router)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,3 +13,4 @@ from .payroll import payroll_router
 | 
				
			|||||||
from .time_tracking import time_tracking_router
 | 
					from .time_tracking import time_tracking_router
 | 
				
			||||||
from .billing import billing_router
 | 
					from .billing import billing_router
 | 
				
			||||||
from .task import task_router
 | 
					from .task import task_router
 | 
				
			||||||
 | 
					from .statistics import statistics_router
 | 
				
			||||||
							
								
								
									
										27
									
								
								routers/statistics.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								routers/statistics.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					from typing import Annotated
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from fastapi import APIRouter, Depends
 | 
				
			||||||
 | 
					from sqlalchemy.ext.asyncio import AsyncSession
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from backend.session import get_session
 | 
				
			||||||
 | 
					from schemas.statistics import GetProfitDataRequest, GetProfitDataResponse
 | 
				
			||||||
 | 
					from services.auth import authorized_user
 | 
				
			||||||
 | 
					from services.statistics import StatisticsService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					statistics_router = APIRouter(
 | 
				
			||||||
 | 
					    prefix="/statistics",
 | 
				
			||||||
 | 
					    tags=["statistics"],
 | 
				
			||||||
 | 
					    dependencies=[Depends(authorized_user)]
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@statistics_router.post(
 | 
				
			||||||
 | 
					    '/get-profit-data',
 | 
				
			||||||
 | 
					    response_model=GetProfitDataResponse,
 | 
				
			||||||
 | 
					    operation_id='get_profit_data',
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					async def get_profit_data(
 | 
				
			||||||
 | 
					        session: Annotated[AsyncSession, Depends(get_session)],
 | 
				
			||||||
 | 
					        request: GetProfitDataRequest
 | 
				
			||||||
 | 
					):
 | 
				
			||||||
 | 
					    return await StatisticsService(session).get_profit_data(request)
 | 
				
			||||||
							
								
								
									
										31
									
								
								schemas/statistics.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								schemas/statistics.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					import datetime
 | 
				
			||||||
 | 
					from typing import List, Tuple
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from schemas.base import BaseSchema
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# region Entities
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ProfitDataItem(BaseSchema):
 | 
				
			||||||
 | 
					    date: datetime.date
 | 
				
			||||||
 | 
					    revenue: float
 | 
				
			||||||
 | 
					    profit: float
 | 
				
			||||||
 | 
					    deals_count: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# region Requests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GetProfitDataRequest(BaseSchema):
 | 
				
			||||||
 | 
					    date_range: Tuple[datetime.date, datetime.date]
 | 
				
			||||||
 | 
					    client_id: int
 | 
				
			||||||
 | 
					    base_marketplace_key: str
 | 
				
			||||||
 | 
					    deal_status_id: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# region Responses
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GetProfitDataResponse(BaseSchema):
 | 
				
			||||||
 | 
					    data: List[ProfitDataItem]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# endregion
 | 
				
			||||||
							
								
								
									
										183
									
								
								services/statistics.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										183
									
								
								services/statistics.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,183 @@
 | 
				
			|||||||
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from sqlalchemy import select, and_, union_all, func, Subquery
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from models import DealService, Deal, DealStatusHistory, DealProductService, DealProduct, Service
 | 
				
			||||||
 | 
					from schemas.statistics import GetProfitDataResponse, GetProfitDataRequest, ProfitDataItem
 | 
				
			||||||
 | 
					from services.base import BaseService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class StatisticsService(BaseService):
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _get_sub_status_history():
 | 
				
			||||||
 | 
					        last_statuses = (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                DealStatusHistory.deal_id,
 | 
				
			||||||
 | 
					                func.max(DealStatusHistory.changed_at).label('changed_at')
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .group_by(DealStatusHistory.deal_id)
 | 
				
			||||||
 | 
					            .subquery()
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                Deal.id.label('deal_id'),
 | 
				
			||||||
 | 
					                last_statuses.c.changed_at,
 | 
				
			||||||
 | 
					                Deal.current_status,
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .join(last_statuses, last_statuses.c.deal_id == Deal.id)
 | 
				
			||||||
 | 
					            .subquery()
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _get_filtered_sub_status_history(date_from: datetime.date, date_to: datetime.date):
 | 
				
			||||||
 | 
					        sub_status_history = StatisticsService._get_sub_status_history()
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            select(sub_status_history)
 | 
				
			||||||
 | 
					            .where(sub_status_history.c.changed_at.between(date_from, date_to))
 | 
				
			||||||
 | 
					            .subquery()
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _fill_dates_gaps(rows, date_from: datetime.date, date_to: datetime.date) -> list[ProfitDataItem]:
 | 
				
			||||||
 | 
					        dates = defaultdict(lambda: {"deals_count": 0, "profit": 0, "revenue": 0})
 | 
				
			||||||
 | 
					        for row in rows:
 | 
				
			||||||
 | 
					            dates[row.changed_at.date()] = {
 | 
				
			||||||
 | 
					                "deals_count": row.deals_count,
 | 
				
			||||||
 | 
					                "profit": row.profit,
 | 
				
			||||||
 | 
					                "revenue": row.revenue
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        data = []
 | 
				
			||||||
 | 
					        while date_from < date_to:
 | 
				
			||||||
 | 
					            data_item = ProfitDataItem(
 | 
				
			||||||
 | 
					                date=date_from,
 | 
				
			||||||
 | 
					                deals_count=dates[date_from]["deals_count"],
 | 
				
			||||||
 | 
					                profit=dates[date_from]["profit"],
 | 
				
			||||||
 | 
					                revenue=dates[date_from]["revenue"],
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            data.append(data_item)
 | 
				
			||||||
 | 
					            date_from += timedelta(days=1)
 | 
				
			||||||
 | 
					        return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _get_stmt_deal_services(sub_filtered_status_history: Subquery):
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                Deal.id.label("deal_id"),
 | 
				
			||||||
 | 
					                func.date_trunc(
 | 
				
			||||||
 | 
					                    "day",
 | 
				
			||||||
 | 
					                    sub_filtered_status_history.c.changed_at,
 | 
				
			||||||
 | 
					                ).label("changed_at"),
 | 
				
			||||||
 | 
					                func.sum(DealService.price * DealService.quantity).label("revenue"),
 | 
				
			||||||
 | 
					                func.sum((DealService.price - Service.cost) * DealService.quantity).label("profit"),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .join(DealService, Deal.id == DealService.deal_id)
 | 
				
			||||||
 | 
					            .join(Service, DealService.service_id == Service.id)
 | 
				
			||||||
 | 
					            .join(sub_filtered_status_history, Deal.id == sub_filtered_status_history.c.deal_id)
 | 
				
			||||||
 | 
					            .where(Deal.is_deleted == False)
 | 
				
			||||||
 | 
					            .group_by(Deal.id, "changed_at")
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _apply_filters(request: GetProfitDataRequest, stmt_deal_services, stmt_deal_product_services):
 | 
				
			||||||
 | 
					        if request.client_id != -1:
 | 
				
			||||||
 | 
					            stmt_deal_services = stmt_deal_services.where(Deal.client_id == request.client_id)
 | 
				
			||||||
 | 
					            stmt_deal_product_services = stmt_deal_product_services.where(Deal.client_id == request.client_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if request.base_marketplace_key != "all":
 | 
				
			||||||
 | 
					            stmt_deal_services = stmt_deal_services.where(Deal.base_marketplace_key == request.base_marketplace_key)
 | 
				
			||||||
 | 
					            stmt_deal_product_services = stmt_deal_product_services.where(
 | 
				
			||||||
 | 
					                Deal.base_marketplace_key == request.base_marketplace_key)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if request.deal_status_id != -1:
 | 
				
			||||||
 | 
					            stmt_deal_services = stmt_deal_services.where(Deal.current_status == request.deal_status_id)
 | 
				
			||||||
 | 
					            stmt_deal_product_services = stmt_deal_product_services.where(Deal.current_status == request.deal_status_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return stmt_deal_services, stmt_deal_product_services
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _get_stmt_product_services():
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                Deal.id.label("deal_id"),
 | 
				
			||||||
 | 
					                func.sum(DealProductService.price * DealProduct.quantity).label("revenue"),
 | 
				
			||||||
 | 
					                func.sum((DealProductService.price - Service.cost) * DealProduct.quantity).label("profit"),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .join(DealProduct, Deal.id == DealProduct.deal_id)
 | 
				
			||||||
 | 
					            .join(
 | 
				
			||||||
 | 
					                DealProductService,
 | 
				
			||||||
 | 
					                and_(
 | 
				
			||||||
 | 
					                    DealProductService.deal_id == Deal.id,
 | 
				
			||||||
 | 
					                    DealProductService.product_id == DealProduct.product_id,
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .join(Service, DealProductService.service_id == Service.id)
 | 
				
			||||||
 | 
					            .where(Deal.is_deleted == False)
 | 
				
			||||||
 | 
					            .group_by(Deal.id)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _group_by_date(stmt):
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                stmt.c.changed_at,
 | 
				
			||||||
 | 
					                func.count(stmt.c.deal_id).label("deals_count"),
 | 
				
			||||||
 | 
					                func.sum(stmt.c.revenue).label("revenue"),
 | 
				
			||||||
 | 
					                func.sum(stmt.c.profit).label("profit"),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .group_by(stmt.c.changed_at)
 | 
				
			||||||
 | 
					            .order_by(stmt.c.changed_at.asc())
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async def get_profit_data(self, request: GetProfitDataRequest) -> GetProfitDataResponse:
 | 
				
			||||||
 | 
					        date_from, date_to = request.date_range
 | 
				
			||||||
 | 
					        date_to += timedelta(days=1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sub_filtered_status_history = self._get_filtered_sub_status_history(date_from, date_to)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_deal_services = self._get_stmt_deal_services(sub_filtered_status_history)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_deal_product_services = self._get_stmt_product_services()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_deal_services, stmt_deal_product_services = self._apply_filters(
 | 
				
			||||||
 | 
					            request,
 | 
				
			||||||
 | 
					            stmt_deal_services,
 | 
				
			||||||
 | 
					            stmt_deal_product_services
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sub_deal_product_services = stmt_deal_product_services.subquery()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_join_deals_statuses = (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                sub_deal_product_services.c.deal_id,
 | 
				
			||||||
 | 
					                func.date_trunc(
 | 
				
			||||||
 | 
					                    "day",
 | 
				
			||||||
 | 
					                    sub_filtered_status_history.c.changed_at
 | 
				
			||||||
 | 
					                ).label("changed_at"),
 | 
				
			||||||
 | 
					                sub_deal_product_services.c.revenue.label("revenue"),
 | 
				
			||||||
 | 
					                sub_deal_product_services.c.profit.label("profit"),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .join(sub_filtered_status_history, sub_deal_product_services.c.deal_id == sub_filtered_status_history.c.deal_id)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sub_grouped_by_deals = (
 | 
				
			||||||
 | 
					            select(
 | 
				
			||||||
 | 
					                stmt_union.c.deal_id,
 | 
				
			||||||
 | 
					                stmt_union.c.changed_at,
 | 
				
			||||||
 | 
					                func.sum(stmt_union.c.profit).label("profit"),
 | 
				
			||||||
 | 
					                func.sum(stmt_union.c.revenue).label("revenue"),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .group_by(stmt_union.c.deal_id, stmt_union.c.changed_at)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        stmt_grouped_by_date = self._group_by_date(sub_grouped_by_deals)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        result = await self.session.execute(stmt_grouped_by_date)
 | 
				
			||||||
 | 
					        rows = result.all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        data = self._fill_dates_gaps(rows, date_from, date_to)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return GetProfitDataResponse(data=data)
 | 
				
			||||||
							
								
								
									
										19
									
								
								test.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								test.py
									
									
									
									
									
								
							@@ -1,21 +1,30 @@
 | 
				
			|||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
 | 
					import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from sqlalchemy.ext.asyncio import AsyncSession
 | 
					from sqlalchemy.ext.asyncio import AsyncSession
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from backend.session import session_maker
 | 
					from backend.session import session_maker
 | 
				
			||||||
from services.billing import BillingService
 | 
					from schemas.statistics import GetProfitDataRequest
 | 
				
			||||||
 | 
					from services.statistics import StatisticsService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def main():
 | 
					async def main():
 | 
				
			||||||
    session: AsyncSession = session_maker()
 | 
					    session: AsyncSession = session_maker()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request = GetProfitDataRequest(
 | 
				
			||||||
 | 
					        date_range=(
 | 
				
			||||||
 | 
					            datetime.date(2020, 1, 1),
 | 
				
			||||||
 | 
					            datetime.date(2020, 1, 31),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        service = BillingService(session)
 | 
					        service = StatisticsService(session)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        pdf_file = await service.create_billing_document_pdf(121)
 | 
					        result = await service.get_profit_data(request)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        with open("report.pdf", "wb") as f:
 | 
					        # for res in result:
 | 
				
			||||||
            f.write(pdf_file.getvalue())
 | 
					        #     print(res)
 | 
				
			||||||
    finally:
 | 
					    finally:
 | 
				
			||||||
        await session.close()
 | 
					        await session.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user