From 2c776a60e8424a1a2f6e80df23052c8eb9b977bb Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Thu, 12 Jul 2018 13:25:11 +0100 Subject: [PATCH] [perf] Return Promise types --- .../firebase/perf/RNFirebasePerformance.java | 62 +++++++++++++++++++ bridge/e2e/perf/perf.e2e.js | 15 +++++ lib/modules/perf/HttpMetric.js | 5 +- lib/modules/perf/Trace.js | 5 +- lib/modules/perf/index.js | 39 +++++++++++- 5 files changed, 121 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/perf/RNFirebasePerformance.java b/android/src/main/java/io/invertase/firebase/perf/RNFirebasePerformance.java index da7c071d..1499899c 100644 --- a/android/src/main/java/io/invertase/firebase/perf/RNFirebasePerformance.java +++ b/android/src/main/java/io/invertase/firebase/perf/RNFirebasePerformance.java @@ -48,6 +48,7 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule { @ReactMethod public void getTraceAttribute(String identifier, String attribute, Promise promise) { promise.resolve(getOrCreateTrace(identifier).getAttribute(attribute)); +<<<<<<< Updated upstream } @ReactMethod @@ -75,6 +76,35 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule { } @ReactMethod +======= + } + + @ReactMethod + public void getTraceAttributes(String identifier, Promise promise) { + Map attributes = getOrCreateTrace(identifier).getAttributes(); + WritableMap map = Arguments.createMap(); + + for (Map.Entry entry : attributes.entrySet()) { + map.putString(entry.getKey(), entry.getValue()); + } + + promise.resolve(map); + } + + @ReactMethod + public void getTraceLongMetric(String identifier, String metricName, Promise promise) { + Integer value = Long.valueOf(getOrCreateTrace(identifier).getLongMetric(metricName)).intValue(); + promise.resolve(value); + } + + @ReactMethod + public void incrementTraceMetric(String identifier, String metricName, Integer incrementBy, Promise promise) { + getOrCreateTrace(identifier).incrementMetric(metricName, incrementBy.longValue()); + promise.resolve(null); + } + + @ReactMethod +>>>>>>> Stashed changes public void putTraceAttribute(String identifier, String attribute, String value, Promise promise) { getOrCreateTrace(identifier).putAttribute(attribute, value); promise.resolve(null); @@ -197,8 +227,40 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule { if (httpMetrics.containsKey(identifier)) { return httpMetrics.get(identifier); } +<<<<<<< Updated upstream HttpMetric httpMetric = FirebasePerformance.getInstance().newHttpMetric(url, httpMethod); httpMetrics.put(identifier, httpMetric); return httpMetric; } +======= + HttpMetric httpMetric = FirebasePerformance.getInstance().newHttpMetric(url, this.mapStringToMethod(httpMethod)); + httpMetrics.put(identifier, httpMetric); + return httpMetric; + } + + private String mapStringToMethod(String value) { + switch (value) { + case "CONNECT": + return FirebasePerformance.HttpMethod.CONNECT; + case "DELETE": + return FirebasePerformance.HttpMethod.DELETE; + case "GET": + return FirebasePerformance.HttpMethod.GET; + case "HEAD": + return FirebasePerformance.HttpMethod.HEAD; + case "OPTIONS": + return FirebasePerformance.HttpMethod.OPTIONS; + case "PATCH": + return FirebasePerformance.HttpMethod.PATCH; + case "POST": + return FirebasePerformance.HttpMethod.POST; + case "PUT": + return FirebasePerformance.HttpMethod.PUT; + case "TRACE": + return FirebasePerformance.HttpMethod.TRACE; + } + + return ""; + } +>>>>>>> Stashed changes } diff --git a/bridge/e2e/perf/perf.e2e.js b/bridge/e2e/perf/perf.e2e.js index 20f0f41d..2936668b 100644 --- a/bridge/e2e/perf/perf.e2e.js +++ b/bridge/e2e/perf/perf.e2e.js @@ -30,6 +30,7 @@ describe('perf()', () => { describe('newHttpMetric()', () => { it('returns an instance of HttpMetric', async () => { +<<<<<<< Updated upstream const trace = firebase.perf().newHttpMetric('foo', 'bar'); trace.constructor.name.should.be.equal('HttpMetric'); }); @@ -38,6 +39,20 @@ describe('perf()', () => { (() => firebase.perf().newHttpMetric(123, [1, 2])).should.throw( 'firebase.perf().newHttpMetric() requires url and httpMethod string values' ); +======= + const trace = firebase.perf().newHttpMetric('foo', 'GET'); + trace.constructor.name.should.be.equal('HttpMetric'); + }); + + it('errors if url/httpMethod not a string', async () => { + (() => firebase.perf().newHttpMetric(123, [1, 2])).should.throw( + 'firebase.perf().newHttpMetric() requires url and httpMethod string values' + ); + }); + + it('errors if httpMethod not a valid type', async () => { + (() => firebase.perf().newHttpMetric('foo', 'FOO')).should.throw(); // TODO error +>>>>>>> Stashed changes }); }); }); diff --git a/lib/modules/perf/HttpMetric.js b/lib/modules/perf/HttpMetric.js index cbeeab94..eb85df2d 100644 --- a/lib/modules/perf/HttpMetric.js +++ b/lib/modules/perf/HttpMetric.js @@ -16,7 +16,7 @@ export default class HttpMetric { this.httpMethod = httpMethod; } - getAttribute(attribute: string): Promise { + getAttribute(attribute: string): Promise { return getNativeModule(this._perf).getHttpMetricAttribute( this.url, this.httpMethod, @@ -31,7 +31,8 @@ export default class HttpMetric { ); } - putAttribute(attribute: string, value: string): Promise { + // TODO return true or false + putAttribute(attribute: string, value: string): Promise { return getNativeModule(this._perf).putHttpMetricAttribute( this.url, this.httpMethod, diff --git a/lib/modules/perf/Trace.js b/lib/modules/perf/Trace.js index bf784c61..174f1bef 100644 --- a/lib/modules/perf/Trace.js +++ b/lib/modules/perf/Trace.js @@ -14,7 +14,7 @@ export default class Trace { this.identifier = identifier; } - getAttribute(attribute: string): Promise { + getAttribute(attribute: string): Promise { return getNativeModule(this._perf).getTraceAttribute( this.identifier, attribute @@ -40,7 +40,8 @@ export default class Trace { ); } - putAttribute(attribute: string, value: string): Promise { + // TODO return true or false + putAttribute(attribute: string, value: string): Promise { return getNativeModule(this._perf).putTraceAttribute( this.identifier, attribute, diff --git a/lib/modules/perf/index.js b/lib/modules/perf/index.js index 92282167..a7a25ad7 100644 --- a/lib/modules/perf/index.js +++ b/lib/modules/perf/index.js @@ -12,6 +12,29 @@ import type App from '../core/app'; export const MODULE_NAME = 'RNFirebasePerformance'; export const NAMESPACE = 'perf'; +const HTTP_METHODS = { + CONNECT: true, + DELETE: true, + GET: true, + HEAD: true, + OPTIONS: true, + PATCH: true, + POST: true, + PUT: true, + TRACE: true, +}; + +type HttpMethod = + | 'CONNECT' + | 'DELETE' + | 'GET' + | 'HEAD' + | 'OPTIONS' + | 'PATCH' + | 'POST' + | 'PUT' + | 'TRACE'; + export default class PerformanceMonitoring extends ModuleBase { constructor(app: App) { super(app, { @@ -49,13 +72,27 @@ export default class PerformanceMonitoring extends ModuleBase { return new Trace(this, trace); } - newHttpMetric(url: string, httpMethod: string) { + /** + * Return a new HttpMetric instance + * @param url + * @param httpMethod + * @returns {HttpMetric} + */ + newHttpMetric(url: string, httpMethod: HttpMethod): HttpMetric { if (typeof url !== 'string' || typeof httpMethod !== 'string') { throw new Error( 'firebase.perf().newHttpMetric() requires url and httpMethod string values' ); } + if (!HTTP_METHODS[httpMethod]) { + throw new Error( + `firebase.perf().newHttpMetric() httpMethod should be one of ${Object.keys( + HTTP_METHODS + ).join(', ')}` + ); + } + return new HttpMetric(this, url, httpMethod); } }