Skip to content

Commit

Permalink
Moved filters into views_helper.py, deleted now obsolete filters.py, …
Browse files Browse the repository at this point in the history
…moved (wrongly placed by us) functions from run_v2.py to run.py
  • Loading branch information
gitjannes committed Dec 17, 2024
1 parent 6fe7878 commit 1e341f5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 90 deletions.
63 changes: 63 additions & 0 deletions protzilla/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
Expand Down
64 changes: 0 additions & 64 deletions protzilla/run_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
Expand Down
22 changes: 0 additions & 22 deletions ui/runs/filter.py

This file was deleted.

6 changes: 2 additions & 4 deletions ui/runs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions ui/runs/views_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 1e341f5

Please sign in to comment.