feat: filters for profit statistics table

This commit is contained in:
2024-11-21 17:26:17 +04:00
parent c9be4f098e
commit 8733e6d94b
2 changed files with 31 additions and 26 deletions

View File

@@ -23,15 +23,17 @@ class ProfitTableDataItem(BaseSchema):
# region Requests
class GetProfitChartDataRequest(BaseSchema):
class CommonProfitFilters(BaseSchema):
date_range: Tuple[datetime.date, datetime.date]
client_id: int
base_marketplace_key: str
deal_status_id: int
class GetProfitChartDataRequest(CommonProfitFilters):
pass
class GetProfitTableDataRequest(BaseSchema):
date_range: Tuple[datetime.date, datetime.date]
class GetProfitTableDataRequest(CommonProfitFilters):
group_table_by: ProfitTableGroupBy
# endregion

View File

@@ -8,7 +8,7 @@ from enums.profit_table_group_by import ProfitTableGroupBy
from models import DealService, Deal, DealStatusHistory, DealProductService, DealProduct, Service, Client, \
ShippingWarehouse, BaseMarketplace
from schemas.statistics import GetProfitChartDataResponse, GetProfitChartDataRequest, ProfitChartDataItem, \
GetProfitTableDataResponse, GetProfitTableDataRequest, ProfitTableDataItem
GetProfitTableDataResponse, GetProfitTableDataRequest, ProfitTableDataItem, CommonProfitFilters
from services.base import BaseService
@@ -112,7 +112,7 @@ class StatisticsService(BaseService):
)
@staticmethod
def _apply_filters(request: GetProfitChartDataRequest, stmt_deal_services, stmt_deal_product_services):
def _apply_chart_filters(request: CommonProfitFilters, 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)
@@ -266,7 +266,7 @@ class StatisticsService(BaseService):
rows = result.all()
return rows
async def get_profit_chart_data(self, request: GetProfitChartDataRequest) -> GetProfitChartDataResponse:
async def get_data_grouped_by_date(self, request: CommonProfitFilters, is_chart: bool = True):
date_from, date_to = request.date_range
date_to += timedelta(days=1)
@@ -274,7 +274,7 @@ class StatisticsService(BaseService):
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
stmt_deal_product_services = self._get_stmt_product_services()
stmt_deal_services, stmt_deal_product_services = self._apply_filters(
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
request,
stmt_deal_services,
stmt_deal_product_services
@@ -283,26 +283,14 @@ class StatisticsService(BaseService):
rows = await self._get_data_rows_grouped_by_date(
stmt_deal_services, stmt_deal_product_services, sub_deals_dates
)
return self._fill_dates_gaps(rows, date_from, date_to, is_chart)
data = self._fill_dates_gaps(rows, date_from, date_to)
async def get_profit_chart_data(self, request: GetProfitChartDataRequest) -> GetProfitChartDataResponse:
data = await self.get_data_grouped_by_date(request)
return GetProfitChartDataResponse(data=data)
async def _get_table_grouped_by_dates(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse:
date_from, date_to = request.date_range
date_to += timedelta(days=1)
sub_deals_dates = self._get_sub_deals_created_at(date_from, date_to)
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
stmt_deal_product_services = self._get_stmt_product_services()
rows = await self._get_data_rows_grouped_by_date(
stmt_deal_services, stmt_deal_product_services, sub_deals_dates
)
data = self._fill_dates_gaps(rows, date_from, date_to, False)
data = await self.get_data_grouped_by_date(request, False)
return GetProfitTableDataResponse(data=data)
async def _table_data_from_stmt(self, stmt) -> GetProfitTableDataResponse:
@@ -323,7 +311,7 @@ class StatisticsService(BaseService):
date_from, date_to = request.date_range
date_to += timedelta(days=1)
sub_deals_dates = self._get_sub_deals_created_at(date_from, date_to)
sub_deals_dates = self._get_deals_dates(request.deal_status_id, date_from, date_to)
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
@@ -342,6 +330,12 @@ class StatisticsService(BaseService):
.join(Deal, Deal.id == stmt_deal_product_services.c.deal_id)
)
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
request,
stmt_deal_services,
stmt_deal_product_services
)
sub_union = union_all(stmt_deal_services, stmt_deal_product_services).subquery()
sub_grouped_by_deals = self._group_by_deals(sub_union)
@@ -360,9 +354,18 @@ class StatisticsService(BaseService):
sub_deals_dates = self._get_filtered_sub_status_history(date_from, date_to)
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
sub_deal_product_services = self._get_stmt_product_services().subquery()
stmt_deal_product_services = self._get_stmt_product_services()
stmt_join_deals_statuses = self._get_joined_deals_and_statuses(sub_deal_product_services, sub_deals_dates)
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
request,
stmt_deal_services,
stmt_deal_product_services
)
stmt_join_deals_statuses = self._get_joined_deals_and_statuses(
stmt_deal_product_services.subquery(),
sub_deals_dates
)
sub_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery()