Merge pull request #852 from pabdavis/statistics-json
[Proposal] Ability to get statistics data unformatted
Showing
2 changed files
with
30 additions
and
0 deletions
| ... | @@ -111,6 +111,27 @@ func (m *UrlMap) GetMap() map[string]interface{} { | ... | @@ -111,6 +111,27 @@ func (m *UrlMap) GetMap() map[string]interface{} { |
| 111 | return content | 111 | return content |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | func (m *UrlMap) GetMapData() []map[string]interface{} { | ||
| 115 | |||
| 116 | resultLists := make([]map[string]interface{}, 0) | ||
| 117 | |||
| 118 | for k, v := range m.urlmap { | ||
| 119 | for kk, vv := range v { | ||
| 120 | result := map[string]interface{}{ | ||
| 121 | "request_url": k, | ||
| 122 | "method": kk, | ||
| 123 | "times": vv.RequestNum, | ||
| 124 | "total_time": toS(vv.TotalTime), | ||
| 125 | "max_time": toS(vv.MaxTime), | ||
| 126 | "min_time": toS(vv.MinTime), | ||
| 127 | "avg_time": toS(time.Duration(int64(vv.TotalTime) / vv.RequestNum)), | ||
| 128 | } | ||
| 129 | resultLists = append(resultLists, result) | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return resultLists | ||
| 133 | } | ||
| 134 | |||
| 114 | // global statistics data map | 135 | // global statistics data map |
| 115 | var StatisticsMap *UrlMap | 136 | var StatisticsMap *UrlMap |
| 116 | 137 | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | package toolbox | 15 | package toolbox |
| 16 | 16 | ||
| 17 | import ( | 17 | import ( |
| 18 | "encoding/json" | ||
| 18 | "testing" | 19 | "testing" |
| 19 | "time" | 20 | "time" |
| 20 | ) | 21 | ) |
| ... | @@ -28,4 +29,12 @@ func TestStatics(t *testing.T) { | ... | @@ -28,4 +29,12 @@ func TestStatics(t *testing.T) { |
| 28 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000)) | 29 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000)) |
| 29 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) | 30 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) |
| 30 | t.Log(StatisticsMap.GetMap()) | 31 | t.Log(StatisticsMap.GetMap()) |
| 32 | |||
| 33 | data := StatisticsMap.GetMapData() | ||
| 34 | b, err := json.Marshal(data) | ||
| 35 | if err != nil { | ||
| 36 | t.Errorf(err.Error()) | ||
| 37 | } | ||
| 38 | |||
| 39 | t.Log(string(b)) | ||
| 31 | } | 40 | } | ... | ... |
-
Please register or sign in to post a comment