from __future__ import unicode_literals
# -*- coding: UTF-8 -*-
from django.shortcuts import render
from django.http import HttpResponseServerError, JsonResponse, HttpResponse
from libs.menu import updateMenuNumbers
from libs.statistic import *
from libs.system import *
from jobs.models import Job
from config.models import *
# import pytz


def index(request):
    context = {'contentheader': 'Dashboard', 'contentheadersmall': 'Control Panel'}
    updateMenuNumbers(context)
    updateservicestatus(context)
    return render(request, 'pages/index.html', context)


def goeswrong(request):
    return HttpResponseServerError()


def lastjobswidget(request):
    """ Last Jobs widget """
    jdis = [a['name'] for a in ConfResource.objects.filter(compid__type='D', type__name='Job',
                                                           confparameter__name='.Disabledfordelete').values('name')]
    lastjobs = Job.objects.filter(jobstatus__in=['T', 'E', 'f', 'A', 'I']).exclude(name__in=jdis)\
                          .order_by('-jobid').values()[:4]
    elines = ''
    if len(lastjobs) < 4:
        # add missing empty lines
        elines = '0' * (4-len(lastjobs))
    context = {'LastJobs': lastjobs, 'ELines': elines}
    return render(request, 'widgets/lastjobswidget.html', context)


def cpuutilwidget(request):
    hours = 24
    npoints = 200
    data = generate_series_stats(parname='system.cpu.util', npoints=npoints, hours=hours, field='fvalue')
    context = {'color': '#00c0ef', 'label': 'CPU Util%', 'data': data}
    return JsonResponse(context)


def backupsizewidget(request):
    hours = 12
    npoints = 50
    data = generate_series_stats(parname='bacula.size.jobs.bytes', npoints=npoints, hours=hours, div=1073741824.0)   # , allownull=False
    barwidth = (hours * 2000000) / npoints
    context = {'color': '#39cccc', 'label': 'GBytes', 'barWidth': barwidth, 'data': data}
    return JsonResponse(context)


def runningjobswidget(request):
    hours = 12
    npoints = 100
    data = generate_series_stats(parname='bacula.jobs.running', npoints=npoints, hours=hours)
    barwidth = (hours * 2000000) / npoints
    context = {'color': '#5f5aad', 'label': 'Jobs', 'barWidth': barwidth, 'data': data}
    return JsonResponse(context)


def alljobswidget(request):
    hours = 12
    npoints = 100
    data = generate_series_stats(parname='bacula.jobs.all', npoints=npoints, hours=hours)
    context = {'color': '#001d41', 'label': 'Jobs', 'data': data}
    return JsonResponse(context)


def helppage(request, page):
    context = {}
    try:
        return render(request, 'helps/' + page + '.html', context)
    except:
        return HttpResponse('No help available for this topic.')

@@ -1326,23 +1326,8 @@
 t):%0A
-    hours = 24%0A
     
@@ -1339,24 +1339,26 @@
 ts = 200%0A   
+ #
  data = gene
@@ -1448,16 +1448,99 @@
 value')%0A
+    data = generate_series_fvalue_fast(parname='system.cpu.util', npoints=npoints)%0A
     cont
@@ -1664,39 +1664,24 @@
 t(request):%0A
-    hours = 12%0A
     npoints 
@@ -1686,14 +1686,16 @@
 s = 
-5
+6
 0%0A   
+ #
  dat
@@ -1824,16 +1824,124 @@
 l=False%0A
+    data = generate_series_nvalue_fast(parname='bacula.size.jobs.bytes', npoints=npoints, div=1073741824.0)%0A
     barw
@@ -1947,33 +1947,24 @@
 width = 
-(hours * 2
+10
 000000
-)
  / npoin
@@ -2119,39 +2119,24 @@
 t(request):%0A
-    hours = 12%0A
     npoints 
@@ -2129,34 +2129,33 @@
 :%0A    npoints = 
-10
+3
 0%0A    data = gen
@@ -2159,37 +2159,43 @@
 generate_series_
-stats
+nvalue_fast
 (parname='bacula
@@ -2221,37 +2221,24 @@
 ints=npoints
-, hours=hours
 )%0A    barwid
@@ -2246,25 +2246,15 @@
 h = 
-(hours * 2
+8
 000000
-)
  / n
@@ -2415,23 +2415,8 @@
 t):%0A
-    hours = 12%0A
     
@@ -2425,18 +2425,17 @@
 oints = 
-10
+3
 0%0A    da
@@ -2447,37 +2447,43 @@
 generate_series_
-stats
+nvalue_fast
 (parname='bacula
@@ -2509,29 +2509,16 @@
 =npoints
-, hours=hours
 )%0A    co

Add stat_*_fast to the all dashboard plot widgets.