From 8dfc5052e9bea9303261631295c9400f0a2d9235 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sat, 30 Mar 2024 10:37:31 -0600 Subject: [PATCH] ditch more FE enums for clarity and cleanliness --- .../app/components/status/status.component.ts | 2 - .../apps-routes/app-show/app-show.page.html | 4 +- .../apps-routes/app-show/app-show.page.ts | 13 +- .../app-show-status.component.ts | 11 +- .../built-in/health/health.component.ts | 8 +- .../ui/src/app/services/api/api.types.ts | 23 +--- .../ui/src/app/services/dep-error.service.ts | 60 ++------- .../services/pkg-status-rendering.service.ts | 116 +++++++----------- .../ui/src/app/util/get-package-info.ts | 13 +- 9 files changed, 80 insertions(+), 170 deletions(-) diff --git a/web/projects/ui/src/app/components/status/status.component.ts b/web/projects/ui/src/app/components/status/status.component.ts index 03894406e..c9fec4968 100644 --- a/web/projects/ui/src/app/components/status/status.component.ts +++ b/web/projects/ui/src/app/components/status/status.component.ts @@ -3,7 +3,6 @@ import { ConnectionService } from 'src/app/services/connection.service' import { InstallingInfo } from 'src/app/services/patch-db/data-model' import { PrimaryRendering, - PrimaryStatus, StatusRendering, } from 'src/app/services/pkg-status-rendering.service' @@ -13,7 +12,6 @@ import { styleUrls: ['./status.component.scss'], }) export class StatusComponent { - PS = PrimaryStatus PR = PrimaryRendering @Input() rendering!: StatusRendering diff --git a/web/projects/ui/src/app/pages/apps-routes/app-show/app-show.page.html b/web/projects/ui/src/app/pages/apps-routes/app-show/app-show.page.html index 238d1d208..5b3123131 100644 --- a/web/projects/ui/src/app/pages/apps-routes/app-show/app-show.page.html +++ b/web/projects/ui/src/app/pages/apps-routes/app-show/app-show.page.html @@ -17,9 +17,9 @@ - + any) | null = null if (depError) { - if (depError.type === DependencyErrorType.NotInstalled) { + if (depError.type === 'notInstalled') { errorText = 'Not installed' fixText = 'Install' fixAction = () => this.fixDep(pkg, manifest, 'install', depId) - } else if (depError.type === DependencyErrorType.IncorrectVersion) { + } else if (depError.type === 'incorrectVersion') { errorText = 'Incorrect version' fixText = 'Update' fixAction = () => this.fixDep(pkg, manifest, 'update', depId) - } else if (depError.type === DependencyErrorType.ConfigUnsatisfied) { + } else if (depError.type === 'configUnsatisfied') { errorText = 'Config not satisfied' fixText = 'Auto config' fixAction = () => this.fixDep(pkg, manifest, 'configure', depId) - } else if (depError.type === DependencyErrorType.NotRunning) { + } else if (depError.type === 'notRunning') { errorText = 'Not running' fixText = 'Start' - } else if (depError.type === DependencyErrorType.HealthChecksFailed) { + } else if (depError.type === 'healthChecksFailed') { errorText = 'Required health check not passing' - } else if (depError.type === DependencyErrorType.Transitive) { + } else if (depError.type === 'transitive') { errorText = 'Dependency has a dependency issue' } } diff --git a/web/projects/ui/src/app/pages/apps-routes/app-show/components/app-show-status/app-show-status.component.ts b/web/projects/ui/src/app/pages/apps-routes/app-show/components/app-show-status/app-show-status.component.ts index d67ce2941..23d9affd4 100644 --- a/web/projects/ui/src/app/pages/apps-routes/app-show/components/app-show-status/app-show-status.component.ts +++ b/web/projects/ui/src/app/pages/apps-routes/app-show/components/app-show-status/app-show-status.component.ts @@ -3,7 +3,6 @@ import { UiLauncherService } from 'src/app/services/ui-launcher.service' import { PackageStatus, PrimaryRendering, - PrimaryStatus, } from 'src/app/services/pkg-status-rendering.service' import { DataModel, @@ -67,19 +66,15 @@ export class AppShowStatusComponent { } get isRunning(): boolean { - return this.status.primary === PrimaryStatus.Running + return this.status.primary === 'running' } get canStop(): boolean { - return [ - PrimaryStatus.Running, - PrimaryStatus.Starting, - PrimaryStatus.Restarting, - ].includes(this.status.primary) + return ['running', 'starting', 'restarting'].includes(this.status.primary) } get isStopped(): boolean { - return this.status.primary === PrimaryStatus.Stopped + return this.status.primary === 'stopped' } get sigtermTimeout(): string | null { diff --git a/web/projects/ui/src/app/pages/widgets/built-in/health/health.component.ts b/web/projects/ui/src/app/pages/widgets/built-in/health/health.component.ts index bc496b219..8586c3b35 100644 --- a/web/projects/ui/src/app/pages/widgets/built-in/health/health.component.ts +++ b/web/projects/ui/src/app/pages/widgets/built-in/health/health.component.ts @@ -5,7 +5,6 @@ import { DataModel, PackageDataEntry, } from 'src/app/services/patch-db/data-model' -import { PrimaryStatus } from 'src/app/services/pkg-status-rendering.service' import { getPackageInfo, PkgInfo } from '../../../../util/get-package-info' import { combineLatest } from 'rxjs' import { DepErrorService } from 'src/app/services/dep-error.service' @@ -56,14 +55,11 @@ export class HealthComponent { private getCount(label: string, pkgs: PkgInfo[]): number { switch (label) { case 'Error': - return pkgs.filter( - a => a.primaryStatus !== PrimaryStatus.Stopped && a.error, - ).length + return pkgs.filter(a => a.primaryStatus !== 'stopped' && a.error).length case 'Needs Attention': return pkgs.filter(a => a.warning).length case 'Stopped': - return pkgs.filter(a => a.primaryStatus === PrimaryStatus.Stopped) - .length + return pkgs.filter(a => a.primaryStatus === 'stopped').length case 'Transitioning': return pkgs.filter(a => a.transitioning).length default: diff --git a/web/projects/ui/src/app/services/api/api.types.ts b/web/projects/ui/src/app/services/api/api.types.ts index 43edea08e..38c80187d 100644 --- a/web/projects/ui/src/app/services/api/api.types.ts +++ b/web/projects/ui/src/app/services/api/api.types.ts @@ -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' } diff --git a/web/projects/ui/src/app/services/dep-error.service.ts b/web/projects/ui/src/app/services/dep-error.service.ts index fe145055b..987499a5d 100644 --- a/web/projects/ui/src/app/services/dep-error.service.ts +++ b/web/projects/ui/src/app/services/dep-error.service.ts @@ -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 export type PkgDependencyErrors = Record @@ -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 -} diff --git a/web/projects/ui/src/app/services/pkg-status-rendering.service.ts b/web/projects/ui/src/app/services/pkg-status-rendering.service.ts index d95a5ccdc..a3f19b18f 100644 --- a/web/projects/ui/src/app/services/pkg-status-rendering.service.ts +++ b/web/projects/ui/src/app/services/pkg-status-rendering.service.ts @@ -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 = { - [PrimaryStatus.Installing]: { +export const PrimaryRendering: Record = { + 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 = { - [DependencyStatus.Warning]: { display: 'Issue', color: 'warning' }, - [DependencyStatus.Satisfied]: { display: 'Satisfied', color: 'success' }, +export const DependencyRendering: Record = { + warning: { display: 'Issue', color: 'warning' }, + satisfied: { display: 'Satisfied', color: 'success' }, } -export const HealthRendering: Record = { - [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 = { + 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' }, } diff --git a/web/projects/ui/src/app/util/get-package-info.ts b/web/projects/ui/src/app/util/get-package-info.ts index 936ed9954..3e506ea61 100644 --- a/web/projects/ui/src/app/util/get-package-info.ts +++ b/web/projects/ui/src/app/util/get-package-info.ts @@ -1,7 +1,5 @@ import { PackageDataEntry } from '../services/patch-db/data-model' import { - DependencyStatus, - HealthStatus, PrimaryRendering, PrimaryStatus, renderPkgStatus, @@ -21,15 +19,12 @@ export function getPackageInfo( entry, primaryRendering, primaryStatus: statuses.primary, - error: - statuses.health === HealthStatus.Failure || - statuses.dependency === DependencyStatus.Warning, - warning: statuses.primary === PrimaryStatus.NeedsConfig, + error: statuses.health === 'failure' || statuses.dependency === 'warning', + warning: statuses.primary === 'needsConfig', transitioning: primaryRendering.showDots || - statuses.health === HealthStatus.Waiting || - statuses.health === HealthStatus.Loading || - statuses.health === HealthStatus.Starting, + statuses.health === 'loading' || + statuses.health === 'starting', } }