Skip to content

Commit

Permalink
Improve something
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Jan 2, 2025
1 parent 5526786 commit c000806
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 52 deletions.
14 changes: 2 additions & 12 deletions src/ert/config/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import os.path
import shutil
from datetime import datetime
from pathlib import Path
from typing import no_type_check

from pydantic import field_validator
from pydantic.dataclasses import dataclass

from ert.shared.status.utils import byte_with_unit
from ert.shared.status.utils import byte_with_unit, get_mount_directory

from .parsing import (
ConfigDict,
Expand Down Expand Up @@ -94,7 +93,7 @@ def validate_runpath(cls, runpath_format_string: str) -> str:
ConfigWarning.warn(msg)
logger.warning(msg)
with contextlib.suppress(Exception):
mount_dir = _get_mount_directory(runpath_format_string)
mount_dir = get_mount_directory(runpath_format_string)
total_space, used_space, free_space = shutil.disk_usage(mount_dir)
percentage_used = used_space / total_space
if (
Expand Down Expand Up @@ -165,12 +164,3 @@ def _replace_runpath_format(format_string: str) -> str:
format_string = format_string.replace("%d", "<IENS>", 1)
format_string = format_string.replace("%d", "<ITER>", 1)
return format_string


def _get_mount_directory(runpath: str) -> Path:
path = Path(runpath).absolute()

while not path.is_mount():
path = path.parent

return path
8 changes: 3 additions & 5 deletions src/ert/gui/simulation/run_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
from ert.run_models.event import RunModelDataEvent, RunModelErrorEvent
from ert.shared.status.utils import (
byte_with_unit,
disk_space_status,
file_has_content,
format_running_time,
get_mount_directory,
)

from ..find_ert_info import find_ert_info
Expand Down Expand Up @@ -196,7 +196,7 @@ def __init__(
self._ticker = QTimer(self)
self._ticker.timeout.connect(self._on_ticker)

self.run_path_mp = run_model.run_paths._runpath_format
self.run_path_mp = get_mount_directory(run_model.run_paths._runpath_format)

self._total_progress_label = QLabel(
_TOTAL_PROGRESS_TEMPLATE.format(
Expand Down Expand Up @@ -430,10 +430,8 @@ def _on_ticker(self) -> None:
self.running_time.setText(format_running_time(runtime))

maximum_memory_usage = self._snapshot_model.root.max_memory_usage
disk_usage = disk_space_status(self.run_path_mp)

if disk_usage:
self.disk_space.update_status(self.run_path_mp)
self.disk_space.update_status(self.run_path_mp)

Check failure on line 434 in src/ert/gui/simulation/run_dialog.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Argument 1 to "update_status" of "DiskSpaceWidget" has incompatible type "Path"; expected "str"

if maximum_memory_usage:
self.memory_usage.setText(
Expand Down
66 changes: 37 additions & 29 deletions src/ert/gui/simulation/view/disk_space_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,59 @@


class DiskSpaceWidget(QWidget):
def __init__(self, parent=None):
def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(parent)

layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(10)

# Text label
self.label = QLabel(self)
self.usage_label = QLabel(self)
self.space_left_label = QLabel(self)

# Progress bar
self.progress_bar = QProgressBar(self)
self.progress_bar.setRange(0, 100)
self.progress_bar.setTextVisible(True)
self.progress_bar.setFixedWidth(100)
self.progress_bar.setAlignment(Qt.AlignCenter)

# Add color styling based on usage
self.progress_bar.setStyleSheet("""
QProgressBar {
border: 1px solid #ccc;
border-radius: 2px;
text-align: center;
}
QProgressBar::chunk {
background-color: qlineargradient(
x1: 0, y1: 0.5, x2: 1, y2: 0.5,
stop: 0 #2ecc71,
stop: 0.7 #f1c40f,
stop: 0.9 #e74c3c
);
}
""")

layout.addWidget(self.label)
self.progress_bar.setAlignment(Qt.AlignCenter) # type: ignore

layout.addWidget(self.usage_label)
layout.addWidget(self.progress_bar)
layout.addWidget(self.space_left_label)

def update_status(self, run_path_mp):
def update_status(self, mount_dir: str) -> None:
"""Update both the label and progress bar with current disk usage"""
disk_usage = disk_space_status(run_path_mp)
if disk_usage is not None:
self.label.setText("Disk space used runpath:")
self.progress_bar.setValue(int(disk_usage))
self.progress_bar.setFormat(f"{disk_usage:.1f}%")
disk_info = disk_space_status(mount_dir)
if disk_info is not None:
usage = int(disk_info[0])
self.usage_label.setText("Disk space runpath:")
self.progress_bar.setValue(usage)
self.progress_bar.setFormat(f"{disk_info[0]:.1f}%")

# Set color based on usage threshold
if usage >= 90:
color = "#e74c3c" # Red for critical usage
elif usage >= 70:
color = "#f1c40f" # Yellow for warning
else:
color = "#2ecc71" # Green for normal usage

self.progress_bar.setStyleSheet(f"""
QProgressBar {{
border: 1px solid #ccc;
border-radius: 2px;
text-align: center;
}}
QProgressBar::chunk {{
background-color: {color};
}}
""")

self.space_left_label.setText(f"{disk_info[1]} free")

self.setVisible(True)
else:
self.setVisible(False)
12 changes: 6 additions & 6 deletions src/ert/shared/status/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ def get_ert_memory_usage() -> int:
return usage.ru_maxrss // rss_scale


def disk_space_status(runpath: str) -> float | None:
def disk_space_status(mount_dir: str) -> tuple[float, str] | None:
with contextlib.suppress(Exception):
mount_dir = _get_mount_directory(runpath)
total_space, used_space, _free_space = shutil.disk_usage(mount_dir)
percentage_used = used_space / total_space
return percentage_used
disk_info = shutil.disk_usage(mount_dir)
percentage_used = (disk_info.used / disk_info.total) * 100
return percentage_used, byte_with_unit(disk_info.free)
return None


def _get_mount_directory(runpath: str) -> Path:
def get_mount_directory(runpath: str) -> Path:
path = Path(runpath).absolute()

while not path.is_mount():
Expand Down

0 comments on commit c000806

Please sign in to comment.