Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
5c492460b9
@ -18,7 +18,7 @@ export class RestService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getCarEvents(vin) {
|
getCarEvents(vin) {
|
||||||
return this.http.get(this.currentLocation + 'car_events/?vin=' + vin, {observe: 'response'});
|
return this.http.get(this.currentLocation + 'car_events?vin=' + vin, {observe: 'response'});
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrafficLights() {
|
getTrafficLights() {
|
||||||
|
|||||||
@ -67,6 +67,14 @@ class EventLogger:
|
|||||||
|
|
||||||
:param bytes msg: pickled msg binary to log
|
:param bytes msg: pickled msg binary to log
|
||||||
"""
|
"""
|
||||||
|
key, to_log = self._unpack_message_to_log(msg)
|
||||||
|
|
||||||
|
if self.log_to_redis:
|
||||||
|
self.redis.lpush(key, "{}".format(to_log))
|
||||||
|
if self.verbose:
|
||||||
|
print(to_log)
|
||||||
|
|
||||||
|
def _unpack_message_to_log(self, msg: bytes):
|
||||||
try:
|
try:
|
||||||
msg = pickle.loads(msg)
|
msg = pickle.loads(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -90,9 +98,9 @@ class EventLogger:
|
|||||||
try:
|
try:
|
||||||
to_log = json.dumps(msg.to_dict())
|
to_log = json.dumps(msg.to_dict())
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
to_log = str(msg)
|
try:
|
||||||
|
to_log = json.dumps(msg)
|
||||||
|
except (json.decoder.JSONDecodeError, TypeError):
|
||||||
|
to_log = str(msg)
|
||||||
|
|
||||||
if self.log_to_redis:
|
return key, to_log
|
||||||
self.redis.lpush(key, "{}".format(to_log))
|
|
||||||
if self.verbose:
|
|
||||||
print(to_log)
|
|
||||||
|
|||||||
68
components/event_store/service/test_event_logger.py
Normal file
68
components/event_store/service/test_event_logger.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import pickle
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import geopy
|
||||||
|
from dse_shared_libs.daf import DAF
|
||||||
|
from dse_shared_libs.target_velocity import TargetVelocity
|
||||||
|
from dse_shared_libs.traffic_light_color import TrafficLightColor
|
||||||
|
from dse_shared_libs.traffic_light_state import TrafficLightState
|
||||||
|
from redis import StrictRedis
|
||||||
|
|
||||||
|
from event_logger import EventLogger
|
||||||
|
|
||||||
|
|
||||||
|
class TestEventLogger(unittest.TestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.el = EventLogger(StrictRedis(), False, False)
|
||||||
|
self.timestamp = datetime.datetime.now()
|
||||||
|
|
||||||
|
def test_unpack_daf(self):
|
||||||
|
daf = DAF(vehicle_identification_number='my_vin',
|
||||||
|
gps_location=geopy.Point(0, 0, 0),
|
||||||
|
velocity=130.0,
|
||||||
|
near_crash_event=False,
|
||||||
|
timestamp=self.timestamp,
|
||||||
|
)
|
||||||
|
|
||||||
|
key, message = self.el._unpack_message_to_log(pickle.dumps(daf))
|
||||||
|
|
||||||
|
self.assertEqual(key, 'DAF:my_vin')
|
||||||
|
self.assertEqual(message, json.dumps(daf.to_dict()))
|
||||||
|
|
||||||
|
def test_unpack_target_velocity(self):
|
||||||
|
tv = TargetVelocity(vin='my_other_vin',
|
||||||
|
target_velocity=120.0,
|
||||||
|
timestamp=self.timestamp)
|
||||||
|
|
||||||
|
key, message = self.el._unpack_message_to_log(pickle.dumps(tv))
|
||||||
|
|
||||||
|
self.assertEqual(key, 'TV:my_other_vin')
|
||||||
|
self.assertEqual(message, json.dumps(tv.to_dict()))
|
||||||
|
|
||||||
|
def test_unpack_traffic_light_state(self):
|
||||||
|
tls = TrafficLightState(tlid='my_traffic_light',
|
||||||
|
color=TrafficLightColor.GREEN,
|
||||||
|
last_switch=self.timestamp)
|
||||||
|
|
||||||
|
key, message = self.el._unpack_message_to_log(pickle.dumps(tls))
|
||||||
|
|
||||||
|
self.assertEqual(key, 'TL:my_traffic_light')
|
||||||
|
self.assertEqual(message, json.dumps(tls.to_dict()))
|
||||||
|
|
||||||
|
def test_unpack_unknown_dict(self):
|
||||||
|
unknown = dict(foo='foo', bar='bar')
|
||||||
|
|
||||||
|
key, message = self.el._unpack_message_to_log(pickle.dumps(unknown))
|
||||||
|
|
||||||
|
self.assertEqual(key, 'UNKNOWN')
|
||||||
|
self.assertEqual(message, json.dumps(unknown))
|
||||||
|
|
||||||
|
def test_unpack_unknown_object(self):
|
||||||
|
obj = datetime.datetime.now()
|
||||||
|
|
||||||
|
key, message = self.el._unpack_message_to_log(pickle.dumps(obj))
|
||||||
|
|
||||||
|
self.assertEqual(key, 'UNKNOWN')
|
||||||
|
self.assertEqual(message, str(obj))
|
||||||
@ -9,23 +9,22 @@ import json
|
|||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
api_bp = flask.Blueprint("api", __name__, url_prefix="/api/v1/resources")
|
api_bp = flask.Blueprint("api", __name__, url_prefix="/api/v1")
|
||||||
|
|
||||||
API = flask_restx.Api(api_bp)
|
API = flask_restx.Api(api_bp)
|
||||||
app.register_blueprint(api_bp)
|
app.register_blueprint(api_bp)
|
||||||
|
|
||||||
|
NAMESPACE = API.namespace("resources")
|
||||||
NAMESPACE = API.namespace("car_events")
|
|
||||||
|
|
||||||
|
|
||||||
ENTITY_IDENT_URL = 'http://entityident:5002/api/v1/resources/'
|
ENTITY_IDENT_URL = 'http://entityident:5002/api/v1/resources/'
|
||||||
EVENT_STORE_URL = 'http://eventstore:5001/api/keys/'
|
EVENT_STORE_URL = 'http://eventstore:5001/api/keys/'
|
||||||
|
|
||||||
|
|
||||||
@NAMESPACE.route('/')
|
@NAMESPACE.route('/car_events', doc={'description': 'Get most current car event for specific car'})
|
||||||
class CarsEvents(flask_restx.Resource):
|
class CarsEvents(flask_restx.Resource):
|
||||||
@NAMESPACE.doc(params={'vin': {'description': 'Vehicle Identifier Number of car',
|
@NAMESPACE.doc(params={'vin': {'description': 'Vehicle Identifier Number of car',
|
||||||
'type': 'String', 'default': '5GZCZ43D13S812715'}})
|
'type': 'String', 'default': '5GZCZ43D13S812715'}})
|
||||||
|
@NAMESPACE.response(200, 'Success')
|
||||||
def get(self):
|
def get(self):
|
||||||
vin = request.args.get('vin')
|
vin = request.args.get('vin')
|
||||||
|
|
||||||
@ -40,45 +39,57 @@ class CarsEvents(flask_restx.Resource):
|
|||||||
return jsonify(cars)
|
return jsonify(cars)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/v1/resources/traffic_light_events', methods=['GET'])
|
@NAMESPACE.route('/traffic_light_events', doc={'description': 'Get most current traffic light event'
|
||||||
def get_traffic_light_events():
|
'for specific traffic light'})
|
||||||
id = request.args.get('id')
|
class TrafficLightEvents(flask_restx.Resource):
|
||||||
|
@NAMESPACE.doc(params={'id': {'description': 'Unique ID of traffic light',
|
||||||
|
'type': 'int', 'default': '1'}})
|
||||||
|
@NAMESPACE.response(200, 'Success')
|
||||||
|
def get(self):
|
||||||
|
id = request.args.get('id')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(EVENT_STORE_URL + 'TL:' + id + '/0/')
|
response = requests.get(EVENT_STORE_URL + 'TL:' + id + '/0/')
|
||||||
traffic_lights = json.loads(response.text)
|
traffic_lights = json.loads(response.text)
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError as e:
|
except requests.exceptions.ConnectionError as e:
|
||||||
print("Is the EVENT_STORE_URL running and reachable?")
|
print("Is the EVENT_STORE_URL running and reachable?")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
return json_util.dumps(traffic_lights)
|
return jsonify(traffic_lights)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/v1/resources/cars', methods=['GET'])
|
@NAMESPACE.route('/cars', doc={'description': 'Get entity details for all cars'})
|
||||||
def get_cars():
|
class Car(flask_restx.Resource):
|
||||||
try:
|
@NAMESPACE.response(200, 'Success')
|
||||||
response = requests.get(ENTITY_IDENT_URL + 'cars')
|
def get(self):
|
||||||
cars = response.json()['cursor']
|
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError as e:
|
try:
|
||||||
print("Is the entity_ident_server running and reachable?")
|
response = requests.get(ENTITY_IDENT_URL + 'cars')
|
||||||
raise e
|
cars = response.json()['cursor']
|
||||||
|
|
||||||
return json_util.dumps(cars)
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
print("Is the entity_ident_server running and reachable?")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
return jsonify(cars)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/v1/resources/traffic_lights', methods=['GET'])
|
@NAMESPACE.route('/traffic_lights', doc={'description': 'Get entity details for all traffic lights'})
|
||||||
def get_traffic_lights():
|
class TrafficLights(flask_restx.Resource):
|
||||||
try:
|
|
||||||
response = requests.get(ENTITY_IDENT_URL + 'traffic_lights')
|
|
||||||
traffic_lights = response.json()['cursor']
|
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError as e:
|
@NAMESPACE.response(200, 'Success')
|
||||||
print("Is the entity_ident_server running and reachable?")
|
def get(self):
|
||||||
raise e
|
|
||||||
|
|
||||||
return json_util.dumps(traffic_lights)
|
try:
|
||||||
|
response = requests.get(ENTITY_IDENT_URL + 'traffic_lights')
|
||||||
|
traffic_lights = response.json()['cursor']
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
print("Is the entity_ident_server running and reachable?")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
return jsonify(traffic_lights)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user