Skip to content

Commit

Permalink
Merge pull request #22 from telefonicaid/release-1.4.0
Browse files Browse the repository at this point in the history
Release 1.4.0
  • Loading branch information
geonexus committed Jun 29, 2015
2 parents dd8833f + ec72d25 commit c03030a
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .gitchangelog.rc
Expand Up @@ -122,7 +122,7 @@ tag_filter_regexp = r'^v[0-9]+\.[0-9]+(\.[0-9]+)?$'
##
## This label will be used as the changelog Title of the last set of changes
## between last valid tag and HEAD if any.
unreleased_version_label = "v1.4.0 (unreleased)"
unreleased_version_label = "v1.5.0 (unreleased)"


## ``output_engine`` is a callable
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -47,8 +47,8 @@ License
:target: https://travis-ci.org/telefonicaid/fiware-facts
.. |Coverage Status| image:: https://coveralls.io/repos/telefonicaid/fiware-facts/badge.png?branch=develop
:target: https://coveralls.io/r/telefonicaid/fiware-facts
.. |Pypi Version| image:: https://pypip.in/v/fiware-facts/badge.png
.. |Pypi Version| image:: https://badge.fury.io/py/fiware-facts.svg
:target: https://pypi.python.org/pypi/fiware-facts/
.. |Pypi License| image:: https://pypip.in/license/fiware-facts/badge.png
.. |Pypi License| image:: https://img.shields.io/pypi/l/fiware-facts.svg
:target: https://pypi.python.org/pypi/fiware-facts/

9 changes: 8 additions & 1 deletion conf/fiware-facts.cfg
Expand Up @@ -29,10 +29,17 @@ redisPort: 6379
redisHost: localhost
redisQueue: policymanager
rabbitMQ: localhost
cloto: localhost
cloto: 127.0.0.1
clotoVersion: v1.0
name: policymanager.facts

[mysql]
host: localhost
charset: utf8
db: cloto
user:
password:

[loggers]
keys: root

Expand Down
55 changes: 0 additions & 55 deletions facts/cloto_client.py

This file was deleted.

66 changes: 66 additions & 0 deletions facts/cloto_db_client.py
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright 2014 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of FI-WARE project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with opensource@tid.es
#
__author__ = 'gjp'
import logging.config
from config import config
import MySQLdb as mysql
from keystoneclient.exceptions import NotFound


class cloto_db_client():
"""This class provides methods to provide connection with Cloto database.
"""
conn = None

def get_window_size(self, tenantId):
"""
This method is in charge of retrieve the window size of a tenantId from cloto database.
:param tenantId: the id of the tenant to request the windowsize
:return: the window size
"""
try:
if self.conn == None:
self.conn = mysql.connect(charset=config.get('mysql', 'charset'), use_unicode=True,
host=config.get('mysql', 'host'),
user=config.get('mysql', 'user'), passwd=config.get('mysql', 'password'),
db=config.get('mysql', 'db'))
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM cloto.cloto_tenantinfo WHERE tenantId="%s"' % tenantId)
data = cursor.fetchall()
if len(data) == 0:
raise NotFound('{"error": "TenantID %s not found in cloto database"}' % tenantId)
else:
tenant_information = data[0]
window_size = tenant_information[1]

except Exception, e:
logging.error("Error %s" % e.message)
raise e
finally:
if self.conn:
self.conn.close()
self.conn = None
return window_size
17 changes: 15 additions & 2 deletions facts/config.py
Expand Up @@ -45,22 +45,35 @@
'clotoVersion': 'v1.0', # Cloto API version
'redisHost': 'localhost', # host of Redis
'redisQueue': 'policymanager', # name of the queue in redis
'rabbitMQ': 'localhost', # IP of the RabbitMQ server
'cloto': 'localhost', # IP of fiware-cloto component
'rabbitMQ': 'localhost', # IP of the RabbitMQ server
'cloto': '127.0.0.1', # IP of fiware-cloto component
'clotoVersion': 'v1.0', # Cloto API version
'name': 'policymanager.facts', # name of the server
'logLevel': 'INFO',
'logFormat': '%(asctime)s %(levelname)s policymanager.facts %(message)s'
}

cfg_mysql_defaults = {
'host': 'localhost', # host of Cloto Mysql database
'charset': 'utf8', # charset of the data base
'user': '', # user of cloto database
'password': '', # password for the user
'db': 'cloto', # name of the cloto dataBase, default: cloto
}

config = SafeConfigParser(cfg_defaults)

# Create the common section in the same way that we have in the configuration file: fiware-facts.cfg
config.add_section('common')
config.add_section('mysql')

# Initialize the content of the config parameters
for key, value in cfg_defaults.items():
config.set('common', key, str(value))

for key, value in cfg_mysql_defaults.items():
config.set('mysql', key, str(value))

windowsize_facts = datetime.timedelta(seconds=10)

fact_attributes = ['serverId', 'CpuValue', 'MemValue', 'DataTime']
Expand Down
11 changes: 6 additions & 5 deletions facts/myredis.py
Expand Up @@ -78,11 +78,13 @@ def range(self, tenantid, serverid):
return self.r.lrange(tenantid + "." + serverid, -100, 100)

