Removed all pycha references from project. All charts are now created

using matplotlib
This commit is contained in:
Adolfo Gómez García 2018-04-25 12:15:05 +02:00
parent 1406b036f7
commit 496e5fb60d
7 changed files with 111 additions and 108 deletions

View File

@ -36,7 +36,6 @@ from uds.models import StatsCounters
from uds.models import getSqlDatetime
from uds.models import StatsEvents
from uds.models import optimizeTable
from django.db import connection
import datetime
import time
import six
@ -171,6 +170,7 @@ class StatsManager(object):
stamp = int(time.mktime(stamp.timetuple())) # pylint: disable=maybe-no-member
try:
# Replaces nulls for ''
def noneToEmpty(value):
return six.text_type(value) if value is not None else ''

View File

@ -35,6 +35,9 @@ import logging
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
logger = logging.getLogger(__name__)
@ -45,8 +48,9 @@ def barChart(size, data, output):
ind = np.arange(len(d))
ys = data['y']
width = 0.35
width = 0.60
fig = Figure(figsize=(size[0], size[1]), dpi=size[2])
FigureCanvas(fig) # Stores canvas on fig.canvas
axis = fig.add_subplot(111)
axis.grid(color='r', linestyle='dotted', linewidth=0.1, alpha=0.5)
@ -67,7 +71,6 @@ def barChart(size, data, output):
axis.set_xticklabels([data['xtickFnc'](v) for v in axis.get_xticks()])
axis.legend()
FigureCanvas(fig) # Stores canvas on fig.canvas
fig.savefig(output, format='png', transparent=True)
@ -77,6 +80,7 @@ def lineChart(size, data, output):
y = data['y']
fig = Figure(figsize=(size[0], size[1]), dpi=size[2])
FigureCanvas(fig) # Stores canvas on fig.canvas
axis = fig.add_subplot(111)
axis.grid(color='r', linestyle='dotted', linewidth=0.1, alpha=0.5)
@ -97,6 +101,49 @@ def lineChart(size, data, output):
axis.set_xticklabels([data['xtickFnc'](v) for v in axis.get_xticks()])
axis.legend()
FigureCanvas(fig) # Stores canvas on fig.canvas
fig.savefig(output, format='png', transparent=True)
def surfaceChart(size, data, output):
x = data['x']
y = data['y']
z = data['z']
logger.debug('X: {}'.format(x))
logger.debug('Y: {}'.format(y))
logger.debug('Z: {}'.format(z))
x, y = np.meshgrid(x, y)
z = np.array(z)
logger.debug('X\': {}'.format(x))
logger.debug('Y\': {}'.format(y))
logger.debug('Z\': {}'.format(z))
fig = Figure(figsize=(size[0], size[1]), dpi=size[2])
FigureCanvas(fig) # Stores canvas on fig.canvas
axis = fig.add_subplot(111, projection='3d')
# axis.grid(color='r', linestyle='dotted', linewidth=0.1, alpha=0.5)
if data.get('wireframe', False) is True:
axis.plot_wireframe(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm) # @UndefinedVariable
else:
axis.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm) # @UndefinedVariable
axis.set_title(data.get('title', ''))
axis.set_xlabel(data['xlabel'])
axis.set_ylabel(data['ylabel'])
axis.set_zlabel(data['zlabel'])
if data.get('allTicks', True) is True:
axis.set_xticks(data['x'])
axis.set_yticks(data['y'])
if 'xtickFnc' in data:
axis.set_xticklabels([data['xtickFnc'](v) for v in axis.get_xticks()])
if 'ytickFnc' in data:
axis.set_yticklabels([data['ytickFnc'](v) for v in axis.get_yticks()])
fig.savefig(output, format='png', transparent=True)

View File

@ -49,10 +49,7 @@ import logging
logger = logging.getLogger(__name__)
__updated__ = '2018-04-24'
# several constants as Width height, margins, ..
WIDTH, HEIGHT = 1920, 1080
__updated__ = '2018-04-25'
class UsageSummaryByPool(StatsReport):

View File

