-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [OPIK-340]: project metrics initial commit; * [OPIK-340]: finish project metrics; * [OPIK-340]: project metrics initial commit; * [OPIK-340]: finish project metrics; * [OPIK-340]: replace type with interface; * [OPIK-340]: remove metrics from TRACE_DATA_TYPE; * [OPIK-340]: improve code readability; * [OPIK-340]: make a generic ChartTooltipContent component; --------- Co-authored-by: Sasha <[email protected]>
- Loading branch information
Showing
17 changed files
with
764 additions
and
133 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
52 changes: 52 additions & 0 deletions
52
apps/opik-frontend/src/api/feedback/useRequestChartMutation.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,52 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import get from "lodash/get"; | ||
|
||
import axios, { AxiosError } from "axios"; | ||
|
||
import { useToast } from "@/components/ui/use-toast"; | ||
import { APP_VERSION } from "@/constants/app"; | ||
import { STATS_ANONYMOUS_ID, STATS_COMET_ENDPOINT } from "@/api/api"; | ||
|
||
type UseRequestChartMutationParams = { | ||
feedback: string; | ||
}; | ||
|
||
const EVENT_TYPE = "opik_request_chart_fe"; | ||
|
||
const useRequestChartMutation = () => { | ||
const { toast } = useToast(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ feedback }: UseRequestChartMutationParams) => { | ||
return axios.post(STATS_COMET_ENDPOINT, { | ||
anonymous_id: STATS_ANONYMOUS_ID, | ||
event_type: EVENT_TYPE, | ||
event_properties: { | ||
feedback, | ||
version: APP_VERSION || null, | ||
}, | ||
}); | ||
}, | ||
onSuccess: () => { | ||
toast({ | ||
title: "Feedback sent", | ||
description: "Thank you for sharing your thoughts with us", | ||
}); | ||
}, | ||
onError: (error: AxiosError) => { | ||
const message = get( | ||
error, | ||
["response", "data", "message"], | ||
error.message, | ||
); | ||
|
||
toast({ | ||
title: "Error", | ||
description: message, | ||
variant: "destructive", | ||
}); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useRequestChartMutation; |
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,67 @@ | ||
import { QueryFunctionContext, useQuery } from "@tanstack/react-query"; | ||
import api, { PROJECTS_REST_ENDPOINT, QueryConfig } from "@/api/api"; | ||
import { ProjectMetricTrace } from "@/types/projects"; | ||
|
||
export enum METRIC_NAME_TYPE { | ||
FEEDBACK_SCORES = "FEEDBACK_SCORES", | ||
TRACE_COUNT = "TRACE_COUNT", | ||
DURATION = "DURATION", | ||
TOKEN_USAGE = "TOKEN_USAGE", | ||
} | ||
|
||
export enum INTERVAL_TYPE { | ||
HOURLY = "HOURLY", | ||
DAILY = "DAILY", | ||
WEEKLY = "WEEKLY", | ||
} | ||
|
||
type UseProjectMetricsParams = { | ||
projectId: string; | ||
metricName: METRIC_NAME_TYPE; | ||
interval: INTERVAL_TYPE; | ||
intervalStart: string; | ||
intervalEnd: string; | ||
}; | ||
|
||
interface ProjectMetricsResponse { | ||
results: ProjectMetricTrace[]; | ||
} | ||
|
||
const getProjectMetric = async ( | ||
{ signal }: QueryFunctionContext, | ||
{ | ||
projectId, | ||
metricName, | ||
interval, | ||
intervalStart, | ||
intervalEnd, | ||
}: UseProjectMetricsParams, | ||
) => { | ||
const { data } = await api.post<ProjectMetricsResponse>( | ||
`${PROJECTS_REST_ENDPOINT}${projectId}/metrics`, | ||
{ | ||
metric_type: metricName, | ||
interval, | ||
interval_start: intervalStart, | ||
interval_end: intervalEnd, | ||
}, | ||
{ | ||
signal, | ||
}, | ||
); | ||
|
||
return data?.results; | ||
}; | ||
|
||
const useProjectMetric = ( | ||
params: UseProjectMetricsParams, | ||
config?: QueryConfig<ProjectMetricTrace[]>, | ||
) => { | ||
return useQuery({ | ||
queryKey: ["projectMetrics", params], | ||
queryFn: (context) => getProjectMetric(context, params), | ||
...config, | ||
}); | ||
}; | ||
|
||
export default useProjectMetric; |
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.