github.com/grahambrereton-form3/tilt@v0.10.18/internal/hud/webview/view.go (about) 1 package webview 2 3 // TODO(dmiller): delete these tests once StateToWebView is deleted 4 import ( 5 "time" 6 7 "github.com/windmilleng/tilt/internal/container" 8 "github.com/windmilleng/tilt/internal/dockercompose" 9 "github.com/windmilleng/tilt/internal/k8s" 10 "github.com/windmilleng/tilt/pkg/model" 11 12 "github.com/golang/protobuf/ptypes" 13 "github.com/golang/protobuf/ptypes/timestamp" 14 15 proto_webview "github.com/windmilleng/tilt/pkg/webview" 16 ) 17 18 type ResourceInfoView interface { 19 resourceInfoView() 20 RuntimeLog() model.Log 21 Status() string 22 } 23 24 type DCResourceInfo struct { 25 ConfigPaths []string `json:"configPaths"` 26 ContainerStatus dockercompose.Status `json:"containerStatus"` 27 ContainerID container.ID `json:"containerID"` 28 Log model.Log `json:"log"` 29 StartTime time.Time `json:"startTime"` 30 } 31 32 func NewDCResourceInfo(configPaths []string, status dockercompose.Status, cID container.ID, log model.Log, startTime time.Time) DCResourceInfo { 33 return DCResourceInfo{ 34 ConfigPaths: configPaths, 35 ContainerStatus: status, 36 ContainerID: cID, 37 Log: log, 38 StartTime: startTime, 39 } 40 } 41 42 func NewProtoDCResourceInfo(configPaths []string, status dockercompose.Status, cID container.ID, log model.Log, startTime time.Time) (*proto_webview.DCResourceInfo, error) { 43 start, err := timeToProto(startTime) 44 if err != nil { 45 return nil, err 46 } 47 return &proto_webview.DCResourceInfo{ 48 ConfigPaths: configPaths, 49 ContainerStatus: string(status), 50 ContainerID: string(cID), 51 Log: log.String(), 52 StartTime: start, 53 }, nil 54 } 55 56 var _ ResourceInfoView = DCResourceInfo{} 57 58 func (DCResourceInfo) resourceInfoView() {} 59 func (dcInfo DCResourceInfo) RuntimeLog() model.Log { return dcInfo.Log } 60 func (dcInfo DCResourceInfo) Status() string { return string(dcInfo.ContainerStatus) } 61 62 type K8sResourceInfo struct { 63 PodName string `json:"podName"` 64 PodCreationTime time.Time `json:"podCreationTime"` 65 PodUpdateStartTime time.Time `json:"podUpdateStartTime"` 66 PodStatus string `json:"podStatus"` 67 PodStatusMessage string `json:"podStatusMessage"` 68 AllContainersReady bool `json:"allContainersReady"` 69 PodRestarts int `json:"podRestarts"` 70 PodLog model.Log `json:"podLog"` 71 } 72 73 var _ ResourceInfoView = K8sResourceInfo{} 74 75 func (K8sResourceInfo) resourceInfoView() {} 76 func (k8sInfo K8sResourceInfo) RuntimeLog() model.Log { return k8sInfo.PodLog } 77 func (k8sInfo K8sResourceInfo) Status() string { 78 status := k8sInfo.PodStatus 79 if status == "Running" && !k8sInfo.AllContainersReady { 80 status = "Pending" 81 } 82 return status 83 } 84 85 type YAMLResourceInfo struct { 86 K8sResources []string `json:"k8sResources"` 87 } 88 89 var _ ResourceInfoView = YAMLResourceInfo{} 90 91 func (YAMLResourceInfo) resourceInfoView() {} 92 func (yamlInfo YAMLResourceInfo) RuntimeLog() model.Log { return model.NewLog("") } 93 func (yamlInfo YAMLResourceInfo) Status() string { return "" } 94 95 // Local resources have no run time info, so it's all empty. 96 type LocalResourceInfo struct{} 97 98 var _ ResourceInfoView = LocalResourceInfo{} 99 100 func (LocalResourceInfo) resourceInfoView() {} 101 func (LocalResourceInfo) RuntimeLog() model.Log { return model.NewLog("") } 102 func (LocalResourceInfo) Status() string { return "" } 103 104 type BuildRecord struct { 105 Edits []string `json:"edits"` 106 Error string `json:"error"` 107 Warnings []string `json:"warnings"` 108 StartTime time.Time `json:"startTime"` 109 FinishTime time.Time `json:"finishTime"` // IsZero() == true for in-progress builds 110 Log model.Log `json:"log"` 111 IsCrashRebuild bool `json:"isCrashRebuild"` 112 } 113 114 func ToWebViewBuildRecord(br model.BuildRecord) BuildRecord { 115 e := "" 116 if br.Error != nil { 117 e = br.Error.Error() 118 } 119 return BuildRecord{ 120 Edits: br.Edits, 121 Error: e, 122 Warnings: br.Warnings, 123 StartTime: br.StartTime, 124 FinishTime: br.FinishTime, 125 Log: br.Log, 126 IsCrashRebuild: br.Reason.IsCrashOnly(), 127 } 128 } 129 130 func timeToProto(t time.Time) (*timestamp.Timestamp, error) { 131 ts, err := ptypes.TimestampProto(t) 132 if err != nil { 133 return nil, err 134 } 135 136 return ts, nil 137 } 138 139 func ToProtoBuildRecord(br model.BuildRecord) (*proto_webview.BuildRecord, error) { 140 e := "" 141 if br.Error != nil { 142 e = br.Error.Error() 143 } 144 145 start, err := timeToProto(br.StartTime) 146 if err != nil { 147 return nil, err 148 } 149 finish, err := timeToProto(br.FinishTime) 150 if err != nil { 151 return nil, err 152 } 153 154 return &proto_webview.BuildRecord{ 155 Edits: br.Edits, 156 Error: e, 157 Warnings: br.Warnings, 158 StartTime: start, 159 FinishTime: finish, 160 Log: br.Log.String(), 161 IsCrashRebuild: br.Reason.IsCrashOnly(), 162 }, nil 163 } 164 165 func ToProtoBuildRecords(brs []model.BuildRecord) ([]*proto_webview.BuildRecord, error) { 166 ret := make([]*proto_webview.BuildRecord, len(brs)) 167 for i, br := range brs { 168 r, err := ToProtoBuildRecord(br) 169 if err != nil { 170 return nil, err 171 } 172 ret[i] = r 173 } 174 return ret, nil 175 } 176 177 func ToWebViewBuildRecords(brs []model.BuildRecord) []BuildRecord { 178 ret := make([]BuildRecord, len(brs)) 179 for i, br := range brs { 180 ret[i] = ToWebViewBuildRecord(br) 181 } 182 return ret 183 } 184 185 type Alert struct { 186 AlertType string `json:"alertType"` 187 Header string `json:"header"` 188 Message string `json:"msg"` 189 Timestamp string `json:"timestamp"` 190 ResourceName string `json:"resourceName"` 191 } 192 193 type Resource struct { 194 Name model.ManifestName `json:"name"` 195 DirectoriesWatched []string `json:"directoriesWatched"` 196 PathsWatched []string `json:"pathsWatched"` 197 LastDeployTime time.Time `json:"lastDeployTime"` 198 TriggerMode model.TriggerMode `json:"triggerMode"` 199 200 BuildHistory []BuildRecord `json:"buildHistory"` 201 CurrentBuild BuildRecord `json:"currentBuild"` 202 203 PendingBuildReason model.BuildReason `json:"pendingBuildReason"` 204 PendingBuildEdits []string `json:"pendingBuildEdits"` 205 PendingBuildSince time.Time `json:"pendingBuildSince"` 206 HasPendingChanges bool `json:"hasPendingChanges"` 207 208 Endpoints []string `json:"endpoints"` 209 PodID k8s.PodID `json:"podID"` 210 211 // Only one of these resource info fields will be populated 212 K8sResourceInfo *K8sResourceInfo `json:"k8sResourceInfo,omitempty"` 213 DCResourceInfo *DCResourceInfo `json:"dcResourceInfo,omitempty"` 214 YAMLResourceInfo *YAMLResourceInfo `json:"yamlResourceInfo,omitempty"` 215 LocalResourceInfo *LocalResourceInfo `json:"localResourceInfo,omitempty"` 216 217 RuntimeStatus RuntimeStatus `json:"runtimeStatus"` 218 219 IsTiltfile bool `json:"isTiltfile"` 220 ShowBuildStatus bool `json:"showBuildStatus"` // if true, we show status & time in 'Build Status'; else, "N/A" 221 CombinedLog model.Log `json:"combinedLog"` 222 CrashLog model.Log `json:"crashLog"` 223 224 Alerts []Alert `json:"alerts"` 225 226 Facets []model.Facet `json:"facets"` 227 } 228 229 func (r Resource) ResourceInfo() ResourceInfoView { 230 if r.K8sResourceInfo != nil { 231 return *r.K8sResourceInfo 232 } 233 if r.DCResourceInfo != nil { 234 return *r.DCResourceInfo 235 } 236 if r.YAMLResourceInfo != nil { 237 return *r.YAMLResourceInfo 238 } 239 if r.LocalResourceInfo != nil { 240 return *r.LocalResourceInfo 241 } 242 243 // return an empty info, just to avoid NPEs 244 return K8sResourceInfo{} 245 } 246 247 func (r Resource) LastBuild() BuildRecord { 248 if len(r.BuildHistory) == 0 { 249 return BuildRecord{} 250 } 251 return r.BuildHistory[0] 252 } 253 254 type RuntimeStatus string 255 256 const ( 257 RuntimeStatusOK RuntimeStatus = "ok" 258 RuntimeStatusPending RuntimeStatus = "pending" 259 RuntimeStatusError RuntimeStatus = "error" 260 RuntimeStatusNotApplicable RuntimeStatus = "not_applicable" 261 ) 262 263 type TiltBuild struct { 264 Version string `json:"version"` 265 CommitSHA string `json:"commitSHA"` 266 Date string `json:"date"` 267 Dev bool `json:"dev"` 268 } 269 270 type View struct { 271 Log model.Log `json:"log"` 272 Resources []Resource `json:"resources"` 273 LogTimestamps bool `json:"logTimestamps"` 274 275 FeatureFlags map[string]bool `json:"featureFlags"` 276 277 NeedsAnalyticsNudge bool `json:"needsAnalyticsNudge"` 278 279 RunningTiltBuild TiltBuild `json:"runningTiltBuild"` 280 LatestTiltBuild TiltBuild `json:"latestTiltBuild"` 281 282 TiltCloudUsername string `json:"tiltCloudUsername"` 283 TiltCloudSchemeHost string `json:"tiltCloudSchemeHost"` 284 TiltCloudTeamID string `json:"tiltCloudTeamID"` 285 286 FatalError string `json:"fatalError"` 287 }