mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-05 03:01:11 +00:00
257 lines
6.1 KiB
JavaScript
257 lines
6.1 KiB
JavaScript
const EventEmitter = require('events').EventEmitter;
|
|
const _ = require('lodash');
|
|
const assign = require('object-assign');
|
|
const debug = require('debug')('app-store');
|
|
|
|
const AppDispatcher = require('../dispatcher/app-dispatcher');
|
|
const ActionTypes = require('../constants/action-types');
|
|
const Naming = require('../constants/naming');
|
|
|
|
// Helpers
|
|
|
|
function findCurrentTopology(subTree, topologyId) {
|
|
let foundTopology;
|
|
|
|
_.each(subTree, function(topology) {
|
|
if (_.endsWith(topology.url, topologyId)) {
|
|
foundTopology = topology;
|
|
}
|
|
if (!foundTopology) {
|
|
foundTopology = findCurrentTopology(topology.sub_topologies, topologyId);
|
|
}
|
|
if (foundTopology) {
|
|
return false;
|
|
}
|
|
});
|
|
|
|
return foundTopology;
|
|
}
|
|
|
|
// Initial values
|
|
|
|
let connectionState = 'disconnected';
|
|
let currentGrouping = 'none';
|
|
let currentTopologyId = 'containers';
|
|
let version = '';
|
|
let mouseOverEdgeId = null;
|
|
let mouseOverNodeId = null;
|
|
let nodes = {};
|
|
let nodeDetails = null;
|
|
let selectedNodeId = null;
|
|
let topologies = [];
|
|
|
|
// Store API
|
|
|
|
const AppStore = assign({}, EventEmitter.prototype, {
|
|
|
|
CHANGE_EVENT: 'change',
|
|
|
|
getAppState: function() {
|
|
return {
|
|
topologyId: currentTopologyId,
|
|
grouping: this.getCurrentGrouping(),
|
|
selectedNodeId: this.getSelectedNodeId()
|
|
};
|
|
},
|
|
|
|
getConnectionState: function() {
|
|
return connectionState;
|
|
},
|
|
|
|
getCurrentTopology: function() {
|
|
return findCurrentTopology(topologies, currentTopologyId);
|
|
},
|
|
|
|
getCurrentTopologyUrl: function() {
|
|
const topology = this.getCurrentTopology();
|
|
|
|
if (topology) {
|
|
return topology.url;
|
|
}
|
|
},
|
|
|
|
getCurrentGrouping: function() {
|
|
return currentGrouping;
|
|
},
|
|
|
|
getHighlightedEdgeIds: function() {
|
|
if (mouseOverNodeId) {
|
|
// all neighbour combinations because we dont know which direction exists
|
|
const node = nodes[mouseOverNodeId];
|
|
return _.flatten(
|
|
_.map(node.adjacency, function(nodeId) {
|
|
return [
|
|
[nodeId, mouseOverNodeId].join(Naming.EDGE_ID_SEPARATOR),
|
|
[mouseOverNodeId, nodeId].join(Naming.EDGE_ID_SEPARATOR)
|
|
];
|
|
})
|
|
);
|
|
}
|
|
if (mouseOverEdgeId) {
|
|
return mouseOverEdgeId;
|
|
}
|
|
return null;
|
|
},
|
|
|
|
getHighlightedNodeIds: function() {
|
|
if (mouseOverNodeId) {
|
|
const node = nodes[mouseOverNodeId];
|
|
return _.union(node.adjacency, [mouseOverNodeId]);
|
|
}
|
|
if (mouseOverEdgeId) {
|
|
return mouseOverEdgeId.split(Naming.EDGE_ID_SEPARATOR);
|
|
}
|
|
return null;
|
|
},
|
|
|
|
getNodeDetails: function() {
|
|
return nodeDetails;
|
|
},
|
|
|
|
getNodes: function() {
|
|
return nodes;
|
|
},
|
|
|
|
getSelectedNodeId: function() {
|
|
return selectedNodeId;
|
|
},
|
|
|
|
getTopologies: function() {
|
|
return topologies;
|
|
},
|
|
|
|
getTopologyIdForUrl: function(url) {
|
|
return url.split('/').pop();
|
|
},
|
|
|
|
getVersion: function() {
|
|
return version;
|
|
}
|
|
});
|
|
|
|
// Store Dispatch Hooks
|
|
|
|
AppStore.registeredCallback = function(payload) {
|
|
switch (payload.type) {
|
|
|
|
case ActionTypes.CLICK_CLOSE_DETAILS:
|
|
selectedNodeId = null;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.CLICK_GROUPING:
|
|
if (payload.grouping !== currentGrouping) {
|
|
currentGrouping = payload.grouping;
|
|
nodes = {};
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
}
|
|
break;
|
|
|
|
case ActionTypes.CLICK_NODE:
|
|
selectedNodeId = payload.nodeId;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.CLICK_TOPOLOGY:
|
|
if (payload.topologyId !== currentTopologyId) {
|
|
currentTopologyId = payload.topologyId;
|
|
nodes = {};
|
|
}
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.ENTER_EDGE:
|
|
mouseOverEdgeId = payload.edgeId;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.ENTER_NODE:
|
|
mouseOverNodeId = payload.nodeId;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.HIT_ESC_KEY:
|
|
nodeDetails = null;
|
|
selectedNodeId = null;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.LEAVE_EDGE:
|
|
mouseOverEdgeId = null;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.LEAVE_NODE:
|
|
mouseOverNodeId = null;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.RECEIVE_NODE_DETAILS:
|
|
nodeDetails = payload.details;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.RECEIVE_NODES_DELTA:
|
|
debug('RECEIVE_NODES_DELTA',
|
|
'remove', _.size(payload.delta.remove),
|
|
'update', _.size(payload.delta.update),
|
|
'add', _.size(payload.delta.add));
|
|
|
|
connectionState = 'connected';
|
|
|
|
// nodes that no longer exist
|
|
_.each(payload.delta.remove, function(nodeId) {
|
|
// in case node disappears before mouseleave event
|
|
if (mouseOverNodeId === nodeId) {
|
|
mouseOverNodeId = null;
|
|
}
|
|
if (nodes[nodeId] && _.contains(mouseOverEdgeId, nodeId)) {
|
|
mouseOverEdgeId = null;
|
|
}
|
|
delete nodes[nodeId];
|
|
});
|
|
|
|
// update existing nodes
|
|
_.each(payload.delta.update, function(node) {
|
|
nodes[node.id] = node;
|
|
});
|
|
|
|
// add new nodes
|
|
_.each(payload.delta.add, function(node) {
|
|
nodes[node.id] = node;
|
|
});
|
|
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.RECEIVE_TOPOLOGIES:
|
|
topologies = payload.topologies;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.RECEIVE_API_DETAILS:
|
|
version = payload.version;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
case ActionTypes.ROUTE_TOPOLOGY:
|
|
if (currentTopologyId !== payload.state.topologyId
|
|
|| currentGrouping !== payload.state.grouping) {
|
|
nodes = {};
|
|
}
|
|
currentTopologyId = payload.state.topologyId;
|
|
currentGrouping = payload.state.grouping;
|
|
selectedNodeId = payload.state.selectedNodeId;
|
|
AppStore.emit(AppStore.CHANGE_EVENT);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
|
|
}
|
|
};
|
|
|
|
AppStore.dispatchToken = AppDispatcher.register(AppStore.registeredCallback);
|
|
|
|
module.exports = AppStore;
|