@finos/legend-extension-dsl-data-space
Version:
Legend extension for Data Space DSL
220 lines • 9.95 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {} from '@finos/legend-application';
import { QueryBuilderState, QUERY_BUILDER_LAMBDA_WRITER_MODE, } from '@finos/legend-query-builder';
import { getMappingCompatibleClasses, Package, QueryDataSpaceExecutionContext, elementBelongsToPackage, } from '@finos/legend-graph';
import { ActionState, filterByType, uniq, } from '@finos/legend-shared';
import { action, computed, flow, makeObservable, observable } from 'mobx';
import { renderDataSpaceQueryBuilderSetupPanelContent, } from '../../components/query-builder/DataSpaceQueryBuilder.js';
import { DataSpace, } from '../../graph/metamodel/pure/model/packageableElements/dataSpace/DSL_DataSpace_DataSpace.js';
import {} from '../../graph-manager/action/analytics/DataSpaceAnalysis.js';
import { ResolvedDataSpaceEntityWithOrigin } from '../shared/DataSpaceInfo.js';
import { buildDataSpaceExecutableAnalysisResultFromExecutable } from '../../graph-manager/action/analytics/DataSpaceAnalysisHelper.js';
import { compareLabelFn } from '@finos/legend-art';
const matchesDataElement = (_class, element) => {
if (_class === element) {
return true;
}
if (element instanceof Package) {
return elementBelongsToPackage(_class, element);
}
return false;
};
export const resolveUsableDataSpaceClasses = (dataSpace, mapping, graphManagerState, queryBuilderState) => {
let compatibleClasses = getMappingCompatibleClasses(mapping, graphManagerState.usableClasses);
const mappingModelCoverageAnalysisResult = queryBuilderState?.dataSpaceAnalysisResult?.mappingToMappingCoverageResult?.get(mapping.path);
if (
// This check is to make sure that we have `info` field present in `MappedEntity` which
// contains information about the mapped class path
mappingModelCoverageAnalysisResult?.mappedEntities.some((m) => m.info !== undefined)) {
const uniqueCompatibleClasses = uniq(mappingModelCoverageAnalysisResult.mappedEntities
// is root entity filters out mapped classes
.filter((e) => e.info?.isRootEntity)
.map((e) => e.info?.classPath));
compatibleClasses = graphManagerState.graph.classes.filter((c) => uniqueCompatibleClasses.includes(c.path));
}
if (dataSpace.elements?.length) {
const elements = dataSpace.elements;
return compatibleClasses.filter((_class) => {
const _classElements = elements
.filter((e) => matchesDataElement(_class, e.element.value))
// we sort because we respect the closest definition to the element.
.sort((a, b) => b.element.value.path.length - a.element.value.path.length);
if (!_classElements.length) {
return false;
}
return !_classElements[0]?.exclude;
});
}
return compatibleClasses;
};
export const buildDataSpaceOption = (value) => ({
label: value.title ?? value.name,
value,
});
export class DataSpaceQueryBuilderState extends QueryBuilderState {
onDataSpaceChange;
onExecutionContextChange;
onRuntimeChange;
onClassChange;
dataSpaceAnalysisResult;
extraOptionsConfig;
entities;
TEMPORARY__setupPanelContentRenderer = () => renderDataSpaceQueryBuilderSetupPanelContent(this);
dataSpace;
executionContext;
showRuntimeSelector = false;
isTemplateQueryDialogOpen = false;
isLightGraphEnabled;
displayedTemplateQueries;
advancedSearchState;
loadEntitiesState = ActionState.create();
prioritizeEntityFunc;
constructor(applicationStore, graphManagerState, workflow, actionConfig, dataSpace, executionContext, isLightGraphEnabled, prioritizeEntityFunc, onDataSpaceChange, dataSpaceAnalysisResult, onExecutionContextChange, onRuntimeChange, onClassChange, config, sourceInfo, extraOptionsConfig) {
super(applicationStore, graphManagerState, workflow, config, sourceInfo);
makeObservable(this, {
executionContext: observable,
showRuntimeSelector: observable,
isTemplateQueryDialogOpen: observable,
isLightGraphEnabled: observable,
displayedTemplateQueries: observable,
advancedSearchState: observable,
selectedDataSpaceOption: computed,
setExecutionContext: action,
setShowRuntimeSelector: action,
setTemplateQueryDialogOpen: action,
setIsLightGraphEnabled: action,
intialize: flow,
loadEntities: flow,
entities: observable,
});
this.dataSpace = dataSpace;
this.executionContext = executionContext;
this.onDataSpaceChange = onDataSpaceChange;
this.onExecutionContextChange = onExecutionContextChange;
this.onRuntimeChange = onRuntimeChange;
this.onClassChange = onClassChange;
this.dataSpaceAnalysisResult = dataSpaceAnalysisResult;
this.workflowState.updateActionConfig(actionConfig);
this.isLightGraphEnabled = isLightGraphEnabled;
this.prioritizeEntityFunc = prioritizeEntityFunc;
this.extraOptionsConfig = extraOptionsConfig;
if (dataSpaceAnalysisResult?.__INTERNAL__useRelationTDS) {
this.setLambdaWriteMode(QUERY_BUILDER_LAMBDA_WRITER_MODE.TYPED_FETCH_STRUCTURE);
}
}
get dataSpaceOptions() {
const sortedAllOptions = (this.entities ?? [])
.map(buildDataSpaceOption)
.sort(compareLabelFn);
return this.prioritizeEntityFunc
? [
...sortedAllOptions.filter((val) => this.prioritizeEntityFunc?.(val.value)),
...sortedAllOptions.filter((val) => !this.prioritizeEntityFunc?.(val.value)),
]
: sortedAllOptions;
}
get sideBarClassName() {
return this.showRuntimeSelector
? 'query-builder__setup__data-space--with-runtime'
: 'query-builder__setup__data-space';
}
getQueryExecutionContext() {
const queryExeContext = new QueryDataSpaceExecutionContext();
queryExeContext.dataSpacePath = this.dataSpace.path;
queryExeContext.executionKey = this.executionContext.name;
return queryExeContext;
}
get isAdvancedDataSpaceSearchEnabled() {
return false;
}
get isDataSpaceLinkable() {
return false;
}
copyDataSpaceLinkToClipboard() {
if (!this.isDataSpaceLinkable) {
this.applicationStore.notificationService.notifyError('Data space link is not available.');
}
}
get selectedDataSpaceOption() {
return {
label: this.dataSpace.title ?? this.dataSpace.name,
value: new ResolvedDataSpaceEntityWithOrigin(undefined, this.dataSpace.title, this.dataSpace.name, this.dataSpace.path, this.dataSpace.defaultExecutionContext.title ??
this.dataSpace.defaultExecutionContext.name),
};
}
get canVisitTemplateQuery() {
return false;
}
getElementType() {
return DataSpace;
}
*loadEntities() {
this.loadEntitiesState.inProgress();
this.entities = this.graphManagerState.graph.allOwnElements
.filter(filterByType(this.getElementType()))
.map((element) => this.transformElement(element));
this.loadEntitiesState.complete();
}
transformElement(element) {
return new ResolvedDataSpaceEntityWithOrigin(undefined, element.title, element.name, element.path, element.defaultExecutionContext.title ??
element.defaultExecutionContext.name);
}
setTemplateQueryDialogOpen(val) {
this.isTemplateQueryDialogOpen = val;
}
setExecutionContext(val) {
this.executionContext = val;
}
setShowRuntimeSelector(val) {
this.showRuntimeSelector = val;
}
setIsLightGraphEnabled(val) {
this.isLightGraphEnabled = val;
}
buildFunctionAnalysisInfo() {
let functionInfoMap = new Map();
let dependencyFunctionInfoMap = new Map();
const functionInfos = this.dataSpaceAnalysisResult?.functionInfos;
if (functionInfos) {
functionInfoMap = functionInfos;
}
const dependencyFunctionInfos = this.dataSpaceAnalysisResult?.dependencyFunctionInfos;
if (dependencyFunctionInfos) {
dependencyFunctionInfoMap = dependencyFunctionInfos;
}
return {
functionInfoMap,
dependencyFunctionInfoMap,
};
}
*intialize() {
this.displayedTemplateQueries =
this.dataSpace.executables && this.dataSpace.executables.length > 0
? (yield buildDataSpaceExecutableAnalysisResultFromExecutable(this.dataSpace, this.dataSpace.executables, this.graphManagerState))
: this.dataSpaceAnalysisResult?.executables.filter((ex) => ex.info !== undefined);
}
visitTemplateQuery(dataSpace, template) {
this.applicationStore.notificationService.notifyError('Visiting template query is not supported yet.');
}
hideAdvancedSearchPanel() {
this.advancedSearchState = undefined;
}
showAdvancedSearchPanel(dataSpace) {
this.applicationStore.notificationService.notifyError('Advanced search panel not supported.');
}
}
//# sourceMappingURL=DataSpaceQueryBuilderState.js.map