ui: Add External Sources Filter to Node > Service Instances (#9368)

* Add service collections to get all ExternalServices

* Add a basic collection helper

* Use the collections to get all ExternalSources

* Remove old Controllers
This commit is contained in:
John Cowen 2020-12-14 14:25:33 +00:00 committed by GitHub
parent 83a8f266e5
commit 13fb1445c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 158 additions and 126 deletions

View File

@ -1,20 +0,0 @@
import { computed } from '@ember/object';
import Controller from '@ember/controller';
export default class IndexController extends Controller {
@computed('items.[]')
get services() {
return this.items.filter(function(item) {
return item.Kind !== 'connect-proxy';
});
}
@computed('services')
get externalSources() {
const sources = this.services.reduce(function(prev, item) {
return prev.concat(item.ExternalSources || []);
}, []);
// unique, non-empty values, alpha sort
return [...new Set(sources)].filter(Boolean).sort();
}
}

View File

@ -1,13 +0,0 @@
import { computed } from '@ember/object';
import Controller from '@ember/controller';
export default class InstancesController extends Controller {
@computed('items')
get externalSources() {
const sources = this.items.reduce(function(prev, item) {
return prev.concat(item.ExternalSources || []);
}, []);
// unique, non-empty values, alpha sort
return [...new Set(sources)].filter(Boolean).sort();
}
}

View File

@ -0,0 +1,23 @@
import Helper from '@ember/component/helper';
import { get } from '@ember/object';
import { Collection as Service } from 'consul-ui/models/service';
import { Collection as ServiceInstance } from 'consul-ui/models/service-instance';
const collections = {
service: Service,
'service-instance': ServiceInstance,
};
class EmptyCollection {}
export default class CollectionHelper extends Helper {
compute([collection, str], hash) {
if (collection.length > 0) {
// TODO: Looksee if theres ever going to be a public way to get this
const modelName = get(collection, 'firstObject')._internalModel.modelName;
const Collection = collections[modelName];
return new Collection(collection);
} else {
return new EmptyCollection();
}
}
}

View File

@ -2,10 +2,27 @@ import Model, { attr, belongsTo } from '@ember-data/model';
import { fragmentArray } from 'ember-data-model-fragments/attributes'; import { fragmentArray } from 'ember-data-model-fragments/attributes';
import { computed, get, set } from '@ember/object'; import { computed, get, set } from '@ember/object';
import { or, filter, alias } from '@ember/object/computed'; import { or, filter, alias } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
export const PRIMARY_KEY = 'uid'; export const PRIMARY_KEY = 'uid';
export const SLUG_KEY = 'Node.Node,Service.ID'; export const SLUG_KEY = 'Node.Node,Service.ID';
export const Collection = class Collection {
@tracked items;
constructor(items) {
this.items = items;
}
get ExternalSources() {
const sources = this.items.reduce(function(prev, item) {
return prev.concat(item.ExternalSources || []);
}, []);
// unique, non-empty values, alpha sort
return [...new Set(sources)].filter(Boolean).sort();
}
};
export default class ServiceInstance extends Model { export default class ServiceInstance extends Model {
@attr('string') uid; @attr('string') uid;

View File

@ -1,9 +1,26 @@
import Model, { attr } from '@ember-data/model'; import Model, { attr } from '@ember-data/model';
import { computed, get } from '@ember/object'; import { computed, get } from '@ember/object';
import { tracked } from '@glimmer/tracking';
export const PRIMARY_KEY = 'uid'; export const PRIMARY_KEY = 'uid';
export const SLUG_KEY = 'Name'; export const SLUG_KEY = 'Name';
export const Collection = class Collection {
@tracked items;
constructor(items) {
this.items = items;
}
get ExternalSources() {
const sources = this.items.reduce(function(prev, item) {
return prev.concat(item.ExternalSources || []);
}, []);
// unique, non-empty values, alpha sort
return [...new Set(sources)].filter(Boolean).sort();
}
};
export default class Service extends Model { export default class Service extends Model {
@attr('string') uid; @attr('string') uid;
@attr('string') Name; @attr('string') Name;

View File

@ -7,13 +7,13 @@
) )
) as |filters|}} ) as |filters|}}
{{#let (or sortBy "Status:asc") as |sort|}} {{#let (or sortBy "Status:asc") as |sort|}}
{{#let item.Services as |items|}} {{#let (reject-by 'Service.Kind' 'connect-proxy' item.Services) as |items|}}
<div class="tab-section"> <div class="tab-section">
<div role="tabpanel"> <div role="tabpanel">
{{#if (gt items.length 0) }} {{#if (gt items.length 0) }}
<input type="checkbox" id="toolbar-toggle" /> <input type="checkbox" id="toolbar-toggle" />
<Consul::ServiceInstance::SearchBar <Consul::ServiceInstance::SearchBar
@sources={{externalSources}} @sources={{get (collection items) 'ExternalSources'}}
@search={{search}} @search={{search}}
@onsearch={{action (mut search) value="target.value"}} @onsearch={{action (mut search) value="target.value"}}
@searchproperties={{searchProperties}} @searchproperties={{searchProperties}}

View File

@ -1,98 +1,106 @@
{{page-title 'Services'}} {{page-title 'Services'}}
<EventSource @src={{items}} /> <EventSource @src={{items}} />
{{#let (hash
statuses=(if status (split status ',') undefined) {{#let
kinds=(if kind (split kind ',') undefined)
sources=(if source (split source ',') undefined) (or sortBy "Status:asc")
searchproperties=(if (not-eq searchproperty undefined)
(split searchproperty ',') (hash
(array 'Name' 'Tags') statuses=(if status (split status ',') undefined)
kinds=(if kind (split kind ',') undefined)
sources=(if source (split source ',') undefined)
searchproperties=(if (not-eq searchproperty undefined)
(split searchproperty ',')
(array 'Name' 'Tags')
)
) )
) as |filters|}}
{{#let (or sortBy "Status:asc") as |sort|}}
<AppView>
<BlockSlot @name="notification" as |status type|>
<Consul::Service::Notifications
@type={{type}}
@status={{status}}
/>
</BlockSlot>
<BlockSlot @name="header">
<h1>
Services <em>{{format-number services.length}} total</em>
</h1>
<label for="toolbar-toggle"></label>
</BlockSlot>
<BlockSlot @name="toolbar">
{{#if (gt services.length 0) }}
<Consul::Service::SearchBar
@sources={{externalSources}}
@search={{search}} (reject-by 'Kind' 'connect-proxy' items)
@onsearch={{action (mut search) value="target.value"}}
@sort={{sort}} as |sort filters items|}}
@onsort={{action (mut sortBy) value="target.selected"}}
<AppView>
<BlockSlot @name="notification" as |status type|>
<Consul::Service::Notifications
@type={{type}}
@status={{status}}
/>
</BlockSlot>
<BlockSlot @name="header">
<h1>
Services <em>{{format-number items.length}} total</em>
</h1>
<label for="toolbar-toggle"></label>
</BlockSlot>
<BlockSlot @name="toolbar">
{{#if (gt items.length 0) }}
<Consul::Service::SearchBar
@sources={{get (collection items) 'ExternalSources'}}
@filter={{filters}}
@onfilter={{hash
searchproperty=(action (mut searchproperty) value="target.selectedItems")
status=(action (mut status) value="target.selectedItems")
kind=(action (mut kind) value="target.selectedItems")
source=(action (mut source) value="target.selectedItems")
}}
/>
{{/if}}
</BlockSlot>
<BlockSlot @name="content">
<DataCollection
@type="service"
@sort={{sort}}
@filters={{filters}}
@search={{search}} @search={{search}}
@items={{services}} @onsearch={{action (mut search) value="target.value"}}
as |collection|>
<collection.Collection> @sort={{sort}}
<Consul::Service::List @onsort={{action (mut sortBy) value="target.selected"}}
@sort={{sort}}
@filters={{filters}} @filter={{filters}}
@search={{search}} @onfilter={{hash
@items={{collection.items}} searchproperty=(action (mut searchproperty) value="target.selectedItems")
> status=(action (mut status) value="target.selectedItems")
</Consul::Service::List> kind=(action (mut kind) value="target.selectedItems")
</collection.Collection> source=(action (mut source) value="target.selectedItems")
<collection.Empty> }}
<EmptyState @allowLogin={{true}}> />
<BlockSlot @name="header"> {{/if}}
<h2> </BlockSlot>
{{#if (gt services.length 0)}} <BlockSlot @name="content">
No services found <DataCollection
{{else}} @type="service"
Welcome to Services @sort={{sort}}
{{/if}} @filters={{filters}}
</h2> @search={{search}}
</BlockSlot> @items={{items}}
<BlockSlot @name="body"> as |collection|>
<p> <collection.Collection>
{{#if (gt services.length 0)}} <Consul::Service::List
No services where found matching that search, or you may not have access to view the services you are searching for. @items={{collection.items}}
{{else}} >
There don't seem to be any registered services, or you may not have access to view services yet. </Consul::Service::List>
{{/if}} </collection.Collection>
</p> <collection.Empty>
</BlockSlot> <EmptyState @allowLogin={{true}}>
<BlockSlot @name="actions"> <BlockSlot @name="header">
<li class="docs-link"> <h2>
<a href="{{env 'CONSUL_DOCS_URL'}}/commands/services" rel="noopener noreferrer" target="_blank">Documentation on services</a> {{#if (gt services.length 0)}}
</li> No services found
<li class="learn-link"> {{else}}
<a href="{{env 'CONSUL_DOCS_LEARN_URL'}}/consul/getting-started/services" rel="noopener noreferrer" target="_blank">Read the guide</a> Welcome to Services
</li> {{/if}}
</BlockSlot> </h2>
</EmptyState> </BlockSlot>
</collection.Empty> <BlockSlot @name="body">
</DataCollection> <p>
</BlockSlot> {{#if (gt services.length 0)}}
</AppView> No services where found matching that search, or you may not have access to view the services you are searching for.
{{/let}} {{else}}
There don't seem to be any registered services, or you may not have access to view services yet.
{{/if}}
</p>
</BlockSlot>
<BlockSlot @name="actions">
<li class="docs-link">
<a href="{{env 'CONSUL_DOCS_URL'}}/commands/services" rel="noopener noreferrer" target="_blank">Documentation on services</a>
</li>
<li class="learn-link">
<a href="{{env 'CONSUL_DOCS_LEARN_URL'}}/consul/getting-started/services" rel="noopener noreferrer" target="_blank">Read the guide</a>
</li>
</BlockSlot>
</EmptyState>
</collection.Empty>
</DataCollection>
</BlockSlot>
</AppView>
{{/let}} {{/let}}

View File

@ -12,7 +12,7 @@
{{#if (gt items.length 0) }} {{#if (gt items.length 0) }}
<input type="checkbox" id="toolbar-toggle" /> <input type="checkbox" id="toolbar-toggle" />
<Consul::ServiceInstance::SearchBar <Consul::ServiceInstance::SearchBar
@sources={{externalSources}} @sources={{get (collection items) 'ExternalSources'}}
@search={{search}} @search={{search}}
@onsearch={{action (mut search) value="target.value"}} @onsearch={{action (mut search) value="target.value"}}
@searchproperties={{searchProperties}} @searchproperties={{searchProperties}}