Skip to content

Commit dee30c7

Browse files
authored
Merge pull request #1158 from c-bata/use-latest-trials-hook
Add `useLatestStudyDetail` hook
2 parents f7e23cb + 8ce29b0 commit dee30c7

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

optuna_dashboard/ts/components/StudyDetail.tsx

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ import {
99
useTheme,
1010
} from "@mui/material"
1111
import Grid from "@mui/material/Grid"
12-
import { useAtomValue } from "jotai"
13-
import React, { FC, useEffect, useMemo } from "react"
12+
import React, { FC, useMemo } from "react"
1413
import { Link, useParams } from "react-router-dom"
1514

16-
import { actionCreator } from "../action"
1715
import { useConstants } from "../constantsProvider"
18-
import {
19-
reloadIntervalState,
20-
useStudyDetailValue,
21-
useStudyIsPreferential,
22-
useStudyName,
23-
} from "../state"
16+
import { useLatestStudyDetail } from "../hooks/useLatestStudyDetail"
17+
import { useStudyIsPreferential, useStudyName } from "../state"
2418
import { AppDrawer, PageId } from "./AppDrawer"
2519
import { Contour } from "./GraphContour"
2620
import { GraphEdf } from "./GraphEdf"
@@ -54,45 +48,21 @@ export const StudyDetail: FC<{
5448
const { url_prefix } = useConstants()
5549

5650
const theme = useTheme()
57-
const action = actionCreator()
5851
const studyId = useURLVars()
59-
const studyDetail = useStudyDetailValue(studyId)
60-
const reloadInterval = useAtomValue(reloadIntervalState)
6152
const studyName = useStudyName(studyId)
6253
const isPreferential = useStudyIsPreferential(studyId)
6354

64-
const title =
65-
studyName !== null ? `${studyName} (id=${studyId})` : `Study #${studyId}`
66-
67-
useEffect(() => {
68-
action.updateStudyDetail(studyId)
69-
}, [])
70-
71-
useEffect(() => {
72-
if (reloadInterval < 0) {
73-
return
74-
}
75-
const nTrials = studyDetail ? studyDetail.trials.length : 0
76-
let interval = reloadInterval * 1000
77-
55+
const studyDetail = useLatestStudyDetail({
56+
studyId: studyId,
7857
// For Human-in-the-loop Optimization, the interval is set to 2 seconds
7958
// when the number of trials is small, and the page is "trialList" or top page of preferential.
80-
if (
59+
shortInterval:
8160
(!isPreferential && page === "trialList") ||
82-
(isPreferential && page === "top")
83-
) {
84-
if (nTrials < 100) {
85-
interval = 2000
86-
} else if (nTrials < 500) {
87-
interval = 5000
88-
}
89-
}
61+
(!!isPreferential && page === "top"),
62+
})
9063

91-
const intervalId = setInterval(() => {
92-
action.updateStudyDetail(studyId)
93-
}, interval)
94-
return () => clearInterval(intervalId)
95-
}, [reloadInterval, studyDetail, page])
64+
const title =
65+
studyName !== null ? `${studyName} (id=${studyId})` : `Study #${studyId}`
9666

9767
let content = null
9868
if (page === "top") {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useAtomValue } from "jotai"
2+
import { useEffect } from "react"
3+
import { actionCreator } from "../action"
4+
import { reloadIntervalState, useStudyDetailValue } from "../state"
5+
import { StudyDetail } from "../types/optuna"
6+
7+
export const useLatestStudyDetail = ({
8+
studyId,
9+
shortInterval,
10+
}: {
11+
studyId: number
12+
shortInterval: boolean
13+
}): StudyDetail | null => {
14+
const action = actionCreator()
15+
const reloadInterval = useAtomValue(reloadIntervalState)
16+
const studyDetail = useStudyDetailValue(studyId)
17+
18+
useEffect(() => {
19+
action.updateStudyDetail(studyId)
20+
}, [])
21+
22+
useEffect(() => {
23+
if (reloadInterval < 0) {
24+
return
25+
}
26+
const nTrials = studyDetail ? studyDetail.trials.length : 0
27+
let interval = reloadInterval * 1000
28+
29+
if (shortInterval) {
30+
if (nTrials < 100) {
31+
interval = 2000
32+
} else if (nTrials < 500) {
33+
interval = 5000
34+
}
35+
}
36+
37+
const intervalId = setInterval(() => {
38+
action.updateStudyDetail(studyId)
39+
}, interval)
40+
return () => clearInterval(intervalId)
41+
}, [reloadInterval, studyDetail])
42+
43+
return studyDetail
44+
}

0 commit comments

Comments
 (0)