@@ -226,30 +226,30 @@ class SlsMonitor {
226
226
const len = datas . length
227
227
const newValues = [ ]
228
228
229
- const val = {
230
- Timestamp : 0 ,
231
- Value : 0
232
- }
229
+ let val = 0
233
230
for ( var i = 0 ; i < len ; i ++ ) {
234
231
const item = datas [ i ]
235
232
if ( i > 0 && ! ( ( i + 1 ) % period ) ) {
236
- let v = val . Value + item . Value
233
+ let v = val + item . Value
237
234
if ( ! ( ~ ~ v == v ) ) {
238
235
v = parseFloat ( v . toFixed ( 2 ) , 10 )
239
236
}
240
237
newValues . push ( {
241
- Timestamp : val . Timestamp ,
238
+ Timestamp : datas [ i + 1 - period ] . Timestamp ,
242
239
Value : v
243
240
} )
244
- val . Timestamp = 0
245
- val . Value = 0
241
+ val = 0
246
242
} else {
247
- if ( val . Timestamp == 0 ) {
248
- val . Timestamp = item . Timestamp
249
- }
250
- val . Value += item . Value
243
+ val += item . Value
251
244
}
252
245
}
246
+
247
+ if ( len % period ) {
248
+ newValues . push ( {
249
+ Timestamp : datas [ len - ( len % period ) ] . Timestamp ,
250
+ Value : val
251
+ } )
252
+ }
253
253
return newValues
254
254
}
255
255
@@ -301,7 +301,7 @@ class SlsMonitor {
301
301
302
302
const threshold = dstPeriod / srcPeriod
303
303
const len = responses . length
304
- let times = [ ]
304
+ const times = [ ]
305
305
for ( var i = 0 ; i < len ; i ++ ) {
306
306
const result = responses [ i ]
307
307
if ( result . Response . Error ) {
@@ -313,14 +313,6 @@ class SlsMonitor {
313
313
continue
314
314
}
315
315
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
-
324
316
const tlen = result . Response . DataPoints [ 0 ] . Timestamps . length
325
317
const values = result . Response . DataPoints [ 0 ] . Values
326
318
let total = [ ]
@@ -336,28 +328,20 @@ class SlsMonitor {
336
328
total . sort ( ( v1 , v2 ) => {
337
329
return v1 - v2
338
330
} )
339
- // times.push(timestamp )
331
+ times . push ( result . Response . DataPoints [ 0 ] . Timestamps [ n + 1 - threshold ] )
340
332
p95 . push ( this . percentile ( total , 95 ) )
341
333
p50 . push ( this . percentile ( total , 50 ) )
342
- // timestamp = 0
343
334
total = [ ]
344
335
} else {
345
336
total . push ( values [ n ] )
346
- // if (timestamp == 0)
347
- // timestamp = result.Response.DataPoints[0].Timestamps[n]
348
337
}
349
338
}
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 ) ] )
356
343
}
357
344
358
- // if (timestamp != 0)
359
- // times.push(timestamp)
360
-
361
345
result . Response . MetricName = 'Duration-P50'
362
346
result . Response . DataPoints [ 0 ] . Timestamps = times
363
347
result . Response . DataPoints [ 0 ] . Values = p50
@@ -374,44 +358,6 @@ class SlsMonitor {
374
358
}
375
359
}
376
360
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
-
415
361
aggrLatencyP ( datas , srcPeriod , dstPeriod ) {
416
362
if ( srcPeriod == dstPeriod || srcPeriod > dstPeriod ) {
417
363
return
@@ -590,7 +536,6 @@ class SlsMonitor {
590
536
let responses = [ ]
591
537
let results
592
538
let firstRequestFlag = true
593
-
594
539
const attributes = await this . describeAttributes ( 0 , 200 )
595
540
for ( var i = 0 ; i < attributes . Response . Data . TotalCount ; i ++ ) {
596
541
const metricAttribute = attributes . Response . Data . Data [ i ]
@@ -626,7 +571,6 @@ class SlsMonitor {
626
571
if ( ! firstRequestFlag ) {
627
572
await SlsMonitor . sleep ( 1000 )
628
573
}
629
-
630
574
results = await getMetricsResponse ( requestHandlers )
631
575
responses = responses . concat ( results )
632
576
console . log ( 'getCustomMetrics' , JSON . stringify ( responses ) )
@@ -652,24 +596,12 @@ class SlsMonitor {
652
596
653
597
const metrics = [ 'Invocation' , 'Error' , 'Duration' ]
654
598
655
- let durationPeriod
656
-
599
+ const reqPeriod = 60
657
600
const requestHandlers = [ ]
658
601
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
- }
670
602
req . Namespace = 'qce/scf_v2'
671
603
req . MetricName = metrics [ i ]
672
-
604
+ req . Period = reqPeriod
673
605
req . StartTime = rangeTime . rangeStart
674
606
req . EndTime = rangeTime . rangeEnd
675
607
req . Instances = [
@@ -696,7 +628,7 @@ class SlsMonitor {
696
628
Promise . all ( requestHandlers )
697
629
. then ( ( results ) => {
698
630
console . log ( 'getScfMetrics' , JSON . stringify ( results ) )
699
- this . aggrDurationP ( results , durationPeriod , period )
631
+ this . aggrDatas ( results , reqPeriod , period )
700
632
resolve ( results )
701
633
} )
702
634
. catch ( ( error ) => {
@@ -705,6 +637,165 @@ class SlsMonitor {
705
637
} )
706
638
}
707
639
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
+
708
799
async createService ( ) {
709
800
const client = new TencentCloudClient ( this . credentials , {
710
801
host : 'monitor.tencentcloudapi.com' ,
0 commit comments