Advertisement
Mochinov

Untitled

May 18th, 2023
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. import json
  2. import uuid
  3. import numpy as np
  4. import pandas as pd
  5. from copy import deepcopy
  6.  
  7. from config.settings import BASE_DIR
  8. from forecasts.clickhouse.db import get_session
  9. from .replenishment_formulas_for_result import FormulasCalculation
  10. from replen_optimizer.clickhouse.replen_optimizer_results.services.replen_result_creator import ReplenishmentResultCreator
  11. from replen_optimizer.clickhouse.replen_optimizer_results.services.replen_result_creator import ReplenishmentOptimizerResultCreator
  12. from replen_optimizer.clickhouse.replen_optimizer_results.services.db_repository import ReplenishmentOptimizerResultDataClickhouse
  13.  
  14.  
  15. class ReplenishmentOutput:
  16.  
  17. def __init__(self, *args, **kwargs) -> None:
  18. self.scenario = kwargs.get("scenario")
  19.  
  20. def get_result_output_data(self) -> json:
  21.  
  22. with get_session() as session:
  23.  
  24. replenishment_optimizer_result_data = ReplenishmentOptimizerResultDataClickhouse(
  25. session=session,
  26. )
  27. result_data = replenishment_optimizer_result_data.get_result_data(
  28. result_id=self.scenario.id,
  29. )[1:100]
  30. return result_data.to_dict("records")
  31.  
  32. def add_result_optimizer_data(self):
  33.  
  34. result_data_frame = f'{BASE_DIR}/storage/replen_optimizer/result/scenario/{self.scenario.id}/result.csv'
  35. result_data_frame = pd.read_csv(result_data_frame)
  36. result_data_frame.columns = [
  37. 'artcode_nm',
  38. 'hub_nm',
  39. 'region_nm',
  40. 'transfer',
  41. 'region_target',
  42. 'region_stock',
  43. 'priority',
  44. ]
  45. result_data_frame = result_data_frame.rename(columns={
  46. "artcode_nm": "art_code_nm",
  47. })
  48.  
  49. result_data_frame['scenario_id'] = self.scenario.pk
  50. result_data_frame['id'] = [uuid.uuid4() for _ in range(len(result_data_frame.index))]
  51.  
  52. with get_session() as session:
  53. data_creator = ReplenishmentOptimizerResultCreator(
  54. session=session,
  55. df=result_data_frame,
  56. result_id=self.scenario.id,
  57. )
  58. data_creator.execute()
  59.  
  60.  
  61. def calculate_result(self) -> pd.DataFrame:
  62. target_stock_hub = f'{BASE_DIR}/storage/replen_optimizer/inputs/in_target_hub.csv' # TODO: bump
  63. target_stock_hub = pd.read_csv(target_stock_hub)
  64. target_stock_hub.columns = [
  65. 'hub_nm',
  66. 'artcode_nm',
  67. 'target_stock_hub',
  68. 'min_target_stock',
  69. ]
  70.  
  71. target_stock_reg = f'{BASE_DIR}/storage/replen_optimizer/inputs/in_target_reg.csv' # TODO: bump
  72. target_stock_reg = pd.read_csv(target_stock_reg)
  73. target_stock_reg.columns = [
  74. 'region_nm',
  75. 'artcode_nm',
  76. 'target_stock_reg',
  77. ]
  78.  
  79.  
  80. with get_session() as session:
  81. replenishment_optimizer_result_data = ReplenishmentOptimizerResultDataClickhouse(
  82. session=session,
  83. )
  84. source_stock = replenishment_optimizer_result_data.get_source_stock(
  85. result_id=self.scenario.id,
  86. )
  87. destination_stock = replenishment_optimizer_result_data.get_destination_stock(
  88. result_id=self.scenario.id,
  89. )
  90. forecasts_hub = replenishment_optimizer_result_data.get_forecasts_hub(
  91. result_id=self.scenario.id,
  92. )
  93. forecasts_region = replenishment_optimizer_result_data.get_forecasts_region(
  94. result_id=self.scenario.id,
  95. )
  96. result_data_frame = replenishment_optimizer_result_data.get_result_data_optimizer(
  97. result_id=self.scenario.id,
  98. )
  99. calculated_data_frame = pd.DataFrame()
  100. calculated_data_frame['id'] = [uuid.uuid4() for _ in range(len(result_data_frame.index))]
  101. calculated_data_frame['scenario_id'] = self.scenario.pk
  102. calculated_data_frame['art_code_nm'] = result_data_frame['art_code_nm']
  103. calculated_data_frame['hub_nm'] = result_data_frame['hub_nm']
  104. calculated_data_frame['region_nm'] = result_data_frame['region_nm']
  105.  
  106. # HUBS Forecast
  107. calculated_data_frame['source_stock'] = result_data_frame.apply(lambda row: FormulasCalculation._found_source_stock(row, source_stock), axis=1)
  108. calculated_data_frame['manual_correction'] = 0
  109. calculated_data_frame['calculated_transfer'] = result_data_frame["transfer"]
  110. calculated_data_frame['final_transfer'] = calculated_data_frame['calculated_transfer'] + calculated_data_frame['manual_correction']
  111. calculated_data_frame['fc_1_hub'] = result_data_frame.apply(lambda row: FormulasCalculation._found_forecasts_hub_sum_fc_1(row, forecasts_hub), axis=1)
  112. calculated_data_frame['cover_source'] = calculated_data_frame['source_stock'] / calculated_data_frame['fc_1_hub'] * 30
  113. 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)
  114. calculated_data_frame['final_cover_source'] = calculated_data_frame['final_source_stock'] / calculated_data_frame['fc_1_hub'] * 30
  115. 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
  116.  
  117. # REGION Forecast
  118. calculated_data_frame['fc_1_region'] = result_data_frame.apply(lambda row: FormulasCalculation._found_forecasts_region_sum_fc_1(row, forecasts_region), axis=1)
  119. calculated_data_frame['destination_stock'] = result_data_frame.apply(lambda row: FormulasCalculation._found_destination_stock(row, destination_stock), axis=1)
  120. 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)
  121. calculated_data_frame['cover_destination'] = calculated_data_frame['destination_stock'] / calculated_data_frame['fc_1_region'] * 30
  122. calculated_data_frame['final_cover_destination'] = calculated_data_frame['final_destination_stock'] / calculated_data_frame['fc_1_region'] * 30
  123. 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
  124.  
  125. calculated_data_frame = calculated_data_frame.drop('fc_1_hub', axis=1)
  126. calculated_data_frame = calculated_data_frame.drop('fc_1_region', axis=1)
  127. calculated_data_frame.replace(np.nan, 0)
  128. calculated_data_frame.replace([np.inf, -np.inf], 0, inplace=True)
  129. calculated_data_frame["cover_source"] = calculated_data_frame["cover_source"].astype(int, errors='ignore')
  130. calculated_data_frame["final_cover_source"] = calculated_data_frame["final_cover_source"].astype(int, errors='ignore')
  131. calculated_data_frame["target_cover_source"] = calculated_data_frame["target_cover_source"].astype(int)
  132. calculated_data_frame["cover_destination"] = calculated_data_frame["cover_destination"].astype(int, errors='ignore')
  133. calculated_data_frame["final_cover_destination"] = calculated_data_frame["final_cover_destination"].astype(int, errors='ignore')
  134. calculated_data_frame["target_cover_destination"] = calculated_data_frame["target_cover_destination"].astype(int, errors='ignore')
  135.  
  136. data_creator = ReplenishmentResultCreator(
  137. session=session,
  138. df=calculated_data_frame,
  139. result_id=self.scenario.id,
  140. )
  141. data_creator.execute()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement