Skip to content

Commit

Permalink
consolidating host views to single file (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidslusser committed Apr 25, 2023
1 parent cfeabdc commit f1da8c0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 83 deletions.
2 changes: 1 addition & 1 deletion handyhelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

__title__ = 'django-handyhelpers'
__version__ = '0.3.1'
__version__ = '0.3.2'
__author__ = 'David Slusser'
__email__ = 'dbslusser@gmail.com'
__license__ = 'GPL-3.0'
Expand Down
9 changes: 4 additions & 5 deletions handyhelpers/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.urls import path
from handyhelpers.views import action
from handyhelpers.views import ajax
from handyhelpers.views import host
from handyhelpers.views import htmx

Expand All @@ -25,10 +24,10 @@
path('show_all_list_view', action.ShowAllListView.as_view(), name='show_all_list_view'),

# ajax views
path('host_interface_stats/', ajax.GetHostNetworkStats.as_view(), name='host_interface_stats'),
path('host_process_details/', ajax.GetHostProcessDetails.as_view(), name='host_process_details'),
path('host_partition_usage/', ajax.GetHostParitionUsage.as_view(), name='host_partition_usage'),
path('get_host_cpu_stats/', ajax.GetHostCpuStats.as_view(), name='get_host_cpu_stats'),
path('host_interface_stats/', host.GetHostNetworkStats.as_view(), name='host_interface_stats'),
path('host_process_details/', host.GetHostProcessDetails.as_view(), name='host_process_details'),
path('host_partition_usage/', host.GetHostParitionUsage.as_view(), name='host_partition_usage'),
path('get_host_cpu_stats/', host.GetHostCpuStats.as_view(), name='get_host_cpu_stats'),

# htmx views
path('get_host_processes/', htmx.GetHostProcesses.as_view(), name='get_host_processes'),
Expand Down
77 changes: 0 additions & 77 deletions handyhelpers/views/ajax.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json
import psutil

from django.views import View
from django.http import HttpResponse
from django.template import loader


class AjaxGetView(View):
Expand All @@ -22,78 +20,3 @@ def get(self, request, *args, **kwargs):
except Exception:
return HttpResponse(json.dumps({'server_response': self.template.render({'data': None})}),
content_type='application/javascript')


class GetHostNetworkStats(AjaxGetView):
"""
Description:
Get network statistics for a given network interface on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_interface_stats.htm')

def get(self, request, *args, **kwargs):
interface = self.request.GET['client_response']
self.data = psutil.net_if_stats()[interface]
return super().get(request, *args, **kwargs)


class GetHostProcessDetails(AjaxGetView):
"""
Description:
Get process details for a given process on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_process_details.htm')

def get(self, request, *args, **kwargs):
proc = request.GET['client_response']
try:
self.data = psutil.Process(int(proc))
except psutil.AccessDenied:
self.data = {}
return super().get(request, *args, **kwargs)


class GetHostParitionUsage(AjaxGetView):
"""
Description:
Get disk usage for a given partition on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_partition_usage.htm')

def get(self, request, *args, **kwargs):
part = request.GET['client_response']
self.data = psutil.disk_usage(part)
return super().get(request, *args, **kwargs)


class GetHostCpuStats(AjaxGetView):
"""
Description:
Get CPU status for a given cpu on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_cpu_stats.htm')

def get(self, request, *args, **kwargs):
cpu = int(request.GET['client_response'])
self.data = dict(
time=psutil.cpu_times(percpu=True)[cpu],
time_percent=psutil.cpu_times_percent(percpu=True)[cpu],
frequency=psutil.cpu_freq(percpu=True)[cpu],
)
return super().get(request, *args, **kwargs)
77 changes: 77 additions & 0 deletions handyhelpers/views/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from dateutil.relativedelta import relativedelta
from django.shortcuts import render
from django.template import loader
from django.views.generic import (View)
from handyhelpers.views.ajax import AjaxGetView

# import forms
from handyhelpers.forms import HostProcessFilterForm
Expand Down Expand Up @@ -142,3 +144,78 @@ def get(self, request, *args, **kwargs):
context['load_avg_1'], context['load_avg_5'], context['load_avg_15'] = \
[round(x / psutil.cpu_count() * 100, 2) for x in psutil.getloadavg()]
return render(request, self.template_name, context=context)


class GetHostNetworkStats(AjaxGetView):
"""
Description:
Get network statistics for a given network interface on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_interface_stats.htm')

def get(self, request, *args, **kwargs):
interface = self.request.GET['client_response']
self.data = psutil.net_if_stats()[interface]
return super().get(request, *args, **kwargs)


class GetHostProcessDetails(AjaxGetView):
"""
Description:
Get process details for a given process on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_process_details.htm')

def get(self, request, *args, **kwargs):
proc = request.GET['client_response']
try:
self.data = psutil.Process(int(proc))
except psutil.AccessDenied:
self.data = {}
return super().get(request, *args, **kwargs)


class GetHostParitionUsage(AjaxGetView):
"""
Description:
Get disk usage for a given partition on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_partition_usage.htm')

def get(self, request, *args, **kwargs):
part = request.GET['client_response']
self.data = psutil.disk_usage(part)
return super().get(request, *args, **kwargs)


class GetHostCpuStats(AjaxGetView):
"""
Description:
Get CPU status for a given cpu on the host machine.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/host_cpu_stats.htm')

def get(self, request, *args, **kwargs):
cpu = int(request.GET['client_response'])
self.data = dict(
time=psutil.cpu_times(percpu=True)[cpu],
time_percent=psutil.cpu_times_percent(percpu=True)[cpu],
frequency=psutil.cpu_freq(percpu=True)[cpu],
)
return super().get(request, *args, **kwargs)

0 comments on commit f1da8c0

Please sign in to comment.