cloud-report
Version:
Collects and analyzes cloud resources
84 lines (83 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../../types");
const utils_1 = require("../../../utils");
const base_1 = require("../../base");
class RDSInstancesReservationAnalyzer extends base_1.BaseAnalyzer {
analyze(params, fullReport) {
const allInstances = params.instances;
const allReservedInstances = params.reserved_instances;
if (!allInstances || !allReservedInstances) {
return undefined;
}
const instances_reserved = { type: types_1.CheckAnalysisType.CostOptimization };
instances_reserved.what = "Are there any long running instances which should be reserved?";
instances_reserved.why = `You can reserve the RDS instance which
are you going to run for long time to save the cost.`;
instances_reserved.recommendation = "Recommended to reserve all long running instances";
instances_reserved.benchmark = ['all'];
const allRegionsAnalysis = {};
for (const region in allInstances) {
const regionInstances = allInstances[region];
allRegionsAnalysis[region] = [];
const instanceCountMap = this.getCountOfInstancesReservedByInstanceClassAndEngine(allReservedInstances[region]);
for (const instance of regionInstances) {
const instanceAnalysis = {};
instanceAnalysis.resource = instance;
instanceAnalysis.resourceSummary = {
name: "DBInstance",
value: instance.DBInstanceIdentifier,
};
const runningFromDays = utils_1.CommonUtil.daysFrom(instance.LaunchTime);
if (this.getInstancesReservedCount(instanceCountMap, instance.DBInstanceClass, instance.Engine) === 1) {
instanceAnalysis.severity = types_1.SeverityStatus.Good;
instanceAnalysis.message = "Instance is reserved";
}
else {
if (runningFromDays > 365) {
instanceAnalysis.severity = types_1.SeverityStatus.Warning;
}
else {
instanceAnalysis.severity = types_1.SeverityStatus.Info;
}
instanceAnalysis.message = `Instance is running from ${runningFromDays} days`;
instanceAnalysis.action = "Reserve the instance to save costs";
}
allRegionsAnalysis[region].push(instanceAnalysis);
}
}
instances_reserved.regions = allRegionsAnalysis;
return { instances_reserved };
}
getCountOfInstancesReservedByInstanceClassAndEngine(reservedInstances) {
if (!reservedInstances || !reservedInstances.length) {
return {};
}
return reservedInstances.filter((reservedInstance) => {
return reservedInstance.State === "active";
}).reduce((instanceCountMap, reservedInstance) => {
instanceCountMap[reservedInstance.DBInstanceClass] =
instanceCountMap[reservedInstance.DBInstanceClass] || {};
instanceCountMap[reservedInstance.DBInstanceClass][reservedInstance.ProductDescription] =
instanceCountMap[reservedInstance.DBInstanceClass][reservedInstance.ProductDescription]
|| { actual: 0, used: 0 };
instanceCountMap[reservedInstance.DBInstanceClass][reservedInstance.ProductDescription].actual
+= reservedInstance.DBInstanceCount;
return instanceCountMap;
}, {});
}
getInstancesReservedCount(instanceCountMap, instanceClass, dbEngine) {
if (!instanceCountMap[instanceClass] || !instanceCountMap[instanceClass][dbEngine]) {
return 0;
}
const instanceCountObj = instanceCountMap[instanceClass][dbEngine];
if (instanceCountObj.actual - instanceCountObj.used > 0) {
instanceCountObj.used++;
return 1;
}
else {
return 0;
}
}
}
exports.RDSInstancesReservationAnalyzer = RDSInstancesReservationAnalyzer;