Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions code/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cosmotech-run-orchestrator~=2.0.2
azure-storage-blob
CosmoTech-Acceleration-Library @ git+https://github.com/Cosmo-Tech/CosmoTech-Acceleration-Library@feature/cosmotech_api_5.0
cosmotech-api~=5.0.0b3
cosmoTech-acceleration-library @ git+https://github.com/Cosmo-Tech/CosmoTech-Acceleration-Library@2.1.0-rc1
cosmotech-api~=5.0.0rc5
faker~=30.6.0
psycopg2-binary==2.9.*
numpy~=2.3.2
83 changes: 83 additions & 0 deletions code/run_templates/brewery/parameters_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import json
import shutil
import csv
from pathlib import Path

from cosmotech.coal.utils.configuration import ENVIRONMENT_CONFIGURATION as EC
from cosmotech.orchestrator.utils.logger import get_logger

LOGGER = get_logger('parameters_handler')


def update_first_row_in_csv(csv_path, updated_values):
"""
Read a CSV file and change the first data row with new values.

Args:
csv_path: Path to the CSV file
updated_values: Dictionary with column names as keys and new values
"""
# Read the CSV file
with open(csv_path, 'r', newline='') as f:
reader = csv.DictReader(f)
rows = list(reader)
fieldnames = reader.fieldnames

# Update the first row with new values
if rows:
for key, value in updated_values.items():
if key in rows[0]:
rows[0][key] = value

# Write back to the CSV file
with open(csv_path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)


def read_parameters():
with open(
Path(EC.cosmotech.parameters_absolute_path) / "parameters.json"
) as f:
parameters = {d["parameterId"]: d["value"] for d in json.loads(f.read())}

LOGGER.info("Parameters loaded from JSON")
return parameters


def fetch_parameter_file_path(param_name: str) -> Path:
for r, d, f in os.walk(EC.cosmotech.parameters_absolute_path):
if param_name in r:
return Path(r) / f[0]
raise FileNotFoundError(f"Parameter file for {param_name} not found.")


def main():
LOGGER.info("Starting parameter handler")

# get_parameter from json
parameters = read_parameters()
# update dataset Bar.csv with param
updated_values = {}
if "NbWaiters" in parameters:
updated_values["NbWaiters"] = parameters["NbWaiters"]
if "RestockQty" in parameters:
updated_values["RestockQty"] = parameters["RestockQty"]
if "Stock" in parameters:
updated_values["Stock"] = parameters["Stock"]

bar_data_path = Path(EC.cosmotech.dataset_absolute_path) / "Bar.csv"
update_first_row_in_csv(bar_data_path, updated_values)
LOGGER.info("Updated Bar.csv with parameters")

# replace bar.csv if file is provided as parameter
bar_param_path = fetch_parameter_file_path("initial_stock_dataset") / "initial_stock_dataset"
if bar_data_path.exists():
# replace dataset Bar.csv with parameter Bar.csv
shutil.copy(bar_param_path, bar_data_path)
LOGGER.info("Bar.csv replaced by given file")


if __name__ == "__main__":
main()
53 changes: 53 additions & 0 deletions code/run_templates/brewery/run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"steps": [
{
"id": "fetch_parameters",
"command": "csm-data",
"arguments": ["api", "run-load-data"],
"useSystemEnvironment": true
},
{
"id": "parameters_handler",
"command": "python3",
"arguments": [
"code/run_templates/brewery/parameters_handler.py"
],
"precedents": [
"fetch_parameters"
],
"useSystemEnvironment": true
},
{
"id": "engine",
"command": "csm-simulator",
"arguments": [
"-i",
"BreweryDemoSimulationWithConnector"
],
"precedents": [
"parameters_handler"
],
"useSystemEnvironment": true
},
{
"id": "load_results_to_store",
"command": "csm-data",
"arguments": [
"store",
"load-csv-folder"
],
"precedents": ["engine"],
"useSystemEnvironment": true
},
{
"id": "send_results_to_psql",
"command": "csm-data",
"arguments": [
"store",
"output"
],
"precedents": ["load_results_to_store"],
"useSystemEnvironment": true
}
]
}
1 change: 0 additions & 1 deletion code/run_templates/common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from cosmotech.coal.cosmotech_api.connection import get_api_client
from cosmotech.coal.utils.logger import get_logger as _get_logger


LOGGER = _get_logger("my_etl_logger")


Expand Down
4 changes: 2 additions & 2 deletions code/run_templates/common/download_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

_d_data = download_dataset(ORG_ID, WS_ID, dataset_id, False)

tmp_path = pathlib.Path(_d_data['folder_path'])
tmp_path = pathlib.Path(_d_data["folder_path"])

for _p in tmp_path.glob("*"):
shutil.copy(_p, SIM_DATA_PATH)

LOGGER.info(f"Downloaded dataset {dataset_id}")
LOGGER.info(f"Downloaded dataset {dataset_id}")
Loading