Skip to content

Commit

Permalink
New machines widget, fix some timezone errors
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamgilbert committed Oct 20, 2015
1 parent 788ec3a commit 43341bc
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 11 deletions.
5 changes: 3 additions & 2 deletions server/plugins/activity/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from django.shortcuts import get_object_or_404
import server.utils as utils
from datetime import datetime, timedelta, date
import django.utils.timezone

now = datetime.now()
now = django.utils.timezone.now()
hour_ago = now - timedelta(hours=1)
today = date.today()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down
73 changes: 73 additions & 0 deletions server/plugins/newmachines/newmachines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from yapsy.IPlugin import IPlugin
from yapsy.PluginManager import PluginManager
from django.template import loader, Context
from django.db.models import Count
from server.models import *
from django.shortcuts import get_object_or_404
import server.utils as utils
from datetime import datetime, timedelta, date
import django.utils.timezone

now = django.utils.timezone.now()
this_day = now - timedelta(hours=24)
week_ago = this_day - timedelta(days=7)
month_ago = this_day - timedelta(days=30)

class NewMachines(IPlugin):
def plugin_type(self):
return 'builtin'

def show_widget(self, page, machines=None, theid=None):
# The data is data is pulled from the database and passed to a template.

# There are three possible views we're going to be rendering to - front, bu_dashbaord and group_dashboard. If page is set to bu_dashboard, or group_dashboard, you will be passed a business_unit or machine_group id to use (mainly for linking to the right search).
if page == 'front':
t = loader.get_template('newmachines/templates/front.html')

if page == 'bu_dashboard':
t = loader.get_template('newmachines/templates/id.html')

if page == 'group_dashboard':
t = loader.get_template('newmachines/templates/id.html')

if machines:
print this_day
today = machines.filter(first_checkin__gte=this_day).count()
this_week = machines.filter(first_checkin__gte=week_ago).count()
this_month = machines.filter(first_checkin__gte=month_ago).count()
else:
today = 0
this_week = 0
this_month = 0

c = Context({
'title': 'New Machines',
'today_label': 'Today',
'today_count': today,
'week_label': 'This Week',
'week_count': this_week,
'month_label': 'This Month',
'month_count': this_month,
'plugin': 'NewMachines',
'theid': theid,
'page': page
})
return t.render(c), 4

def filter_machines(self, machines, data):
if data == 'day':
machines = machines.filter(first_checkin__gte=this_day)
title = 'Machines first seen today'

elif data == 'week':
machines = machines.filter(first_checkin__gte=week_ago)
title = 'Machines first seen this week'

elif data == 'month':
machines = machines.filter(first_checkin__gte=month_ago)
title = 'Machines first seen this month'

else:
machines = None

return machines, title
8 changes: 8 additions & 0 deletions server/plugins/newmachines/newmachines.yapsy-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Core]
Name = NewMachines
Module = newmachines

[Documentation]
Author = Graham Gilbert
Version = 0.1
Website = http://grahamgilbert.com
24 changes: 24 additions & 0 deletions server/plugins/newmachines/templates/front.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="col-md-4">
<div class="panel panel-default panel-traffic-light">
<div class="panel-heading">
{{ title }}
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<a href="{% url 'machine_list_front' plugin 'day' %}" class="btn btn-info">
<span class="bigger"> {{ today_count }} </span><br />
{{ today_label }}
</a>

<a href="{% url 'machine_list_front' plugin 'week' %}" class="btn btn-info">
<span class="bigger"> {{ week_count }} </span><br />
{{ week_label }}
</a>

<a href="{% url 'machine_list_front' plugin 'month' %}" class="btn btn-info">
<span class="bigger"> {{ month_count }} </span><br />
{{ month_label }}
</a>
</div>
</div>
</div>
24 changes: 24 additions & 0 deletions server/plugins/newmachines/templates/id.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
{{ title }}
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<a href="{% url 'machine_list_id' plugin 'day' page theid %}" class="btn btn-info">
<span class="bigger"> {{ today_count }} </span><br />
{{ today_label }}
</a>

<a href="{% url 'machine_list_id' plugin 'week' page theid %}" class="btn btn-info">
<span class="bigger"> {{ week_count }} </span><br />
{{ week_label }}
</a>

<a href="{% url 'machine_list_id' plugin 'month' page theid %}" class="btn btn-info">
<span class="bigger"> {{ month_count }} </span><br />
{{ month_label }}
</a>
</div>
</div>
</div>
5 changes: 3 additions & 2 deletions server/plugins/puppetstatus/puppetstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import server.utils as utils
from django.db.models import Q
from datetime import datetime, timedelta, date
import django.utils.timezone

now = datetime.now()
now = django.utils.timezone.now()
hour_ago = now - timedelta(hours=1)
today = date.today()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down
4 changes: 3 additions & 1 deletion server/plugins/status/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from django.shortcuts import get_object_or_404
import server.utils as utils
from datetime import datetime, timedelta, date
import django.utils.timezone

today = date.today()
now = django.utils.timezone.now()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down
13 changes: 7 additions & 6 deletions server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pytz
import watson
import unicodecsv as csv
import django.utils.timezone
# This will only work if BRUTE_PROTECT == True
try:
import axes.utils
Expand Down Expand Up @@ -82,9 +83,9 @@ def index(request):
profile.level = 'GA'
profile.save()
user_level = user.userprofile.level
now = datetime.now()
now = django.utils.timezone.now()
hour_ago = now - timedelta(hours=1)
today = date.today()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down Expand Up @@ -500,9 +501,9 @@ def bu_dashboard(request, bu_id):
else:
is_editor = False
machines = utils.getBUmachines(bu_id)
now = datetime.now()
now = django.utils.timezone.now()
hour_ago = now - timedelta(hours=1)
today = date.today()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down Expand Up @@ -541,9 +542,9 @@ def overview_list_all(request, req_type, data, bu_id=None):
activity = None
inactivity = None
disk_space = None
now = datetime.now()
now = django.utils.timezone.now()
hour_ago = now - timedelta(hours=1)
today = date.today()
today = now - timedelta(hours=24)
week_ago = today - timedelta(days=7)
month_ago = today - timedelta(days=30)
three_months_ago = today - timedelta(days=90)
Expand Down

0 comments on commit 43341bc

Please sign in to comment.