ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
116 lines (115 loc) • 4.33 kB
JavaScript
async function checkTaskEnvironmentVariables(params) {
const { task, rdsInstance } = params;
const taskName = task.displayName.toLowerCase();
const serviceName = task.serviceName.toLowerCase();
const rdsIdentifier = rdsInstance.dbInstanceIdentifier.toLowerCase();
const rdsSegments = rdsIdentifier.split("-").filter((s) => s.length > 2);
const basicMatches = [
{
condition: taskName.includes(rdsIdentifier),
score: 40,
detail: "Task name contains RDS identifier",
},
{
condition: serviceName.includes(rdsIdentifier),
score: 40,
detail: "Service name contains RDS identifier",
},
];
const segmentMatches = rdsSegments.flatMap((segment) => [
{
condition: taskName.includes(segment),
score: 15,
detail: `Task name segment match: ${segment}`,
},
{
condition: serviceName.includes(segment),
score: 15,
detail: `Service name segment match: ${segment}`,
},
]);
const allMatches = [...basicMatches, ...segmentMatches];
const positiveMatches = allMatches.filter((match) => match.condition);
const totalScore = positiveMatches.reduce((sum, match) => sum + match.score, 0);
const matchDetails = positiveMatches.map((match) => match.detail);
return {
hasMatch: totalScore > 20,
score: totalScore,
matchDetails,
};
}
export async function scoreTasksByNaming(params) {
const { tasks, cluster, rdsInstance } = params;
const rdsName = rdsInstance.dbInstanceIdentifier.toLowerCase();
const rdsSegments = rdsName.split("-").filter((s) => s.length > 2);
return tasks.map((task) => {
const taskName = task.displayName.toLowerCase();
const serviceName = task.serviceName.toLowerCase();
const scoreCalculations = [
{
condition: taskName.includes(rdsName),
score: 35,
reason: "完全名前一致",
},
{
condition: serviceName.includes(rdsName),
score: 30,
reason: "サービス名一致",
},
];
const baseResults = scoreCalculations.filter((calc) => calc.condition);
const baseScore = baseResults.reduce((total, calc) => total + calc.score, 0);
const segmentResults = rdsSegments
.flatMap((segment) => [
{
condition: taskName.includes(segment),
score: 20,
reason: `セグメント一致: ${segment}`,
},
{
condition: serviceName.includes(segment),
score: 15,
reason: `サービスセグメント一致: ${segment}`,
},
])
.filter((calc) => calc.condition);
const segmentScore = segmentResults.reduce((total, calc) => total + calc.score, 0);
const totalScore = 25 + baseScore + segmentScore;
const confidence = totalScore >= 75 ? "high" : totalScore >= 50 ? "medium" : "low";
return {
cluster,
task,
confidence,
method: "naming",
score: totalScore,
reason: "名前類似性関連",
};
});
}
export async function scoreTasksAgainstRDS(params) {
const { ecsClient, tasks, cluster, rdsInstance } = params;
const envCheckPromises = tasks.map(async (task) => {
const envCheck = await checkTaskEnvironmentVariables({
ecsClient,
task,
rdsInstance,
});
return { task, envCheck };
});
const envCheckResults = await Promise.all(envCheckPromises);
const envResults = envCheckResults
.filter(({ envCheck }) => envCheck.hasMatch)
.map(({ task, envCheck }) => {
const confidence = envCheck.score >= 80 ? "high" : envCheck.score >= 50 ? "medium" : "low";
return {
cluster,
task,
confidence,
method: "environment",
score: envCheck.score,
reason: "データベース接続関連",
};
});
const analysisMatchResults = [];
return [...envResults, ...analysisMatchResults];
}