-
Notifications
You must be signed in to change notification settings - Fork 60
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
Realtime evaluation result table update #248
Merged
andresgutgon
merged 1 commit into
main
from
feature/realtime-evaluation-result-table-update
Sep 25, 2024
Merged
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions
51
...itUuid]/documents/[documentUuid]/evaluations/[evaluationId]/_components/Content/index.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,51 @@ | ||
'use client' | ||
|
||
import { | ||
Commit, | ||
EvaluationAggregationTotals, | ||
EvaluationDto, | ||
EvaluationMeanValue, | ||
EvaluationModalValue, | ||
} from '@latitude-data/core/browser' | ||
import { EvaluationResultWithMetadata } from '@latitude-data/core/repositories' | ||
|
||
import { EvaluationResults } from '../EvaluationResults' | ||
import { MetricsSummary } from '../MetricsSummary' | ||
import { useEvaluationStatus } from './useEvaluationStatus' | ||
|
||
export default function Content<T extends boolean>({ | ||
commit, | ||
evaluation, | ||
evaluationResults, | ||
documentUuid, | ||
aggregationTotals, | ||
isNumeric, | ||
meanOrModal, | ||
}: { | ||
commit: Commit | ||
evaluation: EvaluationDto | ||
documentUuid: string | ||
evaluationResults: EvaluationResultWithMetadata[] | ||
aggregationTotals: EvaluationAggregationTotals | ||
isNumeric: T | ||
meanOrModal: T extends true ? EvaluationMeanValue : EvaluationModalValue | ||
}) { | ||
const { jobs } = useEvaluationStatus({ evaluation }) | ||
return ( | ||
<> | ||
<MetricsSummary | ||
commit={commit} | ||
evaluation={evaluation} | ||
documentUuid={documentUuid} | ||
aggregationTotals={aggregationTotals} | ||
isNumeric={isNumeric} | ||
meanOrModal={meanOrModal} | ||
/> | ||
<EvaluationResults | ||
evaluation={evaluation} | ||
evaluationResults={evaluationResults} | ||
jobs={jobs} | ||
/> | ||
</> | ||
) | ||
} |
72 changes: 72 additions & 0 deletions
72
...ents/[documentUuid]/evaluations/[evaluationId]/_components/Content/useEvaluationStatus.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,72 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
|
||
import { Evaluation } from '@latitude-data/core/browser' | ||
import { useCurrentDocument } from '@latitude-data/web-ui' | ||
import { | ||
useSockets, | ||
type EventArgs, | ||
} from '$/components/Providers/WebsocketsProvider/useSockets' | ||
|
||
import { isEvaluationRunDone } from '../../_lib/isEvaluationRunDone' | ||
import { useRefetchStats } from './useRefetchStats' | ||
|
||
const DISAPERING_IN_MS = 5000 | ||
|
||
export function useEvaluationStatus({ | ||
evaluation, | ||
}: { | ||
evaluation: Evaluation | ||
}) { | ||
const timeoutRef = useRef<number | null>(null) | ||
const [jobs, setJobs] = useState<EventArgs<'evaluationStatus'>[]>([]) | ||
const document = useCurrentDocument() | ||
const { refetchStats } = useRefetchStats({ evaluation, document }) | ||
const onMessage = useCallback( | ||
(args: EventArgs<'evaluationStatus'>) => { | ||
if (evaluation.id !== args.evaluationId) return | ||
if (document.documentUuid !== args.documentUuid) return | ||
|
||
const done = isEvaluationRunDone(args) | ||
|
||
if (done) { | ||
refetchStats() | ||
} | ||
|
||
setJobs((prevJobs) => { | ||
const jobIndex = prevJobs.findIndex( | ||
(job) => job.batchId === args.batchId, | ||
) | ||
|
||
if (jobIndex === -1) { | ||
return [...prevJobs, args] | ||
} else { | ||
const newJobs = [...prevJobs] | ||
newJobs[jobIndex] = args | ||
|
||
if (done) { | ||
setTimeout(() => { | ||
setJobs((currentJobs) => | ||
currentJobs.filter((job) => job.batchId !== args.batchId), | ||
) | ||
}, DISAPERING_IN_MS) | ||
} | ||
|
||
return newJobs | ||
} | ||
}) | ||
}, | ||
[evaluation.id, document.documentUuid, refetchStats], | ||
) | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
} | ||
} | ||
}, []) | ||
|
||
useSockets({ event: 'evaluationStatus', onMessage }) | ||
|
||
return { jobs } | ||
} |
79 changes: 79 additions & 0 deletions
79
...ocuments/[documentUuid]/evaluations/[evaluationId]/_components/Content/useRefetchStats.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,79 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { | ||
DocumentVersion, | ||
Evaluation, | ||
EvaluationResultableType, | ||
} from '@latitude-data/core/browser' | ||
import { useCurrentCommit } from '@latitude-data/web-ui' | ||
import useEvaluationResultsCounters from '$/stores/evaluationResultCharts/evaluationResultsCounters' | ||
import useEvaluationResultsMeanValue from '$/stores/evaluationResultCharts/evaluationResultsMeanValue' | ||
import useEvaluationResultsModalValue from '$/stores/evaluationResultCharts/evaluationResultsModalValue' | ||
import useAverageResultsAndCostOverCommit from '$/stores/evaluationResultCharts/numericalResults/averageResultAndCostOverCommitStore' | ||
import useAverageResultOverTime from '$/stores/evaluationResultCharts/numericalResults/averageResultOverTimeStore' | ||
|
||
export function useRefetchStats({ | ||
evaluation, | ||
document, | ||
}: { | ||
evaluation: Evaluation | ||
document: DocumentVersion | ||
}) { | ||
const { commit } = useCurrentCommit() | ||
const evaluationId = evaluation.id | ||
const commitUuid = commit.uuid | ||
const documentUuid = document.documentUuid | ||
const isNumeric = | ||
evaluation.configuration.type === EvaluationResultableType.Number | ||
|
||
const { refetch: refetchAverageResulstAndCostsOverCommit } = | ||
useAverageResultsAndCostOverCommit({ | ||
evaluation, | ||
documentUuid, | ||
}) | ||
const { refetch: refetchAverageResultOverTime } = useAverageResultOverTime({ | ||
evaluation, | ||
documentUuid, | ||
}) | ||
const { refetch: refetchMean } = useEvaluationResultsMeanValue({ | ||
commitUuid, | ||
documentUuid, | ||
evaluationId, | ||
}) | ||
const { refetch: refetchModal } = useEvaluationResultsModalValue({ | ||
commitUuid, | ||
documentUuid, | ||
evaluationId, | ||
}) | ||
const { refetch: refetchTotals } = useEvaluationResultsCounters({ | ||
commitUuid, | ||
documentUuid, | ||
evaluationId, | ||
}) | ||
|
||
const refetchStats = useCallback(() => { | ||
console.log('refetchStats') | ||
|
||
Promise.all([ | ||
refetchTotals(), | ||
...(isNumeric | ||
? [ | ||
refetchMean(), | ||
refetchAverageResulstAndCostsOverCommit(), | ||
refetchAverageResultOverTime(), | ||
] | ||
: [refetchModal()]), | ||
]) | ||
}, [ | ||
isNumeric, | ||
refetchMean, | ||
refetchModal, | ||
refetchTotals, | ||
refetchAverageResulstAndCostsOverCommit, | ||
refetchAverageResultOverTime, | ||
]) | ||
|
||
return { | ||
refetchStats, | ||
} | ||
} |
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.
is this refetch actually the mutate function? if so i wouldn't rename it it's better to expose the real name so devs understand what they can do with this
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 knew this will come out. I don't agree in this case to be honest. I think fetch express better the intention here
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.
If this function just triggers a new request to update the data, I agree with @andresgutgon here ☝️
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.
but it doesn't just do that, you can also pass it data to mutate the store
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.
In general yes, but in this use case is a refetch. We're talking semantics here