From 663fde7481f5fca068c28073d38133d52324a046 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 1 Aug 2023 14:24:26 +0200 Subject: [PATCH] Show red dot when switch sync errors are currently happening. (#202) --- cmd/switch_test.go | 16 ++++++++-------- cmd/tableprinters/switch.go | 10 +++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/switch_test.go b/cmd/switch_test.go index 71edeae..4a58690 100644 --- a/cmd/switch_test.go +++ b/cmd/switch_test.go @@ -141,8 +141,8 @@ func Test_SwitchCmd_MultiResult(t *testing.T) { }, wantTable: pointer.Pointer(` ID PARTITION RACK OS STATUS -1 1 rack-1 🦔  ● -2 1 rack-1 🐢  ● +1 1 rack-1 🦔 ● +2 1 rack-1 🐢 ● `), wantWideTable: pointer.Pointer(` ID PARTITION RACK OS METALCORE IP MODE LAST SYNC SYNC DURATION LAST SYNC ERROR @@ -157,8 +157,8 @@ ID PARTITION RACK OS METALCORE IP MODE LAST wantMarkdown: pointer.Pointer(` | ID | PARTITION | RACK | OS | STATUS | |----|-----------|--------|----|--------| -| 1 | 1 | rack-1 | 🦔 |  ● | -| 2 | 1 | rack-1 | 🐢 |  ● | +| 1 | 1 | rack-1 | 🦔 | ● | +| 2 | 1 | rack-1 | 🐢 | ● | `), }, { @@ -189,7 +189,7 @@ ID PARTITION RACK OS METALCORE IP MODE LAST }, wantTable: pointer.Pointer(` ID PARTITION RACK OS STATUS -1 1 rack-1 🦔  ● +1 1 rack-1 🦔 ● `), wantWideTable: pointer.Pointer(` ID PARTITION RACK OS METALCORE IP MODE LAST SYNC SYNC DURATION LAST SYNC ERROR @@ -202,7 +202,7 @@ ID PARTITION RACK OS METALCORE IP MODE LAST S wantMarkdown: pointer.Pointer(` | ID | PARTITION | RACK | OS | STATUS | |----|-----------|--------|----|--------| -| 1 | 1 | rack-1 | 🦔 |  ● | +| 1 | 1 | rack-1 | 🦔 | ● | `), }, { @@ -353,7 +353,7 @@ func Test_SwitchCmd_SingleResult(t *testing.T) { want: switch1, wantTable: pointer.Pointer(` ID PARTITION RACK OS STATUS -1 1 rack-1 🦔  ● +1 1 rack-1 🦔 ● `), wantWideTable: pointer.Pointer(` ID PARTITION RACK OS METALCORE IP MODE LAST SYNC SYNC DURATION LAST SYNC ERROR @@ -366,7 +366,7 @@ ID PARTITION RACK OS METALCORE IP MODE LAST S wantMarkdown: pointer.Pointer(` | ID | PARTITION | RACK | OS | STATUS | |----|-----------|--------|----|--------| -| 1 | 1 | rack-1 | 🦔 |  ● | +| 1 | 1 | rack-1 | 🦔 | ● | `), }, { diff --git a/cmd/tableprinters/switch.go b/cmd/tableprinters/switch.go index d90ee98..3387d66 100644 --- a/cmd/tableprinters/switch.go +++ b/cmd/tableprinters/switch.go @@ -44,11 +44,11 @@ func (t *TablePrinter) SwitchTable(data []*models.V1SwitchResponse, wide bool) ( syncAge := time.Since(syncTime) syncDur := time.Duration(*s.LastSync.Duration).Round(time.Millisecond) if syncAge >= time.Minute*10 || syncDur >= 30*time.Second { - shortStatus += color.RedString(dot) + shortStatus = color.RedString(dot) } else if syncAge >= time.Minute*1 || syncDur >= 20*time.Second { - shortStatus += color.YellowString(dot) + shortStatus = color.YellowString(dot) } else { - shortStatus += color.GreenString(dot) + shortStatus = color.GreenString(dot) } syncAgeStr = humanizeDuration(syncAge) @@ -60,6 +60,10 @@ func (t *TablePrinter) SwitchTable(data []*models.V1SwitchResponse, wide bool) ( // after 7 days we do not show sync errors anymore if !errorTime.IsZero() && time.Since(errorTime) < 7*24*time.Hour { syncError = fmt.Sprintf("%s ago: %s", humanizeDuration(time.Since(errorTime)), s.LastSyncError.Error) + + if errorTime.After(time.Time(pointer.SafeDeref(s.LastSync.Time))) { + shortStatus = color.RedString(dot) + } } }