From 96eec9ce110875499351ee08f86f586875748a0b Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Fri, 4 Dec 2020 06:48:58 -0800 Subject: [PATCH] Fix no previous enqueue entry's UI display --- conversion_helpers.go | 13 +++++--- ui/src/api.ts | 4 ++- ui/src/components/SchedulerEntriesTable.tsx | 36 ++++++++++++--------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/conversion_helpers.go b/conversion_helpers.go index 2f49e93..93381f5 100644 --- a/conversion_helpers.go +++ b/conversion_helpers.go @@ -219,8 +219,9 @@ type SchedulerEntry struct { TaskType string `json:"task_type"` TaskPayload asynq.Payload `json:"task_payload"` Opts []string `json:"options"` - NextEnqueueAt time.Time `json:"next_enqueue_at"` - PrevEnqueueAt time.Time `json:"prev_enqueue_at"` + NextEnqueueAt string `json:"next_enqueue_at"` + // This field is omitted if there were no previous enqueue events. + PrevEnqueueAt string `json:"prev_enqueue_at,omitempty"` } func toSchedulerEntry(e *asynq.SchedulerEntry) *SchedulerEntry { @@ -228,14 +229,18 @@ func toSchedulerEntry(e *asynq.SchedulerEntry) *SchedulerEntry { for _, o := range e.Opts { opts = append(opts, o.String()) } + prev := "" + if !e.Prev.IsZero() { + prev = e.Prev.Format(time.RFC3339) + } return &SchedulerEntry{ ID: e.ID, Spec: e.Spec, TaskType: e.Task.Type, TaskPayload: e.Task.Payload, Opts: opts, - NextEnqueueAt: e.Next, - PrevEnqueueAt: e.Prev, + NextEnqueueAt: e.Next.Format(time.RFC3339), + PrevEnqueueAt: prev, } } diff --git a/ui/src/api.ts b/ui/src/api.ts index a7a2c75..19d40a1 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -108,7 +108,9 @@ export interface SchedulerEntry { task_payload: { [key: string]: any }; options: string[]; next_enqueue_at: string; - prev_enqueue_at: string; + // prev_enqueue_at will be omitted + // if there were no previous enqueue events. + prev_enqueue_at?: string; } export interface PaginationOptions extends Record { diff --git a/ui/src/components/SchedulerEntriesTable.tsx b/ui/src/components/SchedulerEntriesTable.tsx index a216d6d..d669ee5 100644 --- a/ui/src/components/SchedulerEntriesTable.tsx +++ b/ui/src/components/SchedulerEntriesTable.tsx @@ -118,36 +118,38 @@ export default function SchedulerEntriesTable(props: Props) { } }; - const cmpFunc = (e1: SchedulerEntry, q2: SchedulerEntry): number => { + const cmpFunc = (e1: SchedulerEntry, e2: SchedulerEntry): number => { let isE1Smaller: boolean; switch (sortBy) { case SortBy.EntryId: - if (e1.id === q2.id) return 0; - isE1Smaller = e1.id < q2.id; + if (e1.id === e2.id) return 0; + isE1Smaller = e1.id < e2.id; break; case SortBy.Spec: - if (e1.spec === q2.spec) return 0; - isE1Smaller = e1.spec < q2.spec; + if (e1.spec === e2.spec) return 0; + isE1Smaller = e1.spec < e2.spec; break; case SortBy.Type: - if (e1.task_type === q2.task_type) return 0; - isE1Smaller = e1.task_type < q2.task_type; + if (e1.task_type === e2.task_type) return 0; + isE1Smaller = e1.task_type < e2.task_type; break; case SortBy.Payload: - if (e1.task_payload === q2.task_payload) return 0; - isE1Smaller = e1.task_payload < q2.task_payload; + if (e1.task_payload === e2.task_payload) return 0; + isE1Smaller = e1.task_payload < e2.task_payload; break; case SortBy.Options: - if (e1.options === q2.options) return 0; - isE1Smaller = e1.options < q2.options; + if (e1.options === e2.options) return 0; + isE1Smaller = e1.options < e2.options; break; case SortBy.NextEnqueue: - if (e1.next_enqueue_at === q2.next_enqueue_at) return 0; - isE1Smaller = e1.next_enqueue_at < q2.next_enqueue_at; + if (e1.next_enqueue_at === e2.next_enqueue_at) return 0; + isE1Smaller = e1.next_enqueue_at < e2.next_enqueue_at; break; case SortBy.PrevEnqueue: - if (e1.prev_enqueue_at === q2.prev_enqueue_at) return 0; - isE1Smaller = e1.prev_enqueue_at < q2.prev_enqueue_at; + const e1PrevEnqueueAt = e1.prev_enqueue_at || ""; + const e2PrevEnqueueAt = e2.prev_enqueue_at || ""; + if (e1PrevEnqueueAt === e2PrevEnqueueAt) return 0; + isE1Smaller = e1PrevEnqueueAt < e2PrevEnqueueAt; break; default: // eslint-disable-next-line no-throw-literal @@ -228,7 +230,9 @@ export default function SchedulerEntriesTable(props: Props) { {durationBefore(entry.next_enqueue_at)} - {timeAgo(entry.prev_enqueue_at)} + {entry.prev_enqueue_at + ? timeAgo(entry.prev_enqueue_at) + : "N/A"} );