• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

djangoaddicts / django-hostutils / 8075260951

28 Feb 2024 04:24AM UTC coverage: 99.32% (+0.02%) from 99.298%
8075260951

push

github

web-flow
adding rbac option (#21)

35 of 37 branches covered (94.59%)

Branch coverage included in aggregate %.

257 of 257 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.21
/src/djangoaddicts/hostutils/views/gui.py
1
import datetime
1✔
2

3
import psutil
1✔
4
from dateutil.relativedelta import relativedelta
1✔
5
from django.conf import settings
1✔
6
from django.shortcuts import render
1✔
7
from django.views.generic import View
1✔
8
from handyhelpers.permissions import InAnyGroup
1✔
9

10
# import forms
11
from djangoaddicts.hostutils.forms import HostProcessFilterForm
1✔
12

13

14
class ShowHost(InAnyGroup, View):
1✔
15
    """Display dashboard like page showing an overview of host data"""
16

17
    template_name = "hostutils/bs5/detail/detail_host.html"
1✔
18
    title = "Host Dashboard"
1✔
19
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
20

21
    def get(self, request, *args, **kwargs):
1✔
22
        """allow get method"""
23
        now = datetime.datetime.now()
1✔
24
        context = {}
1✔
25
        context["title"] = self.title
1✔
26
        context["subtitle"] = psutil.os.uname()[1]
1✔
27
        context["cpu_count"] = psutil.cpu_count(logical=False)
1✔
28
        context["memory"] = psutil.virtual_memory()
1✔
29
        context["disk_usage"] = psutil.disk_usage("/")
1✔
30
        context["disk_io_counters"] = psutil.disk_io_counters()
1✔
31
        context["network"] = psutil.net_connections()
1✔
32
        context["pids"] = psutil.pids()
1✔
33

34
        boot_time = psutil.boot_time()
1✔
35
        diff = relativedelta(now, datetime.datetime.fromtimestamp(boot_time))
1✔
36
        context["times"] = {}
1✔
37
        context["times"]["boot_time"] = datetime.datetime.fromtimestamp(boot_time)
1✔
38
        context["times"]["up_time"] = (
1✔
39
            f"{diff.days} days, {diff.hours} hours, {diff.minutes} minutes, " f"{diff.seconds} seconds"
40
        )
41

42
        context["platform"] = psutil.os.uname()
1✔
43

44
        return render(request, self.template_name, context=context)
1✔
45

46

47
class ShowHostCpu(InAnyGroup, View):
1✔
48
    """Display dashboard like page showing host cpu data"""
49

50
    template_name = "hostutils/bs5/detail/cpu.html"
1✔
51
    title = "CPU Dashboard"
1✔
52
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
53

54
    def get(self, request, *args, **kwargs):
1✔
55
        """CPU Dashboard"""
56
        context = {}
1✔
57
        context["title"] = self.title
1✔
58
        context["subtitle"] = psutil.os.uname()[1]
1✔
59
        context["stats"] = psutil.cpu_stats()
1✔
60
        context["physical_count"] = psutil.cpu_count(logical=False)
1✔
61
        context["logical_count"] = psutil.cpu_count(logical=True)
1✔
62
        context["percent"] = psutil.cpu_percent(interval=None)
1✔
63
        context["times_list"] = psutil.cpu_times(percpu=True)
1✔
64
        context["times_percent_list"] = psutil.cpu_times_percent(percpu=True)
1✔
65
        context["percent_list"] = psutil.cpu_percent(interval=None, percpu=True)
1✔
66
        context["frequency_list"] = psutil.cpu_freq(percpu=True)
1✔
67
        context["cpu_range"] = list(range(psutil.cpu_count(logical=True)))
1✔
68
        cpu_data = {}
1✔
69
        for i in range(context["logical_count"]):
1✔
70
            cpu_data[i] = {
1✔
71
                "times": context["times_list"][i],
72
                "time_percent": context["times_percent_list"][i],
73
                "percent": context["percent_list"][i],
74
                "frequency": context["frequency_list"][i],
75
            }
76
        context["cpu_data"] = cpu_data
1✔
77
        context["load_avg_1"], context["load_avg_5"], context["load_avg_15"] = [
1!
78
            round(x / psutil.cpu_count() * 100, 2) for x in psutil.getloadavg()
79
        ]
80
        return render(request, self.template_name, context=context)
1✔
81

82

83
class ShowHostDisk(InAnyGroup, View):
1✔
84
    """Display dashboard like page showing host disk data"""
85

86
    template_name = "hostutils/bs5/detail/disk.html"
1✔
87
    title = "Disk Dashboard"
1✔
88
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
89

90
    def get(self, request, *args, **kwargs):
1✔
91
        """allow get method"""
92
        context = {}
1✔
93
        context["title"] = self.title
1✔
94
        context["subtitle"] = psutil.os.uname()[1]
1✔
95
        context["usage"] = psutil.disk_usage("/")
1✔
96
        context["io_counters"] = psutil.disk_io_counters()
1✔
97
        context["partition_lists"] = psutil.disk_partitions()
1✔
98
        return render(request, self.template_name, context=context)
1✔
99

100

101
class ShowHostMemory(InAnyGroup, View):
1✔
102
    """Display dashboard like page showing host memory data"""
103

104
    template_name = "hostutils/bs5/detail/memory.html"
1✔
105
    title = "Memory Dashboard"
1✔
106
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
107

108
    def get(self, request, *args, **kwargs):
1✔
109
        """allow get method"""
110
        context = {}
1✔
111
        context["title"] = self.title
1✔
112
        context["subtitle"] = psutil.os.uname()[1]
1✔
113
        context["virtual"] = psutil.virtual_memory()
1✔
114
        context["swap"] = psutil.swap_memory()
1✔
115
        return render(request, self.template_name, context=context)
1✔
116

117

118
class ShowHostNetwork(InAnyGroup, View):
1✔
119
    """Display dashboard like page showing host network data"""
120

121
    template_name = "hostutils/bs5/detail/network.html"
1✔
122
    title = "Network Dashboard"
1✔
123
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
124

125
    def get(self, request, *args, **kwargs):
1✔
126
        """allow get method"""
127
        context = {}
1✔
128
        context["title"] = self.title
1✔
129
        context["subtitle"] = psutil.os.uname()[1]
1✔
130
        context["connection_list"] = psutil.net_connections()
1✔
131
        context["interface_list"] = psutil.net_if_addrs()
1✔
132
        context["stats_list"] = psutil.net_if_stats()
1✔
133
        context["counters"] = psutil.net_io_counters()
1✔
134
        return render(request, self.template_name, context=context)
1✔
135

136

137
class ShowHostProcesses(InAnyGroup, View):
1✔
138
    """Display dashboard like page showing host process data"""
139

140
    template_name = "hostutils/bs5/detail/processes.html"
1✔
141
    title = "Process Dashboard"
1✔
142
    permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])}
