Ejemplo n.º 1
0
class Run(DB.Model):
    """measurement of resource state"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_identifier = DB.Column(DB.Integer,
                                    DB.ForeignKey('resource.identifier'))
    resource = DB.relationship('Resource',
                               backref=DB.backref('runs', lazy='dynamic'))
    checked_datetime = DB.Column(DB.DateTime, nullable=False)
    success = DB.Column(DB.Boolean, nullable=False)
    response_time = DB.Column(DB.Float, nullable=False)
    message = DB.Column(DB.Text, default='OK')

    def __init__(self,
                 resource,
                 success,
                 response_time,
                 message='OK',
                 checked_datetime=datetime.utcnow()):
        self.resource = resource
        self.success = success
        self.response_time = response_time
        self.checked_datetime = checked_datetime
        self.message = message

    def __repr__(self):
        return '<Run %r>' % (self.identifier)
Ejemplo n.º 2
0
class Resource(DB.Model):
    """HTTP accessible resource"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_type = DB.Column(DB.Text, nullable=False)
    title = DB.Column(DB.Text, nullable=False)
    url = DB.Column(DB.Text, nullable=False)
    latitude = DB.Column(DB.Float)
    longitude = DB.Column(DB.Float)
    owner_identifier = DB.Column(DB.Text, DB.ForeignKey('user.username'))
    owner = DB.relationship('User',
                            backref=DB.backref('username2', lazy='dynamic'))

    def __init__(self, owner, resource_type, title, url):
        self.resource_type = resource_type
        self.title = title
        self.url = url
        self.owner = owner

        # get latitude/longitude from IP
        addr = socket.gethostbyname(urlparse(url).hostname)
        freegeoip_url = 'http://telize.com/geoip/%s' % addr
        try:
            content = json.loads(urlopen(freegeoip_url).read())
            self.latitude = content['latitude']
            self.longitude = content['longitude']
        except Exception, err:  # skip storage
            LOGGER.exception('Could not derive coordinates: %s', err)
Ejemplo n.º 3
0
class CheckVars(DB.Model):
    """Identifies and parameterizes check function, applies to single Probe"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    probe_vars_identifier = DB.Column(DB.Integer,
                                      DB.ForeignKey('probe_vars.identifier'))
    probe_vars = DB.relationship('ProbeVars',
                                 backref=DB.backref(
                                     'check_vars',
                                     cascade="all, delete-orphan"))
    check_class = DB.Column(DB.Text, nullable=False)

    # JSON string object specifying actual parameters for the Check
    # See http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html
    _parameters = DB.Column("parameters", DB.Text, default={})

    def __init__(self, probe_vars, check_class, parameters={}):
        self.probe_vars = probe_vars
        self.check_class = check_class
        self.parameters = parameters

    @property
    def parameters(self):
        return json.loads(self._parameters)

    @parameters.setter
    def parameters(self, parameters):
        self._parameters = json.dumps(parameters)

    def __repr__(self):
        return '<CheckVars %r>' % self.identifier
Ejemplo n.º 4
0
class ProbeVars(DB.Model):
    """
    Identifies and parameterizes single Probe class. Probe
    instance applies to single parent Resource.
    """

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_identifier = DB.Column(DB.Integer,
                                    DB.ForeignKey('resource.identifier'))
    resource = DB.relationship('Resource',
                               backref=DB.backref(
                                   'probe_vars',
                                   lazy='dynamic',
                                   cascade="all, delete-orphan"))
    probe_class = DB.Column(DB.Text, nullable=False)

    # JSON string object specifying actual parameters for the Probe
    # See http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html
    _parameters = DB.Column("parameters", DB.Text, default={})

    def __init__(self, resource_obj, probe_class, parameters={}):
        self.resource = resource_obj
        self.probe_class = probe_class
        self.parameters = parameters

    @property
    def parameters(self):
        return json.loads(self._parameters)

    @parameters.setter
    def parameters(self, parameters):
        self._parameters = json.dumps(parameters)

    @property
    def probe_instance(self):
        return Factory.create_obj(self.probe_class)

    @property
    def name(self):
        return self.probe_instance.NAME

    @property
    def probe_parameters(self):
        return self.probe_instance.REQUEST_PARAMETERS

    def __repr__(self):
        return '<ProbeVars %r>' % self.identifier
Ejemplo n.º 5
0
class Run(DB.Model):
    """measurement of resource state"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_identifier = DB.Column(DB.Integer,
                                    DB.ForeignKey('resource.identifier'))
    resource = DB.relationship('Resource',
                               backref=DB.backref('runs', lazy='dynamic'))
    checked_datetime = DB.Column(DB.DateTime, nullable=False)
    success = DB.Column(DB.Boolean, nullable=False)
    response_time = DB.Column(DB.Float, nullable=False)
    message = DB.Column(DB.Text, default='OK')

    # Make report 'deferred' as not to be included in all queries.
    report = deferred(DB.Column(DB.Text, default={}))

    def __init__(self, resource, result, checked_datetime=datetime.utcnow()):
        self.resource = resource
        self.success = result.success
        self.response_time = result.response_time_str
        self.checked_datetime = checked_datetime
        self.message = result.message
        self.report = result.get_report()

    # JSON string object specifying report for the Run
    # See http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html
    _report = DB.Column("report", DB.Text, default={})

    @property
    def report(self):
        return json.loads(self._report)

    @report.setter
    def report(self, report):
        self._report = json.dumps(report)

    def __repr__(self):
        return '<Run %r>' % (self.identifier)
