fixed util.stats & util.states for python 3.7

This commit is contained in:
Adolfo Gómez García 2019-08-06 11:49:08 +02:00
parent e103fb02d2
commit 6fac308d80
15 changed files with 76 additions and 231 deletions

View File

@ -66,7 +66,7 @@ class StatsManager:
model.objects.filter(stamp__lt=minTime).delete()
# Counter stats
def addCounter(self, owner_type: int, owner_id: int, counterType: int, counterValue: int, stamp: typing.Optional[datetime.datetime] = None):
def addCounter(self, owner_type: int, owner_id: int, counterType: int, counterValue: int, stamp: typing.Optional[datetime.datetime] = None) -> bool:
"""
Adds a new counter stats to database.

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 Virtual Cable S.L.
# Copyright (c) 2013-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -25,16 +25,15 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import unicode_literals
from uds.core.util.Config import GlobalConfig
from django.http import HttpResponseRedirect
import logging
from django.http import HttpResponseRedirect
from uds.core.util.Config import GlobalConfig
logger = logging.getLogger(__name__)
class XUACompatibleMiddleware(object):
class XUACompatibleMiddleware:
"""
Add a X-UA-Compatible header to the response
This header tells to Internet Explorer to render page with latest
@ -50,7 +49,7 @@ class XUACompatibleMiddleware(object):
return response
class RedirectMiddleware(object):
class RedirectMiddleware:
NO_REDIRECT = [
'rest',
'pam',
@ -68,7 +67,7 @@ class RedirectMiddleware(object):
redirect = False
break
if GlobalConfig.REDIRECT_TO_HTTPS.getBool() and request.is_secure() is False and redirect:
if redirect and request.is_secure() is False and GlobalConfig.REDIRECT_TO_HTTPS.getBool():
if request.method == 'POST':
url = request.build_absolute_uri(GlobalConfig.LOGIN_URL.get())
else:

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Virtual Cable S.L.
# Copyright (c) 2012-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -40,4 +40,3 @@ from . import publication
from . import userService
from . import servicePool
from . import task
from . import userService

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,5 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
__updated__ = '2016-02-19'
ALLOW = 'ALLOW'
DENY = 'DENY'

View File

@ -30,14 +30,9 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
from django.utils.translation import ugettext_noop as _, ugettext_lazy
import six
import logging
__updated__ = '2016-02-19'
from django.utils.translation import ugettext_noop as _, ugettext_lazy
logger = logging.getLogger(__name__)
@ -155,4 +150,4 @@ def dictionary():
"""
Returns a dictionary with current active locale translation of States to States String
"""
return dict([(k, ugettext_lazy(v)) for k, v in six.iteritems(string)])
return {k: ugettext_lazy(v) for k, v in string.items()}

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,6 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import ACTIVE # @UnusedImport
__updated__ = '2016-02-19'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,5 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import ERROR, FINISHED, RUNNING # @UnusedImport
__updated__ = '2016-02-19'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,7 +30,6 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import ERROR, USABLE, REMOVABLE, REMOVED, CANCELED, LAUNCHING, PREPARING # @UnusedImport

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,7 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import ACTIVE, REMOVABLE, REMOVING, REMOVED # @UnusedImport
__updated__ = '2016-02-19'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,6 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import FOR_EXECUTE # @UnusedImport
__updated__ = '2016-02-19'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Virtual Cable S.L.
# Copyright (c) 2015-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,9 +30,6 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
# pylint: disable=unused-import
from .common import ERROR, USABLE, PREPARING, REMOVABLE, REMOVING, REMOVED # @UnusedImport
__updated__ = '2016-02-19'

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 Virtual Cable S.L.
# Copyright (c) 2013-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,

View File

@ -1,121 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
import datetime
import cairo
import pycha.line
import time
import six
from uds.models import getSqlDatetime
import counters
# Chart types
CHART_TYPE_LINE, CHART_TYPE_AREA, CHART_TYPE_BAR = range(3) # @UndefinedVariable
__typeTitles = None
def make(obj, counterType, **kwargs):
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((int(time.mktime(x[0].timetuple())), x[1]) for x in 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('iso-8859-1', errors='ignore'), 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,
}
},
'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.render()
output = six.StringIO()
surface.write_to_png(output)
return output.getvalue()

View File

