Skip to content

Commit

Permalink
move getAttributes to firebase module, add mocks for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rinej committed Aug 9, 2024
1 parent 38e12d6 commit caab39b
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 49 deletions.
3 changes: 1 addition & 2 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import useThemeStyles from '@hooks/useThemeStyles';
import ControlSelection from '@libs/ControlSelection';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import getAttributes from '@libs/Firebase/utils';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReceiptUtils from '@libs/ReceiptUtils';
Expand Down Expand Up @@ -374,7 +373,7 @@ function ReportPreview({
<View style={[styles.chatItemMessage, containerStyles]}>
<PressableWithoutFeedback
onPress={() => {
Timing.start(CONST.TIMING.SWITCH_REPORT, getAttributes());
Timing.start(CONST.TIMING.SWITCH_REPORT);
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(iouReportID));
}}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
Expand Down
31 changes: 22 additions & 9 deletions src/libs/Firebase/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import crashlytics from '@react-native-firebase/crashlytics';
import perf from '@react-native-firebase/perf';
import * as Environment from '@libs/Environment/Environment';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as SessionUtils from '@libs/SessionUtils';
import type {Log, StartTrace, StopTrace, TraceMap} from './types';
import type {FirebaseAttributes, Log, StartTrace, StopTrace, TraceMap} from './types';

const traceMap: TraceMap = {};

const startTrace: StartTrace = (customEventName, attributes) => {
const startTrace: StartTrace = (customEventName) => {
const start = global.performance.now();
if (Environment.isDevelopment()) {
return;
Expand All @@ -17,17 +19,14 @@ const startTrace: StartTrace = (customEventName, attributes) => {
return;
}

const session = SessionUtils.getSession();
const attributes = getAttributes();

perf()
.startTrace(customEventName)
.then((trace) => {
trace.putAttribute('accountID', session?.accountID?.toString() ?? 'N/A');
if (attributes) {
Object.entries(attributes).forEach(([name, value]) => {
trace.putAttribute(name, value);
});
}
Object.entries(attributes).forEach(([name, value]) => {
trace.putAttribute(name, value);
});
traceMap[customEventName] = {
trace,
start,
Expand Down Expand Up @@ -60,6 +59,20 @@ const log: Log = (action: string) => {
crashlytics().log(action);
};

function getAttributes(): FirebaseAttributes {
const session = SessionUtils.getSession();

const accountId = session?.accountID?.toString() ?? 'N/A';
const reportsLength = ReportUtils.getAllReportsLength().toString();
const personalDetailsLength = PersonalDetailsUtils.getPersonalDetailsLength().toString();

return {
accountId,
reportsLength,
personalDetailsLength,
};
}

export default {
startTrace,
stopTrace,
Expand Down
1 change: 1 addition & 0 deletions src/libs/Firebase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type StartTrace = (customEventName: string, attributes?: FirebaseAttributes) =>
type StopTrace = (customEventName: string) => void;
type Log = (action: string) => void;
type FirebaseAttributes = {
accountId: string;
personalDetailsLength: string;
reportsLength: string;
};
Expand Down
18 changes: 0 additions & 18 deletions src/libs/Firebase/utils.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import {READ_COMMANDS} from '@libs/API/types';
import getAttributes from '@libs/Firebase/utils';
import HttpUtils from '@libs/HttpUtils';
import KeyboardShortcut from '@libs/KeyboardShortcut';
import Log from '@libs/Log';
Expand Down Expand Up @@ -211,7 +210,7 @@ function AuthScreens() {
let initialReportID: string | undefined;
const isInitialRender = useRef(true);
if (isInitialRender.current) {
Timing.start(CONST.TIMING.HOMEPAGE_INITIAL_RENDER, getAttributes());
Timing.start(CONST.TIMING.HOMEPAGE_INITIAL_RENDER);

const currentURL = getCurrentUrl();
if (currentURL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import WorkspaceSwitcherButton from '@components/WorkspaceSwitcherButton';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import getAttributes from '@libs/Firebase/utils';
import Navigation from '@libs/Navigation/Navigation';
import Performance from '@libs/Performance';
import SignInButton from '@pages/home/sidebar/SignInButton';
Expand Down Expand Up @@ -66,7 +65,7 @@ function TopBar({breadcrumbLabel, activeWorkspaceID, shouldDisplaySearch = true}
accessibilityLabel={translate('sidebarScreen.buttonFind')}
style={[styles.flexRow, styles.mr2, styles.touchableButtonImage]}
onPress={Session.checkIfActionIsAllowed(() => {
Timing.start(CONST.TIMING.CHAT_FINDER_RENDER, getAttributes());
Timing.start(CONST.TIMING.CHAT_FINDER_RENDER);
Performance.markStart(CONST.TIMING.CHAT_FINDER_RENDER);
Navigation.navigate(ROUTES.CHAT_FINDER);
})}
Expand Down
3 changes: 1 addition & 2 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import times from '@src/utils/times';
import Timing from './actions/Timing';
import * as ErrorUtils from './ErrorUtils';
import filterArrayByMatch from './filterArrayByMatch';
import getAttributes from './Firebase/utils';
import localeCompare from './LocaleCompare';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as Localize from './Localize';
Expand Down Expand Up @@ -2078,7 +2077,7 @@ function getOptions(
* Build the options for the Search view
*/
function getSearchOptions(options: OptionList, searchValue = '', betas: Beta[] = []): Options {
Timing.start(CONST.TIMING.LOAD_SEARCH_OPTIONS, getAttributes());
Timing.start(CONST.TIMING.LOAD_SEARCH_OPTIONS);
Performance.markStart(CONST.TIMING.LOAD_SEARCH_OPTIONS);
const optionList = getOptions(options, {
betas,
Expand Down
5 changes: 2 additions & 3 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {GetMissingOnyxMessagesParams, HandleRestrictedEventParams, OpenAppP
import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as Browser from '@libs/Browser';
import {buildEmojisTrie} from '@libs/EmojiTrie';
import getAttributes from '@libs/Firebase/utils';
import Log from '@libs/Log';
import getCurrentUrl from '@libs/Navigation/currentUrl';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -265,7 +264,7 @@ function reconnectApp(updateIDFrom: OnyxEntry<number> = 0) {
//
// - Look through the local report actions and reports to find the most recently modified report action or report.
// - We send this to the server so that it can compute which new chats the user needs to see and return only those as an optimization.
Timing.start(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION, getAttributes());
Timing.start(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION);
params.mostRecentReportActionLastModified = ReportActionsUtils.getMostRecentReportActionLastModified();
Timing.end(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION, '', 500);

Expand Down Expand Up @@ -294,7 +293,7 @@ function finalReconnectAppAfterActivatingReliableUpdates(): Promise<void | OnyxT
//
// - Look through the local report actions and reports to find the most recently modified report action or report.
// - We send this to the server so that it can compute which new chats the user needs to see and return only those as an optimization.
Timing.start(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION, getAttributes());
Timing.start(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION);
params.mostRecentReportActionLastModified = ReportActionsUtils.getMostRecentReportActionLastModified();
Timing.end(CONST.TIMING.CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION, '', 500);

Expand Down
3 changes: 1 addition & 2 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import useViewportOffsetTop from '@hooks/useViewportOffsetTop';
import Timing from '@libs/actions/Timing';
import getAttributes from '@libs/Firebase/utils';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import clearReportNotifications from '@libs/Notification/clearReportNotifications';
Expand Down Expand Up @@ -273,7 +272,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro

const wasReportAccessibleRef = useRef(false);
if (firstRenderRef.current) {
Timing.start(CONST.TIMING.CHAT_RENDER, getAttributes());
Timing.start(CONST.TIMING.CHAT_RENDER);
Performance.markStart(CONST.TIMING.CHAT_RENDER);
}
const [isComposerFocus, setIsComposerFocus] = useState(false);
Expand Down
3 changes: 1 addition & 2 deletions src/pages/home/report/ReportActionItemParentAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
import TripDetailsView from '@components/ReportActionItem/TripDetailsView';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import getAttributes from '@libs/Firebase/utils';
import Navigation from '@libs/Navigation/Navigation';
import onyxSubscribe from '@libs/onyxSubscribe';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
Expand Down Expand Up @@ -136,7 +135,7 @@ function ReportActionItemParentAction({
// Pop the chat report screen before navigating to the linked report action.
Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(ancestor.report.parentReportID ?? '-1', ancestor.reportAction.reportActionID));
}
Timing.start(CONST.TIMING.SWITCH_REPORT, getAttributes());
Timing.start(CONST.TIMING.SWITCH_REPORT);
}
: undefined
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/home/report/ReportActionItemThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Timing from '@libs/actions/Timing';
import getAttributes from '@libs/Firebase/utils';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import type {Icon} from '@src/types/onyx/OnyxCommon';
Expand Down Expand Up @@ -47,7 +46,7 @@ function ReportActionItemThread({numberOfReplies, icons, mostRecentReply, childR
<PressableWithSecondaryInteraction
onPress={() => {
Report.navigateToAndOpenChildReport(childReportID);
Timing.start(CONST.TIMING.SWITCH_REPORT, getAttributes());
Timing.start(CONST.TIMING.SWITCH_REPORT);
}}
role={CONST.ROLE.BUTTON}
accessibilityLabel={`${numberOfReplies} ${replyText}`}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/home/report/ThreadDivider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import getAttributes from '@libs/Firebase/utils';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import type {Ancestor} from '@libs/ReportUtils';
Expand Down Expand Up @@ -49,7 +48,7 @@ function ThreadDivider({ancestor, isLinkDisabled = false}: ThreadDividerProps) {
) : (
<PressableWithoutFeedback
onPress={() => {
Timing.start(CONST.TIMING.SWITCH_REPORT, getAttributes());
Timing.start(CONST.TIMING.SWITCH_REPORT);
const isVisibleAction = ReportActionsUtils.shouldReportActionBeVisible(ancestor.reportAction, ancestor.reportAction.reportActionID ?? '-1');
// Pop the thread report screen before navigating to the chat report.
Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(ancestor.report.reportID ?? '-1'));
Expand Down
5 changes: 2 additions & 3 deletions src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {updateLastAccessedWorkspace} from '@libs/actions/Policy/Policy';
import * as Browser from '@libs/Browser';
import getAttributes from '@libs/Firebase/utils';
import TopBar from '@libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar';
import Navigation from '@libs/Navigation/Navigation';
import Performance from '@libs/Performance';
Expand All @@ -20,7 +19,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
* Function called when a pinned chat is selected.
*/
const startTimer = () => {
Timing.start(CONST.TIMING.SWITCH_REPORT, getAttributes());
Timing.start(CONST.TIMING.SWITCH_REPORT);
Performance.markStart(CONST.TIMING.SWITCH_REPORT);
};

Expand All @@ -32,7 +31,7 @@ function BaseSidebarScreen() {

useEffect(() => {
Performance.markStart(CONST.TIMING.SIDEBAR_LOADED);
Timing.start(CONST.TIMING.SIDEBAR_LOADED, getAttributes());
Timing.start(CONST.TIMING.SIDEBAR_LOADED);
}, []);

useEffect(() => {
Expand Down
5 changes: 5 additions & 0 deletions tests/perf-test/BaseOptionsList.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type BaseOptionsListWrapperProps = {
canSelectMultipleOptions?: boolean;
};

jest.mock('@src/libs/actions/Timing', () => ({
start: jest.fn(),
end: jest.fn(),
}));

describe('[BaseOptionsList]', () => {
function BaseOptionsListWrapper({canSelectMultipleOptions = false}: BaseOptionsListWrapperProps) {
const [selectedIds, setSelectedIds] = useState<string[]>([]);
Expand Down
5 changes: 5 additions & 0 deletions tests/perf-test/ChatFinderPage.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ jest.mock('@react-navigation/native', () => {
};
});

jest.mock('@src/libs/actions/Timing', () => ({
start: jest.fn(),
end: jest.fn(),
}));

jest.mock('@src/components/withNavigationFocus', () => (Component: ComponentType<WithNavigationFocusProps>) => {
function WithNavigationFocus(props: WithNavigationFocusProps) {
return (
Expand Down
5 changes: 5 additions & 0 deletions tests/perf-test/OptionsListUtils.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ jest.mock('@react-navigation/native', () => {
};
});

jest.mock('@src/libs/actions/Timing', () => ({
start: jest.fn(),
end: jest.fn(),
}));

const options = OptionsListUtils.createOptionList(personalDetails, reports);

/* GetOption is the private function and is never called directly, we are testing the functions which call getOption with different params */
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/OptionsListUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

type PersonalDetailsList = Record<string, PersonalDetails & ReportUtils.OptionData>;

jest.mock('@src/libs/actions/Timing', () => ({
start: jest.fn(),
end: jest.fn(),
}));

describe('OptionsListUtils', () => {
// Given a set of reports with both single participants and multiple participants some pinned and some not
const REPORTS: OnyxCollection<Report> = {
Expand Down

0 comments on commit caab39b

Please sign in to comment.