From 5994f48e7e23d13a5e51601c2874c4d8bf58f34a Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 2 May 2016 20:22:37 +0800 Subject: [PATCH] integrate boltdb engine. Signed-off-by: Bo-Yi Wu --- gorush/status.go | 18 ++++-- gorush/status_test.go | 104 ++++++++++++++++++---------------- storage/boltdb/boltdb.go | 87 ++++++++++++++-------------- storage/boltdb/boltdb_test.go | 34 +++++------ storage/redis/redis.go | 12 ++-- 5 files changed, 136 insertions(+), 119 deletions(-) diff --git a/gorush/status.go b/gorush/status.go index b29e4db..724c2b4 100644 --- a/gorush/status.go +++ b/gorush/status.go @@ -1,6 +1,7 @@ package gorush import ( + "github.com/appleboy/gorush/gorush/storage/boltdb" "github.com/appleboy/gorush/gorush/storage/memory" "github.com/appleboy/gorush/gorush/storage/redis" "github.com/gin-gonic/gin" @@ -29,18 +30,27 @@ type IosStatus struct { } // InitAppStatus for initialize app status -func InitAppStatus() { +func InitAppStatus() error { switch PushConf.Stat.Engine { case "memory": StatStorage = memory.New() case "redis": StatStorage = redis.New(PushConf) - StatStorage.Init() - // case "boltdb": - // initBoltDB() + err := StatStorage.Init() + + if err != nil { + LogError.Error("redis error: " + err.Error()) + + return err + } + + case "boltdb": + StatStorage = boltdb.New(PushConf) default: StatStorage = memory.New() } + + return nil } func appStatusHandler(c *gin.Context) { diff --git a/gorush/status_test.go b/gorush/status_test.go index dc065c7..fd51000 100644 --- a/gorush/status_test.go +++ b/gorush/status_test.go @@ -74,21 +74,23 @@ func TestStatForMemoryEngine(t *testing.T) { assert.Equal(t, int64(500), val) } -// func TestRedisServerSuccess(t *testing.T) { -// PushConf.Stat.Redis.Addr = "localhost:6379" +func TestRedisServerSuccess(t *testing.T) { + PushConf.Stat.Engine = "redis" + PushConf.Stat.Redis.Addr = "localhost:6379" -// err := initRedis() + err := InitAppStatus() -// assert.NoError(t, err) -// } + assert.NoError(t, err) +} -// func TestRedisServerError(t *testing.T) { -// PushConf.Stat.Redis.Addr = "localhost:6370" +func TestRedisServerError(t *testing.T) { + PushConf.Stat.Engine = "redis" + PushConf.Stat.Redis.Addr = "localhost:6370" -// err := initRedis() + err := InitAppStatus() -// assert.Error(t, err) -// } + assert.Error(t, err) +} func TestStatForRedisEngine(t *testing.T) { var val int64 @@ -117,48 +119,50 @@ func TestStatForRedisEngine(t *testing.T) { assert.Equal(t, int64(500), val) } -// func TestDefaultEngine(t *testing.T) { -// var val int64 -// PushConf.Stat.Engine = "test" -// InitAppStatus() +func TestDefaultEngine(t *testing.T) { + var val int64 + PushConf.Stat.Engine = "test" + InitAppStatus() -// addTotalCount(1) -// addIosSuccess(2) -// addIosError(3) -// addAndroidSuccess(4) -// addAndroidError(5) + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) -// val = getTotalCount() -// assert.Equal(t, int64(1), val) -// val = getIosSuccess() -// assert.Equal(t, int64(2), val) -// val = getIosError() -// assert.Equal(t, int64(3), val) -// val = getAndroidSuccess() -// assert.Equal(t, int64(4), val) -// val = getAndroidError() -// assert.Equal(t, int64(5), val) -// } + val = StatStorage.GetTotalCount() + assert.Equal(t, int64(100), val) + val = StatStorage.GetIosSuccess() + assert.Equal(t, int64(200), val) + val = StatStorage.GetIosError() + assert.Equal(t, int64(300), val) + val = StatStorage.GetAndroidSuccess() + assert.Equal(t, int64(400), val) + val = StatStorage.GetAndroidError() + assert.Equal(t, int64(500), val) +} -// func TestStatForBoltDBEngine(t *testing.T) { -// var val int64 -// PushConf.Stat.Engine = "boltdb" -// InitAppStatus() +func TestStatForBoltDBEngine(t *testing.T) { + var val int64 + PushConf.Stat.Engine = "boltdb" + InitAppStatus() -// addTotalCount(100) -// addIosSuccess(200) -// addIosError(300) -// addAndroidSuccess(400) -// addAndroidError(500) + StatStorage.Reset() -// val = getTotalCount() -// assert.Equal(t, int64(100), val) -// val = getIosSuccess() -// assert.Equal(t, int64(200), val) -// val = getIosError() -// assert.Equal(t, int64(300), val) -// val = getAndroidSuccess() -// assert.Equal(t, int64(400), val) -// val = getAndroidError() -// assert.Equal(t, int64(500), val) -// } + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) + + val = StatStorage.GetTotalCount() + assert.Equal(t, int64(100), val) + val = StatStorage.GetIosSuccess() + assert.Equal(t, int64(200), val) + val = StatStorage.GetIosError() + assert.Equal(t, int64(300), val) + val = StatStorage.GetAndroidSuccess() + assert.Equal(t, int64(400), val) + val = StatStorage.GetAndroidError() + assert.Equal(t, int64(500), val) +} diff --git a/storage/boltdb/boltdb.go b/storage/boltdb/boltdb.go index 90e399f..4f9ba18 100644 --- a/storage/boltdb/boltdb.go +++ b/storage/boltdb/boltdb.go @@ -1,37 +1,40 @@ package boltdb import ( - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/gorush/config" "github.com/asdine/storm" ) +// Stat variable for redis +const ( + TotalCountKey = "gorush-total-count" + IosSuccessKey = "gorush-ios-success-count" + IosErrorKey = "gorush-ios-error-count" + AndroidSuccessKey = "gorush-android-success-count" + AndroidErrorKey = "gorush-android-error-count" +) + // Storage implements the storage interface for gorush (https://github.com/appleboy/gorush) -func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage { +func New(config config.ConfYaml) *Storage { return &Storage{ - stat: stat, config: config, } } type Storage struct { - config gorush.ConfYaml - stat gorush.StatusApp + config config.ConfYaml } -func (s *Storage) initBoltDB() { - s.stat.TotalCount = s.getTotalCount() - s.stat.Ios.PushSuccess = s.getIosSuccess() - s.stat.Ios.PushError = s.getIosError() - s.stat.Android.PushSuccess = s.getAndroidSuccess() - s.stat.Android.PushError = s.getAndroidError() +func (s *Storage) Init() error { + return nil } -func (s *Storage) resetBoltDB() { - s.setBoltDB(gorush.TotalCountKey, 0) - s.setBoltDB(gorush.IosSuccessKey, 0) - s.setBoltDB(gorush.IosErrorKey, 0) - s.setBoltDB(gorush.AndroidSuccessKey, 0) - s.setBoltDB(gorush.AndroidErrorKey, 0) +func (s *Storage) Reset() { + s.setBoltDB(TotalCountKey, 0) + s.setBoltDB(IosSuccessKey, 0) + s.setBoltDB(IosErrorKey, 0) + s.setBoltDB(AndroidSuccessKey, 0) + s.setBoltDB(AndroidErrorKey, 0) } func (s *Storage) setBoltDB(key string, count int64) { @@ -46,62 +49,62 @@ func (s *Storage) getBoltDB(key string, count *int64) { defer db.Close() } -func (s *Storage) addTotalCount(count int64) { - total := s.getTotalCount() + count - s.setBoltDB(gorush.TotalCountKey, total) +func (s *Storage) AddTotalCount(count int64) { + total := s.GetTotalCount() + count + s.setBoltDB(TotalCountKey, total) } -func (s *Storage) addIosSuccess(count int64) { - total := s.getIosSuccess() + count - s.setBoltDB(gorush.IosSuccessKey, total) +func (s *Storage) AddIosSuccess(count int64) { + total := s.GetIosSuccess() + count + s.setBoltDB(IosSuccessKey, total) } -func (s *Storage) addIosError(count int64) { - total := s.getIosError() + count - s.setBoltDB(gorush.IosErrorKey, total) +func (s *Storage) AddIosError(count int64) { + total := s.GetIosError() + count + s.setBoltDB(IosErrorKey, total) } -func (s *Storage) addAndroidSuccess(count int64) { - total := s.getAndroidSuccess() + count - s.setBoltDB(gorush.AndroidSuccessKey, total) +func (s *Storage) AddAndroidSuccess(count int64) { + total := s.GetAndroidSuccess() + count + s.setBoltDB(AndroidSuccessKey, total) } -func (s *Storage) addAndroidError(count int64) { - total := s.getAndroidError() + count - s.setBoltDB(gorush.AndroidErrorKey, total) +func (s *Storage) AddAndroidError(count int64) { + total := s.GetAndroidError() + count + s.setBoltDB(AndroidErrorKey, total) } -func (s *Storage) getTotalCount() int64 { +func (s *Storage) GetTotalCount() int64 { var count int64 - s.getBoltDB(gorush.TotalCountKey, &count) + s.getBoltDB(TotalCountKey, &count) return count } -func (s *Storage) getIosSuccess() int64 { +func (s *Storage) GetIosSuccess() int64 { var count int64 - s.getBoltDB(gorush.IosSuccessKey, &count) + s.getBoltDB(IosSuccessKey, &count) return count } -func (s *Storage) getIosError() int64 { +func (s *Storage) GetIosError() int64 { var count int64 - s.getBoltDB(gorush.IosErrorKey, &count) + s.getBoltDB(IosErrorKey, &count) return count } -func (s *Storage) getAndroidSuccess() int64 { +func (s *Storage) GetAndroidSuccess() int64 { var count int64 - s.getBoltDB(gorush.AndroidSuccessKey, &count) + s.getBoltDB(AndroidSuccessKey, &count) return count } -func (s *Storage) getAndroidError() int64 { +func (s *Storage) GetAndroidError() int64 { var count int64 - s.getBoltDB(gorush.AndroidErrorKey, &count) + s.getBoltDB(AndroidErrorKey, &count) return count } diff --git a/storage/boltdb/boltdb_test.go b/storage/boltdb/boltdb_test.go index 61b28cb..b219d8a 100644 --- a/storage/boltdb/boltdb_test.go +++ b/storage/boltdb/boltdb_test.go @@ -1,7 +1,7 @@ package boltdb import ( - "github.com/appleboy/gorush/gorush" + c "github.com/appleboy/gorush/gorush/config" "github.com/stretchr/testify/assert" "testing" ) @@ -9,32 +9,32 @@ import ( func TestRedisEngine(t *testing.T) { var val int64 - config := gorush.BuildDefaultPushConf() + config := c.BuildDefaultPushConf() - boltDB := New(config, gorush.StatusApp{}) - boltDB.initBoltDB() - boltDB.resetBoltDB() + boltDB := New(config) + boltDB.Init() + boltDB.Reset() - boltDB.addTotalCount(10) - val = boltDB.getTotalCount() + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() assert.Equal(t, int64(10), val) - boltDB.addTotalCount(10) - val = boltDB.getTotalCount() + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() assert.Equal(t, int64(20), val) - boltDB.addIosSuccess(20) - val = boltDB.getIosSuccess() + boltDB.AddIosSuccess(20) + val = boltDB.GetIosSuccess() assert.Equal(t, int64(20), val) - boltDB.addIosError(30) - val = boltDB.getIosError() + boltDB.AddIosError(30) + val = boltDB.GetIosError() assert.Equal(t, int64(30), val) - boltDB.addAndroidSuccess(40) - val = boltDB.getAndroidSuccess() + boltDB.AddAndroidSuccess(40) + val = boltDB.GetAndroidSuccess() assert.Equal(t, int64(40), val) - boltDB.addAndroidError(50) - val = boltDB.getAndroidError() + boltDB.AddAndroidError(50) + val = boltDB.GetAndroidError() assert.Equal(t, int64(50), val) } diff --git a/storage/redis/redis.go b/storage/redis/redis.go index cb9005f..cebe44f 100644 --- a/storage/redis/redis.go +++ b/storage/redis/redis.go @@ -25,7 +25,7 @@ func New(config config.ConfYaml) *Storage { } } -func getRedisInt64Result(key string, count *int64) { +func getInt64(key string, count *int64) { val, _ := RedisClient.Get(key).Result() *count, _ = strconv.ParseInt(val, 10, 64) } @@ -88,35 +88,35 @@ func (s *Storage) AddAndroidError(count int64) { func (s *Storage) GetTotalCount() int64 { var count int64 - getRedisInt64Result(TotalCountKey, &count) + getInt64(TotalCountKey, &count) return count } func (s *Storage) GetIosSuccess() int64 { var count int64 - getRedisInt64Result(IosSuccessKey, &count) + getInt64(IosSuccessKey, &count) return count } func (s *Storage) GetIosError() int64 { var count int64 - getRedisInt64Result(IosErrorKey, &count) + getInt64(IosErrorKey, &count) return count } func (s *Storage) GetAndroidSuccess() int64 { var count int64 - getRedisInt64Result(AndroidSuccessKey, &count) + getInt64(AndroidSuccessKey, &count) return count } func (s *Storage) GetAndroidError() int64 { var count int64 - getRedisInt64Result(AndroidErrorKey, &count) + getInt64(AndroidErrorKey, &count) return count }