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

Revision 7588, 16.4 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 convert_duration_to_seconds = function( str )
19{
20  var ret = 0;
21  var parts = new String( str ).split( '.' ).shift().split( ':' ).reverse();
22  var parts_count = parts.length;
23   
24  for( var i = 0; i < parts_count; i++ )
25  {
26    ret += parseInt( parts[i], 10 ) * Math.pow( 60, i );
27  }
28
29  return ret;
30}
31
32var convert_seconds_to_readable_time = function( value )
33{
34  var text = [];
35  value = parseInt( value );
36
37  var minutes = Math.floor( value / 60 );
38  var hours = Math.floor( minutes / 60 );
39
40  if( 0 !== hours )
41  {
42    text.push( hours + 'h' );
43    value -= hours * 60 * 60;
44    minutes -= hours * 60;
45  }
46
47  if( 0 !== minutes )
48  {
49    text.push( minutes + 'm' );
50    value -= minutes * 60;
51  }
52
53  if( 0 !== value )
54  {
55    text.push( value + 's' );
56  }
57
58  return text.join( ' ' );
59}
60
61sammy.bind
62(
63  'dataimport_queryhandler_load',
64  function( event, params )
65  {
66    var core_basepath = params.active_core.attr( 'data-basepath' );
67
68    $.ajax
69    (
70      {
71        url : core_basepath + '/admin/mbeans?cat=QUERYHANDLER&wt=json',
72        dataType : 'json',
73        beforeSend : function( xhr, settings )
74        {
75        },
76        success : function( response, text_status, xhr )
77        {
78          var handlers = response['solr-mbeans'][1];
79          var dataimport_handlers = [];
80          for( var key in handlers )
81          {
82            if( handlers[key]['class'] !== key &&
83              handlers[key]['class'] === 'org.apache.solr.handler.dataimport.DataImportHandler' )
84            {
85              dataimport_handlers.push( key );
86            }
87          }
88          params.callback( dataimport_handlers );
89        },
90        error : function( xhr, text_status, error_thrown)
91        {
92        },
93        complete : function( xhr, text_status )
94        {
95        }
96      }
97    );
98  }
99);
100
101// #/:core/dataimport
102sammy.get
103(
104  /^#\/([\w\d-]+)\/(dataimport)$/,
105  function( context )
106  {
107    sammy.trigger
108    (
109      'dataimport_queryhandler_load',
110      {
111        active_core : this.active_core,
112        callback :  function( dataimport_handlers )
113        {
114          if( 0 === dataimport_handlers.length )
115          {
116            $( '#content' )
117              .html( 'sorry, no dataimport-handler defined!' );
118
119            return false;
120          }
121
122          context.redirect( context.path + '/' + dataimport_handlers[0] );
123        }
124      }
125    );
126  }
127);
128
129// #/:core/dataimport
130sammy.get
131(
132  /^#\/([\w\d-]+)\/(dataimport)\//,
133  function( context )
134  {
135    var core_basepath = this.active_core.attr( 'data-basepath' );
136    var content_element = $( '#content' );
137
138    var path_parts = this.path.match( /^(.+\/dataimport\/)(.*)$/ );
139    var handler_url = core_basepath + path_parts[2];
140       
141    $( 'li.dataimport', this.active_core )
142      .addClass( 'active' );
143
144    $.get
145    (
146      'tpl/dataimport.html',
147      function( template )
148      {
149        content_element
150          .html( template );
151
152        var dataimport_element = $( '#dataimport', content_element );
153        var form_element = $( '#form', dataimport_element );
154        var config_element = $( '#config', dataimport_element );
155        var config_error_element = $( '#config-error', dataimport_element );
156
157        // handler
158
159        sammy.trigger
160        (
161          'dataimport_queryhandler_load',
162          {
163            active_core : context.active_core,
164            callback :  function( dataimport_handlers )
165            {
166              var handlers_element = $( '#navigation ul', form_element );
167              var handlers = [];
168
169              for( var i = 0; i < dataimport_handlers.length; i++ )
170              {
171                handlers.push
172                (
173                    '<li><a href="' + path_parts[1] + dataimport_handlers[i] + '">' +
174                    dataimport_handlers[i] +
175                    '</a></li>'
176                );
177              }
178
179              $( handlers_element )
180                .html( handlers.join( "\n") ) ;
181                           
182              $( 'a[href="' + context.path + '"]', handlers_element ).closest( 'li' )
183                .addClass( 'current' );
184
185              $( 'form', form_element )
186                .show();
187            }
188          }
189        );
190
191        // config
192
193        function dataimport_fetch_config()
194        {
195          $.ajax
196          (
197            {
198              url : handler_url + '?command=show-config',
199              dataType : 'xml',
200              context : $( '#dataimport_config', config_element ),
201              beforeSend : function( xhr, settings )
202              {
203              },
204              success : function( config, text_status, xhr )
205              {
206                dataimport_element
207                  .removeClass( 'error' );
208                                   
209                config_error_element
210                  .hide();
211
212                config_element
213                  .addClass( 'hidden' );
214
215
216                var entities = [ '<option value=""></option>' ];
217
218                $( 'document > entity', config )
219                  .each
220                  (
221                    function( i, element )
222                    {
223                      entities.push( '<option>' + $( element ).attr( 'name' ).esc() + '</option>' );
224                    }
225                  );
226                               
227                $( '#entity', form_element )
228                  .html( entities.join( "\n" ) );
229              },
230              error : function( xhr, text_status, error_thrown )
231              {
232                if( 'parsererror' === error_thrown )
233                {
234                  dataimport_element
235                    .addClass( 'error' );
236                                   
237                  config_error_element
238                    .show();
239
240                  config_element
241                    .removeClass( 'hidden' );
242                }
243              },
244              complete : function( xhr, text_status )
245              {
246                var code = $(
247                  '<pre class="syntax language-xml"><code>' +
248                  xhr.responseText.esc() +
249                  '</code></pre>'
250                );
251                this.html( code );
252
253                if( 'success' === text_status )
254                {
255                  hljs.highlightBlock( code.get(0) );
256                }
257              }
258            }
259          );
260        }
261        dataimport_fetch_config();
262
263        $( '.toggle', config_element )
264          .die( 'click' )
265          .live
266          (
267            'click',
268            function( event )
269            {
270              $( this ).parents( '.block' )
271                .toggleClass( 'hidden' );
272                           
273              return false;
274            }
275          )
276
277        var reload_config_element = $( '.reload_config', config_element );
278        reload_config_element
279          .die( 'click' )
280          .live
281          (
282            'click',
283            function( event )
284            {
285              $.ajax
286              (
287                {
288                  url : handler_url + '?command=reload-config',
289                  dataType : 'xml',
290                  context: $( this ),
291                  beforeSend : function( xhr, settings )
292                  {
293                    this
294                      .removeClass( 'error' )
295                      .addClass( 'loader' );
296                  },
297                  success : function( response, text_status, xhr )
298                  {
299                    this
300                      .addClass( 'success' );
301
302                    window.setTimeout
303                    (
304                      function()
305                      {
306                        reload_config_element
307                          .removeClass( 'success' );
308                      },
309                      5000
310                    );
311                  },
312                  error : function( xhr, text_status, error_thrown )
313                  {
314                    this
315                      .addClass( 'error' );
316                  },
317                  complete : function( xhr, text_status )
318                  {
319                    this
320                      .removeClass( 'loader' );
321                                       
322                    dataimport_fetch_config();
323                  }
324                }
325              );
326              return false;
327            }
328          )
329
330        // state
331               
332        function dataimport_fetch_status()
333        {
334          $.ajax
335          (
336            {
337              url : handler_url + '?command=status',
338              dataType : 'xml',
339              beforeSend : function( xhr, settings )
340              {
341              },
342              success : function( response, text_status, xhr )
343              {
344                var state_element = $( '#current_state', content_element );
345
346                var status = $( 'str[name="status"]', response ).text();
347                var rollback_element = $( 'str[name="Rolledback"]', response );
348                var messages_count = $( 'lst[name="statusMessages"] str', response ).size();
349
350                var started_at = $( 'str[name="Full Dump Started"]', response ).text();
351                if( !started_at )
352                {
353                  started_at = (new Date()).toGMTString();
354                }
355
356                function dataimport_compute_details( response, details_element )
357                {
358                  var details = [];
359                                   
360                  var requests = parseInt( $( 'str[name="Total Requests made to DataSource"]', response ).text(), 10 );
361                  if( requests )
362                  {
363                    details.push
364                    (
365                      '<abbr title="Total Requests made to DataSource">Requests</abbr>: ' +
366                      requests
367                    );
368                  }
369
370                  var fetched = parseInt( $( 'str[name="Total Rows Fetched"]', response ).text(), 10 );
371                  if( fetched )
372                  {
373                    details.push
374                    (
375                      '<abbr title="Total Rows Fetched">Fetched</abbr>: ' +
376                      fetched
377                    );
378                  }
379
380                  var skipped = parseInt( $( 'str[name="Total Documents Skipped"]', response ).text(), 10 );
381                  if( requests )
382                  {
383                    details.push
384                    (
385                      '<abbr title="Total Documents Skipped">Skipped</abbr>: ' +
386                      skipped
387                    );
388                  }
389
390                  var processed = parseInt( $( 'str[name="Total Documents Processed"]', response ).text(), 10 );
391                  if( processed )
392                  {
393                    details.push
394                    (
395                      '<abbr title="Total Documents Processed">Processed</abbr>: ' +
396                      processed
397                    );
398                  }
399
400                  details_element
401                    .html( details.join( ', ' ) )
402                    .show();
403                }
404
405                state_element
406                  .removeClass( 'indexing' )
407                  .removeClass( 'success' )
408                  .removeClass( 'failure' );
409                               
410                $( '.info', state_element )
411                  .removeClass( 'loader' );
412
413                if( 0 !== rollback_element.size() )
414                {
415                  state_element
416                    .addClass( 'failure' )
417                    .show();
418
419                  $( '.time', state_element )
420                    .text( rollback_element.text() )
421                    .timeago()
422                    .show();
423
424                  $( '.info strong', state_element )
425                    .text( $( 'str[name=""]', response ).text() );
426
427                  $( '.info .details', state_element )
428                    .hide();
429                                   
430                  console.debug( 'rollback @ ', rollback_element.text() );
431                }
432                else if( 'idle' === status && 0 !== messages_count )
433                {
434                  state_element
435                    .addClass( 'success' )
436                    .show();
437
438                  $( '.time', state_element )
439                    .text( started_at )
440                    .timeago()
441                    .show();
442
443                  $( '.info strong', state_element )
444                    .text( $( 'str[name=""]', response ).text() );
445
446                  dataimport_compute_details( response, $( '.info .details', state_element ) );
447                }
448                else if( 'busy' === status )
449                {
450                  state_element
451                    .addClass( 'indexing' )
452                    .show();
453
454                  $( '.time', state_element )
455                    .text( started_at )
456                    .timeago()
457                    .show();
458
459                  $( '.info', state_element )
460                    .addClass( 'loader' );
461
462                  var indexing_text = 'Indexing ...';
463
464                  var time_elapsed_text = $( 'str[name="Time Elapsed"]', response ).text();
465                  time_elapsed_text = convert_seconds_to_readable_time( convert_duration_to_seconds( time_elapsed_text ) );
466                  if( time_elapsed_text.length )
467                  {
468                    indexing_text = 'Indexing since ' + time_elapsed_text
469                  }
470
471                  $( '.info strong', state_element )
472                    .text( indexing_text );
473                                   
474                  dataimport_compute_details( response, $( '.info .details', state_element ) );
475
476                  window.setTimeout( dataimport_fetch_status, 2000 );
477                }
478                else
479                {
480                  state_element.hide();
481                }
482              },
483              error : function( xhr, text_status, error_thrown )
484              {
485                console.debug( arguments );
486
487                reload_config_element
488                  .addClass( 'error' );
489              },
490              complete : function( xhr, text_status )
491              {
492              }
493            }
494          );
495        }
496        dataimport_fetch_status();
497
498        // form
499
500        $( 'form', form_element )
501          .ajaxForm
502          (
503            {
504              url : handler_url,
505              dataType : 'xml',
506              beforeSend : function( xhr, settings )
507              {
508                $( 'form button', form_element )
509                  .addClass( 'loader' );
510              },
511              beforeSubmit : function( array, form, options )
512              {
513                var entity = $( '#entity', form ).val();
514                if( entity.length )
515                {
516                  array.push( { name : 'entity', value: entity } );
517                }
518
519                var start = parseInt( $( '#start', form ).val(), 10 );
520                if( start )
521                {
522                  array.push( { name : 'start', value: start } );
523                }
524
525                var rows = parseInt( $( '#rows', form ).val(), 10 );
526                if( rows )
527                {
528                  array.push( { name : 'rows', value: rows } );
529                }
530
531                var custom_parameters = $( '#custom_parameters', form ).val();
532                if( custom_parameters.length )
533                {
534                  array.push( { name : 'custom_parameters', value: custom_parameters } );
535                }
536              },
537              success : function( response, text_status, xhr )
538              {
539                dataimport_fetch_status();
540              },
541              error : function( xhr, text_status, error_thrown )
542              {
543                console.debug( arguments );
544              },
545              complete : function( xhr, text_status )
546              {
547                $( 'form button', form_element )
548                  .removeClass( 'loader' );
549              }
550            }
551          );
552      }
553    );
554  }
555);
Note: See TracBrowser for help on using the repository browser.