Changeset: f9baa0c79960 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=f9baa0c79960 Modified Files: clients/iotapi/src/Settings/filesystem.py clients/iotapi/src/Settings/iotlogger.py clients/iotapi/src/main.py clients/iotclient/src/Flask/restresources.py clients/iotclient/src/Settings/filesystem.py clients/iotclient/src/Settings/iotlogger.py clients/iotclient/src/Settings/mapiconnection.py clients/iotclient/src/Streams/jsonschemas.py clients/iotclient/src/Streams/streams.py clients/iotclient/src/Streams/streamscontext.py clients/iotclient/src/Streams/streamscreator.py clients/iotclient/src/main.py Branch: iot Log Message:
Added auto-flushed streams, removed unnecessary variables, web api is now running as a subprocess diffs (truncated from 780 to 300 lines): diff --git a/clients/iotapi/src/Settings/filesystem.py b/clients/iotapi/src/Settings/filesystem.py --- a/clients/iotapi/src/Settings/filesystem.py +++ b/clients/iotapi/src/Settings/filesystem.py @@ -1,34 +1,26 @@ import sys - import os from iotlogger import add_log -BASKETS_BASE_DIRECTORY = "baskets" - -if sys.platform in ("linux", "linux2", "darwin"): - Filesystem_Location = '/etc/iotcollector' -elif sys.platform == "win32": - Filesystem_Location = os.path.join(os.path.dirname(__file__), os.pardir) - Baskets_Location = None -def set_filesystem_location(new_location): - global Filesystem_Location - Filesystem_Location = new_location - - -def init_file_system(): +def init_file_system(new_location=None): global Baskets_Location + if new_location is None: + if sys.platform in ("linux", "linux2", "darwin"): + new_location = '/etc/iotcollector' + elif sys.platform == "win32": + new_location = os.path.join(os.path.dirname(__file__), os.pardir) + else: + new_location = new_location + try: - Baskets_Location = os.path.join(Filesystem_Location, BASKETS_BASE_DIRECTORY) + Baskets_Location = os.path.join(new_location, "baskets") if not os.path.exists(Baskets_Location): os.makedirs(Baskets_Location) - - if not os.path.exists(Filesystem_Location): - os.makedirs(Filesystem_Location) except (Exception, OSError) as ex: print >> sys.stdout, ex add_log(50, ex) diff --git a/clients/iotapi/src/Settings/iotlogger.py b/clients/iotapi/src/Settings/iotlogger.py --- a/clients/iotapi/src/Settings/iotlogger.py +++ b/clients/iotapi/src/Settings/iotlogger.py @@ -3,23 +3,22 @@ import sys import os -if sys.platform in ("linux", "linux2", "darwin"): - logging_location = '/var/log/iot/iotapi.log' -elif sys.platform == "win32": - logging_location = os.path.join(os.path.dirname(__file__), os.pardir, 'iotapi.log') +Logger = logging.getLogger("IOTAPILog") -logger = logging.getLogger("IOTAPILog") +def init_logging(new_location): + global Logger -def set_logging_location(new_location): - global logging_location - logging_location = new_location + if new_location is None: + if sys.platform in ("linux", "linux2", "darwin"): + logging_location = '/var/log/iot/iotapi.log' + elif sys.platform == "win32": + logging_location = os.path.join(os.path.dirname(__file__), os.pardir, 'iotapi.log') + else: + logging_location = new_location - -def init_logging(): - global logger try: - logger = logging.getLogger("IOTLog") + logger = logging.getLogger("IOTAPILog") logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') @@ -35,4 +34,4 @@ def init_logging(): def add_log(lvl, message, *args, **kwargs): - logger.log(lvl, message, *args, **kwargs) + Logger.log(lvl, message, *args, **kwargs) diff --git a/clients/iotapi/src/main.py b/clients/iotapi/src/main.py --- a/clients/iotapi/src/main.py +++ b/clients/iotapi/src/main.py @@ -5,8 +5,8 @@ import sys from multiprocessing import Process from threading import Thread -from Settings.filesystem import init_file_system, set_filesystem_location -from Settings.iotlogger import init_logging, add_log, set_logging_location +from Settings.filesystem import init_file_system +from Settings.iotlogger import init_logging, add_log from Settings.mapiconnection import init_monetdb_connection, close_monetdb_connection from Streams.streampolling import init_stream_polling_thread from WebSockets.websockets import init_websockets, terminate_websockets @@ -18,14 +18,13 @@ def signal_handler(signal, frame): subprocess.terminate() -def start_process(sockets_host, sockets_port, connection_hostname, connection_port, connection_user, - connection_password, connection_database): +def start_process(filesystem_location, logging_location, sockets_host, sockets_port, connection_hostname, con_port, + con_user, con_password, con_database): # WARNING The initiation order must be this!!! - init_logging() # init logging context - init_file_system() # init filesystem + init_logging(logging_location) # init logging context + init_file_system(filesystem_location) # init filesystem # init mapi connection - init_monetdb_connection(connection_hostname, connection_port, connection_user, connection_password, - connection_database) + init_monetdb_connection(connection_hostname, con_port, con_user, con_password, con_database) init_stream_polling_thread(60) # start polling thread1 = Thread(target=init_websockets, args=(sockets_host, sockets_port)) @@ -48,36 +47,38 @@ def main(argv): print >> sys.stdout, "Error while parsing the arguments!" sys.exit(1) + filesystem_location = None + logging_location = None sockets_host = '0.0.0.0' sockets_port = 8002 - connection_hostname = '127.0.0.1' - connection_port = 50000 - connection_user = 'monetdb' - connection_database = 'iotdb' + con_hostname = '127.0.0.1' + con_port = 50000 + con_user = 'monetdb' + con_database = 'iotdb' for opt, arg in opts: if opt in ('-f', '--filesystem'): - set_filesystem_location(arg) + filesystem_location = arg elif opt in ('-l', '--log'): - set_logging_location(arg) + logging_location = arg elif opt in ('-sh', '--shost'): sockets_host = arg elif opt in ('-sp', '--sport'): sockets_port = int(arg) elif opt in ('-h', '--host'): - connection_hostname = arg + con_hostname = arg elif opt in ('-p', '--port'): - connection_port = int(arg) + con_port = int(arg) elif opt in ('-u', '--user'): - connection_user = arg + con_user = arg elif opt in ('-d', '--database'): - connection_database = arg + con_database = arg - connection_password = getpass.getpass(prompt='Insert password for user ' + connection_user + ':') - subprocess = Process(target=start_process, args=(sockets_host, sockets_port, connection_hostname, connection_port, - connection_user, connection_password, connection_database)) + con_password = getpass.getpass(prompt='Insert password for user ' + con_user + ':') + subprocess = Process(target=start_process, args=(filesystem_location, logging_location, sockets_host, sockets_port, + con_hostname, con_port, con_user, con_password, con_database)) subprocess.start() signal.signal(signal.SIGINT, signal_handler) subprocess.join() diff --git a/clients/iotclient/src/Flask/restresources.py b/clients/iotclient/src/Flask/restresources.py --- a/clients/iotclient/src/Flask/restresources.py +++ b/clients/iotclient/src/Flask/restresources.py @@ -11,21 +11,19 @@ from Streams.jsonschemas import CREATE_S from Streams.streamscontext import IOTStreamsContext from Settings.iotlogger import add_log -Streams_Context = None -Create_Streams_Validator = None -Delete_Streams_Validator = None +Streams_Context = IOTStreamsContext() +Create_Streams_Validator = Draft4Validator(CREATE_STREAMS_SCHEMA, format_checker=FormatChecker()) +Delete_Streams_Validator = Draft4Validator(DELETE_STREAMS_SCHEMA, format_checker=FormatChecker()) Local_Timezone = get_localzone() # for the correction of dates we must add the system's timezone def init_rest_resources(): - global Streams_Context, Create_Streams_Validator, Delete_Streams_Validator, Local_Timezone + global Streams_Context - Create_Streams_Validator = Draft4Validator(CREATE_STREAMS_SCHEMA, format_checker=FormatChecker()) - Delete_Streams_Validator = Draft4Validator(DELETE_STREAMS_SCHEMA, format_checker=FormatChecker()) try: Streams_Context = IOTStreamsContext() except BaseException as ex: - print >> sys.stdout, ex + print ex add_log(50, ex) sys.exit(1) @@ -33,6 +31,9 @@ def init_rest_resources(): class StreamInput(Resource): """RESTful API for stream's input""" + def __init__(self): + super(StreamInput, self).__init__() + def get(self, schema_name, stream_name): # check a single stream data try: # check if stream exists, if not return 404 stream = Streams_Context.get_existing_stream(schema_name, stream_name) @@ -61,6 +62,9 @@ class StreamInput(Resource): class StreamsInfo(Resource): """Collect all streams information""" + def __init__(self): + super(StreamsInfo, self).__init__() + def get(self): # get all streams data return Streams_Context.get_streams_data(), 200 diff --git a/clients/iotclient/src/Settings/filesystem.py b/clients/iotclient/src/Settings/filesystem.py --- a/clients/iotclient/src/Settings/filesystem.py +++ b/clients/iotclient/src/Settings/filesystem.py @@ -1,56 +1,39 @@ +import os import sys -import os +from iotlogger import add_log from Utilities.filecreator import create_file_if_not_exists -from iotlogger import add_log +Baskets_Location = None +Configfile_Location = None -BASKETS_BASE_DIRECTORY = "baskets" -CONFIG_FILE_DEFAULT_NAME = "config.json" -if sys.platform in ("linux", "linux2", "darwin"): - Filesystem_Location = '/etc/iotcollector' -elif sys.platform == "win32": - Filesystem_Location = os.path.join(os.path.dirname(__file__), os.pardir) +def init_file_system(new_location=None): + global Baskets_Location, Configfile_Location -Baskets_Location = None -Config_File_Location = None -Host_Identifier = None - - -def set_filesystem_location(new_location): - global Filesystem_Location - Filesystem_Location = new_location - - -def init_file_system(host_identifier=None, new_configfile_location=None): - global Baskets_Location, Config_File_Location, Host_Identifier + if new_location is None: + if sys.platform in ("linux", "linux2", "darwin"): + filesystem_location = '/etc/iotcollector' + elif sys.platform == "win32": + filesystem_location = os.path.join(os.path.dirname(__file__), os.pardir) + else: + filesystem_location = new_location try: - Baskets_Location = os.path.join(Filesystem_Location, BASKETS_BASE_DIRECTORY) + Baskets_Location = os.path.join(filesystem_location, "baskets") if not os.path.exists(Baskets_Location): os.makedirs(Baskets_Location) - - if new_configfile_location is not None: - Config_File_Location = create_file_if_not_exists(new_configfile_location, init_text='[]') - else: - Config_File_Location = create_file_if_not_exists( - os.path.join(Filesystem_Location, CONFIG_FILE_DEFAULT_NAME), init_text='[]') - - Host_Identifier = host_identifier + Configfile_Location = create_file_if_not_exists(os.path.join(filesystem_location, "config.json"), + init_text='[]') except (Exception, OSError) as ex: - print >> sys.stdout, ex + print ex add_log(50, ex) sys.exit(1) -def get_baskets_base_location(): +def get_baskets_location(): return Baskets_Location _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list