import { assign , cloneDeep } from 'lodash' ; import { SavedObjectsClientContract } from 'kibana/public' ; import { EmbeddableStart } from '../services/embeddable' ; import { SavedObject , SavedObjectsStart } from '../services/saved_objects' ; import { Filter , ISearchSource , Query , RefreshInterval } from '../services/data' ; import { createDashboardEditUrl } from '../dashboard_constants' ; import { extractReferences , injectReferences } from '../../common/saved_dashboard_references' ; import { SavedObjectAttributes , SavedObjectReference } from '../../../../core/types' ; import { DashboardOptions } from '../types' ; import { ControlStyle } from '../../../controls/public' ; export interface DashboardSavedObject extends SavedObject { id ? : string ; timeRestore : boolean ; timeTo ? : string ; timeFrom ? : string ; description ? : string ; panelsJSON : string ; optionsJSON ? : string ; uiStateJSON ? : string ; lastSavedTitle : string ; refreshInterval ? : RefreshInterval ; searchSource : ISearchSource ; getQuery ( ) : Query ; getFilters ( ) : Filter [ ] ; getFullEditPath : ( editMode ? : boolean ) => string ; outcome ? : string ; aliasId ? : string ; controlGroupInput ? : { controlStyle ? : ControlStyle ; panelsJSON ? : string ; } ; } const defaults = { title : '' , hits : 0 , description : '' , panelsJSON : '[]' , optionsJSON : JSON . stringify ( { useMargins : true , syncColors : false , hidePanelTitles : false , } as DashboardOptions ) , version : 1 , timeRestore : false , timeTo : undefined , timeFrom : undefined , refreshInterval : undefined , } ; export function createSavedDashboardClass ( savedObjectStart , embeddableStart , savedObjectsClient ) { class SavedDashboard extends savedObjectStart . SavedObjectClass { public static type = 'dashboard' ; public static mapping = { title : 'text' , hits : 'integer' , description : 'text' , panelsJSON : 'text' , optionsJSON : 'text' , version : 'integer' , timeRestore : 'boolean' , timeTo : 'keyword' , timeFrom : 'keyword' , refreshInterval : { type : 'object' , properties : { display : { type : 'keyword' } , pause : { type : 'boolean' } , section : { type : 'integer' } , value : { type : 'integer' } , } , } , controlGroupInput : { type : 'object' , properties : { controlStyle : { type : 'keyword' } , panelsJSON : { type : 'text' } , } , } , } ; public static fieldOrder = [ 'title' , 'description' ] ; public static searchSource = true ; public showInRecentlyAccessed = true ; public outcome ? : string ; public aliasId ? : string ; constructor ( arg ) { super ( { type : SavedDashboard . type , mapping : SavedDashboard . mapping , searchSource : SavedDashboard . searchSource , extractReferences : ( opts ) => extractReferences ( opts , { embeddablePersistableStateService : embeddableStart } ) , injectReferences : ( so , references ) => { const newAttributes = injectReferences ( { attributes : so . _serialize ( ) . attributes , references } , { embeddablePersistableStateService : embeddableStart , } ) ; Object . assign ( so , newAttributes ) ; } , id : typeof arg === 'object' ? arg . id : arg , defaults , } ) ; const id = typeof arg === 'object' ? arg . id : arg ; const useResolve = typeof arg === 'object' ? arg . useResolve : false ; this . getFullPath = ( ) => `/app/dashboards#${ createDashboardEditUrl ( this . aliasId || this . id ) }` ; if ( useResolve || true ) { this . init = async ( ) => { const esType = SavedDashboard . type ; if ( ! esType ) throw new Error ( 'You must define a type name to use SavedObject objects.' ) ; if ( ! id ) { assign ( this , defaults ) ; await this . hydrateIndexPattern ! ( ) ; return this ; } const { outcome , alias_target_id : aliasId , saved_object : resp , } = await savedObjectsClient . resolve ( esType , id ) ; const respMapped = { _id : resp . id , _type : resp . type , _source : cloneDeep ( resp . attributes ) , references : resp . references , found : ! ! resp . _version , } ; this . outcome = outcome ; this . aliasId = aliasId ; await this . applyESResp ( respMapped ) ; return this ; } ; } } getQuery ( ) { return this . searchSource ! . getOwnField ( 'query' ) || { query : '' , language : 'kuery' } ; } getFilters ( ) { return this . searchSource ! . getOwnField ( 'filter' ) || [ ] ; } getFullEditPath = ( editMode ? ) => { return `/app/dashboards#${ createDashboardEditUrl ( this . id , editMode ) }` ; } ; } return SavedDashboard as unknown as new ( id ) => DashboardSavedObject ; }