sort logging

This commit is contained in:
Umputun
2018-05-06 13:40:58 -05:00
parent b3aa7381ce
commit 66ff83cae5
5 changed files with 15 additions and 5 deletions
+4 -3
View File
@@ -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)
}
+1 -1
View File
@@ -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
}
+3
View File
@@ -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) {
+6
View File
@@ -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:
+1 -1
View File
@@ -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/