source: sandbox/expresso-solr/solr/example/work/jetty-0.0.0.0-8983-solr.war-_solr-any-/webapp/js/scripts/cloud.js @ 7588

Revision 7588, 18.6 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1/*
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements.  See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License.  You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18var init_debug = function( cloud_element )
19{
20  var debug_element = $( '#debug', cloud_element );
21  var debug_button = $( '#menu #cloud .dump a' );
22
23  var clipboard_element = $( '.clipboard', debug_element );
24  var clipboard_button = $( 'a', clipboard_element );
25
26  debug_button
27    .die( 'click' )
28    .live
29    (
30      'click',
31      function( event )
32      {
33        debug_element.trigger( 'show' );
34        return false;
35      }
36    );
37
38  $( '.close', debug_element )
39    .die( 'click' )
40    .live
41    (
42      'click',
43      function( event )
44      {
45        debug_element.trigger( 'hide' );
46        return false;
47      }
48    );
49
50  $( '.clipboard', debug_element )
51    .die( 'click' )
52    .live
53    (
54      'click',
55      function( event )
56      {
57        return false;
58      }
59    );
60
61  debug_element
62    .die( 'show' )
63    .live
64    (
65      'show',
66      function( event )
67      {
68        debug_element.show();
69
70        $.ajax
71        (
72          {
73            url : app.config.solr_path + '/zookeeper?wt=json&dump=true',
74            dataType : 'text',
75            context : debug_element,
76            beforeSend : function( xhr, settings )
77            {
78              $( '.debug', debug_element )
79                .html( '<span class="loader">Loading Dump ...</span>' );
80
81              ZeroClipboard.setMoviePath( 'img/ZeroClipboard.swf' );
82
83              clipboard_client = new ZeroClipboard.Client();
84                             
85              clipboard_client.addEventListener
86              (
87                'load',
88                function( client )
89                {
90                }
91              );
92
93              clipboard_client.addEventListener
94              (
95                'complete',
96                function( client, text )
97                {
98                  clipboard_element
99                    .addClass( 'copied' );
100
101                  clipboard_button
102                    .data( 'text', clipboard_button.text() )
103                    .text( clipboard_button.data( 'copied' ) );
104                }
105              );
106            },
107            success : function( response, text_status, xhr )
108            {
109              clipboard_client.glue
110              (
111                clipboard_element.get(0),
112                clipboard_button.get(0)
113              );
114
115              clipboard_client.setText( response.replace( /\\/g, '\\\\' ) );
116
117              $( '.debug', debug_element )
118                .removeClass( 'loader' )
119                .text( response );
120            },
121            error : function( xhr, text_status, error_thrown )
122            {
123            },
124            complete : function( xhr, text_status )
125            {
126            }
127          }
128        );
129      }
130    )
131    .die( 'hide' )
132    .live
133    (
134      'hide',
135      function( event )
136      {
137        $( '.debug', debug_element )
138          .empty();
139
140        clipboard_element
141          .removeClass( 'copied' );
142
143        clipboard_button
144          .data( 'copied', clipboard_button.text() )
145          .text( clipboard_button.data( 'text' ) );
146
147        clipboard_client.destroy();
148
149        debug_element.hide();
150      }
151    );
152};
153
154var helper_path_class = function( p )
155{
156  var classes = [ 'link' ];
157  classes.push( 'lvl-' + p.target.depth );
158
159  if( p.target.data && p.target.data.leader )
160  {
161    classes.push( 'leader' );
162  }
163
164  if( p.target.data && p.target.data.state )
165  {
166    classes.push( p.target.data.state );
167  }
168
169  return classes.join( ' ' );
170};
171
172var helper_node_class = function( d )
173{
174  var classes = [ 'node' ];
175  classes.push( 'lvl-' + d.depth );
176
177  if( d.data && d.data.leader )
178  {
179    classes.push( 'leader' );
180  }
181
182  if( d.data && d.data.state )
183  {
184    classes.push( d.data.state );
185  }
186
187  return classes.join( ' ' );
188};
189
190var helper_data = {
191  protocol: [],
192  host: [],
193  hostname: [],
194  port: [],
195  pathname: []
196};
197
198var helper_node_text = function( d )
199{
200  if( !d.data || !d.data.uri )
201  {
202    return d.name;
203  }
204
205  var name = d.data.uri.hostname;
206
207  if( 1 !== helper_data.protocol.length )
208  {
209    name = d.data.uri.protocol + '//' + name;
210  }
211
212  if( 1 !== helper_data.port.length )
213  {
214    name += ':' + d.data.uri.port;
215  }
216
217  if( 1 !== helper_data.pathname.length )
218  {
219    name += d.data.uri.pathname;
220  }
221
222  return name;
223};
224
225var generate_graph = function( graph_element, graph_data, leaf_count )
226{
227  var w = graph_element.width(),
228      h = leaf_count * 20;
229
230  var tree = d3.layout.tree()
231    .size([h, w - 400]);
232
233  var diagonal = d3.svg.diagonal()
234    .projection(function(d) { return [d.y, d.x]; });
235
236  var vis = d3.select( '#canvas' ).append( 'svg' )
237    .attr( 'width', w )
238    .attr( 'height', h)
239    .append( 'g' )
240      .attr( 'transform', 'translate(100, 0)' );
241
242  var nodes = tree.nodes( graph_data );
243
244  var link = vis.selectAll( 'path.link' )
245    .data( tree.links( nodes ) )
246    .enter().append( 'path' )
247      .attr( 'class', helper_path_class )
248      .attr( 'd', diagonal );
249
250  var node = vis.selectAll( 'g.node' )
251    .data( nodes )
252    .enter().append( 'g' )
253      .attr( 'class', helper_node_class )
254      .attr( 'transform', function(d) { return 'translate(' + d.y + ',' + d.x + ')'; } )
255
256  node.append( 'circle' )
257    .attr( 'r', 4.5 );
258
259  node.append( 'text' )
260    .attr( 'dx', function( d ) { return 0 === d.depth ? -8 : 8; } )
261    .attr( 'dy', function( d ) { return 5; } )
262    .attr( 'text-anchor', function( d ) { return 0 === d.depth ? 'end' : 'start'; } )
263    .attr( 'data-href', function( d ) { return d.name; } )
264    .text( helper_node_text );
265
266  $( 'text[data-href*="//"]', graph_element )
267    .die( 'click' )
268    .live
269    (
270      'click',
271      function()
272      {
273        location.href = $( this ).data( 'href' );
274      }
275    );
276};
277
278var generate_rgraph = function( graph_element, graph_data, leaf_count )
279{
280  var max_val = Math.min( graph_element.width(), $( 'body' ).height() )
281  var r = max_val / 2;
282
283  var cluster = d3.layout.cluster()
284    .size([360, r - 160]);
285
286  var diagonal = d3.svg.diagonal.radial()
287    .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });
288
289  var vis = d3.select( '#canvas' ).append( 'svg' )
290    .attr( 'width', r * 2 )
291    .attr( 'height', r * 2 )
292    .append( 'g' )
293      .attr( 'transform', 'translate(' + r + ',' + r + ')' );
294
295  var nodes = cluster.nodes( graph_data );
296
297  var link = vis.selectAll( 'path.link' )
298    .data( cluster.links( nodes ) )
299    .enter().append( 'path' )
300      .attr( 'class', helper_path_class )
301      .attr( 'd', diagonal );
302
303  var node = vis.selectAll( 'g.node' )
304    .data( nodes )
305    .enter().append( 'g' )
306      .attr( 'class', helper_node_class )
307      .attr( 'transform', function(d) { return 'rotate(' + (d.x - 90) + ')translate(' + d.y + ')'; } )
308
309  node.append( 'circle' )
310    .attr( 'r', 4.5 );
311
312  node.append( 'text' )
313    .attr( 'dx', function(d) { return d.x < 180 ? 8 : -8; } )
314    .attr( 'dy', '.31em' )
315    .attr( 'text-anchor', function(d) { return d.x < 180 ? 'start' : 'end'; } )
316    .attr( 'transform', function(d) { return d.x < 180 ? null : 'rotate(180)'; } )
317    .attr( 'data-href', function( d ) { return d.name; } )
318    .text( helper_node_text );
319
320  $( 'text[data-href*="//"]', graph_element )
321    .die( 'click' )
322    .live
323    (
324      'click',
325      function()
326      {
327        location.href = $( this ).data( 'href' );
328      }
329    );
330};
331
332var prepare_graph = function( graph_element, callback )
333{
334  $.ajax
335  (
336    {
337      url : app.config.solr_path + '/zookeeper?wt=json&path=%2Flive_nodes',
338      dataType : 'json',
339      success : function( response, text_status, xhr )
340      {
341        var live_nodes = {};
342        for( var c in response.tree[0].children )
343        {
344          live_nodes[response.tree[0].children[c].data.title] = true;
345        }
346
347        $.ajax
348        (
349          {
350            url : app.config.solr_path + '/zookeeper?wt=json&detail=true&path=%2Fclusterstate.json',
351            dataType : 'json',
352            context : graph_element,
353            beforeSend : function( xhr, settings )
354            {
355              this
356                .show();
357            },
358            success : function( response, text_status, xhr )
359            {
360              var state = null;
361              eval( 'state = ' + response.znode.data + ';' );
362             
363              var leaf_count = 0;
364              var graph_data = {
365                name: null,
366                children : []
367              };
368
369              for( var c in state )
370              {
371                var shards = [];
372                for( var s in state[c] )
373                {
374                  var nodes = [];
375                  for( var n in state[c][s] )
376                  {
377                    leaf_count++;
378
379                    var uri = state[c][s][n].base_url;
380                    var parts = uri.match( /^(\w+:)\/\/(([\w\d\.-]+)(:(\d+))?)(.+)$/ );
381                    var uri_parts = {
382                      protocol: parts[1],
383                      host: parts[2],
384                      hostname: parts[3],
385                      port: parseInt( parts[5] || 80, 10 ),
386                      pathname: parts[6]
387                    };
388                   
389                    helper_data.protocol.push( uri_parts.protocol );
390                    helper_data.host.push( uri_parts.host );
391                    helper_data.hostname.push( uri_parts.hostname );
392                    helper_data.port.push( uri_parts.port );
393                    helper_data.pathname.push( uri_parts.pathname );
394
395                    var status = state[c][s][n].state;
396
397                    if( !live_nodes[state[c][s][n].node_name] )
398                    {
399                      status = 'gone';
400                    }
401
402                    var node = {
403                      name: uri,
404                      data: {
405                        type : 'node',
406                        state : status,
407                        leader : 'true' === state[c][s][n].leader,
408                        uri : uri_parts
409                      }
410                    };
411                    nodes.push( node );
412                  }
413
414                  var shard = {
415                    name: s,
416                    data: {
417                      type : 'shard',
418                    },
419                    children: nodes
420                  };
421                  shards.push( shard );
422                }
423
424                var collection = {
425                  name: c,
426                  data: {
427                    type : 'collection',
428                  },
429                  children: shards
430                };
431                graph_data.children.push( collection );
432              }
433             
434              helper_data.protocol = $.unique( helper_data.protocol );
435              helper_data.host = $.unique( helper_data.host );
436              helper_data.hostname = $.unique( helper_data.hostname );
437              helper_data.port = $.unique( helper_data.port );
438              helper_data.pathname = $.unique( helper_data.pathname );
439
440              callback( graph_element, graph_data, leaf_count );
441            },
442            error : function( xhr, text_status, error_thrown)
443            {
444            },
445            complete : function( xhr, text_status )
446            {
447            }
448          }
449        );
450      },
451      error : function( xhr, text_status, error_thrown)
452      {
453      },
454      complete : function( xhr, text_status )
455      {
456      }
457    }
458  );
459
460};
461
462var init_graph = function( graph_element )
463{
464  prepare_graph
465  (
466    graph_element,
467    function( graph_element, graph_data, leaf_count )
468    {
469      generate_graph( graph_element, graph_data, leaf_count );
470    }
471  );
472}
473
474var init_rgraph = function( graph_element )
475{
476  prepare_graph
477  (
478    graph_element,
479    function( graph_element, graph_data, leaf_count )
480    {
481      generate_rgraph( graph_element, graph_data, leaf_count );
482    }
483  );
484}
485
486var init_tree = function( tree_element )
487{
488  $.ajax
489  (
490    {
491      url : app.config.solr_path + '/zookeeper?wt=json',
492      dataType : 'json',
493      context : tree_element,
494      beforeSend : function( xhr, settings )
495      {
496        this
497          .show();
498      },
499      success : function( response, text_status, xhr )
500      {
501        var self = this;
502                     
503        $( '#tree', this )
504          .jstree
505          (
506            {
507              "plugins" : [ "json_data" ],
508              "json_data" : {
509                "data" : response.tree,
510                "progressive_render" : true
511              },
512              "core" : {
513                "animation" : 0
514              }
515            }
516          )
517          .jstree
518          (
519            'open_node',
520            'li:first'
521          );
522
523        var tree_links = $( '#tree a', this );
524
525        tree_links
526          .die( 'click' )
527          .live
528          (
529            'click',
530            function( event )
531            {
532              $( 'a.active', $( this ).parents( '#tree' ) )
533                .removeClass( 'active' );
534                                 
535              $( this )
536                .addClass( 'active' );
537
538              tree_element
539                .addClass( 'show' );
540
541              var file_content = $( '#file-content' );
542
543              $( 'a.close', file_content )
544                .die( 'click' )
545                .live
546                (
547                  'click',
548                  function( event )
549                  {
550                    $( '#tree a.active' )
551                      .removeClass( 'active' );
552                                     
553                    tree_element
554                      .removeClass( 'show' );
555
556                    return false;
557                  }
558                );
559
560              $.ajax
561              (
562                {
563                  url : this.href,
564                  dataType : 'json',
565                  context : file_content,
566                  beforeSend : function( xhr, settings )
567                  {
568                  },
569                  success : function( response, text_status, xhr )
570                  {
571                    var props = [];
572                    for( var key in response.znode.prop )
573                    {
574                      props.push
575                      (
576                        '<li><dl class="clearfix">' + "\n" +
577                          '<dt>' + key.esc() + '</dt>' + "\n" +
578                          '<dd>' + response.znode.prop[key].esc() + '</dd>' + "\n" +
579                        '</dl></li>'
580                      );
581                    }
582
583                    $( '#prop ul', this )
584                      .empty()
585                      .html( props.join( "\n" ) );
586
587                    $( '#prop ul li:odd', this )
588                      .addClass( 'odd' );
589
590                    var data_element = $( '#data', this );
591
592                    if( 0 !== parseInt( response.znode.prop.children_count ) )
593                    {
594                      data_element.hide();
595                    }
596                    else
597                    {
598                      var highlight = false;
599                      var data = '<em>File "' + response.znode.path + '" has no Content</em>';
600
601                      if( response.znode.data )
602                      {
603                        var classes = '';
604                        var path = response.znode.path.split( '.' );
605
606                        if( 1 < path.length )
607                        {
608                          highlight = true;
609                          classes = 'syntax language-' + path.pop().esc();
610                        }
611
612                        data = '<pre class="' + classes + '">'
613                             + response.znode.data.esc()
614                             + '</pre>';
615                      }
616                               
617
618                      data_element
619                          .show()
620                          .html( data );
621
622                      if( highlight )
623                      {
624                        hljs.highlightBlock( data_element.get(0) );
625                      }
626                    }
627                  },
628                  error : function( xhr, text_status, error_thrown)
629                  {
630                  },
631                  complete : function( xhr, text_status )
632                  {
633                  }
634                }
635              );
636
637              return false;
638            }
639          );
640      },
641      error : function( xhr, text_status, error_thrown )
642      {
643        var message = 'Loading of <code>' + app.config.zookeeper_path + '</code> failed with "' + text_status + '" '
644                    + '(<code>' + error_thrown.message + '</code>)';
645
646        if( 200 !== xhr.status )
647        {
648          message = 'Loading of <code>' + app.config.zookeeper_path + '</code> failed with HTTP-Status ' + xhr.status + ' ';
649        }
650
651        this
652          .html( '<div class="block" id="error">' + message + '</div>' );
653      },
654      complete : function( xhr, text_status )
655      {
656      }
657    }
658  );
659};
660
661// #/~cloud
662sammy.get
663(
664  /^#\/(~cloud)$/,
665  function( context )
666  {
667    var content_element = $( '#content' );
668
669    $.get
670    (
671      'tpl/cloud.html',
672      function( template )
673      {
674        content_element
675          .html( template );
676
677        var cloud_element = $( '#cloud', content_element );
678        var navigation_element = $( '#menu #cloud' );
679
680        init_debug( cloud_element );
681
682        $( '.tree', navigation_element )
683          .die( 'activate' )
684          .live
685          (
686            'activate',
687            function( event )
688            {
689              $( this ).addClass( 'active' );
690              init_tree( $( '#tree-content', cloud_element ) );
691            }
692          );
693
694        $( '.graph', navigation_element )
695          .die( 'activate' )
696          .live
697          (
698            'activate',
699            function( event )
700            {
701              $( this ).addClass( 'active' );
702              init_graph( $( '#graph-content', cloud_element ) );
703            }
704          );
705
706        $( '.rgraph', navigation_element )
707          .die( 'activate' )
708          .live
709          (
710            'activate',
711            function( event )
712            {
713              $( this ).addClass( 'active' );
714              init_rgraph( $( '#graph-content', cloud_element ) );
715            }
716          );
717
718        $( 'a[href="' + context.path + '"]', navigation_element )
719          .trigger( 'activate' );
720       
721      }
722    );
723  }
724);
Note: See TracBrowser for help on using the repository browser.