2016-04-15 01:41:10 +00:00
|
|
|
package gorush
|
|
|
|
|
|
|
|
import (
|
2016-12-20 01:34:25 +00:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2016-05-03 02:26:13 +00:00
|
|
|
"github.com/appleboy/gorush/storage/boltdb"
|
2016-09-19 08:19:20 +00:00
|
|
|
"github.com/appleboy/gorush/storage/buntdb"
|
|
|
|
"github.com/appleboy/gorush/storage/leveldb"
|
2016-05-03 02:26:13 +00:00
|
|
|
"github.com/appleboy/gorush/storage/memory"
|
|
|
|
"github.com/appleboy/gorush/storage/redis"
|
2016-04-15 02:24:39 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-06-26 04:21:32 +00:00
|
|
|
"github.com/thoas/stats"
|
2016-04-15 01:41:10 +00:00
|
|
|
)
|
|
|
|
|
2016-06-26 04:21:32 +00:00
|
|
|
// Stats provide response time, status code count, etc.
|
|
|
|
var Stats = stats.New()
|
|
|
|
|
2016-04-15 08:59:57 +00:00
|
|
|
// StatusApp is app status structure
|
2016-04-15 01:41:10 +00:00
|
|
|
type StatusApp struct {
|
2016-09-30 02:49:52 +00:00
|
|
|
Version string `json:"version"`
|
2016-04-15 01:41:10 +00:00
|
|
|
QueueMax int `json:"queue_max"`
|
|
|
|
QueueUsage int `json:"queue_usage"`
|
|
|
|
TotalCount int64 `json:"total_count"`
|
|
|
|
Ios IosStatus `json:"ios"`
|
|
|
|
Android AndroidStatus `json:"android"`
|
|
|
|
}
|
|
|
|
|
2016-04-15 08:59:57 +00:00
|
|
|
// AndroidStatus is android structure
|
2016-04-15 01:41:10 +00:00
|
|
|
type AndroidStatus struct {
|
|
|
|
PushSuccess int64 `json:"push_success"`
|
|
|
|
PushError int64 `json:"push_error"`
|
|
|
|
}
|
|
|
|
|
2016-04-15 08:59:57 +00:00
|
|
|
// IosStatus is iOS structure
|
2016-04-15 01:41:10 +00:00
|
|
|
type IosStatus struct {
|
|
|
|
PushSuccess int64 `json:"push_success"`
|
|
|
|
PushError int64 `json:"push_error"`
|
|
|
|
}
|
|
|
|
|
2016-04-22 09:54:00 +00:00
|
|
|
// InitAppStatus for initialize app status
|
2016-05-02 12:22:37 +00:00
|
|
|
func InitAppStatus() error {
|
2017-07-26 01:14:18 +00:00
|
|
|
LogAccess.Debug("Init App Status Engine as ", PushConf.Stat.Engine)
|
2016-04-22 09:54:00 +00:00
|
|
|
switch PushConf.Stat.Engine {
|
|
|
|
case "memory":
|
2016-05-02 09:03:08 +00:00
|
|
|
StatStorage = memory.New()
|
2016-05-02 11:42:21 +00:00
|
|
|
case "redis":
|
|
|
|
StatStorage = redis.New(PushConf)
|
2016-05-02 12:22:37 +00:00
|
|
|
case "boltdb":
|
|
|
|
StatStorage = boltdb.New(PushConf)
|
2016-09-19 08:19:20 +00:00
|
|
|
case "buntdb":
|
|
|
|
StatStorage = buntdb.New(PushConf)
|
|
|
|
case "leveldb":
|
|
|
|
StatStorage = leveldb.New(PushConf)
|
2016-04-22 09:54:00 +00:00
|
|
|
default:
|
2017-06-24 14:47:35 +00:00
|
|
|
LogError.Error("storage error: can't find storage driver")
|
|
|
|
return errors.New("can't find storage driver")
|
2016-04-22 09:54:00 +00:00
|
|
|
}
|
2016-05-02 12:22:37 +00:00
|
|
|
|
2016-12-20 01:34:25 +00:00
|
|
|
if err := StatStorage.Init(); err != nil {
|
2016-09-19 08:19:20 +00:00
|
|
|
LogError.Error("storage error: " + err.Error())
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-02 12:22:37 +00:00
|
|
|
return nil
|
2016-04-15 01:41:10 +00:00
|
|
|
}
|
2016-04-15 02:24:39 +00:00
|
|
|
|
|
|
|
func appStatusHandler(c *gin.Context) {
|
|
|
|
result := StatusApp{}
|
|
|
|
|
2016-09-30 02:49:52 +00:00
|
|
|
result.Version = GetVersion()
|
2016-04-15 02:24:39 +00:00
|
|
|
result.QueueMax = cap(QueueNotification)
|
|
|
|
result.QueueUsage = len(QueueNotification)
|
2016-05-02 09:03:08 +00:00
|
|
|
result.TotalCount = StatStorage.GetTotalCount()
|
|
|
|
result.Ios.PushSuccess = StatStorage.GetIosSuccess()
|
|
|
|
result.Ios.PushError = StatStorage.GetIosError()
|
|
|
|
result.Android.PushSuccess = StatStorage.GetAndroidSuccess()
|
|
|
|
result.Android.PushError = StatStorage.GetAndroidError()
|
2016-04-15 02:24:39 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
}
|
2016-06-26 04:21:32 +00:00
|
|
|
|
|
|
|
func sysStatsHandler(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, Stats.Data())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatMiddleware response time, status code count, etc.
|
|
|
|
func StatMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
beginning, recorder := Stats.Begin(c.Writer)
|
|
|
|
c.Next()
|
|
|
|
Stats.End(beginning, recorder)
|
|
|
|
}
|
|
|
|
}
|