[feat] negodata: 통계 인상억제율(재협상)·월별 차트 — 직전 라운드 투찰가 대비 인하율 파생 집계
This commit is contained in:
1 parent
e7a51b1b75
commit
e8d2acaade
10 files changed
+172
-4
No files matched your search
@@ -2,6 +2,7 @@ from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
|
||||
from sqlalchemy import select, func, and_, case
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
@@ -54,6 +55,14 @@ class IStatisticsCRUD(ABC):
|
||||
async def regen_avg_round(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def markup_suppression(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def markup_suppression_monthly(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def card_usage(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
@@ -187,6 +196,87 @@ class StatisticsCRUD(IStatisticsCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, 0.0
|
||||
|
||||
async def markup_suppression(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
# 인상억제율(재협상 전용): 같은 견적번호(qt_number)의 직전 라운드 투찰가 대비 이번 라운드 투찰가가
|
||||
# 얼마나 안 올랐나 = avg((직전투찰 − 이번투찰) / 직전투찰). 양수=인하(억제 성공), 음수=인상 허용.
|
||||
# 직전·이번 둘 다 유효 투찰(bid_price)이 있는 재협상 쌍만 대상(직전이 개찰/거부면 비교 불가 → 제외).
|
||||
# 새 컬럼 없이 sessions.qt_number+qt_round+bid_price 로만 파생.
|
||||
try:
|
||||
prev = aliased(sessions)
|
||||
stmt = (
|
||||
select(func.avg((prev.bid_price - sessions.bid_price) * 1.0 / prev.bid_price))
|
||||
.select_from(sessions)
|
||||
.join(
|
||||
prev,
|
||||
and_(
|
||||
prev.qt_number == sessions.qt_number,
|
||||
prev.item_id == sessions.item_id,
|
||||
prev.supplier_id == sessions.supplier_id,
|
||||
prev.qt_round == sessions.qt_round - 1,
|
||||
prev.bid_price.isnot(None),
|
||||
prev.bid_price > 0,
|
||||
prev.deleted == False, # noqa: E712
|
||||
),
|
||||
)
|
||||
.join(quotations, quotations.qt_id == sessions.quotation_id)
|
||||
.where(
|
||||
and_(
|
||||
*_company_scope(company_id, owner),
|
||||
sessions.bid_price.isnot(None),
|
||||
sessions.qt_round >= 2,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
quotations.updated_at >= since,
|
||||
)
|
||||
)
|
||||
)
|
||||
err, rows = await DB_SESSION_MNG.execute(cdb, stmt)
|
||||
if err != ErrorType.SUCCESS:
|
||||
return err, 0.0
|
||||
val = rows[0] if rows else None
|
||||
return ErrorType.SUCCESS, float(val) if val is not None else 0.0
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, 0.0
|
||||
|
||||
async def markup_suppression_monthly(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
# 월별 인상억제율: 이번 라운드 마감월(quotations.updated_at)별 avg((직전투찰 − 이번투찰)/직전투찰).
|
||||
try:
|
||||
prev = aliased(sessions)
|
||||
month = func.to_char(quotations.updated_at, "YYYY-MM")
|
||||
stmt = (
|
||||
select(month.label("m"), func.avg((prev.bid_price - sessions.bid_price) * 1.0 / prev.bid_price))
|
||||
.select_from(sessions)
|
||||
.join(
|
||||
prev,
|
||||
and_(
|
||||
prev.qt_number == sessions.qt_number,
|
||||
prev.item_id == sessions.item_id,
|
||||
prev.supplier_id == sessions.supplier_id,
|
||||
prev.qt_round == sessions.qt_round - 1,
|
||||
prev.bid_price.isnot(None),
|
||||
prev.bid_price > 0,
|
||||
prev.deleted == False, # noqa: E712
|
||||
),
|
||||
)
|
||||
.join(quotations, quotations.qt_id == sessions.quotation_id)
|
||||
.where(
|
||||
and_(
|
||||
*_company_scope(company_id, owner),
|
||||
sessions.bid_price.isnot(None),
|
||||
sessions.qt_round >= 2,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
quotations.updated_at >= since,
|
||||
)
|
||||
)
|
||||
.group_by(month)
|
||||
.order_by(month)
|
||||
)
|
||||
err, rows = await DB_SESSION_MNG.execute(cdb, stmt)
|
||||
return (err, list(rows) if err == ErrorType.SUCCESS else [])
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def card_usage(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
# 카드 유형별 사용 빈도: card_used_yn=True 채팅을 card_type 별 집계(협상형 견적에서만 채팅 생성).
|
||||
try:
|
||||
|
||||
@@ -15,12 +15,18 @@ class StatKpi(WebPacketProtocol):
|
||||
savings_delta_mom: int = 0 # 전월 대비 절감액 증감
|
||||
closed_count: int = 0 # 마감 견적 수(창)
|
||||
regen_avg_round: float = 0.0 # 평균 재견적 라운드
|
||||
markup_suppression_rate: float = 0.0 # 인상억제율(재협상: 직전 라운드 투찰가 대비 이번 투찰가 인하율, 파생)
|
||||
|
||||
|
||||
class StatMonthPoint(WebPacketProtocol):
|
||||
month: str # 'YYYY-MM'
|
||||
savings: int = 0
|
||||
rate: float = 0.0
|
||||
rate: float = 0.0 # 절감률
|
||||
|
||||
|
||||
class StatMarkupPoint(WebPacketProtocol):
|
||||
month: str # 'YYYY-MM'
|
||||
rate: float = 0.0 # 인상억제율(직전 라운드 투찰가 대비 인하율)
|
||||
|
||||
|
||||
class StatOutcome(WebPacketProtocol):
|
||||
@@ -60,6 +66,7 @@ class StatCardUsage(WebPacketProtocol):
|
||||
class StatScope(WebPacketProtocol):
|
||||
kpi: StatKpi = Field(default_factory=StatKpi)
|
||||
trend: list[StatMonthPoint] = []
|
||||
markup_trend: list[StatMarkupPoint] = [] # 월별 인상억제율(재협상)
|
||||
outcome: StatOutcome = Field(default_factory=StatOutcome)
|
||||
participation: StatParticipation = Field(default_factory=StatParticipation)
|
||||
type_split: list[StatTypeRow] = []
|
||||
|
||||
@@ -12,6 +12,7 @@ from router.v1.statistics.protocol import (
|
||||
StatScope,
|
||||
StatKpi,
|
||||
StatMonthPoint,
|
||||
StatMarkupPoint,
|
||||
StatOutcome,
|
||||
StatParticipation,
|
||||
StatTypeRow,
|
||||
@@ -51,19 +52,22 @@ class StatisticsService:
|
||||
type_rows = await self._read(lambda s: self.stat_crud.type_counts(s, company_uuid, owner_uuid, since))
|
||||
part_rows = await self._read(lambda s: self.stat_crud.participation_counts(s, company_uuid, owner_uuid, since))
|
||||
regen = await self._read_scalar(lambda s: self.stat_crud.regen_avg_round(s, company_uuid, owner_uuid, since))
|
||||
markup = await self._read_scalar(lambda s: self.stat_crud.markup_suppression(s, company_uuid, owner_uuid, since))
|
||||
markup_rows = await self._read(lambda s: self.stat_crud.markup_suppression_monthly(s, company_uuid, owner_uuid, since))
|
||||
card_rows = await self._read(lambda s: self.stat_crud.card_usage(s, company_uuid, owner_uuid, since))
|
||||
|
||||
scope.trend = self._trend(win_rows, labels)
|
||||
scope.markup_trend = [StatMarkupPoint(month=r[0], rate=float(r[1] or 0.0)) for r in markup_rows]
|
||||
scope.categories = self._categories(win_rows)
|
||||
scope.outcome = self._outcome(outcome_rows)
|
||||
scope.participation = self._participation(part_rows)
|
||||
scope.type_split = self._type_split(type_rows, win_rows)
|
||||
scope.cards = self._cards(card_rows)
|
||||
scope.kpi = self._kpi(win_rows, scope.trend, scope.outcome, regen)
|
||||
scope.kpi = self._kpi(win_rows, scope.trend, scope.outcome, regen, markup)
|
||||
return scope
|
||||
|
||||
# ── 파생 계산 ───────────────────────────────────────────────
|
||||
def _kpi(self, win_rows, trend, outcome, regen) -> StatKpi:
|
||||
def _kpi(self, win_rows, trend, outcome, regen, markup) -> StatKpi:
|
||||
k = StatKpi()
|
||||
total_saving = sum(int(r.target_price) - int(r.bid_price) for r in win_rows)
|
||||
total_target = sum(int(r.target_price) for r in win_rows)
|
||||
@@ -75,6 +79,7 @@ class StatisticsService:
|
||||
k.closed_count = closed
|
||||
k.award_rate = (outcome.awarded / closed) if closed else 0.0
|
||||
k.regen_avg_round = round(regen, 2)
|
||||
k.markup_suppression_rate = round(markup, 4) # 인상억제율(재협상 직전 라운드 투찰가 대비, 파생)
|
||||
# 전월 대비: 마지막 두 달 절감액 차(창에 2개월 미만이면 0).
|
||||
k.savings_delta_mom = (trend[-1].savings - trend[-2].savings) if len(trend) >= 2 else 0
|
||||
return k
|
||||
|
||||
Reference in new issue
Block a user