From 66ff83cae549dc299064252d5cf49ebe26b425c3 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 6 May 2018 13:40:58 -0500 Subject: [PATCH] sort logging --- app/rest/api/rest.go | 7 ++++--- app/rest/tree.go | 2 +- app/rest/tree_test.go | 3 +++ app/store/store.go | 6 ++++++ remark.rest | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 3e4aae08..0c3f4f9d 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -215,10 +215,11 @@ func (s *Rest) updateCommentCtrl(w http.ResponseWriter, r *http.Request) { // find comments for given post. Returns in tree or plain formats, sorted func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { locator := store.Locator{SiteID: r.URL.Query().Get("site"), URL: r.URL.Query().Get("url")} - log.Printf("[DEBUG] get comments for %+v", locator) + sort := r.URL.Query().Get("sort") + log.Printf("[DEBUG] get comments for %+v, sort %s", locator, sort) data, err := s.Cache.Get(rest.URLKey(r), time.Hour, func() ([]byte, error) { - comments, e := s.DataService.Find(locator, r.URL.Query().Get("sort")) + comments, e := s.DataService.Find(locator, sort) if e != nil { return nil, e } @@ -226,7 +227,7 @@ func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { var b []byte switch r.URL.Query().Get("format") { case "tree": - b, e = encodeJSONWithHTML(rest.MakeTree(maskedComments, r.URL.Query().Get("sort"))) + b, e = encodeJSONWithHTML(rest.MakeTree(maskedComments, sort)) default: b, e = encodeJSONWithHTML(maskedComments) } diff --git a/app/rest/tree.go b/app/rest/tree.go index 9f2fe9f6..fb954525 100644 --- a/app/rest/tree.go +++ b/app/rest/tree.go @@ -78,7 +78,7 @@ func (t *Tree) sortNodes(sortType string) { case "+score", "-score", "score": if strings.HasPrefix(sortType, "-") { if t.Nodes[i].Comment.Score == t.Nodes[j].Comment.Score { - return t.Nodes[i].Comment.Timestamp.After(t.Nodes[j].Comment.Timestamp) + return t.Nodes[i].Comment.Timestamp.Before(t.Nodes[j].Comment.Timestamp) } return t.Nodes[i].Comment.Score > t.Nodes[j].Comment.Score } diff --git a/app/rest/tree_test.go b/app/rest/tree_test.go index d579cbf9..a0e54fa3 100644 --- a/app/rest/tree_test.go +++ b/app/rest/tree_test.go @@ -81,6 +81,9 @@ func TestMakeTreeSorts(t *testing.T) { res = MakeTree(comments, "-score") assert.Equal(t, "2", res.Nodes[0].Comment.ID) + assert.Equal(t, "1", res.Nodes[1].Comment.ID) + assert.Equal(t, "3", res.Nodes[2].Comment.ID) + assert.Equal(t, "6", res.Nodes[3].Comment.ID) } func BenchmarkTree(b *testing.B) { diff --git a/app/store/store.go b/app/store/store.go index e6e961d1..1cb9696a 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -45,8 +45,14 @@ func sortComments(comments []Comment, sortFld string) []Comment { case "+score", "-score", "score": if strings.HasPrefix(sortFld, "-") { + if comments[i].Score == comments[j].Score { + return comments[i].Timestamp.Before(comments[j].Timestamp) + } return comments[i].Score > comments[j].Score } + if comments[i].Score == comments[j].Score { + return comments[i].Timestamp.Before(comments[j].Timestamp) + } return comments[i].Score < comments[j].Score default: diff --git a/remark.rest b/remark.rest index ecc1b601..b49a1df0 100644 --- a/remark.rest +++ b/remark.rest @@ -1,6 +1,6 @@ ### find request with tree -GET {{host}}/api/v1/find?site=remark&sort=+score&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/ +GET {{host}}/api/v1/find?site=remark&sort=-score&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/ ### find request with plain GET {{host}}/api/v1/find?site=remark&sort=-score&format=plain&url=https://radio-t.com/p/2017/12/16/podcast-576/