cloud-report
Version:
Collects and analyzes cloud resources
83 lines (82 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../../types");
const utils_1 = require("../../../utils");
const base_1 = require("../../base");
class EC2InstancesReservationAnalyzer 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 EC2 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.getCountOfInstancesReservedByInstanceType(allReservedInstances[region]);
for (const instance of regionInstances) {
if (instance.State.Name !== "running") {
continue;
}
const instanceAnalysis = {};
instanceAnalysis.resource = instance;
instanceAnalysis.resourceSummary = {
name: "Instance",
value: `${utils_1.ResourceUtil.getNameByTags(instance)} | ${instance.InstanceId}`,
};
const runningFromDays = utils_1.CommonUtil.daysFrom(instance.LaunchTime);
if (this.getInstancesReservedCount(instanceCountMap, instance.InstanceType) === 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 };
}
getCountOfInstancesReservedByInstanceType(reservedInstances) {
if (!reservedInstances || !reservedInstances.length) {
return {};
}
return reservedInstances.filter((reservedInstance) => {
return reservedInstance.State === "active";
}).reduce((instanceCountMap, reservedInstance) => {
instanceCountMap[reservedInstance.InstanceType] =
instanceCountMap[reservedInstance.InstanceType] || { actual: 0, used: 0 };
instanceCountMap[reservedInstance.InstanceType].actual += reservedInstance.InstanceCount;
return instanceCountMap;
}, {});
}
getInstancesReservedCount(instanceCountMap, instanceType) {
const instanceCountObj = instanceCountMap[instanceType];
if (!instanceCountObj) {
return 0;
}
if (instanceCountObj.actual - instanceCountObj.used > 0) {
instanceCountObj.used++;
return 1;
}
else {
return 0;
}
}
}
exports.EC2InstancesReservationAnalyzer = EC2InstancesReservationAnalyzer;