From 9a609aa5fef5f43a9b55ad70dcd198d90cccdbc4 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 22 Apr 2016 22:45:19 +0800 Subject: [PATCH] update variable. Signed-off-by: Bo-Yi Wu --- gorush/const.go | 9 +++++++++ gorush/global.go | 2 +- gorush/status.go | 32 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/gorush/const.go b/gorush/const.go index b3b137c..dc3fb6f 100644 --- a/gorush/const.go +++ b/gorush/const.go @@ -18,3 +18,12 @@ const ( // FailedPush is log block FailedPush = "failed-push" ) + +// Stat variable for redis +const ( + gorushTotalCount = "gorush-total-count" + gorushIosSuccess = "gorush-ios-success-count" + gorushIosError = "gorush-ios-error-count" + gorushAndroidSuccess = "gorush-android-success-count" + gorushAndroidError = "gorush-android-error-count" +) diff --git a/gorush/global.go b/gorush/global.go index f5a5008..9beab50 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -22,6 +22,6 @@ var ( LogError *logrus.Logger // RushStatus is notification status RushStatus StatusApp - // Redis + // RedisClient is global variable for redis RedisClient *redis.Client ) diff --git a/gorush/status.go b/gorush/status.go index 029f8e6..6e23e7c 100644 --- a/gorush/status.go +++ b/gorush/status.go @@ -29,6 +29,13 @@ type IosStatus struct { PushError int64 `json:"push_error"` } +func getRedisInt64Result(key string) int64 { + val, _ := RedisClient.Get(key).Result() + count, _ := strconv.ParseInt(val, 10, 64) + + return count +} + func initApp() { RushStatus.TotalCount = 0 RushStatus.Ios.PushSuccess = 0 @@ -65,7 +72,7 @@ func addTotalCount(count int64) { case "memory": atomic.AddInt64(&RushStatus.TotalCount, count) case "redis": - RedisClient.Set("key1", strconv.Itoa(int(count)), 0) + RedisClient.Set(gorushTotalCount, strconv.Itoa(int(count)), 0) default: atomic.AddInt64(&RushStatus.TotalCount, count) } @@ -76,7 +83,7 @@ func addIosSuccess(count int64) { case "memory": atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) case "redis": - RedisClient.Set("key2", strconv.Itoa(int(count)), 0) + RedisClient.Set(gorushIosSuccess, strconv.Itoa(int(count)), 0) default: atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) } @@ -87,7 +94,7 @@ func addIosError(count int64) { case "memory": atomic.AddInt64(&RushStatus.Ios.PushError, count) case "redis": - RedisClient.Set("key3", strconv.Itoa(int(count)), 0) + RedisClient.Set(gorushIosError, strconv.Itoa(int(count)), 0) default: atomic.AddInt64(&RushStatus.Ios.PushError, count) } @@ -99,7 +106,7 @@ func addAndroidSuccess(count int64) { atomic.AddInt64(&RushStatus.Android.PushSuccess, count) case "redis": - RedisClient.Set("key4", strconv.Itoa(int(count)), 0) + RedisClient.Set(gorushAndroidSuccess, strconv.Itoa(int(count)), 0) default: atomic.AddInt64(&RushStatus.Android.PushSuccess, count) } @@ -110,7 +117,7 @@ func addAndroidError(count int64) { case "memory": atomic.AddInt64(&RushStatus.Android.PushError, count) case "redis": - RedisClient.Set("key5", strconv.Itoa(int(count)), 0) + RedisClient.Set(gorushAndroidError, strconv.Itoa(int(count)), 0) default: atomic.AddInt64(&RushStatus.Android.PushError, count) } @@ -122,8 +129,7 @@ func getTotalCount() int64 { case "memory": count = atomic.LoadInt64(&RushStatus.TotalCount) case "redis": - val, _ := RedisClient.Get("key1").Result() - count, _ = strconv.ParseInt(val, 10, 64) + count = getRedisInt64Result(gorushAndroidError) default: count = atomic.LoadInt64(&RushStatus.TotalCount) } @@ -137,8 +143,7 @@ func getIosSuccess() int64 { case "memory": count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) case "redis": - val, _ := RedisClient.Get("key2").Result() - count, _ = strconv.ParseInt(val, 10, 64) + count = getRedisInt64Result(gorushAndroidError) default: count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) } @@ -152,8 +157,7 @@ func getIosError() int64 { case "memory": count = atomic.LoadInt64(&RushStatus.Ios.PushError) case "redis": - val, _ := RedisClient.Get("key3").Result() - count, _ = strconv.ParseInt(val, 10, 64) + count = getRedisInt64Result(gorushAndroidError) default: count = atomic.LoadInt64(&RushStatus.Ios.PushError) } @@ -167,8 +171,7 @@ func getAndroidSuccess() int64 { case "memory": count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) case "redis": - val, _ := RedisClient.Get("key4").Result() - count, _ = strconv.ParseInt(val, 10, 64) + count = getRedisInt64Result(gorushAndroidError) default: count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) } @@ -182,8 +185,7 @@ func getAndroidError() int64 { case "memory": count = atomic.LoadInt64(&RushStatus.Android.PushError) case "redis": - val, _ := RedisClient.Get("key5").Result() - count, _ = strconv.ParseInt(val, 10, 64) + count = getRedisInt64Result(gorushAndroidError) default: count = atomic.LoadInt64(&RushStatus.Android.PushError) }