Skip to content

Commit

Permalink
Refs #79. Added features in ServiceQuerySessions & ServiceQueryTests API
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Apr 6, 2022
1 parent abc82c2 commit 142c947
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ def post(self):
session_name = TeraSessionType.get_session_type_by_id(json_session['id_session_type']).session_type_name
session_date = json_session['session_start_datetime']
if not isinstance(session_date, datetime):
import dateutil.parser as parser
session_date = parser.parse(json_session['session_start_datetime'])
session_date = datetime.fromisoformat(json_session['session_start_datetime'])
# session_name += ' [' + session_date.strftime('%d-%m-%Y %H:%M') + ']'
session_name += ' [' + str(session_date.year) + '.' + str(TeraSession.get_count()) + ']'
json_session['session_name'] = session_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from modules.FlaskModule.FlaskModule import service_api_ns as api
from opentera.db.models.TeraTest import TeraTest
from opentera.db.models.TeraService import TeraService
from opentera.db.models.TeraTestType import TeraTestType
from opentera.redis.RedisVars import RedisVars
from modules.DatabaseModule.DBManager import DBManager
from sqlalchemy import exc
from datetime import datetime


# Parser definition(s)
Expand Down Expand Up @@ -137,11 +139,32 @@ def post(self):
if 'id_session' not in test_info:
return gettext('Unknown session'), 400

if 'id_test_type' not in test_info:
if 'id_test_type' not in test_info and 'test_type_uuid' not in test_info:
return gettext('Missing id_test_type field'), 400

if 'test_name' not in test_info and not test_info['test_name']:
return gettext('Invalid test name'), 400
if 'test_type_uuid' in test_info:
test_type = TeraTestType.get_test_type_by_uuid(test_info['test_type_uuid'])
if not test_type:
return gettext('Invalid test type'), 400
test_info.pop('test_type_uuid')
test_info['id_test_type'] = test_type.id_test_type
else:
test_type = TeraTestType.get_test_type_by_id(test_info['id_test_type'])

if 'test_datetime' not in test_info:
test_info['test_datetime'] = datetime.now()

if 'test_name' not in test_info:
test_date = test_info['test_datetime']
if not isinstance(test_date, datetime):
test_date = datetime.fromisoformat(test_info['test_datetime'])
if test_type.test_type_key:
test_name = test_type.test_type_key
else:
test_name = test_type.test_name
test_name += ' [' + str(test_date.year) + '.' + \
str(TeraTest.count_with_filters({'id_session': test_info['id_session']})) + ']'
test_info['test_name'] = test_name

# Check if the service can create/update that test
if test_info['id_test'] != 0 and 'id_session' not in test_info:
Expand Down
15 changes: 15 additions & 0 deletions teraserver/python/opentera/db/models/TeraTest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from opentera.db.Base import db, BaseModel
from enum import Enum
import uuid
import json


class TeraTestStatus(Enum):
Expand Down Expand Up @@ -182,4 +183,18 @@ def insert(cls, test):
# Generate UUID
test.test_uuid = str(uuid.uuid4())

# Check if summary is in json
if isinstance(test.test_summary, dict):
test.test_summary = json.dumps(test.test_summary)

super().insert(test)

@classmethod
def update(cls, update_id: int, values: dict):

# Check if summary is in json
if 'test_summary' in values:
if isinstance(values['test_summary'], dict):
values['test_summary'] = json.dumps(values['test_summary'])

super().update(update_id=update_id, values=values)
4 changes: 4 additions & 0 deletions teraserver/python/opentera/db/models/TeraTestType.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def get_test_type_by_id(test_type_id: int):
def get_test_type_by_key(tt_key: int):
return TeraTestType.query.filter_by(test_type_key=tt_key).first()

@staticmethod
def get_test_type_by_uuid(tt_uuid: int):
return TeraTestType.query.filter_by(test_type_uuid=tt_uuid).first()

@staticmethod
def get_test_types_for_service(id_service: int):
return TeraTestType.query.filter_by(id_service=id_service).all()
Expand Down

0 comments on commit 142c947

Please sign in to comment.