08ce1d30112f2b42d526c582525791a4190ced72
[ccsdk/features.git] /
1 (function( app, i18n, joey ) {
2
3         var ui = app.ns("ui");
4         var ut = app.ns("ut");
5
6         ui.NodesView = ui.AbstractWidget.extend({
7                 defaults: {
8                         interactive: true,
9                         aliasRenderer: "list",
10                         scaleReplicas: 1,
11                         cluster: null,
12                         data: null
13                 },
14                 init: function() {
15                         this._super();
16                         this.interactive = this.config.interactive;
17                         this.cluster = this.config.cluster;
18                         this._aliasRenderFunction = {
19                                 "none": this._aliasRender_template_none,
20                                 "list": this._aliasRender_template_list,
21                                 "full": this._aliasRender_template_full
22                         }[ this.config.aliasRenderer ];
23                         this._styleSheetEl = joey({ tag: "STYLE", text: ".uiNodesView-nullReplica, .uiNodesView-replica { zoom: " + this.config.scaleReplicas + " }" });
24                         this.el = $( this._main_template( this.config.data.cluster, this.config.data.indices ) );
25                 },
26
27                 _newAliasAction_handler: function( index ) {
28                         var fields = new app.ux.FieldCollection({
29                                 fields: [
30                                         new ui.TextField({ label: i18n.text("AliasForm.AliasName"), name: "alias", require: true })
31                                 ]
32                         });
33                         var dialog = new ui.DialogPanel({
34                                 title: i18n.text("AliasForm.NewAliasForIndexName", index.name),
35                                 body: new ui.PanelForm({ fields: fields }),
36                                 onCommit: function(panel, args) {
37                                         if(fields.validate()) {
38                                                 var data = fields.getData();
39                                                 var command = {
40                                                         "actions" : [
41                                                                 { "add" : { "index" : index.name, "alias" : data["alias"] } }
42                                                         ]
43                                                 };
44                                                 this.config.cluster.post('_aliases', JSON.stringify(command), function(d) {
45                                                         dialog.close();
46                                                         alert(JSON.stringify(d));
47                                                         this.fire("redraw");
48                                                 }.bind(this) );
49                                         }
50                                 }.bind(this)
51                         }).open();
52                 },
53                 _postIndexAction_handler: function(action, index, redraw) {
54                         this.cluster.post(encodeURIComponent( index.name ) + "/" + encodeURIComponent( action ), null, function(r) {
55                                 alert(JSON.stringify(r));
56                                 redraw && this.fire("redraw");
57                         }.bind(this));
58                 },
59                 _optimizeIndex_handler: function(index) {
60                         var fields = new app.ux.FieldCollection({
61                                 fields: [
62                                         new ui.TextField({ label: i18n.text("OptimizeForm.MaxSegments"), name: "max_num_segments", value: "1", require: true }),
63                                         new ui.CheckField({ label: i18n.text("OptimizeForm.ExpungeDeletes"), name: "only_expunge_deletes", value: false }),
64                                         new ui.CheckField({ label: i18n.text("OptimizeForm.FlushAfter"), name: "flush", value: true }),
65                                         new ui.CheckField({ label: i18n.text("OptimizeForm.WaitForMerge"), name: "wait_for_merge", value: false })
66                                 ]
67                         });
68                         var dialog = new ui.DialogPanel({
69                                 title: i18n.text("OptimizeForm.OptimizeIndex", index.name),
70                                 body: new ui.PanelForm({ fields: fields }),
71                                 onCommit: function( panel, args ) {
72                                         if(fields.validate()) {
73                                                 this.cluster.post(encodeURIComponent( index.name ) + "/_optimize", fields.getData(), function(r) {
74                                                         alert(JSON.stringify(r));
75                                                 });
76                                                 dialog.close();
77                                         }
78                                 }.bind(this)
79                         }).open();
80                 },
81                 _testAnalyser_handler: function(index) {
82                         this.cluster.get(encodeURIComponent( index.name ) + "/_analyze?text=" + encodeURIComponent( prompt( i18n.text("IndexCommand.TextToAnalyze") ) ), function(r) {
83                                 alert(JSON.stringify(r, true, "  "));
84                         });
85                 },
86                 _deleteIndexAction_handler: function(index) {
87                         if( prompt( i18n.text("AliasForm.DeleteAliasMessage", i18n.text("Command.DELETE"), index.name ) ) === i18n.text("Command.DELETE") ) {
88                                 this.cluster["delete"](encodeURIComponent( index.name ), null, function(r) {
89                                         alert(JSON.stringify(r));
90                                         this.fire("redraw");
91                                 }.bind(this) );
92                         }
93                 },
94                 _shutdownNode_handler: function(node) {
95                         if(prompt( i18n.text("IndexCommand.ShutdownMessage", i18n.text("Command.SHUTDOWN"), node.cluster.name ) ) === i18n.text("Command.SHUTDOWN") ) {
96                                 this.cluster.post( "_cluster/nodes/" + encodeURIComponent( node.name ) + "/_shutdown", null, function(r) {
97                                         alert(JSON.stringify(r));
98                                         this.fire("redraw");
99                                 }.bind(this));
100                         }
101                 },
102                 _deleteAliasAction_handler: function( index, alias ) {
103                         if( confirm( i18n.text("Command.DeleteAliasMessage" ) ) ) {
104                                 var command = {
105                                         "actions" : [
106                                                 { "remove" : { "index" : index.name, "alias" : alias.name } }
107                                         ]
108                                 };
109                                 this.config.cluster.post('_aliases', JSON.stringify(command), function(d) {
110                                         alert(JSON.stringify(d));
111                                         this.fire("redraw");
112                                 }.bind(this) );
113                         }
114                 },
115
116                 _replica_template: function(replica) {
117                         var r = replica.replica;
118                         return { tag: "DIV",
119                                 cls: "uiNodesView-replica" + (r.primary ? " primary" : "") + ( " state-" + r.state ),
120                                 text: r.shard.toString(),
121                                 onclick: function() { new ui.JsonPanel({
122                                         json: replica.status || r,
123                                         title: r.index + "/" + r.node + " [" + r.shard + "]" });
124                                 }
125                         };
126                 },
127                 _routing_template: function(routing) {
128                         var cell = { tag: "TD", cls: "uiNodesView-routing" + (routing.open ? "" : " close"), children: [] };
129                         for(var i = 0; i < routing.replicas.length; i++) {
130                                 if(i % routing.max_number_of_shards === 0 && i > 0) {
131                                         cell.children.push({ tag: "BR" });
132                                 }
133                                 if( routing.replicas[i] ) {
134                                         cell.children.push(this._replica_template(routing.replicas[i]));
135                                 } else {
136                                         cell.children.push( { tag: "DIV", cls: "uiNodesView-nullReplica" } );
137                                 }
138                         }
139                         return cell;
140                 },
141                 _nodeControls_template: function( node ) { return (
142                         { tag: "DIV", cls: "uiNodesView-controls", children: [
143                                 new ui.MenuButton({
144                                         label: i18n.text("NodeInfoMenu.Title"),
145                                         menu: new ui.MenuPanel({
146                                                 items: [
147                                                         { text: i18n.text("NodeInfoMenu.ClusterNodeInfo"), onclick: function() { new ui.JsonPanel({ json: node.cluster, title: node.name });} },
148                                                         { text: i18n.text("NodeInfoMenu.NodeStats"), onclick: function() { new ui.JsonPanel({ json: node.stats, title: node.name });} }
149                                                 ]
150                                         })
151                                 }),
152                                 new ui.MenuButton({
153                                         label: i18n.text("NodeActionsMenu.Title"),
154                                         menu: new ui.MenuPanel({
155                                                 items: [
156                                                         { text: i18n.text("NodeActionsMenu.Shutdown"), onclick: function() { this._shutdownNode_handler(node); }.bind(this) }
157                                                 ]
158                                         })
159                                 })
160                         ] }
161                 ); },
162                 _nodeIcon_template: function( node ) {
163                         var icon, alt;
164                         if( node.name === "Unassigned" ) {
165                                 icon = "fa-exclamation-triangle";
166                                 alt = i18n.text( "NodeType.Unassigned" );
167                         } else if( node.cluster.settings && "tribe" in node.cluster.settings) {
168                                 icon = "fa-sitemap";
169                                 alt = i18n.text("NodeType.Tribe" );
170                         } else {
171                                 icon = "fa-" + (node.master_node ? "star" : "circle") + (node.data_node ? "" : "-o" );
172                                 alt = i18n.text( node.master_node ? ( node.data_node ? "NodeType.Master" : "NodeType.Coord" ) : ( node.data_node ? "NodeType.Worker" : "NodeType.Client" ) );
173                         }
174                         return { tag: "TD", title: alt, cls: "uiNodesView-icon", children: [
175                                 { tag: "SPAN", cls: "fa fa-2x " + icon }
176                         ] };
177                 },
178                 _node_template: function(node) {
179                         return { tag: "TR", cls: "uiNodesView-node" + (node.master_node ? " master": ""), children: [
180                                 this._nodeIcon_template( node ),
181                                 { tag: "TH", children: node.name === "Unassigned" ? [
182                                         { tag: "H3", text: node.name }
183                                 ] : [
184                                         { tag: "H3", text: node.cluster.name },
185                                         { tag: "DIV", text: node.cluster.hostname },
186                                         this.interactive ? this._nodeControls_template( node ) : null
187                                 ] }
188                         ].concat(node.routings.map(this._routing_template, this))};
189                 },
190                 _indexHeaderControls_template: function( index ) { return (
191                         { tag: "DIV", cls: "uiNodesView-controls", children: [
192                                 new ui.MenuButton({
193                                         label: i18n.text("IndexInfoMenu.Title"),
194                                         menu: new ui.MenuPanel({
195                                                 items: [
196                                                         { text: i18n.text("IndexInfoMenu.Status"), onclick: function() { new ui.JsonPanel({ json: index.status, title: index.name }); } },
197                                                         { text: i18n.text("IndexInfoMenu.Metadata"), onclick: function() { new ui.JsonPanel({ json: index.metadata, title: index.name }); } }
198                                                 ]
199                                         })
200                                 }),
201                                 new ui.MenuButton({
202                                         label: i18n.text("IndexActionsMenu.Title"),
203                                         menu: new ui.MenuPanel({
204                                                 items: [
205                                                         { text: i18n.text("IndexActionsMenu.NewAlias"), onclick: function() { this._newAliasAction_handler(index); }.bind(this) },
206                                                         { text: i18n.text("IndexActionsMenu.Refresh"), onclick: function() { this._postIndexAction_handler("_refresh", index, false); }.bind(this) },
207                                                         { text: i18n.text("IndexActionsMenu.Flush"), onclick: function() { this._postIndexAction_handler("_flush", index, false); }.bind(this) },
208                                                         { text: i18n.text("IndexActionsMenu.Optimize"), onclick: function () { this._optimizeIndex_handler(index); }.bind(this) },
209                                                         { text: i18n.text("IndexActionsMenu.Snapshot"), disabled: closed, onclick: function() { this._postIndexAction_handler("_gateway/snapshot", index, false); }.bind(this) },
210                                                         { text: i18n.text("IndexActionsMenu.Analyser"), onclick: function() { this._testAnalyser_handler(index); }.bind(this) },
211                                                         { text: (index.state === "close") ? i18n.text("IndexActionsMenu.Open") : i18n.text("IndexActionsMenu.Close"), onclick: function() { this._postIndexAction_handler((index.state === "close") ? "_open" : "_close", index, true); }.bind(this) },
212                                                         { text: i18n.text("IndexActionsMenu.Delete"), onclick: function() { this._deleteIndexAction_handler(index); }.bind(this) }
213                                                 ]
214                                         })
215                                 })
216                         ] }
217                 ); },
218                 _indexHeader_template: function( index ) {
219                         var closed = index.state === "close";
220                         var line1 = closed ? "index: close" : ( "size: " + (index.status && index.status.primaries && index.status.total ? ut.byteSize_template( index.status.primaries.store.size_in_bytes ) + " (" + ut.byteSize_template( index.status.total.store.size_in_bytes ) + ")" : "unknown" ) );
221                         var line2 = closed ? "\u00A0" : ( "docs: " + (index.status && index.status.primaries && index.status.primaries.docs && index.status.total && index.status.total.docs ? index.status.primaries.docs.count.toLocaleString() + " (" + (index.status.total.docs.count + index.status.total.docs.deleted).toLocaleString() + ")" : "unknown" ) );
222                         return index.name ? { tag: "TH", cls: (closed ? "close" : ""), children: [
223                                 { tag: "H3", text: index.name },
224                                 { tag: "DIV", text: line1 },
225                                 { tag: "DIV", text: line2 },
226                                 this.interactive ? this._indexHeaderControls_template( index ) : null
227                         ] } : [ { tag: "TD" }, { tag: "TH" } ];
228                 },
229                 _aliasRender_template_none: function( cluster, indices ) {
230                         return null;
231                 },
232                 _aliasRender_template_list: function( cluster, indices ) {
233                         return cluster.aliases.length && { tag: "TBODY", children: [
234                                 { tag: "TR", children: [
235                                         { tag: "TD" }
236                                 ].concat( indices.map( function( index ) {
237                                         return { tag: "TD", children: index.metadata && index.metadata.aliases.map( function( alias ) {
238                                                 return { tag: "LI", text: alias };
239                                         } ) };
240                                 })) }
241                         ] };
242                 },
243                 _aliasRender_template_full: function( cluster, indices ) {
244                         return cluster.aliases.length && { tag: "TBODY", children: cluster.aliases.map( function(alias, row) {
245                                 return { tag: "TR", children: [ { tag: "TD" },{ tag: "TD" } ].concat(alias.indices.map(function(index, i) {
246                                         if (index) {
247                                                 return {
248                                                         tag: "TD",
249                                                         css: { background: "#" + "9ce9c7fc9".substr((row+6)%7,3) },
250                                                         cls: "uiNodesView-hasAlias" + ( alias.min === i ? " min" : "" ) + ( alias.max === i ? " max" : "" ),
251                                                         text: alias.name,
252                                                         children: this.interactive ? [
253                                                                 {       tag: 'SPAN',
254                                                                         text: i18n.text("General.CloseGlyph"),
255                                                                         cls: 'uiNodesView-hasAlias-remove',
256                                                                         onclick: this._deleteAliasAction_handler.bind( this, index, alias )
257                                                                 }
258                                                         ]: null
259                                                 };
260                                         }       else {
261                                                 return { tag: "TD" };
262                                         }
263                                 }, this ) ) };
264                         }, this )       };
265                 },
266                 _main_template: function(cluster, indices) {
267                         return { tag: "TABLE", cls: "table uiNodesView", children: [
268                                 this._styleSheetEl,
269                                 { tag: "THEAD", children: [ { tag: "TR", children: indices.map(this._indexHeader_template, this) } ] },
270                                 this._aliasRenderFunction( cluster, indices ),
271                                 { tag: "TBODY", children: cluster.nodes.map(this._node_template, this) }
272                         ] };
273                 }
274
275         });
276
277 })( this.app, this.i18n, this.joey );