Ejemplo n.º 6
0
class Resource(DB.Model):
    """HTTP accessible resource"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_type = DB.Column(DB.Text, nullable=False)
    title = DB.Column(DB.Text, nullable=False)
    url = DB.Column(DB.Text, nullable=False)
    latitude = DB.Column(DB.Float)
    longitude = DB.Column(DB.Float)
    owner_identifier = DB.Column(DB.Text, DB.ForeignKey('user.username'))
    owner = DB.relationship('User',
                            backref=DB.backref('username2', lazy='dynamic'))
    tags = DB.relationship('Tag', secondary=resource_tags, backref='resource')

    def __init__(self, owner, resource_type, title, url, tags):
        self.resource_type = resource_type
        self.title = title
        self.url = url
        self.owner = owner
        self.tags = tags

        # get latitude/longitude from hostname
        try:
            self.latitude, self.longitude = util.geocode(url)
        except Exception as err:  # skip storage
            LOGGER.exception('Could not derive coordinates: %s', err)
            self.latitude = 0.0
            self.longitude = 0.0

    def __repr__(self):
        return '<Resource %r %r>' % (self.identifier, self.title)

    @property
    def get_capabilities_url(self):
        if self.resource_type.startswith('OGC:') \
                and self.resource_type != 'OGC:STA':
            url = '%s%s' % (self.url,
                            RESOURCE_TYPES[self.resource_type]['capabilities'])
        else:
            url = self.url
        return url

    @property
    def all_response_times(self):
        result = 0
        if self.runs.count() > 0:
            result = [run.response_time for run in self.runs]
        return result

    @property
    def first_run(self):
        return self.runs.order_by(Run.checked_datetime.asc()).first()

    @property
    def last_run(self):
        return self.runs.order_by(Run.checked_datetime.desc()).first()

    @property
    def average_response_time(self):
        result = 0
        if self.runs.count() > 0:
            query = [run.response_time for run in self.runs]
            result = util.average(query)
        return result

    @property
    def min_response_time(self):
        result = 0
        if self.runs.count() > 0:
            query = [run.response_time for run in self.runs]
            result = min(query)
        return result

    @property
    def max_response_time(self):
        result = 0
        if self.runs.count() > 0:
            query = [run.response_time for run in self.runs]
            result = max(query)
        return result

    @property
    def reliability(self):
        result = 0
        if self.runs.count() > 0:
            total_runs = self.runs.count()
            success_runs = self.runs.filter_by(success=True).count()
            result = util.percentage(success_runs, total_runs)
        return result

    @property
    def tags2csv(self):
        return ','.join([t.name for t in self.tags])

    def snippet(self):
        if not self.last_run:
            return 'No Runs yet'

        report = '''
        - time: %s <br/>
        - success: %s <br/>
        - message: %s <br/>
        - response_time: %s
        ''' % (self.last_run.checked_datetime, self.last_run.success,
               self.last_run.message, self.last_run.response_time)

        return report

    def runs_to_json(self):
        runs = []
        for run in self.runs.order_by(Run.checked_datetime).all():
            runs.append({
                'datetime': run.checked_datetime.isoformat(),
                'value': run.response_time,
                'success': 1 if run.success else 0
            })
        return runs

    def success_to_colors(self):
        colors = []
        for run in self.runs.order_by(Run.checked_datetime).all():
            if run.success == 1:
                colors.append('#5CB85C')  # green
            else:
                colors.append('#D9534F')  # red
        return colors
Ejemplo n.º 7
0
class Resource(DB.Model):
    """HTTP accessible resource"""

    identifier = DB.Column(DB.Integer, primary_key=True, autoincrement=True)
    resource_type = DB.Column(DB.Text, nullable=False)
    title = DB.Column(DB.Text, nullable=False)
    url = DB.Column(DB.Text, nullable=False)
    latitude = DB.Column(DB.Float)
    longitude = DB.Column(DB.Float)
    owner_identifier = DB.Column(DB.Text, DB.ForeignKey('user.username'))
    owner = DB.relationship('User',
                            backref=DB.backref('username2', lazy='dynamic'))

    def __init__(self, owner, resource_type, title, url):
        self.resource_type = resource_type
        self.title = title
        self.url = url
        self.owner = owner

        # get latitude/longitude from hostname
        try:
            self.latitude, self.longitude = util.geocode(url)
        except Exception as err:  # skip storage
            LOGGER.exception('Could not derive coordinates: %s', err)

    def __repr__(self):
        return '<Resource %r %r>' % (self.identifier, self.title)

    @property
    def get_capabilities_url(self):
        if self.resource_type.startswith('OGC:'):
            url = '%s%s' % (self.url,
                            RESOURCE_TYPES[self.resource_type]['capabilities'])
        else:
            url = self.url
        return url

    @property
    def all_response_times(self):
        return [run.response_time for run in self.runs]

    @property
    def first_run(self):
        return self.runs.having(func.min(Run.checked_datetime)).group_by(
            Run.checked_datetime).order_by(Run.checked_datetime.asc()).first()

    @property
    def last_run(self):
        return self.runs.having(func.max(Run.checked_datetime)).group_by(
            Run.checked_datetime).order_by(
                Run.checked_datetime.desc()).first()

    @property
    def average_response_time(self):
        query = [run.response_time for run in self.runs]
        return util.average(query)

    @property
    def min_response_time(self):
        query = [run.response_time for run in self.runs]
        return min(query)

    @property
    def max_response_time(self):
        query = [run.response_time for run in self.runs]
        return max(query)

    @property
    def reliability(self):
        total_runs = self.runs.count()
        success_runs = self.runs.filter_by(success=True).count()
        return util.percentage(success_runs, total_runs)

    def snippet(self):
        return util.get_python_snippet(self)

    def runs_to_json(self):
        runs = []
        for run in self.runs.group_by(Run.checked_datetime).all():
            runs.append({
                'datetime': run.checked_datetime.isoformat(),
                'value': run.response_time,
                'success': 1 if run.success else 0
            })
        return runs

    def success_to_colors(self):
        colors = []
        for run in self.runs.group_by(Run.checked_datetime).all():
            if run.success == 1:
                colors.append('#5CB85C')  # green
            else:
                colors.append('#D9534F')  # red
        return colors