kubesphere.io/s2irun@v3.2.1+incompatible/pkg/api/helpers.go (about)

     1  package api
     2  
     3  import "time"
     4  
     5  // RecordStageAndStepInfo records details about each build stage and step
     6  func RecordStageAndStepInfo(stages []StageInfo, stageName StageName, stepName StepName, startTime time.Time, endTime time.Time) []StageInfo {
     7  	// Make sure that the stages slice is initialized
     8  	if len(stages) == 0 {
     9  		stages = make([]StageInfo, 0)
    10  	}
    11  
    12  	// If the stage already exists  update the endTime and Duration, and append the new step.
    13  	for stageKey, stageVal := range stages {
    14  		if stageVal.Name == stageName {
    15  			stages[stageKey].DurationMilliseconds = endTime.Sub(stages[stageKey].StartTime).Nanoseconds() / int64(time.Millisecond)
    16  			if len(stages[stageKey].Steps) == 0 {
    17  				stages[stageKey].Steps = make([]StepInfo, 0)
    18  			}
    19  			stages[stageKey].Steps = append(stages[stageKey].Steps, StepInfo{
    20  				Name:                 stepName,
    21  				StartTime:            startTime,
    22  				DurationMilliseconds: endTime.Sub(startTime).Nanoseconds() / int64(time.Millisecond),
    23  			})
    24  			return stages
    25  		}
    26  	}
    27  
    28  	// If the stageName does not exist, add it to the slice along with the new step.
    29  	steps := make([]StepInfo, 0)
    30  	steps = append(steps, StepInfo{
    31  		Name:                 stepName,
    32  		StartTime:            startTime,
    33  		DurationMilliseconds: endTime.Sub(startTime).Nanoseconds() / int64(time.Millisecond),
    34  	})
    35  	stages = append(stages, StageInfo{
    36  		Name:                 stageName,
    37  		StartTime:            startTime,
    38  		DurationMilliseconds: endTime.Sub(startTime).Nanoseconds() / int64(time.Millisecond),
    39  		Steps:                steps,
    40  	})
    41  	return stages
    42  }