@@ -22,13 +22,27 @@ import (
2222 "github.com/GoogleCloudPlatform/khi/pkg/inspection/metadata"
2323)
2424
25+ // ProgressMetadataKey is the key used to store and retrieve Progress metadata
26+ // from a context or metadata map.
2527var ProgressMetadataKey = metadata.NewMetadataKey [* Progress ]("progress" )
2628
27- const TASK_PHASE_RUNNING = "RUNNING"
28- const TASK_PHASE_DONE = "DONE"
29- const TASK_PHASE_ERROR = "ERROR"
30- const TASK_PHASE_CANCELLED = "CANCELLED"
29+ // TaskProgressPhase represents the lifecycle phase of a task's progress.
30+ type TaskProgressPhase string
31+
32+ // Constants defining the possible phases of a task's progress.
33+ const (
34+ // TaskPhaseRunning indicates that the task is currently executing.
35+ TaskPhaseRunning TaskProgressPhase = "RUNNING"
36+ // TaskPhaseDone indicates that the task has completed successfully.
37+ TaskPhaseDone = "DONE"
38+ // TaskPhaseError indicates that the task terminated with an error.
39+ TaskPhaseError = "ERROR"
40+ // TaskPhaseCancelled indicates that the task was cancelled before completion.
41+ TaskPhaseCancelled = "CANCELLED"
42+ )
3143
44+ // TaskProgress represents the progress of a single task within an inspection.
45+ // It includes an ID, a human-readable label, a status message, and completion percentage.
3246type TaskProgress struct {
3347 Id string `json:"id"`
3448 Label string `json:"label"`
@@ -37,6 +51,7 @@ type TaskProgress struct {
3751 Indeterminate bool `json:"indeterminate"`
3852}
3953
54+ // NewTaskProgress creates and initializes a new TaskProgress object with the given ID.
4055func NewTaskProgress (id string ) * TaskProgress {
4156 return & TaskProgress {
4257 Id : id ,
@@ -60,18 +75,21 @@ func (tp *TaskProgress) MarkIndeterminate() {
6075 tp .Percentage = 0
6176}
6277
78+ // Progress aggregates the progress of all tasks in an inspection run.
79+ // It tracks the overall phase, total progress, and the progress of individual active tasks.
6380type Progress struct {
64- Phase string `json:"phase"`
65- TotalProgress * TaskProgress `json:"totalProgress"`
66- TaskProgresses []* TaskProgress `json:"progresses"`
67- totalTaskCount int `json:"-"`
68- resolvedTaskCount int `json:"-"`
69- lock sync.Mutex `json:"-"`
81+ Phase TaskProgressPhase `json:"phase"`
82+ TotalProgress * TaskProgress `json:"totalProgress"`
83+ TaskProgresses []* TaskProgress `json:"progresses"`
84+ totalTaskCount int `json:"-"`
85+ resolvedTaskCount int `json:"-"`
86+ lock sync.Mutex `json:"-"`
7087}
7188
89+ // NewProgress creates and initializes a new Progress object.
7290func NewProgress () * Progress {
7391 return & Progress {
74- Phase : TASK_PHASE_RUNNING ,
92+ Phase : TaskPhaseRunning ,
7593 TaskProgresses : make ([]* TaskProgress , 0 ),
7694 TotalProgress : NewTaskProgress ("Total" ),
7795 lock : sync.Mutex {},
@@ -92,15 +110,20 @@ func (p *Progress) ToSerializable() interface{} {
92110 return p
93111}
94112
113+ // SetTotalTaskCount sets the total number of tasks that will be tracked.
114+ // This is used to calculate the overall progress percentage.
95115func (p * Progress ) SetTotalTaskCount (count int ) {
96116 p .totalTaskCount = count
97117 p .updateTotalTaskProgress ()
98118}
99119
100- func (p * Progress ) GetTaskProgress (id string ) (* TaskProgress , error ) {
120+ // GetOrCreateTaskProgress retrieves the TaskProgress for a given task ID.
121+ // If no progress object exists for the ID, a new one is created and added to the list.
122+ // It returns an error if the overall progress is no longer in the RUNNING phase.
123+ func (p * Progress ) GetOrCreateTaskProgress (id string ) (* TaskProgress , error ) {
101124 p .lock .Lock ()
102125 defer p .lock .Unlock ()
103- if p .Phase != TASK_PHASE_RUNNING {
126+ if p .Phase != TaskPhaseRunning {
104127 return nil , fmt .Errorf ("the current progress phase is not RUNNING but %s" , p .Phase )
105128 }
106129 for _ , progress := range p .TaskProgresses {
@@ -113,10 +136,13 @@ func (p *Progress) GetTaskProgress(id string) (*TaskProgress, error) {
113136 return taskProgress , nil
114137}
115138
139+ // ResolveTask marks a task as resolved by removing it from the active progress list
140+ // and increments the count of resolved tasks.
141+ // It returns an error if the overall progress is no longer in the RUNNING phase.
116142func (p * Progress ) ResolveTask (id string ) error {
117143 p .lock .Lock ()
118144 defer p .lock .Unlock ()
119- if p .Phase != TASK_PHASE_RUNNING {
145+ if p .Phase != TaskPhaseRunning {
120146 return fmt .Errorf ("the current progress phase is not RUNNING but %s" , p .Phase )
121147 }
122148 newTaskProgress := make ([]* TaskProgress , 0 )
@@ -131,37 +157,46 @@ func (p *Progress) ResolveTask(id string) error {
131157 return nil
132158}
133159
134- func (p * Progress ) Done () error {
160+ // MarkDone transitions the overall progress to the DONE phase.
161+ // It clears all active task progresses and marks the total progress as 100% complete.
162+ // It returns an error if the overall progress is no longer in the RUNNING phase.
163+ func (p * Progress ) MarkDone () error {
135164 p .lock .Lock ()
136165 defer p .lock .Unlock ()
137- if p .Phase != TASK_PHASE_RUNNING {
166+ if p .Phase != TaskPhaseRunning {
138167 return fmt .Errorf ("the current progress phase is not RUNNING but %s" , p .Phase )
139168 }
140- p .Phase = TASK_PHASE_DONE
169+ p .Phase = TaskPhaseDone
141170 p .resolvedTaskCount = p .totalTaskCount
142171 p .TaskProgresses = make ([]* TaskProgress , 0 )
143172 p .updateTotalTaskProgress ()
144173 return nil
145174}
146175
147- func (p * Progress ) Cancel () error {
176+ // MarkCancelled transitions the overall progress to the CANCELLED phase.
177+ // It clears all active task progresses.
178+ // It returns an error if the overall progress is no longer in the RUNNING phase.
179+ func (p * Progress ) MarkCancelled () error {
148180 p .lock .Lock ()
149181 defer p .lock .Unlock ()
150- if p .Phase != TASK_PHASE_RUNNING {
182+ if p .Phase != TaskPhaseRunning {
151183 return fmt .Errorf ("the current progress phase is not RUNNING but %s" , p .Phase )
152184 }
153- p .Phase = TASK_PHASE_CANCELLED
185+ p .Phase = TaskPhaseCancelled
154186 p .TaskProgresses = make ([]* TaskProgress , 0 )
155187 return nil
156188}
157189
158- func (p * Progress ) Error () error {
190+ // MarkError transitions the overall progress to the ERROR phase.
191+ // It clears all active task progresses.
192+ // It returns an error if the overall progress is no longer in the RUNNING phase.
193+ func (p * Progress ) MarkError () error {
159194 p .lock .Lock ()
160195 defer p .lock .Unlock ()
161- if p .Phase != TASK_PHASE_RUNNING {
196+ if p .Phase != TaskPhaseRunning {
162197 return fmt .Errorf ("the current progress phase is not RUNNING but %s" , p .Phase )
163198 }
164- p .Phase = TASK_PHASE_ERROR
199+ p .Phase = TaskPhaseError
165200 p .TaskProgresses = make ([]* TaskProgress , 0 )
166201 return nil
167202}
0 commit comments