@ -30,35 +30,28 @@
"""
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
from django.utils.translation import ugettext, ugettext_lazy as _
from uds.core.ui.UserInterface import gui
from uds.core.util.stats import counters
import six
import csv
import io
import datetime
import logging
from .base import StatsReport
from uds.models import ServicePool
import cairo
# import pycha.line
# import pycha.bar
import pycha.stackedbar
import datetime
import logging
import six
from uds.core.reports import graphs
logger = logging.getLogger(__name__)
__updated__ = '2018-02-08'
__updated__ = '2018-04-25'
# several constants as Width height, margins, ..
WIDTH, HEIGHT = 1920, 1080
WIDTH, HEIGHT, DPI = 19.2, 10.8, 100
SIZE = (WIDTH, HEIGHT, DPI)
class CountersPoolAssigned(StatsReport):
@ -108,7 +101,7 @@ class CountersPoolAssigned(StatsReport):
hours = {}
for i in range(24):
hours[i] = 0
hours[i] = i * i
for x in counters.getCounters(pool, counters.CT_ASSIGNED, since=start, to=end, limit=24, use_max=True, all=False):
hour = x[0].hour
@ -125,76 +118,24 @@ class CountersPoolAssigned(StatsReport):
def generate(self):
items = self.getData()
graph1 = six.BytesIO()
graph1 = io.BytesIO()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) # @UndefinedVariable
options = {
'encoding': 'utf-8',
'axis': {
'x': {
'ticks': [
dict(v=i, label='{:02d}'.format(i)) for i in range(24)
],
'range': (0, 23),
'showLines': True,
},
'y': {
'tickCount': 10,
'showLines': True,
},
'tickFontSize': 16,
},
'background': {
'chartColor': '#f0f0f0',
'baseColor': '#f0f0f0',
'lineColor': '#187FF2'
},
'colorScheme': {
'name': 'gradient',
'args': {
'initialColor': 'blue',
},
},
'legend': {
'hide': False,
'legendFontSize': 16,
'position': {
'left': 96,
'top': 40,
}
},
'padding': {
'left': 48,
'top': 16,
'right': 48,
'bottom': 48,
},
X = list(range(24))
d = {
'title': _('Services by hour'),
'x': X,
'xtickFnc': lambda l: '{:02d}'.format(l),
'xlabel': _('Hour'),
'y': [
{
'label': i['name'],
'data': [i['hours'][v] for v in X]
} for i in items
],
'ylabel': 'Services'
}
# chart = pycha.line.LineChart(surface, options)
# chart = pycha.bar.VerticalBarChart(surface, options)
chart = pycha.stackedbar.StackedVerticalBarChart(surface, options)
dataset = []
for pool in items:
logger.debug(pool['hours'])
ds = list((i, pool['hours'][i]) for i in range(24))
# logger.debug(ds)
dataset.append((ugettext('Services for {}').format(pool['name']), ds))
# logger.debug('Dataset: {}'.format(dataset))
chart.addDataset(dataset)
chart.render()
surface.write_to_png(graph1)
del chart
del surface # calls finish, flushing to image
logger.debug('Items: {}'.format(items))
graphs.barChart(SIZE, d, graph1)
return self.templateAsPDF(
'uds/reports/stats/pools-usage-day.html',
@ -220,7 +161,7 @@ class CountersPoolAssignedCSV(CountersPoolAssigned):
pools = CountersPoolAssigned.pools
def generate(self):
output = six.StringIO()
output = io.StringIO()
writer = csv.writer(output)
writer.writerow([ugettext('Pool'), ugettext('Hour'), ugettext('Services')])

View File

@ -49,10 +49,7 @@ import logging
logger = logging.getLogger(__name__)
__updated__ = '2018-02-08'
# several constants as Width height, margins, ..
WIDTH, HEIGHT = 1920, 1080
__updated__ = '2018-04-25'
class UsageByPool(StatsReport):

View File

@ -48,7 +48,7 @@ from uds.core.util import tools
logger = logging.getLogger(__name__)
__updated__ = '2018-04-24'
__updated__ = '2018-04-25'
# several constants as Width height
WIDTH, HEIGHT, DPI = 19.2, 10.8, 100
@ -142,12 +142,15 @@ class StatsReportLogin(StatsReport):
dataWeek = [0] * 7
dataHour = [0] * 24
dataWeekHour = [[0] * 24 for _ in range(7)]
for val in events.statsManager().getEvents(events.OT_AUTHENTICATOR, events.ET_LOGIN, since=start, to=end):
s = datetime.datetime.fromtimestamp(val.stamp)
dataWeek[s.weekday()] += 1
dataHour[s.hour] += 1
dataWeekHour[s.weekday()][s.hour] += 1
logger.debug('Data: {} {}'.format(s.weekday(), s.hour))
return dataWeek, dataHour
return dataWeek, dataHour, dataWeekHour
def generate(self):
# Sample query:
@ -157,10 +160,6 @@ class StatsReportLogin(StatsReport):
# ' GROUP BY CEIL(stamp/(3600))'
# ' ORDER BY block'
# Generate the sampling intervals and get data from db
start = self.startDate.stamp()
end = self.endDate.stamp()
xLabelFormat, data, reportData = self.getRangeData()
#
@ -188,14 +187,15 @@ class StatsReportLogin(StatsReport):
graph2 = io.BytesIO()
graph3 = io.BytesIO()
dataWeek, dataHour = self.getWeekHourlyData()
graph4 = io.BytesIO()
dataWeek, dataHour, dataWeekHour = self.getWeekHourlyData()
X = list(range(7))
d = {
'title': _('Users Access (by week)'),
'x': X,
'xtickFnc': lambda l: [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday')][l],
'xlabel': _('Day of Week'),
'xlabel': _('Day of week'),
'y': [
{
'label': 'Users',
@ -223,6 +223,23 @@ class StatsReportLogin(StatsReport):
graphs.barChart(SIZE, d, graph3)
X = list(range(24))
Y = list(range(7))
d = {
'title': _('Users Access (by hour)'),
'x': X,
'xlabel': _('Hour'),
'xtickFnc': lambda l: l,
'y': Y,
'ylabel': _('Day of week'),
'ytickFnc': lambda l: [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday')][l],
'z': dataWeekHour,
'zlabel': _('Users')
}
graphs.surfaceChart(SIZE, d, graph4)
return self.templateAsPDF(
'uds/reports/stats/user-access.html',
dct={
@ -233,7 +250,7 @@ class StatsReportLogin(StatsReport):
},
header=ugettext('Users access to UDS'),
water=ugettext('UDS Report for users access'),
images={'graph1': graph1.getvalue(), 'graph2': graph2.getvalue(), 'graph3': graph3.getvalue()},
images={'graph1': graph1.getvalue(), 'graph2': graph2.getvalue(), 'graph3': graph3.getvalue(), 'graph4': graph4.getvalue()},
)
@ -251,7 +268,7 @@ class StatsReportLoginCSV(StatsReportLogin):
samplingPoints = StatsReportLogin.samplingPoints
def generate(self):
output = six.StringIO()
output = io.StringIO()
writer = csv.writer(output)
reportData = self.getRangeData()[2]

View File

@ -11,15 +11,19 @@
<p style="text-align: center">
{% trans "Users Access by date" %}
</p>
<img src="image://graph1" class="center" style="height: 7.2cm;">
<img src="image://graph1" class="center" style="height: 10.0cm;">
<p style="text-align: center">
{% trans "Users Access by day of week" %}
</p>
<img src="image://graph2" class="center" style="height: 7.2cm;">
<p style="text-align: center">
<img src="image://graph2" class="center" style="height: 10.0cm;">
<p style="page-break-before: always; text-align: center;">
{% trans "Users Access by hour" %}
</p>
<img src="image://graph3" class="center" style="height: 7.2cm;">
<img src="image://graph3" class="center" style="height: 10.0cm;">
<p style="text-align: center">
{% trans "Users Access by day of week/hour" %}
</p>
<img src="image://graph4" class="center" style="height: 10.0cm;">
<p style="page-break-before: always; text-align: center;">
{% trans "Users Access by date" %}