Files
container/Sources/Services/ContainerAPIService/DiskUsage/DiskUsageService.swift
T
Raj d327a50219 Add container system df command for disk usage reporting (#902)
- Closes #884. 

## Type of Change
- [ ] Bug fix
- [x] New feature  
- [ ] Breaking change
- [ ] Documentation update

## Motivation and Context
This PR implements the `container system df` command to display disk
usage statistics for images, containers, and volumes, along with their
total count, active count, size, and reclaimable space for each resource
type.

Active resources are determined by container mount references and
running state, while reclaimable space is calculated from inactive or
stopped resources.

Example output:
```
~/container ❯ container system df
TYPE           TOTAL  ACTIVE  SIZE      RECLAIMABLE
Images         4      3       4.42 GB   516.5 MB (11%)
Containers     4      2       2.69 GB   1.51 GB (56%)
Local Volumes  3      2       208.5 MB  66.2 MB (32%)
```

I'll have some follow-on PRs that will add `-v/--verbose` flag for
detailed per-resource information, `--filter` flag for filtering output
by resource type, and a `--debug` flag for debug statistics like block
usage, clone counts etc.

## Testing
- [x] Tested locally
- [x] Added/updated tests
- [ ] Added/updated docs
2025-11-20 09:19:00 -08:00

82 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerClient
import Logging
/// Service for calculating disk usage across all resource types
public actor DiskUsageService {
private let containersService: ContainersService
private let volumesService: VolumesService
private let log: Logger
public init(
containersService: ContainersService,
volumesService: VolumesService,
log: Logger
) {
self.containersService = containersService
self.volumesService = volumesService
self.log = log
}
/// Calculate disk usage for all resource types
public func calculateDiskUsage() async throws -> DiskUsageStats {
log.debug("calculating disk usage for all resources")
// Get active image references first (needed for image calculation)
let activeImageRefs = await containersService.getActiveImageReferences()
// Query all services concurrently
async let imageStats = ClientImage.calculateDiskUsage(activeReferences: activeImageRefs)
async let containerStats = containersService.calculateDiskUsage()
async let volumeStats = volumesService.calculateDiskUsage()
let (imageData, containerData, volumeData) = try await (imageStats, containerStats, volumeStats)
let stats = DiskUsageStats(
images: ResourceUsage(
total: imageData.totalCount,
active: imageData.activeCount,
sizeInBytes: imageData.totalSize,
reclaimable: imageData.reclaimableSize
),
containers: ResourceUsage(
total: containerData.0,
active: containerData.1,
sizeInBytes: containerData.2,
reclaimable: containerData.3
),
volumes: ResourceUsage(
total: volumeData.0,
active: volumeData.1,
sizeInBytes: volumeData.2,
reclaimable: volumeData.3
)
)
log.debug(
"disk usage calculation complete",
metadata: [
"images_total": "\(imageData.totalCount)",
"containers_total": "\(containerData.0)",
"volumes_total": "\(volumeData.0)",
])
return stats
}
}