Skip to content

Commit

Permalink
Convert NSAsserts to log client error stats
Browse files Browse the repository at this point in the history
This is a fix for the first issue in
e-mission/e-mission-docs#722
notably, converting all the asserts to stats so that the app won't crash
e-mission/e-mission-docs#722 (comment)
and returning NULL from those conditions

The new stats names are stored in the `cordova-usercache` plugin
  • Loading branch information
shankari committed Apr 27, 2022
1 parent 693ca0e commit 054b3ed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-em-transition-notify"
version="1.2.6">
version="1.2.7">

<name>TransitionNotification</name>
<description>Transition notification. Specially good for trip start and trip end notifications </description>
Expand Down
58 changes: 44 additions & 14 deletions src/ios/BEMTransitionNotifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]) {
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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];
Expand Down

0 comments on commit 054b3ed

Please sign in to comment.