@ -30,14 +30,13 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
import datetime
import logging
import typing
from uds.models import NEVER
from django.utils.translation import ugettext_lazy as _
from uds.core.managers import statsManager
import datetime
import logging
from uds.models import NEVER
logger = logging.getLogger(__name__)
@ -48,13 +47,13 @@ logger = logging.getLogger(__name__)
CT_LOAD, CT_STORAGE, CT_ASSIGNED, CT_INUSE,
) = range(4)
__caRead = None
__caWrite = None
__transDict = None
__typeTitles = None
__caRead: typing.Dict = {}
__caWrite: typing.Dict = {}
__transDict: typing.Dict = {}
__typeTitles: typing.Dict = {}
def addCounter(obj, counterType, counterValue, stamp=None):
def addCounter(obj: typing.Any, counterType: int, counterValue: int, stamp: typing.Optional[datetime.datetime] = None) -> bool:
"""
Adds a counter stat to specified object
@ -64,14 +63,15 @@ def addCounter(obj, counterType, counterValue, stamp=None):
note: Runtime checks are done so if we try to insert an unssuported stat, this won't be inserted and it will be logged
"""
if type(obj) not in __caWrite.get(counterType, ()):
logger.error('Type {0} does not accepts counter of type {1}'.format(type(obj), counterValue))
type_ = type(obj)
if type_ not in __caWrite.get(counterType, ()): # pylint: disable
logger.error('Type %s does not accepts counter of type %s', type_, counterValue)
return False
return statsManager().addCounter(__transDict[type(obj)], obj.id, counterType, counterValue, stamp)
def getCounters(obj, counterType, **kwargs):
def getCounters(obj: typing.Any, counterType: int, **kwargs):
"""
Get counters
@ -92,20 +92,21 @@ def getCounters(obj, counterType, **kwargs):
to = kwargs.get('to', datetime.datetime.now())
limit = kwargs.get('limit', 1000)
use_max = kwargs.get('use_max', False)
type_ = type(obj)
readFncTbl = __caRead.get(type(obj), None)
readFncTbl = __caRead.get(type_)
if readFncTbl is None:
logger.error('Type {0} has no registered stats'.format(type(obj)))
if not readFncTbl:
logger.error('Type %s has no registered stats', type_)
return
fnc = readFncTbl.get(counterType, None)
fnc = readFncTbl.get(counterType)
if fnc is None:
logger.error('Type {0} has no registerd stats of type {1}'.format(type(obj), counterType))
if not fnc:
logger.error('Type %s has no registerd stats of type %s', type_, counterType)
return
if kwargs.get('all', None) is not True:
if not kwargs.get('all', False):
owner_ids = fnc(obj)
else:
owner_ids = None
@ -128,17 +129,12 @@ def _initializeData():
"""
from uds.models import Provider, Service, DeployedService
global __caWrite
global __caRead
global __transDict
global __typeTitles
__caWrite = {
__caWrite.update({
CT_LOAD: (Provider,),
CT_STORAGE: (Service,),
CT_ASSIGNED: (DeployedService,),
CT_INUSE: (DeployedService,),
}
})
# OBtain ids from variups type of object to retrieve stats
def get_Id(obj):
@ -156,23 +152,23 @@ def _initializeData():
res += get_S_DS_Ids(i)
return res
__caRead = {
Provider: {
CT_LOAD: get_Id,
CT_STORAGE: get_P_S_Ids,
CT_ASSIGNED: get_P_S_DS_Ids,
CT_INUSE: get_P_S_DS_Ids
},
Service: {
CT_STORAGE: get_Id,
CT_ASSIGNED: get_S_DS_Ids,
CT_INUSE: get_S_DS_Ids
},
DeployedService: {
CT_ASSIGNED: get_Id,
CT_INUSE: get_Id
}
}
__caRead.update({
Provider: {
CT_LOAD: get_Id,
CT_STORAGE: get_P_S_Ids,
CT_ASSIGNED: get_P_S_DS_Ids,
CT_INUSE: get_P_S_DS_Ids
},
Service: {
CT_STORAGE: get_Id,
CT_ASSIGNED: get_S_DS_Ids,
CT_INUSE: get_S_DS_Ids
},
DeployedService: {
CT_ASSIGNED: get_Id,
CT_INUSE: get_Id
}
})
def _getIds(obj):
to = type(obj)
@ -194,19 +190,19 @@ def _initializeData():
# Dict to convert objects to owner types
# Dict for translations
__transDict = {
__transDict.update({
DeployedService: OT_DEPLOYED,
Service: OT_SERVICE,
Provider: OT_PROVIDER
}
})
# Titles of types
__typeTitles = {
__typeTitles.update({
CT_ASSIGNED: _('Assigned'),
CT_INUSE: _('In use'),
CT_LOAD: _('Load'),
CT_STORAGE: _('Storage')
}
})
_initializeData()

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 Virtual Cable S.L.
# Copyright (c) 2013-2019 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -30,16 +30,16 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
import datetime
import logging
import typing
from uds.models import NEVER
from uds.core.managers import statsManager
import datetime
import logging
logger = logging.getLogger(__name__)
EventTupleType = typing.Tuple[datetime.datetime, str, str, str, str, int]
# Posible events, note that not all are used by every possible owner type
(
@ -60,10 +60,10 @@ logger = logging.getLogger(__name__)
) = range(4)
__transDict = None
__transDict: typing.Dict = {}
def addEvent(obj, eventType, **kwargs):
def addEvent(obj: typing.Any, eventType: int, **kwargs) -> bool:
"""
Adds a event stat to specified object
@ -77,7 +77,7 @@ def addEvent(obj, eventType, **kwargs):
return statsManager().addEvent(__transDict[type(obj)], obj.id, eventType, **kwargs)
def getEvents(obj, eventType, **kwargs):
def getEvents(obj: typing.Any, eventType: int, **kwargs) -> typing.Generator[EventTupleType, None, None]:
"""
Get events
@ -96,13 +96,14 @@ def getEvents(obj, eventType, **kwargs):
since = kwargs.get('since', NEVER)
to = kwargs.get('to', datetime.datetime.now())
type_ = type(obj)
if kwargs.get('all', False) is True:
if kwargs.get('all', False):
owner_id = None
else:
owner_id = obj.pk
for i in statsManager().getEvents(__transDict[type(obj)], eventType, owner_id=owner_id, since=since, to=to):
for i in statsManager().getEvents(__transDict[type_], eventType, owner_id=owner_id, since=since, to=to):
val = (datetime.datetime.fromtimestamp(i.stamp), i.fld1, i.fld2, i.fld3, i.fld4, i.event_type)
yield val
@ -111,21 +112,17 @@ def getEvents(obj, eventType, **kwargs):
def _initializeData():
"""
Initializes dictionaries.
Hides data from global var space
"""
from uds.models import Provider, Service, DeployedService, Authenticator
global __transDict
# Dict to convert objects to owner types
# Dict for translations
__transDict = {
__transDict.update({
DeployedService: OT_DEPLOYED,
Service: OT_SERVICE,
Provider: OT_PROVIDER,
Authenticator: OT_AUTHENTICATOR,
}
})
_initializeData()