From f0033a22255369db1878bfe86b33e4534ee9cc57 Mon Sep 17 00:00:00 2001 From: Mattia Mancini <mancini@astron.nl> Date: Tue, 1 May 2018 16:35:40 +0000 Subject: [PATCH] Story SW-300: starting implementing raw station test parser to parse raw station test output --- .gitattributes | 1 + .../monitoringdb/models/stationtest.py | 11 --- .../monitoringdb/station_test_raw_parser.py | 86 +++++++++++++++++++ .../django_rest/monitoringdb/views.py | 47 +++++++++- .../DBInterface/django_rest/urls.py | 4 +- 5 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/station_test_raw_parser.py diff --git a/.gitattributes b/.gitattributes index e2c80a76347..5f8d427bd1f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1779,6 +1779,7 @@ LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/migrations/__init__.py -t LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/__init__.py -text LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.py -text LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/serializers.py -text +LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/station_test_raw_parser.py -text LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/tests.py -text LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py -text LCU/MaintenanceDB/DBInterface/django_rest/requirements.txt -text diff --git a/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.py b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.py index df34dbfae40..c4c23fc553e 100644 --- a/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.py +++ b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.py @@ -146,17 +146,6 @@ class TileError(PolymorphicModel): class TileNoSignalError(TileError): polarization = models.CharField(max_length=1) - tile_id = models.SmallIntegerField(default=-1) - parent_component_error = models.ForeignKey(HBANoSignal, - related_name='tile_errors', - on_delete=models.CASCADE) - - -class TileNoSignalError(TileError): - polarization = models.CharField(max_length=1) - parent_component_error = models.ForeignKey(HBANoSignal, - related_name='tile_errors', - on_delete=models.CASCADE) class TileModemError(TileError): diff --git a/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/station_test_raw_parser.py b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/station_test_raw_parser.py new file mode 100644 index 00000000000..49d1f4264e0 --- /dev/null +++ b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/station_test_raw_parser.py @@ -0,0 +1,86 @@ +""" +This modules contains all the function needed to parse a raw station test output into the models used to describe it +""" +from datetime import datetime + +def parse_key_value_pairs(content): + """ + Parse a key value pair returning a dict. + es 'key=value' -> {'key': 'value'} + :param content: the string to parse + :return: dict + """ + assert '=' in content + pairs = content.split('=') + return {pairs[0]:pairs[1]} + + +def is_component_type(component_type, values): + """ + Receive a list of values that compose a line in the station test output and returns true if + the line refers to the component_type type false otherwise + :param values: a list of values that compose a line in the station test output + :param component_type: component type + :return: True or False + """ + return values[1] == component_type + + +def filter_by_component_type(component_type, contents): + """ + Receive a list of rows that compose a line in the station test output and returns a list of rows + that are referred to the component type type + :param values: a list of values that compose a line in the station test output + :param component_type: component type + :return: a list of values that refers to the component_type + """ + return filter(lambda content: is_component_type(component_type, content), contents) + + +def parse_datetime(date, date_time): + """ + If the datetime is only the time of the current date it used the data + to generate the date time + :param date: + :param date_time: + :return: + """ + if 'T' in date_time: + return datetime.strptime(date_time, '%Y-%m-%dT%H:%M:%S') + else: + return datetime.strptime("T".join([date, date_time]), '%Y%m%dT%H:%M:%S') + + + +def parse_from_raw_station_test(content): + """ + Expects a string content with the station test output + and output a list of Django Models + :param content: string content with the station test output + :return: a list of Django models + """ + model_list = [] + + station_name = '' + start_time = '' + end_time = '' + + preparsed_content = [] + for line in content.split('\n'): + values = [parse_key_value_pairs(value) if '=' in value else value for value in line.split(',')] + + preparsed_content.append(values) + + for row in filter_by_component_type('NFO', preparsed_content): + print('parsing ', row) + if row[3] == "STATION": + station_name = row[4]['NAME'] + elif row[3] == "RUNTIME": + start_time = parse_datetime(row[0], row[4]['START']) + end_time = parse_datetime(row[0], row[5]['STOP']) + print('end time', end_time) + print(station_name, start_time, end_time) + + + + return model_list \ No newline at end of file diff --git a/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py index 1c4e17f6c6b..e485e2c051f 100644 --- a/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py +++ b/LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py @@ -1,7 +1,14 @@ from django.shortcuts import render -from rest_framework import viewsets -from .models import StationTest -from .serializers import * +from rest_framework import viewsets, status +from rest_framework.response import Response +from rest_framework.decorators import api_view +from .models import StationTest, ComponentError +from .serializers import StationTestSerializer, ComponentErrorPolimorphicSerializer +from .station_test_raw_parser import parse_from_raw_station_test + +import logging + +logger = logging.getLogger(__name__) # Create your views here. class StationTestViewSet(viewsets.ModelViewSet): @@ -12,3 +19,37 @@ class StationTestViewSet(viewsets.ModelViewSet): class ComponentErrorViewSet(viewsets.ModelViewSet): queryset = ComponentError.objects.all() serializer_class = ComponentErrorPolimorphicSerializer + + +@api_view(['POST']) +def insert_raw_station_test(request): + """ + This function is meant to parse a request of the form + { + "content": "[STATION TEST RAW TEXT]" + } + parse the content field and create all the station_test entity related into the database + :param request: HTTP request + :return: + """ + if request.method == 'POST': + print(request.data['content']) + + if 'content' in request.data: + try: + for entry in parse_from_raw_station_test(request.data['content']): + entry.save() + except Exception as e: # TODO this response does not work + logger.exception("exception occurred while parsing raw station info %s: %s", request.data['content'], e) + return Response(exception=True, + data="the post message is not correct." + + " It has to be of the form \{'content':[RAWSTRING]\}: %s" % (e,), + status=status.HTTP_400_BAD_REQUEST) + else: + return Response(exception=True, + data="the post message is not correct." + + " It has to be of the form \{'content':[RAWSTRING]\}", status=status.HTTP_400_BAD_REQUEST) + + for i in request.data: + print(i) + return Response(status=status.HTTP_200_OK) \ No newline at end of file diff --git a/LCU/MaintenanceDB/DBInterface/django_rest/urls.py b/LCU/MaintenanceDB/DBInterface/django_rest/urls.py index 39c38b967fc..5acd6050981 100644 --- a/LCU/MaintenanceDB/DBInterface/django_rest/urls.py +++ b/LCU/MaintenanceDB/DBInterface/django_rest/urls.py @@ -14,6 +14,7 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin + from django.conf.urls import url, include from rest_framework import routers from .monitoringdb import views @@ -25,5 +26,6 @@ router.register(r'componenterrors', views.ComponentErrorViewSet) urlpatterns = [ url(r'^', include(router.urls)), - url(r'^api-auth', include('rest_framework.urls', namespace='rest_framework')) + url(r'^api-auth', include('rest_framework.urls', namespace='rest_framework')), + url(r'^stationtests/insert_raw', views.insert_raw_station_test) ] -- GitLab