-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
refactor webhookAnalytics call and enrich analytics module #8253
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
35a64da
refactor webhook call and analytics
anamarn 2adc9ed
eliminate console log
anamarn d8c8350
Small fixes
FelixMalfait acfb7c7
Improve typing
FelixMalfait 2e44eaa
fix comments, doing serverlessFunctionGraphs
anamarn 9bea5a0
add serverless function graphs and tests
anamarn 214a7da
Merge branch 'main' into feat/refacto-analytics-front
anamarn 248250a
add new tewt
anamarn b3ee38e
eliminate composite and add test
anamarn 1c674ba
Unrelated change (blue focus on textarea)
FelixMalfait 9e299b4
Remove useEffect
FelixMalfait e4ddcae
clean code and add correct icon
anamarn 7cbeec7
correct tsconfig
anamarn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/twenty-front/src/modules/analytics/components/AnalyticsGraphEffect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useAnalyticsGraphDataState } from '@/analytics/hooks/useAnalyticsGraphDataState'; | ||
import { useGraphData } from '@/analytics/hooks/useGraphData'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
import { AnalyticsTinybirdJwtMap } from '~/generated-metadata/graphql'; | ||
|
||
type AnalyticsGraphEffectProps = { | ||
recordId: string; | ||
recordType: string; | ||
endpointName: keyof AnalyticsTinybirdJwtMap; | ||
}; | ||
|
||
export const AnalyticsGraphEffect = ({ | ||
recordId, | ||
recordType, | ||
endpointName, | ||
}: AnalyticsGraphEffectProps) => { | ||
const { analyticsState, transformDataFunction } = | ||
useAnalyticsGraphDataState(endpointName); | ||
const setGraphData = useSetRecoilState(analyticsState); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
const { fetchGraphData } = useGraphData({ | ||
recordId, | ||
recordType, | ||
endpointName, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!isLoaded) { | ||
fetchGraphData('7D').then((graphInput) => { | ||
setGraphData(transformDataFunction(graphInput)); | ||
}); | ||
setIsLoaded(true); | ||
} | ||
}, [ | ||
fetchGraphData, | ||
isLoaded, | ||
setGraphData, | ||
recordId, | ||
recordType, | ||
endpointName, | ||
transformDataFunction, | ||
]); | ||
|
||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/twenty-front/src/modules/analytics/constants/AnalyticsGraphOptionMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const ANALYTICS_GRAPH_OPTION_MAP = { | ||
'7D': { granularity: 'day' }, | ||
'1D': { granularity: 'hour' }, | ||
'12H': { granularity: 'hour' }, | ||
'4H': { granularity: 'hour' }, | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/twenty-front/src/modules/analytics/constants/AnalyticsGraphOptionMapCustom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//Use this map when granularity is custom meanwhile i'll use the first map | ||
export const ANALYTICS_GRAPH_OPTION_MAP_CUSTOM = { | ||
'7D': { granularity: 'custom', tickIntervalInMinutes: '420' }, | ||
'1D': { granularity: 'custom', tickIntervalInMinutes: '60' }, | ||
'12H': { granularity: 'custom', tickIntervalInMinutes: '30' }, | ||
'4H': { granularity: 'custom', tickIntervalInMinutes: '10' }, | ||
}; |
87 changes: 87 additions & 0 deletions
87
packages/twenty-front/src/modules/analytics/hooks/__tests__/useAnalyticsTinybirdJwt.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useAnalyticsTinybirdJwts } from '@/analytics/hooks/useAnalyticsTinybirdJwts'; | ||
import { CurrentUser, currentUserState } from '@/auth/states/currentUserState'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper'; | ||
|
||
const Wrapper = getJestMetadataAndApolloMocksWrapper({ | ||
apolloMocks: [], | ||
}); | ||
|
||
describe('useAnalyticsTinybirdJwts', () => { | ||
const JWT_NAME = 'getWebhookAnalytics'; | ||
const TEST_JWT_TOKEN = 'test-jwt-token'; | ||
|
||
it('should return undefined when no user is logged in', () => { | ||
const { result } = renderHook( | ||
() => { | ||
const setCurrentUserState = useSetRecoilState(currentUserState); | ||
return { | ||
hook: useAnalyticsTinybirdJwts(JWT_NAME), | ||
setCurrentUserState, | ||
}; | ||
}, | ||
{ wrapper: Wrapper }, | ||
); | ||
|
||
act(() => { | ||
result.current.setCurrentUserState(null); | ||
}); | ||
|
||
expect(result.current.hook).toBeUndefined(); | ||
}); | ||
|
||
it('should return the correct JWT token when available', () => { | ||
const { result } = renderHook( | ||
() => { | ||
const setCurrentUserState = useSetRecoilState(currentUserState); | ||
return { | ||
hook: useAnalyticsTinybirdJwts(JWT_NAME), | ||
setCurrentUserState, | ||
}; | ||
}, | ||
{ wrapper: Wrapper }, | ||
); | ||
|
||
act(() => { | ||
result.current.setCurrentUserState({ | ||
id: '1', | ||
email: '[email protected]', | ||
canImpersonate: false, | ||
userVars: {}, | ||
analyticsTinybirdJwts: { | ||
[JWT_NAME]: TEST_JWT_TOKEN, | ||
}, | ||
} as CurrentUser); | ||
}); | ||
|
||
expect(result.current.hook).toBe(TEST_JWT_TOKEN); | ||
}); | ||
|
||
it('should return undefined when JWT token is not available', () => { | ||
const { result } = renderHook( | ||
() => { | ||
const setCurrentUserState = useSetRecoilState(currentUserState); | ||
return { | ||
hook: useAnalyticsTinybirdJwts(JWT_NAME), | ||
setCurrentUserState, | ||
}; | ||
}, | ||
{ wrapper: Wrapper }, | ||
); | ||
|
||
act(() => { | ||
result.current.setCurrentUserState({ | ||
id: '1', | ||
email: '[email protected]', | ||
canImpersonate: false, | ||
userVars: {}, | ||
analyticsTinybirdJwts: { | ||
getPageviewsAnalytics: TEST_JWT_TOKEN, | ||
}, | ||
} as CurrentUser); | ||
}); | ||
|
||
expect(result.current.hook).toBeUndefined(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/twenty-front/src/modules/analytics/hooks/useAnalyticsGraphDataState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { webhookAnalyticsGraphDataState } from '@/analytics/states/webhookAnalyticsGraphDataState'; | ||
|
||
import { mapWebhookAnalyticsResultToNivoLineInput } from '@/analytics/utils/mapWebhookAnalyticsResultToNivoLineInput'; | ||
|
||
export const useAnalyticsGraphDataState = (endpointName: string) => { | ||
switch (endpointName) { | ||
case 'getWebhookAnalytics': | ||
return { | ||
analyticsState: webhookAnalyticsGraphDataState, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like what you want to use here might be componentState? Look for |
||
transformDataFunction: mapWebhookAnalyticsResultToNivoLineInput, | ||
}; | ||
default: | ||
throw new Error( | ||
`No analytics state associated with endpoint "${endpointName}"`, | ||
); | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/twenty-front/src/modules/analytics/hooks/useAnalyticsTinybirdJwts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
import { currentUserState } from '@/auth/states/currentUserState'; | ||
import { AnalyticsTinybirdJwtMap } from '~/generated-metadata/graphql'; | ||
|
||
export const useAnalyticsTinybirdJwts = ( | ||
jwtName: keyof AnalyticsTinybirdJwtMap, | ||
): string | undefined => { | ||
const currentUser = useRecoilValue(currentUserState); | ||
|
||
if (!currentUser) { | ||
return undefined; | ||
} | ||
|
||
return currentUser.analyticsTinybirdJwts?.[jwtName]; | ||
}; |
49 changes: 49 additions & 0 deletions
49
packages/twenty-front/src/modules/analytics/hooks/useGraphData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useAnalyticsTinybirdJwts } from '@/analytics/hooks/useAnalyticsTinybirdJwts'; | ||
import { fetchGraphDataOrThrow } from '@/analytics/utils/fetchGraphDataOrThrow'; | ||
|
||
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar'; | ||
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar'; | ||
import { isUndefined } from '@sniptt/guards'; | ||
import { AnalyticsTinybirdJwtMap } from '~/generated-metadata/graphql'; | ||
|
||
export const useGraphData = ({ | ||
recordType, | ||
recordId, | ||
endpointName, | ||
}: { | ||
recordType: string; | ||
recordId: string; | ||
endpointName: keyof AnalyticsTinybirdJwtMap; | ||
}) => { | ||
const { enqueueSnackBar } = useSnackBar(); | ||
const tinybirdJwt = useAnalyticsTinybirdJwts(endpointName); | ||
|
||
const fetchGraphData = async ( | ||
windowLengthGraphOption: '7D' | '1D' | '12H' | '4H', | ||
) => { | ||
try { | ||
if (isUndefined(tinybirdJwt)) { | ||
throw new Error('No jwt associated with this endpoint found'); | ||
} | ||
|
||
return await fetchGraphDataOrThrow({ | ||
recordId, | ||
recordType, | ||
windowLength: windowLengthGraphOption, | ||
tinybirdJwt, | ||
endpointName, | ||
}); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
enqueueSnackBar( | ||
`Something went wrong while fetching webhook usage: ${error.message}`, | ||
{ | ||
variant: SnackBarVariant.Error, | ||
}, | ||
); | ||
} | ||
return []; | ||
} | ||
}; | ||
return { fetchGraphData }; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? Or can we remove it before merging?