Advertisement
Mochinov

Untitled

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