From b452047d6d385f112931e286c9b7809f795886f9 Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Thu, 21 Feb 2019 15:05:37 -0800 Subject: [ios, macos] Modify the performance event format. --- platform/darwin/src/MGLMapboxEventsDelegate.h | 12 +++++ platform/darwin/src/MGLNetworkConfiguration.h | 4 ++ platform/darwin/src/MGLNetworkConfiguration.m | 57 ++++++++++++++-------- .../darwin/src/MGLNetworkConfiguration_Private.h | 9 +--- platform/ios/ios.xcodeproj/project.pbxproj | 6 +++ platform/ios/sdk-files.json | 1 + platform/ios/src/MGLMapView.mm | 14 +----- platform/ios/src/MGLMapboxEvents.h | 2 +- platform/ios/src/Mapbox.h | 1 + platform/macos/macos.xcodeproj/project.pbxproj | 4 ++ platform/macos/sdk-files.json | 1 + platform/macos/src/Mapbox.h | 1 + 12 files changed, 69 insertions(+), 43 deletions(-) create mode 100644 platform/darwin/src/MGLMapboxEventsDelegate.h diff --git a/platform/darwin/src/MGLMapboxEventsDelegate.h b/platform/darwin/src/MGLMapboxEventsDelegate.h new file mode 100644 index 0000000000..3f76522552 --- /dev/null +++ b/platform/darwin/src/MGLMapboxEventsDelegate.h @@ -0,0 +1,12 @@ +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol MGLMapboxEventsDelegate + +- (BOOL)shouldReceiveEvents; +- (void)didReceiveEvent:(NSString *)eventName withAttributes:(NSDictionary *)attributes; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLNetworkConfiguration.h b/platform/darwin/src/MGLNetworkConfiguration.h index 6c56050aae..e14298f203 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.h +++ b/platform/darwin/src/MGLNetworkConfiguration.h @@ -2,6 +2,8 @@ #import "MGLFoundation.h" +@protocol MGLMapboxEventsDelegate; + NS_ASSUME_NONNULL_BEGIN /** @@ -32,6 +34,8 @@ MGL_EXPORT */ @property (atomic, strong, null_resettable) NSURLSessionConfiguration *sessionConfiguration; +@property (nonatomic, weak, nullable) id eventsDelegate; + @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m index 6f5959e70f..00786786d0 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.m +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -1,4 +1,5 @@ #import "MGLNetworkConfiguration_Private.h" +#import "MGLMapboxEventsDelegate.h" NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; @@ -9,7 +10,6 @@ static NSString * const MGLResourceType = @"resource_type"; @property (strong) NSURLSessionConfiguration *sessionConfig; @property (nonatomic, strong) NSMutableDictionary *events; -@property (nonatomic, weak, nullable) id delegate; @end @@ -63,34 +63,49 @@ static NSString * const MGLResourceType = @"resource_type"; } - (void)stopDownloadEvent:(NSString *)urlString { + [self sendEventForURL:urlString withAction:nil]; +} + +- (void)cancelDownloadEvent:(NSString *)urlString { + [self sendEventForURL:urlString withAction:@"cancel"]; +} + +- (void)sendEventForURL:(NSString *)urlString withAction:(NSString *)action +{ if (urlString && [self.events objectForKey:urlString]) { - if ([self.delegate respondsToSelector:@selector(networkConfiguration:didReceiveNetworkEvent:)]) + if ([self.eventsDelegate shouldReceiveEvents]) { - NSDictionary *parameters = [self.events objectForKey:urlString]; - NSDate *startDate = [parameters objectForKey:MGLStartTime]; - NSDate *endDate = [NSDate date]; - NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate]; - NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init]; - iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; - - NSDictionary *eventAttributes = @{ - @"event" : kMGLDownloadPerformanceEvent, - @"created" : [iso8601Formatter stringFromDate:[NSDate date]], - @"sessionId" : [NSUUID UUID].UUIDString, - @"counters" : @[ @{ @"name" : @"elapsed_time" }, @{ @"value" : @(elapsedTime) } ], - @"attributes" : @[ @{ @"name" : @"resource" }, @{ @"value" : urlString }, ] - }; - - [self.delegate networkConfiguration:self didReceiveNetworkEvent:eventAttributes]; + NSDictionary *eventAttributes = [self eventAttributesForURL:urlString withAction:action]; + [self.eventsDelegate didReceiveEvent:kMGLDownloadPerformanceEvent withAttributes:eventAttributes]; } [self.events removeObjectForKey:urlString]; } } -- (void)cancelDownloadEvent:(NSString *)urlString { - if (urlString && [self.events objectForKey:urlString]) { - [self.events removeObjectForKey:urlString]; +- (NSDictionary *)eventAttributesForURL:(NSString *)urlString withAction:(NSString *)action +{ + NSDictionary *parameters = [self.events objectForKey:urlString]; + NSDate *startDate = [parameters objectForKey:MGLStartTime]; + NSDate *endDate = [NSDate date]; + NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate]; + NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init]; + iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; + NSString *createdDate = [iso8601Formatter stringFromDate:[NSDate date]]; + + NSMutableArray *attributes = [NSMutableArray array]; + [attributes addObject:@{ @"name" : @"resource" , @"value" : urlString }]; + [attributes addObject:@{ @"name" : MGLResourceType , @"value" : [parameters objectForKey:MGLResourceType] }]; + if (action) { + [attributes addObject:@{ @"name" : @"action" , @"value" : action }]; } + + return @{ + @"event" : kMGLDownloadPerformanceEvent, + @"created" : createdDate, + @"sessionId" : [NSUUID UUID].UUIDString, + @"counters" : @[ @{ @"name" : @"elapsed_time" , @"value" : @(elapsedTime) } ], + @"attributes" : attributes + }; } @end diff --git a/platform/darwin/src/MGLNetworkConfiguration_Private.h b/platform/darwin/src/MGLNetworkConfiguration_Private.h index 8d55aea220..2481ee1fea 100644 --- a/platform/darwin/src/MGLNetworkConfiguration_Private.h +++ b/platform/darwin/src/MGLNetworkConfiguration_Private.h @@ -1,20 +1,13 @@ #import "MGLNetworkConfiguration.h" +@protocol MGLMapboxEventsDelegate; NS_ASSUME_NONNULL_BEGIN extern NSString * const kMGLDownloadPerformanceEvent; -@protocol MGLNetworkEventDelegate - -@optional -- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didReceiveNetworkEvent:(NSDictionary *)event; - -@end - @interface MGLNetworkConfiguration (Private) @property (nonatomic, strong) NSMutableDictionary *events; -@property (nonatomic, weak, nullable) id delegate; - (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType; - (void)stopDownloadEvent:(NSString *)urlString; diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 42af2f662f..8c5b01d9ef 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -37,6 +37,8 @@ 1F26B6C320E1A351007BCC21 /* simple_route.json in Resources */ = {isa = PBXBuildFile; fileRef = 1F26B6C220E1A351007BCC21 /* simple_route.json */; }; 1F2B94C0221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */; }; 1F2B94C1221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */; }; + 1F2B95072220A73B00210640 /* MGLMapboxEventsDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B95052220A73B00210640 /* MGLMapboxEventsDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 1F2B95082220A73B00210640 /* MGLMapboxEventsDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B95052220A73B00210640 /* MGLMapboxEventsDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F6A82A221360F9D00BA5B41 /* MGLLoggingConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F6A82A321360F9D00BA5B41 /* MGLLoggingConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F6A82A421360F9D00BA5B41 /* MGLLoggingConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F6A82A121360F9C00BA5B41 /* MGLLoggingConfiguration.m */; }; @@ -866,6 +868,7 @@ 1F26B6C020E189C9007BCC21 /* MBXCustomLocationViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBXCustomLocationViewController.m; sourceTree = ""; }; 1F26B6C220E1A351007BCC21 /* simple_route.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = simple_route.json; sourceTree = ""; }; 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLNetworkConfiguration_Private.h; sourceTree = ""; }; + 1F2B95052220A73B00210640 /* MGLMapboxEventsDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLMapboxEventsDelegate.h; sourceTree = ""; }; 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLLoggingConfiguration.h; sourceTree = ""; }; 1F6A82A121360F9C00BA5B41 /* MGLLoggingConfiguration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLLoggingConfiguration.m; sourceTree = ""; }; 1F6A82A62138871900BA5B41 /* MGLLoggingConfiguration_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLLoggingConfiguration_Private.h; sourceTree = ""; }; @@ -2071,6 +2074,7 @@ 558DE79F1E5615E400C7916D /* MGLFoundation.mm */, DA8847E21CBAFA5100AB86E3 /* MGLMapCamera.h */, DA8848031CBAFA6200AB86E3 /* MGLMapCamera.mm */, + 1F2B95052220A73B00210640 /* MGLMapboxEventsDelegate.h */, 927FBCFD1F4DB05500F8BF1F /* MGLMapSnapshotter.h */, 927FBCFE1F4DB05500F8BF1F /* MGLMapSnapshotter.mm */, DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */, @@ -2504,6 +2508,7 @@ DA88485C1CBAFB9800AB86E3 /* MGLFaux3DUserLocationAnnotationView.h in Headers */, 35305D4A1D22AA6A0007D005 /* NSData+MGLAdditions.h in Headers */, 359F57461D2FDDA6005217F1 /* MGLUserLocationAnnotationView_Private.h in Headers */, + 1F2B95072220A73B00210640 /* MGLMapboxEventsDelegate.h in Headers */, 404C26E21D89B877000AA13D /* MGLTileSource.h in Headers */, DA8847FD1CBAFA5100AB86E3 /* MGLTilePyramidOfflineRegion.h in Headers */, DA88482F1CBAFA6200AB86E3 /* NSProcessInfo+MGLAdditions.h in Headers */, @@ -2693,6 +2698,7 @@ 353933F61D3FB785003F57D7 /* MGLBackgroundStyleLayer.h in Headers */, DABFB85D1CBE99E500D62B32 /* MGLAccountManager.h in Headers */, 74CB5EB2219B252C00102936 /* MGLStyleLayerManager.h in Headers */, + 1F2B95082220A73B00210640 /* MGLMapboxEventsDelegate.h in Headers */, 9221BAB020699F8A0054BDF4 /* MGLTilePyramidOfflineRegion_Private.h in Headers */, 96E516F5200059B100A02306 /* MGLNetworkConfiguration.h in Headers */, 96E516F42000597D00A02306 /* NSData+MGLAdditions.h in Headers */, diff --git a/platform/ios/sdk-files.json b/platform/ios/sdk-files.json index 175f828605..fba4964782 100644 --- a/platform/ios/sdk-files.json +++ b/platform/ios/sdk-files.json @@ -193,6 +193,7 @@ "MGLMapViewDelegate.h": "platform/ios/src/MGLMapViewDelegate.h", "MGLDistanceFormatter.h": "platform/darwin/src/MGLDistanceFormatter.h", "MGLOpenGLStyleLayer.h": "platform/darwin/src/MGLOpenGLStyleLayer.h", + "MGLMapboxEventsDelegate.h": "platform/darwin/src/MGLMapboxEventsDelegate.h", "MGLTileSource.h": "platform/darwin/src/MGLTileSource.h", "MGLTilePyramidOfflineRegion.h": "platform/darwin/src/MGLTilePyramidOfflineRegion.h", "MGLVectorTileSource.h": "platform/darwin/src/MGLVectorTileSource.h", diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 2c8492f12a..752a1a780b 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -74,7 +74,6 @@ #import "MGLMapAccessibilityElement.h" #import "MGLLocationManager_Private.h" #import "MGLLoggingConfiguration_Private.h" -#import "MGLNetworkConfiguration_Private.h" #include #include @@ -194,8 +193,7 @@ public: MGLSMCalloutViewDelegate, MGLCalloutViewDelegate, MGLMultiPointDelegate, - MGLAnnotationImageDelegate, - MGLNetworkEventDelegate> + MGLAnnotationImageDelegate> @property (nonatomic) EAGLContext *context; @property (nonatomic) GLKView *glView; @@ -498,9 +496,6 @@ public: // setup default location manager self.locationManager = nil; - - // setup the download events - [MGLNetworkConfiguration sharedManager].delegate = self; // Set up annotation management and selection state. _annotationImagesByIdentifier = [NSMutableDictionary dictionary]; @@ -6929,11 +6924,4 @@ private: self.showsUserHeadingIndicator = showsHeading; } -#pragma mark - MGLNetworkEventDelegate - -- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didReceiveNetworkEvent:(NSDictionary *)event -{ - [MGLMapboxEvents pushEvent:kMGLDownloadPerformanceEvent withAttributes:event]; -} - @end diff --git a/platform/ios/src/MGLMapboxEvents.h b/platform/ios/src/MGLMapboxEvents.h index cbac578798..cb3132656f 100644 --- a/platform/ios/src/MGLMapboxEvents.h +++ b/platform/ios/src/MGLMapboxEvents.h @@ -3,7 +3,7 @@ NS_ASSUME_NONNULL_BEGIN -@interface MGLMapboxEvents : NSObject +@interface MGLMapboxEvents : NSObject + (nullable instancetype)sharedInstance; diff --git a/platform/ios/src/Mapbox.h b/platform/ios/src/Mapbox.h index 43300b6aa5..a056f5a6af 100644 --- a/platform/ios/src/Mapbox.h +++ b/platform/ios/src/Mapbox.h @@ -72,3 +72,4 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[]; #import "MGLLocationManager.h" #import "MGLLoggingConfiguration.h" #import "MGLNetworkConfiguration.h" +#import "MGLMapboxEventsDelegate.h" diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index e79aa776e6..09e8f8f470 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 170A82C4201FB6EC00943087 /* MGLHeatmapColorTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 170A82C2201FAFF800943087 /* MGLHeatmapColorTests.mm */; }; 1753ED401E53CE6100A9FD90 /* MGLConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 1753ED3F1E53CE5200A9FD90 /* MGLConversion.h */; }; 1F2B94C3221E22E600210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94C2221E22E500210640 /* MGLNetworkConfiguration_Private.h */; }; + 1F2B950C2220B93D00210640 /* MGLMapboxEventsDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B950B2220B93C00210640 /* MGLMapboxEventsDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F7454A31ECFB00300021D39 /* MGLLight_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F7454A01ECFB00300021D39 /* MGLLight_Private.h */; }; 1F7454A41ECFB00300021D39 /* MGLLight.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F7454A11ECFB00300021D39 /* MGLLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F7454A51ECFB00300021D39 /* MGLLight.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F7454A21ECFB00300021D39 /* MGLLight.mm */; }; @@ -332,6 +333,7 @@ 170A82C2201FAFF800943087 /* MGLHeatmapColorTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLHeatmapColorTests.mm; sourceTree = ""; }; 1753ED3F1E53CE5200A9FD90 /* MGLConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLConversion.h; sourceTree = ""; }; 1F2B94C2221E22E500210640 /* MGLNetworkConfiguration_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLNetworkConfiguration_Private.h; sourceTree = ""; }; + 1F2B950B2220B93C00210640 /* MGLMapboxEventsDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLMapboxEventsDelegate.h; sourceTree = ""; }; 1F7454A01ECFB00300021D39 /* MGLLight_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLLight_Private.h; sourceTree = ""; }; 1F7454A11ECFB00300021D39 /* MGLLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLLight.h; sourceTree = ""; }; 1F7454A21ECFB00300021D39 /* MGLLight.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLLight.mm; sourceTree = ""; }; @@ -1207,6 +1209,7 @@ 558DE7A51E56161C00C7916D /* MGLFoundation.mm */, DAE6C34D1CC31E0400DB3429 /* MGLMapCamera.h */, DAE6C36E1CC31E2A00DB3429 /* MGLMapCamera.mm */, + 1F2B950B2220B93C00210640 /* MGLMapboxEventsDelegate.h */, 92092EEE1F5EB10E00AF5130 /* MGLMapSnapshotter.h */, 92092EEF1F5EB10E00AF5130 /* MGLMapSnapshotter.mm */, DD0902B01DB1AC6400C5BDCE /* MGLNetworkConfiguration.h */, @@ -1341,6 +1344,7 @@ DAE6C39A1CC31E2A00DB3429 /* NSProcessInfo+MGLAdditions.h in Headers */, 92F2C3EB1F0E3A1900268EC0 /* MGLRendererFrontend.h in Headers */, DA8F258B1D51CA540010E6B5 /* MGLLineStyleLayer.h in Headers */, + 1F2B950C2220B93D00210640 /* MGLMapboxEventsDelegate.h in Headers */, 35C6DF841E214C0400ACA483 /* MGLDistanceFormatter.h in Headers */, DA8F25B21D51CB270010E6B5 /* NSValue+MGLStyleAttributeAdditions.h in Headers */, 55335DF9212EC542000CE5F8 /* NSImage+MGLAdditions.h in Headers */, diff --git a/platform/macos/sdk-files.json b/platform/macos/sdk-files.json index afc5efbabe..9b9c0da841 100644 --- a/platform/macos/sdk-files.json +++ b/platform/macos/sdk-files.json @@ -113,6 +113,7 @@ "MGLOverlay.h": "platform/darwin/src/MGLOverlay.h", "MGLPolyline.h": "platform/darwin/src/MGLPolyline.h", "MGLLineStyleLayer.h": "platform/darwin/src/MGLLineStyleLayer.h", + "MGLMapboxEventsDelegate.h": "platform/darwin/src/MGLMapboxEventsDelegate.h", "MGLDistanceFormatter.h": "platform/darwin/src/MGLDistanceFormatter.h", "MGLHeatmapStyleLayer.h": "platform/darwin/src/MGLHeatmapStyleLayer.h", "MGLOfflineRegion.h": "platform/darwin/src/MGLOfflineRegion.h", diff --git a/platform/macos/src/Mapbox.h b/platform/macos/src/Mapbox.h index 4d54c753df..4eac706a28 100644 --- a/platform/macos/src/Mapbox.h +++ b/platform/macos/src/Mapbox.h @@ -67,3 +67,4 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[]; #import "NSPredicate+MGLAdditions.h" #import "MGLLoggingConfiguration.h" #import "MGLNetworkConfiguration.h" +#import "MGLMapboxEventsDelegate.h" -- cgit v1.2.1