Reorganize code for the taglist
This commit is contained in:
parent
5d19d3c72f
commit
921cea3efb
3 changed files with 46 additions and 37 deletions
37
database.go
37
database.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Book struct {
|
type Book struct {
|
||||||
|
@ -59,3 +60,39 @@ func GetBook(coll *mgo.Collection, query bson.M, r ...int) (books []Book, num in
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tagsList []struct {
|
||||||
|
Subject string "_id"
|
||||||
|
Count int "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t tagsList) Len() int {
|
||||||
|
return len(t)
|
||||||
|
}
|
||||||
|
func (t tagsList) Less(i, j int) bool {
|
||||||
|
return t[i].Count > t[j].Count
|
||||||
|
}
|
||||||
|
func (t tagsList) Swap(i, j int) {
|
||||||
|
aux := t[i]
|
||||||
|
t[i] = t[j]
|
||||||
|
t[j] = aux
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTags(coll *mgo.Collection) (tagsList, error) {
|
||||||
|
// TODO: cache the tags
|
||||||
|
var mr mgo.MapReduce
|
||||||
|
mr.Map = "function() { " +
|
||||||
|
"this.subject.forEach(function(s) { emit(s, 1); });" +
|
||||||
|
"}"
|
||||||
|
mr.Reduce = "function(tag, vals) { " +
|
||||||
|
"var count = 0;" +
|
||||||
|
"vals.forEach(function() { count += 1; });" +
|
||||||
|
"return count;" +
|
||||||
|
"}"
|
||||||
|
var result tagsList
|
||||||
|
_, err := coll.Find(nil).MapReduce(&mr, &result)
|
||||||
|
if err == nil {
|
||||||
|
sort.Sort(result)
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
42
trantor.go
42
trantor.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -15,6 +14,7 @@ const (
|
||||||
NEW_BOOKS_COLL = "new"
|
NEW_BOOKS_COLL = "new"
|
||||||
USERS_COLL = "users"
|
USERS_COLL = "users"
|
||||||
PASS_SALT = "ImperialLibSalt"
|
PASS_SALT = "ImperialLibSalt"
|
||||||
|
TAGS_DISPLAY = 50
|
||||||
)
|
)
|
||||||
|
|
||||||
type aboutData struct {
|
type aboutData struct {
|
||||||
|
@ -88,48 +88,20 @@ type indexData struct {
|
||||||
Tags []string
|
Tags []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type tagsList []struct {
|
|
||||||
Subject string "_id"
|
|
||||||
Count int "value"
|
|
||||||
}
|
|
||||||
func (t tagsList) Len() int {
|
|
||||||
return len(t)
|
|
||||||
}
|
|
||||||
func (t tagsList) Less(i, j int) bool {
|
|
||||||
return t[i].Count > t[j].Count
|
|
||||||
}
|
|
||||||
func (t tagsList) Swap(i, j int) {
|
|
||||||
aux := t[i]
|
|
||||||
t[i] = t[j]
|
|
||||||
t[j] = aux
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
|
func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var data indexData
|
var data indexData
|
||||||
|
|
||||||
/* get the tags */
|
/* get the tags */
|
||||||
// TODO: cache the tags
|
tags, err := GetTags(coll)
|
||||||
var mr mgo.MapReduce
|
|
||||||
mr.Map = "function() { " +
|
|
||||||
"this.subject.forEach(function(s) { emit(s, 1); });" +
|
|
||||||
"}"
|
|
||||||
mr.Reduce = "function(tag, vals) { " +
|
|
||||||
"var count = 0;" +
|
|
||||||
"vals.forEach(function() { count += 1; });" +
|
|
||||||
"return count;" +
|
|
||||||
"}"
|
|
||||||
var result tagsList
|
|
||||||
_, err := coll.Find(nil).MapReduce(&mr, &result)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sort.Sort(result)
|
length := len(tags)
|
||||||
length := len(result)
|
if length > TAGS_DISPLAY {
|
||||||
if length > 50 {
|
length = TAGS_DISPLAY
|
||||||
length = 50
|
|
||||||
}
|
}
|
||||||
data.Tags = make([]string, length)
|
data.Tags = make([]string, length)
|
||||||
for i, tag := range result {
|
for i, tag := range tags {
|
||||||
if i == 50 {
|
if i == TAGS_DISPLAY {
|
||||||
break /* display only 50 */
|
break /* display only 50 */
|
||||||
}
|
}
|
||||||
if tag.Subject != "" {
|
if tag.Subject != "" {
|
||||||
|
|
|
@ -187,7 +187,7 @@ func parseDate(date []string) string {
|
||||||
return strings.Replace(date[0], "Unspecified: ", "", -1)
|
return strings.Replace(date[0], "Unspecified: ", "", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func keywords(b map[string] interface{}) (k []string) {
|
func keywords(b map[string]interface{}) (k []string) {
|
||||||
title, _ := b["title"].(string)
|
title, _ := b["title"].(string)
|
||||||
k = strings.Split(title, " ")
|
k = strings.Split(title, " ")
|
||||||
author, _ := b["author"].([]string)
|
author, _ := b["author"].([]string)
|
||||||
|
@ -202,7 +202,7 @@ func keywords(b map[string] interface{}) (k []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFile(coll *mgo.Collection, path string) (string, error) {
|
func parseFile(coll *mgo.Collection, path string) (string, error) {
|
||||||
book := map[string] interface{}{}
|
book := map[string]interface{}{}
|
||||||
|
|
||||||
e, err := epub.Open(path, 0)
|
e, err := epub.Open(path, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Reference in a new issue