UNPKG

tencentcloud-edgeone-migration-nodejs-v2

Version:

tencentcloud cdn config copy to edgeone

100 lines (94 loc) 3.38 kB
const logger = require('../logger'); const { t } = require("../../i18n/trans"); const { TEO } = require('../utils'); const genLog = require("../../logGenerator"); const capi = require('../api') async function BandwidthAlert(zoneId, domain, domainConfig, report) { const keyConfig = domainConfig.BandwidthAlert; if (!keyConfig || !keyConfig.StatisticItems || keyConfig.StatisticItems.length === 0) { return null; } const { StatisticItems } = keyConfig; const fluxRules = StatisticItems.filter(rule => { return rule.Metric === 'flux'; }); const httpsRules = StatisticItems.filter(rule => { return rule.Metric === 'https'; }); if (fluxRules.length > 1 || httpsRules.length > 1) { report.Detail = t(`{{detail}}检测到 CDN 存在多条封顶类型相同的规则,但 EO 相同封顶类型的规则,只能创建一条; `, {detail: report.Detail}); genLog.errorLog(t('检测到 CDN 存在多条封顶类型相同的规则,但 EO 相同封顶类型的规则,只能创建一条')); return null; } if (StatisticItems.find(rule => { return rule.Cycle === 43200; })) { report.Detail = t(`{{detail}}EO 不支持周期为自然月的规则迁移; `, {detail: report.Detail}); genLog.errorLog(t('EO 不支持周期为自然月的规则迁移')); return null; } if (StatisticItems.find(rule => { return rule.Metric === 'bandwidth'; })) { report.Detail = t(`{{detail}}EO 不支持封顶类型为 带宽封顶; `, {detail:report.Detail}); genLog.errorLog(t('EO 不支持封顶类型为 带宽封顶')); return null; } for (rule of StatisticItems) { let param = { 'ZoneId': zoneId, 'Scope': 'domain_flow', 'Domains': [domain], 'CappingOperation': 'offline', 'Action': 'CreateUsageCappingStrategy', 'Period': rule.Cycle === 5 ? '5min' : rule.Cycle === 1440 ? 'day' : 'hour', 'AlarmThreshold': rule.AlertPercentage, }; if (rule.Metric === 'flux') { param['UsageType'] = 'flow'; let threshold = rule.BpsThreshold; let unit = 'MB'; if (threshold >= 1000000000000) { threshold = Math.floor(threshold / 1000000000000); unit = 'TB'; } else if (threshold >= 1000000000) { threshold = Math.floor(threshold / 1000000000); unit = 'GB'; } else { threshold = Math.floor(threshold / 1000000); unit = 'MC'; } param['CappingThreshold'] = threshold; param['CappingUnit'] = unit; } else { let threshold = rule.BpsThreshold; let unit = '10_thousand_times'; if (threshold >= 100000000) { threshold = Math.floor(threshold / 100000000); unit = '100_million_times'; } else if (threshold >= 1000000) { threshold = Math.floor(threshold / 1000000); unit = '1_million_times'; } else { threshold = Math.floor(threshold / 10000); unit = '10_thousand_times'; } param['CappingThreshold'] = threshold; param['CappingUnit'] = unit; } try { await capi("CreateUsageCappingStrategy", TEO, param) } catch (e) { report.Detail = t(`{{detail}} 用量封顶配置迁移失败:{{result}}; `, {detail: report.Detail, result: e.toString()}); genLog.errorLog(e) } } return null; } module.exports = BandwidthAlert;