From ca3e7568a1ae9b3f433caf22d95e222bc0bad83b Mon Sep 17 00:00:00 2001 From: Bill Davis Date: Thu, 9 Oct 2014 16:32:56 -0400 Subject: [PATCH 1/2] Add ability to get statistics in json format --- toolbox/statistics.go | 26 ++++++++++++++++++++++++++ toolbox/statistics_test.go | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/toolbox/statistics.go b/toolbox/statistics.go index 382daba0..779fd637 100644 --- a/toolbox/statistics.go +++ b/toolbox/statistics.go @@ -15,6 +15,7 @@ package toolbox import ( + "encoding/json" "fmt" "sync" "time" @@ -111,6 +112,31 @@ func (m *UrlMap) GetMap() map[string]interface{} { return content } +func (m *UrlMap) GetMapJSON() ([]byte, error) { + return json.Marshal(m) +} + +func (m UrlMap) MarshalJSON() ([]byte, error) { + + resultLists := make([]map[string]interface{}, 0) + + for k, v := range m.urlmap { + for kk, vv := range v { + result := map[string]interface{}{ + "request_url": k, + "method": kk, + "times": vv.RequestNum, + "total_time": toS(vv.TotalTime), + "max_time": toS(vv.MaxTime), + "min_time": toS(vv.MinTime), + "avg_time": toS(time.Duration(int64(vv.TotalTime) / vv.RequestNum)), + } + resultLists = append(resultLists, result) + } + } + return json.Marshal(resultLists) +} + // global statistics data map var StatisticsMap *UrlMap diff --git a/toolbox/statistics_test.go b/toolbox/statistics_test.go index 448b2af5..97860d32 100644 --- a/toolbox/statistics_test.go +++ b/toolbox/statistics_test.go @@ -28,4 +28,10 @@ func TestStatics(t *testing.T) { StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000)) StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) t.Log(StatisticsMap.GetMap()) + + jsonString, err := StatisticsMap.GetMapJSON() + if err != nil { + t.Errorf(err.Error()) + } + t.Log(string(jsonString)) } From 5c1e8e42b9f84c9f9e1e977247cf649983efb44c Mon Sep 17 00:00:00 2001 From: Bill Davis Date: Thu, 9 Oct 2014 17:07:28 -0400 Subject: [PATCH 2/2] Reworked implementation to not return encoded json --- toolbox/statistics.go | 9 ++------- toolbox/statistics_test.go | 7 +++++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/toolbox/statistics.go b/toolbox/statistics.go index 779fd637..beeafc7b 100644 --- a/toolbox/statistics.go +++ b/toolbox/statistics.go @@ -15,7 +15,6 @@ package toolbox import ( - "encoding/json" "fmt" "sync" "time" @@ -112,11 +111,7 @@ func (m *UrlMap) GetMap() map[string]interface{} { return content } -func (m *UrlMap) GetMapJSON() ([]byte, error) { - return json.Marshal(m) -} - -func (m UrlMap) MarshalJSON() ([]byte, error) { +func (m *UrlMap) GetMapData() []map[string]interface{} { resultLists := make([]map[string]interface{}, 0) @@ -134,7 +129,7 @@ func (m UrlMap) MarshalJSON() ([]byte, error) { resultLists = append(resultLists, result) } } - return json.Marshal(resultLists) + return resultLists } // global statistics data map diff --git a/toolbox/statistics_test.go b/toolbox/statistics_test.go index 97860d32..ac29476c 100644 --- a/toolbox/statistics_test.go +++ b/toolbox/statistics_test.go @@ -15,6 +15,7 @@ package toolbox import ( + "encoding/json" "testing" "time" ) @@ -29,9 +30,11 @@ func TestStatics(t *testing.T) { StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) t.Log(StatisticsMap.GetMap()) - jsonString, err := StatisticsMap.GetMapJSON() + data := StatisticsMap.GetMapData() + b, err := json.Marshal(data) if err != nil { t.Errorf(err.Error()) } - t.Log(string(jsonString)) + + t.Log(string(b)) }