-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
77 lines (64 loc) · 3.74 KB
/
dashboard.py
File metadata and controls
77 lines (64 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Home view."""
import datetime
from django.views.generic import TemplateView
from django.db.models import Count
from django.db.models.functions import TruncMonth
from django.db.models import Q
from ..exploit.models import Exploit
class DashboardTemplateView(TemplateView):
"""Hometemplate view."""
template_name = "pages/dashboard.html"
def get_context_data(self, **kwargs):
Color = ["progress-bar bg-success", "progress-bar bg-info text-dark", "progress-bar bg-warning text-dark", "progress-bar bg-danger" ]
"""Get context data."""
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Get a count of the total number of exploits we have into our context.
context["exploits_count"] = Exploit.objects.all().count()
# Get a count of how many exploit entries have been added within the last day.
date_from = datetime.datetime.now() - datetime.timedelta(days=1)
context["24_hours"] = Exploit.objects.filter(date_created__gte=date_from).count()
# Get a count oh how many exploits have been added within the last 7 days.
date_from = datetime.datetime.now() - datetime.timedelta(days=7)
context["7_days"] = Exploit.objects.filter(date_created__gte=date_from).count()
# For each source, get a count of its associated exploits, it percent representation, and what color to display it as.
sources = Exploit.objects.values('source').annotate(count=Count('source')).order_by()
color = 0
for source in sources:
source['percentage'] = format(source['count'] / context["exploits_count"] * 100, '.2f')
if color > 3:
color = 0
source['color'] = Color[color]
color += 1
context["sources"] = sources
# Begin assembling the data for the highchart.
# Start by querying for number of monthly entries per source this year.
data_sets = Exploit.objects.filter(Q(date_created__year=datetime.datetime.now().date().year)).values('source').annotate(month=TruncMonth('date_created')).values('month').annotate(c=Count('id')).values('month', 'c', 'source').order_by('source')
# Get a list of the sources.
options = Exploit.objects.order_by().values('source').distinct()
print(data_sets)
# Set a list of 0s sized by current month digit. This will hold the per month count.
data_set = [0] * (datetime.datetime.now().month)
graph_data = []
# Get number of monthly entries with respect to source in a graphable format.
for option in options:
for data in data_sets:
if data['source'] == option['source']:
if data['month'] is not None:
data_set[data['month'].month-1] = data['c']
# Append the source with its monthly counts to graph_data.
graph_data.append({"name": option['source'], "data": data_set})
# Reset the monthly counts to 0.
data_set = [0] * (datetime.datetime.now().month )
# oldest_date = Exploit.objects.order_by('date_published').values('date_published').first()
current_date = datetime.datetime.now()
# year = oldest_date['date_published'].year
# Save year info to context for highchart
context["years"] = [2022, 2023]
context["year"] = current_date.year
# Set the generated graph data into context.
context["data"] = graph_data
# Add failure to download count to context
context["download_failures"] = Exploit.objects.filter(Q(download_failed=True, ignore=False, fixed=False)).count()
# Return our built context to be used by the template.
return context