update with stats

This commit is contained in:
Jan Halfar 2013-09-19 15:36:45 +02:00
parent 4815a73db8
commit b03da8b9c6
7 changed files with 101 additions and 33 deletions

Binary file not shown.

View File

@ -5,8 +5,10 @@ import (
"github.com/foomo/ContentServer/server/log"
"github.com/foomo/ContentServer/server/repo/content"
"github.com/foomo/ContentServer/server/requests"
"github.com/foomo/ContentServer/server/responses"
"github.com/foomo/ContentServer/server/utils"
"strings"
"time"
)
type Repo struct {
@ -103,7 +105,6 @@ func (repo *Repo) GetNode(repoNode *content.RepoNode, expanded bool, mimeTypes [
} else {
fmt.Println("no see for", childNode.GetName(language), childNode.Hidden)
}
}
return node
}
@ -154,26 +155,46 @@ func (repo *Repo) GetContent(r *requests.Content) *content.SiteContent {
return c
}
func (repo *Repo) builDirectory(dirNode *content.RepoNode) {
func builDirectory(dirNode *content.RepoNode, directory map[string]*content.RepoNode, uRIDirectory map[string]*content.RepoNode) {
log.Debug("repo.buildDirectory: " + dirNode.Id)
repo.Directory[dirNode.Id] = dirNode
directory[dirNode.Id] = dirNode
//todo handle duplicate uris
for _, languageURIs := range dirNode.URIs {
for _, URI := range languageURIs {
log.Debug(" URI: " + URI + " => Id: " + dirNode.Id)
repo.URIDirectory[URI] = dirNode
uRIDirectory[URI] = dirNode
}
}
for _, childNode := range dirNode.Nodes {
repo.builDirectory(childNode)
builDirectory(childNode, directory, uRIDirectory)
}
}
func (repo *Repo) Update() {
repo.Node = content.NewRepoNode()
utils.Get(repo.server, repo.Node)
repo.Node.WireParents()
repo.Directory = make(map[string]*content.RepoNode)
repo.URIDirectory = make(map[string]*content.RepoNode)
repo.builDirectory(repo.Node)
func (repo *Repo) Update() *responses.Update {
updateResponse := responses.NewUpdate()
newNode := content.NewRepoNode()
startTimeRepo := time.Now()
utils.Get(repo.server, newNode)
updateResponse.Stats.RepoRuntime = time.Now().Sub(startTimeRepo).Seconds()
startTimeOwn := time.Now()
newNode.WireParents()
newDirectory := make(map[string]*content.RepoNode)
newURIDirectory := make(map[string]*content.RepoNode)
builDirectory(newNode, newDirectory, newURIDirectory)
repo.Node = newNode
repo.Directory = newDirectory
repo.URIDirectory = newURIDirectory
updateResponse.Stats.OwnRuntime = time.Now().Sub(startTimeOwn).Seconds()
updateResponse.Stats.NumberOfNodes = len(repo.Directory)
updateResponse.Stats.NumberOfURIs = len(repo.URIDirectory)
return updateResponse
}

View File

@ -0,0 +1,8 @@
package requests
type Update struct {
}
func NewUpdate() *Update {
return new(Update)
}

13
server/responses/error.go Normal file
View File

@ -0,0 +1,13 @@
package responses
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
func NewError(code int, message string) *Error {
error := new(Error)
error.Code = code
error.Message = message
return error
}

View File

@ -0,0 +1 @@
package responses

View File

@ -0,0 +1,14 @@
package responses
type Update struct {
Stats struct {
NumberOfNodes int `json:"numberOfNodes"`
NumberOfURIs int `json:"numberOfURIs"`
RepoRuntime float64 `json:"repoRuntime"`
OwnRuntime float64 `json:"ownRuntime"`
} `json:"stats"`
}
func NewUpdate() *Update {
return new(Update)
}

View File

@ -7,6 +7,7 @@ import (
"github.com/foomo/ContentServer/server/log"
"github.com/foomo/ContentServer/server/repo"
"github.com/foomo/ContentServer/server/requests"
"github.com/foomo/ContentServer/server/responses"
"net"
"strconv"
"strings"
@ -56,22 +57,28 @@ func handleSocketRequest(handler string, jsonBuffer []byte) (replyBytes []byte,
nodesMap := contentRepo.GetNodes(nodesRequest)
reply = nodesMap
break
case "update":
updateRequest := requests.NewUpdate()
jsonErr = json.Unmarshal(jsonBuffer, &updateRequest)
log.Debug(" updateRequest: " + fmt.Sprint(updateRequest))
updateResponse := contentRepo.Update()
reply = updateResponse
break
default:
err = errors.New(log.Error(" can not handle this one " + handler))
errorResponse := responses.NewError(1, "unkown handler")
reply = errorResponse
}
if err == nil {
if jsonErr != nil {
log.Error(" could not read incoming json: " + fmt.Sprint(jsonErr))
err = jsonErr
} else {
encodedBytes, jsonErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ")
if jsonErr != nil {
log.Error(" could not read incoming json: " + fmt.Sprint(jsonErr))
err = jsonErr
log.Error(" could not encode reply " + fmt.Sprint(jsonErr))
} else {
encodedBytes, jsonErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ")
if jsonErr != nil {
err = jsonErr
log.Error(" could not encode reply " + fmt.Sprint(jsonErr))
} else {
replyBytes = encodedBytes
}
replyBytes = encodedBytes
}
}
return replyBytes, err
@ -109,22 +116,26 @@ func handleConnection(conn net.Conn) {
reply, handlingError := handleSocketRequest(requestHandler, jsonBuffer)
if handlingError != nil {
log.Error("socket.handleConnection: handlingError " + fmt.Sprint(handlingError))
if reply == nil {
log.Error("giving up with nil reply")
conn.Close()
return
}
}
headerBytes := []byte(strconv.Itoa(len(reply)))
reply = concat(headerBytes, reply)
log.Debug(" replying: " + string(reply))
_, writeError := conn.Write(reply)
if writeError != nil {
log.Error("socket.handleConnection: could not write my reply: " + fmt.Sprint(writeError))
return
} else {
headerBytes := []byte(strconv.Itoa(len(reply)))
reply = concat(headerBytes, reply)
log.Debug(" replying: " + string(reply))
_, writeError := conn.Write(reply)
if writeError != nil {
log.Error("socket.handleConnection: could not write my reply: " + fmt.Sprint(writeError))
return
} else {
log.Debug(" replied. waiting for next request on open connection")
//return
}
log.Debug(" replied. waiting for next request on open connection")
//return
}
} else {
log.Error("can not read empty json")
conn.Close()
return
}
} else {