Skip to content

Add get_storage_info() method to public library API#116

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-115-add-storage-info-api
Jan 13, 2026
Merged

Add get_storage_info() method to public library API#116
inureyes merged 2 commits into
mainfrom
feature/issue-115-add-storage-info-api

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

  • Add StorageReader trait and LocalStorageReader implementation for retrieving storage/disk information
  • Add get_storage_info() -> Vec<StorageInfo> method to AllSmi struct
  • Add has_storage_monitoring() helper method
  • Export StorageInfo and StorageReader in the prelude
  • Update library usage example with storage info demonstration

Implementation Details

The implementation follows the existing reader pattern used by other resource types (GpuReader, CpuReader, MemoryReader, ChassisReader):

  • StorageReader trait (src/storage/reader.rs): Defines the interface for reading storage information
  • LocalStorageReader (src/storage/reader.rs): Implementation using sysinfo::Disks with Docker-aware filtering
  • AllSmi integration (src/client.rs): Added storage_reader field and get_storage_info() method
  • Public exports (src/prelude.rs): Export StorageInfo, StorageReader, and create_storage_reader

Example Usage

use all_smi::prelude::*;

fn main() -> Result<()> {
    let smi = AllSmi::new()?;
    
    for storage in smi.get_storage_info() {
        let used_bytes = storage.total_bytes - storage.available_bytes;
        let usage_percent = if storage.total_bytes > 0 {
            (used_bytes as f64 / storage.total_bytes as f64) * 100.0
        } else {
            0.0
        };
        let total_gb = storage.total_bytes as f64 / 1024.0 / 1024.0 / 1024.0;
        let available_gb = storage.available_bytes as f64 / 1024.0 / 1024.0 / 1024.0;
        
        println!("{}: {:.1} GB / {:.1} GB ({:.1}% used)",
            storage.mount_point, available_gb, total_gb, usage_percent);
    }
    
    Ok(())
}

Test plan

  • All existing tests pass (cargo test --lib)
  • New storage reader tests pass
  • Library usage example works correctly with storage info
  • Clippy passes without errors

Closes #115

Add StorageReader trait and LocalStorageReader implementation for
retrieving storage/disk information through the AllSmi library API.

Changes:
- Create src/storage/reader.rs with StorageReader trait and
  LocalStorageReader implementation using sysinfo::Disks
- Add storage_reader field to AllSmi struct
- Implement get_storage_info() -> Vec<StorageInfo> method on AllSmi
- Add has_storage_monitoring() helper method
- Export StorageInfo and StorageReader in prelude
- Update examples/library_usage.rs with storage info example
- Add comprehensive unit tests for the new functionality

The implementation follows the existing reader pattern (GpuReader,
CpuReader, MemoryReader, ChassisReader) and reuses the existing
filter_docker_aware_disks() utility for filtering system directories
and Docker-specific bind mounts.

Closes #115
@inureyes inureyes added priority:medium Medium priority issue status:review Under review type:enhancement New feature or request labels Jan 13, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Security and Performance Review

Analysis Summary

  • PR Branch: feature/issue-115-add-storage-info-api
  • Scope: changed-files (5 files, +310 lines)
  • Languages: Rust
  • Total issues: 0 critical/high, 2 low suggestions

Overall Assessment: APPROVED

This PR adds a well-designed get_storage_info() method to the public library API following the established patterns in the codebase. The implementation is clean, secure, and performant.


Security Review

Input Validation: N/A (No user input)

  • The storage reader reads from system APIs (sysinfo::Disks) without any user-supplied parameters.

Authentication/Authorization: N/A

  • This is a local system monitoring API that reads publicly available filesystem information.

Data Exposure: PASS

  • Storage information exposed (mount points, sizes) is standard system information.
  • No sensitive data (file contents, credentials) is exposed.
  • Uses existing filter_docker_aware_disks() to exclude sensitive system directories.

Cryptography: N/A

  • No cryptographic operations in this change.

Performance Review

