2019-12-17 19:27:28 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { set, get, computed } from '@ember/object';
|
|
|
|
|
2020-05-19 16:18:04 +00:00
|
|
|
import { createRoute, getSplitters, getRoutes, getResolvers } from './utils';
|
2019-12-17 19:27:28 +00:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
dom: service('dom'),
|
|
|
|
ticker: service('ticker'),
|
|
|
|
dataStructs: service('data-structs'),
|
|
|
|
classNames: ['discovery-chain'],
|
|
|
|
classNameBindings: ['active'],
|
|
|
|
selectedId: '',
|
|
|
|
init: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners = this.dom.listeners();
|
|
|
|
},
|
2020-11-30 16:52:13 +00:00
|
|
|
didInsertElement: function() {
|
|
|
|
this._listeners.add(this.dom.document(), {
|
|
|
|
click: e => {
|
|
|
|
// all route/splitter/resolver components currently
|
|
|
|
// have classes that end in '-card'
|
|
|
|
if (!this.dom.closest('[class$="-card"]', e.target)) {
|
|
|
|
set(this, 'active', false);
|
|
|
|
set(this, 'selectedId', '');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2019-12-17 19:27:28 +00:00
|
|
|
},
|
|
|
|
willDestroyElement: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners.remove();
|
|
|
|
this.ticker.destroy(this);
|
|
|
|
},
|
|
|
|
splitters: computed('chain.Nodes', function() {
|
2020-01-16 16:31:09 +00:00
|
|
|
return getSplitters(get(this, 'chain.Nodes'));
|
2019-12-17 19:27:28 +00:00
|
|
|
}),
|
2020-01-16 16:31:09 +00:00
|
|
|
routes: computed('chain.Nodes', function() {
|
2020-01-30 16:09:05 +00:00
|
|
|
const routes = getRoutes(get(this, 'chain.Nodes'), this.dom.guid);
|
|
|
|
// if we have no routes with a PathPrefix of '/' or one with no definition at all
|
|
|
|
// then add our own 'default catch all'
|
|
|
|
if (
|
|
|
|
!routes.find(item => get(item, 'Definition.Match.HTTP.PathPrefix') === '/') &&
|
|
|
|
!routes.find(item => typeof item.Definition === 'undefined')
|
|
|
|
) {
|
2020-02-06 12:06:48 +00:00
|
|
|
let nextNode;
|
|
|
|
const resolverID = `resolver:${this.chain.ServiceName}.${this.chain.Namespace}.${this.chain.Datacenter}`;
|
|
|
|
const splitterID = `splitter:${this.chain.ServiceName}.${this.chain.Namespace}`;
|
2020-05-19 16:18:04 +00:00
|
|
|
// The default router should look for a splitter first,
|
|
|
|
// if there isn't one try the default resolver
|
|
|
|
if (typeof this.chain.Nodes[splitterID] !== 'undefined') {
|
2020-01-30 16:09:05 +00:00
|
|
|
nextNode = splitterID;
|
2020-05-19 16:18:04 +00:00
|
|
|
} else if (typeof this.chain.Nodes[resolverID] !== 'undefined') {
|
|
|
|
nextNode = resolverID;
|
2020-01-30 16:09:05 +00:00
|
|
|
}
|
2020-02-06 12:06:48 +00:00
|
|
|
if (typeof nextNode !== 'undefined') {
|
2020-05-19 16:18:04 +00:00
|
|
|
const route = {
|
2020-02-06 12:06:48 +00:00
|
|
|
Default: true,
|
|
|
|
ID: `route:${this.chain.ServiceName}`,
|
|
|
|
Name: this.chain.ServiceName,
|
|
|
|
Definition: {
|
|
|
|
Match: {
|
|
|
|
HTTP: {
|
|
|
|
PathPrefix: '/',
|
|
|
|
},
|
2020-01-30 16:09:05 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-06 12:06:48 +00:00
|
|
|
NextNode: nextNode,
|
2020-05-19 16:18:04 +00:00
|
|
|
};
|
|
|
|
routes.push(createRoute(route, this.chain.ServiceName, this.dom.guid));
|
2020-02-06 12:06:48 +00:00
|
|
|
}
|
2020-01-30 16:09:05 +00:00
|
|
|
}
|
|
|
|
return routes;
|
2019-12-17 19:27:28 +00:00
|
|
|
}),
|
2020-12-11 09:38:33 +00:00
|
|
|
nodes: computed('routes', 'splitters', 'resolvers', function() {
|
|
|
|
let nodes = this.resolvers.reduce((prev, item) => {
|
|
|
|
prev[`resolver:${item.ID}`] = item;
|
|
|
|
item.Children.reduce((prev, item) => {
|
|
|
|
prev[`resolver:${item.ID}`] = item;
|
|
|
|
return prev;
|
|
|
|
}, prev);
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
nodes = this.splitters.reduce((prev, item) => {
|
|
|
|
prev[item.ID] = item;
|
|
|
|
return prev;
|
|
|
|
}, nodes);
|
|
|
|
nodes = this.routes.reduce((prev, item) => {
|
|
|
|
prev[item.ID] = item;
|
|
|
|
return prev;
|
|
|
|
}, nodes);
|
|
|
|
Object.entries(nodes).forEach(([key, value]) => {
|
|
|
|
if (typeof value.NextNode !== 'undefined') {
|
|
|
|
value.NextItem = nodes[value.NextNode];
|
|
|
|
}
|
|
|
|
if (typeof value.Splits !== 'undefined') {
|
|
|
|
value.Splits.forEach(item => {
|
|
|
|
if (typeof item.NextNode !== 'undefined') {
|
|
|
|
item.NextItem = nodes[item.NextNode];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return '';
|
|
|
|
}),
|
2020-01-16 16:31:09 +00:00
|
|
|
resolvers: computed('chain.{Nodes,Targets}', function() {
|
|
|
|
return getResolvers(
|
2019-12-17 19:27:28 +00:00
|
|
|
this.chain.Datacenter,
|
|
|
|
this.chain.Namespace,
|
|
|
|
get(this, 'chain.Targets'),
|
2020-01-16 16:31:09 +00:00
|
|
|
get(this, 'chain.Nodes')
|
2019-12-17 19:27:28 +00:00
|
|
|
);
|
|
|
|
}),
|
2020-05-19 16:18:04 +00:00
|
|
|
graph: computed('splitters', 'routes.[]', function() {
|
2019-12-17 19:27:28 +00:00
|
|
|
const graph = this.dataStructs.graph();
|
2020-02-06 12:06:48 +00:00
|
|
|
this.splitters.forEach(item => {
|
|
|
|
item.Splits.forEach(splitter => {
|
|
|
|
graph.addLink(item.ID, splitter.NextNode);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.routes.forEach((route, i) => {
|
|
|
|
graph.addLink(route.ID, route.NextNode);
|
2019-12-17 19:27:28 +00:00
|
|
|
});
|
|
|
|
return graph;
|
|
|
|
}),
|
|
|
|
selected: computed('selectedId', 'graph', function() {
|
|
|
|
if (this.selectedId === '' || !this.dom.element(`#${this.selectedId}`)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const id = this.selectedId;
|
2020-01-16 16:31:09 +00:00
|
|
|
const type = id.split(':').shift();
|
2019-12-17 19:27:28 +00:00
|
|
|
const nodes = [id];
|
|
|
|
const edges = [];
|
|
|
|
this.graph.forEachLinkedNode(id, (linkedNode, link) => {
|
|
|
|
nodes.push(linkedNode.id);
|
|
|
|
edges.push(`${link.fromId}>${link.toId}`);
|
|
|
|
this.graph.forEachLinkedNode(linkedNode.id, (linkedNode, link) => {
|
2020-01-16 16:31:09 +00:00
|
|
|
const nodeType = linkedNode.id.split(':').shift();
|
2019-12-17 19:27:28 +00:00
|
|
|
if (type !== nodeType && type !== 'splitter' && nodeType !== 'splitter') {
|
|
|
|
nodes.push(linkedNode.id);
|
|
|
|
edges.push(`${link.fromId}>${link.toId}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
nodes: nodes.map(item => `#${CSS.escape(item)}`),
|
|
|
|
edges: edges.map(item => `#${CSS.escape(item)}`),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
actions: {
|
|
|
|
click: function(e) {
|
|
|
|
const id = e.currentTarget.getAttribute('id');
|
|
|
|
if (id === this.selectedId) {
|
|
|
|
set(this, 'active', false);
|
|
|
|
set(this, 'selectedId', '');
|
|
|
|
} else {
|
|
|
|
set(this, 'active', true);
|
|
|
|
set(this, 'selectedId', id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|