diff options
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 41 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store_test.go | 8 |
2 files changed, 42 insertions, 7 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 49b70de..24e9ae6 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -2,6 +2,7 @@ package sfive import ( "database/sql" + "time" //"time" _ "github.com/mattn/go-sqlite3" @@ -395,14 +396,48 @@ type statsResult struct { LastStartTime *int64 } -func (s sqliteStore) GetStats(filter *StatsFilter) (statsResult, error) { // (map[string]interface{}, error) { +type StatsResult struct { + UpdateCount int + ClientCount float32 + BytesSent uint + BytesReceived uint + StartTime time.Time + LastStartTime time.Time +} + +func toApiStatsResult(value statsResult) (res StatsResult) { + if value.UpdateCount != nil { + res.UpdateCount = *value.UpdateCount + } + if value.ClientCount != nil { + res.ClientCount = *value.ClientCount + } + if value.BytesSent != nil { + res.BytesSent = *value.BytesSent + } + if value.BytesReceived != nil { + res.BytesReceived = *value.BytesReceived + } + if value.StartTime != nil { + res.StartTime = time.Unix(*value.StartTime, 0) + } + if value.LastStartTime != nil { + res.LastStartTime = time.Unix(*value.LastStartTime, 0) + } + return res +} + +func (s sqliteStore) GetStats(filter *StatsFilter) (StatsResult, error) { // (map[string]interface{}, error) { sourceSql, parameters := getFilteredDataUpdateSelect(filter) _ = sourceSql sql := "select " + getCountEntriesSql() + "," + getClientCountSql() + "," + getBpsStatsSql() + " from " + sourceSql - s5l.Printf("stats sql: %v", sql) + // s5l.Printf("stats sql: %v", sql) res := statsResult{} err := s.db.SelectOne(&res, sql, parameters) - return res, err + if err == nil { + return toApiStatsResult(res), nil + } + return StatsResult{}, err } func NewStore(path string) (store sqliteStore, err error) { diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index c3d62f9..a34633e 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -36,8 +36,8 @@ func TestAppend(t *testing.T) { } stats, err := store.GetStats(nil) - clientCount := int(*stats.ClientCount) - updateCount := *stats.UpdateCount + clientCount := int(stats.ClientCount) + updateCount := stats.UpdateCount if 3 != clientCount { t.Errorf("Failed fo append, invalid number of clients, 3 != %v", clientCount) } @@ -51,7 +51,7 @@ func TestAppend(t *testing.T) { if err != nil { t.Errorf("Failed to get stats: %v", err) } else { - updateCount = *stats.UpdateCount + updateCount = stats.UpdateCount if 0 != updateCount { t.Errorf("Failed to filter entries by start time, 0 != %v", updateCount) } @@ -66,7 +66,7 @@ func IgnoreTestCount(t *testing.T) { defer store.Close() stats, err := store.GetStats(nil) - clientCount := int(*stats.ClientCount) + clientCount := int(stats.ClientCount) if 0 != clientCount { t.Errorf("Failed to count correctly.") } |