-
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
Add webhook response graph from the last 5 days #7487
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb9fde7
add webhook response graph and send webhook events to tinybird
anamarn 21fe7b3
merge with main
anamarn c59ccad
correct clean code
anamarn a656724
put feature flag to false in seeds
anamarn 437226f
correct feat flag
anamarn d39e75a
Merge branch 'main' into feat/webhooks-graphs
anamarn 050d5ff
correct CI errors
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
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
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
59 changes: 59 additions & 0 deletions
59
...rc/modules/settings/developers/webhook/components/SettingsDevelopersWebhookUsageGraph.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,59 @@ | ||
import { webhookGraphDataState } from '@/settings/developers/webhook/states/webhookGraphDataState'; | ||
import styled from '@emotion/styled'; | ||
import { ResponsiveLine } from '@nivo/line'; | ||
import { Section } from '@react-email/components'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { H2Title } from 'twenty-ui'; | ||
|
||
export type NivoLineInput = { | ||
id: string | number; | ||
color?: string; | ||
data: Array<{ | ||
x: number | string | Date; | ||
y: number | string | Date; | ||
}>; | ||
}; | ||
const StyledGraphContainer = styled.div` | ||
height: 200px; | ||
width: 100%; | ||
`; | ||
export const SettingsDeveloppersWebhookUsageGraph = () => { | ||
const webhookGraphData = useRecoilValue(webhookGraphDataState); | ||
|
||
return ( | ||
<> | ||
{webhookGraphData.length ? ( | ||
<Section> | ||
<H2Title title="Statistics" /> | ||
<StyledGraphContainer> | ||
<ResponsiveLine | ||
data={webhookGraphData} | ||
colors={(d) => d.color} | ||
margin={{ top: 0, right: 0, bottom: 50, left: 60 }} | ||
xFormat="time:%Y-%m-%d %H:%M%" | ||
xScale={{ | ||
type: 'time', | ||
useUTC: false, | ||
format: '%Y-%m-%d %H:%M:%S', | ||
precision: 'hour', | ||
}} | ||
yScale={{ | ||
type: 'linear', | ||
}} | ||
axisBottom={{ | ||
tickValues: 'every day', | ||
format: '%b %d', | ||
}} | ||
enableTouchCrosshair={true} | ||
enableGridY={false} | ||
enableGridX={false} | ||
enablePoints={false} | ||
/> | ||
</StyledGraphContainer> | ||
</Section> | ||
) : ( | ||
<></> | ||
)} | ||
</> | ||
); | ||
}; |
101 changes: 101 additions & 0 deletions
101
...ules/settings/developers/webhook/components/SettingsDevelopersWebhookUsageGraphEffect.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,101 @@ | ||
import { NivoLineInput } from '@/settings/developers/webhook/components/SettingsDevelopersWebhookUsageGraph'; | ||
import { webhookGraphDataState } from '@/settings/developers/webhook/states/webhookGraphDataState'; | ||
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar'; | ||
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar'; | ||
import { useEffect } from 'react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
|
||
type SettingsDevelopersWebhookUsageGraphEffectProps = { | ||
webhookId: string; | ||
}; | ||
|
||
export const SettingsDevelopersWebhookUsageGraphEffect = ({ | ||
webhookId, | ||
}: SettingsDevelopersWebhookUsageGraphEffectProps) => { | ||
const setWebhookGraphData = useSetRecoilState(webhookGraphDataState); | ||
|
||
const { enqueueSnackBar } = useSnackBar(); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const queryString = new URLSearchParams({ | ||
webhookIdRequest: webhookId, | ||
}).toString(); | ||
const token = 'REPLACE_ME'; | ||
|
||
const response = await fetch( | ||
`https://api.eu-central-1.aws.tinybird.co/v0/pipes/getWebhooksAnalytics.json?${queryString}`, | ||
{ | ||
headers: { | ||
Authorization: 'Bearer ' + token, | ||
}, | ||
}, | ||
); | ||
const result = await response.json(); | ||
|
||
if (!response.ok) { | ||
enqueueSnackBar('Something went wrong while fetching webhook usage', { | ||
variant: SnackBarVariant.Error, | ||
}); | ||
return; | ||
} | ||
|
||
const graphInput = result.data | ||
.flatMap( | ||
(dataRow: { | ||
start_interval: string; | ||
failure_count: number; | ||
success_count: number; | ||
}) => [ | ||
{ | ||
x: dataRow.start_interval, | ||
y: dataRow.failure_count, | ||
id: 'failure_count', | ||
color: 'red', | ||
}, | ||
{ | ||
x: dataRow.start_interval, | ||
y: dataRow.success_count, | ||
id: 'success_count', | ||
color: 'green', | ||
}, | ||
], | ||
) | ||
.reduce( | ||
( | ||
acc: NivoLineInput[], | ||
{ | ||
id, | ||
x, | ||
y, | ||
color, | ||
}: { id: string; x: string; y: number; color: string }, | ||
) => { | ||
const existingGroupIndex = acc.findIndex( | ||
(group) => group.id === id, | ||
); | ||
const isExistingGroup = existingGroupIndex !== -1; | ||
|
||
if (isExistingGroup) { | ||
return acc.map((group, index) => | ||
index === existingGroupIndex | ||
? { ...group, data: [...group.data, { x, y }] } | ||
: group, | ||
); | ||
} else { | ||
return [...acc, { id, color, data: [{ x, y }] }]; | ||
} | ||
}, | ||
[], | ||
); | ||
setWebhookGraphData(graphInput); | ||
} catch (error) { | ||
enqueueSnackBar('Something went wrong while fetching webhook usage', { | ||
variant: SnackBarVariant.Error, | ||
}); | ||
} | ||
}; | ||
fetchData(); | ||
}, [enqueueSnackBar, setWebhookGraphData, webhookId]); | ||
return <></>; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...ages/twenty-front/src/modules/settings/developers/webhook/states/webhookGraphDataState.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 @@ | ||
import { createState } from 'twenty-ui'; | ||
import { NivoLineInput } from '~/pages/settings/developers/webhooks/components/SettingsDevelopersWebhookUsageGraph'; | ||
|
||
export const webhookGraphDataState = createState<NivoLineInput[]>({ | ||
key: 'webhookGraphData', | ||
defaultValue: [], | ||
}); |
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
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
2 changes: 1 addition & 1 deletion
2
.../pages/settings/developers/__stories__/webhooks/SettingsDevelopersWebhooksNew.stories.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
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
File renamed without changes.
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Logger } from '@nestjs/common'; | ||
|
||
import { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service'; | ||
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator'; | ||
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator'; | ||
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants'; | ||
|
@@ -18,17 +19,41 @@ export type CallWebhookJobData = { | |
@Processor(MessageQueue.webhookQueue) | ||
export class CallWebhookJob { | ||
private readonly logger = new Logger(CallWebhookJob.name); | ||
|
||
constructor(private readonly httpService: HttpService) {} | ||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly analyticsService: AnalyticsService, | ||
) {} | ||
|
||
@Process(CallWebhookJob.name) | ||
async handle(data: CallWebhookJobData): Promise<void> { | ||
try { | ||
await this.httpService.axiosRef.post(data.targetUrl, data); | ||
this.logger.log( | ||
`CallWebhookJob successfully called on targetUrl '${data.targetUrl}'`, | ||
const response = await this.httpService.axiosRef.post( | ||
data.targetUrl, | ||
data, | ||
); | ||
const eventInput = { | ||
action: 'webhook.response', | ||
payload: { | ||
status: response.status, | ||
url: data.targetUrl, | ||
webhookId: data.webhookId, | ||
eventName: data.eventName, | ||
}, | ||
}; | ||
|
||
this.analyticsService.create(eventInput, 'webhook', data.workspaceId); | ||
} catch (err) { | ||
const eventInput = { | ||
action: 'webhook.response', | ||
payload: { | ||
status: err.response.status, | ||
url: data.targetUrl, | ||
webhookId: data.webhookId, | ||
eventName: data.eventName, | ||
}, | ||
}; | ||
Comment on lines
45
to
+54
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. style: Consider wrapping err.response.status in a try-catch block, as err.response might be undefined for network errors |
||
|
||
this.analyticsService.create(eventInput, 'webhook', data.workspaceId); | ||
this.logger.error( | ||
`Error calling webhook on targetUrl '${data.targetUrl}': ${err}`, | ||
); | ||
|
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
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
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.
I see a usage in ActivityLog.tsx