diff --git a/coreplugins/diagnostic/plugin.py b/coreplugins/diagnostic/plugin.py index bc6068906..1edda6f1c 100644 --- a/coreplugins/diagnostic/plugin.py +++ b/coreplugins/diagnostic/plugin.py @@ -1,4 +1,8 @@ -from app.plugins import PluginBase, Menu, MountPoint +from rest_framework.response import Response +from rest_framework import status, permissions +from rest_framework.decorators import api_view, permission_classes + +from app.plugins import PluginBase, Menu, MountPoint, get_current_plugin from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.utils.translation import gettext as _ @@ -30,30 +34,58 @@ def get_memory_stats(): except: return {} +def get_diagnostic_stats(): + plugin = get_current_plugin() + with plugin.python_imports(): + import psutil + + # Disk space + total_disk_space, used_disk_space, free_disk_space = shutil.disk_usage('./') + + # CPU Stats + cpu_percent_used = psutil.cpu_percent() + cpu_percent_free = 100 - cpu_percent_used + cpu_freq = psutil.cpu_freq() + + diagnostic_stats = { + 'total_disk_space': total_disk_space, + 'used_disk_space': used_disk_space, + 'free_disk_space': free_disk_space, + 'cpu_percent_used': round(cpu_percent_used, 2), + 'cpu_percent_free': round(cpu_percent_free, 2), + 'cpu_freq_current': round(cpu_freq.current / 1000, 2), + } + + # Memory (Linux only) + memory_stats = get_memory_stats() + if 'free' in memory_stats: + diagnostic_stats['free_memory'] = memory_stats['free'] + diagnostic_stats['used_memory'] = memory_stats['used'] + diagnostic_stats['total_memory'] = memory_stats['total'] + + return diagnostic_stats class Plugin(PluginBase): def main_menu(self): return [Menu(_("Diagnostic"), self.public_url(""), "fa fa-chart-pie fa-fw")] + + def api_mount_points(self): + + @api_view() + @permission_classes((permissions.IsAuthenticated,)) + def diagnostic(request): + diagnostic_stats = get_diagnostic_stats() + return Response(diagnostic_stats) + + return [ + MountPoint('/', diagnostic) + ] def app_mount_points(self): @login_required def diagnostic(request): - # Disk space - total_disk_space, used_disk_space, free_disk_space = shutil.disk_usage('./') - - template_args = { - 'title': 'Diagnostic', - 'total_disk_space': total_disk_space, - 'used_disk_space': used_disk_space, - 'free_disk_space': free_disk_space - } - - # Memory (Linux only) - memory_stats = get_memory_stats() - if 'free' in memory_stats: - template_args['free_memory'] = memory_stats['free'] - template_args['used_memory'] = memory_stats['used'] - template_args['total_memory'] = memory_stats['total'] + template_args = get_diagnostic_stats() + template_args['title'] = 'Diagnostic' return render(request, self.template_path("diagnostic.html"), template_args) diff --git a/coreplugins/diagnostic/requirements.txt b/coreplugins/diagnostic/requirements.txt new file mode 100644 index 000000000..a7e7363b7 --- /dev/null +++ b/coreplugins/diagnostic/requirements.txt @@ -0,0 +1 @@ +psutil==5.9.5 \ No newline at end of file diff --git a/coreplugins/diagnostic/templates/diagnostic.html b/coreplugins/diagnostic/templates/diagnostic.html index adc399293..9ba76edab 100644 --- a/coreplugins/diagnostic/templates/diagnostic.html +++ b/coreplugins/diagnostic/templates/diagnostic.html @@ -11,20 +11,34 @@

{% trans 'Storage Space' %}

-

{% trans 'Free' context 'Megabytes of storage space' %}: {{ free_disk_space|filesizeformat }} | +

+ {% trans 'Free' context 'Megabytes of storage space' %}: {{ free_disk_space|filesizeformat }} | {% trans 'Used' context 'Megabytes of storage space' %}: {{ used_disk_space|filesizeformat }} | - {% trans 'Total' context 'Megabytes of storage space' %}: {{ total_disk_space|filesizeformat }}

+ {% trans 'Total' context 'Megabytes of storage space' %}: {{ total_disk_space|filesizeformat }} +

+ {% if total_memory %} +
+

{% trans 'Memory' context 'Computer memory (RAM)' %}

+
+ +
+

+ {% trans 'Free' context 'Megabytes of memory space' %}: {{ free_memory|filesizeformat }} | + {% trans 'Used' context 'Megabytes of memory space' %}: {{ used_memory|filesizeformat }} | + {% trans 'Total' context 'Megabytes of memory space'%}: {{ total_memory|filesizeformat }} +

+
+ {% endif %}
- {% if total_memory %} -

{% trans 'Memory' context 'Computer memory (RAM)' %}

+

{% trans 'CPU Usage' context 'Computer CPU Usage' %}

- +
-

{% trans 'Free' context 'Megabytes of memory space' %}: {{ free_memory|filesizeformat }} | - {% trans 'Used' context 'Megabytes of memory space' %}: {{ used_memory|filesizeformat }} | - {% trans 'Total' context 'Megabytes of memory space'%}: {{ total_memory|filesizeformat }}

- {% endif %} +

+ {% trans 'CPU Usage' context 'CPU usage percentage' %}: {{ cpu_percent_used }}% | + {% trans 'CPU Frequency' context 'CPU frequenzy in Heartz' %}: {{ cpu_freq_current }} GHz +

@@ -33,74 +47,178 @@

{% trans 'Memory' context 'Computer memory (RAM)' %}

{% trans 'Note!' %} {% blocktrans with win_hyperv_link="Windows (Hyper-V)" win_wsl2_link="Windows (WSL2)" mac_link="MacOS" %}These values might be relative to the virtualization environment in which the application is running, not necessarily the values of the your machine. See instructions for {{ win_hyperv_link }}, {{ win_wsl2_link }}, and {{ mac_link }} for changing these values in a Docker setup.{% endblocktrans %}
{% endblock %} diff --git a/requirements.txt b/requirements.txt index 957204f8a..1f02f3989 100644 --- a/requirements.txt +++ b/requirements.txt @@ -63,4 +63,3 @@ eventlet==0.32.0 ; sys_platform == "win32" pyopenssl==19.1.0 ; sys_platform == "win32" numpy==1.21.1 drf-yasg==1.20.0 -