From 8f2d9eaf6ed1f368b65c99cd5f83e9c7ca91cc45 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Tue, 20 Aug 2024 20:29:32 +0200 Subject: [PATCH] code refactoring --- frontend/src/types/index.ts | 9 ++++++++- frontend/src/views/Livestream.tsx | 18 ++++++++---------- server/handlers/archive.go | 1 - server/internal/livestream/livestream.go | 3 ++- server/internal/message_queue.go | 4 +--- server/internal/playlist.go | 2 +- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts index 9160420..d530b35 100644 --- a/frontend/src/types/index.ts +++ b/frontend/src/types/index.ts @@ -102,8 +102,15 @@ export type CustomTemplate = { content: string } +export enum LiveStreamStatus { + WAITING, + IN_PROGRESS, + COMPLETED, + ERRORED +} + export type LiveStreamProgress = Record \ No newline at end of file diff --git a/frontend/src/views/Livestream.tsx b/frontend/src/views/Livestream.tsx index 54a51d5..4917d70 100644 --- a/frontend/src/views/Livestream.tsx +++ b/frontend/src/views/Livestream.tsx @@ -13,7 +13,7 @@ import LivestreamSpeedDial from '../components/livestream/LivestreamSpeedDial' import NoLivestreams from '../components/livestream/NoLivestreams' import { useSubscription } from '../hooks/observable' import { useRPC } from '../hooks/useRPC' -import { LiveStreamProgress } from '../types' +import { LiveStreamProgress, LiveStreamStatus } from '../types' const LiveStreamMonitorView: React.FC = () => { const { client } = useRPC() @@ -45,15 +45,15 @@ const LiveStreamMonitorView: React.FC = () => { return `${hrs}:${mts}:${ss}` } - const mapStatusToChip = (status: number): React.ReactNode => { + const mapStatusToChip = (status: LiveStreamStatus): React.ReactNode => { switch (status) { - case 0: + case LiveStreamStatus.WAITING: return - case 1: + case LiveStreamStatus.IN_PROGRESS: return - case 2: + case LiveStreamStatus.COMPLETED: return - case 3: + case LiveStreamStatus.ERRORED: return default: return @@ -99,15 +99,13 @@ const LiveStreamMonitorView: React.FC = () => { {mapStatusToChip(progress[k].Status)} - {/* TODO: change to enum */} - {progress[k].Status === 0 + {progress[k].Status === LiveStreamStatus.WAITING ? formatMicro(Number(progress[k].WaitTime)) : "-" } - {/* TODO: change to enum */} - {progress[k].Status === 0 + {progress[k].Status === LiveStreamStatus.WAITING ? new Date(progress[k].LiveDate).toLocaleString() : "-" } diff --git a/server/handlers/archive.go b/server/handlers/archive.go index 552add7..d77db4f 100644 --- a/server/handlers/archive.go +++ b/server/handlers/archive.go @@ -158,7 +158,6 @@ func SendFile(w http.ResponseWriter, r *http.Request) { root := config.Instance().DownloadPath - // TODO: further path / file validations if strings.Contains(filepath.Dir(filename), root) { http.ServeFile(w, r, filename) return diff --git a/server/internal/livestream/livestream.go b/server/internal/livestream/livestream.go index 2109354..c8e0add 100644 --- a/server/internal/livestream/livestream.go +++ b/server/internal/livestream/livestream.go @@ -167,8 +167,9 @@ func (l *LiveStream) monitorStartTime(r io.Reader, doneWait chan struct{}) { waitTimeScanner() } - l.status = inProgress } + + l.status = inProgress } func (l *LiveStream) WaitTime() <-chan time.Duration { diff --git a/server/internal/message_queue.go b/server/internal/message_queue.go index 2f54a9c..bc4f06e 100644 --- a/server/internal/message_queue.go +++ b/server/internal/message_queue.go @@ -53,7 +53,6 @@ func (m *MessageQueue) downloadConsumer() { sem := semaphore.NewWeighted(int64(m.concurrency)) m.eventBus.SubscribeAsync(queueName, func(p *Process) { - //TODO: provide valid context sem.Acquire(context.Background(), 1) defer sem.Release(1) @@ -82,8 +81,7 @@ func (m *MessageQueue) metadataSubscriber() { sem := semaphore.NewWeighted(1) m.eventBus.SubscribeAsync(queueName, func(p *Process) { - //TODO: provide valid context - sem.Acquire(context.TODO(), 1) + sem.Acquire(context.Background(), 1) defer sem.Release(1) slog.Info("received process from event bus", diff --git a/server/internal/playlist.go b/server/internal/playlist.go index f3eed3e..c61c90b 100644 --- a/server/internal/playlist.go +++ b/server/internal/playlist.go @@ -69,7 +69,7 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error { 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)) proc := &Process{