Skip to content
Snippets Groups Projects
Commit 6e76fd9d authored by Mattia Mancini's avatar Mattia Mancini
Browse files

Story SW-300: fixing branch

parent a4dcc246
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
...@@ -15,6 +15,7 @@ logger = logging.getLogger('probe_mdb') ...@@ -15,6 +15,7 @@ logger = logging.getLogger('probe_mdb')
This program is meant to load the station tests and RTSM present in a certain directory to the database This program is meant to load the station tests and RTSM present in a certain directory to the database
""" """
def setup_argument_parser(): def setup_argument_parser():
parser = argparse.ArgumentParser(prog='probe_mdb') parser = argparse.ArgumentParser(prog='probe_mdb')
parser.add_argument('path', help='format and path format of the file to load. es. /where/is/stored/*.dat') parser.add_argument('path', help='format and path format of the file to load. es. /where/is/stored/*.dat')
...@@ -63,8 +64,8 @@ def perform_request(args, content): ...@@ -63,8 +64,8 @@ def perform_request(args, content):
logging.debug('request content %s', json_content) logging.debug('request content %s', json_content)
response = requests.post(full_address, data=content) response = requests.post(full_address, data=content)
logging.info('response acknowledged: status code is %s, reason %s, content %s', response.status_code, logging.info('response acknowledged: status code is %s, reason %s, content %s', response.status_code,
response.reason, response.reason,
response.content) response.content)
if response.status_code == 200: if response.status_code == 200:
return True return True
else: else:
...@@ -81,8 +82,8 @@ def create_query_string(args): ...@@ -81,8 +82,8 @@ def create_query_string(args):
return query return query
if __name__=='__main__': if __name__ == '__main__':
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s",level=logging.DEBUG) logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=logging.DEBUG)
parser = setup_argument_parser() parser = setup_argument_parser()
args = parse_arguments(parser) args = parse_arguments(parser)
...@@ -93,4 +94,4 @@ if __name__=='__main__': ...@@ -93,4 +94,4 @@ if __name__=='__main__':
logging.info('file %s processed', filename) logging.info('file %s processed', filename)
else: else:
logging.error('error on file %s', filename) logging.error('error on file %s', filename)
sys.exit(1) sys.exit(1)
\ No newline at end of file
import logging
import argparse import argparse
import requests
import sys
import json import json
from pandas.io.json import json_normalize import logging
import sys
from datetime import datetime from datetime import datetime
import pprint
import requests
from pandas.io.json import json_normalize
logger = logging.getLogger('probe_mdb') logger = logging.getLogger('probe_mdb')
...@@ -17,18 +17,17 @@ time and on a certain station name ...@@ -17,18 +17,17 @@ time and on a certain station name
def setup_argument_parser(): def setup_argument_parser():
parser = argparse.ArgumentParser(prog='probe_mdb') parser = argparse.ArgumentParser(prog='probe_mdb')
parser.add_argument('--address', help='address of the server. default [localhost]:8000', default='http://localhost:8000') parser.add_argument('--address', help='address of the server. default [localhost]:8000',
parser.add_argument('-s', '--station_test', help='probe station tests', action='store_false') default='http://localhost:8000')
parser.add_argument('-r', '--rtsm', help='probe rtsm tests', action='store_false') parser.add_argument('--from_date', help='tests from date [YYYY-MM-DD]', default=None)
parser.add_argument('-f', help='tests from date [YYYY-MM-DD]', default=None) parser.add_argument('--to_date', help='tests to date [YYYY-MM-DD]', default=None)
parser.add_argument('-t', help='tests to date [YYYY-MM-DD]', default=None)
parser.add_argument('--station', help='select a specific station. es. CS001C', default=None) parser.add_argument('--station', help='select a specific station. es. CS001C', default=None)
parser.add_argument('--station_type', help='select a specific station type. es. I', choices=['C','R', 'I']) parser.add_argument('--station_type', help='select a specific station type. es. I', choices=['C', 'R', 'I'])
parser.add_argument('--to_csv', help='prints results into a csv file', default=None) parser.add_argument('--to_csv', help='prints results into a csv file', default=None)
return parser return parser
def parse_arguments(parser): def parse_arguments(parser):
args = parser.parse_args() args = parser.parse_args()
if args.station_test or args.rtsm: if args.station_test or args.rtsm:
...@@ -37,9 +36,14 @@ def parse_arguments(parser): ...@@ -37,9 +36,14 @@ def parse_arguments(parser):
parser.print_help() parser.print_help()
def perform_query(args): def perform_query(query_string, address):
address = args.address """
full_address = '/'.join([address,create_query_string(args)]) Execute the query string to the specified address
:param query_string: the query string to be done
:param address: the address at which it will be done
:return: a list of dicts or an empty list
"""
full_address = '/'.join([address, query_string])
logging.info('performing query %s', full_address) logging.info('performing query %s', full_address)
response = requests.get(full_address) response = requests.get(full_address)
logging.info('response acknowledged: status code is %s', response.status_code) logging.info('response acknowledged: status code is %s', response.status_code)
...@@ -70,46 +74,134 @@ def result_to_pandas(result): ...@@ -70,46 +74,134 @@ def result_to_pandas(result):
return dataframe return dataframe
def create_query_string(args): def get_query_string_for_time_limit(from_date, to_date):
"""
Create the query string to query the data in a certain time stamp
:param from_date: date from which the results are queried
:param to_date: date to which the results are queries
:return: the query string
"""
query = '' query = ''
if args.station_test: try:
date = datetime.strptime(from_date, '%Y-%m-%d')
query += '&start_time__year__gte=%d' % (date.year)
query += '&start_time__month__gte=%d' % (date.month)
query += '&start_time__day__gte=%d' % (date.day)
except ValueError:
logger.error('format of from date not valid; YYYY-MM-DD es. 2016-15-12')
sys.exit(1)
try:
date = datetime.strptime(to_date, '%Y-%m-%d')
query += '&start_time__year__lte=%d' % (date.year)
query += '&start_time__month__lte=%d' % (date.month)
query += '&start_time__day__lte=%d' % (date.day)
except ValueError:
logger.error('format of to date not valid; YYYY-MM-DD es. 2016-15-12')
sys.exit(1)
return query
def get_query_string_for_type(station_type):
"""
Create the query string to probe a certain station time in the time range [start_time, end_time]
:param station_type: it can be RTSM or STATION_TEST
:param start_time: date from which the results are queried
:param end_time: date to which the results are queried
:return:
"""
query = ''
if station_type == 'RTSM':
query += 'rtsm?' query += 'rtsm?'
elif args.rtsm: elif station_type == 'STATION_TEST':
query += 'stationtests?' query += 'stationtests?'
else:
logger.error('please specify an station_type=[RTSM|STATION_TEST]')
raise ValueError('please specify an station_type=[RTSM|STATION_TEST]')
return query
if args.f:
try:
date = datetime.strptime(args.f, '%Y-%m-%d')
query += '&start_time__year__gte=%d' % (date.year)
query += '&start_time__month__gte=%d' % (date.month)
query += '&start_time__day__gte=%d' % (date.day)
except ValueError: def get_query_string_for_station_name(station_name):
logger.error('format of from date not valid; YYYY-MM-DD es. 2016-15-12') """
sys.exit(1) Return the query string to filter the results to a specific station name
:param station_name:
:return:
"""
query = '&station_name={}'.format(station_name.strip(' \'\"'))
return query
if args.t:
try:
date = datetime.strptime(args.f, '%Y-%m-%d')
query += '&start_time__year__lte=%d' % (date.year)
query += '&start_time__month__lte=%d' % (date.month)
query += '&start_time__day__lte=%d' % (date.day)
except ValueError: class TestType():
logger.error('format of to date not valid; YYYY-MM-DD es. 2016-15-12') """
sys.exit(1) Enum class
"""
STATION_TEST = 'STATION_TEST'
RTSM = 'RTSM'
def query_station_test(address, from_time, to_time, station_name=""):
"""
Queries the station test for a given time span and a station name
"""
query_string = get_query_string_for_type(TestType.STATION_TEST)
query_string += get_query_string_for_time_limit(from_time, to_time)
if station_name:
query_string += get_query_string_for_station_name(station_name)
return perform_query(query_string, address)
def query_rtsm(address, from_time, to_time, station_name=""):
"""
Queries the station test for a given time span and a station name
"""
query_string = get_query_string_for_type(TestType.RTSM)
query_string += get_query_string_for_time_limit(from_time, to_time)
if station_name:
query_string += get_query_string_for_station_name(station_name)
return perform_query(query_string, address)
return query
def print_out_station_test_summary(station_test):
summary_fields = ['station_name', 'station_type', 'start_time', 'end_time', 'performed checks']
if __name__=='__main__': values = [station_test[item] for item in summary_fields]
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s",level=logging.DEBUG)
for field_name, value in zip(summary_fields, values):
pprint.pprint(" = ".join([field_name, value]), indent=5)
print(field_name, value)
def print_out(station_test_results, rtsm_results):
"""
Prints out the formatted station test results and RTSM results
:param station_test_results:
:param rtsm_results:
:return:
"""
for station_test_result in station_test_results:
print_out_station_test_summary(station_test_result)
def probe_mdb():
parser = setup_argument_parser() parser = setup_argument_parser()
args = parse_arguments(parser) args = parse_arguments(parser)
if args is None: if args is None:
sys.exit(1) sys.exit(1)
result = perform_query(args)
print(result) station_test_results = query_station_test(args.address, args.from_date, args.to_date, args.station)
pandas_table = result_to_pandas(result)
if args.to_csv: rtsm_results = query_rtsm(args.address, args.from_date, args.to_date, args.station)
pandas_table.to_csv(args.to_csv)
\ No newline at end of file print_out(station_test_results, rtsm_results)
if __name__ == '__main__':
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=logging.DEBUG)
probe_mdb()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment