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

     1  import { FilterLevel, FilterSource } from "./logfilters"
     2  import { LogAlert, LogAlertIndex } from "./LogStore"
     3  import { LogLevel, UIResource } from "./types"
     4  
     5  export type Alert = {
     6    source: FilterSource
     7    level: FilterLevel
     8    resourceName: string
     9  }
    10  
    11  // Runtime alerts can only come from the current pod.
    12  //
    13  // All alerts are derived from the log store, to ensure that clearing logs
    14  // also clears the alerts.
    15  //
    16  // TODO(nick): Add support for docker compose, local resource runtime alerts.
    17  function runtimeAlerts(r: UIResource, alertIndex: LogAlertIndex): Alert[] {
    18    let name = r.metadata?.name || ""
    19    let spanId = r.status?.k8sResourceInfo?.spanID || ""
    20    if (!spanId) {
    21      return []
    22    }
    23    return alertIndex.alertsForSpanId(spanId).map((logAlert: LogAlert): Alert => {
    24      return {
    25        resourceName: name,
    26        source: FilterSource.runtime,
    27        level:
    28          logAlert.level == LogLevel.WARN ? FilterLevel.warn : FilterLevel.error,
    29      }
    30    })
    31  }
    32  
    33  // Build alerts can only come from the most recently finished build.
    34  //
    35  // All alerts are derived from the log store, to ensure that clearing logs
    36  // also clears the alerts.
    37  function buildAlerts(r: UIResource, alertIndex: LogAlertIndex): Alert[] {
    38    let name = r.metadata?.name || ""
    39    const latestBuild = (r.status?.buildHistory ?? [])[0]
    40    const spanId = latestBuild?.spanID ?? ""
    41    if (!spanId) {
    42      return []
    43    }
    44    return alertIndex.alertsForSpanId(spanId).map((logAlert: LogAlert): Alert => {
    45      return {
    46        resourceName: name,
    47        source: FilterSource.build,
    48        level:
    49          logAlert.level == LogLevel.WARN ? FilterLevel.warn : FilterLevel.error,
    50      }
    51    })
    52  }
    53  
    54  function combinedAlerts(r: UIResource, alertIndex: LogAlertIndex): Alert[] {
    55    let result = runtimeAlerts(r, alertIndex)
    56    result.push(...buildAlerts(r, alertIndex))
    57    return result
    58  }
    59  
    60  function buildWarningCount(r: UIResource, alertIndex: LogAlertIndex): number {
    61    return buildAlerts(r, alertIndex).filter((alert) => {
    62      return alert.level == FilterLevel.warn
    63    }).length
    64  }
    65  
    66  function runtimeWarningCount(r: UIResource, alertIndex: LogAlertIndex): number {
    67    return runtimeAlerts(r, alertIndex).filter((alert) => {
    68      return alert.level == FilterLevel.warn
    69    }).length
    70  }
    71  
    72  export {
    73    combinedAlerts,
    74    buildAlerts,
    75    runtimeAlerts,
    76    buildWarningCount,
    77    runtimeWarningCount,
    78  }