buildTaskEvent constructs a TaskEvent from the combined query row.
( row database.GetTelemetryTaskEventsRow, createdAfter time.Time, now time.Time, )
| 1029 | |
| 1030 | // buildTaskEvent constructs a TaskEvent from the combined query row. |
| 1031 | func buildTaskEvent( |
| 1032 | row database.GetTelemetryTaskEventsRow, |
| 1033 | createdAfter time.Time, |
| 1034 | now time.Time, |
| 1035 | ) TaskEvent { |
| 1036 | event := TaskEvent{ |
| 1037 | TaskID: row.TaskID.String(), |
| 1038 | } |
| 1039 | |
| 1040 | var ( |
| 1041 | hasStartBuild = row.StartBuildCreatedAt.Valid |
| 1042 | isResumed = hasStartBuild && row.StartBuildNumber.Valid && row.StartBuildNumber.Int32 > 1 |
| 1043 | hasStopBuild = row.StopBuildCreatedAt.Valid |
| 1044 | startedAfterStop = hasStartBuild && hasStopBuild && row.StartBuildCreatedAt.Time.After(row.StopBuildCreatedAt.Time) |
| 1045 | currentlyPaused = hasStopBuild && !startedAfterStop |
| 1046 | ) |
| 1047 | |
| 1048 | // Pause-related fields (requires a stop build). |
| 1049 | if hasStopBuild { |
| 1050 | event.LastPausedAt = &row.StopBuildCreatedAt.Time |
| 1051 | switch { |
| 1052 | case row.StopBuildReason.Valid && row.StopBuildReason.BuildReason == database.BuildReasonTaskAutoPause: |
| 1053 | event.PauseReason = ptr.Ref("auto") |
| 1054 | case row.StopBuildReason.Valid && row.StopBuildReason.BuildReason == database.BuildReasonTaskManualPause: |
| 1055 | event.PauseReason = ptr.Ref("manual") |
| 1056 | default: |
| 1057 | event.PauseReason = ptr.Ref("other") |
| 1058 | } |
| 1059 | |
| 1060 | // Idle duration: time between last working status and the pause. |
| 1061 | if row.LastWorkingStatusAt.Valid && |
| 1062 | row.StopBuildCreatedAt.Time.After(row.LastWorkingStatusAt.Time) { |
| 1063 | idle := row.StopBuildCreatedAt.Time.Sub(row.LastWorkingStatusAt.Time) |
| 1064 | event.IdleDurationMS = ptr.Ref(idle.Milliseconds()) |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // Resume-related fields (requires task_resume start after stop). |
| 1069 | if startedAfterStop { |
| 1070 | // Paused duration: time between pause and resume. |
| 1071 | if row.StartBuildCreatedAt.Time.After(createdAfter) { |
| 1072 | paused := row.StartBuildCreatedAt.Time.Sub(row.StopBuildCreatedAt.Time) |
| 1073 | event.PausedDurationMS = ptr.Ref(paused.Milliseconds()) |
| 1074 | } |
| 1075 | |
| 1076 | // Below only relevant for "resumed" tasks, not when initially created. |
| 1077 | if isResumed { |
| 1078 | event.LastResumedAt = &row.StartBuildCreatedAt.Time |
| 1079 | switch { |
| 1080 | // TODO(Cian): will this exist? Future readers may know better than I. |
| 1081 | // case row.StartBuildReason == database.BuildReasonTaskAutoResume: |
| 1082 | // event.ResumeReason = ptr.Ref("auto") |
| 1083 | case row.StartBuildReason.BuildReason == database.BuildReasonTaskResume: |
| 1084 | event.ResumeReason = ptr.Ref("manual") |
| 1085 | default: // Task resumed by starting workspace? |
| 1086 | event.ResumeReason = ptr.Ref("other") |
| 1087 | } |
| 1088 | } |
no test coverage detected