Skip to content

Commit e4b9976

Browse files
syzhyugasun
authored andcommitted
feat: add mertics type filter
1 parent e0c9a28 commit e4b9976

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/baas/metrics/index.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class Metrics {
1717
this.timezone = options.timezone || '+08:00'
1818
}
1919

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+
2028
async scfMetrics(startTime, endTime, period) {
2129
const rangeTime = {
2230
rangeStart: startTime,
@@ -46,7 +54,7 @@ class Metrics {
4654
return responses
4755
}
4856

49-
async getDatas(startTime, endTime) {
57+
async getDatas(startTime, endTime, metricsType = Metrics.Type.All) {
5058
startTime = moment(startTime)
5159
endTime = moment(endTime)
5260

@@ -83,23 +91,33 @@ class Metrics {
8391
period = 86400 // day
8492
}
8593

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+
}
101104

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+
}
103121
return response
104122
}
105123

0 commit comments

Comments
 (0)