diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 43 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store_test.go | 29 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5typesApi.go | 2 | ||||
-rwxr-xr-x | src/hub/test-client | 2 |
4 files changed, 24 insertions, 52 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 6aa3be8..306cd1b 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -2,7 +2,7 @@ package sfive import ( "database/sql" - "time" + //"time" _ "github.com/mattn/go-sqlite3" @@ -113,11 +113,12 @@ func insertAnd(needsAnd *bool) (res string) { } func getFilteredDataUpdateSelect(filter *StatsFilter) (string, map[string]interface{}) { + const baseQuery = "(select * from " + dataUpdatesTn + "," + sourcesTn + " on " + dataUpdatesTn + ".SourceId = " + sourcesTn + ".Id" if isEmptyFilter(filter) { - return dataUpdatesTn, nil + return baseQuery + ")", nil } - query := "(select * from " + dataUpdatesTn + " outer join " + sourcesTn + " on " + dataUpdatesTn + ".SourceId = " + sourcesTn + ".Id where" + query := baseQuery + " where" parameters := make(map[string]interface{}) needsAnd := false @@ -359,42 +360,6 @@ func getBpsStatsSql() string { return "sum(BytesSent) as BytesSent, sum(BytesReceived) as BytesReceived, min(StartTime) as StartTime, max(StartTime) as LastStartTime" } -func (s sqliteStore) CountUpdateEntries(filter *StatsFilter) (count int64, err error) { - sourceSql, parameters := getFilteredDataUpdateSelect(filter) - count, err = s.db.SelectInt("select "+getCountEntriesSql()+" from "+sourceSql, parameters) - return -} - -func (s sqliteStore) CountClients(filter *StatsFilter) int { - sourceSql, parameters := getFilteredDataUpdateSelect(filter) - count, err := s.db.SelectInt("select "+getClientCountSql()+" from "+sourceSql, parameters) - if err != nil { - s5l.Printf("CountClients: %v", err) - return -1 - } - return int(count) -} - -type bpsQueryResult struct { - BytesSent uint - BytesReceived uint - StartTime time.Time - LastStartTime time.Time -} - -func (s sqliteStore) GetAverageBps(filter *StatsFilter) (uint, error) { - sourceSql, parameters := getFilteredDataUpdateSelect(filter) - res := bpsQueryResult{} - err := s.db.SelectOne( - res, - "select "+getBpsStatsSql()+" from "+sourceSql, parameters) - if err == nil { - bps := (res.BytesSent + res.BytesReceived) / uint(res.StartTime.Sub(res.LastStartTime).Seconds()) - return bps, nil - } - return 0, err -} - type statsResult struct { UpdateCount *int ClientCount *float32 diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index 14be34d..022d662 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -35,21 +35,26 @@ func TestAppend(t *testing.T) { t.Errorf("Failed to append: %v", err) } - clientCnt := store.CountClients(nil) - if 3 != clientCnt { - t.Errorf("Failed fo append, invalid number of clients, 3 != %v", clientCnt) + stats, err := store.GetStats(nil) + clientCount := int(*stats.ClientCount) + updateCount := *stats.UpdateCount + if 3 != clientCount { + t.Errorf("Failed fo append, invalid number of clients, 3 != %v", clientCount) } - - updateCnt, err := store.CountUpdateEntries(nil) - if 1 != updateCnt { - t.Errorf("Failed to append, invalid number of updates, 1 != %v", updateCnt) + if 1 != updateCount { + t.Errorf("Failed to append, invalid number of updates, 1 != %v", updateCount) } queryStartTime := time.Date(2015, time.December, 24, 1, 1, 1, 0, time.UTC) filterStruct := StatsFilter{start: &queryStartTime} - updateCnt, err = store.CountUpdateEntries(&filterStruct) - if 0 != updateCnt { - t.Errorf("Failed to filter entries by start time, 0 != %v", updateCnt) + stats, err = store.GetStats(&filterStruct) + if err != nil { + t.Errorf("Failed to get stats: %v", err) + } else { + updateCount = *stats.UpdateCount + if 0 != updateCount { + t.Errorf("Failed to filter entries by start time, 0 != %v", updateCount) + } } } @@ -60,7 +65,9 @@ func IgnoreTestCount(t *testing.T) { } defer store.Close() - if 0 != store.CountClients(nil) { + stats, err := store.GetStats(nil) + clientCount := int(*stats.ClientCount) + if 0 != clientCount { t.Errorf("Failed to count correctly.") } } diff --git a/src/hub/src/spreadspace.org/sfive/s5typesApi.go b/src/hub/src/spreadspace.org/sfive/s5typesApi.go index 7b0fb52..d9af20a 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesApi.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesApi.go @@ -23,8 +23,8 @@ type SourceId struct { type ClientData struct { Ip string `json:"ip"` - BytesSent uint `json:"bytes-sent"` UserAgent string `json:"user-agent"` + BytesSent uint `json:"bytes-sent"` } type SourceData struct { diff --git a/src/hub/test-client b/src/hub/test-client index a26ce75..859b2da 100755 --- a/src/hub/test-client +++ b/src/hub/test-client @@ -9,7 +9,7 @@ echo show query result wget -q -S -O - 'http://localhost:8000/updates?from=2013-10-24T05:00:00Z&to=2013-10-24T00:05:20Z' echo '\npost update' -wget --post-file='../../dat/sample-post.json' 'http://localhost:8000/updates' +wget -q -S -O - --post-file='../../dat/sample-post.json' 'http://localhost:8000/updates' echo '\ndone' |