summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Grüneis <gimpf@gimpf.org>2014-10-13 18:11:41 +0200
committerMarkus Grüneis <gimpf@gimpf.org>2014-10-13 18:12:23 +0200
commit79407cfc14d8141957c4c42f02f088d0691bcf1f (patch)
treea5cd212feca61eefbab49e6648bd7dbb28d30daa
parentadd example data (diff)
add db (gorp) initialization
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5store.go87
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5store_test.go13
2 files changed, 80 insertions, 20 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go
index 604a9ff..28b6210 100644
--- a/src/hub/src/spreadspace.org/sfive/s5store.go
+++ b/src/hub/src/spreadspace.org/sfive/s5store.go
@@ -2,9 +2,60 @@ package sfive
import (
"database/sql"
+ "log"
"time"
+
+ _ "github.com/mattn/go-sqlite3"
+
+ "github.com/coopernurse/gorp"
)
+type streamIdDb struct {
+ Id int
+ StreamId
+}
+
+type dataUpdateDb struct {
+ Id int
+ DataUpdate
+}
+
+type dataUpdateToStreamIdDb struct {
+ StreamId int
+ DataUpdateId int
+}
+
+func initDb() *gorp.DbMap {
+ // connect to db using standard Go database/sql API
+ // use whatever database/sql driver you wish
+ db, err := sql.Open("sqlite3", "/home/gimpf/test.sqlite")
+ checkErr(err, "sql.Open failed")
+
+ // construct a gorp DbMap
+ dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
+
+ // add a table, setting the table name to 'posts' and
+ // specifying that the Id property is an auto incrementing PK
+ dbmap.AddTableWithName(streamIdDb{}, "StreamIds").SetKeys(true, "Id")
+ dbmap.AddTableWithName(dataUpdateDb{}, "DataUpdates").SetKeys(true, "Id")
+ // dbmap.AddTableWithName(dataUpdateToStreamIdDb{}, "DataUpdatesPerStream").SetKeys(true, "StreamId", "DataUpdateId")
+
+ // create the table. in a production system you'd generally
+ // use a migration tool, or create the tables via scripts
+ err = dbmap.DropTablesIfExists()
+ checkErr(err, "Drop tables failed")
+ err = dbmap.CreateTables()
+ checkErr(err, "Create tables failed")
+
+ return dbmap
+}
+
+func checkErr(err error, msg string) {
+ if err != nil {
+ log.Fatalln(msg, err)
+ }
+}
+
type StatsFilter struct {
start *time.Time
end *time.Time
@@ -22,40 +73,36 @@ type StatsContainer interface {
Locations(filter StatsFilter) map[string]int
}
-type SqliteStore struct {
- db *sql.DB
+type sqliteStore struct {
+ db *gorp.DbMap
}
-func InitSqlDb(db *sql.DB) {
-
-}
-
-func NewStore() (store StatsContainer, err error) {
- db, err := sql.Open("sqlite3", ":memory:")
- if err != nil {
- return
- }
- res := &SqliteStore{db}
- store = res
- return
-}
-
-func (s *SqliteStore) Append(update StatisticsData) (err error) {
+func (s *sqliteStore) Append(update StatisticsData) (err error) {
// TODO
return nil
}
-func (s *SqliteStore) ClientCount(filter StatsFilter) uint {
+func (s *sqliteStore) ClientCount(filter StatsFilter) uint {
return 0
// TODO
}
-func (s *SqliteStore) AverageBps(filter StatsFilter) uint {
+func (s *sqliteStore) AverageBps(filter StatsFilter) uint {
return 0
// TODO
}
-func (s *SqliteStore) Locations(filter StatsFilter) map[string]int {
+func (s *sqliteStore) Locations(filter StatsFilter) map[string]int {
return nil
// TODO
}
+
+func NewStore() (store StatsContainer, err error) {
+ db := initDb()
+ if db == nil {
+ return
+ }
+ res := &sqliteStore{db}
+ store = res
+ return
+}
diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go
new file mode 100644
index 0000000..7708525
--- /dev/null
+++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go
@@ -0,0 +1,13 @@
+package sfive
+
+import (
+ "testing"
+ "time"
+)
+
+func TestStuff(t *testing.T) {
+ gd := initDb()
+ dat := dataUpdateDb{-1, DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC), Duration: 5000}}
+ gd.Insert(dat)
+ // _ = gd.Db
+}