Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import uuid
- import numpy as np
- import pandas as pd
- from config.settings import BASE_DIR
- from forecasts.clickhouse.db import get_session
- from .replenishment_formulas_for_result import FormulasCalculation
- from replen_optimizer.clickhouse.replen_optimizer_results.services.replen_result_creator import ReplenishmentResultCreator
- from replen_optimizer.clickhouse.replen_optimizer_results.services.replen_result_creator import ReplenishmentOptimizerResultCreator
- from replen_optimizer.clickhouse.replen_optimizer_results.services.db_repository import ReplenishmentOptimizerResultDataClickhouse
- class ReplenishmentOutput:
- def __init__(self, *args, **kwargs) -> None:
- self.scenario = kwargs.get("scenario")
- def add_result_optimizer_data(self):
- result_data_frame = f'{BASE_DIR}/storage/replen_optimizer/result/result.csv'
- result_data_frame = pd.read_csv(result_data_frame)
- result_data_frame.columns = [
- 'artcode_nm',
- 'hub_nm',
- 'region_nm',
- 'transfer',
- 'region_target',
- 'region_stock',
- 'priority',
- ]
- result_data_frame = result_data_frame.rename(columns={
- "artcode_nm": "art_code_nm",
- })
- result_data_frame['scenario_id'] = self.scenario.pk
- result_data_frame['id'] = [uuid.uuid4() for _ in range(len(result_data_frame.index))]
- with get_session() as session:
- data_creator = ReplenishmentOptimizerResultCreator(
- session=session,
- df=result_data_frame,
- result_id=self.scenario.id,
- )
- data_creator.execute()
- def calculate_result(self) -> pd.DataFrame:
- target_stock_hub = f'{BASE_DIR}/storage/replen_optimizer/inputs/in_target_hub.csv' # TODO: bump
- target_stock_hub = pd.read_csv(target_stock_hub)
- target_stock_hub.columns = [
- 'hub_nm',
- 'artcode_nm',
- 'target_stock_hub',
- 'min_target_stock',
- ]
- target_stock_reg = f'{BASE_DIR}/storage/replen_optimizer/inputs/in_target_reg.csv' # TODO: bump
- target_stock_reg = pd.read_csv(target_stock_reg)
- target_stock_reg.columns = [
- 'region_nm',
- 'artcode_nm',
- 'target_stock_reg',
- ]
- with get_session() as session:
- replenishment_optimizer_result_data = ReplenishmentOptimizerResultDataClickhouse(
- session=session,
- )
- source_stock = replenishment_optimizer_result_data.get_source_stock(
- result_id=self.scenario.id,
- )
- destination_stock = replenishment_optimizer_result_data.get_destination_stock(
- result_id=self.scenario.id,
- )
- forecasts_hub = replenishment_optimizer_result_data.get_forecasts_hub(
- result_id=self.scenario.id,
- )
- forecasts_region = replenishment_optimizer_result_data.get_forecasts_region(
- result_id=self.scenario.id,
- )
- result_data_frame = replenishment_optimizer_result_data.get_result_data_optimizer(
- result_id=self.scenario.id,
- )
- calculated_data_frame = pd.DataFrame()
- calculated_data_frame['id'] = [uuid.uuid4() for _ in range(len(result_data_frame.index))]
- calculated_data_frame['scenario_id'] = self.scenario.pk
- calculated_data_frame['art_code_nm'] = result_data_frame['art_code_nm']
- calculated_data_frame['hub_nm'] = result_data_frame['hub_nm']
- calculated_data_frame['region_nm'] = result_data_frame['region_nm']
- # HUBS Forecast
- calculated_data_frame['source_stock'] = result_data_frame.apply(lambda row: FormulasCalculation._found_source_stock(row, source_stock), axis=1)
- calculated_data_frame['manual_correction'] = 0
- calculated_data_frame['calculated_transfer'] = result_data_frame["transfer"]
- calculated_data_frame['final_transfer'] = calculated_data_frame['calculated_transfer'] + calculated_data_frame['manual_correction']
- calculated_data_frame['fc_1_hub'] = result_data_frame.apply(lambda row: FormulasCalculation._found_forecasts_hub_sum_fc_1(row, forecasts_hub), axis=1)
- calculated_data_frame['cover_source'] = calculated_data_frame['source_stock'] / (calculated_data_frame['fc_1_hub'] * 30)
- calculated_data_frame['final_source_stock'] = calculated_data_frame['source_stock'] - result_data_frame.apply(lambda row: FormulasCalculation._found_sum_result_data_hub(row, result_data_frame), axis=1)
- calculated_data_frame['final_cover_source'] = calculated_data_frame['final_source_stock'] / (calculated_data_frame['fc_1_hub'] * 30)
- calculated_data_frame['target_cover_source'] = result_data_frame.apply(lambda row: FormulasCalculation._found_target_stock_hub(row, target_stock_hub), axis=1) / (calculated_data_frame['fc_1_hub'] * 30)
- # REGION Forecast
- calculated_data_frame['fc_1_region'] = result_data_frame.apply(lambda row: FormulasCalculation._found_forecasts_region_sum_fc_1(row, forecasts_region), axis=1)
- calculated_data_frame['destination_stock'] = result_data_frame.apply(lambda row: FormulasCalculation._found_destination_stock(row, destination_stock), axis=1)
- calculated_data_frame['final_destination_stock'] = calculated_data_frame['destination_stock'] + result_data_frame.apply(lambda row: FormulasCalculation._found_sum_result_data_region(row, result_data_frame), axis=1)
- calculated_data_frame['cover_destination'] = calculated_data_frame['destination_stock'] / (calculated_data_frame['fc_1_region'] * 30)
- calculated_data_frame['final_cover_destination'] = calculated_data_frame['final_destination_stock'] / (calculated_data_frame['fc_1_region'] * 30)
- calculated_data_frame['target_cover_destination'] = result_data_frame.apply(lambda row: FormulasCalculation._found_target_stock_reg(row, target_stock_reg), axis=1) / (calculated_data_frame['fc_1_region'] * 30)
- calculated_data_frame.fillna(0, inplace=True)
- calculated_data_frame = calculated_data_frame.drop('fc_1_hub', axis=1)
- calculated_data_frame = calculated_data_frame.drop('fc_1_region', axis=1)
- calculated_data_frame.replace([np.inf, -np.inf], 0, inplace=True)
- calculated_data_frame["cover_source"] = calculated_data_frame["cover_source"].astype(np.int32)
- calculated_data_frame["final_cover_source"] = calculated_data_frame["final_cover_source"].astype(np.int32)
- calculated_data_frame["target_cover_source"] = calculated_data_frame["target_cover_source"].astype(np.int32)
- calculated_data_frame["cover_destination"] = calculated_data_frame["cover_destination"].astype(np.int32)
- calculated_data_frame["final_cover_destination"] = calculated_data_frame["final_cover_destination"].astype(np.int32)
- calculated_data_frame["target_cover_destination"] = calculated_data_frame["target_cover_destination"].astype(np.int32)
- data_creator = ReplenishmentResultCreator(
- session=session,
- df=calculated_data_frame,
- result_id=self.scenario.id,
- )
- data_creator.execute()
- def get_queryset(self, query: dict) -> json:
- with get_session() as session:
- replenishment_optimizer_result_data = ReplenishmentOptimizerResultDataClickhouse(
- session=session,
- )
- result_data = replenishment_optimizer_result_data.get_data_aggregations(
- result_id=self.scenario.id,
- query_params=query,
- )
- return result_data.to_dict("records")
- def get_queryset_filter(self, query: dict) -> json:
- with get_session() as session:
- replenishment_optimizer_result_data = ReplenishmentOptimizerResultDataClickhouse(
- session=session,
- )
- result_data = replenishment_optimizer_result_data.get_queryset_filter_data(
- result_id=self.scenario.id,
- query_params=query,
- )
- return result_data.to_dict("records")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement