Fix recent changes calculation
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2019-03-03 08:30:04 +01:00
parent e80383911d
commit ca643a2f2b
3 changed files with 150 additions and 18 deletions

20
main.go
View File

@ -398,6 +398,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
} }
func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 // group recentchanges on Page
if len(changes) > 0 { if len(changes) > 0 {
f := len(changes) - 1 f := groupRecentChanges(changes)
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
}
}
changes = changes[f:] changes = changes[f:]
} }

46
recent.go Normal file
View File

@ -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
}

102
recent_test.go Normal file
View File

@ -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")
}