diff --git a/main.go b/main.go index 3a95c2e..72a5fec 100644 --- a/main.go +++ b/main.go @@ -398,6 +398,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), 500) return } + } func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -429,24 +430,7 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // group recentchanges on Page if len(changes) > 0 { - f := len(changes) - 1 - i := f - - for { - if changes[f].Page == changes[i].Page && changes[f].Date.Truncate(24*time.Hour) == changes[i].Date.Truncate(24*time.Hour) { - changes[f].Count++ - i-- - } else { - changes[f].EndDate = changes[i+1].Date - f-- - changes[f] = changes[i] - } - - if i <= 0 { - break - } - } - + f := groupRecentChanges(changes) changes = changes[f:] } diff --git a/recent.go b/recent.go new file mode 100644 index 0000000..c953f02 --- /dev/null +++ b/recent.go @@ -0,0 +1,46 @@ +package main + +import ( + "time" +) + +func combine(x, y Change) Change { + x.Count++ + if x.Body == "" { + x.Body = y.Body + } else { + x.Body += " " + y.Body + } + x.EndDate = y.Date + + return x +} + +func changeEqual(x, y Change) bool { + return x.Page == y.Page && x.Date.Truncate(24*time.Hour) == y.Date.Truncate(24*time.Hour) +} + +func groupRecentChanges(changes []Change) int { + if len(changes) <= 1 { + return 0 + } + + f := len(changes) - 1 + i := f - 1 + + for { + if changeEqual(changes[f], changes[i]) { + changes[f] = combine(changes[f], changes[i]) + } else { + f-- + } + + if i <= 0 { + break + } + + i-- + } + + return f +} diff --git a/recent_test.go b/recent_test.go new file mode 100644 index 0000000..36fc1ab --- /dev/null +++ b/recent_test.go @@ -0,0 +1,102 @@ +package main + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestGroupNoRecentChanges(t *testing.T) { + changes := []Change{} + f := groupRecentChanges(changes) + assert.Equal(t, 0, f, "should return 0") +} + +func TestGroupOneRecentChange(t *testing.T) { + changes := []Change{ + Change{ + Page: "Home", + Date: time.Now(), + Body: "test", + }, + } + f := groupRecentChanges(changes) + assert.Equal(t, 0, f, "should return 0") + assert.Equal(t, "Home", changes[0].Page, "change should not have changed") + assert.Equal(t, "test", changes[0].Body, "change should not have changed") + assert.Equal(t, 0, changes[0].Count, "change should not have changed") +} + +func TestGroupTwoDiffRecentChanges(t *testing.T) { + d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z") + assert.NoError(t, err, "should parse first date") + + d2, err := time.Parse(time.RFC3339, "2019-02-02T11:00:00Z") + assert.NoError(t, err, "should parse second date") + + changes := []Change{ + Change{ + Page: "Home", + Date: d2, + Body: "summary 2", + }, + Change{ + Page: "Home", + Date: d1, + Body: "summary 1", + }, + } + + f := groupRecentChanges(changes) + + assert.Equal(t, 0, f, "should new position of first element") + + assert.Equal(t, "Home", changes[0].Page, "change should not have changed") + assert.Equal(t, "summary 2", changes[0].Body, "change should not have changed") + assert.Equal(t, 0, changes[0].Count, "change should not have changed") + + assert.Equal(t, "Home", changes[1].Page, "change should not have changed") + assert.Equal(t, "summary 1", changes[1].Body, "change should not have changed") + assert.Equal(t, 0, changes[1].Count, "change should not have changed") +} + +func TestGroupTwoRecentChanges(t *testing.T) { + d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z") + assert.NoError(t, err, "should parse first date") + d2, err := time.Parse(time.RFC3339, "2019-01-01T11:00:00Z") + assert.NoError(t, err, "should parse second date") + changes := []Change{ + Change{ + Page: "Home", + Date: d2, + Body: "summary 2", + }, + Change{ + Page: "Home", + Date: d1, + Body: "summary 1", + }, + } + f := groupRecentChanges(changes) + assert.Equal(t, 1, f, "should new position of first element") + assert.Equal(t, "Home", changes[f].Page, "change should not have changed") + assert.Equal(t, "summary 1 summary 2", changes[f].Body, "change should not have changed") + assert.Equal(t, 1, changes[f].Count, "change should not have changed") +} + +func TestChangeNotEqual(t *testing.T) { + d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z") + assert.NoError(t, err, "should parse first date") + d2, err := time.Parse(time.RFC3339, "2019-02-02T11:00:00Z") + assert.NoError(t, err, "should parse second date") + assert.Equal(t, false, changeEqual(Change{Page: "Home", Date: d1}, Change{Page: "Home", Date: d2}), "compare dates") +} + +func TestChangeEqual(t *testing.T) { + d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z") + assert.NoError(t, err, "should parse first date") + d2, err := time.Parse(time.RFC3339, "2019-01-01T11:00:00Z") + assert.NoError(t, err, "should parse second date") + assert.Equal(t, true, changeEqual(Change{Page: "Home", Date: d1}, Change{Page: "Home", Date: d2}), "compare dates") +}