1✔
143

144
    def get(self, request, *args, **kwargs):
1✔
145
        """allow get method"""
146
        context = {}
1✔
147
        context["title"] = self.title
1✔
148
        context["now"] = datetime.datetime.now()
1✔
149
        context["subtitle"] = psutil.os.uname()[1]
1✔
150
        counts = {"running": 0, "sleeping": 0, "idle": 0, "stopped": 0, "zombie": 0, "dead": 0, "disk-sleep": 0}
1✔
151
        process_list = []
1✔
152
        for process in psutil.process_iter():
1✔
153
            try:
1✔
154
                counts[process.status()] += 1
1✔
155
                process_list.append(
1✔
156
                    {
157
                        "pid": process.pid,
158
                        "name": process.name(),
159
                        "status": process.status(),
160
                        "create_time": process.create_time(),
161
                    }
162
                )
163
            except (psutil.NoSuchProcess, psutil.AccessDenied):
1✔
164
                continue
1✔
165
        context["process_list"] = process_list
1✔
166
        context["counts"] = counts
1✔
167
        filter_form = {}
1✔
168
        filter_form["form"] = HostProcessFilterForm(request.GET or None)
1✔
169
        filter_form["modal_name"] = "filter_processes"
1✔
170
        filter_form["modal_size"] = "modal-lg"
1✔
171
        filter_form["modal_title"] = "Filter Host Processes"
1✔
172
        filter_form["hx_method"] = "hx-get"
1✔
173
        filter_form["hx_url"] = "/hostutils/get_host_processes"
1✔
174
        filter_form["hx_target"] = "id_process_list_container"
1✔
175
        filter_form["method"] = "GET"
1✔
176
        filter_form["action"] = "Filter"
1✔
177
        context["filter_form"] = filter_form
1✔
178
        return render(request, self.template_name, context=context)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc