const getInVis = require ( '../util/immutable-utils' ) . getInVis ; import { Dispatch } from 'redux' ; import { createStandardAction } from 'typesafe-actions' ; import { Datum } from 'vega-typings/types' ; import { State } from '../store' ; import { Dataset , DatasetRecord , Schema } from '../store/factory/Dataset' ; import { LyraAggregateTransform , PipelineRecord , Pipeline } from '../store/factory/Pipeline' ; import * as dsUtil from '../util/dataset-utils' ; import { addDataset } from './datasetActions' ; import { selectPipeline } from './inspectorActions' ; import { assignId } from '../util/counter' ; import { ThunkDispatch } from 'redux-thunk' ; param { Object } pipeline param { Object } ds param { Array } values returns { Function } export function addPipeline ( pipeline , ds , values ) { return function ( dispatch , getState ) { const pid = pipeline . _id || assignId ( dispatch , getState ( ) ) ; const dsId = assignId ( dispatch , getState ( ) ) ; const newDs = ds . merge ( { _id : dsId , name : `${ pipeline . name }_source_${ dsId }` , _parent : pid } ) ; dispatch ( addDataset ( newDs , values ) ) ; dispatch ( baseAddPipeline ( pipeline . merge ( { _id : pid , _source : dsId } ) , pid ) ) ; dispatch ( selectPipeline ( pid ) ) ; } ; } param { Object } pipelineId param { Object } dsId returns { Function } export function derivePipeline ( pipelineId ) { return function ( dispatch , getState ) { const state = getState ( ) ; const pipeline = state . getIn ( [ 'vis' , 'present' , 'pipelines' , String ( pipelineId ) ] ) ; const srcId = pipeline . _source ; const srcSchema = state . getIn ( [ 'vis' , 'present' , 'datasets' , String ( srcId ) , '_schema' ] ) ; const dsId = assignId ( dispatch , getState ( ) ) ; const pid = assignId ( dispatch , getState ( ) ) ; const ds = Dataset ( { _id : dsId , name : pipeline . get ( 'name' ) + '_derived_' + dsId , source : String ( srcId ) , transform : [ ] , _schema : srcSchema , _parent : pid } ) ; const newPipeline = Pipeline ( { _id : pid , name : `${ pipeline . name }_derived` , _source : dsId } ) ; dispatch ( baseAddPipeline ( newPipeline , pid ) ) ; dispatch ( addDataset ( ds , null ) ) ; dispatch ( selectPipeline ( pid ) ) ; } ; } param { number } id param { Object } aggregate returns { Function } export function aggregatePipeline ( id , aggregate ) { return function ( dispatch , getState ) { const state = getState ( ) ; const pipeline = state . getIn ( [ 'vis' , 'present' , 'pipelines' , String ( id ) ] ) ; const srcId = pipeline . _source ; const srcSchema = state . getIn ( [ 'vis' , 'present' , 'datasets' , String ( srcId ) , '_schema' ] ) ; const schema = dsUtil . aggregateSchema ( srcSchema , aggregate ) ; const key = ( aggregate . groupby as string [ ] ) . join ( '|' ) ; const partialAggregateId = pipeline . _aggregates . get ( "" ) ; if ( partialAggregateId ) { const ds = Dataset ( { _id : partialAggregateId , name : pipeline . get ( 'name' ) + '_groupby_' + key , source : String ( srcId ) , transform : [ aggregate ] , _schema : schema , _parent : id } ) ; dispatch ( addDataset ( ds , null ) ) ; dispatch ( baseAggregatePipeline ( { dsId : undefined , key : "" } , id ) ) ; dispatch ( baseAggregatePipeline ( { dsId : aggregate . _id = partialAggregateId , key : key } , id ) ) ; } else { const dsId = assignId ( dispatch , getState ( ) ) ; const ds = Dataset ( { _id : dsId , name : pipeline . get ( 'name' ) + '_groupby_' + key , source : String ( srcId ) , transform : [ aggregate ] , _schema : schema , _parent : id } ) ; dispatch ( addDataset ( ds , null ) ) ; dispatch ( baseAggregatePipeline ( { dsId : aggregate . _id = dsId , key : key } , id ) ) ; } } ; } export const baseAddPipeline = createStandardAction ( 'ADD_PIPELINE' ) < PipelineRecord , number > ( ) ; export const baseAggregatePipeline = createStandardAction ( 'AGGREGATE_PIPELINE' ) < { dsId : number ; key : string ; } , number > ( ) ; export const updatePipelineProperty = createStandardAction ( 'UPDATE_PIPELINE_PROPERTY' ) < { property : string ; value : any ; } , number > ( ) ;