Skip to content

Commit

Permalink
- pep8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed May 9, 2018
1 parent 659b23a commit eb5de19
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 113 deletions.
137 changes: 81 additions & 56 deletions geonode/contrib/monitoring/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
#########################################################################
#
#
# Copyright (C) 2017 OSGeo
#
Expand All @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
#
from __future__ import print_function

import logging
Expand Down Expand Up @@ -66,6 +66,7 @@ def get_geoip():


class Host(models.Model):

"""
Describes one physical instance
"""
Expand All @@ -82,6 +83,7 @@ def __str__(self):


class ServiceType(models.Model):

"""
Service Type list
"""
Expand Down Expand Up @@ -111,6 +113,7 @@ def is_system_monitor(self):


class Service(models.Model):

"""
Service is a entity describing deployed processes.
"""
Expand Down Expand Up @@ -343,7 +346,8 @@ class RequestEvent(models.Model):
# map=some map
#
# list is separated with newline
# resources = models.TextField(blank=True, default='', help_text=_("Resources name (style, layer, document, map)"))
# resources = models.TextField(blank=True, default='',
# help_text=_("Resources name (style, layer, document, map)"))
resources = models.ManyToManyField(MonitoredResource, blank=True,
help_text=_(
"List of resources affected"),
Expand Down Expand Up @@ -445,26 +449,27 @@ def from_geonode(cls, service, request, response):
ip = '127.0.0.1'
try:
ip = gethostbyname(ip)
except Exception, err:
except Exception as err:
pass

geoip = get_geoip()
try:
client_loc = geoip.city(ip)
except Exception, err:
log.warning("Cannot resolve %s: %s", ip,err)
except Exception as err:
log.warning("Cannot resolve %s: %s", ip, err)
client_loc = None

if client_loc:
lat, lon = client_loc['latitude'], client_loc['longitude'],
country = client_loc.get('country_code3') or client_loc['country_code']
country = client_loc.get(
'country_code3') or client_loc['country_code']
if len(country) == 2:
_c = pycountry.countries.get(alpha_2=country)
country = _c.alpha_3

region = client_loc['region']
city = client_loc['city']

data = {'received': received,
'created': created,
'host': request.get_host(),
Expand All @@ -473,7 +478,8 @@ def from_geonode(cls, service, request, response):
'request_path': request.get_full_path(),
'request_method': request.method,
'response_status': response.status_code,
'response_size': response.get('Content-length') or len(response.getvalue()),
'response_size':
response.get('Content-length') or len(response.getvalue()),
'response_type': response.get('Content-type'),
'response_time': duration,
'user_agent': ua,
Expand Down Expand Up @@ -516,13 +522,14 @@ def from_geoserver(cls, service, request_data, received=None):
geoip = get_geoip()
try:
client_loc = geoip.city(ip)
except Exception, err:
log.warning("Cannot resolve %s: %s", ip,err)
except Exception as err:
log.warning("Cannot resolve %s: %s", ip, err)
client_loc = None

if client_loc:
lat, lon = client_loc['latitude'], client_loc['longitude'],
country = client_loc.get('country_code3') or client_loc['country_code']
country = client_loc.get(
'country_code3') or client_loc['country_code']
if len(country) == 2:
_c = pycountry.countries.get(alpha_2=country)
country = _c.alpha_3
Expand All @@ -546,7 +553,9 @@ def from_geoserver(cls, service, request_data, received=None):
'host': rd['host'],
'ows_service': OWSService.get(rd.get('service')),
'service': service,
'request_path': '{}?{}'.format(rd['path'], rd['queryString']) if rd.get('queryString') else rd['path'],
'request_path':
'{}?{}'.format(rd['path'], rd['queryString']) if rd.get(
'queryString') else rd['path'],
'request_method': rd['httpMethod'],
'response_status': rd['responseStatus'],
'response_size': rl[0] if isinstance(rl, list) else rl,
Expand All @@ -568,7 +577,12 @@ def from_geoserver(cls, service, request_data, received=None):
resources = cls._get_resources('layer', resource_names)
if rd.get('error'):
try:
etype = rd['error']['@class'] if '@class' in rd['error'] else rd['error']['class']
etype = rd[
'error'][
'@class'] if '@class' in rd[
'error'] else rd[
'error'][
'class']
edata = '\n'.join(rd['error']['stackTrace']['trace'])
emessage = rd['error']['detailMessage']
ExceptionEvent.add_error(
Expand Down Expand Up @@ -597,7 +611,7 @@ class ExceptionEvent(models.Model):
def add_error(cls, from_service, error_type, stack_trace,
request=None, created=None, message=None):
received = datetime.utcnow().replace(tzinfo=pytz.utc)
if not isinstance(error_type, types.StringTypes):
if not isinstance(error_type, (str,)):
_cls = error_type.__class__
error_type = '{}.{}'.format(_cls.__module__, _cls.__name__)
if not message:
Expand Down Expand Up @@ -855,8 +869,9 @@ class NotificationCheck(models.Model):
null=True,
blank=True,
help_text=_("Marker of last delivery"))
grace_period = models.DurationField(null=False, default=GRACE_PERIOD_10M, choices=GRACE_PERIODS,
help_text=_("Minimum time between subsequent notifications"))
grace_period = models.DurationField(
null=False, default=GRACE_PERIOD_10M, choices=GRACE_PERIODS,
help_text=_("Minimum time between subsequent notifications"))
severity = models.CharField(max_length=32,
null=False,
default=SEVERITY_ERROR,
Expand Down Expand Up @@ -946,8 +961,8 @@ def check_for(cls, for_timestamp=None, active=None):

@classmethod
def get_steps(cls, min_, max_, thresholds):
if isinstance(thresholds, (types.IntType, types.LongType,
types.FloatType, Decimal,)):
if isinstance(thresholds, (int, int,
float, Decimal,)):
if min_ is None or max_ is None:
raise ValueError(
"Cannot use numeric threshold if one of min/max is None")
Expand Down Expand Up @@ -996,14 +1011,14 @@ def create(cls, name, description, user_threshold, severity=None):

metric = Metric.objects.get(name=metric_name)
steps = cls.get_steps(minimum, maximum, thresholds)
nm = NotificationMetricDefinition.objects.create(notification_check=inst,
metric=metric,
description=_description,
min_value=minimum,
max_value=maximum,
steps=len(
steps) if steps else None,
field_option=field_opt)
nm = NotificationMetricDefinition.objects.create(
notification_check=inst,
metric=metric,
description=_description,
min_value=minimum,
max_value=maximum,
steps=len(steps) if steps else None,
field_option=field_opt)
user_thresholds[nm.field_name] = {'min': minimum,
'max': maximum,
'metric': metric_name,
Expand Down Expand Up @@ -1034,10 +1049,11 @@ class F(forms.Form):
grace_period = forms.DurationField(required=False)

def __init__(self, *args, **kwargs):
initial = {'emails': list(this.get_emails()) + [u.email for u in this.get_users()],
'severity': this.severity,
'active': this.active,
'grace_period': this.grace_period}
initial = {
'emails': list(this.get_emails()) + [u.email for u in this.get_users()],
'severity': this.severity,
'active': this.active,
'grace_period': this.grace_period}
kwargs['initial'] = initial
super(F, self).__init__(*args, **kwargs)
fields = self.fields
Expand Down Expand Up @@ -1095,10 +1111,11 @@ def process_user_form(self, data, is_json=False):
if field == 'max_timeout':
val = timedelta(seconds=int(val))
ndef = self.get_definition_for(key)
ncheck = MetricNotificationCheck.objects.create(notification_check=inst,
metric=metric,
definition=ndef,
**{field: val})
ncheck = MetricNotificationCheck.objects.create(
notification_check=inst,
metric=metric,
definition=ndef,
**{field: val})
out.append(ncheck)
U = get_user_model()
self.receivers.all().delete()
Expand Down Expand Up @@ -1136,11 +1153,10 @@ class NotificationMetricDefinition(models.Model):
FIELD_OPTION_MIN_VALUE = 'min_value'
FIELD_OPTION_MAX_VALUE = 'max_value'
FIELD_OPTION_MAX_TIMEOUT = 'max_timeout'
FIELD_OPTION_CHOICES = ((FIELD_OPTION_MIN_VALUE, _("Value must be above"),),
(FIELD_OPTION_MAX_VALUE, _("Value must be below"),),
(FIELD_OPTION_MAX_TIMEOUT, _(
"Last update must not be older than"),)
)
FIELD_OPTION_CHOICES = (
(FIELD_OPTION_MIN_VALUE, _("Value must be above"),),
(FIELD_OPTION_MAX_VALUE, _("Value must be below"),),
(FIELD_OPTION_MAX_TIMEOUT, _("Last update must not be older than"),))

notification_check = models.ForeignKey(
NotificationCheck, related_name='definitions')
Expand Down Expand Up @@ -1204,7 +1220,11 @@ def current_value(self):
val_ = val.total_seconds()
else:
val_ = val
return {'class': '{}.{}'.format(val.__class__.__module__, val.__class__.__name__),
return {
'class':
'{}.{}'.format(
val.__class__.__module__,
val.__class__.__name__),
'value': val_}
except MetricNotificationCheck.DoesNotExist:
return
Expand Down Expand Up @@ -1246,9 +1266,10 @@ def populate_min_max(self):
except MetricNotificationCheck.DoesNotExist:
try:
mcheck = MetricNotificationCheck.objects\
.filter(notification_check=self.notification_check,
metric=self.metric,
**{'{}__isnull'.format(self.field_option): False})\
.filter(
notification_check=self.notification_check,
metric=self.metric,
**{'{}__isnull'.format(self.field_option): False})\
.get()
if mcheck:
self.metric_check = mcheck
Expand Down Expand Up @@ -1456,27 +1477,31 @@ class BuiltIns(object):
metrics_rate = ('response.time', 'response.size',)
# metrics_count = ('request.count', 'request.method', 'request.

geonode_metrics = ('request', 'request.count', 'request.ip', 'request.ua', 'request.path',
'request.ua.family', 'request.method', 'response.error.count',
'request.country', 'request.region', 'request.city',
'response.time', 'response.status', 'response.size',
'response.error.types',)
geonode_metrics = (
'request', 'request.count', 'request.ip', 'request.ua', 'request.path',
'request.ua.family', 'request.method', 'response.error.count',
'request.country', 'request.region', 'request.city',
'response.time', 'response.status', 'response.size',
'response.error.types',)
host_metrics = ('load.1m', 'load.5m', 'load.15m',
'mem.free', 'mem.usage', 'mem.usage.percent', 'mem.buffers', 'mem.all',
'uptime', 'cpu.usage', 'cpu.usage.rate', 'cpu.usage.percent',
'storage.free', 'storage.total', 'storage.used', # mountpoint is the label
'storage.free', 'storage.total', 'storage.used',
# mountpoint is the label
'network.in', 'network.out', 'network.in.rate', 'network.out.rate',)

rates = ('response.time', 'response.size', 'network.in.rate', 'network.out.rate', 'load.1m', 'load.5m',
'load.15m', 'cpu.usage.rate', 'cpu.usage.percent', 'cpu.usage', 'mem.usage.percent',
'storage.free', 'storage.total', 'storage.used',)
rates = (
'response.time', 'response.size', 'network.in.rate', 'network.out.rate', 'load.1m', 'load.5m',
'load.15m', 'cpu.usage.rate', 'cpu.usage.percent', 'cpu.usage', 'mem.usage.percent',
'storage.free', 'storage.total', 'storage.used',)

values = ('request.ip', 'request.ua', 'request.ua.family', 'request.path',
'request.method', 'request.country', 'request.region',
'request.city', 'response.status', 'response.ereror.types',)

values_numeric = ('storage.total', 'storage.used', 'storage.free', 'mem.free', 'mem.usage',
'mem.buffers', 'mem.all',)
values_numeric = (
'storage.total', 'storage.used', 'storage.free', 'mem.free', 'mem.usage',
'mem.buffers', 'mem.all',)
counters = (
'request.count',
'network.in',
Expand Down

0 comments on commit eb5de19

Please sign in to comment.