Skip to content

Commit

Permalink
fix(ui): list IPs in reverse chronological order
Browse files Browse the repository at this point in the history
- Fix #730
  • Loading branch information
qdm12 committed Jun 26, 2024
1 parent 08c9561 commit d3ec047
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/models/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ type HistoryEvent struct { // current and previous ips

// GetPreviousIPs returns an antichronological list of previous
// IP addresses if there is any.
func (h History) GetPreviousIPs() []netip.Addr {
func (h History) GetPreviousIPs() (previousIPs []netip.Addr) {
if len(h) <= 1 {
return nil
}
IPs := make([]netip.Addr, len(h)-1)
const two = 2
for i := len(h) - two; i >= 0; i-- {
IPs[i] = h[i].IP
previousIPs = make([]netip.Addr, len(h)-1)
mostRecentPreviousIPIndex := len(h) - 2 //nolint:gomnd
for i := range previousIPs {
previousIPs[i] = h[mostRecentPreviousIPIndex-i].IP
}
return IPs
return previousIPs
}

// GetCurrentIP returns the current IP address (latest in history).
Expand Down
46 changes: 46 additions & 0 deletions internal/models/history_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
package models

import (
"net/netip"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func Test_GetPreviousIPs(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
h History
previousIPs []netip.Addr
}{
"empty_history": {
h: History{},
},
"single_event": {
h: History{
{IP: netip.MustParseAddr("1.2.3.4")},
},
},
"two_events": {
h: History{
{IP: netip.MustParseAddr("1.2.3.4")},
{IP: netip.MustParseAddr("5.6.7.8")}, // last one
},
previousIPs: []netip.Addr{
netip.MustParseAddr("1.2.3.4"),
},
},
"three_events": {
h: History{
{IP: netip.MustParseAddr("1.2.3.4")},
{IP: netip.MustParseAddr("5.6.7.8")},
{IP: netip.MustParseAddr("9.6.7.8")}, // last one
},
previousIPs: []netip.Addr{
netip.MustParseAddr("5.6.7.8"),
netip.MustParseAddr("1.2.3.4"),
},
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
previousIPs := testCase.h.GetPreviousIPs()
assert.Equal(t, testCase.previousIPs, previousIPs)
})
}
}

func Test_GetDurationSinceSuccess(t *testing.T) {
t.Parallel()
tests := map[string]struct {
Expand Down

0 comments on commit d3ec047

Please sign in to comment.