2017-12-15 21:39:18 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
2021-04-29 20:00:59 +00:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
2020-11-06 14:21:38 +00:00
|
|
|
import { alias } from '@ember/object/computed';
|
2017-09-19 14:47:10 +00:00
|
|
|
import PromiseObject from '../utils/classes/promise-object';
|
2018-08-02 22:56:11 +00:00
|
|
|
import PromiseArray from '../utils/classes/promise-array';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { namespace } from '../adapters/application';
|
2018-11-02 05:07:58 +00:00
|
|
|
import jsonWithDefault from '../utils/json-with-default';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2020-11-06 14:21:38 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
2018-08-09 18:03:37 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class SystemService extends Service {
|
|
|
|
@service token;
|
|
|
|
@service store;
|
2017-10-07 00:14:08 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('activeRegion')
|
|
|
|
get leader() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const token = this.token;
|
2017-10-07 00:14:08 +00:00
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
return PromiseObject.create({
|
2017-10-07 00:14:08 +00:00
|
|
|
promise: token
|
|
|
|
.authorizedRequest(`/${namespace}/status/leader`)
|
2017-09-19 14:47:10 +00:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(rpcAddr => ({ rpcAddr }))
|
|
|
|
.then(leader => {
|
|
|
|
// Dirty self so leader can be used as a dependent key
|
|
|
|
this.notifyPropertyChange('leader.rpcAddr');
|
|
|
|
return leader;
|
|
|
|
}),
|
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-10-10 01:50:49 +00:00
|
|
|
|
2020-10-26 06:25:28 +00:00
|
|
|
@computed
|
|
|
|
get agent() {
|
|
|
|
const token = this.token;
|
|
|
|
return PromiseObject.create({
|
|
|
|
promise: token
|
|
|
|
.authorizedRawRequest(`/${namespace}/agent/self`)
|
|
|
|
.then(jsonWithDefault({}))
|
|
|
|
.then(agent => {
|
2021-07-08 12:01:15 +00:00
|
|
|
if (agent?.config?.Version) {
|
2021-06-16 15:40:24 +00:00
|
|
|
const { Version, VersionPrerelease, VersionMetadata } = agent.config.Version;
|
|
|
|
agent.version = Version;
|
|
|
|
if (VersionPrerelease) agent.version = `${agent.version}-${VersionPrerelease}`;
|
|
|
|
if (VersionMetadata) agent.version = `${agent.version}+${VersionMetadata}`;
|
|
|
|
}
|
2020-10-26 06:25:28 +00:00
|
|
|
return agent;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed
|
|
|
|
get defaultRegion() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const token = this.token;
|
2018-08-09 02:34:56 +00:00
|
|
|
return PromiseObject.create({
|
|
|
|
promise: token
|
|
|
|
.authorizedRawRequest(`/${namespace}/agent/members`)
|
2018-08-09 18:03:37 +00:00
|
|
|
.then(jsonWithDefault({}))
|
2018-08-09 02:34:56 +00:00
|
|
|
.then(json => {
|
|
|
|
return { region: json.ServerRegion };
|
|
|
|
}),
|
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-09 02:34:56 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed
|
|
|
|
get regions() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const token = this.token;
|
2018-08-02 22:56:11 +00:00
|
|
|
|
|
|
|
return PromiseArray.create({
|
2018-08-09 18:03:37 +00:00
|
|
|
promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
|
2018-08-02 22:56:11 +00:00
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('regions.[]')
|
|
|
|
get activeRegion() {
|
|
|
|
const regions = this.regions;
|
|
|
|
const region = window.localStorage.nomadActiveRegion;
|
|
|
|
|
|
|
|
if (regions.includes(region)) {
|
|
|
|
return region;
|
2018-08-09 02:34:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
set activeRegion(value) {
|
|
|
|
if (value == null) {
|
|
|
|
window.localStorage.removeItem('nomadActiveRegion');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// All localStorage values are strings. Stringify first so
|
|
|
|
// the return value is consistent with what is persisted.
|
|
|
|
const strValue = value + '';
|
|
|
|
window.localStorage.nomadActiveRegion = strValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed('regions.[]')
|
|
|
|
get shouldShowRegions() {
|
|
|
|
return this.get('regions.length') > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed('activeRegion', 'defaultRegion.region', 'shouldShowRegions')
|
|
|
|
get shouldIncludeRegion() {
|
|
|
|
return this.shouldShowRegions && this.activeRegion !== this.get('defaultRegion.region');
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed('activeRegion')
|
|
|
|
get namespaces() {
|
2018-08-04 01:17:12 +00:00
|
|
|
return PromiseArray.create({
|
2020-01-31 00:06:35 +00:00
|
|
|
promise: this.store.findAll('namespace').then(namespaces => namespaces.compact()),
|
2018-08-04 01:17:12 +00:00
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-10-10 01:50:49 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('namespaces.[]')
|
|
|
|
get shouldShowNamespaces() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const namespaces = this.namespaces.toArray();
|
2017-10-23 17:26:48 +00:00
|
|
|
return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-10-23 17:26:48 +00:00
|
|
|
|
2021-04-29 20:00:59 +00:00
|
|
|
// The cachedNamespace is set on pages that have a namespaces filter.
|
|
|
|
// It is set so other pages that have a namespaces filter can default to
|
|
|
|
// what the previous namespaces filter page used rather than defaulting
|
|
|
|
// to 'default' or '*'.
|
|
|
|
@tracked cachedNamespace = null;
|
2020-11-06 14:21:38 +00:00
|
|
|
|
|
|
|
@task(function*() {
|
|
|
|
const emptyLicense = { License: { Features: [] } };
|
|
|
|
|
|
|
|
try {
|
|
|
|
return yield this.token
|
|
|
|
.authorizedRawRequest(`/${namespace}/operator/license`)
|
|
|
|
.then(jsonWithDefault(emptyLicense));
|
|
|
|
} catch (e) {
|
|
|
|
return emptyLicense;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
fetchLicense;
|
|
|
|
|
ui: Change global search to use fuzzy search API (#10412)
This updates the UI to use the new fuzzy search API. It’s a drop-in
replacement so the / shortcut to jump to search is preserved, and
results can be cycled through and chosen via arrow keys and the
enter key.
It doesn’t use everything returned by the API:
* deployments and evaluations: these match by id, doesn’t seem like
people would know those or benefit from quick navigation to them
* namespaces: doesn’t seem useful as they currently function
* scaling policies
* tasks: the response doesn’t include an allocation id, which means they
can’t be navigated to in the UI without an additional query
* CSI volumes: aren’t actually returned by the API
Since there’s no API to check the server configuration and know whether
the feature has been disabled, this adds another query in
route:application#beforeModel that acts as feature detection: if the
attempt to query fails (500), the global search field is hidden.
Upon having added another query on load, I realised that beforeModel was
being triggered any time service:router#transitionTo was being called,
which happens upon navigating to a search result, for instance, because
of refreshModel being present on the region query parameter. This PR
adds a check for transition.queryParamsOnly and skips rerunning the
onload queries (token permissions check, license check, fuzzy search
feature detection).
Implementation notes:
* there are changes to unrelated tests to ignore the on-load feature
detection query
* some lifecycle-related guards against undefined were required to
address failures when navigating to an allocation
* the minimum search length of 2 characters is hard-coded as there’s
currently no way to determine min_term_length in the UI
2021-04-28 18:31:05 +00:00
|
|
|
@task(function*() {
|
|
|
|
try {
|
|
|
|
const request = yield this.token.authorizedRequest('/v1/search/fuzzy', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
Text: 'feature-detection-query',
|
|
|
|
Context: 'namespaces',
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
return request.ok;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
checkFuzzySearchPresence;
|
|
|
|
|
2020-11-06 14:21:38 +00:00
|
|
|
@alias('fetchLicense.lastSuccessful.value') license;
|
ui: Change global search to use fuzzy search API (#10412)
This updates the UI to use the new fuzzy search API. It’s a drop-in
replacement so the / shortcut to jump to search is preserved, and
results can be cycled through and chosen via arrow keys and the
enter key.
It doesn’t use everything returned by the API:
* deployments and evaluations: these match by id, doesn’t seem like
people would know those or benefit from quick navigation to them
* namespaces: doesn’t seem useful as they currently function
* scaling policies
* tasks: the response doesn’t include an allocation id, which means they
can’t be navigated to in the UI without an additional query
* CSI volumes: aren’t actually returned by the API
Since there’s no API to check the server configuration and know whether
the feature has been disabled, this adds another query in
route:application#beforeModel that acts as feature detection: if the
attempt to query fails (500), the global search field is hidden.
Upon having added another query on load, I realised that beforeModel was
being triggered any time service:router#transitionTo was being called,
which happens upon navigating to a search result, for instance, because
of refreshModel being present on the region query parameter. This PR
adds a check for transition.queryParamsOnly and skips rerunning the
onload queries (token permissions check, license check, fuzzy search
feature detection).
Implementation notes:
* there are changes to unrelated tests to ignore the on-load feature
detection query
* some lifecycle-related guards against undefined were required to
address failures when navigating to an allocation
* the minimum search length of 2 characters is hard-coded as there’s
currently no way to determine min_term_length in the UI
2021-04-28 18:31:05 +00:00
|
|
|
@alias('checkFuzzySearchPresence.last.value') fuzzySearchEnabled;
|
2020-11-06 14:21:38 +00:00
|
|
|
|
|
|
|
@computed('license.License.Features.[]')
|
|
|
|
get features() {
|
|
|
|
return this.get('license.License.Features') || [];
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|