mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-02 05:23:14 +00:00
0.2.5 initial commit
Makefile incomplete
This commit is contained in:
15
ui/src/app/util/cleanup.ts
Normal file
15
ui/src/app/util/cleanup.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Injectable, OnDestroy } from '@angular/core'
|
||||
import { Subscription } from 'rxjs'
|
||||
|
||||
@Injectable()
|
||||
export abstract class Cleanup implements OnDestroy {
|
||||
private toCleanup: Subscription[] = []
|
||||
|
||||
ngOnDestroy () {
|
||||
this.toCleanup.forEach(s => s.unsubscribe())
|
||||
}
|
||||
|
||||
cleanup (...s: Subscription[]) {
|
||||
this.toCleanup.push(...s)
|
||||
}
|
||||
}
|
||||
252
ui/src/app/util/countries.json
Normal file
252
ui/src/app/util/countries.json
Normal file
@@ -0,0 +1,252 @@
|
||||
{
|
||||
"AD": "Andorra",
|
||||
"AE": "United Arab Emirates",
|
||||
"AF": "Afghanistan",
|
||||
"AG": "Antigua and Barbuda",
|
||||
"AI": "Anguilla",
|
||||
"AL": "Albania",
|
||||
"AM": "Armenia",
|
||||
"AO": "Angola",
|
||||
"AQ": "Antarctica",
|
||||
"AR": "Argentina",
|
||||
"AS": "American Samoa",
|
||||
"AT": "Austria",
|
||||
"AU": "Australia",
|
||||
"AW": "Aruba",
|
||||
"AX": "Aland Islands",
|
||||
"AZ": "Azerbaijan",
|
||||
"BA": "Bosnia and Herzegovina",
|
||||
"BB": "Barbados",
|
||||
"BD": "Bangladesh",
|
||||
"BE": "Belgium",
|
||||
"BF": "Burkina Faso",
|
||||
"BG": "Bulgaria",
|
||||
"BH": "Bahrain",
|
||||
"BI": "Burundi",
|
||||
"BJ": "Benin",
|
||||
"BL": "Saint Barthelemy",
|
||||
"BM": "Bermuda",
|
||||
"BN": "Brunei",
|
||||
"BO": "Bolivia",
|
||||
"BQ": "Bonaire, Saint Eustatius and Saba ",
|
||||
"BR": "Brazil",
|
||||
"BS": "Bahamas",
|
||||
"BT": "Bhutan",
|
||||
"BV": "Bouvet Island",
|
||||
"BW": "Botswana",
|
||||
"BY": "Belarus",
|
||||
"BZ": "Belize",
|
||||
"CA": "Canada",
|
||||
"CC": "Cocos Islands",
|
||||
"CD": "Democratic Republic of the Congo",
|
||||
"CF": "Central African Republic",
|
||||
"CG": "Republic of the Congo",
|
||||
"CH": "Switzerland",
|
||||
"CI": "Ivory Coast",
|
||||
"CK": "Cook Islands",
|
||||
"CL": "Chile",
|
||||
"CM": "Cameroon",
|
||||
"CN": "China",
|
||||
"CO": "Colombia",
|
||||
"CR": "Costa Rica",
|
||||
"CU": "Cuba",
|
||||
"CV": "Cape Verde",
|
||||
"CW": "Curacao",
|
||||
"CX": "Christmas Island",
|
||||
"CY": "Cyprus",
|
||||
"CZ": "Czech Republic",
|
||||
"DE": "Germany",
|
||||
"DJ": "Djibouti",
|
||||
"DK": "Denmark",
|
||||
"DM": "Dominica",
|
||||
"DO": "Dominican Republic",
|
||||
"DZ": "Algeria",
|
||||
"EC": "Ecuador",
|
||||
"EE": "Estonia",
|
||||
"EG": "Egypt",
|
||||
"EH": "Western Sahara",
|
||||
"ER": "Eritrea",
|
||||
"ES": "Spain",
|
||||
"ET": "Ethiopia",
|
||||
"FI": "Finland",
|
||||
"FJ": "Fiji",
|
||||
"FK": "Falkland Islands",
|
||||
"FM": "Micronesia",
|
||||
"FO": "Faroe Islands",
|
||||
"FR": "France",
|
||||
"GA": "Gabon",
|
||||
"GB": "United Kingdom",
|
||||
"GD": "Grenada",
|
||||
"GE": "Georgia",
|
||||
"GF": "French Guiana",
|
||||
"GG": "Guernsey",
|
||||
"GH": "Ghana",
|
||||
"GI": "Gibraltar",
|
||||
"GL": "Greenland",
|
||||
"GM": "Gambia",
|
||||
"GN": "Guinea",
|
||||
"GP": "Guadeloupe",
|
||||
"GQ": "Equatorial Guinea",
|
||||
"GR": "Greece",
|
||||
"GS": "South Georgia and the South Sandwich Islands",
|
||||
"GT": "Guatemala",
|
||||
"GU": "Guam",
|
||||
"GW": "Guinea-Bissau",
|
||||
"GY": "Guyana",
|
||||
"HK": "Hong Kong",
|
||||
"HM": "Heard Island and McDonald Islands",
|
||||
"HN": "Honduras",
|
||||
"HR": "Croatia",
|
||||
"HT": "Haiti",
|
||||
"HU": "Hungary",
|
||||
"ID": "Indonesia",
|
||||
"IE": "Ireland",
|
||||
"IL": "Israel",
|
||||
"IM": "Isle of Man",
|
||||
"IN": "India",
|
||||
"IO": "British Indian Ocean Territory",
|
||||
"IQ": "Iraq",
|
||||
"IR": "Iran",
|
||||
"IS": "Iceland",
|
||||
"IT": "Italy",
|
||||
"JE": "Jersey",
|
||||
"JM": "Jamaica",
|
||||
"JO": "Jordan",
|
||||
"JP": "Japan",
|
||||
"KE": "Kenya",
|
||||
"KG": "Kyrgyzstan",
|
||||
"KH": "Cambodia",
|
||||
"KI": "Kiribati",
|
||||
"KM": "Comoros",
|
||||
"KN": "Saint Kitts and Nevis",
|
||||
"KP": "North Korea",
|
||||
"KR": "South Korea",
|
||||
"KW": "Kuwait",
|
||||
"KY": "Cayman Islands",
|
||||
"KZ": "Kazakhstan",
|
||||
"LA": "Laos",
|
||||
"LB": "Lebanon",
|
||||
"LC": "Saint Lucia",
|
||||
"LI": "Liechtenstein",
|
||||
"LK": "Sri Lanka",
|
||||
"LR": "Liberia",
|
||||
"LS": "Lesotho",
|
||||
"LT": "Lithuania",
|
||||
"LU": "Luxembourg",
|
||||
"LV": "Latvia",
|
||||
"LY": "Libya",
|
||||
"MA": "Morocco",
|
||||
"MC": "Monaco",
|
||||
"MD": "Moldova",
|
||||
"ME": "Montenegro",
|
||||
"MF": "Saint Martin",
|
||||
"MG": "Madagascar",
|
||||
"MH": "Marshall Islands",
|
||||
"MK": "Macedonia",
|
||||
"ML": "Mali",
|
||||
"MM": "Myanmar",
|
||||
"MN": "Mongolia",
|
||||
"MO": "Macao",
|
||||
"MP": "Northern Mariana Islands",
|
||||
"MQ": "Martinique",
|
||||
"MR": "Mauritania",
|
||||
"MS": "Montserrat",
|
||||
"MT": "Malta",
|
||||
"MU": "Mauritius",
|
||||
"MV": "Maldives",
|
||||
"MW": "Malawi",
|
||||
"MX": "Mexico",
|
||||
"MY": "Malaysia",
|
||||
"MZ": "Mozambique",
|
||||
"NA": "Namibia",
|
||||
"NC": "New Caledonia",
|
||||
"NE": "Niger",
|
||||
"NF": "Norfolk Island",
|
||||
"NG": "Nigeria",
|
||||
"NI": "Nicaragua",
|
||||
"NL": "Netherlands",
|
||||
"NO": "Norway",
|
||||
"NP": "Nepal",
|
||||
"NR": "Nauru",
|
||||
"NU": "Niue",
|
||||
"NZ": "New Zealand",
|
||||
"OM": "Oman",
|
||||
"PA": "Panama",
|
||||
"PE": "Peru",
|
||||
"PF": "French Polynesia",
|
||||
"PG": "Papua New Guinea",
|
||||
"PH": "Philippines",
|
||||
"PK": "Pakistan",
|
||||
"PL": "Poland",
|
||||
"PM": "Saint Pierre and Miquelon",
|
||||
"PN": "Pitcairn",
|
||||
"PR": "Puerto Rico",
|
||||
"PS": "Palestinian Territory",
|
||||
"PT": "Portugal",
|
||||
"PW": "Palau",
|
||||
"PY": "Paraguay",
|
||||
"QA": "Qatar",
|
||||
"RE": "Reunion",
|
||||
"RO": "Romania",
|
||||
"RS": "Serbia",
|
||||
"RU": "Russia",
|
||||
"RW": "Rwanda",
|
||||
"SA": "Saudi Arabia",
|
||||
"SB": "Solomon Islands",
|
||||
"SC": "Seychelles",
|
||||
"SD": "Sudan",
|
||||
"SE": "Sweden",
|
||||
"SG": "Singapore",
|
||||
"SH": "Saint Helena",
|
||||
"SI": "Slovenia",
|
||||
"SJ": "Svalbard and Jan Mayen",
|
||||
"SK": "Slovakia",
|
||||
"SL": "Sierra Leone",
|
||||
"SM": "San Marino",
|
||||
"SN": "Senegal",
|
||||
"SO": "Somalia",
|
||||
"SR": "Suriname",
|
||||
"SS": "South Sudan",
|
||||
"ST": "Sao Tome and Principe",
|
||||
"SV": "El Salvador",
|
||||
"SX": "Sint Maarten",
|
||||
"SY": "Syria",
|
||||
"SZ": "Swaziland",
|
||||
"TC": "Turks and Caicos Islands",
|
||||
"TD": "Chad",
|
||||
"TF": "French Southern Territories",
|
||||
"TG": "Togo",
|
||||
"TH": "Thailand",
|
||||
"TJ": "Tajikistan",
|
||||
"TK": "Tokelau",
|
||||
"TL": "East Timor",
|
||||
"TM": "Turkmenistan",
|
||||
"TN": "Tunisia",
|
||||
"TO": "Tonga",
|
||||
"TR": "Turkey",
|
||||
"TT": "Trinidad and Tobago",
|
||||
"TV": "Tuvalu",
|
||||
"TW": "Taiwan",
|
||||
"TZ": "Tanzania",
|
||||
"UA": "Ukraine",
|
||||
"UG": "Uganda",
|
||||
"UM": "United States Minor Outlying Islands",
|
||||
"US": "United States",
|
||||
"UY": "Uruguay",
|
||||
"UZ": "Uzbekistan",
|
||||
"VA": "Vatican",
|
||||
"VC": "Saint Vincent and the Grenadines",
|
||||
"VE": "Venezuela",
|
||||
"VG": "British Virgin Islands",
|
||||
"VI": "U.S. Virgin Islands",
|
||||
"VN": "Vietnam",
|
||||
"VU": "Vanuatu",
|
||||
"WF": "Wallis and Futuna",
|
||||
"WS": "Samoa",
|
||||
"XK": "Kosovo",
|
||||
"YE": "Yemen",
|
||||
"YT": "Mayotte",
|
||||
"ZA": "South Africa",
|
||||
"ZM": "Zambia",
|
||||
"ZW": "Zimbabwe"
|
||||
}
|
||||
82
ui/src/app/util/map-subject.util.ts
Normal file
82
ui/src/app/util/map-subject.util.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Subject, BehaviorSubject } from 'rxjs'
|
||||
import { PropertySubject, initPropertySubject, complete, peekProperties, PropertySubjectId } from './property-subject.util'
|
||||
import { NgZone } from '@angular/core'
|
||||
import { both, diff } from './misc.util'
|
||||
|
||||
export type Update<T extends { id: string }> = Partial<T> & {
|
||||
id: string
|
||||
}
|
||||
export type Delta<T> = { action: 'add' | 'delete', id: string } | { action: 'update', id: string, effectedFields: Partial<T> }
|
||||
|
||||
export class MapSubject<T extends { id: string }> {
|
||||
contents: { [id: string]: PropertySubject<T> } = { }
|
||||
$delta$ = new Subject<Delta<T>>()
|
||||
|
||||
constructor (
|
||||
private readonly zone: NgZone = new NgZone({ shouldCoalesceEventChangeDetection: true }),
|
||||
) { }
|
||||
|
||||
get ids () : string[] { return Object.keys(this.contents) }
|
||||
get all () : T[] { return this.ids.map(id => this.peek(id) as T) }
|
||||
|
||||
getContents () : PropertySubjectId<T>[] {
|
||||
return Object.entries(this.contents).map( ([k, v]) => ({ id: k, subject: v }))
|
||||
}
|
||||
|
||||
add (t: T): void {
|
||||
this.contents[t.id] = initPropertySubject(t)
|
||||
this.$delta$.next({ action: 'add', id: t.id })
|
||||
}
|
||||
|
||||
delete (id: string): void {
|
||||
const t$ = this.contents[id]
|
||||
if (!t$) return
|
||||
complete(t$)
|
||||
delete this.contents[id]
|
||||
this.$delta$.next({ action: 'delete', id })
|
||||
}
|
||||
|
||||
update (newValues: Update<T>): void {
|
||||
const t$ = this.contents[newValues.id] as PropertySubject<T>
|
||||
|
||||
if (!t$) {
|
||||
this.contents[newValues.id] = initPropertySubject(newValues) as PropertySubject<T>
|
||||
return
|
||||
}
|
||||
|
||||
const effectedFields = { }
|
||||
const oldKeys = Object.keys(t$)
|
||||
const newKeys = Object.keys(newValues)
|
||||
|
||||
const newKeysInUpdate = diff(newKeys, oldKeys)
|
||||
newKeysInUpdate.forEach(keyToAdd => {
|
||||
t$[keyToAdd] = new BehaviorSubject(newValues[keyToAdd])
|
||||
effectedFields[keyToAdd] = newValues[keyToAdd]
|
||||
})
|
||||
|
||||
const keysToUpdate = both(newKeys, oldKeys)
|
||||
keysToUpdate.forEach(keyToUpdate => {
|
||||
const valueToUpdate = newValues[keyToUpdate]
|
||||
if (JSON.stringify(t$[keyToUpdate].getValue()) !== JSON.stringify(valueToUpdate)) {
|
||||
this.zone.run(() => t$[keyToUpdate].next(valueToUpdate))
|
||||
effectedFields[keyToUpdate] = newValues[keyToUpdate]
|
||||
}
|
||||
})
|
||||
|
||||
if (Object.keys(effectedFields).length) {
|
||||
this.$delta$.next({
|
||||
action: 'update',
|
||||
id: newValues.id,
|
||||
effectedFields,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
watch (id: string): undefined | PropertySubject<T> {
|
||||
return this.contents[id]
|
||||
}
|
||||
|
||||
peek (id: string): T | undefined {
|
||||
return this.contents[id] && peekProperties(this.contents[id])
|
||||
}
|
||||
}
|
||||
192
ui/src/app/util/metrics.util.ts
Normal file
192
ui/src/app/util/metrics.util.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import * as Ajv from 'ajv'
|
||||
import { JsonPointer } from 'jsonpointerx'
|
||||
|
||||
const ajv = new Ajv({ jsonPointers: true, allErrors: true, nullable: true })
|
||||
const ajvWithDefaults = new Ajv({ jsonPointers: true, allErrors: true, useDefaults: true, nullable: true, removeAdditional: 'failing' })
|
||||
const schemaV1 = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': { 'type': 'string' },
|
||||
'value': { 'type': 'string' },
|
||||
'description': { 'type': 'string', 'nullable': true, 'default': null },
|
||||
'copyable': { 'type': 'boolean', 'default': false },
|
||||
'qr': { 'type': 'boolean', 'default': false },
|
||||
},
|
||||
'required': ['name', 'value', 'copyable', 'qr'],
|
||||
'additionalProperties': false,
|
||||
}
|
||||
const schemaV1Compiled = ajv.compile(schemaV1)
|
||||
const schemaV1CompiledWithDefaults = ajvWithDefaults.compile(schemaV1)
|
||||
const schemaV2 = {
|
||||
'anyOf': [
|
||||
{
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'type': { 'type': 'string', 'const': 'string' },
|
||||
'value': { 'type': 'string' },
|
||||
'description': { 'type': 'string', 'nullable': true, 'default': null },
|
||||
'copyable': { 'type': 'boolean', 'default': false },
|
||||
'qr': { 'type': 'boolean', 'default': false },
|
||||
'masked': { 'type': 'boolean', 'default': false },
|
||||
},
|
||||
'required': ['type', 'value', 'description', 'copyable', 'qr', 'masked'],
|
||||
'additionalProperties': false,
|
||||
},
|
||||
{
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'type': { 'type': 'string', 'const': 'object' },
|
||||
'value': {
|
||||
'type': 'object',
|
||||
'patternProperties': {
|
||||
'^.*$': {
|
||||
'$ref': '#',
|
||||
},
|
||||
},
|
||||
},
|
||||
'description': { 'type': 'string', 'nullable': true, 'default': null },
|
||||
|
||||
},
|
||||
'required': ['type', 'value', 'description'],
|
||||
'additionalProperties': false,
|
||||
},
|
||||
],
|
||||
}
|
||||
const schemaV2Compiled = ajv.compile(schemaV2)
|
||||
const schemaV2CompiledWithDefaults = ajvWithDefaults.compile(schemaV2)
|
||||
|
||||
export function parseMetricsPermissive (metrics: any, errorCallback: (err: Error) => any = console.warn): AppMetrics {
|
||||
if (typeof metrics !== 'object' || metrics === null) {
|
||||
errorCallback(new TypeError(`${metrics} is not an object`))
|
||||
return { }
|
||||
}
|
||||
if (typeof metrics.version !== 'number' || !metrics.data) {
|
||||
return Object.entries(metrics)
|
||||
.filter(([_, value]) => {
|
||||
if (typeof value === 'string') {
|
||||
return true
|
||||
} else {
|
||||
errorCallback(new TypeError(`${value} is not a string`))
|
||||
return false
|
||||
}
|
||||
})
|
||||
.map(([name, value]) => ({
|
||||
name,
|
||||
value: {
|
||||
value: String(value),
|
||||
description: null,
|
||||
copyable: false,
|
||||
qr: false,
|
||||
masked: false,
|
||||
},
|
||||
}))
|
||||
.reduce((acc, { name, value }) => {
|
||||
acc[name] = value
|
||||
return acc
|
||||
}, { })
|
||||
}
|
||||
const typedMetrics = metrics as AppMetricsVersioned<number>
|
||||
switch (typedMetrics.version) {
|
||||
case 1:
|
||||
return parseMetricsV1Permissive(typedMetrics.data, errorCallback)
|
||||
case 2:
|
||||
return parseMetricsV2Permissive(typedMetrics.data, errorCallback)
|
||||
default:
|
||||
errorCallback(new Error(`unknown metrics version ${metrics.version}, attempting to parse as v2`))
|
||||
return parseMetricsV2Permissive(typedMetrics.data, errorCallback)
|
||||
}
|
||||
}
|
||||
|
||||
function parseMetricsV1Permissive (metrics: AppMetricsV1, errorCallback: (err: Error) => any): AppMetrics {
|
||||
return metrics.reduce((prev: AppMetricsV2, cur: AppMetricV1, idx: number) => {
|
||||
schemaV1Compiled(cur)
|
||||
if (schemaV1Compiled.errors) {
|
||||
for (let err of schemaV1Compiled.errors) {
|
||||
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`))
|
||||
if (err.dataPath) {
|
||||
JsonPointer.set(cur, err.dataPath, undefined)
|
||||
}
|
||||
}
|
||||
if (!schemaV1CompiledWithDefaults(cur)) {
|
||||
for (let err of schemaV1CompiledWithDefaults.errors) {
|
||||
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`))
|
||||
}
|
||||
return prev
|
||||
}
|
||||
}
|
||||
prev[cur.name] = {
|
||||
type: 'string',
|
||||
value: cur.value,
|
||||
description: cur.description,
|
||||
copyable: cur.copyable,
|
||||
qr: cur.qr,
|
||||
masked: false,
|
||||
}
|
||||
return prev
|
||||
}, { })
|
||||
}
|
||||
|
||||
function parseMetricsV2Permissive (metrics: AppMetricsV2, errorCallback: (err: Error) => any): AppMetrics {
|
||||
return Object.entries(metrics).reduce((prev, [name, value], idx) => {
|
||||
schemaV2Compiled(value)
|
||||
if (schemaV2Compiled.errors) {
|
||||
for (let err of schemaV2Compiled.errors) {
|
||||
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`))
|
||||
if (err.dataPath) {
|
||||
JsonPointer.set(value, err.dataPath, undefined)
|
||||
}
|
||||
}
|
||||
if (!schemaV2CompiledWithDefaults(value)) {
|
||||
for (let err of schemaV2CompiledWithDefaults.errors) {
|
||||
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`))
|
||||
}
|
||||
return prev
|
||||
}
|
||||
}
|
||||
prev[name] = value
|
||||
return prev
|
||||
}, { })
|
||||
}
|
||||
|
||||
export type AppMetrics = AppMetricsV2 // chnage this type when updating versions
|
||||
|
||||
export type AppMetricsVersioned<T extends number> = {
|
||||
version: T,
|
||||
data: AppMetricsVersionedData<T>
|
||||
}
|
||||
|
||||
export type AppMetricsVersionedData<T extends number> = T extends 1 ? AppMetricsV1 :
|
||||
T extends 2 ? AppMetricsV2 :
|
||||
never
|
||||
|
||||
interface AppMetricV1 {
|
||||
name: string
|
||||
value: string
|
||||
description: string | null
|
||||
copyable: boolean
|
||||
qr: boolean
|
||||
}
|
||||
|
||||
type AppMetricsV1 = AppMetricV1[]
|
||||
|
||||
interface AppMetricsV2 {
|
||||
[name: string]: AppMetricString | AppMetricObject
|
||||
}
|
||||
|
||||
interface AppMetricBase {
|
||||
type: 'string' | 'object'
|
||||
description: string | null
|
||||
}
|
||||
|
||||
interface AppMetricString extends AppMetricBase {
|
||||
type: 'string'
|
||||
value: string
|
||||
copyable: boolean
|
||||
qr: boolean
|
||||
masked: boolean
|
||||
}
|
||||
|
||||
interface AppMetricObject extends AppMetricBase {
|
||||
type: 'object'
|
||||
value: AppMetricsV2
|
||||
}
|
||||
161
ui/src/app/util/misc.util.ts
Normal file
161
ui/src/app/util/misc.util.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
export type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>
|
||||
export type PromiseRes<T> = { result: 'resolve', value: T } | { result: 'reject', value: Error }
|
||||
|
||||
import { OperatorFunction } from 'rxjs'
|
||||
import { map } from 'rxjs/operators'
|
||||
|
||||
export function trace<T> (t: T): T {
|
||||
console.log(`TRACE`, t)
|
||||
return t
|
||||
}
|
||||
|
||||
// curried description. This allows e.g somePromise.thentraceDesc('my result'))
|
||||
export function traceDesc<T> (description: string): (t: T) => T {
|
||||
return t => {
|
||||
console.log(`TRACE`, description, t)
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
// for use in observables. This allows e.g. someObservable.pipe(traceM('my result'))
|
||||
// the practical equivalent of `tap(t => console.log(t, description))`
|
||||
export function traceWheel<T> (description?: string): OperatorFunction<T, T> {
|
||||
return description ? map(traceDesc(description)) : map(trace)
|
||||
}
|
||||
|
||||
export function traceThrowDesc<T> (description: string, t: T | undefined): T {
|
||||
if (!t) throw new Error(description)
|
||||
return t
|
||||
}
|
||||
|
||||
export function thenReturn<T> (act1 : () => Promise<any>, t: T): Promise<T> {
|
||||
return act1().then(() => t)
|
||||
}
|
||||
|
||||
export function modulateTime (ts: Date, count: number, unit: 'days' | 'hours' | 'minutes' | 'seconds' ) {
|
||||
const ms = inMs(count, unit)
|
||||
const toReturn = new Date(ts)
|
||||
toReturn.setMilliseconds( toReturn.getMilliseconds() + ms)
|
||||
return toReturn
|
||||
}
|
||||
|
||||
export function inMs ( count: number, unit: 'days' | 'hours' | 'minutes' | 'seconds' ) {
|
||||
switch (unit){
|
||||
case 'seconds' : return count * 1000
|
||||
case 'minutes' : return inMs(count * 60, 'seconds')
|
||||
case 'hours' : return inMs(count * 60, 'minutes')
|
||||
case 'days' : return inMs(count * 24, 'hours')
|
||||
}
|
||||
}
|
||||
|
||||
export async function tryAll<T1, T2> ( promises: [Promise<T1>, Promise<T2>]): Promise<[PromiseRes<T1>, PromiseRes<T2>]>
|
||||
export async function tryAll ( promises: Promise<any>[] ): Promise<PromiseRes<any>[]> {
|
||||
return Promise.all(promises.map(
|
||||
p => p
|
||||
.then (r => ({ result: 'resolve' as 'resolve', value: r }))
|
||||
.catch(e => ({ result: 'reject' as 'reject', value: e })),
|
||||
))
|
||||
}
|
||||
|
||||
// arr1 - arr2
|
||||
export function diff<T> (arr1: T[], arr2: T[]): T[] {
|
||||
return arr1.filter(x => !arr2.includes(x))
|
||||
}
|
||||
|
||||
// arr1 & arr2
|
||||
export function both<T> (arr1: T[], arr2: T[]): T[] {
|
||||
return arr1.filter(x => arr2.includes(x))
|
||||
}
|
||||
|
||||
export async function doForAtLeast (promises: Promise<any>[], minTime: number): Promise<any[]> {
|
||||
const returned = await Promise.all(promises.concat(pauseFor(minTime)))
|
||||
returned.pop()
|
||||
return returned
|
||||
}
|
||||
|
||||
export function isEmptyObject (obj: object): boolean {
|
||||
if (!obj) return true
|
||||
return Object.keys(obj).length === 0 && obj.constructor === Object
|
||||
}
|
||||
|
||||
export function pauseFor (ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
export type Valued<T> = { [s: string]: T }
|
||||
|
||||
export function toObject<T> (t: T[], map: (t0: T) => string): Valued<T> {
|
||||
return t.reduce( (acc, next) => {
|
||||
acc[map(next)] = next
|
||||
return acc
|
||||
}, { } as Valued<T>)
|
||||
}
|
||||
|
||||
export function toDedupObject<T> (t: T[], t2: T[], map: (t0: T) => string): Valued<T> {
|
||||
return toObject(t.concat(t2), map)
|
||||
}
|
||||
|
||||
export function update<T> (t: Valued<T>, u: Valued<T>): Valued<T> {
|
||||
return { ...t, ...u}
|
||||
}
|
||||
|
||||
export function fromObject<T> (o : Valued<T>): T[] {
|
||||
return Object.values(o)
|
||||
}
|
||||
|
||||
export function deepCloneUnknown<T> (value: T): T {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return value
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return deepCloneArray(value)
|
||||
}
|
||||
return deepCloneObject(value)
|
||||
}
|
||||
|
||||
export function deepCloneObject<T> (source: T) {
|
||||
const result = { }
|
||||
Object.keys(source).forEach(key => {
|
||||
const value = source[key]
|
||||
result[key] = deepCloneUnknown(value)
|
||||
}, { })
|
||||
return result as T
|
||||
}
|
||||
|
||||
export function deepCloneArray (collection: any) {
|
||||
return collection.map(value => {
|
||||
return deepCloneUnknown(value)
|
||||
})
|
||||
}
|
||||
|
||||
export function partitionArray<T> (ts: T[], condition: (t: T) => boolean): [T[], T[]] {
|
||||
const yes = [] as T[]
|
||||
const no = [] as T[]
|
||||
ts.forEach(t => {
|
||||
if (condition(t)) {
|
||||
yes.push(t)
|
||||
} else {
|
||||
no.push(t)
|
||||
}
|
||||
})
|
||||
return [yes, no]
|
||||
}
|
||||
|
||||
export const chill = () => { }
|
||||
export const chillAsync = async () => { }
|
||||
|
||||
export function uniqueBy<T> (ts: T[], uniqueBy: (t: T) => string, prioritize: (t1: T, t2: T) => T) {
|
||||
return Object.values(ts.reduce((acc, next) => {
|
||||
const previousValue = acc[uniqueBy(next)]
|
||||
if (previousValue) {
|
||||
acc[uniqueBy(next)] = prioritize(acc[uniqueBy(next)], previousValue)
|
||||
} else {
|
||||
acc[uniqueBy(next)] = previousValue
|
||||
}
|
||||
return acc
|
||||
}, { }))
|
||||
}
|
||||
|
||||
export function capitalizeFirstLetter (string: string): string {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||
}
|
||||
49
ui/src/app/util/property-subject.util.ts
Normal file
49
ui/src/app/util/property-subject.util.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { BehaviorSubject, Observable, combineLatest, of } from 'rxjs'
|
||||
import { map } from 'rxjs/operators'
|
||||
|
||||
export type PropertySubjectId<T> = {
|
||||
id: string
|
||||
subject: PropertySubject<T>
|
||||
}
|
||||
|
||||
export type PropertySubject<T> = {
|
||||
[k in keyof T]: BehaviorSubject<T[k]>
|
||||
}
|
||||
|
||||
// better type information than Object.entries without the return type cast
|
||||
export function asLabelledList<T> (p : PropertySubject<T>): [string, BehaviorSubject<any>][] {
|
||||
return Object.entries(p)
|
||||
}
|
||||
|
||||
export function peekProperties<T> (ps: PropertySubject<T>) : T {
|
||||
return asLabelledList(ps).reduce( (acc, [key, value]) => {
|
||||
acc[key] = value.getValue()
|
||||
return acc
|
||||
}, { } as T)
|
||||
}
|
||||
|
||||
export function initPropertySubject<T> (t: T): PropertySubject<T> {
|
||||
return Object.entries(t).reduce( (acc, [k, v]) => {
|
||||
acc[k] = new BehaviorSubject(v)
|
||||
return acc
|
||||
}, { } as PropertySubject<T> )
|
||||
}
|
||||
|
||||
export function withKey<V> (k: string, v: BehaviorSubject<V>): Observable<[string, V]> {
|
||||
return combineLatest([of(k), v])
|
||||
}
|
||||
|
||||
export function toObservable<T> (t: PropertySubject<T>): Observable<T> {
|
||||
return combineLatest(
|
||||
asLabelledList(t as any).map(([k, p]) => withKey(k, p)),
|
||||
).pipe(map( kvPairs => {
|
||||
return kvPairs.reduce( (acc, [k, v]) => {
|
||||
acc[k] = v
|
||||
return acc
|
||||
}, { }) as T
|
||||
}))
|
||||
}
|
||||
|
||||
export function complete<T> (t: PropertySubject<T>): void {
|
||||
asLabelledList(t as any).forEach(p => p[1].complete() )
|
||||
}
|
||||
53
ui/src/app/util/rxjs.util.ts
Normal file
53
ui/src/app/util/rxjs.util.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { Observable, from, interval, race, OperatorFunction, Observer, BehaviorSubject } from 'rxjs'
|
||||
import { take, map, switchMap, delay, tap } from 'rxjs/operators'
|
||||
|
||||
export function fromAsync$<S, T> (async: (s: S) => Promise<T>, s: S): Observable<T>
|
||||
export function fromAsync$<T> (async: () => Promise<T>): Observable<T>
|
||||
export function fromAsync$<S, T> (async: (s: S) => Promise<T>, s?: S): Observable<T> {
|
||||
return from(async(s as S))
|
||||
}
|
||||
|
||||
export function fromAsyncP<T> (async: () => Promise<T>): Promise<T>
|
||||
export function fromAsyncP<S, T> (async: (s: S) => Promise<T>, s?: S): Promise<T> {
|
||||
return async(s as S)
|
||||
}
|
||||
|
||||
// emits + completes after ms
|
||||
export function emitAfter$ (ms: number): Observable<number> {
|
||||
return interval(ms).pipe(take(1))
|
||||
}
|
||||
|
||||
export function throwIn<T> (timeout: number): OperatorFunction<T, T> {
|
||||
return o => race(
|
||||
o,
|
||||
emitAfter$(timeout).pipe(map(() => { throw new Error('timeout') } )))
|
||||
}
|
||||
|
||||
export const squash = map(() => { })
|
||||
|
||||
export function fromSync$<S, T> (sync: (s: S) => T, s: S): Observable<T>
|
||||
export function fromSync$<T> (sync: () => T): Observable<T>
|
||||
export function fromSync$<S, T> (sync: (s: S) => T, s?: S): Observable<T> {
|
||||
return new Observable( (subscriber: Observer<T>) => {
|
||||
try {
|
||||
subscriber.next(sync(s as S))
|
||||
subscriber.complete()
|
||||
} catch (e) {
|
||||
subscriber.error(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function onCooldown<T> (cooldown: number, o: () => Observable<T>): Observable<T> {
|
||||
|
||||
const $trigger$ = new BehaviorSubject(true)
|
||||
$trigger$.subscribe(t => console.log('triggering', t))
|
||||
return $trigger$.pipe(
|
||||
switchMap(_ =>
|
||||
o().pipe(
|
||||
delay(cooldown),
|
||||
tap(() => $trigger$.next(true)),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
31
ui/src/app/util/status-rendering.ts
Normal file
31
ui/src/app/util/status-rendering.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { AppStatus } from 'src/app/models/app-model'
|
||||
import { ServerStatus } from 'src/app/models/server-model'
|
||||
|
||||
export const ServerStatusRendering: {
|
||||
[k in ServerStatus]: { display: string; color: string; showDots: boolean; }
|
||||
} = {
|
||||
[ServerStatus.UNKNOWN]: { display: 'Connecting', color: 'dark', showDots: true },
|
||||
[ServerStatus.UNREACHABLE]: { display: 'Unreachable', color: 'danger', showDots: false },
|
||||
[ServerStatus.NEEDS_CONFIG]: { display: 'Needs Config', color: 'warning', showDots: false },
|
||||
[ServerStatus.RUNNING]: { display: 'Connected', color: 'success', showDots: false },
|
||||
[ServerStatus.UPDATING]: { display: 'Updating', color: 'primary', showDots: true },
|
||||
}
|
||||
|
||||
export const AppStatusRendering: {
|
||||
[k in AppStatus]: { display: string; color: string; showDots: boolean; style?: string; }
|
||||
} = {
|
||||
[AppStatus.UNKNOWN]: { display: 'Connecting', color: 'dark', showDots: true },
|
||||
[AppStatus.REMOVING]: { display: 'Removing', color: 'dark', showDots: true },
|
||||
[AppStatus.CRASHED]: { display: 'Crashing', color: 'danger', showDots: true },
|
||||
[AppStatus.NEEDS_CONFIG]: { display: 'Needs Config', color: 'warning', showDots: false },
|
||||
[AppStatus.RUNNING]: { display: 'Running', color: 'success', showDots: false },
|
||||
[AppStatus.UNREACHABLE]: { display: 'Unreachable', color: 'danger', showDots: false },
|
||||
[AppStatus.STOPPED]: { display: 'Not Running', color: 'medium', showDots: false },
|
||||
[AppStatus.CREATING_BACKUP]: { display: 'Backing Up', color: 'dark', showDots: true },
|
||||
[AppStatus.RESTORING_BACKUP]: { display: 'Restoring', color: 'dark', showDots: true },
|
||||
[AppStatus.INSTALLING]: { display: 'Installing', color: 'primary', showDots: true },
|
||||
[AppStatus.DEAD]: { display: 'Dead', color: 'danger', showDots: false },
|
||||
[AppStatus.BROKEN_DEPENDENCIES]: { display: 'Dependency Issue', color: 'warning', showDots: false },
|
||||
[AppStatus.STOPPING]: { display: 'Stopping', color: 'dark', showDots: true },
|
||||
[AppStatus.RESTARTING]: { display: 'Restarting', color: 'dark', showDots: true },
|
||||
}
|
||||
1
ui/src/app/util/types.util.ts
Normal file
1
ui/src/app/util/types.util.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Replace<T, key extends keyof T, withKey extends string> = Omit<T, key> & { [k in withKey]: T[key] }
|
||||
32
ui/src/app/util/web.util.ts
Normal file
32
ui/src/app/util/web.util.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { HttpErrorResponse } from '@angular/common/http'
|
||||
|
||||
export async function copyToClipboard (str: string): Promise<boolean> {
|
||||
if (window.isSecureContext) {
|
||||
return navigator.clipboard.writeText(str)
|
||||
.then(() => {
|
||||
return true
|
||||
})
|
||||
.catch(err => {
|
||||
return false
|
||||
})
|
||||
} else {
|
||||
const el = document.createElement('textarea')
|
||||
el.value = str
|
||||
el.setAttribute('readonly', '')
|
||||
el.style.position = 'absolute'
|
||||
el.style.left = '-9999px'
|
||||
document.body.appendChild(el)
|
||||
el.select()
|
||||
const copy = document.execCommand('copy')
|
||||
document.body.removeChild(el)
|
||||
return copy
|
||||
}
|
||||
}
|
||||
|
||||
export function isUnauthorized (e: HttpErrorResponse): boolean {
|
||||
return !!e.status && 401 === e.status
|
||||
}
|
||||
|
||||
export function isBadRequest (e: HttpErrorResponse): boolean {
|
||||
return !!e.status && 400 === e.status
|
||||
}
|
||||
8
ui/src/app/util/webview.context.ts
Normal file
8
ui/src/app/util/webview.context.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import WebviewContext from '@start9labs/ambassador-sdk/dist/webview-context'
|
||||
|
||||
export const webviewContext = new WebviewContext(async (method: string, data: any) => {
|
||||
throw new Error (`${method} UNIMPLEMENTED`)
|
||||
// switch(method){
|
||||
// case 'getConfigValue': throw new Error ('getConfigValue UNIMPLEMENTED')
|
||||
// }
|
||||
})
|
||||
Reference in New Issue
Block a user