From 1e341f5c629f6ead805b35a192f73aa4dcc6255c Mon Sep 17 00:00:00 2001 From: gitjannes Date: Tue, 17 Dec 2024 15:46:24 +0100 Subject: [PATCH] Moved filters into views_helper.py, deleted now obsolete filters.py, moved (wrongly placed by us) functions from run_v2.py to run.py --- protzilla/run.py | 63 ++++++++++++++++++++++++++++++++++++++++ protzilla/run_v2.py | 64 ----------------------------------------- ui/runs/filter.py | 22 -------------- ui/runs/views.py | 6 ++-- ui/runs/views_helper.py | 22 ++++++++++++++ 5 files changed, 87 insertions(+), 90 deletions(-) delete mode 100644 ui/runs/filter.py diff --git a/protzilla/run.py b/protzilla/run.py index 1f451e89..88429cb9 100644 --- a/protzilla/run.py +++ b/protzilla/run.py @@ -4,9 +4,14 @@ import threading import traceback +import os +import shutil +import datetime + import protzilla.constants.paths as paths from protzilla.steps import Messages, Output, Plots, Step from protzilla.utilities import format_trace +from protzilla.disk_operator import DiskOperator, YamlOperator def get_available_run_names() -> list[str]: @@ -19,6 +24,64 @@ def get_available_run_names() -> list[str]: if not directory.name.startswith(".") ] +def get_available_runinfo() -> tuple[list[dict[str, str | list[str]]], list[dict[str, str | list[str]]], set[str]]: + if not paths.RUNS_PATH.exists(): + return [] + runs = [] + runs_favourited = [] + all_tags = set() + for directory in paths.RUNS_PATH.iterdir(): + if directory.name.startswith("."): + continue + name = directory.name + creation_time = directory.stat().st_ctime + modification_time = directory.stat().st_mtime + + disk_operator = DiskOperator(name, "dummy_workflow_name") + directory_path = os.path.join(paths.RUNS_PATH, name) + run_yaml_path = os.path.join(directory_path, "run.yaml") + step_manager = disk_operator.read_run(run_yaml_path) + steps = step_manager.all_steps + step_names = [] + for step in steps: + step_names.append(step.display_name) + + tags = [] + metadata_yaml_path = os.path.join(directory_path, "metadata.yaml") + if os.path.isfile(metadata_yaml_path): + yaml_operator = YamlOperator() + metadata = yaml_operator.read(metadata_yaml_path) + tags = metadata.get("tags") + + for tag in tags: + all_tags.add(tag) + + run = { + "run_name": name, + "creation_date": datetime.datetime.fromtimestamp(creation_time).strftime("%d %m %Y"), #TODO: reutrn the pure datetime, convert in html) + "modification_date": datetime.datetime.fromtimestamp(modification_time).strftime("%d %m %Y"), + "memory_mode": step_manager.df_mode, + "run_steps" : step_names, + "favourite_status" : step_manager.favourite, + "run_tags": tags + } + + if step_manager.favourite: + runs_favourited.append(run) + else: + runs.append(run) + + for run in runs + runs_favourited: + possible_tags = list(all_tags - run["run_tags"]) + run["addable_tags"] = possible_tags + + return (runs, runs_favourited, all_tags) + +def delete_run_folder(run_name) -> None: + path = os.path.join(paths.RUNS_PATH, run_name) + + if os.path.isdir(path): + shutil.rmtree(path) class Run: class ErrorHandlingContextManager: diff --git a/protzilla/run_v2.py b/protzilla/run_v2.py index 100b771e..7cd813ac 100644 --- a/protzilla/run_v2.py +++ b/protzilla/run_v2.py @@ -4,14 +4,9 @@ import threading import traceback -import os -import shutil -import datetime - import protzilla.constants.paths as paths from protzilla.steps import Messages, Output, Plots, Step from protzilla.utilities import format_trace -from protzilla.disk_operator import DiskOperator, YamlOperator def get_available_run_names() -> list[str]: @@ -23,65 +18,6 @@ def get_available_run_names() -> list[str]: if not directory.name.startswith(".") ] -def get_available_runinfo() -> tuple[list[dict[str, str | list[str]]], list[dict[str, str | list[str]]], set[str]]: - if not paths.RUNS_PATH.exists(): - return [] - runs = [] - runs_favourited = [] - all_tags = set() - for directory in paths.RUNS_PATH.iterdir(): - if directory.name.startswith("."): - continue - name = directory.name - creation_time = directory.stat().st_ctime - modification_time = directory.stat().st_mtime - - disk_operator = DiskOperator(name, "dummy_workflow_name") - directory_path = os.path.join(paths.RUNS_PATH, name) - run_yaml_path = os.path.join(directory_path, "run.yaml") - step_manager = disk_operator.read_run(run_yaml_path) - steps = step_manager.all_steps - step_names = [] - for step in steps: - step_names.append(step.display_name) - - tags = [] - metadata_yaml_path = os.path.join(directory_path, "metadata.yaml") - if os.path.isfile(metadata_yaml_path): - yaml_operator = YamlOperator() - metadata = yaml_operator.read(metadata_yaml_path) - tags = metadata.get("tags") - - for tag in tags: - all_tags.add(tag) - - run = { - "run_name": name, - "creation_date": datetime.datetime.fromtimestamp(creation_time).strftime("%d %m %Y"), #TODO: reutrn the pure datetime, convert in html) - "modification_date": datetime.datetime.fromtimestamp(modification_time).strftime("%d %m %Y"), - "memory_mode": step_manager.df_mode, - "run_steps" : step_names, - "favourite_status" : step_manager.favourite, - "run_tags": tags - } - - if step_manager.favourite: - runs_favourited.append(run) - else: - runs.append(run) - - for run in runs + runs_favourited: - possible_tags = list(all_tags - run["run_tags"]) - run["addable_tags"] = possible_tags - - return (runs, runs_favourited, all_tags) - -def delete_run_folder(run_name) -> None: - path = os.path.join(paths.RUNS_PATH, run_name) - - if os.path.isdir(path): - shutil.rmtree(path) - class Run: class ErrorHandlingContextManager: diff --git a/ui/runs/filter.py b/ui/runs/filter.py deleted file mode 100644 index 4b302622..00000000 --- a/ui/runs/filter.py +++ /dev/null @@ -1,22 +0,0 @@ -def filter_for_name(runs, search_string) -> list[dict[str, str | list[str]]]: - return [run for run in runs if search_string in run["run_name"]] - -def filter_for_steps(runs, search_steps) -> list[dict[str, str | list[str]]]: - return [run for run in runs if all(step in run["run_steps"] for step in search_steps)] - -def filter_for_tags(runs, search_tags) -> list[dict[str, str | list[str]]]: - return [run for run in runs if all(tag in run["run_tags"] for tag in search_tags)] - -def filter_for_memory_mode(runs, memory_mode) -> list[dict[str, str | list[str]]]: - return [run for run in runs if run["memory_mode"] == memory_mode] - -def filter_runs(runs, filters) -> list[dict[str, str | list[str]]]: - if filters["name"]: - runs = filter_for_name(runs, filters["name"]) - if filters["steps"]: - runs = filter_for_steps(runs, filters["steps"]) - if filters["tags"]: - runs = filter_for_tags(runs, filters["tags"]) - if filters["memory_mode"]: - runs = filter_for_memory_mode(runs, filters["memory_mode"]) - return runs \ No newline at end of file diff --git a/ui/runs/views.py b/ui/runs/views.py index 2e52e4de..f666cccc 100644 --- a/ui/runs/views.py +++ b/ui/runs/views.py @@ -21,11 +21,9 @@ from django.urls import reverse from django.conf import settings -from ui.runs.filter import filter_runs #prob should put this file somewhere else. maybe views_helper? import protzilla.constants.paths as paths -from protzilla.run import Run, get_available_run_names +from protzilla.run import Run, delete_run_folder, get_available_runinfo from protzilla.disk_operator import DiskOperator, YamlOperator -from protzilla.run_v2 import delete_run_folder, get_available_runinfo from protzilla.run_helper import log_messages from protzilla.stepfactory import StepFactory from protzilla.steps import Step @@ -42,7 +40,7 @@ make_name_field, make_sidebar, ) -from ui.runs.views_helper import display_message, display_messages, parameters_from_post, get_all_possible_step_names +from ui.runs.views_helper import display_message, display_messages, parameters_from_post, get_all_possible_step_names, filter_runs from .form_mapping import ( get_empty_plot_form_by_method, diff --git a/ui/runs/views_helper.py b/ui/runs/views_helper.py index f32bb7d2..82232649 100644 --- a/ui/runs/views_helper.py +++ b/ui/runs/views_helper.py @@ -106,6 +106,28 @@ def get_displayed_steps( ) return displayed_steps +def filter_for_name(runs, search_string) -> list[dict[str, str | list[str]]]: + return [run for run in runs if search_string in run["run_name"]] + +def filter_for_steps(runs, search_steps) -> list[dict[str, str | list[str]]]: + return [run for run in runs if all(step in run["run_steps"] for step in search_steps)] + +def filter_for_tags(runs, search_tags) -> list[dict[str, str | list[str]]]: + return [run for run in runs if all(tag in run["run_tags"] for tag in search_tags)] + +def filter_for_memory_mode(runs, memory_mode) -> list[dict[str, str | list[str]]]: + return [run for run in runs if run["memory_mode"] == memory_mode] + +def filter_runs(runs, filters) -> list[dict[str, str | list[str]]]: + if filters["name"]: + runs = filter_for_name(runs, filters["name"]) + if filters["steps"]: + runs = filter_for_steps(runs, filters["steps"]) + if filters["tags"]: + runs = filter_for_tags(runs, filters["tags"]) + if filters["memory_mode"]: + runs = filter_for_memory_mode(runs, filters["memory_mode"]) + return runs def display_message(message: dict, request): """