def media(self, lista, windowsize):
""" Calculate the media of a list of lidts
""" Calculate the media of a list of data
:param mylist lista The mylist instance with the data to be added.
:return mylist The media of the data
"""
if isinstance(windowsize, list) and len(windowsize) == 1:
windowsize = int(windowsize[0])
if len(lista) >= windowsize:
return self.sum(lista) / len(lista)
else:
Expand All @@ -106,7 +108,7 @@ def sum(self, lista):
return '[]'

def delete(self):
""" Delete a especific queue from the redis system.
""" Delete a specific queue from the redis system.
"""
self.r.delete(nqueue)

Expand All @@ -117,7 +119,7 @@ def insert_window_size(self, tenantid, data):
:return This operation does not return anything except when the data
is no list or the number of element is not equal to 4.
"""
if isinstance(data, list):
if isinstance(data, int) or isinstance(data, long):
self.r.rpush("windowsize" + "." + tenantid, data)
self.r.ltrim("windowsize" + "." + tenantid, -1, -1)

Expand All @@ -134,9 +136,8 @@ def check_time_stamps(self, tenantid, serverid, lista, data):
"""
from dateutil import parser
textmin = lista[-1].split("'")
textmax = data.split("'")
datemin = parser.parse(textmin[-2], fuzzy=True)
datemax = parser.parse(textmax[-2], fuzzy=True)
datemax = parser.parse(data[-1], fuzzy=True)
from config import windowsize_facts

timediff = datemax - datemin
Expand Down
2 changes: 1 addition & 1 deletion facts/queue.py
Expand Up @@ -82,7 +82,7 @@ def publish_message(self, tenantid, message):
logging.error('AMQP channel not properly created...')
raise Exception("AMQP channel not properly created...")

if self.connection:
if self.connection and self.connection.is_open:
# Close the connection
self.connection.close()
else:
Expand Down
23 changes: 16 additions & 7 deletions server.py
Expand Up @@ -23,7 +23,7 @@
# contact with opensource@tid.es
#

__version__ = '1.3.0'
__version__ = '1.4.0'
__version_info__ = tuple([int(num) for num in __version__.split('.')])
__description__ = 'Facts Listener'
__author__ = 'fla'
Expand All @@ -33,6 +33,7 @@
from facts.queue import myqueue
from facts.jsoncheck import jsoncheck
from gevent.pywsgi import WSGIServer
from keystoneclient.exceptions import NotFound
import logging.config
import sys
import datetime
Expand Down Expand Up @@ -104,7 +105,10 @@ def facts(tenantid, serverid):
content_type=content_type)

# It is a valid payload and we start to process it
result = process_request(request, tenantid, serverid)
try:
result = process_request(request, tenantid, serverid)
except NotFound as ex:
return Response(response=ex.message, status=ex.http_status, content_type=content_type)

if result == True:
return Response(status=httplib.OK)
Expand Down Expand Up @@ -172,9 +176,10 @@ def process_request(request, tenantid, serverid):

# Get the windowsize for the tenant from a redis queue
windowsize = mredis.get_windowsize(tenantid)
if windowsize is []:
from facts import cloto_client
windowsize = cloto_client.get_window_size(tenantid)
if windowsize == []:
from facts import cloto_db_client
myClotoDBClient = cloto_db_client.cloto_db_client()
windowsize = myClotoDBClient.get_window_size(tenantid)
mredis.insert_window_size(tenantid, windowsize)

# If the queue has the number of facts defined by the windows size, it returns the
Expand Down Expand Up @@ -214,7 +219,7 @@ def info(port):
logging.info("Running in stand alone mode")
logging.info("Port: {}".format(port))
logging.info("PID: {}\n".format(pid))
logging.info("https://github.hi.inet/telefonicaid/fiware-facts\n\n\n")
logging.info("https://github.com/telefonicaid/fiware-facts\n\n\n")


# process configuration file (if exists) and setup logging
Expand All @@ -236,6 +241,7 @@ def info(port):
def windowsize_updater():
try:
import pika
connection = None
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="localhost"))
channel = connection.channel()
Expand Down Expand Up @@ -274,7 +280,10 @@ def callback(ch, method, properties, body):
if ex.message:
logging.error("Error %s:" % ex.message)
finally:
connection.close()
if connection == None:
logging.error("There is no connection with RabbitMQ. Please, check if it is alive")
else:
connection.close()

import gevent
gevent.spawn(windowsize_updater)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -23,7 +23,7 @@
# contact with opensource@tid.es
#
from distutils.core import setup
__version__ = '1.3.0'
__version__ = '1.4.0'
setup(
name='fiware-facts',
packages=['fiware-facts'], # this must be the same as the name above
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Expand Up @@ -33,7 +33,7 @@ sonar.projectName=Policy Manager - Facts
sonar.projectKey=com.telefonica.iot:policymanager-facts
sonar.sources=./facts
sonar.dynamicAnalysis=reuseReports
sonar.projectVersion=1.3.0
sonar.projectVersion=1.4.0

### LANGUAGE
sonar.language=py
Expand Down

0 comments on commit c03030a

Please sign in to comment.