1
0
Fork 0

Add statistics to client and command to view them

This commit is contained in:
Aaron Lindsay 2013-09-04 23:09:28 -04:00
parent e375489cae
commit 11b57cda66
4 changed files with 109 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import (
"asink" "asink"
"asink/util" "asink/util"
"code.google.com/p/goconf/conf" "code.google.com/p/goconf/conf"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -124,6 +125,9 @@ func StartClient(args []string) {
} }
func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) { func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) {
StatStartLocalUpdate()
defer StatStopLocalUpdate()
//make the path relative before we save/send it anywhere //make the path relative before we save/send it anywhere
var err error var err error
absolutePath := event.Path absolutePath := event.Path
@ -190,7 +194,9 @@ func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) {
} }
//upload file to remote storage //upload file to remote storage
StatStartUpload()
err = globals.storage.Put(cachedFilename, event.Hash) err = globals.storage.Put(cachedFilename, event.Hash)
StatStopUpload()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -217,6 +223,8 @@ func ProcessLocalEvents(globals AsinkGlobals, eventChan chan *asink.Event) {
} }
func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) { func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
StatStartRemoteUpdate()
defer StatStopRemoteUpdate()
latestLocal := LockPath(event.Path, true) latestLocal := LockPath(event.Path, true)
defer UnlockPath(event) defer UnlockPath(event)
@ -248,7 +256,9 @@ func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
} }
tmpfilename := outfile.Name() tmpfilename := outfile.Name()
outfile.Close() outfile.Close()
StatStartDownload()
err = globals.storage.Get(tmpfilename, event.Hash) err = globals.storage.Get(tmpfilename, event.Hash)
StatStopDownload()
if err != nil { if err != nil {
panic(err) //TODO handle sensibly panic(err) //TODO handle sensibly
} }
@ -307,7 +317,7 @@ func ProcessRemoteEvents(globals AsinkGlobals, eventChan chan *asink.Event) {
} }
} }
func StopClient(args []string) { func getSocketFromArgs(args []string) (string, error) {
const config_usage = "Config File to use" const config_usage = "Config File to use"
userHomeDir := "~" userHomeDir := "~"
@ -323,20 +333,46 @@ func StopClient(args []string) {
config, err := conf.ReadConfigFile(globals.configFileName) config, err := conf.ReadConfigFile(globals.configFileName)
if err != nil { if err != nil {
fmt.Println(err) return "", err
fmt.Println("Error reading config file at ", globals.configFileName, ". Does it exist?")
return
} }
rpcSock, err := config.GetString("local", "socket") //TODO make sure this exists rpcSock, err := config.GetString("local", "socket")
if err != nil { if err != nil {
fmt.Println("Error reading local.socket from config file at ", globals.configFileName) return "", errors.New("Error reading local.socket from config file at " + globals.configFileName)
}
return rpcSock, nil
}
func StopClient(args []string) {
rpcSock, err := getSocketFromArgs(args)
if err != nil {
fmt.Println(err)
return
} }
i := 99 i := 99
returnCode := 0 returnCode := 0
err = asink.RPCCall(rpcSock, "ClientStopper.StopClient", &returnCode, &i) err = asink.RPCCall(rpcSock, "ClientAdmin.StopClient", &i, &returnCode)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func GetStatus(args []string) {
var status string
rpcSock, err := getSocketFromArgs(args)
if err != nil {
fmt.Println(err)
return
}
i := 99
err = asink.RPCCall(rpcSock, "ClientAdmin.GetClientStatus", &i, &status)
if err != nil {
panic(err)
}
fmt.Println(status)
}

View File

@ -27,17 +27,16 @@ var commands []Command = []Command{
fn: StopClient, fn: StopClient,
explanation: "Stop the client daemon", explanation: "Stop the client daemon",
}, },
Command{
cmd: "status",
fn: GetStatus,
explanation: "Get a summary of the client's status",
},
Command{ Command{
cmd: "version", cmd: "version",
fn: PrintVersion, fn: PrintVersion,
explanation: "Display the current version", explanation: "Display the current version",
}, },
/* Command{
cmd: "status",
fn: GetStatus,
explanation: "Get a summary of the client's status",
},
*/
} }
func main() { func main() {

View File

@ -11,19 +11,24 @@ import (
"net/rpc" "net/rpc"
) )
type ClientStopper int type ClientAdmin int
func (c *ClientStopper) StopClient(code *int, result *int) error { func (c *ClientAdmin) StopClient(code *int, result *int) error {
asink.Exit(*code) asink.Exit(*code)
*result = 0 *result = 0
return nil return nil
} }
func (c *ClientAdmin) GetClientStatus(code *int, result *string) error {
*result = GetStats()
return nil
}
func StartRPC(sock string, tornDown chan int) { func StartRPC(sock string, tornDown chan int) {
defer func() { tornDown <- 0 }() //the main thread waits for this to ensure the socket is closed defer func() { tornDown <- 0 }() //the main thread waits for this to ensure the socket is closed
clientstop := new(ClientStopper) clientadmin := new(ClientAdmin)
rpc.Register(clientstop) rpc.Register(clientadmin)
rpc.HandleHTTP() rpc.HandleHTTP()
l, err := net.Listen("unix", sock) l, err := net.Listen("unix", sock)

52
asink/status.go Normal file
View File

@ -0,0 +1,52 @@
/*
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
*/
package main
import (
"fmt"
"sync/atomic"
)
var localUpdates int32 = 0
var remoteUpdates int32 = 0
var fileUploads int32 = 0
var fileDownloads int32 = 0
func GetStats() string {
local := atomic.LoadInt32(&localUpdates)
remote := atomic.LoadInt32(&remoteUpdates)
uploads := atomic.LoadInt32(&fileUploads)
downloads := atomic.LoadInt32(&fileDownloads)
return fmt.Sprintf(`Asink client statistics:
Processing %d file updates (%d local, %d remote)
Uploading %d files
Downloading %d files`, local + remote, local, remote, uploads, downloads)
}
func StatStartLocalUpdate() {
atomic.AddInt32(&localUpdates, 1)
}
func StatStopLocalUpdate() {
atomic.AddInt32(&localUpdates, -1)
}
func StatStartRemoteUpdate() {
atomic.AddInt32(&remoteUpdates, 1)
}
func StatStopRemoteUpdate() {
atomic.AddInt32(&remoteUpdates, -1)
}
func StatStartUpload() {
atomic.AddInt32(&fileUploads, 1)
}
func StatStopUpload() {
atomic.AddInt32(&fileUploads, -1)
}
func StatStartDownload() {
atomic.AddInt32(&fileDownloads, 1)
}
func StatStopDownload() {
atomic.AddInt32(&fileDownloads, -1)
}