Flush telemetry cache when data collection is paused (#7672)

[ios] Flush telemetry cache when data collection is paused

This flushes the cache just before pausing so that data collected
before the pause is not thrown away. It also refactors event
creation to avoid possible nil value inserts into dictionaries
and adds a small optimization to avoid creating immutable
dictionaries to help create mutable ones.

Also:

- Remove cancel API in API client

The cancelAll API is not used. The array of data tasks it maintains
has proven problematic. This removes the API and the problematic
implementation. As safer version of it can be brought back if cancel
task functionality is required in the future.

- Remove unused battery method
This commit is contained in:
Jesse Bounds
2017-01-11 15:55:54 -08:00
committed by GitHub
parent f1fc9bdfeb
commit 4332b3b953
3 changed files with 29 additions and 54 deletions

View File

@@ -9,7 +9,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)postEvents:(NS_ARRAY_OF(MGLMapboxEventAttributes *) *)events completionHandler:(nullable void (^)(NSError * _Nullable error))completionHandler;
- (void)postEvent:(MGLMapboxEventAttributes *)event completionHandler:(nullable void (^)(NSError * _Nullable error))completionHandler;
- (void)cancelAll;
@end

View File

@@ -21,7 +21,6 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST";
@property (nonatomic, copy) NSData *geoTrustCert;
@property (nonatomic, copy) NSData *testServerCert;
@property (nonatomic, copy) NSString *userAgent;
@property (nonatomic) NSMutableArray *dataTasks;
@property (nonatomic) BOOL usesTestServer;
@end
@@ -33,7 +32,6 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST";
if (self) {
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:self delegateQueue:nil];
_dataTasks = [NSMutableArray array];
[self loadCertificates];
[self setupBaseURL];
[self setupUserAgent];
@@ -59,24 +57,15 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST";
error = error ?: statusError;
completionHandler(error);
}
[self.dataTasks removeObject:dataTask];
dataTask = nil;
}];
[dataTask resume];
if (dataTask) {
[self.dataTasks addObject:dataTask];
}
}
- (void)postEvent:(nonnull MGLMapboxEventAttributes *)event completionHandler:(nullable void (^)(NSError * _Nullable error))completionHandler {
[self postEvents:@[event] completionHandler:completionHandler];
}
- (void)cancelAll {
[self.dataTasks makeObjectsPerformSelector:@selector(cancel)];
[self.dataTasks removeAllObjects];
}
#pragma mark Utilities
- (NSURLRequest *)requestForEvents:(NS_ARRAY_OF(MGLMapboxEventAttributes *) *)events {

View File

@@ -278,6 +278,7 @@ const NSTimeInterval MGLFlushInterval = 180;
if (self.paused && enabled) {
[self resumeMetricsCollection];
} else if (!self.paused && !enabled) {
[self flush];
[self pauseMetricsCollection];
}
}
@@ -434,25 +435,31 @@ const NSTimeInterval MGLFlushInterval = 180;
}
- (MGLMapboxEventAttributes *)locationEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
MGLMutableMapboxEventAttributes *attributes = [@{MGLEventKeyEvent: MGLEventTypeLocation,
MGLEventKeySource: MGLEventSource,
MGLEventKeySessionId: self.instanceID,
MGLEventKeyOperatingSystem: self.data.iOSVersion} mutableCopy];
[self addApplicationStateToAttributes:attributes];
MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
attributes[MGLEventKeyEvent] = MGLEventTypeLocation;
attributes[MGLEventKeySource] = MGLEventSource;
attributes[MGLEventKeySessionId] = self.instanceID;
attributes[MGLEventKeyOperatingSystem] = self.data.iOSVersion;
NSString *currentApplicationState = [self applicationState];
if (![currentApplicationState isEqualToString:MGLApplicationStateUnknown]) {
attributes[MGLEventKeyApplicationState] = currentApplicationState;
}
return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}
- (MGLMapboxEventAttributes *)mapLoadEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
MGLMutableMapboxEventAttributes *attributes = [@{MGLEventKeyEvent: MGLEventTypeMapLoad,
MGLEventKeyCreated: [self.rfc3339DateFormatter stringFromDate:[NSDate date]],
MGLEventKeyVendorID: self.data.vendorId,
MGLEventKeyModel: self.data.model,
MGLEventKeyOperatingSystem: self.data.iOSVersion,
MGLEventKeyResolution: @(self.data.scale),
MGLEventKeyAccessibilityFontScale: @([self contentSizeScale]),
MGLEventKeyOrientation: [self deviceOrientation],
MGLEventKeyWifi: @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi])} mutableCopy];
[self addBatteryStateToAttributes:attributes];
MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
attributes[MGLEventKeyEvent] = MGLEventTypeMapLoad;
attributes[MGLEventKeyCreated] = [self.rfc3339DateFormatter stringFromDate:[NSDate date]];
attributes[MGLEventKeyVendorID] = self.data.vendorId;
attributes[MGLEventKeyModel] = self.data.model;
attributes[MGLEventKeyOperatingSystem] = self.data.iOSVersion;
attributes[MGLEventKeyResolution] = @(self.data.scale);
attributes[MGLEventKeyAccessibilityFontScale] = @([self contentSizeScale]);
attributes[MGLEventKeyOrientation] = [self deviceOrientation];
attributes[MGLEventKeyWifi] = @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi]);
return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}
@@ -465,19 +472,22 @@ const NSTimeInterval MGLFlushInterval = 180;
- (MGLMapboxEventAttributes *)mapDragEndEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
MGLMutableMapboxEventAttributes *attributes = [self interactionEvent];
attributes[MGLEventKeyEvent] = MGLEventTypeMapDragEnd;
return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}
- (MGLMutableMapboxEventAttributes *)interactionEvent {
MGLMutableMapboxEventAttributes *attributes = [@{MGLEventKeyCreated: [self.rfc3339DateFormatter stringFromDate:[NSDate date]],
MGLEventKeyOrientation: [self deviceOrientation],
MGLEventKeyWifi: @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi])} mutableCopy];
[self addBatteryStateToAttributes:attributes];
MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
attributes[MGLEventKeyCreated] = [self.rfc3339DateFormatter stringFromDate:[NSDate date]];
attributes[MGLEventKeyOrientation] = [self deviceOrientation];
attributes[MGLEventKeyWifi] = @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi]);
return attributes;
}
- (MGLMapboxEventAttributes *)eventForAttributes:(MGLMutableMapboxEventAttributes *)attributes attributeDictionary:(MGLMapboxEventAttributes *)attributeDictionary {
[attributes addEntriesFromDictionary:attributeDictionary];
return [attributes copy];
}
@@ -594,29 +604,6 @@ const NSTimeInterval MGLFlushInterval = 180;
return result;
}
- (void)addBatteryStateToAttributes:(MGLMutableMapboxEventAttributes *)attributes {
UIDeviceBatteryState batteryState = [[UIDevice currentDevice] batteryState];
switch (batteryState) {
case UIDeviceBatteryStateCharging:
case UIDeviceBatteryStateFull:
attributes[MGLEventKeyPluggedIn] = @(YES);
break;
case UIDeviceBatteryStateUnplugged:
attributes[MGLEventKeyPluggedIn] = @(NO);
break;
default:
// do nothing
break;
}
}
- (void)addApplicationStateToAttributes:(MGLMutableMapboxEventAttributes *)attributes {
NSString *currentApplicationState = [self applicationState];
if (![currentApplicationState isEqualToString:MGLApplicationStateUnknown]) {
attributes[MGLEventKeyApplicationState] = currentApplicationState;
}
}
+ (void)ensureMetricsOptoutExists {
NSNumber *shownInAppNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"];
BOOL metricsEnabledSettingShownInAppFlag = [shownInAppNumber boolValue];