2016-05-02 06:52:38 +00:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
2017-08-24 03:25:05 +00:00
|
|
|
"log"
|
|
|
|
|
2016-05-03 01:56:59 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2017-06-24 16:48:48 +00:00
|
|
|
"github.com/appleboy/gorush/storage"
|
2016-05-02 06:52:38 +00:00
|
|
|
|
2020-04-23 07:58:03 +00:00
|
|
|
"github.com/asdine/storm/v3"
|
2016-05-02 12:22:37 +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 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
|
2020-04-23 07:39:24 +00:00
|
|
|
db *storm.DB
|
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 {
|
2020-04-23 07:39:24 +00:00
|
|
|
var err error
|
|
|
|
s.db, err = storm.Open(s.config.Stat.BoltDB.Path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the storage connection
|
|
|
|
func (s *Storage) Close() error {
|
|
|
|
if s.db == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.db.Close()
|
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() {
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.TotalCountKey, 0)
|
|
|
|
s.setBoltDB(storage.IosSuccessKey, 0)
|
|
|
|
s.setBoltDB(storage.IosErrorKey, 0)
|
|
|
|
s.setBoltDB(storage.AndroidSuccessKey, 0)
|
|
|
|
s.setBoltDB(storage.AndroidErrorKey, 0)
|
2020-09-04 03:01:21 +00:00
|
|
|
s.setBoltDB(storage.HuaweiSuccessKey, 0)
|
|
|
|
s.setBoltDB(storage.HuaweiErrorKey, 0)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) setBoltDB(key string, count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.db.Set(s.config.Stat.BoltDB.Bucket, key, count)
|
2017-08-24 03:25:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("BoltDB set error:", err.Error())
|
|
|
|
}
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) getBoltDB(key string, count *int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.db.Get(s.config.Stat.BoltDB.Bucket, key, count)
|
2017-08-24 03:25:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("BoltDB get error:", err.Error())
|
|
|
|
}
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBoltDB(storage.AndroidErrorKey, total)
|
2016-05-02 06:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 03:01:21 +00:00
|
|
|
// AddHuaweiSuccess record counts of success Huawei push notification.
|
|
|
|
func (s *Storage) AddHuaweiSuccess(count int64) {
|
|
|
|
total := s.GetHuaweiSuccess() + count
|
|
|
|
s.setBoltDB(storage.HuaweiSuccessKey, total)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHuaweiError record counts of error Huawei push notification.
|
|
|
|
func (s *Storage) AddHuaweiError(count int64) {
|
|
|
|
total := s.GetHuaweiError() + count
|
|
|
|
s.setBoltDB(storage.HuaweiErrorKey, total)
|
|
|
|
}
|
|
|
|
|
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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBoltDB(storage.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
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBoltDB(storage.AndroidErrorKey, &count)
|
2016-05-02 06:52:38 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
// GetHuaweiSuccess show success counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiSuccess() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getBoltDB(storage.HuaweiSuccessKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHuaweiError show error counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiError() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getBoltDB(storage.HuaweiErrorKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|