Skip to content

Commit

Permalink
CW-performance-monitoring Added firebase performance monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MeyerPV committed Jul 10, 2024
1 parent 35cb14a commit 7f22e1c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/pages/OldCommon/hooks/useCommonMembers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { trace } from "firebase/performance";
import { CommonService, Logger, UserService } from "@/services";
import { store } from "@/shared/appConfig";
import { LoadingState } from "@/shared/interfaces";
Expand All @@ -10,6 +11,7 @@ import {
selectUserStates,
} from "@/store/states";
import { useDeepCompareEffect } from "react-use";
import { perf } from "@/shared/utils/firebase";

interface Options {
commonId?: string;
Expand Down Expand Up @@ -111,6 +113,9 @@ export const useCommonMembers = ({ commonId }: Options): Return => {

(async () => {
try {
const useCommonMembersTrace = trace(perf, 'useCommonMembers');
useCommonMembersTrace.start();

const cachedUserStates = selectUserStates()(store.getState());
const hasUsersFromCache = commonMembers.some(
({ userId }) => cachedUserStates[userId]?.data,
Expand Down Expand Up @@ -183,6 +188,7 @@ export const useCommonMembers = ({ commonId }: Options): Return => {
};
});
dispatch(cacheActions.updateUserStates(fetchedUsers));
useCommonMembersTrace.stop();
} catch (err) {
Logger.error(err);
setState((prevState) => ({
Expand Down
11 changes: 10 additions & 1 deletion src/shared/hooks/useCases/useDiscussionMessagesById.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useDeepCompareEffect, useUpdateEffect } from "react-use";
import { trace } from "firebase/performance";
import {
DiscussionMessageService,
MESSAGES_NUMBER_IN_BATCH,
Expand All @@ -20,7 +21,7 @@ import {
User,
} from "@/shared/models";
import { InternalLinkData } from "@/shared/utils";
import firebase from "@/shared/utils/firebase";
import firebase, { perf } from "@/shared/utils/firebase";
import {
cacheActions,
selectDiscussionMessagesStateByDiscussionId,
Expand Down Expand Up @@ -153,6 +154,9 @@ export const useDiscussionMessagesById = ({

const fetchRepliedMessages = useCallback(
async (messageId: string, endDate: Date): Promise<void> => {
const fetchRepliedMessagesTrace = trace(perf, 'fetchRepliedMessagesTrace');
fetchRepliedMessagesTrace.start();

if (state.data?.find((item) => item.id === messageId)) {
return Promise.resolve();
}
Expand Down Expand Up @@ -206,6 +210,7 @@ export const useDiscussionMessagesById = ({
updatedDiscussionMessages: discussionsWithText,
}),
);
fetchRepliedMessagesTrace.stop();
},
[
state.data,
Expand Down Expand Up @@ -239,6 +244,9 @@ export const useDiscussionMessagesById = ({
}

try {
const fetchDiscussionMessagesTrace = trace(perf, 'fetchDiscussionMessages');
fetchDiscussionMessagesTrace.start();

DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
discussionId,
lastVisible && lastVisible[discussionId],
Expand Down Expand Up @@ -308,6 +316,7 @@ export const useDiscussionMessagesById = ({
setIsBatchLoading(false);
},
);
fetchDiscussionMessagesTrace.stop();
} catch(err) {
setIsBatchLoading(false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/shared/utils/firebase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "firebase/compat/auth";
import "firebase/compat/firestore";
import "firebase/compat/performance";
import "firebase/compat/storage";
import { getPerformance } from "firebase/performance";
import { local } from "@/config";
import { Environment, REACT_APP_ENV } from "@/shared/constants";
import config from "../../config";
Expand All @@ -11,7 +12,7 @@ interface FirebaseError extends Error {
code: string;
}

firebase.initializeApp(config.firebase);
const app = firebase.initializeApp(config.firebase);

if (REACT_APP_ENV === Environment.Local) {
firebase.auth().useEmulator(local.firebase.authDomain);
Expand All @@ -33,6 +34,7 @@ if (REACT_APP_ENV === Environment.Local) {
});
}

export const perf = getPerformance(app);
// firebase.firestore.setLogLevel("debug");

export const isFirebaseError = (error: any): error is FirebaseError =>
Expand Down

0 comments on commit 7f22e1c

Please sign in to comment.