ditch more FE enums for clarity and cleanliness

This commit is contained in:
Matt Hill
2024-03-30 10:37:31 -06:00
parent 2c308ccd35
commit 8dfc5052e9
9 changed files with 80 additions and 170 deletions

View File

@@ -495,40 +495,29 @@ export type DependencyError =
| DependencyErrorHealthChecksFailed
| DependencyErrorTransitive
export enum DependencyErrorType {
NotInstalled = 'not-installed',
NotRunning = 'not-running',
IncorrectVersion = 'incorrect-version',
ConfigUnsatisfied = 'config-unsatisfied',
HealthChecksFailed = 'health-checks-failed',
InterfaceHealthChecksFailed = 'interface-health-checks-failed',
Transitive = 'transitive',
}
export interface DependencyErrorNotInstalled {
type: DependencyErrorType.NotInstalled
type: 'notInstalled'
}
export interface DependencyErrorNotRunning {
type: DependencyErrorType.NotRunning
type: 'notRunning'
}
export interface DependencyErrorIncorrectVersion {
type: DependencyErrorType.IncorrectVersion
type: 'incorrectVersion'
expected: string // version range
received: string // version
}
export interface DependencyErrorConfigUnsatisfied {
type: DependencyErrorType.ConfigUnsatisfied
error: string
type: 'configUnsatisfied'
}
export interface DependencyErrorHealthChecksFailed {
type: DependencyErrorType.HealthChecksFailed
type: 'healthChecksFailed'
check: HealthCheckResult
}
export interface DependencyErrorTransitive {
type: DependencyErrorType.Transitive
type: 'transitive'
}

View File

