code refactoring

This commit is contained in:
2024-08-20 20:29:32 +02:00
parent c51f320a6f
commit 8f2d9eaf6e
6 changed files with 20 additions and 17 deletions

View File

@@ -102,8 +102,15 @@ export type CustomTemplate = {
content: string content: string
} }
export enum LiveStreamStatus {
WAITING,
IN_PROGRESS,
COMPLETED,
ERRORED
}
export type LiveStreamProgress = Record<string, { export type LiveStreamProgress = Record<string, {
Status: number Status: LiveStreamStatus
WaitTime: string WaitTime: string
LiveDate: string LiveDate: string
}> }>

View File

@@ -13,7 +13,7 @@ import LivestreamSpeedDial from '../components/livestream/LivestreamSpeedDial'
import NoLivestreams from '../components/livestream/NoLivestreams' import NoLivestreams from '../components/livestream/NoLivestreams'
import { useSubscription } from '../hooks/observable' import { useSubscription } from '../hooks/observable'
import { useRPC } from '../hooks/useRPC' import { useRPC } from '../hooks/useRPC'
import { LiveStreamProgress } from '../types' import { LiveStreamProgress, LiveStreamStatus } from '../types'
const LiveStreamMonitorView: React.FC = () => { const LiveStreamMonitorView: React.FC = () => {
const { client } = useRPC() const { client } = useRPC()
@@ -45,15 +45,15 @@ const LiveStreamMonitorView: React.FC = () => {
return `${hrs}:${mts}:${ss}` return `${hrs}:${mts}:${ss}`
} }
const mapStatusToChip = (status: number): React.ReactNode => { const mapStatusToChip = (status: LiveStreamStatus): React.ReactNode => {
switch (status) { switch (status) {
case 0: case LiveStreamStatus.WAITING:
return <Chip label='Waiting/Wait start' color='warning' size='small' /> return <Chip label='Waiting/Wait start' color='warning' size='small' />
case 1: case LiveStreamStatus.IN_PROGRESS:
return <Chip label='Downloading' color='primary' size='small' /> return <Chip label='Downloading' color='primary' size='small' />
case 2: case LiveStreamStatus.COMPLETED:
return <Chip label='Completed' color='success' size='small' /> return <Chip label='Completed' color='success' size='small' />
case 3: case LiveStreamStatus.ERRORED:
return <Chip label='Errored' color='error' size='small' /> return <Chip label='Errored' color='error' size='small' />
default: default:
return <Chip label='Unknown state' color='primary' size='small' /> return <Chip label='Unknown state' color='primary' size='small' />
@@ -99,15 +99,13 @@ const LiveStreamMonitorView: React.FC = () => {
{mapStatusToChip(progress[k].Status)} {mapStatusToChip(progress[k].Status)}
</TableCell> </TableCell>
<TableCell align='right'> <TableCell align='right'>
{/* TODO: change to enum */} {progress[k].Status === LiveStreamStatus.WAITING
{progress[k].Status === 0
? formatMicro(Number(progress[k].WaitTime)) ? formatMicro(Number(progress[k].WaitTime))
: "-" : "-"
} }
</TableCell> </TableCell>
<TableCell align='right'> <TableCell align='right'>
{/* TODO: change to enum */} {progress[k].Status === LiveStreamStatus.WAITING
{progress[k].Status === 0
? new Date(progress[k].LiveDate).toLocaleString() ? new Date(progress[k].LiveDate).toLocaleString()
: "-" : "-"
} }

View File

@@ -158,7 +158,6 @@ func SendFile(w http.ResponseWriter, r *http.Request) {
root := config.Instance().DownloadPath root := config.Instance().DownloadPath
// TODO: further path / file validations
if strings.Contains(filepath.Dir(filename), root) { if strings.Contains(filepath.Dir(filename), root) {
http.ServeFile(w, r, filename) http.ServeFile(w, r, filename)
return return

View File

@@ -167,8 +167,9 @@ func (l *LiveStream) monitorStartTime(r io.Reader, doneWait chan struct{}) {
waitTimeScanner() waitTimeScanner()
} }
l.status = inProgress
} }
l.status = inProgress
} }
func (l *LiveStream) WaitTime() <-chan time.Duration { func (l *LiveStream) WaitTime() <-chan time.Duration {

View File

@@ -53,7 +53,6 @@ func (m *MessageQueue) downloadConsumer() {
sem := semaphore.NewWeighted(int64(m.concurrency)) sem := semaphore.NewWeighted(int64(m.concurrency))
m.eventBus.SubscribeAsync(queueName, func(p *Process) { m.eventBus.SubscribeAsync(queueName, func(p *Process) {
//TODO: provide valid context
sem.Acquire(context.Background(), 1) sem.Acquire(context.Background(), 1)
defer sem.Release(1) defer sem.Release(1)
@@ -82,8 +81,7 @@ func (m *MessageQueue) metadataSubscriber() {
sem := semaphore.NewWeighted(1) sem := semaphore.NewWeighted(1)
m.eventBus.SubscribeAsync(queueName, func(p *Process) { m.eventBus.SubscribeAsync(queueName, func(p *Process) {
//TODO: provide valid context sem.Acquire(context.Background(), 1)
sem.Acquire(context.TODO(), 1)
defer sem.Release(1) defer sem.Release(1)
slog.Info("received process from event bus", slog.Info("received process from event bus",

View File

@@ -69,7 +69,7 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error {
1, 1,
) )
//TODO: it's idiotic but it works: virtually delay the creation time //XXX: it's idiotic but it works: virtually delay the creation time
meta.CreatedAt = time.Now().Add(time.Millisecond * time.Duration(i*10)) meta.CreatedAt = time.Now().Add(time.Millisecond * time.Duration(i*10))
proc := &Process{ proc := &Process{