github.com/manicqin/nomad@v0.9.5/ui/app/services/system.js (about)

     1  import Service, { inject as service } from '@ember/service';
     2  import { computed } from '@ember/object';
     3  import PromiseObject from '../utils/classes/promise-object';
     4  import PromiseArray from '../utils/classes/promise-array';
     5  import { namespace } from '../adapters/application';
     6  import jsonWithDefault from '../utils/json-with-default';
     7  
     8  export default Service.extend({
     9    token: service(),
    10    store: service(),
    11  
    12    leader: computed('activeRegion', function() {
    13      const token = this.token;
    14  
    15      return PromiseObject.create({
    16        promise: token
    17          .authorizedRequest(`/${namespace}/status/leader`)
    18          .then(res => res.json())
    19          .then(rpcAddr => ({ rpcAddr }))
    20          .then(leader => {
    21            // Dirty self so leader can be used as a dependent key
    22            this.notifyPropertyChange('leader.rpcAddr');
    23            return leader;
    24          }),
    25      });
    26    }),
    27  
    28    defaultRegion: computed(function() {
    29      const token = this.token;
    30      return PromiseObject.create({
    31        promise: token
    32          .authorizedRawRequest(`/${namespace}/agent/members`)
    33          .then(jsonWithDefault({}))
    34          .then(json => {
    35            return { region: json.ServerRegion };
    36          }),
    37      });
    38    }),
    39  
    40    regions: computed(function() {
    41      const token = this.token;
    42  
    43      return PromiseArray.create({
    44        promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
    45      });
    46    }),
    47  
    48    activeRegion: computed('regions.[]', {
    49      get() {
    50        const regions = this.regions;
    51        const region = window.localStorage.nomadActiveRegion;
    52  
    53        if (regions.includes(region)) {
    54          return region;
    55        }
    56  
    57        return null;
    58      },
    59      set(key, value) {
    60        if (value == null) {
    61          window.localStorage.removeItem('nomadActiveRegion');
    62        } else {
    63          // All localStorage values are strings. Stringify first so
    64          // the return value is consistent with what is persisted.
    65          const strValue = value + '';
    66          window.localStorage.nomadActiveRegion = strValue;
    67          return strValue;
    68        }
    69      },
    70    }),
    71  
    72    shouldShowRegions: computed('regions.[]', function() {
    73      return this.get('regions.length') > 1;
    74    }),
    75  
    76    shouldIncludeRegion: computed(
    77      'activeRegion',
    78      'defaultRegion.region',
    79      'shouldShowRegions',
    80      function() {
    81        return this.shouldShowRegions &&
    82        this.activeRegion !== this.get('defaultRegion.region');
    83      }
    84    ),
    85  
    86    namespaces: computed('activeRegion', function() {
    87      return PromiseArray.create({
    88        promise: this.store
    89          .findAll('namespace')
    90          .then(namespaces => namespaces.compact()),
    91      });
    92    }),
    93  
    94    shouldShowNamespaces: computed('namespaces.[]', function() {
    95      const namespaces = this.namespaces.toArray();
    96      return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
    97    }),
    98  
    99    activeNamespace: computed('namespaces.[]', {
   100      get() {
   101        const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
   102        const namespace = this.namespaces.findBy('id', namespaceId);
   103  
   104        if (namespace) {
   105          return namespace;
   106        }
   107  
   108        // If the namespace in localStorage is no longer in the cluster, it needs to
   109        // be cleared from localStorage
   110        window.localStorage.removeItem('nomadActiveNamespace');
   111        return this.namespaces.findBy('id', 'default');
   112      },
   113      set(key, value) {
   114        if (value == null) {
   115          window.localStorage.removeItem('nomadActiveNamespace');
   116        } else if (typeof value === 'string') {
   117          window.localStorage.nomadActiveNamespace = value;
   118          return this.namespaces.findBy('id', value);
   119        } else {
   120          window.localStorage.nomadActiveNamespace = value.get('name');
   121          return value;
   122        }
   123      },
   124    }),
   125  
   126    reset() {
   127      this.set('activeNamespace', null);
   128    },
   129  });