1 (function( $, app, i18n ) {
4 var services = app.ns("services");
6 // ( master ) master = true, data = true
7 // ( coordinator ) master = true, data = false
8 // ( worker ) master = false, data = true;
9 // ( client ) master = false, data = false;
12 function nodeSort_name(a, b) {
13 if (!(a.cluster && b.cluster)) {
16 return a.cluster.name.toString().localeCompare( b.cluster.name.toString() );
19 function nodeSort_addr( a, b ) {
20 if (!(a.cluster && b.cluster)) {
23 return a.cluster.transport_address.toString().localeCompare( b.cluster.transport_address.toString() );
26 function nodeSort_type( a, b ) {
27 if (!(a.cluster && b.cluster)) {
32 } else if( b.master_node ) {
34 } else if( a.data_node && !b.data_node ) {
36 } else if( b.data_node && !a.data_node ) {
39 return a.cluster.name.toString().localeCompare( b.cluster.name.toString() );
43 var NODE_SORT_TYPES = {
44 "Sort.ByName": nodeSort_name,
45 "Sort.ByAddress": nodeSort_addr,
46 "Sort.ByType": nodeSort_type
49 function nodeFilter_none( a ) {
53 function nodeFilter_clients( a ) {
54 return (a.master_node || a.data_node );
58 ui.ClusterOverview = ui.Page.extend({
60 cluster: null // (reqired) an instanceof app.services.Cluster
64 this.cluster = this.config.cluster;
65 this.prefs = services.Preferences.instance();
66 this._clusterState = this.config.clusterState;
67 this._clusterState.on("data", this.draw_handler );
68 this._refreshButton = new ui.RefreshButton({
69 onRefresh: this.refresh.bind(this),
70 onChange: function( btn ) {
71 if( btn.value === -1 ) {
76 var nodeSortPref = this.prefs.get("clusterOverview-nodeSort") || Object.keys(NODE_SORT_TYPES)[0];
77 this._nodeSort = NODE_SORT_TYPES[ nodeSortPref ];
78 this._nodeSortMenu = new ui.MenuButton({
79 label: i18n.text( "Preference.SortCluster" ),
80 menu: new ui.SelectMenuPanel({
82 items: Object.keys( NODE_SORT_TYPES ).map( function( k ) {
83 return { text: i18n.text( k ), value: k };
85 onSelect: function( panel, event ) {
86 this._nodeSort = NODE_SORT_TYPES[ event.value ];
87 this.prefs.set("clusterOverview-nodeSort", event.value );
92 this._indicesSort = this.prefs.get( "clusterOverview-indicesSort") || "desc";
93 this._indicesSortMenu = new ui.MenuButton({
94 label: i18n.text( "Preference.SortIndices" ),
95 menu: new ui.SelectMenuPanel({
96 value: this._indicesSort,
98 { value: "desc", text: i18n.text( "SortIndices.Descending" ) },
99 { value: "asc", text: i18n.text( "SortIndices.Ascending" ) } ],
100 onSelect: function( panel, event ) {
101 this._indicesSort = event.value;
102 this.prefs.set( "clusterOverview-indicesSort", this._indicesSort );
107 this._aliasRenderer = this.prefs.get( "clusterOverview-aliasRender" ) || "full";
108 this._aliasMenu = new ui.MenuButton({
109 label: i18n.text( "Preference.ViewAliases" ),
110 menu: new ui.SelectMenuPanel({
111 value: this._aliasRenderer,
113 { value: "full", text: i18n.text( "ViewAliases.Grouped" ) },
114 { value: "list", text: i18n.text( "ViewAliases.List" ) },
115 { value: "none", text: i18n.text( "ViewAliases.None" ) } ],
116 onSelect: function( panel, event ) {
117 this._aliasRenderer = event.value;
118 this.prefs.set( "clusterOverview-aliasRender", this._aliasRenderer );
123 this._indexFilter = new ui.TextField({
124 value: this.prefs.get("clusterOverview-indexFilter"),
125 placeholder: i18n.text( "Overview.IndexFilter" ),
126 onchange: function( indexFilter ) {
127 this.prefs.set("clusterOverview-indexFilter", indexFilter.val() );
131 this.el = $(this._main_template());
132 this.tablEl = this.el.find(".uiClusterOverview-table");
136 this._clusterState.removeObserver( "data", this.draw_handler );
138 refresh: function() {
139 this._refreshButton.disable();
140 this._clusterState.refresh();
142 draw_handler: function() {
143 var data = this._clusterState;
146 var indexFilterRe = new RegExp( this._indexFilter.val() );
147 indexFilter = function(s) { return indexFilterRe.test(s); };
149 indexFilter = function() { return true; };
151 var clusterState = data.clusterState;
152 var status = data.status;
153 var nodeStats = data.nodeStats;
154 var clusterNodes = data.clusterNodes;
158 var nodeIndices = {};
159 var indexIndices = {}, indexIndicesIndex = 0;
160 function newNode(n) {
164 master_node: clusterState.master_node === n
167 function newIndex(i) {
173 function getIndexForNode(n) {
174 return nodeIndices[n] = (n in nodeIndices) ? nodeIndices[n] : nodes.push(newNode(n)) - 1;
176 function getIndexForIndex(routings, i) {
177 var index = indexIndices[i] = (i in indexIndices) ?
178 (routings[indexIndices[i]] = routings[indexIndices[i]] || newIndex(i)) && indexIndices[i]
179 : ( ( routings[indexIndicesIndex] = newIndex(i) ) && indexIndicesIndex++ );
183 $.each(clusterNodes.nodes, function(name, node) {
184 getIndexForNode(name);
188 $.each(clusterState.routing_table.indices, function(name, index){
189 indexNames.push(name);
192 if (this._indicesSort === "desc") indexNames.reverse();
193 indexNames.filter( indexFilter ).forEach(function(name) {
194 var indexObject = clusterState.routing_table.indices[name];
195 $.each(indexObject.shards, function(name, shard) {
196 shard.forEach(function(replica){
197 var node = replica.node;
198 if(node === null) { node = "Unassigned"; }
199 var index = replica.index;
200 var shard = replica.shard;
201 var routings = nodes[getIndexForNode(node)].routings;
202 var indexIndex = getIndexForIndex(routings, index);
203 var replicas = routings[indexIndex].replicas;
204 if(node === "Unassigned" || !indexObject.shards[shard]) {
205 replicas.push({ replica: replica });
209 status: indexObject.shards[shard].filter(function(replica) {
210 return replica.node === node;
217 indices = indices.map(function(index){
221 metadata: clusterState.metadata.indices[index],
222 status: status.indices[index]
225 $.each(clusterState.metadata.indices, function(name, index) {
226 if(index.state === "close" && indexFilter( name )) {
235 nodes.forEach(function(node) {
236 node.stats = nodeStats.nodes[node.name];
237 var cluster = clusterNodes.nodes[node.name];
238 node.cluster = cluster || { name: "<unknown>" };
239 node.data_node = !( cluster && cluster.attributes && cluster.attributes.data === "false" );
240 for(var i = 0; i < indices.length; i++) {
241 node.routings[i] = node.routings[i] || { name: indices[i].name, replicas: [] };
242 node.routings[i].max_number_of_shards = indices[i].metadata.settings["index.number_of_shards"];
243 node.routings[i].open = indices[i].state === "open";
246 var aliasesIndex = {};
248 var indexClone = indices.map(function() { return false; });
249 $.each(clusterState.metadata.indices, function(name, index) {
250 index.aliases.forEach(function(alias) {
251 var aliasIndex = aliasesIndex[alias] = (alias in aliasesIndex) ? aliasesIndex[alias] : aliases.push( { name: alias, max: -1, min: 999, indices: [].concat(indexClone) }) - 1;
252 var indexIndex = indexIndices[name];
253 var aliasRow = aliases[aliasIndex];
254 aliasRow.min = Math.min(aliasRow.min, indexIndex);
255 aliasRow.max = Math.max(aliasRow.max, indexIndex);
256 aliasRow.indices[indexIndex] = indices[indexIndex];
259 cluster.aliases = aliases;
260 cluster.nodes = nodes
261 .filter( nodeFilter_none )
262 .sort( this._nodeSort );
263 indices.unshift({ name: null });
264 this._drawNodesView( cluster, indices );
265 this._refreshButton.enable();
267 _drawNodesView: function( cluster, indices ) {
268 this._nodesView && this._nodesView.remove();
269 this._nodesView = new ui.NodesView({
270 onRedraw: function() {
273 interactive: ( this._refreshButton.value === -1 ),
274 aliasRenderer: this._aliasRenderer,
275 cluster: this.cluster,
281 this._nodesView.attach( this.tablEl );
283 _main_template: function() {
284 return { tag: "DIV", id: this.id(), cls: "uiClusterOverview", children: [
286 label: i18n.text("Overview.PageTitle"),
289 this._indicesSortMenu,
297 { tag: "DIV", cls: "uiClusterOverview-table" }
302 })( this.jQuery, this.app, this.i18n );