import { IMineMeldAPIService } from './minemeldapi' ; export interface IMinemeldCandidateConfigInfo { fabric : boolean ; mgmtbus : boolean ; next_node_id : number ; version : string ; changed : boolean ; } export interface IMinemeldCandidateConfigNode { name : string ; properties : any ; version : string ; deleted ? : boolean ; } export interface IMinemeldConfigNode { prototype ? : string ; inputs ? : string [ ] ; output ? : boolean ; config ? : Object ; } export interface IMinemeldConfig { nodes : { [ id ] : IMinemeldConfigNode ; } ; } export interface IMinemeldConfigService { configInfo : IMinemeldCandidateConfigInfo ; nodesConfig : IMinemeldCandidateConfigNode [ ] ; changed : boolean ; runningConfig ( cancellable ? ) : angular . IPromise < IMinemeldConfig > ; committedConfig ( ) : angular . IPromise < IMinemeldConfig > ; candidateConfig ( ) : angular . IPromise < IMinemeldCandidateConfigNode [ ] > ; refresh ( ) : angular . IPromise < any > ; reload ( config ? ) : angular . IPromise < any > ; saveNodeConfig ( noden ) : angular . IPromise < any > ; deleteNode ( noden ) : angular . IPromise < any > ; commit ( ) : angular . IPromise < any > ; addNode ( name , properties ) : angular . IPromise < any > ; getDataFile ( datafilename , dtype ? ) : angular . IPromise < any > ; saveDataFile ( datafilename , data , hup ? , dtype ? ) : angular . IPromise < any > ; appendDataFile ( datafilename , data , hup ? , dtype ? ) : angular . IPromise < any > ; } interface IMinemeldConfigResource extends angular . resource . IResourceClass < angular . resource . IResource < any > > { put ( params , nodeconfig ) ; del ( params ) ; post ( params , postdata ) ; } export class MinemeldConfigService implements IMinemeldConfigService { deletedNode : IMinemeldCandidateConfigNode = { 'name' : '' , 'properties' : { } , 'deleted' : true , 'version' : '' } ; configInfo : IMinemeldCandidateConfigInfo ; nodesConfig : IMinemeldCandidateConfigNode [ ] ; changed : boolean ; $state : angular . ui . IStateService ; $q : angular . IQService ; MineMeldAPIService : IMineMeldAPIService ; ngInject constructor ( $state , $q , MineMeldAPIService ) { this . $state = $state ; this . $q = $q ; this . MineMeldAPIService = MineMeldAPIService ; this . MineMeldAPIService . onLogout ( this . emptyCache . bind ( this ) ) ; } runningConfig ( cancellable ? ) { var r ; if ( typeof cancellable === 'undefined' ) { cancellable = true ; } r = this . MineMeldAPIService . getAPIResource ( '/config/running' , { } , { get : { method : 'GET' } } , cancellable ) ; return r . get ( ) . $promise . then ( ( result ) => { return result . result ; } ) ; } committedConfig ( ) { var r ; r = this . MineMeldAPIService . getAPIResource ( '/config/committed' , { } , { get : { method : 'GET' } } ) ; return r . get ( ) . $promise . then ( ( result ) => { return result . result ; } ) ; } candidateConfig ( ) { if ( this . nodesConfig ) { return this . $q . when ( this . nodesConfig ) ; } return this . refresh ( ) ; } refresh ( ) { var r ; r = this . MineMeldAPIService . getAPIResource ( '/config/full' , { } , { get : { method : 'GET' } } ) ; return r . get ( ) . $promise . then ( ( result ) => { var nodesConfig ; nodesConfig = result . result . nodes . map ( ( x ) => { if ( x === null ) { return this . deletedNode ; } return x ; } ) ; delete result . result . nodes ; this . configInfo = result . result ; this . changed = this . configInfo . changed ; this . nodesConfig = nodesConfig ; return this . nodesConfig ; } ) ; } reload ( config ? ) { var r ; var params = { } ; r = this . MineMeldAPIService . getAPIResource ( '/config/reload' , { } , { get : { method : 'GET' } } , false ) ; if ( config ) { params . c = config ; } return r . get ( params ) . $promise . then ( ( result ) => { return this . refresh ( ) ; } ) ; } commit ( ) { var r ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/commit' , { } , { post : { method : 'POST' } } , false ) ) ; return r . post ( { } , JSON . stringify ( { version : this . configInfo . version } ) ) . $promise ; } saveNodeConfig ( noden ) { var r ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/node/:noden' , { noden : noden } , { put : { method : 'PUT' } } , false ) ) ; return r . put ( { } , JSON . stringify ( this . nodesConfig [ noden ] ) ) . $promise . then ( ( result ) => { return result . result ; } ) . then ( ( result ) => { this . nodesConfig [ noden ] . version = result ; this . changed = true ; } ) ; } addNode ( name , properties ) { var r ; var config ; config = { name : name , properties : properties , version : this . configInfo . version } ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/node' , { } , { post : { method : 'POST' } } , false ) ) ; return r . post ( { } , JSON . stringify ( config ) ) . $promise . then ( ( result ) => { return result . result ; } ) ; } deleteNode ( noden ) { var r ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/node/:noden' , { noden : noden } , { del : { method : 'DELETE' } } , false ) ) ; return r . del ( { version : this . nodesConfig [ noden ] . version } ) . $promise . then ( ( result ) => { return result . result ; } ) . then ( ( result ) => { this . changed = true ; } ) ; } getDataFile ( datafilename , dtype ? ) { var r ; var params = { } ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/data/:datafilename' , { datafilename : datafilename } , { get : { method : 'GET' } } ) ) ; if ( dtype ) { params . t = dtype ; } return r . get ( params ) . $promise . then ( ( result ) => { return result . result ; } , ( error ) => { if ( error . status === 400 ) { return null ; } throw error ; } ) ; } saveDataFile ( datafilename , data , hup ? ) { var r ; var params = { } ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/data/:datafilename' , { datafilename : datafilename } , { put : { method : 'PUT' } } , false ) ) ; if ( hup ) { params . h = hup ; } return r . put ( params , JSON . stringify ( data ) ) . $promise . then ( ( result ) => { return result . result ; } ) ; } appendDataFile ( datafilename , data , hup ? , dtype ? ) { var r ; var params = { } ; r = < IMinemeldConfigResource > ( this . MineMeldAPIService . getAPIResource ( '/config/data/:datafilename/append' , { datafilename : datafilename } , { post : { method : 'POST' } } , false ) ) ; if ( hup ) { params . h = hup ; } if ( dtype ) { params . t = dtype ; } return r . post ( params , JSON . stringify ( data ) ) . $promise . then ( ( result ) => { return result . result ; } ) ; } private emptyCache ( ) { this . nodesConfig = undefined ; this . configInfo = undefined ; this . changed = undefined ; } }