|
1 | 1 | const { Component } = require('@serverless/core')
|
2 |
| -const { MultiApigw, Scf, Apigw, Cos, Cns, Cam } = require('tencent-component-toolkit') |
3 |
| -const moment = require('moment') |
4 |
| -const util = require('util') |
5 |
| -const { slsMonitor } = require('tencent-cloud-sdk') |
6 |
| - |
7 |
| -const { |
8 |
| - packageCode, |
9 |
| - getDefaultProtocol, |
10 |
| - deleteRecord, |
11 |
| - prepareInputs, |
12 |
| - buildMetrics, |
13 |
| - buildCustomMetrics |
14 |
| -} = require('./utils') |
| 2 | +const { MultiApigw, Scf, Apigw, Cos, Cns, Cam, Metrics } = require('tencent-component-toolkit') |
| 3 | +const { packageCode, getDefaultProtocol, deleteRecord, prepareInputs } = require('./utils') |
15 | 4 | const CONFIGS = require('./config')
|
16 | 5 |
|
17 | 6 | class ServerlessComponent extends Component {
|
@@ -305,102 +294,23 @@ class ServerlessComponent extends Component {
|
305 | 294 | if (!inputs.rangeStart || !inputs.rangeEnd) {
|
306 | 295 | throw new Error('rangeStart and rangeEnd are require inputs')
|
307 | 296 | }
|
308 |
| - inputs.rangeStart = moment(inputs.rangeStart) |
309 |
| - inputs.rangeEnd = moment(inputs.rangeEnd) |
310 |
| - |
311 |
| - if (inputs.rangeStart.isAfter(inputs.rangeEnd)) { |
312 |
| - throw new Error(`The rangeStart provided is after the rangeEnd`) |
313 |
| - } |
314 |
| - |
315 |
| - // Validate: End is not longer than 30 days |
316 |
| - if (inputs.rangeStart.diff(inputs.rangeEnd, 'days') >= 31) { |
317 |
| - throw new Error( |
318 |
| - `The range cannot be longer than 30 days. The supplied range is: ${inputs.rangeStart.diff( |
319 |
| - inputs.rangeEnd, |
320 |
| - 'days' |
321 |
| - )}` |
322 |
| - ) |
323 |
| - } |
324 |
| - |
325 |
| - const diffMinutes = (inputs.rangeEnd - inputs.rangeStart) / 1000 / 60 |
326 |
| - let period |
327 |
| - if (diffMinutes <= 16) { |
328 |
| - // 16 mins |
329 |
| - period = 60 // 1 min |
330 |
| - } else if (diffMinutes <= 61) { |
331 |
| - // 1 hour |
332 |
| - period = 300 // 5 mins |
333 |
| - } else if (diffMinutes <= 1500) { |
334 |
| - // 24 hours |
335 |
| - period = 3600 // hour |
336 |
| - } else { |
337 |
| - period = 86400 // day |
338 |
| - } |
339 |
| - |
340 |
| - const credentials = this.getCredentials() |
341 |
| - const slsClient = new slsMonitor(credentials) |
342 |
| - |
343 |
| - let timeFormat = 'YYYY-MM-DDTHH:mm:ssZ' |
344 |
| - if (inputs.tz) { |
345 |
| - timeFormat = 'YYYY-MM-DDTHH:mm:ss' + inputs.tz |
346 |
| - } |
347 |
| - |
348 |
| - const rangeTime = { |
349 |
| - rangeStart: inputs.rangeStart.format(timeFormat), |
350 |
| - rangeEnd: inputs.rangeEnd.format(timeFormat) |
351 |
| - } |
352 |
| - |
353 | 297 | let functionName, namespace, functionVersion
|
354 | 298 | if (this.state[this.state.region] && this.state[this.state.region].functionName) {
|
355 | 299 | ;({ functionName, namespace, functionVersion } = this.state[this.state.region])
|
356 | 300 | } else {
|
357 | 301 | throw new Error('function name not define')
|
358 | 302 | }
|
359 | 303 |
|
360 |
| - console.log( |
361 |
| - 'getScfMetrics params>>', |
362 |
| - this.state.region, |
363 |
| - rangeTime, |
364 |
| - period, |
365 |
| - functionName, |
366 |
| - namespace |
367 |
| - ) |
368 |
| - const responses = await slsClient.getScfMetrics( |
369 |
| - this.state.region, |
370 |
| - rangeTime, |
371 |
| - period, |
372 |
| - functionName, |
373 |
| - namespace || 'default' |
374 |
| - ) |
375 |
| - console.log('getScf>>>', JSON.stringify(responses)) |
376 |
| - const metricResults = buildMetrics(responses, period) |
377 |
| - |
378 |
| - const reqCustomTime = { |
379 |
| - rangeStart: inputs.rangeStart.format('YYYY-MM-DD HH:mm:ss'), |
380 |
| - rangeEnd: inputs.rangeEnd.format('YYYY-MM-DD HH:mm:ss') |
381 |
| - } |
382 |
| - |
383 |
| - const instances = [ |
384 |
| - util.format('%s|%s|%s', namespace || 'default', functionName, functionVersion || '$LATEST') |
385 |
| - ] |
386 |
| - console.log('customMetrics params>>', this.state.region, instances, reqCustomTime, period) |
387 |
| - const customMetrics = await slsClient.getCustomMetrics( |
388 |
| - this.state.region, |
389 |
| - instances, |
390 |
| - reqCustomTime, |
391 |
| - period |
392 |
| - ) |
393 |
| - console.log('customMetrics>>>', JSON.stringify(customMetrics)) |
394 |
| - |
395 |
| - const customResults = buildCustomMetrics(customMetrics) |
396 |
| - metricResults.metrics = metricResults.metrics.concat(customResults) |
397 |
| - |
398 |
| - if (!metricResults.rangeStart) { |
399 |
| - metricResults.rangeStart = reqCustomTime.rangeStart |
400 |
| - } |
401 |
| - if (!metricResults.rangeEnd) { |
402 |
| - metricResults.rangeEnd = reqCustomTime.rangeEnd |
| 304 | + const options = { |
| 305 | + funcName: functionName, |
| 306 | + namespace: namespace, |
| 307 | + version: functionVersion, |
| 308 | + region: this.state.region, |
| 309 | + timezone: inputs.tz |
403 | 310 | }
|
| 311 | + const credentials = this.getCredentials() |
| 312 | + const mertics = new Metrics(credentials, options) |
| 313 | + const metricResults = await mertics.getDatas(inputs.rangeStart, inputs.rangeEnd) |
404 | 314 | return metricResults
|
405 | 315 | }
|
406 | 316 | }
|
|
0 commit comments