Skip to content

Modern Docker Management, Designed for Everyone

fix: agents not showing compose projects in table

Kyle Mendell0ab2757c47b0
diff --git a/src/lib/services/database/agent-db-service.ts b/src/lib/services/database/agent-db-service.ts
index 6da0fe24d..4895116e8 100644
--- a/src/lib/services/database/agent-db-service.ts
+++ b/src/lib/services/database/agent-db-service.ts
@@ -6,6 +6,28 @@ import { db } from '../../../db';
/**
* Convert database agent to Agent type
*/
+// Helper function to safely convert timestamp
+const safeTimestamp = (timestamp: any, fallback: Date = new Date()): string => {
+ if (!timestamp || timestamp === null || timestamp === undefined) {
+ return fallback.toISOString();
+ }
+
+ // If it's already a string (ISO format), return it
+ if (typeof timestamp === 'string') {
+ const date = new Date(timestamp);
+ return isNaN(date.getTime()) ? fallback.toISOString() : date.toISOString();
+ }
+
+ // If it's a number, treat as Unix timestamp in seconds
+ if (typeof timestamp === 'number' && !isNaN(timestamp)) {
+ // Check if it's in milliseconds (greater than year 2000 in seconds)
+ const date = timestamp > 946684800 ? new Date(timestamp * 1000) : new Date(timestamp);
+ return isNaN(date.getTime()) ? fallback.toISOString() : date.toISOString();
+ }
+
+ return fallback.toISOString();
+};
+
function dbAgentToAgent(dbAgent: any): Agent {
return {
id: dbAgent.id,
@@ -14,8 +36,8 @@ function dbAgentToAgent(dbAgent: any): Agent {
version: dbAgent.version,
capabilities: Array.isArray(dbAgent.capabilities) ? dbAgent.capabilities : JSON.parse(dbAgent.capabilities || '[]'),
status: dbAgent.status,
- lastSeen: dbAgent.lastSeen ? new Date(dbAgent.lastSeen * 1000).toISOString() : new Date().toISOString(),
- registeredAt: dbAgent.registeredAt ? new Date(dbAgent.registeredAt * 1000).toISOString() : new Date().toISOString(),
+ lastSeen: safeTimestamp(dbAgent.lastSeen),
+ registeredAt: safeTimestamp(dbAgent.registeredAt),
metrics: {
containerCount: dbAgent.containerCount,
imageCount: dbAgent.imageCount,
@@ -31,8 +53,8 @@ function dbAgentToAgent(dbAgent: any): Agent {
}
: undefined,
metadata: dbAgent.metadata ? (typeof dbAgent.metadata === 'string' ? JSON.parse(dbAgent.metadata) : dbAgent.metadata) : undefined,
- createdAt: new Date(dbAgent.createdAt * 1000).toISOString(),
- updatedAt: dbAgent.updatedAt ? new Date(dbAgent.updatedAt * 1000).toISOString() : undefined
+ createdAt: safeTimestamp(dbAgent.createdAt),
+ updatedAt: dbAgent.updatedAt ? safeTimestamp(dbAgent.updatedAt) : undefined
};
}
@@ -40,6 +62,28 @@ function dbAgentToAgent(dbAgent: any): Agent {
* Convert database task to AgentTask type
*/
function dbTaskToAgentTask(dbTask: any): AgentTask {
+ // Helper function to safely convert timestamp
+ const safeTimestamp = (timestamp: any, fallback: Date = new Date()): string => {
+ if (!timestamp || timestamp === null || timestamp === undefined) {
+ return fallback.toISOString();
+ }
+
+ // If it's already a string (ISO format), return it
+ if (typeof timestamp === 'string') {
+ const date = new Date(timestamp);
+ return isNaN(date.getTime()) ? fallback.toISOString() : date.toISOString();
+ }
+
+ // If it's a number, treat as Unix timestamp in seconds
+ if (typeof timestamp === 'number' && !isNaN(timestamp)) {
+ // Check if it's in milliseconds (greater than year 2000 in seconds)
+ const date = timestamp > 946684800 ? new Date(timestamp * 1000) : new Date(timestamp);
+ return isNaN(date.getTime()) ? fallback.toISOString() : date.toISOString();
+ }
+
+ return fallback.toISOString();
+ };
+
return {
id: dbTask.id,
agentId: dbTask.agentId,
@@ -48,8 +92,8 @@ function dbTaskToAgentTask(dbTask: any): AgentTask {
status: dbTask.status,
result: dbTask.result ? (typeof dbTask.result === 'string' ? JSON.parse(dbTask.result) : dbTask.result) : undefined,
error: dbTask.error,
- createdAt: new Date(dbTask.createdAt * 1000).toISOString(),
- updatedAt: dbTask.updatedAt ? new Date(dbTask.updatedAt * 1000).toISOString() : undefined
+ createdAt: safeTimestamp(dbTask.createdAt),
+ updatedAt: dbTask.updatedAt ? safeTimestamp(dbTask.updatedAt) : undefined
};
}
diff --git a/src/routes/agents/[agentId]/+page.svelte b/src/routes/agents/[agentId]/+page.svelte
index 020887d37..e869c53da 100644
--- a/src/routes/agents/[agentId]/+page.svelte
+++ b/src/routes/agents/[agentId]/+page.svelte
@@ -162,17 +162,37 @@
}
const taskId = result.task.id;
- return pollTaskCompletion(taskId, ['containers', 'images', 'networks', 'volumes', 'composes'][index]);
+ return pollTaskCompletion(taskId, ['containers', 'images', 'networks', 'volumes', 'stacks'][index]);
})
);
- // Process results
+ // Process results - replace the forEach loop
+ const newResourcesData: {
+ containers: any[];
+ images: any[];
+ networks: any[];
+ volumes: any[];
+ stacks: any[];
+ } = {
+ containers: [],
+ images: [],
+ networks: [],
+ volumes: [],
+ stacks: []
+ };
+
results.forEach((result, index) => {
if (result.status === 'fulfilled' && result.value) {
- const resourceType = ['containers', 'images', 'networks', 'volumes', 'stacks'][index] as keyof typeof resourcesData;
- resourcesData[resourceType] = result.value;
+ const resourceType = ['containers', 'images', 'networks', 'volumes', 'stacks'][index] as keyof typeof newResourcesData;
+ console.log(`Assigning ${result.value.length} items to ${resourceType}:`, result.value);
+ newResourcesData[resourceType] = result.value;
+ } else {
+ console.log(`Result ${index} failed:`, result);
}
});
+
+ // Update the state with the new object
+ resourcesData = newResourcesData;
} catch (err) {
console.error('Failed to load resources data:', err);
resourcesError = err instanceof Error ? err.message : 'Failed to load resource data';
@@ -243,21 +263,31 @@
console.log(`Raw output for ${resourceType}:`, outputString);
if (outputString) {
- // Split by lines and parse each line as JSON
- const lines = outputString.split('\n').filter((line: string) => line.trim());
- console.log(`Task ${taskId} has ${lines.length} lines to parse`);
-
- for (const line of lines) {
- try {
- const parsed = JSON.parse(line.trim());
+ try {
+ // First try to parse the entire output as JSON (for cases like compose ls that return an array)
+ const parsed = JSON.parse(outputString);
+ if (Array.isArray(parsed)) {
+ data = parsed;
+ console.log(`Parsed entire output as JSON array for ${resourceType}:`, data);
+ } else {
data.push(parsed);
- } catch (parseError) {
- console.warn(`Failed to parse line as JSON:`, line, parseError);
- // Skip invalid JSON lines
+ }
+ } catch (parseError) {
+ // If that fails, try parsing line by line
+ const lines = outputString.split('\n').filter((line: string) => line.trim());
+ console.log(`Task ${taskId} has ${lines.length} lines to parse`);
+
+ for (const line of lines) {
+ try {
+ const parsed = JSON.parse(line.trim());
+ data.push(parsed);
+ } catch (lineParseError) {
+ console.warn(`Failed to parse line as JSON:`, line, lineParseError);
+ // Skip invalid JSON lines
+ }
}
}
}
-
console.log(`Parsed data for ${resourceType}:`, data);
return data;
} else if (task.status === 'failed') {
@@ -779,8 +809,7 @@
columns={[
{ accessorKey: 'Name', header: 'Project Name' },
{ accessorKey: 'Status', header: 'Status' },
- { accessorKey: 'ConfigFiles', header: 'Config Files' },
- { accessorKey: 'Services', header: 'Services' }
+ { accessorKey: 'ConfigFiles', header: 'Config Files' }
]}
idKey="Name"
features={{ selection: false }}