Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoQA] Add generic way for attributes, limit custom attributes for firebase #51960

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libs/Firebase/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Firebase/index.web.ts
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/libs/Firebase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type TraceMap = Record<string, Trace>;
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;
Expand All @@ -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<PerfAttributes, 'accountId' | 'personalDetailsLength' | 'reportActionsLength' | 'reportsLength' | 'policiesLength'>;
mountiny marked this conversation as resolved.
Show resolved Hide resolved

export type {StartTrace, StopTrace, TraceMap, Log, PerfAttributes, FirebaseAttributes};
38 changes: 20 additions & 18 deletions src/libs/Firebase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends keyof PerfAttributes>(attributes?: T[]): Pick<PerfAttributes, T> {
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<PerfAttributes, T>;
attributes.forEach((attribute) => {
selectedAttributes[attribute] = allAttributes[attribute];
});
return selectedAttributes;
}

return allAttributes;
}

export default {
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/FirebaseUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -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<keyof PerfAttributes> = [
'accountId',
'personalDetailsLength',
'reportActionsLength',
'reportsLength',
'policiesLength',
'transactionsLength',
'transactionViolationsLength',
'policyType',
'policyRole',
];

const checkAttributes = (attributes: Partial<PerfAttributes>, expectedAttributes: Array<keyof Partial<PerfAttributes>>) => {
expectedAttributes.forEach((attr) => {
expect(attributes).toHaveProperty(attr);
});
expect(Object.keys(attributes).length).toEqual(expectedAttributes.length);
};

it('should return 5 specific attributes', () => {
const requestedAttributes: Array<keyof FirebaseAttributes> = ['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);
});
});
Loading