2016-05-02 06:52:38 +00:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
2016-05-03 01:56:59 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2016-05-02 06:52:38 +00:00
|
|
|
"github.com/asdine/storm"
|
|
|
|
)
|
|
|
|
|
2016-05-02 12:22:37 +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-07-31 13:10:59 +00:00
|
|
|
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
2016-05-02 12:22:37 +00:00
|
|
|
func New(config config.ConfYaml) *Storage {
|
2016-05-02 06:52:38 +00:00
|
|
|
return &Storage{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// Storage is interface structure
|
2016-05-02 06:52:38 +00:00
|
|
|
type Storage struct {
|
2016-05-02 12:22:37 +00:00
|
|
|
config config.ConfYaml
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// Init client storage.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) Init() error {
|
|
|
|
return nil
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// Reset Client storage.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) Reset() {
|
|
|
|
s.setBoltDB(TotalCountKey, 0)
|
|
|
|
s.setBoltDB(IosSuccessKey, 0)
|
|
|
|
s.setBoltDB(IosErrorKey, 0)
|
|
|
|
s.setBoltDB(AndroidSuccessKey, 0)
|
|
|
|
s.setBoltDB(AndroidErrorKey, 0)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) setBoltDB(key string, count int64) {
|
|
|
|
db, _ := storm.Open(s.config.Stat.BoltDB.Path)
|
|
|
|
db.Set(s.config.Stat.BoltDB.Bucket, key, count)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) getBoltDB(key string, count *int64) {
|
|
|
|
db, _ := storm.Open(s.config.Stat.BoltDB.Path)
|
|
|
|
db.Get(s.config.Stat.BoltDB.Bucket, key, count)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// AddTotalCount record push notification count.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) AddTotalCount(count int64) {
|
|
|
|
total := s.GetTotalCount() + count
|
|
|
|
s.setBoltDB(TotalCountKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// AddIosSuccess record counts of success iOS push notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) AddIosSuccess(count int64) {
|
|
|
|
total := s.GetIosSuccess() + count
|
|
|
|
s.setBoltDB(IosSuccessKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// AddIosError record counts of error iOS push notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) AddIosError(count int64) {
|
|
|
|
total := s.GetIosError() + count
|
|
|
|
s.setBoltDB(IosErrorKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// AddAndroidSuccess record counts of success Android push notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
|
|
|
total := s.GetAndroidSuccess() + count
|
|
|
|
s.setBoltDB(AndroidSuccessKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// AddAndroidError record counts of error Android push notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) AddAndroidError(count int64) {
|
|
|
|
total := s.GetAndroidError() + count
|
|
|
|
s.setBoltDB(AndroidErrorKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// GetTotalCount show counts of all notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) GetTotalCount() int64 {
|
2016-05-02 06:52:38 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
s.getBoltDB(TotalCountKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// GetIosSuccess show success counts of iOS notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) GetIosSuccess() int64 {
|
2016-05-02 06:52:38 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
s.getBoltDB(IosSuccessKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// GetIosError show error counts of iOS notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) GetIosError() int64 {
|
2016-05-02 06:52:38 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
s.getBoltDB(IosErrorKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// GetAndroidSuccess show success counts of Android notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) GetAndroidSuccess() int64 {
|
2016-05-02 06:52:38 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
s.getBoltDB(AndroidSuccessKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// GetAndroidError show error counts of Android notification.
|
2016-05-02 12:22:37 +00:00
|
|
|
func (s *Storage) GetAndroidError() int64 {
|
2016-05-02 06:52:38 +00:00
|
|
|
var count int64
|
2016-05-02 12:22:37 +00:00
|
|
|
s.getBoltDB(AndroidErrorKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|