2017-11-13 15:36:31 +00:00
import $ from 'jquery' ;
2018-01-08 15:28:03 +00:00
const ownLoader = require ( './loader/loader-import' ) ;
2017-11-13 15:36:31 +00:00
var app = require ( 'ui/modules' ) . get ( 'apps/webinar_app' , [ ] )
. directive ( 'kbnVis' , [ function ( ) {
return {
restrict : 'E' ,
scope : {
visID : '=visId' ,
2017-12-26 17:22:00 +00:00
specificTimeRange : '=specificTimeRange'
2017-11-13 15:36:31 +00:00
} ,
2018-02-20 11:35:46 +00:00
controller : function VisController ( $scope , $rootScope , $location , savedVisualizations ) {
2018-01-08 15:28:03 +00:00
if ( ! $rootScope . ownHandlers ) $rootScope . ownHandlers = [ ] ;
let implicitFilter = '' ;
let visTitle = '' ;
let fullFilter = '' ;
let rendered = false ;
let visualization = null ;
let visHandler = null ;
2018-02-09 19:35:28 +00:00
let renderInProgress = false ;
2017-11-17 08:58:16 +00:00
2018-02-09 19:35:28 +00:00
const myRender = function ( ) {
2018-02-13 10:30:00 +00:00
if ( ( $rootScope . discoverPendingUpdates && $rootScope . discoverPendingUpdates . length != 0 ) || $scope . visID . includes ( 'Ruleset' ) ) { // There are pending updates from the discover (which is the one who owns the true app state)
2018-01-16 23:17:48 +00:00
2018-02-09 19:35:28 +00:00
if ( ! visualization && ! rendered && ! renderInProgress ) { // There's no visualization object -> create it with proper filters
renderInProgress = true ;
savedVisualizations . get ( $scope . visID ) . then ( savedObj => {
implicitFilter = savedObj . searchSource . get ( 'query' ) [ 'query' ] ;
visTitle = savedObj . vis . title ;
visualization = savedObj ;
2018-02-13 10:30:00 +00:00
if ( $rootScope . discoverPendingUpdates && implicitFilter && $rootScope . discoverPendingUpdates [ 0 ] . query ) {
2018-02-09 19:35:28 +00:00
implicitFilter += ' AND ' + $rootScope . discoverPendingUpdates [ 0 ] . query ;
2018-02-13 10:30:00 +00:00
} else if ( $rootScope . discoverPendingUpdates && ! implicitFilter && $rootScope . discoverPendingUpdates [ 0 ] . query ) {
2018-02-09 19:35:28 +00:00
implicitFilter = $rootScope . discoverPendingUpdates [ 0 ] . query ;
2017-11-28 16:37:31 +00:00
}
2018-02-09 19:35:28 +00:00
if ( visTitle !== 'Wazuh App Overview General Agents status' ) { // We don't want to filter that visualization as it uses another index-pattern
visualization . searchSource
. query ( { language : 'lucene' , query : implicitFilter } )
2018-02-13 10:30:00 +00:00
. set ( 'filter' , $rootScope . discoverPendingUpdates ? $rootScope . discoverPendingUpdates [ 1 ] : { } ) ;
2017-11-28 16:37:31 +00:00
}
2017-11-29 15:06:13 +00:00
2018-02-09 19:35:28 +00:00
let params = { } ;
2018-01-16 23:17:48 +00:00
2018-02-09 19:35:28 +00:00
if ( $scope . specificTimeRange ) {
const timeRange = {
from : 'now-1d/d' ,
to : 'now'
} ;
params = { timeRange : timeRange }
}
2018-02-23 13:49:57 +00:00
$ ( ` [vis-id="' ${ $scope . visID } '"] ` ) . on ( 'renderStart' , ( ) => {
//$("#"+$scope.visID).on('renderStart', () => {
2018-02-20 12:47:04 +00:00
// TBD: Use renderStart to couple it with renderComplete?
2018-02-09 19:35:28 +00:00
} ) ;
2018-02-23 13:49:57 +00:00
visHandler = loader . embedVisualizationWithSavedObject ( $ ( ` [vis-id="' ${ $scope . visID } '"] ` ) , visualization , params ) ;
2018-02-09 19:35:28 +00:00
$rootScope . ownHandlers . push ( visHandler ) ;
visHandler . addRenderCompleteListener ( renderComplete ) ;
} ) ;
2018-01-17 15:13:14 +00:00
2018-02-09 19:35:28 +00:00
} else if ( rendered ) { // There's a visualization object -> just update its filters
2018-02-13 10:30:00 +00:00
if ( $rootScope . discoverPendingUpdates && implicitFilter && $rootScope . discoverPendingUpdates [ 0 ] . query ) {
2018-02-09 19:35:28 +00:00
implicitFilter += ' AND ' + $rootScope . discoverPendingUpdates [ 0 ] . query ;
2018-02-13 10:30:00 +00:00
} else if ( $rootScope . discoverPendingUpdates && ! implicitFilter && $rootScope . discoverPendingUpdates [ 0 ] . query ) {
2018-02-09 19:35:28 +00:00
implicitFilter = $rootScope . discoverPendingUpdates [ 0 ] . query ;
2018-01-17 15:13:14 +00:00
}
2018-02-09 19:35:28 +00:00
if ( visTitle !== 'Wazuh App Overview General Agents status' ) { // We don't want to filter that visualization as it uses another index-pattern
2018-01-17 16:27:23 +00:00
visualization . searchSource
. query ( { language : 'lucene' , query : implicitFilter } )
2018-02-13 10:30:00 +00:00
. set ( 'filter' , $rootScope . discoverPendingUpdates ? $rootScope . discoverPendingUpdates [ 1 ] : { } ) ;
2018-01-17 15:13:14 +00:00
}
}
2018-01-16 23:17:48 +00:00
}
2018-02-09 19:35:28 +00:00
} ;
// Listen for changes
$scope . $on ( 'updateVis' , function ( event , query , filters ) {
myRender ( ) ;
} ) ;
var renderComplete = function ( ) {
rendered = true ;
2018-01-16 23:17:48 +00:00
2018-01-29 11:38:16 +00:00
if ( typeof $rootScope . loadedVisualizations === 'undefined' ) $rootScope . loadedVisualizations = [ ] ;
2018-01-10 14:39:15 +00:00
$rootScope . loadedVisualizations . push ( true ) ;
2018-02-07 10:54:18 +00:00
let currentCompleted = Math . round ( ( $rootScope . loadedVisualizations . length / $rootScope . tabVisualizations [ $location . search ( ) . tab ] ) * 100 ) ;
2018-02-01 15:34:00 +00:00
$rootScope . loadingStatus = ` Rendering visualizations... ${ currentCompleted > 100 ? 100 : currentCompleted } % ` ;
if ( currentCompleted >= 100 ) {
2018-01-12 16:36:24 +00:00
if ( ! visTitle !== 'Wazuh App Overview General Agents status' ) $rootScope . rendered = true ;
2018-01-10 14:39:15 +00:00
// Forcing a digest cycle
2018-01-17 10:44:43 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
2018-01-10 14:39:15 +00:00
}
2018-01-12 16:36:24 +00:00
else if ( ! visTitle !== 'Wazuh App Overview General Agents status' ) $rootScope . rendered = false ;
2018-01-09 18:53:42 +00:00
} ;
2018-01-08 15:28:03 +00:00
// Initializing the visualization
const loader = ownLoader . getVisualizeLoader ( ) ;
2017-12-26 17:22:00 +00:00
2018-02-09 19:35:28 +00:00
myRender ( ) ;
2018-01-08 15:28:03 +00:00
2017-10-09 15:49:10 +00:00
}
2017-11-13 15:36:31 +00:00
}
2018-01-08 15:28:03 +00:00
} ] ) ;