Repository navigation
refactor(progress): unify task progress reporting via context and centralize ETA calculation - #1008
Conversation
…tralize ETA calculation - Replace legacy NewProgressReportableInspectionTask and progressutil with context-based progress package (progress.Report, progress.ReportIndeterminate, progress.NewTracker, progress.NewRatioTracker, progress.ForEach). - Encapsulate TaskProgressMetadata and Progress fields with thread-safe Snapshot methods. - Extract shared progress.TaskInterceptor for runner and test harnesses. - Centralize throughput and ETA calculation in progress trackers. - Update task system documentation (EN/JA) to reflect unified context-based progress reporting. TAG=agy
There was a problem hiding this comment.
Code Review
This pull request refactors the progress reporting system in KHI, replacing the legacy NewProgressReportableInspectionTask and progressutil package with a new context-bound progress package featuring Tracker, ForEach, and RatioTracker for cleaner, thread-safe progress updates. A TaskInterceptor was introduced to automatically manage task progress metadata. Various inspection tasks, tests, and documentation in English and Japanese were updated to align with this new architecture, and the frontend layout was adjusted to display the updated progress labels. The review feedback highlights two critical division-by-zero issues in fetchClusterResourceSnapshots and fetchGKEResourceSnapshots when totalChunks is zero, which would produce NaN or +Inf and cause JSON marshaling to fail.
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, lookup.scope, matchedAssetNames, assetpb.ContentType_RESOURCE, lookup.timeWindow, func(completedChunks, totalChunks int) { | ||
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | ||
| progress.Message = fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames)) | ||
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames))) | ||
| }) |
There was a problem hiding this comment.
If totalChunks is 0 (e.g., when matchedAssetNames is empty), dividing completedChunks by totalChunks will result in NaN or +Inf. When this value is passed to progress.Report and subsequently marshaled to JSON, json.Marshal will fail with json.UnsupportedValueError because JSON does not support NaN or Inf values. Guard against division by zero by checking if totalChunks > 0.
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, lookup.scope, matchedAssetNames, assetpb.ContentType_RESOURCE, lookup.timeWindow, func(completedChunks, totalChunks int) { | |
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | |
| progress.Message = fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames)) | |
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames))) | |
| }) | |
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, lookup.scope, matchedAssetNames, assetpb.ContentType_RESOURCE, lookup.timeWindow, func(completedChunks, totalChunks int) { | |
| ratio := float32(0) | |
| if totalChunks > 0 { | |
| ratio = float32(completedChunks) / float32(totalChunks) | |
| } | |
| progress.Report(ctx, ratio, fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames))) | |
| }) |
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, scope, assetNames, assetpb.ContentType_RESOURCE, timeWindow, func(completedChunks, totalChunks int) { | ||
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | ||
| progress.Message = fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames)) | ||
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames))) | ||
| }) |
There was a problem hiding this comment.
If totalChunks is 0 (e.g., when assetNames is empty), dividing completedChunks by totalChunks will result in NaN or +Inf. When this value is passed to progress.Report and subsequently marshaled to JSON, json.Marshal will fail with json.UnsupportedValueError because JSON does not support NaN or Inf values. Guard against division by zero by checking if totalChunks > 0.
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, scope, assetNames, assetpb.ContentType_RESOURCE, timeWindow, func(completedChunks, totalChunks int) { | |
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | |
| progress.Message = fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames)) | |
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames))) | |
| }) | |
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, scope, assetNames, assetpb.ContentType_RESOURCE, timeWindow, func(completedChunks, totalChunks int) { | |
| ratio := float32(0) | |
| if totalChunks > 0 { | |
| ratio = float32(completedChunks) / float32(totalChunks) | |
| } | |
| progress.Report(ctx, ratio, fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames))) | |
| }) |
| if strings.Contains(lower, "log") { | ||
| return "lps" | ||
| } |
There was a problem hiding this comment.
We can use log/s instead and the logic become simpler.
| unit := "logs" | ||
| if passCount > 0 { | ||
| unit = "steps" | ||
| } |
There was a problem hiding this comment.
We can just use steps. Let's keep it simpler.
| unit := "logs" | ||
| if passCount > 0 { | ||
| unit = "steps" | ||
| } |
| progress.Report(ctx, 0.0, fmt.Sprintf("Fetching asset history (0/%d chunks, %d assets)...", totalChunks, len(matchedAssetNames))) | ||
|
|
||
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, lookup.scope, matchedAssetNames, assetpb.ContentType_RESOURCE, lookup.timeWindow, func(completedChunks, totalChunks int) { | ||
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | ||
| progress.Message = fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames)) | ||
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(matchedAssetNames))) |
There was a problem hiding this comment.
Couldn't we use the tracker? Then we can show ETA as well.
| progress.Report(ctx, 0.0, fmt.Sprintf("Fetching GKE asset history (0/%d chunks, %d assets)...", totalChunks, len(assetNames))) | ||
|
|
||
| timeWindow := &assetpb.TimeWindow{ | ||
| StartTime: timestamppb.New(startTime), | ||
| EndTime: timestamppb.New(endTime), | ||
| } | ||
|
|
||
| temporalAssets, err := fetcher.BatchGetAssetsHistory(ctx, scope, assetNames, assetpb.ContentType_RESOURCE, timeWindow, func(completedChunks, totalChunks int) { | ||
| progress.Percentage = float32(completedChunks) / float32(totalChunks) | ||
| progress.Message = fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames)) | ||
| progress.Report(ctx, float32(completedChunks)/float32(totalChunks), fmt.Sprintf("Fetching GKE asset history (%d/%d chunks, %d assets)...", completedChunks, totalChunks, len(assetNames))) |
|
|
||
| // 3. Build KHI v6 format and flush remaining chunks | ||
| if err := builder.Build(&taskProgressReporter{progress: progress}); err != nil { | ||
| if err := builder.Build(&taskProgressReporter{progressMeta: progress.FromContext(ctx)}); err != nil { |
There was a problem hiding this comment.
We can just pass context then use the new APIs from the context inside Build func.
…s, and Builder.Build context
- Update progress tracker unit labels across log ingester and mapper tasks ('logs' -> 'groups' or omit unit).
- Pass ctx to khifilev6.Builder.Build(ctx) and manage progress tracker internally in Builder.Build.
- Encapsulate progress tracker creation inside CAIFetcher.BatchGetAssetsHistory via ctx and remove onProgress callback.
- Move metadata map context key to inspectionmetadata.MapContextKey and remove inspectioncore.InspectionRunMetadata alias.
TAG=agy
49035f3
Summary
NewProgressReportableInspectionTaskandprogressutilwith a unified context-basedprogresspackage (progress.Report,progress.ReportIndeterminate,progress.NewTracker,progress.NewRatioTracker,progress.ForEach).TaskProgressMetadataandProgressfields with thread-safeSnapshotmethods.progress.TaskInterceptorfor runner and test harnesses.