Skip to content

Commit 9b29a80

Browse files
committed
change get metrics add aggregation response
1 parent f72682d commit 9b29a80

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

src/client.js

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const dotQs = require('dot-qs')
44
const request = require('../lib/request/index')
55
const crypto = require('crypto')
66
const cos = require('../lib/cos/cos')
7+
const util = require('util')
78

89
var defaults = {
910
signatureMethod: 'HmacSHA1',
@@ -180,16 +181,57 @@ class SlsMonitor {
180181
constructor(credentials = {}) {
181182
this.credentials = credentials
182183
}
184+
185+
static rfc3339(t) {
186+
const year = t.getFullYear()
187+
const month = t.getMonth() + 1
188+
const day = t.getDate()
189+
const hours = t.getHours()
190+
const minutes = t.getMinutes()
191+
const sec = t.getSeconds()
192+
193+
const offset = Math.abs(t.getTimezoneOffset())
194+
const offsetH = Math.floor(offset / 60)
195+
const offsetM = offset % 60
196+
197+
return util.format('%d-%s-%sT%s:%s:%s+%s:%s', year, month.toString().padStart(2, 0),
198+
day.toString().padStart(2, 0), hours.toString().padStart(2, 0), minutes.toString().padStart(2, 0), sec.toString().padStart(2, 0), offsetH.toString().padStart(2, 0),
199+
offsetM.toString().padStart(2, 0))
200+
}
201+
183202
async request(data) {
184203
return await new TencentCloudClient(this.credentials, {
185204
host: 'monitor.tencentcloudapi.com',
186205
path: '/'
187206
}).doCloudApiRequest(data)
188207
}
189208

190-
async getScfMetrics(region, rangeTime, period, funcName, ns, version) {
191-
// const cred = new Credential(credentials.secretId, credentials.secretKey);
192-
// const client = new MonitorClient(cred, region || 'ap-guangzhou');
209+
aggregationByDay(responses) {
210+
const len = responses.length
211+
for (var i = 0; i < len; i++) {
212+
const result = responses[i]
213+
const tlen = result.Response.DataPoints[0].Timestamps.length
214+
const values = result.Response.DataPoints[0].Values
215+
let total = values[0]
216+
217+
const newTimes = []
218+
const newValues = []
219+
for (var n = 0; n < tlen; n++) {
220+
if (n > 0 && !(n % 24)) {
221+
newTimes.push(result.Response.DataPoints[0].Timestamps[n])
222+
const v = total / 24
223+
newValues.push(parseFloat((v.toFixed(2)), 10))
224+
total = values[n]
225+
} else {
226+
total += values[n]
227+
}
228+
}
229+
result.Response.DataPoints[0].Timestamps = newTimes
230+
result.Response.DataPoints[0].Values = newValues
231+
}
232+
}
233+
234+
async getScfMetrics(region, rangeTime, funcName, ns, version) {
193235
const client = new TencentCloudClient(this.credentials, {
194236
host: 'monitor.tencentcloudapi.com',
195237
path: '/'
@@ -198,12 +240,32 @@ class SlsMonitor {
198240
Action: 'GetMonitorData',
199241
Version: '2018-07-24',
200242
}
243+
if (rangeTime.rangeEnd <= rangeTime.rangeStart) {
244+
throw new Error('The rangeStart provided is after the rangeEnd')
245+
}
246+
247+
const metrics = ['Duration', 'Invocation', 'Error', 'ConcurrentExecutions', 'ConfigMem', 'FunctionErrorPercentage', 'Http2xx', 'Http432', 'Http433', 'Http434', 'Http4xx', 'Mem', 'MemDuration', 'Syserr'];
201248

202-
const metrics = ['Duration', 'Invocation', 'Error', 'ConcurrentExecutions', 'ConfigMem', 'FunctionErrorPercentage', 'Http2xx', 'Http432', 'Http433', 'Http434', 'Http4xx', 'Mem', 'MemDuration'];
249+
const diffMinutes = (rangeTime.rangeEnd - rangeTime.rangeStart) / 1000 / 60
250+
let period,
251+
aggrFlag = false
203252

253+
if (diffMinutes <= 16) {
254+
// 16 mins
255+
period = 60 // 1 min
256+
} else if (diffMinutes <= 61) {
257+
// 1 hour
258+
period = 300 // 5 mins
259+
} else if (diffMinutes <= 1500) {
260+
// 24 hours
261+
period = 3600 // hour
262+
} else {
263+
period = 3600 // day
264+
aggrFlag = true
265+
}
204266
const result = {
205-
rangeStart: rangeTime.rangeStart,
206-
rangeEnd: rangeTime.rangeEnd,
267+
rangeStart: SlsMonitor.rfc3339(rangeTime.rangeStart),
268+
rangeEnd: SlsMonitor.rfc3339(rangeTime.rangeEnd),
207269
metrics: []
208270
}
209271

@@ -212,8 +274,8 @@ class SlsMonitor {
212274
req.Namespace = 'qce/scf_v2';
213275
req.MetricName = metrics[i];
214276
req.Period = period;
215-
req.StartTime = rangeTime.rangeStart;
216-
req.EndTime = rangeTime.rangeEnd;
277+
req.StartTime = SlsMonitor.rfc3339(rangeTime.rangeStart);
278+
req.EndTime = SlsMonitor.rfc3339(rangeTime.rangeEnd);
217279
req.Instances = [{
218280
Dimensions: [
219281
{
@@ -234,34 +296,10 @@ class SlsMonitor {
234296
}
235297
return new Promise((resolve, reject)=> {
236298
Promise.all(requestHandlers).then((results) => {
237-
for (var i = 0; i < results.length; i++) {
238-
const response = results[i].Response;
239-
const metric = {
240-
type: response.MetricName,
241-
title: response.MetricName,
242-
values: [],
243-
total: 0
244-
}
245-
246-
response.DataPoints[0].Timestamps.forEach((val, i) => {
247-
if (!metric.values[i]) {
248-
metric.values[i] = {
249-
timestamp: val
250-
}
251-
} else {
252-
metric.values[i].timestamp = val
253-
}
254-
255-
if (response.DataPoints[0].Values[i] != undefined) {
256-
metric.values[i].value = response.DataPoints[0].Values[i]
257-
metric.total = Math.round(metric.total + metric.values[i].value)
258-
}
299+
if (aggrFlag)
300+
this.aggregationByDay(results)
259301

260-
})
261-
result.metrics.push(metric)
262-
}
263-
resolve(result)
264-
302+
resolve(results)
265303
}).catch((error) => {
266304
reject(error)
267305
})

0 commit comments

Comments
 (0)