diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2019-04-09 16:10:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-09 16:10:14 -0700 |
commit | 8389e746b6745a68fcd58ece8e398bde0a85b57f (patch) | |
tree | 5ee65928c8636317265018c20bd7f11b2d3e3e4b /platform/darwin/src/MGLNetworkConfiguration.m | |
parent | 2ee342448bc6679ef6735ea0deb23f21dcf7c78b (diff) | |
download | qtlocation-mapboxgl-8389e746b6745a68fcd58ece8e398bde0a85b57f.tar.gz |
[ios, macos] Add Mapbox Maps SDK metrics manager. (#13997)
Added MGLSDKMetricsManager to keep track of SDK related events such as tile download times.
* [ios, macos] Add tile download performance event.
* [ios, macos] Modify the performance event format.
* [ios, macos] Add Mapbox Maps SDK events manager.
* [ios, macos] Remove events delegate.
* [ios, macos] Remove deprecated macro.
* [ios, macos] Rename MGLEventsManager to MGLMetricsManager.
* [ios, macos] Make MGLMetricsDelegate follow delegate convention.
* [ios, macos] Remove mbgl-filesource target dependency on MGLMetricsManager.
* [ios, macos] Update metrics manager documentation.
* [ios, macos] Add device metadata.
* [ios, macos] Rename MGLMetricsManager to MGLSDKMetricsManager.
Diffstat (limited to 'platform/darwin/src/MGLNetworkConfiguration.m')
-rw-r--r-- | platform/darwin/src/MGLNetworkConfiguration.m | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m index 8262e7eea1..bac4d12ee5 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.m +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -1,19 +1,33 @@ -#import "MGLNetworkConfiguration.h" +#import "MGLNetworkConfiguration_Private.h" + +static NSString * const MGLStartTime = @"start_time"; +static NSString * const MGLResourceType = @"resource_type"; +NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; @interface MGLNetworkConfiguration () @property (strong) NSURLSessionConfiguration *sessionConfig; +@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDictionary*> *events; +@property (nonatomic, weak) id<MGLNetworkConfigurationMetricsDelegate> metricsDelegate; @end @implementation MGLNetworkConfiguration +- (instancetype)init { + if (self = [super init]) { + self.sessionConfiguration = nil; + _events = [NSMutableDictionary dictionary]; + } + + return self; +} + + (instancetype)sharedManager { static dispatch_once_t onceToken; static MGLNetworkConfiguration *_sharedManager; dispatch_once(&onceToken, ^{ _sharedManager = [[self alloc] init]; - _sharedManager.sessionConfiguration = nil; }); return _sharedManager; @@ -48,4 +62,53 @@ return sessionConfiguration; } +- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType { + if (urlString && ![self.events objectForKey:urlString]) { + [self.events setObject:@{ MGLStartTime: [NSDate date], MGLResourceType: resourceType } forKey:urlString]; + } +} + +- (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]) { + NSDictionary *eventAttributes = [self eventAttributesForURL:urlString withAction:action]; + [self.metricsDelegate networkConfiguration:self didGenerateMetricEvent:eventAttributes]; + [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 |