Memory Efficiency: PASS

  • Uses Vec appropriately for collecting storage info.
  • Hostname is cached at LocalStorageReader creation time (line 82-83 in reader.rs) to avoid repeated lookups.
  • Disks::new_with_refreshed_list() is called per-invocation which is appropriate for real-time monitoring.

Algorithmic Complexity: PASS

  • O(n) iteration over disks with O(n log n) sort - appropriate for typical disk counts.
  • Uses existing optimized filter_docker_aware_disks() which employs HashSet for O(1) exclusion lookups.

Thread Safety: PASS

  • StorageReader trait requires Send + Sync bounds (line 41 in reader.rs).
  • LocalStorageReader correctly implements these traits.
  • Static DISK_FILTER uses OnceLock for thread-safe lazy initialization.

Code Quality Review

API Consistency: EXCELLENT

  • Follows the established reader pattern (like CpuReader, MemoryReader, ChassisReader).
  • Factory function create_storage_reader() matches existing patterns.
  • Proper trait bounds for thread safety.

Documentation: EXCELLENT

  • Comprehensive doc comments with examples on all public items.
  • Clear explanation of filtering behavior.

Error Handling: PASS

  • Method returns empty Vec rather than erroring when no storage detected (consistent with other readers).
  • has_storage_monitoring() returns true unconditionally since sysinfo is always available.

Testing: PASS

  • 7 unit tests covering:
    • Reader creation
    • Default trait implementation
    • Factory function
    • Send + Sync trait verification
    • Storage info consistency validation
  • All tests pass.

Low Priority Suggestions (Non-blocking)

1. Consider caching disk list for high-frequency calls

Location: src/storage/reader.rs, line 95

Current:

fn get_storage_info(&self) -> Vec<StorageInfo> {
    let disks = Disks::new_with_refreshed_list();
    // ...
}

Observation: Each call creates a new Disks instance and refreshes from the OS. This is correct for real-time monitoring but could be expensive if called very frequently (>10 times/second).

Suggestion for future: If high-frequency polling is needed, consider adding an optional refresh interval or caching mechanism similar to other readers. However, for typical use cases (monitoring dashboards refreshing every 1-5 seconds), this is perfectly acceptable.

Priority: LOW - Only relevant for edge cases with extremely high polling rates.


2. Minor: Consider implementing Debug for LocalStorageReader

Location: src/storage/reader.rs, line 72

Current:

pub struct LocalStorageReader {
    hostname: String,
}

Suggestion: Adding #[derive(Debug)] would improve debuggability and is consistent with Rust API guidelines for public types.

Priority: LOW - Does not affect functionality.


Files Reviewed

File Status Notes
src/storage/reader.rs PASS Well-structured with comprehensive tests
src/storage/mod.rs PASS Proper re-exports
src/client.rs PASS Clean integration with AllSmi struct
src/prelude.rs PASS Appropriate exports for public API
examples/library_usage.rs PASS Good example demonstrating usage

Verification

cargo clippy --lib: PASS (no warnings)
cargo test --lib: PASS (159 tests, including new storage tests)
cargo doc --lib: PASS (builds successfully)

Conclusion

This PR is well-implemented and ready to merge. The code follows established patterns, is thread-safe, properly documented, and includes comprehensive tests. No security vulnerabilities or significant performance concerns were identified.

Recommendation: APPROVE

- Add StorageInfo to table of contents and data types section
- Add get_storage_info() and has_storage_monitoring() to available methods
- Add StorageInfo fields documentation with code examples
- Update Quick Start, Using the Prelude, Low-Level Access sections
- Add storage to Monitoring Dashboard, JSON Export examples
- Add storage threshold to Resource Threshold Alerting example
@inureyes inureyes merged commit 2011071 into main Jan 13, 2026
3 of 4 checks passed
@inureyes inureyes deleted the feature/issue-115-add-storage-info-api branch January 13, 2026 09:33
@inureyes inureyes self-assigned this Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority issue status:review Under review type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add get_storage_info() method to public library API

1 participant