From 5c0ea3c15bb69ac19117327b5aee2d75f01ba332 Mon Sep 17 00:00:00 2001 From: Kevin Ebsen Date: Sun, 3 Sep 2023 15:36:38 +0200 Subject: [PATCH] #90: Added logging to services --- Server/src/services/poi.service.ts | 25 ++++++++++------ Server/src/services/track.service.ts | 40 +++++++++++++++++++------- Server/src/services/tracker.service.ts | 8 ++++-- Server/src/services/vehicle.service.ts | 25 +++++++++------- 4 files changed, 66 insertions(+), 32 deletions(-) diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index 3ee5e423..9001a272 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -34,6 +34,7 @@ export default class POIService { if (track == null) { const tempTrack = await TrackService.getClosestTrack(position) if (tempTrack == null) { + logger.error(`No closest track was found for position ${JSON.stringify(position)}.`) return null } track = tempTrack @@ -42,6 +43,7 @@ export default class POIService { // add kilometer value const enrichedPoint = await this.enrichPOIPosition(position, track) if (enrichedPoint == null) { + logger.error(`The position ${JSON.stringify(position)} could not be enriched.`) return null } // typecast to any, because JSON is expected @@ -62,7 +64,7 @@ export default class POIService { if (track == null) { const tempTrack = await TrackService.getClosestTrack(point) if (tempTrack == null) { - // TODO: log this + logger.error(`No closest track was found for position ${JSON.stringify(point)}.`) return null } track = tempTrack @@ -71,7 +73,7 @@ export default class POIService { // calculate and set track kilometer const trackKm = await TrackService.getPointTrackKm(point, track) if (trackKm == null) { - // TODO: log this + logger.error(`Could not get track distance for position ${JSON.stringify(point)} on track with id ${track.uid}.`) return null } GeoJSONUtils.setTrackKm(point, trackKm) @@ -96,7 +98,7 @@ export default class POIService { // get closest track if none is given const poiPos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position) if (poiPos == null) { - // TODO: log this + logger.error(`Position ${JSON.stringify(poi.position)} could not be parsed.`) return null } @@ -109,19 +111,21 @@ export default class POIService { // Therefore, obtain and typecast the position const poiPos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position) if (poiPos == null) { + logger.error(`Position ${JSON.stringify(poi.position)} could not be parsed.`) return null } // get track of POI to enrich it const track = await database.tracks.getById(poi.trackId) if (track == null) { + logger.error(`Track with id ${poi.trackId} was not found.`) return null } // then enrich it with the given track const enrichedPos = await this.enrichPOIPosition(poiPos, track) if (enrichedPos == null) { - logger.error(`Could not enrich position of POI with ID ${poi.uid}`) + logger.error(`Could not enrich position of POI with ID ${poi.uid}.`) return null } // try to update the poi in the database, now that we have enriched it @@ -132,7 +136,7 @@ export default class POIService { // and re-calculate poiTrackKm (we do not care that much at this point if the update was successful) poiTrackKm = GeoJSONUtils.getTrackKm(enrichedPos) if (poiTrackKm == null) { - logger.error(`Could not get distance as percentage of POI with ID ${poi.uid}.`) + logger.error(`Could not get track kilometer of POI position ${JSON.stringify(enrichedPos)}.`) return null } } @@ -149,17 +153,19 @@ export default class POIService { // get track length const track = await database.tracks.getById(poi.trackId) if (track == null) { + logger.error(`Track with id ${poi.trackId} was not found.`) return null } const trackLength = TrackService.getTrackLength(track) if (trackLength == null) { + logger.error(`Length of track with id ${track.uid} could not be calculated.`) return null } const poiDistKm = await this.getPOITrackDistanceKm(poi) if (poiDistKm == null) { - logger.error(`Could not get distance as percentage of POI with ID ${poi.uid}.`) + logger.error(`Could not get track kilometer of POI with ID ${poi.uid}.`) return null } @@ -178,6 +184,7 @@ export default class POIService { * @returns `POI[]`, either #`count` of nearest POI's or all POI's within `maxDistance` of track-kilometers, but at most #`count`. * That is the array could be empty. */ + // NOT ADDING LOGGING HERE, BECAUSE IT WILL BE REMOVED ANYWAY (see issue #114) public static async getNearbyPOIs( point: GeoJSON.Feature | Vehicle, track?: Track, @@ -323,11 +330,12 @@ export default class POIService { // enrich and update const POITrack = await database.tracks.getById(poi.trackId) if (POITrack == null) { - // TODO: this really should not happen, how to handle? delete POI? + logger.error(`Track with id ${poi.trackId} of POI with id ${poi.uid} was not found.`) return null } const enrichedPoint = await this.enrichPOIPosition(position, POITrack) if (enrichedPoint == null) { + logger.error(`Could not enrich new POI position ${JSON.stringify(position)} for track with id ${POITrack.uid}.`) return null } return database.pois.update(poi.uid, undefined, undefined, undefined, undefined, enrichedPoint) @@ -373,11 +381,12 @@ export default class POIService { // update track kilometer value first const poiPos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position) if (poiPos == null) { - // TODO: log this + logger.error(`Could not parse position ${JSON.stringify(poi.position)} of POI with id ${poi.uid}.`) return null } const updatedPOIPos = await this.enrichPOIPosition(poiPos, track) if (updatedPOIPos == null) { + logger.error(`Could not enrich position ${JSON.stringify(poiPos)} for track with id ${track.uid}.`) return null } diff --git a/Server/src/services/track.service.ts b/Server/src/services/track.service.ts index 08eb0354..4722a1a7 100644 --- a/Server/src/services/track.service.ts +++ b/Server/src/services/track.service.ts @@ -7,6 +7,7 @@ import nearestPointOnLine from "@turf/nearest-point-on-line" import * as turfMeta from "@turf/meta" import * as turfHelpers from "@turf/helpers" import bearing from "@turf/bearing" +import { logger } from "../utils/logger" /** * Service for track management. This also includes handling the GeoJSON track data. @@ -84,6 +85,7 @@ export default class TrackService { // get the track kilometer value from projected point const projectedPoint = await this.getProjectedPointOnTrack(position, track) if (projectedPoint == null) { + logger.error(`Could not project position ${JSON.stringify(position)}.`) return null } return GeoJSONUtils.getTrackKm(projectedPoint) @@ -105,6 +107,7 @@ export default class TrackService { // if an error occured while trying to find the closest track, there is nothing we can do if (tempTrack == null) { + logger.error(`Could not find closest track for position ${JSON.stringify(position)}.`) return null } track = tempTrack @@ -113,7 +116,7 @@ export default class TrackService { // converting feature collection of points from track to linestring to project position onto it const trackData = GeoJSONUtils.parseGeoJSONFeatureCollectionPoints(track.data) if (trackData == null) { - // TODO: log this + logger.error(`Could not parse track data of track with id ${track.uid}.`) return null } const lineStringData: GeoJSON.Feature = turfHelpers.lineString(turfMeta.coordAll(trackData)) @@ -124,7 +127,11 @@ export default class TrackService { // for easier access we set the property of track kilometer to the already calculated value if (projectedPoint.properties["location"] == null) { - // TODO: log this + logger.error( + `Turf did something wrong while computing nearest point on line for track with id ${ + track.uid + } and position ${JSON.stringify(position)}.` + ) // this is a slight overreaction as we can still return the projected point, but the track kilometer property will not be accessible return null } @@ -144,17 +151,20 @@ export default class TrackService { // validate track kilometer value const trackLength = this.getTrackLength(track) if (trackLength == null) { - // TODO: log this + logger.error(`Length of track with id ${track.uid} could not be calculated.`) return null } if (trackKm < 0 || trackKm > trackLength) { + logger.error( + `Unexpected value for track kilometer: ${trackKm}. This needs to be more than 0 and less than ${trackLength}.` + ) return null } // get track data const trackData = GeoJSONUtils.parseGeoJSONFeatureCollectionPoints(track.data) if (trackData == null) { - // TODO: log this + logger.error(`Could not parse track data of track with id ${track.uid}.`) return null } @@ -168,7 +178,7 @@ export default class TrackService { const trackPoint = trackData.features[i] const trackPointKm = GeoJSONUtils.getTrackKm(trackPoint) if (trackPointKm == null) { - // TODO: log this, this should not happen + logger.error(`Could not access track kilometer value of track point ${i} of track with id ${track.uid}.`) return null } @@ -178,7 +188,9 @@ export default class TrackService { } } - // TODO: log this, this would be really weird as we validated the track kilometer value passed + logger.error( + `Track kilometer value ${trackKm} could not be found while iterating track points of track with id ${track.uid}.` + ) return null } @@ -191,6 +203,7 @@ export default class TrackService { const tracks = await database.tracks.getAll() // there are no tracks at all if (tracks.length == 0) { + logger.warn(`No track was found.`) return null } @@ -200,7 +213,7 @@ export default class TrackService { for (let i = 0; i < tracks.length; i++) { const trackData = GeoJSONUtils.parseGeoJSONFeatureCollectionPoints(tracks[i].data) if (trackData == null) { - // TODO: log this + logger.error(`Could not parse track data of track with id ${tracks[i].uid}.`) return null } @@ -209,7 +222,11 @@ export default class TrackService { // this gives us the nearest point on the linestring including the distance to that point const closestPoint: GeoJSON.Feature = nearestPointOnLine(lineStringData, position) if (closestPoint.properties == null || closestPoint.properties["dist"] == null) { - // TODO: this should not happen, so maybe log this + logger.warn( + `Turf did not calculate nearest point on line correctly for position ${JSON.stringify( + position + )} for track with id ${tracks[i].uid}.` + ) continue } @@ -222,6 +239,7 @@ export default class TrackService { // check if closest track was found if (minTrack < 0) { + logger.warn(`Somehow no closest track was found even after iterating all existing tracks.`) return null } else { return tracks[minTrack] @@ -238,7 +256,7 @@ export default class TrackService { // load track data const trackData = GeoJSONUtils.parseGeoJSONFeatureCollectionPoints(track.data) if (trackData == null) { - // TODO: log this + logger.error(`Could not parse track data of track with id ${track.uid}.`) return null } @@ -246,7 +264,7 @@ export default class TrackService { const trackPointsLength = trackData.features.length const trackLength = GeoJSONUtils.getTrackKm(trackData.features[trackPointsLength - 1]) if (trackLength == null) { - // TODO: log this, track data invalid, probably check if track exists and try to get it by id + logger.error(`Could not access track kilometer value of last track point of track with id ${track.uid}.`) return null } return trackLength @@ -260,7 +278,7 @@ export default class TrackService { public static getTrackAsLineString(track: Track): GeoJSON.Feature | null { const trackData = GeoJSONUtils.parseGeoJSONFeatureCollectionPoints(track.data) if (trackData == null) { - // TODO: log this + logger.error(`Could not parse track data of track with id ${track.uid}.`) return null } return turfHelpers.lineString(turfMeta.coordAll(trackData)) diff --git a/Server/src/services/tracker.service.ts b/Server/src/services/tracker.service.ts index 9b11b129..fd9e0e02 100644 --- a/Server/src/services/tracker.service.ts +++ b/Server/src/services/tracker.service.ts @@ -133,15 +133,17 @@ export default class TrackerService { } if (tracker == null || tracker.vehicleId == null) { - // TODO: log this, especially if tracker is still null - // (no vehicle id is not that critical as a tracker could exist without an assigned vehicle, + logger.warn(`Could not log anything for tracker with id ${trackerId}, because it has no assigned vehicle.`) + // (vehicle id is not that critical as a tracker could exist without an assigned vehicle, // but logging will not happen then and would not make sense) return null } const vehicle = await VehicleService.getVehicleById(tracker.vehicleId) if (vehicle == null) { - // TODO: log this, a vehicle should exist if a tracker is assigned to it + logger.error( + `Vehicle with id ${tracker.vehicleId} was not found even though a tracker with id ${trackerId} is assigned to it.` + ) return null } // actual wrapper diff --git a/Server/src/services/vehicle.service.ts b/Server/src/services/vehicle.service.ts index d9a60166..62001362 100644 --- a/Server/src/services/vehicle.service.ts +++ b/Server/src/services/vehicle.service.ts @@ -71,6 +71,7 @@ export default class VehicleService { * @returns `Vehicle[]` either #`count` of nearest vehicles or all vehicles within `distance` of track-kilometers, but at most #`count`. * That is the array could be empty. `null` if an error occurs */ + // NOT ADDING LOGGING HERE, BECAUSE IT WILL BE REMOVED ANYWAY (see issue #114) public static async getNearbyVehicles( point: GeoJSON.Feature | Vehicle, track?: Track, @@ -269,7 +270,7 @@ export default class VehicleService { const lastTrackerLog = trackerLogs[0] const lastTrackerPosition = GeoJSONUtils.parseGeoJSONFeaturePoint(lastTrackerLog.position) if (lastTrackerPosition == null) { - logger.warn(`Position ${trackerLogs[0].position} is not in GeoJSON-format.`) + logger.warn(`Position ${JSON.stringify(trackerLogs[0].position)} is not in GeoJSON-format.`) continue } @@ -326,7 +327,7 @@ export default class VehicleService { // parse position from log const lastPosition = GeoJSONUtils.parseGeoJSONFeaturePoint(log.position) if (lastPosition == null) { - logger.warn(`Position ${log.position} is not in GeoJSON-format.`) + logger.warn(`Position ${JSON.stringify(log.position)} is not in GeoJSON-format.`) continue } @@ -375,7 +376,9 @@ export default class VehicleService { const lastPosition = GeoJSONUtils.parseGeoJSONFeaturePoint(appPositions[i][1].position) if (lastPosition == null) { // at this point this should not happen anymore - logger.error(`Position ${appPositions[i][1].position} is not in GeoJSON-format, but should be.`) + logger.error( + `Position ${JSON.stringify(appPositions[i][1].position)} is not in GeoJSON-format, but should be.` + ) return null } const projectedPoint = nearestPointOnLine(lineStringData, lastPosition) @@ -430,8 +433,6 @@ export default class VehicleService { * @returns the last known position of `vehicle` mapped on its track, null if an error occurs */ private static async getLastKnownVehiclePosition(vehicle: Vehicle): Promise | null> { - // TODO: this could be optimized by computing an average position from all last entries by all trackers - // get last log and track of vehicle const lastLog = await database.logs.getAll(vehicle.uid, undefined, 1) if (lastLog.length != 1) { @@ -465,14 +466,14 @@ export default class VehicleService { // get track point of vehicle const vehicleTrackPoint = await this.getVehiclePosition(vehicle) if (vehicleTrackPoint == null) { - // TODO: log this + logger.error(`Could not compute position of vehicle with id ${vehicle.uid}.`) return null } // get track kilometer for vehicle position const vehicleTrackKm = GeoJSONUtils.getTrackKm(vehicleTrackPoint) if (vehicleTrackKm == null) { - // TODO: log this + logger.error(`Could not read track kilometer value from position ${JSON.stringify(vehicleTrackPoint)}.`) return null } @@ -488,7 +489,7 @@ export default class VehicleService { // get track const track = await database.tracks.getById(vehicle.trackId) if (track == null) { - // TODO: logging + logger.error(`Track with id ${vehicle.trackId} was not found.`) return null } @@ -496,6 +497,9 @@ export default class VehicleService { const trackLength = TrackService.getTrackLength(track) const vehicleDistance = await this.getVehicleTrackDistanceKm(vehicle) if (trackLength == null || vehicleDistance == null) { + logger.error( + `Distance of track with id ${track.uid} or distance of vehicle with id ${vehicle.uid} on that track could not be computed.` + ) return null } @@ -556,7 +560,7 @@ export default class VehicleService { // get track const track = await database.tracks.getById(vehicle.trackId) if (track == null) { - // TODO: log + logger.error(`Track with id ${vehicle.trackId} was not found.`) return 0 } @@ -566,7 +570,7 @@ export default class VehicleService { // finally compute track heading const trackBearing = await TrackService.getTrackHeading(track, trackKm) if (trackBearing == null) { - // TODO: log this + logger.error(`Could not compute heading of track with id ${track.uid} at track kilometer ${trackKm}.`) return 0 } // TODO: maybe give this a buffer of uncertainty @@ -585,6 +589,7 @@ export default class VehicleService { */ public static async getVehicleSpeed(vehicle: Vehicle): Promise { // get all trackers for given vehicle + // TODO: remove necessity of trackers const trackers = await database.trackers.getByVehicleId(vehicle.uid) if (trackers.length == 0) { logger.error(`No tracker found for vehicle ${vehicle.uid}.`)