Skip to content

Commit

Permalink
#90: Added logging to services
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Ebsen committed Sep 3, 2023
1 parent 84c2f7d commit 5c0ea3c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 32 deletions.
25 changes: 17 additions & 8 deletions Server/src/services/poi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}
}
Expand All @@ -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
}

Expand All @@ -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<GeoJSON.Point> | Vehicle,
track?: Track,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
40 changes: 29 additions & 11 deletions Server/src/services/track.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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<GeoJSON.LineString> = turfHelpers.lineString(turfMeta.coordAll(trackData))
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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<GeoJSON.Point> = 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
}

Expand All @@ -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]
Expand All @@ -238,15 +256,15 @@ 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
}

// get last points track kilometer
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
Expand All @@ -260,7 +278,7 @@ export default class TrackService {
public static getTrackAsLineString(track: Track): GeoJSON.Feature<GeoJSON.LineString> | 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))
Expand Down
8 changes: 5 additions & 3 deletions Server/src/services/tracker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5c0ea3c

Please sign in to comment.