@@ -176,12 +176,106 @@ class DomainClient {
176
176
177
177
class CosClient extends cos { }
178
178
179
+ class SlsMonitor {
180
+ constructor ( credentials = { } ) {
181
+ this . credentials = credentials
182
+ }
183
+ async request ( data ) {
184
+ return await new TencentCloudClient ( this . credentials , {
185
+ host : 'monitor.tencentcloudapi.com' ,
186
+ path : '/'
187
+ } ) . doCloudApiRequest ( data )
188
+ }
189
+
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');
193
+ const client = new TencentCloudClient ( this . credentials , {
194
+ host : 'monitor.tencentcloudapi.com' ,
195
+ path : '/'
196
+ } )
197
+ const req = {
198
+ Action : 'GetMonitorData' ,
199
+ Version : '2018-07-24' ,
200
+ }
201
+
202
+ const metrics = [ 'Duration' , 'Invocation' , 'Error' , 'ConcurrentExecutions' , 'ConfigMem' , 'FunctionErrorPercentage' , 'Http2xx' , 'Http432' , 'Http433' , 'Http434' , 'Http4xx' , 'Mem' , 'MemDuration' ] ;
203
+
204
+ const result = {
205
+ rangeStart : rangeTime . rangeStart ,
206
+ rangeEnd : rangeTime . rangeEnd ,
207
+ metrics : [ ]
208
+ }
209
+
210
+ const requestHandlers = [ ]
211
+ for ( var i = 0 ; i < metrics . length ; i ++ ) {
212
+ req . Namespace = 'qce/scf_v2' ;
213
+ req . MetricName = metrics [ i ] ;
214
+ req . Period = period ;
215
+ req . StartTime = rangeTime . rangeStart ;
216
+ req . EndTime = rangeTime . rangeEnd ;
217
+ req . Instances = [ {
218
+ Dimensions : [
219
+ {
220
+ Name : 'functionName' ,
221
+ Value : funcName ,
222
+ } ,
223
+ {
224
+ Name : 'version' ,
225
+ Value : version || '$latest' ,
226
+ } ,
227
+ {
228
+ Name : 'namespace' ,
229
+ Value : ns ,
230
+ }
231
+ ]
232
+ } ] ;
233
+ requestHandlers . push ( client . doCloudApiRequest ( req ) )
234
+ }
235
+ return new Promise ( ( resolve , reject ) => {
236
+ 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
+ }
259
+
260
+ } )
261
+ result . metrics . push ( metric )
262
+ }
263
+ resolve ( result )
264
+
265
+ } ) . catch ( ( error ) => {
266
+ reject ( error )
267
+ } )
268
+ } )
269
+ }
270
+ }
271
+
179
272
module . exports = {
180
273
ScfClient,
181
274
TagClient,
182
275
CamClient,
183
276
CnsClient,
184
277
ApigwClient,
185
278
DomainClient,
186
- CosClient
279
+ CosClient,
280
+ SlsMonitor
187
281
}
0 commit comments