github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/services/system.js (about) 1 import Service, { inject as service } from '@ember/service'; 2 import { computed } from '@ember/object'; 3 import { alias } from '@ember/object/computed'; 4 import PromiseObject from '../utils/classes/promise-object'; 5 import PromiseArray from '../utils/classes/promise-array'; 6 import { namespace } from '../adapters/application'; 7 import jsonWithDefault from '../utils/json-with-default'; 8 import classic from 'ember-classic-decorator'; 9 import { task } from 'ember-concurrency'; 10 11 @classic 12 export default class SystemService extends Service { 13 @service token; 14 @service store; 15 16 @computed('activeRegion') 17 get leader() { 18 const token = this.token; 19 20 return PromiseObject.create({ 21 promise: token 22 .authorizedRequest(`/${namespace}/status/leader`) 23 .then((res) => res.json()) 24 .then((rpcAddr) => ({ rpcAddr })) 25 .then((leader) => { 26 // Dirty self so leader can be used as a dependent key 27 this.notifyPropertyChange('leader.rpcAddr'); 28 return leader; 29 }), 30 }); 31 } 32 33 @computed 34 get agent() { 35 const token = this.token; 36 return PromiseObject.create({ 37 promise: token 38 .authorizedRawRequest(`/${namespace}/agent/self`) 39 .then(jsonWithDefault({})) 40 .then((agent) => { 41 if (agent?.config?.Version) { 42 const { Version, VersionPrerelease, VersionMetadata } = 43 agent.config.Version; 44 agent.version = Version; 45 if (VersionPrerelease) 46 agent.version = `${agent.version}-${VersionPrerelease}`; 47 if (VersionMetadata) 48 agent.version = `${agent.version}+${VersionMetadata}`; 49 } 50 return agent; 51 }), 52 }); 53 } 54 55 @computed 56 get defaultRegion() { 57 const token = this.token; 58 return PromiseObject.create({ 59 promise: token 60 .authorizedRawRequest(`/${namespace}/agent/members`) 61 .then(jsonWithDefault({})) 62 .then((json) => { 63 return { region: json.ServerRegion }; 64 }), 65 }); 66 } 67 68 @computed 69 get regions() { 70 const token = this.token; 71 72 return PromiseArray.create({ 73 promise: token 74 .authorizedRawRequest(`/${namespace}/regions`) 75 .then(jsonWithDefault([])), 76 }); 77 } 78 79 @computed('regions.[]') 80 get activeRegion() { 81 const regions = this.regions; 82 const region = window.localStorage.nomadActiveRegion; 83 84 if (regions.includes(region)) { 85 return region; 86 } 87 88 return null; 89 } 90 91 set activeRegion(value) { 92 if (value == null) { 93 window.localStorage.removeItem('nomadActiveRegion'); 94 return; 95 } else { 96 // All localStorage values are strings. Stringify first so 97 // the return value is consistent with what is persisted. 98 const strValue = value + ''; 99 window.localStorage.nomadActiveRegion = strValue; 100 } 101 } 102 103 @computed('regions.[]') 104 get shouldShowRegions() { 105 return this.get('regions.length') > 1; 106 } 107 108 @computed('activeRegion', 'defaultRegion.region', 'shouldShowRegions') 109 get shouldIncludeRegion() { 110 return ( 111 this.shouldShowRegions && 112 this.activeRegion !== this.get('defaultRegion.region') 113 ); 114 } 115 116 @computed('activeRegion') 117 get namespaces() { 118 return PromiseArray.create({ 119 promise: this.store 120 .findAll('namespace') 121 .then((namespaces) => namespaces.compact()), 122 }); 123 } 124 125 @computed('namespaces.[]') 126 get shouldShowNamespaces() { 127 const namespaces = this.namespaces.toArray(); 128 return ( 129 namespaces.length && 130 namespaces.some((namespace) => namespace.get('id') !== 'default') 131 ); 132 } 133 134 @task(function* () { 135 const emptyLicense = { License: { Features: [] } }; 136 137 try { 138 return yield this.token 139 .authorizedRawRequest(`/${namespace}/operator/license`) 140 .then(jsonWithDefault(emptyLicense)); 141 } catch (e) { 142 return emptyLicense; 143 } 144 }) 145 fetchLicense; 146 147 @task(function* () { 148 try { 149 const request = yield this.token.authorizedRequest('/v1/search/fuzzy', { 150 method: 'POST', 151 body: JSON.stringify({ 152 Text: 'feature-detection-query', 153 Context: 'namespaces', 154 }), 155 }); 156 157 return request.ok; 158 } catch (e) { 159 return false; 160 } 161 }) 162 checkFuzzySearchPresence; 163 164 @alias('fetchLicense.lastSuccessful.value') license; 165 @alias('checkFuzzySearchPresence.last.value') fuzzySearchEnabled; 166 167 @computed('license.License.Features.[]') 168 get features() { 169 return this.get('license.License.Features') || []; 170 } 171 }