chore: Add metric and status package. (#585)
This commit is contained in:
82
status/status.go
Normal file
82
status/status.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/logx"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
"github.com/appleboy/gorush/storage/badger"
|
||||
"github.com/appleboy/gorush/storage/boltdb"
|
||||
"github.com/appleboy/gorush/storage/buntdb"
|
||||
"github.com/appleboy/gorush/storage/leveldb"
|
||||
"github.com/appleboy/gorush/storage/memory"
|
||||
"github.com/appleboy/gorush/storage/redis"
|
||||
|
||||
"github.com/thoas/stats"
|
||||
)
|
||||
|
||||
// Stats provide response time, status code count, etc.
|
||||
var Stats = stats.New()
|
||||
|
||||
// StatStorage implements the storage interface
|
||||
var StatStorage storage.Storage
|
||||
|
||||
// App is status structure
|
||||
type App struct {
|
||||
Version string `json:"version"`
|
||||
QueueMax int `json:"queue_max"`
|
||||
QueueUsage int `json:"queue_usage"`
|
||||
TotalCount int64 `json:"total_count"`
|
||||
Ios IosStatus `json:"ios"`
|
||||
Android AndroidStatus `json:"android"`
|
||||
Huawei HuaweiStatus `json:"huawei"`
|
||||
}
|
||||
|
||||
// AndroidStatus is android structure
|
||||
type AndroidStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// IosStatus is iOS structure
|
||||
type IosStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// HuaweiStatus is huawei structure
|
||||
type HuaweiStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// InitAppStatus for initialize app status
|
||||
func InitAppStatus(conf config.ConfYaml) error {
|
||||
logx.LogAccess.Info("Init App Status Engine as ", conf.Stat.Engine)
|
||||
switch conf.Stat.Engine {
|
||||
case "memory":
|
||||
StatStorage = memory.New()
|
||||
case "redis":
|
||||
StatStorage = redis.New(conf)
|
||||
case "boltdb":
|
||||
StatStorage = boltdb.New(conf)
|
||||
case "buntdb":
|
||||
StatStorage = buntdb.New(conf)
|
||||
case "leveldb":
|
||||
StatStorage = leveldb.New(conf)
|
||||
case "badger":
|
||||
StatStorage = badger.New(conf)
|
||||
default:
|
||||
logx.LogError.Error("storage error: can't find storage driver")
|
||||
return errors.New("can't find storage driver")
|
||||
}
|
||||
|
||||
if err := StatStorage.Init(); err != nil {
|
||||
logx.LogError.Error("storage error: " + err.Error())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
242
status/status_test.go
Normal file
242
status/status_test.go
Normal file
@@ -0,0 +1,242 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/logx"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
PushConf, _ := config.LoadConf("")
|
||||
if err := logx.InitLog(
|
||||
PushConf.Log.AccessLevel,
|
||||
PushConf.Log.AccessLog,
|
||||
PushConf.Log.ErrorLevel,
|
||||
PushConf.Log.ErrorLog,
|
||||
); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestStorageDriverExist(t *testing.T) {
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "Test"
|
||||
err := InitAppStatus(PushConf)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestStatForMemoryEngine(t *testing.T) {
|
||||
// wait android push notification response.
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
var val int64
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "memory"
|
||||
err := InitAppStatus(PushConf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func TestRedisServerSuccess(t *testing.T) {
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "redis"
|
||||
PushConf.Stat.Redis.Addr = "redis:6379"
|
||||
|
||||
err := InitAppStatus(PushConf)
|
||||
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRedisServerError(t *testing.T) {
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "redis"
|
||||
PushConf.Stat.Redis.Addr = "redis:6370"
|
||||
|
||||
err := InitAppStatus(PushConf)
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestStatForRedisEngine(t *testing.T) {
|
||||
var val int64
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "redis"
|
||||
PushConf.Stat.Redis.Addr = "redis:6379"
|
||||
err := InitAppStatus(PushConf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
StatStorage.Init()
|
||||
StatStorage.Reset()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func TestDefaultEngine(t *testing.T) {
|
||||
var val int64
|
||||
// defaul engine as memory
|
||||
PushConf, _ := config.LoadConf("")
|
||||
err := InitAppStatus(PushConf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
StatStorage.Reset()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func TestStatForBoltDBEngine(t *testing.T) {
|
||||
var val int64
|
||||
PushConf, _ := config.LoadConf("")
|
||||
PushConf.Stat.Engine = "boltdb"
|
||||
err := InitAppStatus(PushConf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
StatStorage.Reset()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// func TestStatForBuntDBEngine(t *testing.T) {
|
||||
// var val int64
|
||||
// PushConf.Stat.Engine = "buntdb"
|
||||
// err := InitAppStatus()
|
||||
// assert.Nil(t, err)
|
||||
|
||||
// StatStorage.Reset()
|
||||
|
||||
// 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)
|
||||
// }
|
||||
|
||||
// func TestStatForLevelDBEngine(t *testing.T) {
|
||||
// var val int64
|
||||
// PushConf.Stat.Engine = "leveldb"
|
||||
// err := InitAppStatus()
|
||||
// assert.Nil(t, err)
|
||||
|
||||
// StatStorage.Reset()
|
||||
|
||||
// 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)
|
||||
// }
|
||||
|
||||
// func TestStatForBadgerEngine(t *testing.T) {
|
||||
// var val int64
|
||||
// PushConf.Stat.Engine = "badger"
|
||||
// err := InitAppStatus()
|
||||
// assert.Nil(t, err)
|
||||
|
||||
// StatStorage.Reset()
|
||||
|
||||
// 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)
|
||||
// }
|
||||
Reference in New Issue
Block a user