docs(agent): 统一中文注释并补充部署文档

将代码与测试注释改为中文以符合仓库规范;新增中文部署指南、
示例配置,并完善构建脚本对 VERSION 注入的说明。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
codex
2026-05-19 02:30:49 +00:00
parent 89d5e27874
commit 877e4cf41d
28 changed files with 608 additions and 226 deletions

View File

@@ -8,28 +8,28 @@ import (
"mfd-monitor-agent/internal/status"
)
// Collector gathers one subsection of HostStatus into the provided snapshot.
// Collector 将 HostStatus 的一个子区块采集并写入提供的快照。
type Collector interface {
// Name returns the stable collector name used in partial snapshot errors.
// Name 返回用于 partial 快照错误记录的稳定采集器名称。
Name() string
// Collect fills its subsection of out and returns an error when that subsection is incomplete.
// Collect 填充 out 中对应子区块;该子区块不完整时返回错误。
Collect(context.Context, *status.HostStatus) error
}
// CollectorFunc adapts a function into a Collector for tests and small collectors.
// CollectorFunc 将函数适配为 Collector,供测试与小型采集器使用。
type CollectorFunc func(context.Context, *status.HostStatus) error
// Name returns a generic collector name for function-backed collectors.
// Name 为基于函数的采集器返回通用名称。
func (fn CollectorFunc) Name() string {
return "collector"
}
// Collect calls the wrapped function.
// Collect 调用包装的采集函数。
func (fn CollectorFunc) Collect(ctx context.Context, out *status.HostStatus) error {
return fn(ctx, out)
}
// Cache stores the most recent HostStatus snapshot behind a concurrency-safe interface.
// Cache 在并发安全接口后保存最近一次 HostStatus 快照。
type Cache struct {
collectors []Collector
version string
@@ -39,12 +39,12 @@ type Cache struct {
hasLast bool
}
// NewCache creates an empty snapshot cache using the provided collectors and agent metadata.
// NewCache 使用给定采集器与 agent 元数据创建空的快照缓存。
func NewCache(collectors []Collector, version string, startedAt time.Time) *Cache {
return &Cache{collectors: collectors, version: version, startedAt: startedAt}
}
// Collect runs all collectors once and stores the resulting snapshot even when some collectors fail.
// Collect 运行全部采集器一次并存储结果;部分采集器失败时仍保存快照。
func (c *Cache) Collect(ctx context.Context) status.HostStatus {
out := status.HostStatus{
Agent: status.AgentInfo{
@@ -74,14 +74,14 @@ func (c *Cache) Collect(ctx context.Context) status.HostStatus {
return out
}
// Get returns the latest cached snapshot and whether collection has completed at least once.
// Get 返回最新缓存快照,以及是否至少完成过一次采集。
func (c *Cache) Get() (status.HostStatus, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.last, c.hasLast
}
// Run collects immediately and then refreshes the cache until ctx is canceled.
// Run 立即采集一次,随后在 ctx 取消前按间隔刷新缓存。
func (c *Cache) Run(ctx context.Context, interval time.Duration, timeout time.Duration) {
c.collectWithTimeout(ctx, timeout)
ticker := time.NewTicker(interval)
@@ -96,7 +96,7 @@ func (c *Cache) Run(ctx context.Context, interval time.Duration, timeout time.Du
}
}
// collectWithTimeout runs one collection cycle with an overall deadline.
// collectWithTimeout 在整体截止时间下执行一轮采集。
func (c *Cache) collectWithTimeout(ctx context.Context, timeout time.Duration) {
collectCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

View File

@@ -9,7 +9,7 @@ import (
"mfd-monitor-agent/internal/status"
)
// TestCollectorFailuresReturnPartialSnapshot verifies failed collectors do not fail snapshots.
// TestCollectorFailuresReturnPartialSnapshot 验证采集器失败不会导致整份快照失败。
func TestCollectorFailuresReturnPartialSnapshot(t *testing.T) {
cache := NewCache([]Collector{
CollectorFunc(func(context.Context, *status.HostStatus) error {
@@ -30,7 +30,7 @@ func TestCollectorFailuresReturnPartialSnapshot(t *testing.T) {
}
}
// TestCacheStoresLastSnapshot verifies successful collection is cached.
// TestCacheStoresLastSnapshot 验证成功采集结果被缓存。
func TestCacheStoresLastSnapshot(t *testing.T) {
cache := NewCache([]Collector{
CollectorFunc(func(_ context.Context, out *status.HostStatus) error {
@@ -50,7 +50,7 @@ func TestCacheStoresLastSnapshot(t *testing.T) {
}
}
// TestRunCollectsImmediately verifies Run collects before the first interval tick.
// TestRunCollectsImmediately 验证 Run 在首个间隔 tick 之前立即采集。
func TestRunCollectsImmediately(t *testing.T) {
collected := make(chan struct{})
cache := NewCache([]Collector{
@@ -79,7 +79,7 @@ func TestRunCollectsImmediately(t *testing.T) {
}
}
// TestCollectWithTimeoutMarksCanceledCollectorPartial verifies timeout errors are partial.
// TestCollectWithTimeoutMarksCanceledCollectorPartial 验证超时被记为 partial。
func TestCollectWithTimeoutMarksCanceledCollectorPartial(t *testing.T) {
cache := NewCache([]Collector{
CollectorFunc(func(ctx context.Context, _ *status.HostStatus) error {