refactor: improve Docker container matching with deterministic priority-based logic
Address code review feedback to eliminate ambiguity issues and improve reliability: **Backend Improvements:** - Implement priority-based matching: exact ID → exact name → partial ID - Add input normalization (strip leading slashes, case-insensitive) - Add ambiguity detection with warning logs for debugging - Maintain full backwards compatibility with existing monitors **Frontend Improvements:** - Update validation regex to support leading slashes (/container-name) - Improve error message clarity for user guidance **Resolves Ambiguity Issues:** - Container name "abc" vs Container ID "abc123..." now deterministic - Multiple containers with similar names handled predictably - Case-insensitive matching for better user experience **Priority Order:** 1. Exact full ID match (64-char) - highest priority 2. Exact container name match - case-insensitive 3. Partial ID match - backwards compatibility fallback 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
4693177e4b
commit
c445463c0c
2 files changed
+37
-11
No files matched your search
@@ -130,10 +130,10 @@ const monitorValidation = joi.object({
|
||||
then: joi
|
||||
.string()
|
||||
.trim()
|
||||
.regex(/^[a-z0-9]{64}$/)
|
||||
.regex(/^(\/+)?([a-zA-Z0-9][a-zA-Z0-9_.-]*[a-zA-Z0-9]|[a-zA-Z0-9]+|[a-f0-9]{12,64})$/)
|
||||
.messages({
|
||||
"string.empty": "This field is required.",
|
||||
"string.pattern.base": "Please enter a valid 64-character Docker container ID.",
|
||||
"string.pattern.base": "Please enter a valid container name or ID.",
|
||||
}),
|
||||
otherwise: joi
|
||||
.string()
|
||||
|
||||
@@ -255,16 +255,42 @@ class NetworkService {
|
||||
|
||||
const containers = await docker.listContainers({ all: true });
|
||||
|
||||
// Find container by ID (original behavior) or by name (new feature)
|
||||
let targetContainer = containers.find((c) => {
|
||||
// Check if monitor.url matches container ID (partial match)
|
||||
if (c.Id.startsWith(monitor.url)) {
|
||||
return true;
|
||||
// Normalize input: strip leading slashes and convert to lowercase for comparison
|
||||
const normalizedInput = monitor.url.replace(/^\/+/, "").toLowerCase();
|
||||
|
||||
// Priority-based matching to avoid ambiguity:
|
||||
// 1. Exact full ID match (64-char)
|
||||
let exactIdMatch = containers.find((c) => c.Id.toLowerCase() === normalizedInput);
|
||||
|
||||
// 2. Exact container name match (case-insensitive)
|
||||
let exactNameMatch = containers.find((c) =>
|
||||
c.Names.some((name) => {
|
||||
const cleanName = name.replace(/^\/+/, "").toLowerCase();
|
||||
return cleanName === normalizedInput;
|
||||
})
|
||||
);
|
||||
|
||||
// 3. Partial ID match (fallback for backwards compatibility)
|
||||
let partialIdMatch = containers.find((c) => c.Id.toLowerCase().startsWith(normalizedInput));
|
||||
|
||||
// Select container based on priority
|
||||
let targetContainer = exactIdMatch || exactNameMatch || partialIdMatch;
|
||||
|
||||
// Log warning if ambiguous matches exist
|
||||
if (targetContainer && this.logger) {
|
||||
const matchTypes = [];
|
||||
if (exactIdMatch) matchTypes.push("exact ID");
|
||||
if (exactNameMatch) matchTypes.push("exact name");
|
||||
if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID");
|
||||
|
||||
if (matchTypes.length > 1) {
|
||||
this.logger.warn({
|
||||
message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`,
|
||||
service: this.SERVICE_NAME,
|
||||
method: "requestDocker",
|
||||
});
|
||||
}
|
||||
// Check if monitor.url matches any container name
|
||||
// Container names are stored as ["/container-name"] in the Names array
|
||||
return c.Names.some((name) => name === `/${monitor.url}` || name === monitor.url);
|
||||
});
|
||||
}
|
||||
|
||||
if (!targetContainer) {
|
||||
throw new Error(this.stringService.dockerNotFound);
|
||||
|
||||
Reference in new issue
Block a user