import { get } from 'lodash' ; import { SavedObjectUnsanitizedDoc } from 'kibana/server' ; export const graphMigrations = { '7.0.0' : ( doc ) => { doc . references = doc . references || [ ] ; const wsState = get ( doc , 'attributes.wsState' ) ; if ( typeof wsState !== 'string' ) { return doc ; } let state ; try { state = JSON . parse ( JSON . parse ( wsState ) ) ; } catch ( e ) { return doc ; } const { indexPattern } = state ; if ( ! indexPattern ) { return doc ; } state . indexPatternRefName = 'indexPattern_0' ; delete state . indexPattern ; doc . attributes . wsState = JSON . stringify ( JSON . stringify ( state ) ) ; doc . references . push ( { name : 'indexPattern_0' , type : 'index-pattern' , id : indexPattern , } ) ; return doc ; } , '7.10.0' : ( doc ) => { const wsState = get ( doc , 'attributes.wsState' ) ; if ( typeof wsState !== 'string' ) { return doc ; } let state ; try { state = JSON . parse ( JSON . parse ( wsState ) ) ; } catch ( e ) { return doc ; } if ( state . blacklist ) { state . blocklist = state . blacklist ; delete state . blacklist ; } doc . attributes . wsState = JSON . stringify ( JSON . stringify ( state ) ) ; return doc ; } , '7.11.0' : ( doc ) => { const wsState = get ( doc , 'attributes.wsState' ) ; if ( typeof wsState !== 'string' ) { return doc ; } let state ; try { state = JSON . parse ( JSON . parse ( wsState ) ) ; } catch ( e ) { return doc ; } const indexPatternRefName = state . indexPatternRefName ; const indexPatternReference = doc . references ?. find ( ( reference ) => reference . name === indexPatternRefName ) ; if ( ! indexPatternReference ) { return doc ; } const indexPatternTitle = indexPatternReference . id ; delete state . indexPatternRefName ; doc . attributes . legacyIndexPatternRef = indexPatternTitle ; doc . attributes . wsState = JSON . stringify ( JSON . stringify ( state ) ) ; doc . references = [ ] ; return doc ; } , } ;