@@ -17,6 +17,14 @@ class Metrics {
17
17
this . timezone = options . timezone || '+08:00'
18
18
}
19
19
20
+ static get Type ( ) {
21
+ return Object . freeze ( {
22
+ Base : 1 , // scf base metrics
23
+ Custom : 2 , // report custom metrics
24
+ All : 0xffffffff
25
+ } )
26
+ }
27
+
20
28
async scfMetrics ( startTime , endTime , period ) {
21
29
const rangeTime = {
22
30
rangeStart : startTime ,
@@ -46,7 +54,7 @@ class Metrics {
46
54
return responses
47
55
}
48
56
49
- async getDatas ( startTime , endTime ) {
57
+ async getDatas ( startTime , endTime , metricsType = Metrics . Type . All ) {
50
58
startTime = moment ( startTime )
51
59
endTime = moment ( endTime )
52
60
@@ -83,23 +91,33 @@ class Metrics {
83
91
period = 86400 // day
84
92
}
85
93
86
- const timeFormat = 'YYYY-MM-DDTHH:mm:ss' + this . timezone
87
- let results = await this . scfMetrics (
88
- startTime . format ( timeFormat ) ,
89
- endTime . format ( timeFormat ) ,
90
- period
91
- )
92
-
93
- const response = this . buildMetrics ( results )
94
-
95
- results = await this . customMetrics (
96
- startTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
97
- endTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
98
- period
99
- )
100
- results = this . buildCustomMetrics ( results )
94
+ let response , results
95
+ if ( metricsType & Metrics . Type . Base ) {
96
+ const timeFormat = 'YYYY-MM-DDTHH:mm:ss' + this . timezone
97
+ results = await this . scfMetrics (
98
+ startTime . format ( timeFormat ) ,
99
+ endTime . format ( timeFormat ) ,
100
+ period
101
+ )
102
+ response = this . buildMetrics ( results )
103
+ }
101
104
102
- response . metrics = response . metrics . concat ( results )
105
+ if ( metricsType & Metrics . Type . Custom ) {
106
+ if ( ! response ) {
107
+ response = {
108
+ rangeStart : startTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
109
+ rangeEnd : endTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
110
+ metrics : [ ]
111
+ }
112
+ }
113
+ results = await this . customMetrics (
114
+ startTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
115
+ endTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
116
+ period
117
+ )
118
+ results = this . buildCustomMetrics ( results )
119
+ response . metrics = response . metrics . concat ( results )
120
+ }
103
121
return response
104
122
}
105
123
0 commit comments