2017-05-25 09:39:54 +00:00
|
|
|
#import "RNFirebasePerformance.h"
|
|
|
|
|
2017-05-25 14:03:20 +00:00
|
|
|
#if __has_include(<FirebasePerformance/FIRPerformance.h>)
|
|
|
|
#import <FirebasePerformance/FIRPerformance.h>
|
2018-07-18 15:05:59 +00:00
|
|
|
#import <FirebasePerformance/FIRHTTPMetric.h>
|
2017-05-25 09:39:54 +00:00
|
|
|
@implementation RNFirebasePerformance
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
|
|
|
_traces = [[NSMutableDictionary alloc] init];
|
2018-07-18 15:05:59 +00:00
|
|
|
_httpMetrics = [[NSMutableDictionary alloc] init];
|
2017-05-25 09:39:54 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:05:59 +00:00
|
|
|
- (FIRHTTPMethod) mapStringToMethod:(NSString *) value {
|
|
|
|
if ([value compare:@"get" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodGET;
|
|
|
|
if ([value compare:@"put" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodPUT;
|
|
|
|
if ([value compare:@"post" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodPUT;
|
|
|
|
if ([value compare:@"delete" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodDELETE;
|
|
|
|
if ([value compare:@"head" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodHEAD;
|
|
|
|
if ([value compare:@"patch" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodPATCH;
|
|
|
|
if ([value compare:@"options" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodOPTIONS;
|
|
|
|
if ([value compare:@"trace" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodTRACE;
|
|
|
|
if ([value compare:@"connect" options:NSCaseInsensitiveSearch] == NSOrderedSame) return FIRHTTPMethodCONNECT;
|
|
|
|
return FIRHTTPMethodGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 09:39:54 +00:00
|
|
|
- (FIRTrace *)getOrCreateTrace:(NSString *)identifier {
|
|
|
|
if (_traces[identifier]) {
|
|
|
|
return _traces[identifier];
|
|
|
|
}
|
2017-11-09 15:39:25 +00:00
|
|
|
FIRTrace *trace = [[FIRPerformance sharedInstance] traceWithName:identifier];
|
2017-05-25 09:39:54 +00:00
|
|
|
_traces[identifier] = trace;
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:05:59 +00:00
|
|
|
- (FIRHTTPMetric *)getOrCreateHttpMetric:(NSString *)url httpMethod:(NSString *) httpMethod {
|
|
|
|
NSString *identifier = [NSString stringWithFormat:@"%@%@", url, httpMethod];
|
|
|
|
if (_httpMetrics[identifier]) {
|
|
|
|
return _httpMetrics[identifier];
|
|
|
|
}
|
|
|
|
NSURL * toURL = [NSURL URLWithString:url];
|
|
|
|
FIRHTTPMethod method = [self mapStringToMethod:httpMethod];
|
|
|
|
FIRHTTPMetric *httpMetric = [[FIRHTTPMetric alloc] initWithURL:toURL HTTPMethod:method];
|
|
|
|
_httpMetrics[identifier] = httpMetric;
|
|
|
|
return httpMetric;
|
|
|
|
}
|
|
|
|
|
2017-05-25 09:39:54 +00:00
|
|
|
RCT_EXPORT_METHOD(setPerformanceCollectionEnabled:
|
|
|
|
(BOOL *) enabled) {
|
|
|
|
[FIRPerformance sharedInstance].dataCollectionEnabled = (BOOL) enabled;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:05:59 +00:00
|
|
|
/**
|
|
|
|
* Trace
|
|
|
|
*/
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(startTrace:
|
|
|
|
(NSString *) identifier
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-05-25 09:39:54 +00:00
|
|
|
[[self getOrCreateTrace:identifier] start];
|
2018-07-18 15:05:59 +00:00
|
|
|
resolve([NSNull null]);
|
2017-05-25 09:39:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:05:59 +00:00
|
|
|
RCT_EXPORT_METHOD(stopTrace:
|
|
|
|
(NSString *) identifier
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-05-25 09:39:54 +00:00
|
|
|
[[self getOrCreateTrace:identifier] stop];
|
2018-07-18 15:05:59 +00:00
|
|
|
[_traces removeObjectForKey:identifier];
|
|
|
|
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getTraceAttribute:
|
|
|
|
(NSString *) identifier
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
NSString *value = [[self getOrCreateTrace:identifier] valueForAttribute:attribute];
|
|
|
|
resolve(value ? value : [NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getTraceAttributes:
|
|
|
|
(NSString *) identifier
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
resolve([[self getOrCreateTrace:identifier] attributes]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getTraceLongMetric:
|
|
|
|
(NSString *) identifier
|
|
|
|
metricName:(NSString *) metricName
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
int64_t value = [[self getOrCreateTrace:identifier] valueForIntMetric:metricName];
|
|
|
|
resolve(@(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(incrementTraceMetric:
|
|
|
|
(NSString *) identifier
|
|
|
|
metricName:(NSString *) metricName
|
2018-08-18 15:41:25 +00:00
|
|
|
incrementBy:(nonnull NSNumber *) incrementBy
|
2018-07-18 15:05:59 +00:00
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
int64_t byInt = [incrementBy intValue];
|
|
|
|
[[self getOrCreateTrace:identifier] incrementMetric:metricName byInt:byInt];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(putTraceAttribute:
|
|
|
|
(NSString *) identifier
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
value:(NSString *) value
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
FIRTrace * trace = [self getOrCreateTrace:identifier];
|
|
|
|
[trace setValue:value forAttribute:attribute];
|
|
|
|
|
|
|
|
if (trace.attributes[attribute] != nil) {
|
|
|
|
resolve(@(YES));
|
|
|
|
} else {
|
|
|
|
resolve(@(NO));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(putTraceMetric:
|
|
|
|
(NSString *) identifier
|
|
|
|
attribute:(NSString *) attribute
|
2018-08-18 15:41:25 +00:00
|
|
|
value:(nonnull NSNumber *) value
|
2018-07-18 15:05:59 +00:00
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
int64_t byInt = [value intValue];
|
|
|
|
[[self getOrCreateTrace:identifier] setIntValue:byInt forMetric:attribute];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(removeTraceAttribute:
|
|
|
|
(NSString *) identifier
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateTrace:identifier] removeAttribute:attribute];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP Metric
|
|
|
|
*/
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(startHttpMetric:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] start];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(stopHttpMetric:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] stop];
|
|
|
|
[_httpMetrics removeObjectForKey:[NSString stringWithFormat:@"%@%@", url, httpMethod]];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getHttpMetricAttribute:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
NSString *value = [[self getOrCreateHttpMetric:url httpMethod:httpMethod] valueForAttribute:attribute];
|
|
|
|
resolve(value ? value : [NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getHttpMetricAttributes:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
resolve([[self getOrCreateHttpMetric:url httpMethod:httpMethod] attributes]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(putHttpMetricAttribute:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
value:(NSString *) value
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
FIRHTTPMetric * httpMetric = [self getOrCreateHttpMetric:url httpMethod:httpMethod];
|
|
|
|
[httpMetric setValue:value forAttribute:attribute];
|
|
|
|
|
|
|
|
if (httpMetric.attributes[attribute] != nil) {
|
|
|
|
resolve(@(YES));
|
|
|
|
} else {
|
|
|
|
resolve(@(NO));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(removeHttpMetricAttribute:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
attribute:(NSString *) attribute
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] removeAttribute:attribute];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setHttpMetricResponseCode:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
2018-08-18 15:41:25 +00:00
|
|
|
code:(nonnull NSNumber *) code
|
2018-07-18 15:05:59 +00:00
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] setResponseCode:[code integerValue]];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setHttpMetricRequestPayloadSize:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
2018-08-18 15:41:25 +00:00
|
|
|
bytes:(nonnull NSNumber *) bytes
|
2018-07-18 15:05:59 +00:00
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] setRequestPayloadSize:[bytes longLongValue]];
|
|
|
|
resolve([NSNull null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setHttpMetricResponseContentType:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
|
|
|
type:(NSString *) type
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] setResponseContentType:type];
|
|
|
|
resolve([NSNull null]);
|
2017-05-25 09:39:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:05:59 +00:00
|
|
|
RCT_EXPORT_METHOD(setHttpMetricResponsePayloadSize:
|
|
|
|
(NSString *) url
|
|
|
|
httpMethod:(NSString *) httpMethod
|
2018-08-18 15:41:25 +00:00
|
|
|
bytes:(nonnull NSNumber *) bytes
|
2018-07-18 15:05:59 +00:00
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
[[self getOrCreateHttpMetric:url httpMethod:httpMethod] setResponsePayloadSize:[bytes longLongValue]];
|
|
|
|
resolve([NSNull null]);
|
2017-05-25 09:39:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 08:18:01 +00:00
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-05-25 09:39:54 +00:00
|
|
|
@end
|
2017-05-25 14:03:20 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
@implementation RNFirebasePerformance
|
|
|
|
@end
|
|
|
|
#endif
|