Skip to content

Commit b154e18

Browse files
committed
refactor Methods class, move build and fetch functions to app_setup
1 parent 55cf00d commit b154e18

10 files changed

Lines changed: 94 additions & 54 deletions

File tree

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import csv
22

3-
from backend.utils.methods_handler import Methods
43

5-
6-
def write_launch_info_csv(
4+
def write_launch_info_to_csv(
75
*,
8-
methods: list[str],
9-
dataset_methods: Methods,
6+
parsed_methods: list[str],
107
output_file: str,
118
) -> None:
12-
parsed = dataset_methods.get_methods_from_selection(methods)
13-
149
with open(output_file, "w") as csvfile:
1510
writer = csv.writer(csvfile)
1611
writer.writerow(["dll", "method"])
1712

18-
for item in parsed:
13+
for item in parsed_methods:
1914
if "," in item:
2015
dll, method = item.split(",", 1)
2116
writer.writerow([dll, method])

backend/file_utils/files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from fastapi import UploadFile
44

55

6-
def save_upload_file(file: UploadFile, dst: str) -> None:
7-
os.makedirs(os.path.dirname(dst), exist_ok=True)
8-
with open(dst, "wb") as f:
6+
def save_upload_file(file: UploadFile, dest_filepath: str) -> None:
7+
os.makedirs(os.path.dirname(dest_filepath), exist_ok=True)
8+
with open(dest_filepath, "wb") as f:
99
while chunk := file.file.read(1024 * 1024):
1010
f.write(chunk)
1111

backend/launch_service/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import os
22
import subprocess
33
import requests
4-
from backend.config.paths import RESOURCES_DIR, DATASET_FILE, DOCKER_DIR
4+
from backend.config.paths import (
5+
RESOURCES_DIR,
6+
DATASET_FILE,
7+
DOCKER_DIR,
8+
METHODS_TS_FILE,
9+
)
10+
from backend.utils.methods_handler import Methods
511

612
IMAGE_NAME = "pysymgym-test"
713

@@ -21,12 +27,21 @@ def fetch_dataset():
2127

2228
def build_container():
2329
print(f"Building Docker image '{IMAGE_NAME}'...")
24-
subprocess.run(["docker", "build", "--no-cache", "-t", IMAGE_NAME, DOCKER_DIR], check=True)
30+
subprocess.run(
31+
["docker", "build", "--no-cache", "-t", IMAGE_NAME, DOCKER_DIR], check=True
32+
)
2533
print("Docker build completed.\n")
2634

2735
os.makedirs(RESOURCES_DIR, exist_ok=True)
2836

2937

38+
def update_frontend_selection_options():
39+
print("Updating frontend selection options...")
40+
selection_tree = Methods.parse_dataset_file_for_front_selection(DATASET_FILE)
41+
Methods.write_selection_dataset_to_front_file(selection_tree, METHODS_TS_FILE)
42+
43+
3044
if __name__ == "__main__":
3145
build_container()
3246
fetch_dataset()
47+
update_frontend_selection_options()

backend/main.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
from fastapi import FastAPI, UploadFile, File, Form
33
from fastapi.middleware.cors import CORSMiddleware
44

5-
from backend.config.paths import (
6-
DATASET_FILE,
7-
METHODS_TS_FILE,
8-
)
5+
from backend.config.paths import METHODS_TS_FILE
96
from backend.utils.data_uploader import handle_upload
107
from backend.utils.methods_handler import Methods
118
from backend.utils.task import process_and_cleanup_task
129

13-
dataset_methods = Methods(data_filepath=DATASET_FILE, output_filepath=METHODS_TS_FILE)
10+
DATASET_DLLS_AND_METHODS = (
11+
Methods.get_dlls_with_all_methods_dict_from_front_options_file(METHODS_TS_FILE)
12+
)
1413

1514
app = FastAPI()
1615

@@ -24,15 +23,14 @@
2423

2524
@app.post("/api/upload")
2625
async def handle_submit(
27-
file: UploadFile = File(...),
28-
email: str = Form(...),
29-
methods: str = Form(...),
30-
experiment: str = Form(...),
26+
file: UploadFile = File(...),
27+
email: str = Form(...),
28+
methods: str = Form(...),
29+
experiment: str = Form(...),
3130
):
3231
task_uid = str(shortuuid.uuid())
3332

34-
handle_upload(task_uid, file, methods, dataset_methods)
35-
33+
handle_upload(task_uid, file, methods, DATASET_DLLS_AND_METHODS)
3634
process_and_cleanup_task.delay(task_uid, email, experiment, file.filename)
3735

3836
return {

backend/utils/data_uploader.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import json
2+
from collections import defaultdict
23

3-
from backend.file_utils.csv_methods_writer import write_launch_info_csv
4+
from fastapi import UploadFile
5+
6+
from backend.file_utils.csv_methods_writer import write_launch_info_to_csv
47
from backend.file_utils.files import save_upload_file
58
from backend.utils.methods_handler import Methods
69
from backend.config.paths import LAUNCH_INFO_FILE, MODEL_ONNX_FILE, get_thread_filepath
710

811

912
def handle_upload(
10-
uid,
11-
file,
13+
uid: str,
14+
file: UploadFile,
1215
methods: str,
13-
dataset_methods: Methods,
16+
dataset_dll_and_methods: defaultdict,
1417
) -> None:
15-
selection_methods = json.loads(methods)
18+
launch_methods = Methods.get_launch_info_list_from_selected(
19+
dataset_dll_and_methods, json.loads(methods)
20+
)
1621

1722
save_upload_file(file, get_thread_filepath(uid, MODEL_ONNX_FILE))
1823

19-
write_launch_info_csv(
20-
methods=selection_methods,
21-
dataset_methods=dataset_methods,
24+
write_launch_info_to_csv(
25+
parsed_methods=launch_methods,
2226
output_file=get_thread_filepath(uid, LAUNCH_INFO_FILE),
2327
)

backend/utils/docker_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ARTIFACTS_BASELINE_CSV_FILE,
1111
COMPSTRAT_RESULTS_DIR,
1212
)
13-
from docker.build_container import IMAGE_NAME
13+
from backend.launch_service.app_setup import IMAGE_NAME
1414

1515

1616
class DockerRunner(ABC):

backend/utils/methods_handler.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,42 @@
33

44

55
class Methods:
6-
def __init__(self, data_filepath, output_filepath):
6+
@staticmethod
7+
def write_selection_dataset_to_front_file(
8+
selection_dataset, front_selection_resource_path
9+
):
10+
with open(front_selection_resource_path, "w") as f:
11+
f.write("export const METHODS = ")
12+
json.dump(selection_dataset, f, indent=4)
13+
14+
@staticmethod
15+
def get_dlls_with_all_methods_dict_from_front_options_file(
16+
front_selection_resource_path,
17+
):
18+
with open(front_selection_resource_path, "r") as f:
19+
content = f.read()
20+
prefix = "export const METHODS = "
21+
json_str = content[len(prefix) :]
22+
selection_tree = json.loads(json_str)
23+
24+
dll_methods = defaultdict(list)
25+
for node in selection_tree:
26+
dll_name = node["title"] + ".dll"
27+
dll_methods[dll_name] = []
28+
for child in node["children"]:
29+
dll_methods[dll_name].append(child["value"])
30+
return dll_methods
31+
32+
@staticmethod
33+
def parse_dataset_file_for_front_selection(data_filepath):
734
with open(data_filepath, "r") as f:
835
data = json.load(f)
936

10-
self.dll_methods = defaultdict(list)
1137
groups = defaultdict(list)
1238
for item in data:
1339
groups[item["AssemblyFullName"]].append(item)
14-
self.dll_methods[item["AssemblyFullName"]] = []
1540

16-
tree = []
41+
dataset_tree = []
1742
for assembly, items in groups.items():
1843
node = {"title": assembly[:-4], "value": assembly, "children": []}
1944
seen_children = set()
@@ -22,21 +47,18 @@ def __init__(self, data_filepath, output_filepath):
2247
if name in seen_children:
2348
continue
2449
seen_children.add(name)
25-
dll_method = f"{assembly},{name}"
26-
self.dll_methods[assembly].append(dll_method)
27-
child_node = {"title": name, "value": dll_method}
50+
dll_and_method = f"{assembly},{name}"
51+
child_node = {"title": name, "value": dll_and_method}
2852
node["children"].append(child_node)
29-
tree.append(node)
30-
31-
with open(output_filepath, "w") as f:
32-
f.write("export const METHODS = ")
33-
json.dump(tree, f, indent=4)
53+
dataset_tree.append(node)
54+
return dataset_tree
3455

35-
def get_methods_from_selection(self, selection_list):
36-
selected_methods = []
37-
for item in selection_list:
38-
if item in self.dll_methods.keys():
39-
selected_methods.extend(self.dll_methods[item])
56+
@staticmethod
57+
def get_launch_info_list_from_selected(dll_methods: defaultdict, selected):
58+
launch_info_methods = []
59+
for item in selected:
60+
if item in dll_methods.keys():
61+
launch_info_methods.extend(dll_methods[item])
4062
else:
41-
selected_methods.append(item)
42-
return selected_methods
63+
launch_info_methods.append(item)
64+
return launch_info_methods

backend/utils/task.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from celery import Celery
2+
23
from backend.utils.docker_runner import run_pipeline
34
from backend.utils.results_sender import send_folder_by_email
45
from backend.config.paths import RESULTS_DIR, get_thread_filepath, get_tmp_thread_files
@@ -10,7 +11,12 @@
1011

1112

1213
@celery_app.task
13-
def process_and_cleanup_task(task_uid: str, email: str, experiment: str, filename: str):
14+
def process_and_cleanup_task(
15+
task_uid: str,
16+
email: str,
17+
experiment: str,
18+
filename: str,
19+
):
1420
run_pipeline(task_uid)
1521

1622
send_folder_by_email(

frontend/src/components/ComparisonForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const ComparisonForm: React.FC = () => {
3737
});
3838
if (!res.ok) throw new Error('Upload failed');
3939
const data = await res.json();
40-
message.success('Data successfully submitted');
40+
message.success(data.message);
4141
console.log(data);
4242
} catch (err) {
4343
console.error(err);

0 commit comments

Comments
 (0)