github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/status.tsx (about)

     1  import { buildWarningCount, runtimeWarningCount } from "./alerts"
     2  import { Hold } from "./Hold"
     3  import { LogAlertIndex } from "./LogStore"
     4  import { resourceIsDisabled } from "./ResourceStatus"
     5  import {
     6    ResourceStatus,
     7    RuntimeStatus,
     8    UIResource,
     9    UIResourceStatus,
    10    UpdateStatus,
    11  } from "./types"
    12  
    13  function buildStatus(r: UIResource, alertIndex: LogAlertIndex): ResourceStatus {
    14    let res: UIResourceStatus = r.status || {}
    15  
    16    if (resourceIsDisabled(r)) {
    17      return ResourceStatus.Disabled
    18    } else if (res.updateStatus == UpdateStatus.InProgress) {
    19      return ResourceStatus.Building
    20    } else if (res.updateStatus == UpdateStatus.Pending) {
    21      return ResourceStatus.Pending
    22    } else if (
    23      res.updateStatus == UpdateStatus.NotApplicable ||
    24      res.updateStatus == UpdateStatus.None
    25    ) {
    26      return ResourceStatus.None
    27    } else if (res.updateStatus == UpdateStatus.Error) {
    28      return ResourceStatus.Unhealthy
    29    } else if (buildWarningCount(r, alertIndex) > 0) {
    30      // Warnings are derived from the log store, so that clearing
    31      // logs clears the warning indicator.
    32      return ResourceStatus.Warning
    33    } else if (res.updateStatus == UpdateStatus.Ok) {
    34      return ResourceStatus.Healthy
    35    }
    36    return ResourceStatus.None
    37  }
    38  
    39  function runtimeStatus(
    40    r: UIResource,
    41    alertIndex: LogAlertIndex
    42  ): ResourceStatus {
    43    let res: UIResourceStatus = r.status || {}
    44  
    45    // Regardless of warning logs, check if a resource
    46    // is disabled to return a disabled status
    47    if (resourceIsDisabled(r)) {
    48      return ResourceStatus.Disabled
    49    }
    50  
    51    // Warnings are derived from the log store, so that clearing
    52    // logs clears the warning indicator.
    53    let hasWarnings = runtimeWarningCount(r, alertIndex) > 0
    54    if (hasWarnings) {
    55      if (res.runtimeStatus === RuntimeStatus.Error) {
    56        return ResourceStatus.Unhealthy
    57      } else {
    58        return ResourceStatus.Warning
    59      }
    60    }
    61  
    62    switch (res.runtimeStatus) {
    63      case RuntimeStatus.Error:
    64        return ResourceStatus.Unhealthy
    65      case RuntimeStatus.Pending:
    66        return ResourceStatus.Pending
    67      case RuntimeStatus.Ok:
    68        return ResourceStatus.Healthy
    69      case RuntimeStatus.NotApplicable:
    70      case RuntimeStatus.None:
    71        return ResourceStatus.None
    72    }
    73    return ResourceStatus.None
    74  }
    75  
    76  // A combination of runtime status and build status over a resource view.
    77  // 1) If there's a current or pending build, this is "pending".
    78  // 2) Otherwise, if there's a build error or runtime error, this is "error".
    79  // 3) Otherwise, we fallback to runtime status.
    80  function combinedStatus(
    81    buildStatus: ResourceStatus,
    82    runtimeStatus: ResourceStatus
    83  ): ResourceStatus {
    84    if (
    85      buildStatus !== ResourceStatus.Healthy &&
    86      buildStatus !== ResourceStatus.None
    87    ) {
    88      return buildStatus
    89    }
    90  
    91    if (runtimeStatus === ResourceStatus.None) {
    92      return buildStatus
    93    }
    94  
    95    return runtimeStatus
    96  }
    97  
    98  export function PendingBuildDescription(hold?: Hold | null): string {
    99    let text = "Update: "
   100    if (!hold?.count) {
   101      text += "pending"
   102      return text
   103    }
   104  
   105    text += "waiting on "
   106    const maxToShow = 3
   107    let toShow: string[] = []
   108    if (hold?.images.length) {
   109      text += hold.images.length > 1 ? "images: " : "image: "
   110      toShow = hold.images
   111    } else if (hold?.resources.length) {
   112      text += hold.resources.length > 1 ? "resources: " : "resource: "
   113      toShow = hold.resources
   114    } else if (hold?.clusters.length) {
   115      text += hold.clusters.length > 1 ? "clusters: " : "cluster: "
   116      toShow = hold.clusters
   117    } else {
   118      text += `${hold.count} object${hold.count > 1 ? "s" : ""}`
   119      return text
   120    }
   121  
   122    text += toShow.slice(0, maxToShow).join(", ")
   123    if (toShow.length > maxToShow) {
   124      const extra = toShow.length - maxToShow
   125      text += `, and ${extra} more`
   126    }
   127  
   128    return text
   129  }
   130  
   131  export { buildStatus, runtimeStatus, combinedStatus }