2016-05-02 04:06:08 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2016-05-03 01:56:59 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2016-05-02 04:06:08 +00:00
|
|
|
"gopkg.in/redis.v3"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2016-05-02 11:42:21 +00:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
//
|
|
|
|
var redisClient *redis.Client
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2016-07-31 13:10:59 +00:00
|
|
|
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
2016-05-02 11:42:21 +00:00
|
|
|
func New(config config.ConfYaml) *Storage {
|
2016-05-02 04:06:08 +00:00
|
|
|
return &Storage{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 12:22:37 +00:00
|
|
|
func getInt64(key string, count *int64) {
|
2016-08-01 02:39:33 +00:00
|
|
|
val, _ := redisClient.Get(key).Result()
|
2016-05-02 04:06:08 +00:00
|
|
|
*count, _ = strconv.ParseInt(val, 10, 64)
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Storage is interface structure
|
2016-05-02 04:06:08 +00:00
|
|
|
type Storage struct {
|
2016-05-02 11:42:21 +00:00
|
|
|
config config.ConfYaml
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Init client storage.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) Init() error {
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient = redis.NewClient(&redis.Options{
|
2016-05-02 04:06:08 +00:00
|
|
|
Addr: s.config.Stat.Redis.Addr,
|
|
|
|
Password: s.config.Stat.Redis.Password,
|
|
|
|
DB: s.config.Stat.Redis.DB,
|
|
|
|
})
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
_, err := redisClient.Ping().Result()
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// redis server error
|
|
|
|
log.Println("Can't connect redis server: " + err.Error())
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Reset Client storage.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) Reset() {
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(TotalCountKey, strconv.Itoa(0), 0)
|
|
|
|
redisClient.Set(IosSuccessKey, strconv.Itoa(0), 0)
|
|
|
|
redisClient.Set(IosErrorKey, strconv.Itoa(0), 0)
|
|
|
|
redisClient.Set(AndroidSuccessKey, strconv.Itoa(0), 0)
|
|
|
|
redisClient.Set(AndroidErrorKey, strconv.Itoa(0), 0)
|
2016-05-02 04:34:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddTotalCount record push notification count.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddTotalCount(count int64) {
|
|
|
|
total := s.GetTotalCount() + count
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(TotalCountKey, strconv.Itoa(int(total)), 0)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddIosSuccess record counts of success iOS push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddIosSuccess(count int64) {
|
|
|
|
total := s.GetIosSuccess() + count
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(IosSuccessKey, strconv.Itoa(int(total)), 0)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddIosError record counts of error iOS push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddIosError(count int64) {
|
|
|
|
total := s.GetIosError() + count
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(IosErrorKey, strconv.Itoa(int(total)), 0)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddAndroidSuccess record counts of success Android push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
|
|
|
total := s.GetAndroidSuccess() + count
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(AndroidSuccessKey, strconv.Itoa(int(total)), 0)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddAndroidError record counts of error Android push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddAndroidError(count int64) {
|
|
|
|
total := s.GetAndroidError() + count
|
2016-08-01 02:39:33 +00:00
|
|
|
redisClient.Set(AndroidErrorKey, strconv.Itoa(int(total)), 0)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetTotalCount show counts of all notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetTotalCount() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
getInt64(TotalCountKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetIosSuccess show success counts of iOS notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetIosSuccess() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
getInt64(IosSuccessKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetIosError show error counts of iOS notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetIosError() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
getInt64(IosErrorKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetAndroidSuccess show success counts of Android notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetAndroidSuccess() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
getInt64(AndroidSuccessKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetAndroidError show error counts of Android notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetAndroidError() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
getInt64(AndroidErrorKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|