File size: 1,151 Bytes
b225a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os


def get_last_subdirectory(directory_path: str) -> str | None:
    # Get all subdirectories in the directory
    subdirs = [
        os.path.join(directory_path, name)
        for name in os.listdir(directory_path)
        if os.path.isdir(os.path.join(directory_path, name))
    ]

    # Sort the subdirectories by creation time
    subdirs.sort(key=os.path.getctime)

    # Return the last subdirectory in the list
    return subdirs[-1] if subdirs else None


def get_latest_report_from_agent_directories(
    directory_path: str,
) -> list[tuple[os.DirEntry[str], str]]:
    latest_reports = []

    for subdir in os.scandir(directory_path):
        if subdir.is_dir():
            # Get the most recently created subdirectory within this agent's directory
            latest_subdir = get_last_subdirectory(subdir.path)
            if latest_subdir is not None:
                # Look for 'report.json' in the subdirectory
                report_file = os.path.join(latest_subdir, "report.json")
                if os.path.isfile(report_file):
                    latest_reports.append((subdir, report_file))

    return latest_reports