import type { LogEntry } from "../../../types.js";
import { getCurrentTimestamp } from "../../../utils/time.js";
/**
* Parse time specification (absolute or relative) - pure helper function
*/
export function parseTimeSpec(spec: string): number {
// Check for relative time like "1h", "30m", "2d"
const relativeMatch = spec.match(/^(\d+)([smhd])$/);
if (relativeMatch) {
const value = Number.parseInt(relativeMatch[1], 10);
const unit = relativeMatch[2];
const multipliers: Record<string, number> = {
s: 1,
m: 60,
h: 3600,
d: 86400,
};
return Math.floor(Date.now() / 1000) - value * multipliers[unit];
}
// Absolute timestamp
return Math.floor(new Date(spec).getTime() / 1000);
}
/**
* Parse Docker log output into structured entries - pure helper function
*/
export function parseDockerLogs(raw: string): LogEntry[] {
const lines = raw.split("\n").filter((l) => l.trim());
const entries: LogEntry[] = [];
for (const line of lines) {
// Docker log format: timestamp message
const match = line.match(/^(\d{4}-\d{2}-\d{2}T[\d:.]+Z)\s+(.*)$/);
if (match) {
entries.push({
timestamp: match[1],
stream: "stdout", // Default, actual stream info requires demuxing
message: match[2],
});
} else if (line.trim()) {
entries.push({
timestamp: getCurrentTimestamp(),
stream: "stdout",
message: line,
});
}
}
return entries;
}