Skip to content

Commit

Permalink
feat(cd): add wait time to notifs
Browse files Browse the repository at this point in the history
  • Loading branch information
smrz2001 committed Nov 9, 2023
1 parent ca195ca commit 26ea4a5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
9 changes: 5 additions & 4 deletions cd/manager/common/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ const (
)

const (
JobParam_Id string = "id"
JobParam_Error string = "error"
JobParam_Start string = "start"
JobParam_Source string = "source"
JobParam_Id string = "id"
JobParam_Error string = "error"
JobParam_Elapsed string = "elapsed"
JobParam_Start string = "start"
JobParam_Source string = "source"
)

const (
Expand Down
33 changes: 27 additions & 6 deletions cd/manager/notifs/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
notifField_References string = "References"
notifField_JobId string = "Job ID"
notifField_RunTime string = "Time Running"
notifField_WaitTime string = "Time Waiting"
notifField_Deploy string = "Deployment(s)"
notifField_Anchor string = "Anchor Worker(s)"
notifField_TestE2E string = "E2E Tests"
Expand Down Expand Up @@ -151,15 +152,25 @@ func (n JobNotifs) getNotifFields(jobState job.JobState) []discord.EmbedField {
Value: deployTags,
})
}
// If the job just started, also add the wait time.
if jobState.Stage == job.JobStage_Started {
if elapsedTime, found := jobState.Params[job.JobParam_Elapsed].(string); found {
parsedElapsedTime, _ := time.ParseDuration(elapsedTime)
waitTime := formattedDuration(parsedElapsedTime)
if len(waitTime) > 0 {
fields = append(fields, discord.EmbedField{
Name: notifField_WaitTime,
Value: waitTime,
})
}
}
}
if startTime, found := jobState.Params[job.JobParam_Start].(float64); found {
runTime := time.Since(time.Unix(0, int64(startTime)))
hours := int(runTime.Seconds() / 3600)
minutes := int(runTime.Seconds()/60) % 60
seconds := int(runTime.Seconds()) % 60
if (hours != 0) || (minutes != 0) || (seconds != 0) {
runTime := formattedDuration(time.Since(time.Unix(0, int64(startTime))))
if len(runTime) > 0 {
fields = append(fields, discord.EmbedField{
Name: notifField_RunTime,
Value: fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds),
Value: runTime,
})
}
}
Expand Down Expand Up @@ -295,3 +306,13 @@ func colorForStage(jobStage job.JobStage) discordColor {
return discordColor_Alert
}
}

func formattedDuration(duration time.Duration) string {
hours := int(duration.Seconds() / 3600)
minutes := int(duration.Seconds()/60) % 60
seconds := int(duration.Seconds()) % 60
if (hours != 0) || (minutes != 0) || (seconds != 0) {
return fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds)
}
return ""
}
2 changes: 2 additions & 0 deletions cd/manager/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func IsV5WorkerJob(jobState job.JobState) bool {
// AdvanceJob will move a JobState to a new JobStage in the Database and send an appropriate notification
func AdvanceJob(jobState job.JobState, jobStage job.JobStage, ts time.Time, err error, db Database, notifs Notifs) (job.JobState, error) {
jobState.Stage = jobStage
// Store how much time has elapsed since the stage last changed for this job
jobState.Params[job.JobParam_Elapsed] = time.Since(jobState.Ts).String()
jobState.Ts = ts
if err != nil {
jobState.Params[job.JobParam_Error] = err.Error()
Expand Down

0 comments on commit 26ea4a5

Please sign in to comment.