Skip to content
Snippets Groups Projects
Commit d534818f authored by Srikanth Ravipati's avatar Srikanth Ravipati Committed by Peterson, Peter
Browse files

Made memory widget into a package

and a few other changes
parent 835786e1
No related branches found
No related tags found
No related merge requests found
Showing
with 97 additions and 24 deletions
......@@ -161,8 +161,8 @@ set(TEST_FILES
workbench/plugins/test/test_editor.py
workbench/plugins/test/test_exception_handler.py
workbench/plugins/test/test_jupyterconsole.py
workbench/plugins/test/test_memoryinfo.py
workbench/plugins/test/test_memorypresenter.py
workbench/plugins/test/test_memorywidget/test_memoryinfo.py
workbench/plugins/test/test_memorywidget/test_memorypresenter.py
workbench/plugins/test/test_workspacewidget.py
workbench/utils/test/test_workspacehistorygeneration.py
workbench/widgets/plotselector/test/test_plotselector_model.py
......
......@@ -174,10 +174,9 @@ class MainWindow(QMainWindow):
self.widgets.append(self.workspacewidget)
self.set_splash("Loading memory widget")
from workbench.plugins.memorywidget import MemoryWidget
from workbench.plugins.memorywidget.memorywidget import MemoryWidget
self.memorywidget = MemoryWidget(self)
self.memorywidget.register_plugin()
self.memorywidget.setMaximumHeight(70)
self.widgets.append(self.memorywidget)
# set the link between the algorithm and workspace widget
......
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
......@@ -8,12 +8,12 @@
#
#
from psutil import virtual_memory
conversion_factor_to_GB = 1.0 / (1024 * 1024 * 1024)
def getMemoryUsed():
def get_memory_info():
mem_used = virtual_memory().used
mem_total = virtual_memory().total
mem_used_percent = min(int(round(mem_used * 100 / mem_total)),100)
conversion_factor_to_GB = 1.0 / 1024 / 1024 / 1024
mem_used_GB = mem_used * conversion_factor_to_GB
mem_total_GB = mem_total * conversion_factor_to_GB
return mem_used_percent, mem_used_GB, mem_total_GB
from qtpy.QtCore import QTimer
from workbench.plugins.memoryinfo import getMemoryUsed
from ..memorywidget.memoryinfo import get_memory_info
class MemoryPresenter(object):
def __init__(self, view):
self.view = view
self.updateMemoryUsage()
self.view.updateSignal.connect(self.updateMemoryUsage)
self.update_memory_usage()
self.timer = QTimer()
self.timer.timeout.connect(self.view.onUpdateRequest)
self.timer.timeout.connect(self.update_memory_usage)
self.timer.start(10)
def updateMemoryUsage(self):
mem_used_percent, mem_used, mem_avail = getMemoryUsed()
self.view.setValue(mem_used_percent, mem_used, mem_avail)
def update_memory_usage(self):
mem_used_percent, mem_used, mem_avail = get_memory_info()
self.view.set_value(mem_used_percent, mem_used, mem_avail)
......@@ -8,7 +8,6 @@
#
#
from qtpy.QtWidgets import QWidget, QProgressBar
from qtpy.QtCore import Signal
NORMAL_STYLE = """
QProgressBar::chunk {
......@@ -22,15 +21,14 @@ QProgressBar::chunk {
}
"""
def fromNormalToCritical(critical, currentValue, newValue)->bool:
def from_normal_to_critical(critical, currentValue, newValue)->bool:
return (currentValue < critical and newValue >= critical)
def fromCriticalToNormal(critical, currentValue, newValue)->bool:
def from_critical_to_normal(critical, currentValue, newValue)->bool:
return (currentValue >= critical and newValue < critical)
class MemoryView(QWidget):
updateSignal = Signal()
def __init__(self, parent):
super(MemoryView, self).__init__(parent)
......@@ -38,23 +36,20 @@ class MemoryView(QWidget):
self.critical = 90
self.memory_bar = QProgressBar(self)
def setBarColor(self, currentValue, newValue):
if (fromNormalToCritical(self.critical, currentValue, newValue)):
def set_bar_color(self, currentValue, newValue):
if (from_normal_to_critical(self.critical, currentValue, newValue)):
self.memory_bar.setStyleSheet(CRITICAL_STYLE)
elif (fromCriticalToNormal(self.critical, currentValue, newValue)):
elif (from_critical_to_normal(self.critical, currentValue, newValue)):
self.memory_bar.setStyleSheet(NORMAL_STYLE)
else:
pass
def setValue(self, newValue, mem_used, mem_avail):
def set_value(self, newValue, mem_used, mem_avail):
# newValue is the mem_used_percent(int)
currentValue = self.memory_bar.value()
if currentValue != newValue:
self.setBarColor(currentValue, newValue)
self.set_bar_color(currentValue, newValue)
self.memory_bar.setValue(newValue)
display_str = "%3.1f"%mem_used + "/" + "%3.1f"%mem_avail + " GB " + \
"(" + "%d"%newValue+"%" +")"
self.memory_bar.setFormat(display_str)
def onUpdateRequest(self):
self.updateSignal.emit()
......@@ -9,9 +9,9 @@
#
from qtpy.QtWidgets import QVBoxLayout
from workbench.plugins.base import PluginWidget
from workbench.plugins.memoryview import MemoryView
from workbench.plugins.memorypresenter import MemoryPresenter
from ..base import PluginWidget
from ..memorywidget.memoryview import MemoryView
from ..memorywidget.memorypresenter import MemoryPresenter
class MemoryWidget(PluginWidget):
......@@ -26,6 +26,9 @@ class MemoryWidget(PluginWidget):
self.setLayout(layout)
self.setWindowTitle(self.get_plugin_title())
# 70 is chosen as a good value after testing
# how it looks for different values
self.setMaximumHeight(70)
# ----------------- Plugin API --------------------
......
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
......@@ -8,12 +8,13 @@
#
#
import unittest
from workbench.plugins.memoryinfo import getMemoryUsed
from workbench.plugins.memorywidget.memoryinfo import get_memory_info
class MemoryInfoTest(unittest.TestCase):
def test_MemoryInfo(self):
mem_used_percent, mem_used, mem_total = getMemoryUsed()
def test_memoryinfo(self):
mem_used_percent, mem_used, mem_total = get_memory_info()
self.assertTrue(isinstance(mem_used_percent, int))
self.assertTrue(0 <= mem_used_percent <= 100)
......
......@@ -7,46 +7,40 @@
# This file is part of the mantid workbench.
#
#
from workbench.plugins.memoryview import MemoryView
from workbench.plugins.memoryview import fromNormalToCritical, fromCriticalToNormal
from workbench.plugins.memorypresenter import MemoryPresenter
from workbench.plugins.memorywidget.memoryview import MemoryView, \
from_normal_to_critical, from_critical_to_normal
from workbench.plugins.memorywidget.memorypresenter import MemoryPresenter
import unittest
from unittest import mock
class MemoryPresenterTest(unittest.TestCase):
def setUp(self):
self.view = mock.create_autospec(MemoryView)
self.view.updateSignal = mock.Mock()
self.view.critical = 90
self.view.memory_bar = mock.Mock()
self.view.setBarColor = mock.Mock()
self.view.setValue = mock.Mock()
self.view.onUpdateRequest = mock.Mock()
self.view.set_bar_color = mock.Mock()
self.view.set_value = mock.Mock()
self.presenter = MemoryPresenter(self.view)
def test_doSomething(self):
self.assertTrue(fromNormalToCritical(self.presenter.view.critical,
def test_presenter(self):
self.assertTrue(from_normal_to_critical(self.presenter.view.critical,
75, 95))
self.assertFalse(fromCriticalToNormal(self.presenter.view.critical,
self.assertFalse(from_critical_to_normal(self.presenter.view.critical,
75, 95))
self.assertFalse(fromNormalToCritical(self.presenter.view.critical,
self.assertFalse(from_normal_to_critical(self.presenter.view.critical,
95, 75))
self.assertTrue(fromCriticalToNormal(self.presenter.view.critical,
self.assertTrue(from_critical_to_normal(self.presenter.view.critical,
95, 75))
self.assertEqual(self.presenter.view.setValue.call_count, 1)
self.view.memory_bar.value.retun_value = 95
self.presenter.view.memory_bar.value.retun_value = 95
self.presenter.updateMemoryUsage()
self.assertEqual(self.presenter.view.setValue.call_count, 2)
self.assertEqual(self.presenter.view.set_value.call_count, 1)
self.presenter.update_memory_usage()
self.assertEqual(self.presenter.view.set_value.call_count, 2)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment