-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
324e002
commit b6edc3b
Showing
7 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .disk_space_widget import DiskSpaceWidget | ||
from .progress_widget import ProgressWidget | ||
from .realization import RealizationWidget | ||
from .update import UpdateWidget | ||
|
||
__all__ = ["ProgressWidget", "RealizationWidget", "UpdateWidget"] | ||
__all__ = ["DiskSpaceWidget", "ProgressWidget", "RealizationWidget", "UpdateWidget"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import contextlib | ||
import shutil | ||
from pathlib import Path | ||
|
||
from qtpy.QtCore import Qt | ||
from qtpy.QtWidgets import QHBoxLayout, QLabel, QProgressBar, QWidget | ||
|
||
from ert.shared.status.utils import byte_with_unit | ||
|
||
CRITICAL_RED = "#e74c3c" | ||
WARNING_YELLOW = "#f1c40f" | ||
NORMAL_GREEN = "#2ecc71" | ||
|
||
|
||
class DiskSpaceWidget(QWidget): | ||
def __init__(self, mount_path: Path, parent: QWidget | None = None) -> None: | ||
super().__init__(parent) | ||
|
||
self.mount_path = mount_path | ||
|
||
layout = QHBoxLayout(self) | ||
layout.setContentsMargins(0, 0, 0, 0) | ||
layout.setSpacing(10) | ||
|
||
# Text label | ||
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) # type: ignore | ||
|
||
layout.addWidget(self.usage_label) | ||
layout.addWidget(self.progress_bar) | ||
layout.addWidget(self.space_left_label) | ||
|
||
def _get_status(self) -> tuple[float, str] | None: | ||
with contextlib.suppress(Exception): | ||
disk_info = shutil.disk_usage(self.mount_path) | ||
percentage_used = (disk_info.used / disk_info.total) * 100 | ||
return percentage_used, byte_with_unit(disk_info.free) | ||
return None | ||
|
||
def update_status(self) -> None: | ||
"""Update both the label and progress bar with current disk usage""" | ||
if (disk_info := self._get_status()) is not None: | ||
usage, space_left = disk_info | ||
self.usage_label.setText("Disk space runpath:") | ||
self.progress_bar.setValue(int(usage)) | ||
self.progress_bar.setFormat(f"{usage:.1f}%") | ||
|
||
# Set color based on usage threshold | ||
if usage >= 90: | ||
color = CRITICAL_RED | ||
elif usage >= 70: | ||
color = WARNING_YELLOW | ||
else: | ||
color = NORMAL_GREEN | ||
|
||
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"{space_left} free") | ||
|
||
self.setVisible(True) | ||
else: | ||
self.setVisible(False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/ert/unit_tests/gui/simulation/view/test_disk_space_widget.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from unittest.mock import MagicMock | ||
|
||
from ert.gui.simulation.view import DiskSpaceWidget | ||
from ert.gui.simulation.view.disk_space_widget import ( | ||
CRITICAL_RED, | ||
NORMAL_GREEN, | ||
WARNING_YELLOW, | ||
) | ||
|
||
|
||
def test_disk_space_widget(qtbot): | ||
disk_space_widget = DiskSpaceWidget("/tmp") | ||
qtbot.addWidget(disk_space_widget) | ||
disk_space_widget._get_status = MagicMock() | ||
|
||
disk_space_widget._get_status.return_value = (20.0, "2 jiggabytes") | ||
disk_space_widget.update_status() | ||
assert disk_space_widget.space_left_label.text() == "2 jiggabytes free" | ||
assert disk_space_widget.progress_bar.value() == 20 | ||
assert NORMAL_GREEN in disk_space_widget.progress_bar.styleSheet() | ||
|
||
disk_space_widget._get_status.return_value = (88.0, "2mb") | ||
disk_space_widget.update_status() | ||
assert disk_space_widget.space_left_label.text() == "2mb free" | ||
assert disk_space_widget.progress_bar.value() == 88 | ||
assert WARNING_YELLOW in disk_space_widget.progress_bar.styleSheet() | ||
|
||
disk_space_widget._get_status.return_value = (99.9, "2 bytes") | ||
disk_space_widget.update_status() | ||
assert disk_space_widget.space_left_label.text() == "2 bytes free" | ||
assert disk_space_widget.progress_bar.value() == 99 | ||
assert CRITICAL_RED in disk_space_widget.progress_bar.styleSheet() | ||
|
||
disk_space_widget._get_status.return_value = None | ||
disk_space_widget.update_status() | ||
assert not disk_space_widget.isVisible() |