From 1402b1a1e3e95d7fda4c3f1fc7e5e787d0409e13 Mon Sep 17 00:00:00 2001 From: han0 Date: Thu, 5 Jun 2025 18:26:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E8=BF=91=E5=8D=8A=E5=B9=B4=E5=9D=87=E4=BB=B7?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/api/components/data.py | 3 ++- web/collectors/__init__.py | 9 ++++++--- web/commons/models/price_publish.py | 7 +++++-- web/tasks/once/collect.py | 7 +++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/web/api/components/data.py b/web/api/components/data.py index fa4e8eb..ecf9cde 100644 --- a/web/api/components/data.py +++ b/web/api/components/data.py @@ -38,6 +38,7 @@ def refresh_task(task_id): class CalculateQuery(BaseModel): year: int = Field(title='年份') month: int = Field(title='月份') + only_avg: int = Field(title='是否只计算均值') @data.route('/calculate', methods=['GET']) @@ -51,6 +52,6 @@ def start_calculate(query: CalculateQuery): @data.route('/collect', methods=['GET']) @siwa.doc(tags=[""], summary='触发计算任务', query=CalculateQuery) def start_collect(query: CalculateQuery): - collect(year=query.year, month=query.month) + collect(year=query.year, month=query.month, only_avg=query.only_avg) return Response() diff --git a/web/collectors/__init__.py b/web/collectors/__init__.py index 88bbbf2..77610f0 100644 --- a/web/collectors/__init__.py +++ b/web/collectors/__init__.py @@ -12,7 +12,8 @@ from commons.models.material import Material class Collector: - def __init__(self, year, month, force=True): + def __init__(self, year, month, force=True, only_avg=0): + self.only_avg = only_avg self.year = year self.month = month self.force = True # todo-2 已发布的价格不在覆盖计算 @@ -120,9 +121,11 @@ class Collector: def run(self): # 当月价 - # self.get_from_survey() - self.get_from_result() + if not self.only_avg: + PricePublish.clean(self.year, self.month, type=1) # 清空当月价 + self.get_from_result() # 近半年平均价 + PricePublish.clean(self.year, self.month, type=2) # 清空近半年平均价 self.get_avg() diff --git a/web/commons/models/price_publish.py b/web/commons/models/price_publish.py index a741197..f3f2068 100644 --- a/web/commons/models/price_publish.py +++ b/web/commons/models/price_publish.py @@ -41,8 +41,11 @@ class PricePublish(db.Model, Model, OperationTrackMixin, BaseModelMixin): ) @classmethod - def clean(cls, year, month): - cls.query.filter(cls.year == year, cls.month == month).delete() + def clean(cls, year, month, type=None): + query = cls.query.filter(cls.year == year, cls.month == month) + if type: + query.filter(cls.type == type) + query.delete() @classmethod def get_by_key(cls, name, year, month, type): diff --git a/web/tasks/once/collect.py b/web/tasks/once/collect.py index bce369d..bb7226a 100644 --- a/web/tasks/once/collect.py +++ b/web/tasks/once/collect.py @@ -2,12 +2,11 @@ from collectors import Collector from commons.models.price_publish import PricePublish -def collect(year=2023, month=11): +def collect(year=2023, month=11, only_avg=0): """ 整理发布价格 """ - PricePublish.clean(year, month) # 清空当月数据 - collector = Collector(year, month) + collector = Collector(year, month, only_avg=only_avg) collector.run() @@ -15,7 +14,7 @@ if __name__ == '__main__': from core.factory import ClientApp with ClientApp().app_context(): - collect(2025, 4) + collect(2025, 5) # for i in range(4): # collect(2025, i + 1)