Skip to content

Commit f77bd76

Browse files
authored
Merge pull request #301 from APIParkLab/feature/liujian-1.8
Feature/liujian 1.8
2 parents cef548c + 00ef4d2 commit f77bd76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3786
-790
lines changed

common/format.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
func FormatCountInt64(count int64) string {
9+
switch {
10+
case count < 1000:
11+
return strconv.FormatInt(count, 10)
12+
case count < 1000000:
13+
return fmt.Sprintf("%.1fK", float64(count)/1000)
14+
case count < 1000000000:
15+
return fmt.Sprintf("%.1fM", float64(count)/1000000)
16+
case count < 1000000000000:
17+
return fmt.Sprintf("%.1fB", float64(count)/1000000000)
18+
default:
19+
return fmt.Sprintf("%.1fT", float64(count)/1000000000000)
20+
}
21+
}
22+
23+
func FormatCountFloat64(count float64) string {
24+
switch {
25+
case count < 1000:
26+
return fmt.Sprintf("%.1f", count)
27+
case count < 1000000:
28+
return fmt.Sprintf("%.1fK", count/1000)
29+
case count < 1000000000:
30+
return fmt.Sprintf("%.1fM", count/1000000)
31+
case count < 1000000000000:
32+
return fmt.Sprintf("%.1fB", count/1000000000)
33+
default:
34+
return fmt.Sprintf("%.1fT", count/1000000000000)
35+
}
36+
}
37+
38+
func FormatTime(t int64) string {
39+
if t < 1000 {
40+
return strconv.FormatInt(t, 10) + "ms"
41+
}
42+
if t < 1000000 {
43+
return fmt.Sprintf("%.1fs", float64(t)/1000)
44+
}
45+
if t < 1000000000 {
46+
return fmt.Sprintf("%.1fmin", float64(t)/1000000)
47+
}
48+
if t < 1000000000000 {
49+
return fmt.Sprintf("%.1fhour", float64(t)/1000000000)
50+
}
51+
return fmt.Sprintf("%.1D", float64(t)/1000000000000)
52+
}
53+
54+
func FormatByte(b int64) string {
55+
const (
56+
KB = 1000
57+
MB = KB * 1000
58+
GB = MB * 1000
59+
TB = GB * 1000
60+
PB = TB * 1000
61+
)
62+
63+
switch {
64+
case b < KB:
65+
return fmt.Sprintf("%dB", b)
66+
case b < MB:
67+
return fmt.Sprintf("%.1fKB", float64(b)/KB)
68+
case b < GB:
69+
return fmt.Sprintf("%.1fMB", float64(b)/MB)
70+
case b < TB:
71+
return fmt.Sprintf("%.1fGB", float64(b)/GB)
72+
case b < PB:
73+
return fmt.Sprintf("%.1fTB", float64(b)/TB)
74+
default:
75+
return fmt.Sprintf("%.1fPB", float64(b)/PB)
76+
}
77+
}

common/interface_to_all.go

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func FmtIntFromInterface(val interface{}) int64 {
2929
return int64(ret)
3030
case int:
3131
return int64(ret)
32+
case float64:
33+
return int64(ret)
3234
default:
3335
return 0
3436
}

controller/monitor/iml.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package monitor
22

33
import (
44
"fmt"
5+
"strconv"
56
"time"
67

78
"github.com/APIParkLab/APIPark/module/monitor"
@@ -17,6 +18,66 @@ type imlMonitorStatisticController struct {
1718
module monitor.IMonitorStatisticModule `autowired:""`
1819
}
1920

21+
func (i *imlMonitorStatisticController) ChartRestOverview(ctx *gin.Context, start string, end string) (*monitor_dto.ChartRestOverview, error) {
22+
s, e, err := formatTime(start, end)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return i.module.RestChartOverview(ctx, "", s, e)
27+
}
28+
29+
func (i *imlMonitorStatisticController) ChartAIOverview(ctx *gin.Context, start string, end string) (*monitor_dto.ChartAIOverview, error) {
30+
s, e, err := formatTime(start, end)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return i.module.AIChartOverview(ctx, "", s, e)
35+
}
36+
37+
func (i *imlMonitorStatisticController) AITopN(ctx *gin.Context, start string, end string, limit string) ([]*monitor_dto.TopN, []*monitor_dto.TopN, error) {
38+
s, e, err := formatTime(start, end)
39+
if err != nil {
40+
return nil, nil, err
41+
}
42+
l, err := strconv.Atoi(limit)
43+
if err != nil {
44+
if limit == "" {
45+
l = 10
46+
} else {
47+
return nil, nil, fmt.Errorf("parse limit %s error: %w", limit, err)
48+
}
49+
}
50+
return i.module.Top(ctx, "", s, e, l, "ai")
51+
}
52+
53+
func formatTime(start string, end string) (int64, int64, error) {
54+
s, err := strconv.ParseInt(start, 10, 64)
55+
if err != nil {
56+
return 0, 0, fmt.Errorf("parse start time %s error: %w", start, err)
57+
}
58+
e, err := strconv.ParseInt(end, 10, 64)
59+
if err != nil {
60+
return 0, 0, fmt.Errorf("parse end time %s error: %w", end, err)
61+
}
62+
return s, e, nil
63+
}
64+
65+
func (i *imlMonitorStatisticController) RestTopN(ctx *gin.Context, start string, end string, limit string) ([]*monitor_dto.TopN, []*monitor_dto.TopN, error) {
66+
s, e, err := formatTime(start, end)
67+
if err != nil {
68+
return nil, nil, err
69+
}
70+
l, err := strconv.Atoi(limit)
71+
if err != nil {
72+
if limit == "" {
73+
l = 10
74+
} else {
75+
return nil, nil, fmt.Errorf("parse limit %s error: %w", limit, err)
76+
}
77+
}
78+
return i.module.Top(ctx, "", s, e, l, "rest")
79+
}
80+
2081
func (i *imlMonitorStatisticController) Statistics(ctx *gin.Context, dataType string, input *monitor_dto.StatisticInput) (interface{}, error) {
2182
switch dataType {
2283
case monitor_dto.DataTypeApi:

controller/monitor/statistic.go

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ type IMonitorStatisticController interface {
2222

2323
InvokeTrendInner(ctx *gin.Context, dataType string, typ string, api string, provider string, subscriber string, input *monitor_dto.CommonInput) (*monitor_dto.MonInvokeCountTrend, string, error)
2424
StatisticsInner(ctx *gin.Context, dataType string, typ string, id string, input *monitor_dto.StatisticInput) (interface{}, error)
25+
26+
ChartRestOverview(ctx *gin.Context, start string, end string) (*monitor_dto.ChartRestOverview, error)
27+
ChartAIOverview(ctx *gin.Context, start string, end string) (*monitor_dto.ChartAIOverview, error)
28+
AITopN(ctx *gin.Context, start string, end string, limit string) ([]*monitor_dto.TopN, []*monitor_dto.TopN, error)
29+
RestTopN(ctx *gin.Context, start string, end string, limit string) ([]*monitor_dto.TopN, []*monitor_dto.TopN, error)
2530
}
2631

2732
type IMonitorConfigController interface {

0 commit comments

Comments
 (0)