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/MGLSDKMetricsManager.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/MGLSDKMetricsManager.m')
-rw-r--r-- | platform/darwin/src/MGLSDKMetricsManager.m | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLSDKMetricsManager.m b/platform/darwin/src/MGLSDKMetricsManager.m new file mode 100644 index 0000000000..ed48eaf0c1 --- /dev/null +++ b/platform/darwin/src/MGLSDKMetricsManager.m @@ -0,0 +1,99 @@ +#import "MGLSDKMetricsManager_Private.h" +#import "MGLNetworkConfiguration_Private.h" +#if TARGET_OS_IOS +#import "MGLMapboxEvents.h" +#import <mach-o/arch.h> +#import <sys/utsname.h> +#import <UIKit/UIKit.h> +#endif + +NSString* MGLStringFromMetricType(MGLMetricType metricType) { + NSString *eventName; + + switch (metricType) { + case MGLMetricTypePerformance: + eventName = kMGLDownloadPerformanceEvent; + break; + } + return eventName; +} + +@interface MGLMetricsManager() <MGLNetworkConfigurationMetricsDelegate> + +@property (strong, nonatomic) NSDictionary *metadata; + +@end + +@implementation MGLMetricsManager + ++ (instancetype)sharedManager +{ + static dispatch_once_t once; + static MGLMetricsManager *sharedConfiguration; + dispatch_once(&once, ^{ + sharedConfiguration = [[self alloc] init]; + [MGLNetworkConfiguration sharedManager].metricsDelegate = sharedConfiguration; +#if TARGET_OS_IOS + UIDevice *currentDevice = [UIDevice currentDevice]; + + NSString *osVersion = currentDevice.systemVersion; + + NSString *screenSize = [NSString stringWithFormat:@"%.fx%.f", [UIScreen mainScreen].bounds.size.width, + [UIScreen mainScreen].bounds.size.height]; + + NSLocale *currentLocale = [NSLocale currentLocale]; + NSString *country = [currentLocale objectForKey:NSLocaleCountryCode]; + + NSString *device = deviceName(); + + const NXArchInfo localArchInfo = *NXGetLocalArchInfo(); + NSString *abi = [NSString stringWithUTF8String:localArchInfo.description]; + + NSString *ram = [NSString stringWithFormat:@"%llu", [NSProcessInfo processInfo].physicalMemory]; + + NSString *os = currentDevice.systemName; + + sharedConfiguration.metadata = @{ @"version" : osVersion, + @"screenSize" : screenSize, + @"country" : country, + @"device" : device, + @"abi" : abi, + @"brand" : @"Apple", + @"ram" : ram, + @"os" : os + }; +#endif + }); + return sharedConfiguration; +} + +- (void)handleMetricsEvent:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes { + if ([self.delegate metricsManager:self shouldHandleMetric:metricType]) { + [self.delegate metricsManager:self didCollectMetric:metricType withAttributes:attributes]; + } +} + +#if TARGET_OS_IOS +- (void)pushMetric:(MGLMetricType)metricType withAttributes:(NSDictionary *)attributes { + NSString *eventName = MGLStringFromMetricType(metricType); + NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes]; + [mutableAttributes setObject:self.metadata forKey:@"metadata"]; + + [MGLMapboxEvents pushEvent:eventName withAttributes:mutableAttributes]; +} + +NSString* deviceName() +{ + struct utsname systemInfo; + uname(&systemInfo); + + return [NSString stringWithCString:systemInfo.machine + encoding:NSUTF8StringEncoding]; +} +#endif + +- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didGenerateMetricEvent:(NSDictionary *)metricEvent { + [self handleMetricsEvent:MGLMetricTypePerformance withAttributes:metricEvent]; +} + +@end |