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

Story SW-300: starting implementing raw station test parser to parse raw station test output

parent 8f434744
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
...@@ -1779,6 +1779,7 @@ LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/migrations/__init__.py -t ...@@ -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/__init__.py -text
LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/models/stationtest.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/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/tests.py -text
LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py -text LCU/MaintenanceDB/DBInterface/django_rest/monitoringdb/views.py -text
LCU/MaintenanceDB/DBInterface/django_rest/requirements.txt -text LCU/MaintenanceDB/DBInterface/django_rest/requirements.txt -text
......
...@@ -146,17 +146,6 @@ class TileError(PolymorphicModel): ...@@ -146,17 +146,6 @@ class TileError(PolymorphicModel):
class TileNoSignalError(TileError): class TileNoSignalError(TileError):
polarization = models.CharField(max_length=1) 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): class TileModemError(TileError):
......
"""
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
from django.shortcuts import render from django.shortcuts import render
from rest_framework import viewsets from rest_framework import viewsets, status
from .models import StationTest from rest_framework.response import Response
from .serializers import * 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. # Create your views here.
class StationTestViewSet(viewsets.ModelViewSet): class StationTestViewSet(viewsets.ModelViewSet):
...@@ -12,3 +19,37 @@ class StationTestViewSet(viewsets.ModelViewSet): ...@@ -12,3 +19,37 @@ class StationTestViewSet(viewsets.ModelViewSet):
class ComponentErrorViewSet(viewsets.ModelViewSet): class ComponentErrorViewSet(viewsets.ModelViewSet):
queryset = ComponentError.objects.all() queryset = ComponentError.objects.all()
serializer_class = ComponentErrorPolimorphicSerializer 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
...@@ -14,6 +14,7 @@ Including another URLconf ...@@ -14,6 +14,7 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.conf.urls import url, include from django.conf.urls import url, include
from rest_framework import routers from rest_framework import routers
from .monitoringdb import views from .monitoringdb import views
...@@ -25,5 +26,6 @@ router.register(r'componenterrors', views.ComponentErrorViewSet) ...@@ -25,5 +26,6 @@ router.register(r'componenterrors', views.ComponentErrorViewSet)
urlpatterns = [ urlpatterns = [
url(r'^', include(router.urls)), 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)
] ]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment