From f1ffe20d8d608ba4ce4cc9f3d5bcd6f58486e4aa Mon Sep 17 00:00:00 2001 From: Tomasz Lesniakiewicz Date: Mon, 4 Nov 2024 16:52:19 +0100 Subject: [PATCH 1/2] add generic way for attributes, limit for firebase --- src/libs/Firebase/index.native.ts | 4 ++-- src/libs/Firebase/index.web.ts | 4 ++-- src/libs/Firebase/types.ts | 7 ++++-- src/libs/Firebase/utils.ts | 38 ++++++++++++++++--------------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/libs/Firebase/index.native.ts b/src/libs/Firebase/index.native.ts index 39a23440c77d..ba899dee1732 100644 --- a/src/libs/Firebase/index.native.ts +++ b/src/libs/Firebase/index.native.ts @@ -2,7 +2,7 @@ import crashlytics from '@react-native-firebase/crashlytics'; import perf from '@react-native-firebase/perf'; import * as Environment from '@libs/Environment/Environment'; -import type {Log, StartTrace, StopTrace, TraceMap} from './types'; +import type {FirebaseAttributes, Log, StartTrace, StopTrace, TraceMap} from './types'; import utils from './utils'; const traceMap: TraceMap = {}; @@ -17,7 +17,7 @@ const startTrace: StartTrace = (customEventName) => { return; } - const attributes = utils.getAttributes(); + const attributes: FirebaseAttributes = utils.getAttributes(['accountId', 'personalDetailsLength', 'reportActionsLength', 'reportsLength', 'policiesLength']); perf() .startTrace(customEventName) diff --git a/src/libs/Firebase/index.web.ts b/src/libs/Firebase/index.web.ts index 2d42154d3c26..483e63617978 100644 --- a/src/libs/Firebase/index.web.ts +++ b/src/libs/Firebase/index.web.ts @@ -1,7 +1,7 @@ import {trace} from '@firebase/performance'; import * as Environment from '@libs/Environment/Environment'; import {firebasePerfWeb} from './firebaseWebConfig'; -import type {Log, StartTrace, StopTrace, TraceMap} from './types'; +import type {FirebaseAttributes, Log, StartTrace, StopTrace, TraceMap} from './types'; import utils from './utils'; const traceMap: TraceMap = {}; @@ -19,7 +19,7 @@ const startTrace: StartTrace = (customEventName) => { const perfTrace = trace(firebasePerfWeb, customEventName); - const attributes = utils.getAttributes(); + const attributes: FirebaseAttributes = utils.getAttributes(['accountId', 'personalDetailsLength', 'reportActionsLength', 'reportsLength', 'policiesLength']); Object.entries(attributes).forEach(([name, value]) => { perfTrace.putAttribute(name, value); diff --git a/src/libs/Firebase/types.ts b/src/libs/Firebase/types.ts index 4c970375c226..2f6a8b42505e 100644 --- a/src/libs/Firebase/types.ts +++ b/src/libs/Firebase/types.ts @@ -9,7 +9,7 @@ type TraceMap = Record; type StartTrace = (customEventName: string) => void; type StopTrace = (customEventName: string) => void; type Log = (action: string) => void; -type FirebaseAttributes = { +type PerfAttributes = { accountId: string; personalDetailsLength: string; reportsLength: string; @@ -21,4 +21,7 @@ type FirebaseAttributes = { policyRole: string; }; -export type {StartTrace, StopTrace, TraceMap, Log, FirebaseAttributes}; +// TODO confirm which attributes are required for Firebase +type FirebaseAttributes = Pick; + +export type {StartTrace, StopTrace, TraceMap, Log, PerfAttributes, FirebaseAttributes}; diff --git a/src/libs/Firebase/utils.ts b/src/libs/Firebase/utils.ts index 4f7718e691ed..01df2bfc8a7e 100644 --- a/src/libs/Firebase/utils.ts +++ b/src/libs/Firebase/utils.ts @@ -4,31 +4,33 @@ import {getActivePolicy, getAllPoliciesLength} from '@libs/PolicyUtils'; import {getReportActionsLength} from '@libs/ReportActionsUtils'; import * as ReportConnection from '@libs/ReportConnection'; import * as SessionUtils from '@libs/SessionUtils'; -import type {FirebaseAttributes} from './types'; +import type {PerfAttributes} from './types'; -function getAttributes(): FirebaseAttributes { +function getAttributes(attributes?: T[]): Pick { const session = SessionUtils.getSession(); - - const accountId = session?.accountID?.toString() ?? 'N/A'; - const reportsLength = ReportConnection.getAllReportsLength().toString(); - const reportActionsLength = getReportActionsLength().toString(); - const personalDetailsLength = PersonalDetailsUtils.getPersonalDetailsLength().toString(); - const transactionViolationsLength = getAllTransactionViolationsLength().toString(); - const policiesLength = getAllPoliciesLength().toString(); - const transactionsLength = getAllTransactions().toString(); const policy = getActivePolicy(); - return { - accountId, - reportsLength, - reportActionsLength, - personalDetailsLength, - transactionViolationsLength, - policiesLength, - transactionsLength, + const allAttributes: PerfAttributes = { + accountId: session?.accountID?.toString() ?? 'N/A', + reportsLength: ReportConnection.getAllReportsLength().toString(), + reportActionsLength: getReportActionsLength().toString(), + personalDetailsLength: PersonalDetailsUtils.getPersonalDetailsLength().toString(), + transactionViolationsLength: getAllTransactionViolationsLength().toString(), + policiesLength: getAllPoliciesLength().toString(), + transactionsLength: getAllTransactions().toString(), policyType: policy?.type ?? 'N/A', policyRole: policy?.role ?? 'N/A', }; + + if (attributes && attributes.length > 0) { + const selectedAttributes = {} as Pick; + attributes.forEach((attribute) => { + selectedAttributes[attribute] = allAttributes[attribute]; + }); + return selectedAttributes; + } + + return allAttributes; } export default { From b534a11b1e134a788be2b788a8214b8433ac3a64 Mon Sep 17 00:00:00 2001 From: Tomasz Lesniakiewicz Date: Wed, 6 Nov 2024 15:06:56 +0100 Subject: [PATCH 2/2] add unit test for getAttributes --- tests/unit/FirebaseUtilsTest.ts | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/unit/FirebaseUtilsTest.ts diff --git a/tests/unit/FirebaseUtilsTest.ts b/tests/unit/FirebaseUtilsTest.ts new file mode 100644 index 000000000000..efe273c0d6e1 --- /dev/null +++ b/tests/unit/FirebaseUtilsTest.ts @@ -0,0 +1,40 @@ +import {expect} from '@jest/globals'; +import type {FirebaseAttributes, PerfAttributes} from '@libs/Firebase/types'; +import utils from '@libs/Firebase/utils'; + +describe('getAttributes', () => { + const allAttributes: Array = [ + 'accountId', + 'personalDetailsLength', + 'reportActionsLength', + 'reportsLength', + 'policiesLength', + 'transactionsLength', + 'transactionViolationsLength', + 'policyType', + 'policyRole', + ]; + + const checkAttributes = (attributes: Partial, expectedAttributes: Array>) => { + expectedAttributes.forEach((attr) => { + expect(attributes).toHaveProperty(attr); + }); + expect(Object.keys(attributes).length).toEqual(expectedAttributes.length); + }; + + it('should return 5 specific attributes', () => { + const requestedAttributes: Array = ['accountId', 'personalDetailsLength', 'reportActionsLength', 'reportsLength', 'policiesLength']; + const attributes = utils.getAttributes(requestedAttributes); + checkAttributes(attributes, requestedAttributes); + }); + + it('should return all attributes when no array is passed', () => { + const attributes = utils.getAttributes(); + checkAttributes(attributes, allAttributes); + }); + + it('should return all attributes when an empty array is passed', () => { + const attributes = utils.getAttributes([]); + checkAttributes(attributes, allAttributes); + }); +});