feat: use SSE for dataset import progress
This commit is contained in:
parent
38f9b93076
commit
d35a88ed66
1 changed files with 40 additions and 20 deletions
|
|
@ -9,6 +9,8 @@ import {
|
||||||
type UploadTask,
|
type UploadTask,
|
||||||
} from '@/services/uploadQueue'
|
} from '@/services/uploadQueue'
|
||||||
import { api } from '@/services/api'
|
import { api } from '@/services/api'
|
||||||
|
import { API_BASE_URL, STORAGE_KEYS } from '@/app/constants'
|
||||||
|
import { getStorageItem } from '@/utils/storage'
|
||||||
import {
|
import {
|
||||||
importCocoFile,
|
importCocoFile,
|
||||||
importYoloFiles,
|
importYoloFiles,
|
||||||
|
|
@ -155,29 +157,47 @@ export const DataTab: React.FC<DataTabProps> = ({
|
||||||
const [serverImport, setServerImport] = useState<string | null>(null)
|
const [serverImport, setServerImport] = useState<string | null>(null)
|
||||||
|
|
||||||
const pollServerImport = (jobId: string) => {
|
const pollServerImport = (jobId: string) => {
|
||||||
// Poll the server job until done/error; cheap (one tiny request per 10s).
|
void (async () => {
|
||||||
const timer = window.setInterval(async () => {
|
const token = getStorageItem<string | null>(STORAGE_KEYS.ACCESS_TOKEN, null)
|
||||||
|
const response = await fetch(`${API_BASE_URL}/projects/${project.id}/imports/${jobId}/events`, {
|
||||||
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||||
|
})
|
||||||
|
if (!response.ok || !response.body) throw new Error(`Không kết nối được SSE (${response.status})`)
|
||||||
|
const reader = response.body.getReader()
|
||||||
|
const decoder = new TextDecoder()
|
||||||
|
let buffer = ''
|
||||||
try {
|
try {
|
||||||
const { data } = await api.get<{ status: string; done_files: number; total_files: number; error?: string }>(
|
for (;;) {
|
||||||
`/projects/${project.id}/imports/${jobId}`
|
const chunk = await reader.read()
|
||||||
)
|
if (chunk.done) break
|
||||||
|
buffer += decoder.decode(chunk.value, { stream: true })
|
||||||
|
const events = buffer.split('\n\n')
|
||||||
|
buffer = events.pop() ?? ''
|
||||||
|
for (const event of events) {
|
||||||
|
const line = event.split('\n').find((value) => value.startsWith('data:'))
|
||||||
|
if (!line) continue
|
||||||
|
const data = JSON.parse(line.slice(5)) as { status: string; done_files: number; total_files: number; error?: string }
|
||||||
if (data.status === 'done') {
|
if (data.status === 'done') {
|
||||||
window.clearInterval(timer)
|
|
||||||
setServerImport(null)
|
setServerImport(null)
|
||||||
toast.success(`Nhập xong ${data.done_files} ảnh từ zip`)
|
toast.success(`Nhập xong ${data.done_files} ảnh từ zip`)
|
||||||
onUploadComplete()
|
onUploadComplete()
|
||||||
} else if (data.status === 'error') {
|
return
|
||||||
window.clearInterval(timer)
|
}
|
||||||
|
if (data.status === 'error') {
|
||||||
setServerImport(null)
|
setServerImport(null)
|
||||||
toast.error(`Nhập zip thất bại: ${data.error ?? 'lỗi server'}`)
|
toast.error(`Nhập zip thất bại: ${data.error ?? 'lỗi server'}`)
|
||||||
} else {
|
return
|
||||||
|
}
|
||||||
setServerImport(`Server đang nhập ${data.done_files}/${data.total_files} ảnh…`)
|
setServerImport(`Server đang nhập ${data.done_files}/${data.total_files} ảnh…`)
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
window.clearInterval(timer)
|
|
||||||
setServerImport(null)
|
|
||||||
}
|
}
|
||||||
}, 10000)
|
} finally {
|
||||||
|
await reader.cancel()
|
||||||
|
}
|
||||||
|
})().catch((error) => {
|
||||||
|
setServerImport(null)
|
||||||
|
toast.error(error instanceof Error ? error.message : 'Không theo dõi được job import')
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleZipSelected = async (files: FileList | null) => {
|
const handleZipSelected = async (files: FileList | null) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue