Skip to content

Commit 80710a1

Browse files
committed
fix: scf metrics response discontinuous
1 parent 0804fdb commit 80710a1

File tree

1 file changed

+180
-89
lines changed

1 file changed

+180
-89
lines changed

src/client.js

Lines changed: 180 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -226,30 +226,30 @@ class SlsMonitor {
226226
const len = datas.length
227227
const newValues = []
228228

229-
const val = {
230-
Timestamp: 0,
231-
Value: 0
232-
}
229+
let val = 0
233230
for (var i = 0; i < len; i++) {
234231
const item = datas[i]
235232
if (i > 0 && !((i + 1) % period)) {
236-
let v = val.Value + item.Value
233+
let v = val + item.Value
237234
if (!(~~v == v)) {
238235
v = parseFloat(v.toFixed(2), 10)
239236
}
240237
newValues.push({
241-
Timestamp: val.Timestamp,
238+
Timestamp: datas[i + 1 - period].Timestamp,
242239
Value: v
243240
})
244-
val.Timestamp = 0
245-
val.Value = 0
241+
val = 0
246242
} else {
247-
if (val.Timestamp == 0) {
248-
val.Timestamp = item.Timestamp
249-
}
250-
val.Value += item.Value
243+
val += item.Value
251244
}
252245
}
246+
247+
if (len % period) {
248+
newValues.push({
249+
Timestamp: datas[len - (len % period)].Timestamp,
250+
Value: val
251+
})
252+
}
253253
return newValues
254254
}
255255

@@ -301,7 +301,7 @@ class SlsMonitor {
301301

302302
const threshold = dstPeriod / srcPeriod
303303
const len = responses.length
304-
let times = []
304+
const times = []
305305
for (var i = 0; i < len; i++) {
306306
const result = responses[i]
307307
if (result.Response.Error) {
@@ -313,14 +313,6 @@ class SlsMonitor {
313313
continue
314314
}
315315

316-
if (i == 0) {
317-
times = responses[i + 1].Response.DataPoints[0].Timestamps
318-
} else if (i == len - 1) {
319-
times = responses[i - 1].Response.DataPoints[0].Timestamps
320-
} else {
321-
times = responses[i + 1].Response.DataPoints[0].Timestamps
322-
}
323-
324316
const tlen = result.Response.DataPoints[0].Timestamps.length
325317
const values = result.Response.DataPoints[0].Values
326318
let total = []
@@ -336,28 +328,20 @@ class SlsMonitor {
336328
total.sort((v1, v2) => {
337329
return v1 - v2
338330
})
339-
// times.push(timestamp)
331+
times.push(result.Response.DataPoints[0].Timestamps[n + 1 - threshold])
340332
p95.push(this.percentile(total, 95))
341333
p50.push(this.percentile(total, 50))
342-
// timestamp = 0
343334
total = []
344335
} else {
345336
total.push(values[n])
346-
// if (timestamp == 0)
347-
// timestamp = result.Response.DataPoints[0].Timestamps[n]
348337
}
349338
}
350-
if (tlen < threshold) {
351-
if (total.length > 0) {
352-
p95.push(this.percentile(total, 95))
353-
p50.push(this.percentile(total, 50))
354-
}
355-
times = [result.Response.DataPoints[0].Timestamps[n - 1]]
339+
if (total.length > 0) {
340+
p95.push(this.percentile(total, 95))
341+
p50.push(this.percentile(total, 50))
342+
times.push(result.Response.DataPoints[0].Timestamps[tlen - (tlen % threshold)])
356343
}
357344

358-
// if (timestamp != 0)
359-
// times.push(timestamp)
360-
361345
result.Response.MetricName = 'Duration-P50'
362346
result.Response.DataPoints[0].Timestamps = times
363347
result.Response.DataPoints[0].Values = p50
@@ -374,44 +358,6 @@ class SlsMonitor {
374358
}
375359
}
376360

377-
aggregationByDay(responses) {
378-
const len = responses.length
379-
for (var i = 0; i < len; i++) {
380-
const result = responses[i]
381-
if (result.Response.MetricName.match('Duration')) {
382-
continue
383-
}
384-
385-
const tlen = result.Response.DataPoints[0].Timestamps.length
386-
const values = result.Response.DataPoints[0].Values
387-
388-
let total = 0
389-
let timestamp = 0
390-
const newTimes = []
391-
const newValues = []
392-
393-
for (var n = 0; n < tlen; n++) {
394-
if (n > 0 && !((n + 1) % 24)) {
395-
newTimes.push(timestamp)
396-
timestamp = 0
397-
let v = (total + result.Response.DataPoints[0].Values[n]) / 24
398-
if (!(~~v == v)) {
399-
v = parseFloat(v.toFixed(2), 10)
400-
}
401-
newValues.push(v)
402-
total = values[n]
403-
} else {
404-
total += values[n]
405-
if (timestamp == 0) {
406-
timestamp = result.Response.DataPoints[0].Timestamps[n]
407-
}
408-
}
409-
}
410-
result.Response.DataPoints[0].Timestamps = newTimes
411-
result.Response.DataPoints[0].Values = newValues
412-
}
413-
}
414-
415361
aggrLatencyP(datas, srcPeriod, dstPeriod) {
416362
if (srcPeriod == dstPeriod || srcPeriod > dstPeriod) {
417363
return
@@ -590,7 +536,6 @@ class SlsMonitor {
590536
let responses = []
591537
let results
592538
let firstRequestFlag = true
593-
594539
const attributes = await this.describeAttributes(0, 200)
595540
for (var i = 0; i < attributes.Response.Data.TotalCount; i++) {
596541
const metricAttribute = attributes.Response.Data.Data[i]
@@ -626,7 +571,6 @@ class SlsMonitor {
626571
if (!firstRequestFlag) {
627572
await SlsMonitor.sleep(1000)
628573
}
629-
630574
results = await getMetricsResponse(requestHandlers)
631575
responses = responses.concat(results)
632576
console.log('getCustomMetrics', JSON.stringify(responses))
@@ -652,24 +596,12 @@ class SlsMonitor {
652596

653597
const metrics = ['Invocation', 'Error', 'Duration']
654598

655-
let durationPeriod
656-
599+
const reqPeriod = 60
657600
const requestHandlers = []
658601
for (var i = 0; i < metrics.length; i++) {
659-
if (metrics[i] == 'Duration') {
660-
if (period == 3600) {
661-
req.Period = durationPeriod = 60
662-
} else if (period == 86400) {
663-
req.Period = durationPeriod = 3600
664-
} else {
665-
req.Period = durationPeriod = 60
666-
}
667-
} else {
668-
req.Period = period
669-
}
670602
req.Namespace = 'qce/scf_v2'
671603
req.MetricName = metrics[i]
672-
604+
req.Period = reqPeriod
673605
req.StartTime = rangeTime.rangeStart
674606
req.EndTime = rangeTime.rangeEnd
675607
req.Instances = [
@@ -696,7 +628,7 @@ class SlsMonitor {
696628
Promise.all(requestHandlers)
697629
.then((results) => {
698630
console.log('getScfMetrics', JSON.stringify(results))
699-
this.aggrDurationP(results, durationPeriod, period)
631+
this.aggrDatas(results, reqPeriod, period)
700632
resolve(results)
701633
})
702634
.catch((error) => {
@@ -705,6 +637,165 @@ class SlsMonitor {
705637
})
706638
}
707639

640+
// scf response timestamp data discontinuous
641+
padContent(dataPoints, period) {
642+
const times = []
643+
const values = []
644+
645+
const len = dataPoints.Timestamps.length
646+
for (var i = 0; i < len; i++) {
647+
let timestamp = dataPoints.Timestamps[i]
648+
const value = dataPoints.Values[i]
649+
times.push(timestamp)
650+
values.push(value)
651+
652+
if (i < len - 1) {
653+
const nextTimestamp = dataPoints.Timestamps[i + 1]
654+
while (nextTimestamp - timestamp > period) {
655+
timestamp += period
656+
times.push(timestamp)
657+
values.push(0)
658+
}
659+
}
660+
}
661+
return {
662+
times: times,
663+
values: values
664+
}
665+
}
666+
667+
mergeByPeriod(datas, srcPeriod, dstPeriod) {
668+
if (srcPeriod == dstPeriod || srcPeriod > dstPeriod) {
669+
return null
670+
}
671+
672+
const tlen = datas.Timestamps.length
673+
const period = dstPeriod / srcPeriod
674+
const values = []
675+
const times = []
676+
if (tlen == 0) {
677+
return null
678+
}
679+
680+
let val = 0
681+
for (var n = 0; n < tlen; n++) {
682+
if (n > 0 && !((n + 1) % period)) {
683+
let v = val + datas.Values[n]
684+
if (!(~~v == v)) {
685+
v = parseFloat(v.toFixed(2), 10)
686+
}
687+
times.push(datas.Timestamps[n + 1 - period])
688+
values.push(v)
689+
val = 0
690+
} else {
691+
val += datas.Values[n]
692+
}
693+
}
694+
695+
if (tlen % period) {
696+
times.push(datas.Timestamps[tlen - (tlen % period)])
697+
values.push(val)
698+
}
699+
return {
700+
times: times,
701+
values: values
702+
}
703+
}
704+
705+
padPart(startTime, endTime, period) {
706+
const padTimes = []
707+
const padValues = []
708+
709+
while (startTime < endTime) {
710+
padTimes.push(startTime)
711+
padValues.push(0)
712+
startTime += period
713+
}
714+
return { timestamp: padTimes, values: padValues }
715+
}
716+
717+
aggrDatas(responses, srcPeriod, dstPeriod) {
718+
const len = responses.length
719+
720+
let startTime, endTime, startTimestamp, endTimestamp
721+
for (var i = 0; i < len; i++) {
722+
const response = responses[i].Response
723+
if (response.Error) {
724+
console.log(JSON.stringify(response.Error), response.RequestId)
725+
continue
726+
}
727+
if (response.DataPoints[0].Timestamps.length == 0) {
728+
continue
729+
}
730+
const dataPoints = this.padContent(response.DataPoints[0], srcPeriod)
731+
response.DataPoints[0].Timestamps = dataPoints.times
732+
response.DataPoints[0].Values = dataPoints.values
733+
734+
// response timestamp is tz +08:00
735+
startTime = new Date(response.StartTime)
736+
endTime = new Date(response.EndTime)
737+
738+
let offset = 0
739+
if (startTime.getTimezoneOffset() == 0) {
740+
offset = 8 * 60 * 60
741+
}
742+
startTimestamp = startTime.getTime() / 1000 - offset
743+
endTimestamp = endTime.getTime() / 1000 - offset
744+
745+
const startPads = this.padPart(
746+
startTimestamp,
747+
response.DataPoints[0].Timestamps[0],
748+
response.Period
749+
)
750+
if (startPads.timestamp.length > 0) {
751+
response.DataPoints[0].Timestamps = startPads.timestamp.concat(
752+
response.DataPoints[0].Timestamps
753+
)
754+
}
755+
if (startPads.values.length > 0) {
756+
response.DataPoints[0].Values = startPads.values.concat(response.DataPoints[0].Values)
757+
}
758+
759+
const endPads = this.padPart(
760+
response.DataPoints[0].Timestamps[response.DataPoints[0].Timestamps.length - 1],
761+
endTimestamp + response.Period,
762+
response.Period
763+
)
764+
if (endPads.timestamp.length > 0) {
765+
endPads.timestamp.shift()
766+
response.DataPoints[0].Timestamps = response.DataPoints[0].Timestamps.concat(
767+
endPads.timestamp
768+
)
769+
}
770+
if (endPads.values.length > 0) {
771+
endPads.values.shift()
772+
response.DataPoints[0].Values = response.DataPoints[0].Values.concat(endPads.values)
773+
}
774+
775+
if (response.MetricName == 'Duration') {
776+
this.aggrDurationP(responses, srcPeriod, dstPeriod)
777+
continue
778+
}
779+
780+
let result
781+
switch (dstPeriod) {
782+
case 300:
783+
result = this.mergeByPeriod(response.DataPoints[0], srcPeriod, dstPeriod)
784+
break
785+
case 3600:
786+
result = this.mergeByPeriod(response.DataPoints[0], srcPeriod, dstPeriod)
787+
break
788+
case 86400:
789+
result = this.mergeByPeriod(response.DataPoints[0], srcPeriod, dstPeriod)
790+
break
791+
}
792+
if (result) {
793+
response.DataPoints[0].Timestamps = result.times
794+
response.DataPoints[0].Values = result.values
795+
}
796+
}
797+
}
798+
708799
async createService() {
709800
const client = new TencentCloudClient(this.credentials, {
710801
host: 'monitor.tencentcloudapi.com',

0 commit comments

Comments
 (0)