@@ -9,6 +9,7 @@ import {
} from './patch-db/data-model'
import * as deepEqual from 'fast-deep-equal'
import { isInstalled } from '../util/get-package-data'
import { DependencyError } from './api/api.types'
export type AllDependencyErrors = Record<string, PkgDependencyErrors>
export type PkgDependencyErrors = Record<string, DependencyError | null>
@@ -78,7 +79,7 @@ export class DepErrorService {
// not installed
if (!dep || dep.stateInfo.state !== 'installed') {
return {
type: DependencyErrorType.NotInstalled,
type: 'notInstalled',
}
}
@@ -88,7 +89,7 @@ export class DepErrorService {
// incorrect version
if (!this.emver.satisfies(depManifest.version, versionSpec)) {
return {
type: DependencyErrorType.IncorrectVersion,
type: 'incorrectVersion',
expected: versionSpec,
received: depManifest.version,
}
@@ -97,7 +98,7 @@ export class DepErrorService {
// invalid config
if (Object.values(pkg.status.dependencyConfigErrors).some(err => !!err)) {
return {
type: DependencyErrorType.ConfigUnsatisfied,
type: 'configUnsatisfied',
}
}
@@ -106,7 +107,7 @@ export class DepErrorService {
// not running
if (depStatus !== 'running' && depStatus !== 'starting') {
return {
type: DependencyErrorType.NotRunning,
type: 'notRunning',
}
}
@@ -115,9 +116,11 @@ export class DepErrorService {
// health check failure
if (depStatus === 'running' && currentDep.kind === 'running') {
for (let id of currentDep.healthChecks) {
if (dep.status.main.health[id]?.result !== 'success') {
const check = dep.status.main.health[id]
if (check?.result !== 'success') {
return {
type: DependencyErrorType.HealthChecksFailed,
type: 'healthChecksFailed',
check,
}
}
}
@@ -130,7 +133,7 @@ export class DepErrorService {
if (transitiveError) {
return {
type: DependencyErrorType.Transitive,
type: 'transitive',
}
}
@@ -154,46 +157,3 @@ function dependencyDepth(
depth,
)
}
export type DependencyError =
| DependencyErrorNotInstalled
| DependencyErrorNotRunning
| DependencyErrorIncorrectVersion
| DependencyErrorConfigUnsatisfied
| DependencyErrorHealthChecksFailed
| DependencyErrorTransitive
export enum DependencyErrorType {
NotInstalled = 'notInstalled',
NotRunning = 'notRunning',
IncorrectVersion = 'incorrectVersion',
ConfigUnsatisfied = 'configUnsatisfied',
HealthChecksFailed = 'healthChecksFailed',
Transitive = 'transitive',
}
export interface DependencyErrorNotInstalled {
type: DependencyErrorType.NotInstalled
}
export interface DependencyErrorNotRunning {
type: DependencyErrorType.NotRunning
}
export interface DependencyErrorIncorrectVersion {
type: DependencyErrorType.IncorrectVersion
expected: string // version range
received: string // version
}
export interface DependencyErrorConfigUnsatisfied {
type: DependencyErrorType.ConfigUnsatisfied
}
export interface DependencyErrorHealthChecksFailed {
type: DependencyErrorType.HealthChecksFailed
}
export interface DependencyErrorTransitive {
type: DependencyErrorType.Transitive
}

View File

@@ -1,12 +1,12 @@
import { isEmptyObject } from '@start9labs/shared'
import { PackageDataEntry } from 'src/app/services/patch-db/data-model'
import { PkgDependencyErrors } from './dep-error.service'
import { Status } from '../../../../../../core/startos/bindings/Status'
import { T } from '@start9labs/start-sdk'
export interface PackageStatus {
primary: PrimaryStatus
dependency: DependencyStatus | null
health: HealthStatus | null
health: T.HealthStatus | null
}
export function renderPkgStatus(
@@ -15,59 +15,51 @@ export function renderPkgStatus(
): PackageStatus {
let primary: PrimaryStatus
let dependency: DependencyStatus | null = null
let health: HealthStatus | null = null
let health: T.HealthStatus | null = null
if (pkg.stateInfo.state === 'installed') {
primary = getPrimaryStatus(pkg.status)
primary = getInstalledPrimaryStatus(pkg.status)
dependency = getDependencyStatus(depErrors)
health = getHealthStatus(pkg.status)
} else {
primary = pkg.stateInfo.state as string as PrimaryStatus
primary = pkg.stateInfo.state
}
return { primary, dependency, health }
}
function getPrimaryStatus(status: Status): PrimaryStatus {
function getInstalledPrimaryStatus(status: Status): PrimaryStatus {
if (!status.configured) {
return PrimaryStatus.NeedsConfig
} else if (status.main.status === 'restarting') {
return PrimaryStatus.Restarting
return 'needsConfig'
} else {
return status.main.status as any as PrimaryStatus
}
}
function getDependencyStatus(depErrors: PkgDependencyErrors): DependencyStatus {
return Object.values(depErrors).some(err => !!err)
? DependencyStatus.Warning
: DependencyStatus.Satisfied
return Object.values(depErrors).some(err => !!err) ? 'warning' : 'satisfied'
}
function getHealthStatus(status: Status): HealthStatus | null {
function getHealthStatus(status: Status): T.HealthStatus | null {
if (status.main.status !== 'running' || !status.main.health) {
return null
}
const values = Object.values(status.main.health)
if (values.some(h => !h.result)) {
return HealthStatus.Waiting
}
if (values.some(h => h.result === 'failure')) {
return HealthStatus.Failure
return 'failure'
}
if (values.some(h => h.result === 'loading')) {
return HealthStatus.Loading
return 'loading'
}
if (values.some(h => h.result === 'starting')) {
return HealthStatus.Starting
return 'starting'
}
return HealthStatus.Healthy
return 'success'
}
export interface StatusRendering {
@@ -76,102 +68,88 @@ export interface StatusRendering {
showDots?: boolean
}
export enum PrimaryStatus {
// state
Installing = 'installing',
Updating = 'updating',
Removing = 'removing',
Restoring = 'restoring',
// status
Starting = 'starting',
Running = 'running',
Stopping = 'stopping',
Restarting = 'restarting',
Stopped = 'stopped',
BackingUp = 'backing-up',
// config
NeedsConfig = 'needs-config',
}
export type PrimaryStatus =
| 'installing'
| 'updating'
| 'removing'
| 'restoring'
| 'starting'
| 'running'
| 'stopping'
| 'restarting'
| 'stopped'
| 'backingUp'
| 'needsConfig'
export enum DependencyStatus {
Warning = 'warning',
Satisfied = 'satisfied',
}
export type DependencyStatus = 'warning' | 'satisfied'
export enum HealthStatus {
Failure = 'failure',
Waiting = 'waiting',
Starting = 'starting',
Loading = 'loading',
Healthy = 'healthy',
}
export const PrimaryRendering: Record<string, StatusRendering> = {
[PrimaryStatus.Installing]: {
export const PrimaryRendering: Record<PrimaryStatus, StatusRendering> = {
installing: {
display: 'Installing',
color: 'primary',
showDots: true,
},
[PrimaryStatus.Updating]: {
updating: {
display: 'Updating',
color: 'primary',
showDots: true,
},
[PrimaryStatus.Removing]: {
removing: {
display: 'Removing',
color: 'danger',
showDots: true,
},
[PrimaryStatus.Restoring]: {
restoring: {
display: 'Restoring',
color: 'primary',
showDots: true,
},
[PrimaryStatus.Stopping]: {
stopping: {
display: 'Stopping',
color: 'dark-shade',
showDots: true,
},
[PrimaryStatus.Restarting]: {
restarting: {
display: 'Restarting',
color: 'tertiary',
showDots: true,
},
[PrimaryStatus.Stopped]: {
stopped: {
display: 'Stopped',
color: 'dark-shade',
showDots: false,
},
[PrimaryStatus.BackingUp]: {
backingUp: {
display: 'Backing Up',
color: 'primary',
showDots: true,
},
[PrimaryStatus.Starting]: {
starting: {
display: 'Starting',
color: 'primary',
showDots: true,
},
[PrimaryStatus.Running]: {
running: {
display: 'Running',
color: 'success',
showDots: false,
},
[PrimaryStatus.NeedsConfig]: {
needsConfig: {
display: 'Needs Config',
color: 'warning',
showDots: false,
},
}
export const DependencyRendering: Record<string, StatusRendering> = {
[DependencyStatus.Warning]: { display: 'Issue', color: 'warning' },
[DependencyStatus.Satisfied]: { display: 'Satisfied', color: 'success' },
export const DependencyRendering: Record<DependencyStatus, StatusRendering> = {
warning: { display: 'Issue', color: 'warning' },
satisfied: { display: 'Satisfied', color: 'success' },
}
export const HealthRendering: Record<string, StatusRendering> = {
[HealthStatus.Failure]: { display: 'Failure', color: 'danger' },
[HealthStatus.Starting]: { display: 'Starting', color: 'primary' },
[HealthStatus.Loading]: { display: 'Loading', color: 'primary' },
[HealthStatus.Healthy]: { display: 'Healthy', color: 'success' },
export const HealthRendering: Record<T.HealthStatus, StatusRendering> = {
failure: { display: 'Failure', color: 'danger' },
starting: { display: 'Starting', color: 'primary' },
loading: { display: 'Loading', color: 'primary' },
success: { display: 'Healthy', color: 'success' },
disabled: { display: 'Disabled', color: 'dark' },
}