Skip to content

Commit

Permalink
Populate adhoc dataview to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed Aug 12, 2022
1 parent b5ee47e commit e8657ee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
20 changes: 19 additions & 1 deletion x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ import { LensAttributeService } from '../lens_attribute_service';
import type { ErrorMessage, TableInspectorAdapter } from '../editor_frame_service/types';
import { getLensInspectorService, LensInspector } from '../lens_inspector_service';
import { SharingSavedObjectProps, VisualizationDisplayOptions } from '../types';
import { getActiveDatasourceIdFromDoc, getIndexPatternsObjects, inferTimeField } from '../utils';
import {
getActiveDatasourceIdFromDoc,
getIndexPatternsObjects,
inferTimeField,
getAdHocIndexSpecs,
} from '../utils';
import { getLayerMetaInfo, combineQueryAndFilters } from '../app_plugin/show_underlying_data';
import { convertDataViewIntoLensIndexPattern } from '../indexpattern_service/loader';

Expand Down Expand Up @@ -840,6 +845,19 @@ export class Embeddable
this.savedVis?.references.map(({ id }) => id) || [],
this.deps.dataViews
);
const activeDatasourceId = getActiveDatasourceIdFromDoc(this.savedVis);
if (activeDatasourceId) {
const activeDatasource = this.deps.datasourceMap[activeDatasourceId];
const dataSourceState = this.savedVis?.state.datasourceStates[activeDatasourceId];
const { adHocDataviews } = await getAdHocIndexSpecs(
activeDatasource,
dataSourceState,
this.deps.dataViews
);
if (adHocDataviews.length) {
indexPatterns.push(...adHocDataviews);
}
}

this.indexPatterns = uniqBy(indexPatterns, 'id');

Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugins/lens/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ export async function getIndexPatternsObjects(
return { indexPatterns: fullfilled.map((response) => response.value), rejectedIds };
}

export async function getAdHocIndexSpecs(
dataSource: Datasource,
dataSourceState: unknown,
dataViews: DataViewsContract
): Promise<{ adHocDataviews: DataView[] }> {
const adHocIndexPatterns = dataSource?.getAdHocIndexSpecs?.(dataSourceState);

const adHocDataviews: DataView[] = [];

if (adHocIndexPatterns) {
const adHocSpecs = Object.values(adHocIndexPatterns);
if (adHocSpecs?.length) {
for (const addHocDataView of adHocSpecs) {
const d = await dataViews.create(addHocDataView);
adHocDataviews.push(d);
}
}
}
return { adHocDataviews };
}

export function getRemoveOperation(
activeVisualization: Visualization,
visualizationState: VisualizationState['state'],
Expand Down

0 comments on commit e8657ee

Please sign in to comment.