1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-11 00:58:39 +03:00

Doing first charts tests

This commit is contained in:
Adolfo Gómez 2013-02-11 10:24:47 +00:00
parent 6a49943c67
commit 8a35b7bd69
4 changed files with 112 additions and 8 deletions

View File

@ -112,7 +112,7 @@ class StatsManager(object):
'''
pass
def getCounters(self, ownerType, counterType, ownerIds, since, to, limit):
def getCounters(self, ownerType, counterType, ownerIds, since, to, limit, use_max = False):
'''
Retrieves counters from item
@ -129,8 +129,13 @@ class StatsManager(object):
Iterator, containing (date, counter) each element
'''
from uds.models import StatsCounters
import time
# To Unix epoch
since = int(time.mktime(since.timetuple()))
to = int(time.mktime(to.timetuple()))
return StatsCounters.get_grouped(ownerType, counterType, owner_id = ownerIds, since = since, to = to, limit = limit)
return StatsCounters.get_grouped(ownerType, counterType, owner_id = ownerIds, since = since, to = to, limit = limit, use_max = use_max)
def cleanupCounter(self):

View File

@ -29,10 +29,90 @@
'''
@author: Adolfo Gómez, dkmaster at dkmon dot com
'''
import datetime
import cairo
import pycha.line
from uds.models import getSqlDatetime
import counters
# Chart types
CHART_TYPE_LINE, CHART_TYPE_AREA, CHART_TYPE_BAR = xrange(2)
CHART_TYPE_LINE, CHART_TYPE_AREA, CHART_TYPE_BAR = xrange(3)
def make(fromWhatStat, **kwargs):
__typeTitles = None
def make(obj, counterType, **kwargs):
pass
width, height = (kwargs.get('width', 800), kwargs.get('height', 600))
since = kwargs.get('since', None)
to = kwargs.get('to', None)
if since is None and to is None:
interval = kwargs.get('interval', None)
if interval is not None:
to = getSqlDatetime()
since = to - datetime.timedelta(days=interval)
limit = width
dataset1 = tuple(counters.getCounters(obj, counterType, since=since, to=to, limit=limit, use_max = kwargs.get('use_max', False)))
if len(dataset1) == 0:
dataset1 = ( (getSqlDatetime(True)-3600, 0), (getSqlDatetime(True), 0) )
firstLast = (dataset1[0][0], getSqlDatetime(True))
xLabelFormat = '%y-%m-%d'
diffInterval = firstLast[1] - firstLast[0]
if diffInterval <= 60*60*24: # Less than one day
xLabelFormat = '%H:%M'
elif diffInterval <= 60*60*24*7:
xLabelFormat = '%A'
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
dataset = ( ( counters.getCounterTitle(counterType).encode('ascii'), dataset1 ),)
options = {
'axis': {
'x': {
'ticks': [dict(v=i, label=datetime.datetime.fromtimestamp(i).strftime(xLabelFormat)) for i in firstLast],
'range': (firstLast[0], firstLast[1])
},
'y': {
'tickCount': 4,
}
},
'legend': {'hide': True},
'background': {
'chartColor': '#ffeeff',
'baseColor': '#ffffff',
'lineColor': '#444444'
},
'colorScheme': {
'name': 'gradient',
'args': {
'initialColor': 'red',
},
},
'legend': {
'hide': True,
},
'padding': {
'left': 0,
'bottom': 0,
},
'title': 'Sample Chart'
}
chart = pycha.line.LineChart(surface, options)
chart.addDataset(dataset)
chart.minxval = dataset1[0][0]
chart.render()
surface.write_to_png('/tmp/test.png')
def _initialize():
global __typeTitles

View File

@ -29,6 +29,7 @@
'''
@author: Adolfo Gómez, dkmaster at dkmon dot com
'''
from django.utils.translation import ugettext_lazy as _
from uds.core.managers import statsManager
import logging
@ -44,6 +45,7 @@ logger = logging.getLogger(__name__)
__caRead = None
__caWrite = None
__transDict = None
__typeTitles = None
def addCounter(obj, counterType, counterValue, stamp = None):
@ -82,6 +84,7 @@ def getCounters(obj, counterType, **kwargs):
since = kwargs.get('since', None)
to = kwargs.get('to', None)
limit = kwargs.get('limit', 1000)
use_max = kwargs.get('use_max', False)
readFncTbl = __caRead.get(type(obj), None)
@ -98,9 +101,12 @@ def getCounters(obj, counterType, **kwargs):
owner_ids = fnc(obj)
for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, limit):
for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, limit, use_max):
val = (i.stamp, i.value)
yield val
def getCounterTitle(counterType):
return __typeTitles.get(counterType, '').title()
# Data initialization
def _initializeData():
@ -114,6 +120,7 @@ def _initializeData():
global __caWrite
global __caRead
global __transDict
global __typeTitles
__caWrite = {
CT_LOAD: (Provider,),
@ -184,3 +191,11 @@ def _initializeData():
Provider : OT_PROVIDER
}
# Titles of types
__typeTitles = {
CT_ASSIGNED: _('Assigned'),
CT_INUSE: _('In use'),
CT_LOAD: _('Load'),
CT_STORAGE: _('Storage')
}

View File

@ -1646,6 +1646,7 @@ class StatsCounters(models.Model):
limit = kwargs.get('limit', None)
if limit is not None:
limit = int(limit)
elements = kwargs['limit']
@ -1671,10 +1672,13 @@ class StatsCounters(models.Model):
filt += ' AND stamp>={0} AND stamp<={1} GROUP BY CEIL(stamp/{2}) ORDER BY stamp'.format(
since, to, interval)
fnc = kwargs.get('use_max', False) and 'MAX' or 'AVG'
query = ('SELECT -1 as id,-1 as owner_id,-1 as owner_type,-1 as counter_type,stamp,'
'CEIL(AVG(value)) AS value '
'FROM {0} WHERE {1}').format(StatsCounters._meta.db_table, filt)
'CEIL({0}(value)) AS value '
'FROM {1} WHERE {2}').format(fnc, StatsCounters._meta.db_table, filt)
# We use result as an iterator
return StatsCounters.objects.raw(query)