From 054b3ed3b8d611f411a695f453ef6812fce862de Mon Sep 17 00:00:00 2001 From: Shankari Date: Tue, 26 Apr 2022 18:19:12 -0700 Subject: [PATCH] Convert NSAsserts to log client error stats This is a fix for the first issue in https://github.com/e-mission/e-mission-docs/issues/722 notably, converting all the asserts to stats so that the app won't crash https://github.com/e-mission/e-mission-docs/issues/722#issuecomment-1110075053 and returning NULL from those conditions The new stats names are stored in the `cordova-usercache` plugin --- plugin.xml | 2 +- src/ios/BEMTransitionNotifier.m | 58 +++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/plugin.xml b/plugin.xml index d3e387a..2ffa728 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.2.7"> TransitionNotification Transition notification. Specially good for trip start and trip end notifications diff --git a/src/ios/BEMTransitionNotifier.m b/src/ios/BEMTransitionNotifier.m index 5a0db92..f7ecda6 100644 --- a/src/ios/BEMTransitionNotifier.m +++ b/src/ios/BEMTransitionNotifier.m @@ -8,6 +8,7 @@ #import "LocalNotificationManager.h" #import "SimpleLocation.h" #import "Transition.h" +#import "StatsEvent.h" #define TRIP_STARTED @"trip_started" #define TRIP_ENDED @"trip_ended" @@ -69,7 +70,11 @@ - (void)fireGenericTransitionFor:(NSString*) transition withUserInfo:(NSDictiona // || [transition isEqualToString:CFCTransitionTripEnded]) { NSDictionary* autogenData = [self getTripStartEndData]; - return [self postNativeAndNotify:TRIP_ENDED withData:autogenData]; + if (autogenData != NULL) { + return [self postNativeAndNotify:TRIP_ENDED withData:autogenData]; + } else { + [LocalNotificationManager addNotification:[NSString stringWithFormat:@"autogenData == NULL, skipping generic transition"] showUI:FALSE]; + } } if ([transition isEqualToString:CFCTransitionTrackingStopped]) { @@ -79,7 +84,7 @@ - (void)fireGenericTransitionFor:(NSString*) transition withUserInfo:(NSDictiona if ([transition isEqualToString:CFCTransitionStartTracking]) { return [self postNativeAndNotify:TRACKING_STARTED withData:NULL]; } - } +} - (void)postNativeAndNotify:(NSString*)genericTransition withData:(NSDictionary*)autogenData { @@ -152,7 +157,11 @@ - (NSDictionary*) getTripStartEndData { NSMutableDictionary* retData = [NSMutableDictionary new]; NSArray* lastLocArray = [DataUtils getLastPoints:1]; - NSAssert([lastLocArray count] > 0, @"Found no locations while ending trip!"); + if ([lastLocArray count] == 0) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"no_locations_while_ending_trip"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } SimpleLocation* lastLoc = lastLocArray[0]; retData[@"end_ts"] = @(lastLoc.ts); @@ -164,9 +173,12 @@ - (NSDictionary*) getTripStartEndData if (![endTransition.transition isEqualToString:CFCTransitionTripEndDetected]) { [LocalNotificationManager addNotification:[NSString stringWithFormat:@"endTransition = %@, notified before save?", endTransition.transition]]; } - /* - NSAssert([endTransition.transition isEqualToString:CFCTransitionTripEndDetected], @"lastTransition is %@, NOT TRIP_END_DETECTED", endTransition.transition); - */ + + if (![endTransition.transition isEqualToString:CFCTransitionTripEndDetected]) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"last_transition_is_not_trip_end_detected"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } Transition* startTransition; Transition* beforeStartTransition; @@ -176,7 +188,11 @@ - (NSDictionary*) getTripStartEndData beforeStartTransitionPointer:&beforeStartTransition]; SimpleLocation* firstLoc = [self getFirstPoint:startTransition]; - NSAssert(firstLoc != NULL, @"firstLoc = NULL, cannot set!"); + if (firstLoc == NULL) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"first_loc_null"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } retData[@"start_ts"] = @(firstLoc.ts); retData[@"start_lat"] = @(firstLoc.latitude); retData[@"start_lng"] = @(firstLoc.longitude); @@ -193,7 +209,11 @@ - (SimpleLocation*) getFirstPoint:(Transition*)startTransition getFirstSensorData:@"key.usercache.filtered_location" nEntries:1 wrapperClass:[SimpleLocation class]]; - NSAssert([firstLocArray count] > 0, @"Found no locations while ending trip!"); + if ([firstLocArray count] == 0) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"no_locations_while_ending_trip"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } return firstLocArray[0]; } else { // Find points around the start transition @@ -213,10 +233,18 @@ - (SimpleLocation*) getFirstPoint:(Transition*)startTransition getFirstSensorData:@"key.usercache.filtered_location" nEntries:1 wrapperClass:[SimpleLocation class]]; - NSAssert([firstLocArray count] > 0, @"Found no locations while ending trip!"); + if ([firstLocArray count] == 0) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"no_locations_while_ending_trip"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } SimpleLocation* firstLoc = firstLocArray[0]; - NSAssert(firstLoc.ts > startTransition.ts, @"firstLocArray[0].ts (%f) < startTransition.ts (%f)", - firstLoc.ts, startTransition.ts); + if (firstLoc.ts <= startTransition.ts) { + StatsEvent* se = [[StatsEvent alloc] initForReading:@"first_loc_before_start_transition" + withReading:(firstLoc.ts - startTransition.ts)]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } return firstLoc; } else { // There are points around the start transition. @@ -228,9 +256,11 @@ - (SimpleLocation*) getFirstPoint:(Transition*)startTransition beforePoints:beforePoints afterPoints:equalOrAfterPoints]; - NSAssert([beforePoints count] > 0 || [equalOrAfterPoints count] > 0, - @"beforePoints.count %lu afterPoints.count %lu", - [beforePoints count], [equalOrAfterPoints count]); + if ([beforePoints count] <= 0 && [equalOrAfterPoints count] <= 0) { + StatsEvent* se = [[StatsEvent alloc] initForEvent:@"before_and_after_point_count_zero"]; + [[BuiltinUserCache database] putMessage:@"key.usercache.client_error" value:se]; + return NULL; + } // Interval queries return points sorted in ascending order // So we either return the last point before or the first point after long beforeCount = [beforePoints count];