Skip to content

Commit

Permalink
erge branch 'feat/batch-run-support-re-run' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoel committed Sep 22, 2023
2 parents e001b2d + 93666bd commit 7c3ced3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
22 changes: 19 additions & 3 deletions web/app/components/share/text-generation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,21 @@ const TextGeneration: FC<IMainProps> = ({
const handleRetryAllFailedTask = () => {
setControlRetry(Date.now())
}
const [allTaskList, setAllTaskList, getLatestTaskList] = useGetState<Task[]>([])
const [allTaskList, doSetAllTaskList] = useState<Task[]>([])
const allTaskListRef = useRef<Task[]>([])
const getLatestTaskList = () => allTaskListRef.current
const setAllTaskList = (taskList: Task[]) => {
doSetAllTaskList(taskList)
allTaskListRef.current = taskList
}
const pendingTaskList = allTaskList.filter(task => task.status === TaskStatus.pending)
const noPendingTask = pendingTaskList.length === 0
const showTaskList = allTaskList.filter(task => task.status !== TaskStatus.pending)
const [currGroupNum, setCurrGroupNum, getCurrGroupNum] = useGetState(0)
const allSuccessTaskList = allTaskList.filter(task => task.status === TaskStatus.completed)
const allFailedTaskList = allTaskList.filter(task => task.status === TaskStatus.failed)
const allTaskFinished = allTaskList.every(task => task.status === TaskStatus.completed)
const allTaskRuned = allTaskList.every(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status))
const [batchCompletionRes, setBatchCompletionRes, getBatchCompletionRes] = useGetState<Record<string, string>>({})
const exportRes = allTaskList.map((task) => {
const batchCompletionResLatest = getBatchCompletionRes()
Expand Down Expand Up @@ -256,8 +264,15 @@ const TextGeneration: FC<IMainProps> = ({
const allTasklistLatest = getLatestTaskList()
const batchCompletionResLatest = getBatchCompletionRes()
const pendingTaskList = allTasklistLatest.filter(task => task.status === TaskStatus.pending)
const hadRunedTaskNum = allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length
const needToAddNextGroupTask = hadRunedTaskNum % GROUP_SIZE === 0 && pendingTaskList.length > 0
const hadRunedTaskNum = 1 + allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length
const needToAddNextGroupTask = (getCurrGroupNum() !== hadRunedTaskNum) && pendingTaskList.length > 0 && (hadRunedTaskNum % GROUP_SIZE === 0 || (allTasklistLatest.length - hadRunedTaskNum < GROUP_SIZE))
// avoid add many task at the same time
if (needToAddNextGroupTask)
setCurrGroupNum(hadRunedTaskNum)
// console.group()
// console.log(`[#${taskId}]: ${isSuccess ? 'success' : 'fail'}.currGroupNum: ${getCurrGroupNum()}.hadRunedTaskNum: ${hadRunedTaskNum}, needToAddNextGroupTask: ${needToAddNextGroupTask}`)
// console.log([...allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).map(item => item.id), taskId].sort((a: any, b: any) => a - b).join(','))
// console.groupEnd()
const nextPendingTaskIds = needToAddNextGroupTask ? pendingTaskList.slice(0, GROUP_SIZE).map(item => item.id) : []
const newAllTaskList = allTasklistLatest.map((item) => {
if (item.id === taskId) {
Expand Down Expand Up @@ -488,6 +503,7 @@ const TextGeneration: FC<IMainProps> = ({
<RunBatch
vars={promptConfig.prompt_variables}
onSend={handleRunBatch}
isAllFinished={allTaskRuned}
/>
</div>

Expand Down
19 changes: 19 additions & 0 deletions web/app/components/share/text-generation/result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,39 @@ const Result: FC<IResultProps> = ({
onShowRes()

setResponsingTrue()
const startTime = Date.now()
let isTimeout = false
const runId = setInterval(() => {
if (Date.now() - startTime > 1000 * 60) { // 1min timeout
clearInterval(runId)
setResponsingFalse()
onCompleted(getCompletionRes(), taskId, false)
isTimeout = true
console.log(`[#${taskId}]: timeout`)
}
}, 1000)
sendCompletionMessage(data, {
onData: (data: string, _isFirstMessage: boolean, { messageId }) => {
tempMessageId = messageId
res.push(data)
setCompletionRes(res.join(''))
},
onCompleted: () => {
if (isTimeout)
return

setResponsingFalse()
setMessageId(tempMessageId)
onCompleted(getCompletionRes(), taskId, true)
clearInterval(runId)
},
onError() {
if (isTimeout)
return

setResponsingFalse()
onCompleted(getCompletionRes(), taskId, false)
clearInterval(runId)
},
}, isInstalledApp, installedAppInfo?.id)
}
Expand Down
10 changes: 7 additions & 3 deletions web/app/components/share/text-generation/run-batch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import {
PlayIcon,
} from '@heroicons/react/24/solid'
import { useTranslation } from 'react-i18next'
import cn from 'classnames'
import CSVReader from './csv-reader'
import CSVDownload from './csv-download'
import Button from '@/app/components/base/button'

import { Loading02 } from '@/app/components/base/icons/src/vender/line/general'
export type IRunBatchProps = {
vars: { name: string }[]
onSend: (data: string[][]) => void
isAllFinished: boolean
}

const RunBatch: FC<IRunBatchProps> = ({
vars,
onSend,
isAllFinished,
}) => {
const { t } = useTranslation()

Expand All @@ -31,6 +34,7 @@ const RunBatch: FC<IRunBatchProps> = ({
const handleSend = () => {
onSend(csvData)
}
const Icon = isAllFinished ? PlayIcon : Loading02
return (
<div className='pt-4'>
<CSVReader onParsed={handleParsed} />
Expand All @@ -41,9 +45,9 @@ const RunBatch: FC<IRunBatchProps> = ({
type="primary"
className='mt-4 !h-8 !pl-3 !pr-4'
onClick={handleSend}
disabled={!isParsed}
disabled={!isParsed || !isAllFinished}
>
<PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
<Icon className={cn(!isAllFinished && 'animate-spin', 'shrink-0 w-4 h-4 mr-1')} aria-hidden="true" />
<span className='uppercase text-[13px]'>{t('share.generation.run')}</span>
</Button>
</div>
Expand Down

0 comments on commit 7c3ced3

Please sign in to comment.