2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
import './app.scss';
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
import _ from 'lodash';
|
2021-02-04 11:37:50 +01:00
|
|
|
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { i18n } from '@kbn/i18n';
|
2021-02-04 11:37:50 +01:00
|
|
|
import { NotificationsStart, Toast } from 'kibana/public';
|
|
|
|
import { VisualizeFieldContext } from 'src/plugins/ui_actions/public';
|
|
|
|
import { Datatable } from 'src/plugins/expressions/public';
|
2020-08-21 16:18:14 +02:00
|
|
|
import { EuiBreadcrumb } from '@elastic/eui';
|
2020-11-24 12:37:27 +01:00
|
|
|
import { downloadMultipleAs } from '../../../../../src/plugins/share/public';
|
2020-06-10 15:44:59 +02:00
|
|
|
import {
|
|
|
|
createKbnUrlStateStorage,
|
2020-08-06 15:34:32 +02:00
|
|
|
withNotifyOnErrors,
|
2020-06-10 15:44:59 +02:00
|
|
|
} from '../../../../../src/plugins/kibana_utils/public';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { useKibana } from '../../../../../src/plugins/kibana_react/public';
|
2020-05-05 22:58:36 +02:00
|
|
|
import {
|
|
|
|
OnSaveProps,
|
2020-06-10 18:28:00 +02:00
|
|
|
checkForDuplicateTitle,
|
2020-05-05 22:58:36 +02:00
|
|
|
} from '../../../../../src/plugins/saved_objects/public';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { injectFilterReferences } from '../persistence';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { NativeRenderer } from '../native_renderer';
|
2019-10-16 05:01:43 +02:00
|
|
|
import { trackUiEvent } from '../lens_ui_telemetry';
|
2019-12-09 12:31:49 +01:00
|
|
|
import {
|
2021-02-04 11:37:50 +01:00
|
|
|
DataPublicPluginStart,
|
2020-02-13 13:59:27 +01:00
|
|
|
esFilters,
|
2020-11-24 12:37:27 +01:00
|
|
|
exporters,
|
2021-02-04 11:37:50 +01:00
|
|
|
Filter,
|
2019-12-09 12:31:49 +01:00
|
|
|
IndexPattern as IndexPatternInstance,
|
|
|
|
IndexPatternsContract,
|
2021-02-04 11:37:50 +01:00
|
|
|
Query,
|
|
|
|
SavedQuery,
|
2020-09-23 23:40:52 +02:00
|
|
|
syncQueryStateWithUrl,
|
2020-04-15 12:22:37 +02:00
|
|
|
} from '../../../../../src/plugins/data/public';
|
2021-02-08 21:31:09 +01:00
|
|
|
import { LENS_EMBEDDABLE_TYPE, getFullPath, APP_ID } from '../../common';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { LensAppProps, LensAppServices, LensAppState } from './types';
|
|
|
|
import { getLensTopNavConfig } from './lens_top_nav';
|
2021-02-04 11:37:50 +01:00
|
|
|
import { Document } from '../persistence';
|
2020-12-08 22:39:24 +01:00
|
|
|
import { SaveModal } from './save_modal';
|
2020-09-23 23:40:52 +02:00
|
|
|
import {
|
|
|
|
LensByReferenceInput,
|
|
|
|
LensEmbeddableInput,
|
|
|
|
} from '../editor_frame_service/embeddable/embeddable';
|
2021-01-21 11:00:57 +01:00
|
|
|
import { useTimeRange } from './time_range';
|
2021-02-04 11:37:50 +01:00
|
|
|
import { EditorFrameInstance } from '../types';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
export function App({
|
2020-09-23 23:40:52 +02:00
|
|
|
history,
|
2020-06-01 21:05:27 +02:00
|
|
|
onAppLeave,
|
2020-09-23 23:40:52 +02:00
|
|
|
redirectTo,
|
|
|
|
editorFrame,
|
|
|
|
initialInput,
|
|
|
|
incomingState,
|
|
|
|
redirectToOrigin,
|
2020-12-08 22:39:24 +01:00
|
|
|
redirectToDashboard,
|
2020-09-14 21:32:30 +02:00
|
|
|
setHeaderActionMenu,
|
2020-10-06 10:15:41 +02:00
|
|
|
initialContext,
|
2020-09-23 23:40:52 +02:00
|
|
|
}: LensAppProps) {
|
|
|
|
const {
|
|
|
|
data,
|
|
|
|
chrome,
|
|
|
|
overlays,
|
|
|
|
navigation,
|
|
|
|
uiSettings,
|
|
|
|
application,
|
2020-12-15 19:18:36 +01:00
|
|
|
stateTransfer,
|
2020-09-23 23:40:52 +02:00
|
|
|
notifications,
|
|
|
|
attributeService,
|
|
|
|
savedObjectsClient,
|
2020-11-03 10:33:18 +01:00
|
|
|
savedObjectsTagging,
|
2020-09-23 23:40:52 +02:00
|
|
|
getOriginatingAppName,
|
|
|
|
|
|
|
|
// Temporarily required until the 'by value' paradigm is default.
|
|
|
|
dashboardFeatureFlag,
|
|
|
|
} = useKibana<LensAppServices>().services;
|
|
|
|
|
|
|
|
const [state, setState] = useState<LensAppState>(() => {
|
2019-11-20 12:06:53 +01:00
|
|
|
return {
|
2020-10-06 10:15:41 +02:00
|
|
|
query: data.query.queryString.getQuery(),
|
2020-11-24 13:26:54 +01:00
|
|
|
// Do not use app-specific filters from previous app,
|
|
|
|
// only if Lens was opened with the intention to visualize a field (e.g. coming from Discover)
|
|
|
|
filters: !initialContext
|
|
|
|
? data.query.filterManager.getGlobalFilters()
|
|
|
|
: data.query.filterManager.getFilters(),
|
2020-09-23 23:40:52 +02:00
|
|
|
isLoading: Boolean(initialInput),
|
|
|
|
indexPatternsForTopNav: [],
|
|
|
|
isLinkedToOriginatingApp: Boolean(incomingState?.originatingApp),
|
|
|
|
isSaveModalVisible: false,
|
2020-07-01 10:08:37 +02:00
|
|
|
indicateNoData: false,
|
2020-08-21 18:08:25 +02:00
|
|
|
isSaveable: false,
|
2020-12-29 14:18:30 +01:00
|
|
|
searchSessionId: data.search.session.start(),
|
2019-11-20 12:06:53 +01:00
|
|
|
};
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const { lastKnownDoc } = state;
|
|
|
|
|
2020-07-01 10:08:37 +02:00
|
|
|
const showNoDataPopover = useCallback(() => {
|
|
|
|
setState((prevState) => ({ ...prevState, indicateNoData: true }));
|
|
|
|
}, [setState]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (state.indicateNoData) {
|
|
|
|
setState((prevState) => ({ ...prevState, indicateNoData: false }));
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
setState,
|
|
|
|
state.indicateNoData,
|
|
|
|
state.query,
|
|
|
|
state.filters,
|
|
|
|
state.indexPatternsForTopNav,
|
2020-12-29 14:18:30 +01:00
|
|
|
state.searchSessionId,
|
2020-07-01 10:08:37 +02:00
|
|
|
]);
|
|
|
|
|
2021-01-21 11:00:57 +01:00
|
|
|
const { resolvedDateRange, from: fromDate, to: toDate } = useTimeRange(
|
|
|
|
data,
|
|
|
|
state.lastKnownDoc,
|
|
|
|
setState
|
|
|
|
);
|
2020-12-29 14:18:30 +01:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const onError = useCallback(
|
|
|
|
(e: { message: string }) =>
|
|
|
|
notifications.toasts.addDanger({
|
|
|
|
title: e.message,
|
|
|
|
}),
|
|
|
|
[notifications.toasts]
|
|
|
|
);
|
|
|
|
|
|
|
|
const getLastKnownDocWithoutPinnedFilters = useCallback(
|
|
|
|
function () {
|
|
|
|
if (!lastKnownDoc) return undefined;
|
|
|
|
const [pinnedFilters, appFilters] = _.partition(
|
|
|
|
injectFilterReferences(lastKnownDoc.state?.filters || [], lastKnownDoc.references),
|
|
|
|
esFilters.isFilterPinned
|
|
|
|
);
|
|
|
|
return pinnedFilters?.length
|
|
|
|
? {
|
|
|
|
...lastKnownDoc,
|
|
|
|
state: {
|
|
|
|
...lastKnownDoc.state,
|
|
|
|
filters: appFilters,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: lastKnownDoc;
|
|
|
|
},
|
|
|
|
[lastKnownDoc]
|
|
|
|
);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const getIsByValueMode = useCallback(
|
|
|
|
() =>
|
|
|
|
Boolean(
|
|
|
|
// Temporarily required until the 'by value' paradigm is default.
|
|
|
|
dashboardFeatureFlag.allowByValueEmbeddables &&
|
|
|
|
state.isLinkedToOriginatingApp &&
|
|
|
|
!(initialInput as LensByReferenceInput)?.savedObjectId
|
|
|
|
),
|
|
|
|
[dashboardFeatureFlag.allowByValueEmbeddables, state.isLinkedToOriginatingApp, initialInput]
|
|
|
|
);
|
2020-06-01 21:05:27 +02:00
|
|
|
|
2019-10-03 17:24:45 +02:00
|
|
|
useEffect(() => {
|
2020-02-03 17:19:41 +01:00
|
|
|
// Clear app-specific filters when navigating to Lens. Necessary because Lens
|
2020-10-06 10:15:41 +02:00
|
|
|
// can be loaded without a full page refresh. If the user navigates to Lens from Discover
|
|
|
|
// we keep the filters
|
|
|
|
if (!initialContext) {
|
|
|
|
data.query.filterManager.setAppFilters([]);
|
|
|
|
}
|
2020-02-03 17:19:41 +01:00
|
|
|
|
2019-11-20 12:06:53 +01:00
|
|
|
const filterSubscription = data.query.filterManager.getUpdates$().subscribe({
|
2019-10-03 17:24:45 +02:00
|
|
|
next: () => {
|
2020-12-29 14:18:30 +01:00
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
filters: data.query.filterManager.getFilters(),
|
|
|
|
searchSessionId: data.search.session.start(),
|
|
|
|
}));
|
2019-10-16 05:01:43 +02:00
|
|
|
trackUiEvent('app_filters_updated');
|
2019-10-03 17:24:45 +02:00
|
|
|
},
|
|
|
|
});
|
2020-03-24 20:51:00 +01:00
|
|
|
|
|
|
|
const timeSubscription = data.query.timefilter.timefilter.getTimeUpdate$().subscribe({
|
|
|
|
next: () => {
|
2020-05-22 09:08:58 +02:00
|
|
|
setState((s) => ({
|
2020-03-24 20:51:00 +01:00
|
|
|
...s,
|
2020-12-29 14:18:30 +01:00
|
|
|
searchSessionId: data.search.session.start(),
|
2020-03-24 20:51:00 +01:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-29 14:18:30 +01:00
|
|
|
const autoRefreshSubscription = data.query.timefilter.timefilter
|
|
|
|
.getAutoRefreshFetch$()
|
|
|
|
.subscribe({
|
|
|
|
next: () => {
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
searchSessionId: data.search.session.start(),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-10 15:44:59 +02:00
|
|
|
const kbnUrlStateStorage = createKbnUrlStateStorage({
|
|
|
|
history,
|
2020-09-23 23:40:52 +02:00
|
|
|
useHash: uiSettings.get('state:storeInSessionStorage'),
|
|
|
|
...withNotifyOnErrors(notifications.toasts),
|
2020-06-10 15:44:59 +02:00
|
|
|
});
|
|
|
|
const { stop: stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl(
|
|
|
|
data.query,
|
|
|
|
kbnUrlStateStorage
|
|
|
|
);
|
|
|
|
|
2019-10-03 17:24:45 +02:00
|
|
|
return () => {
|
2020-06-10 15:44:59 +02:00
|
|
|
stopSyncingQueryServiceStateWithUrl();
|
2019-11-20 12:06:53 +01:00
|
|
|
filterSubscription.unsubscribe();
|
2020-03-24 20:51:00 +01:00
|
|
|
timeSubscription.unsubscribe();
|
2020-12-29 14:18:30 +01:00
|
|
|
autoRefreshSubscription.unsubscribe();
|
2019-10-03 17:24:45 +02:00
|
|
|
};
|
2020-08-06 10:10:09 +02:00
|
|
|
}, [
|
|
|
|
data.query.filterManager,
|
|
|
|
data.query.timefilter.timefilter,
|
2020-12-29 14:18:30 +01:00
|
|
|
data.search.session,
|
2020-09-23 23:40:52 +02:00
|
|
|
notifications.toasts,
|
|
|
|
uiSettings,
|
2020-08-06 10:10:09 +02:00
|
|
|
data.query,
|
|
|
|
history,
|
2020-10-06 10:15:41 +02:00
|
|
|
initialContext,
|
2020-08-06 10:10:09 +02:00
|
|
|
]);
|
2020-06-01 21:05:27 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
onAppLeave((actions) => {
|
|
|
|
// Confirm when the user has made any changes to an existing doc
|
|
|
|
// or when the user has configured something without saving
|
|
|
|
if (
|
2020-09-23 23:40:52 +02:00
|
|
|
application.capabilities.visualize.save &&
|
2020-08-21 18:08:25 +02:00
|
|
|
!_.isEqual(state.persistedDoc?.state, getLastKnownDocWithoutPinnedFilters()?.state) &&
|
|
|
|
(state.isSaveable || state.persistedDoc)
|
2020-06-01 21:05:27 +02:00
|
|
|
) {
|
|
|
|
return actions.confirm(
|
|
|
|
i18n.translate('xpack.lens.app.unsavedWorkMessage', {
|
|
|
|
defaultMessage: 'Leave Lens with unsaved work?',
|
|
|
|
}),
|
|
|
|
i18n.translate('xpack.lens.app.unsavedWorkTitle', {
|
|
|
|
defaultMessage: 'Unsaved changes',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return actions.default();
|
|
|
|
}
|
|
|
|
});
|
2020-08-21 18:08:25 +02:00
|
|
|
}, [
|
|
|
|
onAppLeave,
|
2020-09-23 23:40:52 +02:00
|
|
|
lastKnownDoc,
|
2020-08-21 18:08:25 +02:00
|
|
|
state.isSaveable,
|
2020-09-23 23:40:52 +02:00
|
|
|
state.persistedDoc,
|
2020-08-21 18:08:25 +02:00
|
|
|
getLastKnownDocWithoutPinnedFilters,
|
2020-09-23 23:40:52 +02:00
|
|
|
application.capabilities.visualize.save,
|
2020-08-21 18:08:25 +02:00
|
|
|
]);
|
2019-10-03 17:24:45 +02:00
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
// Sync Kibana breadcrumbs any time the saved document's title changes
|
|
|
|
useEffect(() => {
|
2020-09-23 23:40:52 +02:00
|
|
|
const isByValueMode = getIsByValueMode();
|
|
|
|
const breadcrumbs: EuiBreadcrumb[] = [];
|
|
|
|
if (state.isLinkedToOriginatingApp && getOriginatingAppName() && redirectToOrigin) {
|
|
|
|
breadcrumbs.push({
|
|
|
|
onClick: () => {
|
|
|
|
redirectToOrigin();
|
|
|
|
},
|
|
|
|
text: getOriginatingAppName(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!isByValueMode) {
|
|
|
|
breadcrumbs.push({
|
|
|
|
href: application.getUrlForApp('visualize'),
|
2020-05-22 09:08:58 +02:00
|
|
|
onClick: (e) => {
|
2020-09-23 23:40:52 +02:00
|
|
|
application.navigateToApp('visualize', { path: '/' });
|
2020-05-13 09:19:24 +02:00
|
|
|
e.preventDefault();
|
|
|
|
},
|
2019-09-17 20:57:53 +02:00
|
|
|
text: i18n.translate('xpack.lens.breadcrumbsTitle', {
|
2021-02-11 19:46:35 +01:00
|
|
|
defaultMessage: 'Visualize Library',
|
2019-09-17 20:57:53 +02:00
|
|
|
}),
|
2020-09-23 23:40:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
let currentDocTitle = i18n.translate('xpack.lens.breadcrumbsCreate', {
|
|
|
|
defaultMessage: 'Create',
|
|
|
|
});
|
|
|
|
if (state.persistedDoc) {
|
|
|
|
currentDocTitle = isByValueMode
|
|
|
|
? i18n.translate('xpack.lens.breadcrumbsByValue', { defaultMessage: 'Edit visualization' })
|
|
|
|
: state.persistedDoc.title;
|
|
|
|
}
|
|
|
|
breadcrumbs.push({ text: currentDocTitle });
|
|
|
|
chrome.setBreadcrumbs(breadcrumbs);
|
2020-08-21 16:18:14 +02:00
|
|
|
}, [
|
2020-09-23 23:40:52 +02:00
|
|
|
dashboardFeatureFlag.allowByValueEmbeddables,
|
|
|
|
state.isLinkedToOriginatingApp,
|
|
|
|
getOriginatingAppName,
|
2020-08-21 16:18:14 +02:00
|
|
|
state.persistedDoc,
|
2020-09-23 23:40:52 +02:00
|
|
|
redirectToOrigin,
|
|
|
|
getIsByValueMode,
|
|
|
|
initialInput,
|
|
|
|
application,
|
|
|
|
chrome,
|
2020-08-21 16:18:14 +02:00
|
|
|
]);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
!initialInput ||
|
|
|
|
(attributeService.inputIsRefType(initialInput) &&
|
|
|
|
initialInput.savedObjectId === state.persistedDoc?.savedObjectId)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState((s) => ({ ...s, isLoading: true }));
|
|
|
|
attributeService
|
|
|
|
.unwrapAttributes(initialInput)
|
|
|
|
.then((attributes) => {
|
|
|
|
if (!initialInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const doc = {
|
|
|
|
...initialInput,
|
|
|
|
...attributes,
|
|
|
|
type: LENS_EMBEDDABLE_TYPE,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (attributeService.inputIsRefType(initialInput)) {
|
|
|
|
chrome.recentlyAccessed.add(
|
|
|
|
getFullPath(initialInput.savedObjectId),
|
|
|
|
attributes.title,
|
|
|
|
initialInput.savedObjectId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
getAllIndexPatterns(
|
|
|
|
_.uniq(doc.references.filter(({ type }) => type === 'index-pattern').map(({ id }) => id)),
|
|
|
|
data.indexPatterns,
|
|
|
|
notifications
|
|
|
|
)
|
|
|
|
.then((indexPatterns) => {
|
|
|
|
// Don't overwrite any pinned filters
|
|
|
|
data.query.filterManager.setAppFilters(
|
|
|
|
injectFilterReferences(doc.state.filters, doc.references)
|
|
|
|
);
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
isLoading: false,
|
|
|
|
persistedDoc: doc,
|
|
|
|
lastKnownDoc: doc,
|
|
|
|
query: doc.state.query,
|
|
|
|
indexPatternsForTopNav: indexPatterns,
|
|
|
|
}));
|
2020-08-06 10:10:09 +02:00
|
|
|
})
|
2020-08-21 18:08:25 +02:00
|
|
|
.catch((e) => {
|
2020-08-06 10:10:09 +02:00
|
|
|
setState((s) => ({ ...s, isLoading: false }));
|
|
|
|
redirectTo();
|
|
|
|
});
|
2020-09-23 23:40:52 +02:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
setState((s) => ({ ...s, isLoading: false }));
|
|
|
|
notifications.toasts.addDanger(
|
|
|
|
i18n.translate('xpack.lens.app.docLoadingError', {
|
|
|
|
defaultMessage: 'Error loading saved document',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
redirectTo();
|
|
|
|
});
|
|
|
|
}, [
|
|
|
|
notifications,
|
|
|
|
data.indexPatterns,
|
|
|
|
data.query.filterManager,
|
|
|
|
initialInput,
|
|
|
|
attributeService,
|
|
|
|
redirectTo,
|
|
|
|
chrome.recentlyAccessed,
|
|
|
|
state.persistedDoc?.savedObjectId,
|
|
|
|
state.persistedDoc?.state,
|
|
|
|
]);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2021-01-29 15:59:05 +01:00
|
|
|
const tagsIds =
|
|
|
|
state.persistedDoc && savedObjectsTagging
|
|
|
|
? savedObjectsTagging.ui.getTagIdsFromReferences(state.persistedDoc.references)
|
|
|
|
: [];
|
|
|
|
|
2020-06-10 18:28:00 +02:00
|
|
|
const runSave = async (
|
2020-05-05 22:58:36 +02:00
|
|
|
saveProps: Omit<OnSaveProps, 'onTitleDuplicate' | 'newDescription'> & {
|
|
|
|
returnToOrigin: boolean;
|
2020-12-08 22:39:24 +01:00
|
|
|
dashboardId?: string | null;
|
2020-06-10 18:28:00 +02:00
|
|
|
onTitleDuplicate?: OnSaveProps['onTitleDuplicate'];
|
|
|
|
newDescription?: string;
|
2020-11-03 10:33:18 +01:00
|
|
|
newTags?: string[];
|
2020-09-23 23:40:52 +02:00
|
|
|
},
|
|
|
|
options: { saveToLibrary: boolean }
|
2020-05-05 22:58:36 +02:00
|
|
|
) => {
|
|
|
|
if (!lastKnownDoc) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-03 10:33:18 +01:00
|
|
|
|
|
|
|
let references = lastKnownDoc.references;
|
2021-01-29 15:59:05 +01:00
|
|
|
if (savedObjectsTagging) {
|
|
|
|
references = savedObjectsTagging.ui.updateTagsReferences(
|
|
|
|
references,
|
|
|
|
saveProps.newTags || tagsIds
|
|
|
|
);
|
2020-11-03 10:33:18 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const docToSave = {
|
2020-08-21 18:08:25 +02:00
|
|
|
...getLastKnownDocWithoutPinnedFilters()!,
|
2020-06-10 18:28:00 +02:00
|
|
|
description: saveProps.newDescription,
|
2020-05-05 22:58:36 +02:00
|
|
|
title: saveProps.newTitle,
|
2020-11-03 10:33:18 +01:00
|
|
|
references,
|
2020-05-05 22:58:36 +02:00
|
|
|
};
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
// Required to serialize filters in by value mode until
|
|
|
|
// https://github.com/elastic/kibana/issues/77588 is fixed
|
|
|
|
if (getIsByValueMode()) {
|
|
|
|
docToSave.state.filters.forEach((filter) => {
|
|
|
|
if (typeof filter.meta.value === 'function') {
|
|
|
|
delete filter.meta.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const originalInput = saveProps.newCopyOnSave ? undefined : initialInput;
|
|
|
|
const originalSavedObjectId = (originalInput as LensByReferenceInput)?.savedObjectId;
|
2020-11-02 19:35:33 +01:00
|
|
|
if (options.saveToLibrary) {
|
|
|
|
try {
|
|
|
|
await checkForDuplicateTitle(
|
|
|
|
{
|
|
|
|
id: originalSavedObjectId,
|
|
|
|
title: docToSave.title,
|
|
|
|
copyOnSave: saveProps.newCopyOnSave,
|
|
|
|
lastSavedTitle: lastKnownDoc.title,
|
|
|
|
getEsType: () => 'lens',
|
|
|
|
getDisplayName: () =>
|
|
|
|
i18n.translate('xpack.lens.app.saveModalType', {
|
|
|
|
defaultMessage: 'Lens visualization',
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
saveProps.isTitleDuplicateConfirmed,
|
|
|
|
saveProps.onTitleDuplicate,
|
|
|
|
{
|
|
|
|
savedObjectsClient,
|
|
|
|
overlays,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore duplicate title failure, user notified in save modal
|
|
|
|
return;
|
|
|
|
}
|
2020-09-23 23:40:52 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
const newInput = (await attributeService.wrapAttributes(
|
|
|
|
docToSave,
|
|
|
|
options.saveToLibrary,
|
|
|
|
originalInput
|
|
|
|
)) as LensEmbeddableInput;
|
|
|
|
|
|
|
|
if (saveProps.returnToOrigin && redirectToOrigin) {
|
|
|
|
// disabling the validation on app leave because the document has been saved.
|
|
|
|
onAppLeave((actions) => {
|
|
|
|
return actions.default();
|
|
|
|
});
|
|
|
|
redirectToOrigin({ input: newInput, isCopied: saveProps.newCopyOnSave });
|
|
|
|
return;
|
2020-12-08 22:39:24 +01:00
|
|
|
} else if (saveProps.dashboardId && redirectToDashboard) {
|
|
|
|
// disabling the validation on app leave because the document has been saved.
|
|
|
|
onAppLeave((actions) => {
|
|
|
|
return actions.default();
|
|
|
|
});
|
|
|
|
redirectToDashboard(newInput, saveProps.dashboardId);
|
|
|
|
return;
|
2020-06-10 18:28:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:46:10 +02:00
|
|
|
notifications.toasts.addSuccess(
|
|
|
|
i18n.translate('xpack.lens.app.saveVisualization.successNotificationText', {
|
|
|
|
defaultMessage: `Saved '{visTitle}'`,
|
|
|
|
values: {
|
|
|
|
visTitle: docToSave.title,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
if (
|
|
|
|
attributeService.inputIsRefType(newInput) &&
|
|
|
|
newInput.savedObjectId !== originalSavedObjectId
|
|
|
|
) {
|
|
|
|
chrome.recentlyAccessed.add(
|
|
|
|
getFullPath(newInput.savedObjectId),
|
|
|
|
docToSave.title,
|
|
|
|
newInput.savedObjectId
|
|
|
|
);
|
2020-05-22 09:08:58 +02:00
|
|
|
setState((s) => ({
|
2020-05-05 22:58:36 +02:00
|
|
|
...s,
|
|
|
|
isSaveModalVisible: false,
|
2020-09-23 23:40:52 +02:00
|
|
|
isLinkedToOriginatingApp: false,
|
2020-05-05 22:58:36 +02:00
|
|
|
}));
|
2020-12-15 19:18:36 +01:00
|
|
|
// remove editor state so the connection is still broken after reload
|
2021-02-08 21:31:09 +01:00
|
|
|
stateTransfer.clearEditorState(APP_ID);
|
2020-12-15 19:18:36 +01:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
redirectTo(newInput.savedObjectId);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-05 22:58:36 +02:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const newDoc = {
|
|
|
|
...docToSave,
|
|
|
|
...newInput,
|
|
|
|
};
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
persistedDoc: newDoc,
|
|
|
|
lastKnownDoc: newDoc,
|
|
|
|
isSaveModalVisible: false,
|
|
|
|
isLinkedToOriginatingApp: false,
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.dir(e);
|
|
|
|
trackUiEvent('save_failed');
|
|
|
|
setState((s) => ({ ...s, isSaveModalVisible: false }));
|
|
|
|
}
|
|
|
|
};
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2021-02-04 11:37:50 +01:00
|
|
|
const lastKnownDocRef = useRef(state.lastKnownDoc);
|
|
|
|
lastKnownDocRef.current = state.lastKnownDoc;
|
|
|
|
|
|
|
|
const activeDataRef = useRef(state.activeData);
|
|
|
|
activeDataRef.current = state.activeData;
|
|
|
|
|
2020-04-15 12:22:37 +02:00
|
|
|
const { TopNavMenu } = navigation.ui;
|
2019-10-28 09:40:21 +01:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const savingPermitted = Boolean(state.isSaveable && application.capabilities.visualize.save);
|
2020-11-24 12:37:27 +01:00
|
|
|
const unsavedTitle = i18n.translate('xpack.lens.app.unsavedFilename', {
|
|
|
|
defaultMessage: 'unsaved',
|
|
|
|
});
|
2020-09-23 23:40:52 +02:00
|
|
|
const topNavConfig = getLensTopNavConfig({
|
|
|
|
showSaveAndReturn: Boolean(
|
|
|
|
state.isLinkedToOriginatingApp &&
|
|
|
|
// Temporarily required until the 'by value' paradigm is default.
|
|
|
|
(dashboardFeatureFlag.allowByValueEmbeddables || Boolean(initialInput))
|
|
|
|
),
|
2020-11-24 12:37:27 +01:00
|
|
|
enableExportToCSV: Boolean(
|
|
|
|
state.isSaveable && state.activeData && Object.keys(state.activeData).length
|
|
|
|
),
|
2020-09-23 23:40:52 +02:00
|
|
|
isByValueMode: getIsByValueMode(),
|
|
|
|
showCancel: Boolean(state.isLinkedToOriginatingApp),
|
|
|
|
savingPermitted,
|
|
|
|
actions: {
|
2020-11-24 12:37:27 +01:00
|
|
|
exportToCSV: () => {
|
|
|
|
if (!state.activeData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const datatables = Object.values(state.activeData);
|
|
|
|
const content = datatables.reduce<Record<string, { content: string; type: string }>>(
|
|
|
|
(memo, datatable, i) => {
|
|
|
|
// skip empty datatables
|
|
|
|
if (datatable) {
|
|
|
|
const postFix = datatables.length > 1 ? `-${i + 1}` : '';
|
|
|
|
|
|
|
|
memo[`${lastKnownDoc?.title || unsavedTitle}${postFix}.csv`] = {
|
|
|
|
content: exporters.datatableToCSV(datatable, {
|
|
|
|
csvSeparator: uiSettings.get('csv:separator', ','),
|
|
|
|
quoteValues: uiSettings.get('csv:quoteValues', true),
|
|
|
|
formatFactory: data.fieldFormats.deserialize,
|
|
|
|
}),
|
|
|
|
type: exporters.CSV_MIME_TYPE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
if (content) {
|
|
|
|
downloadMultipleAs(content);
|
|
|
|
}
|
|
|
|
},
|
2020-09-23 23:40:52 +02:00
|
|
|
saveAndReturn: () => {
|
|
|
|
if (savingPermitted && lastKnownDoc) {
|
|
|
|
// disabling the validation on app leave because the document has been saved.
|
|
|
|
onAppLeave((actions) => {
|
|
|
|
return actions.default();
|
|
|
|
});
|
|
|
|
runSave(
|
|
|
|
{
|
|
|
|
newTitle: lastKnownDoc.title,
|
|
|
|
newCopyOnSave: false,
|
|
|
|
isTitleDuplicateConfirmed: false,
|
|
|
|
returnToOrigin: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
saveToLibrary:
|
|
|
|
(initialInput && attributeService.inputIsRefType(initialInput)) ?? false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
showSaveModal: () => {
|
|
|
|
if (savingPermitted) {
|
|
|
|
setState((s) => ({ ...s, isSaveModalVisible: true }));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cancel: () => {
|
|
|
|
if (redirectToOrigin) {
|
|
|
|
redirectToOrigin();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return (
|
2020-09-23 23:40:52 +02:00
|
|
|
<>
|
|
|
|
<div className="lnsApp">
|
|
|
|
<div className="lnsApp__header">
|
|
|
|
<TopNavMenu
|
|
|
|
setMenuMountPoint={setHeaderActionMenu}
|
|
|
|
config={topNavConfig}
|
|
|
|
showSearchBar={true}
|
|
|
|
showDatePicker={true}
|
|
|
|
showQueryBar={true}
|
|
|
|
showFilterBar={true}
|
|
|
|
indexPatterns={state.indexPatternsForTopNav}
|
|
|
|
showSaveQuery={Boolean(application.capabilities.visualize.saveQuery)}
|
|
|
|
savedQuery={state.savedQuery}
|
|
|
|
data-test-subj="lnsApp_topNav"
|
|
|
|
screenTitle={'lens'}
|
|
|
|
appName={'lens'}
|
|
|
|
onQuerySubmit={(payload) => {
|
|
|
|
const { dateRange, query } = payload;
|
2020-12-29 14:18:30 +01:00
|
|
|
const currentRange = data.query.timefilter.timefilter.getTime();
|
|
|
|
if (dateRange.from !== currentRange.from || dateRange.to !== currentRange.to) {
|
2020-09-23 23:40:52 +02:00
|
|
|
data.query.timefilter.timefilter.setTime(dateRange);
|
|
|
|
trackUiEvent('app_date_change');
|
|
|
|
} else {
|
2020-12-29 14:18:30 +01:00
|
|
|
// Query has changed, renew the session id.
|
|
|
|
// Time change will be picked up by the time subscription
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
searchSessionId: data.search.session.start(),
|
|
|
|
}));
|
2020-09-23 23:40:52 +02:00
|
|
|
trackUiEvent('app_query_change');
|
|
|
|
}
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
query: query || s.query,
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
onSaved={(savedQuery) => {
|
|
|
|
setState((s) => ({ ...s, savedQuery }));
|
|
|
|
}}
|
|
|
|
onSavedQueryUpdated={(savedQuery) => {
|
|
|
|
const savedQueryFilters = savedQuery.attributes.filters || [];
|
|
|
|
const globalFilters = data.query.filterManager.getGlobalFilters();
|
|
|
|
data.query.filterManager.setFilters([...globalFilters, ...savedQueryFilters]);
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
savedQuery: { ...savedQuery }, // Shallow query for reference issues
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
onClearSavedQuery={() => {
|
|
|
|
data.query.filterManager.setFilters(data.query.filterManager.getGlobalFilters());
|
|
|
|
setState((s) => ({
|
|
|
|
...s,
|
|
|
|
savedQuery: undefined,
|
|
|
|
filters: data.query.filterManager.getGlobalFilters(),
|
|
|
|
query: data.query.queryString.getDefaultQuery(),
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
query={state.query}
|
2020-12-29 14:18:30 +01:00
|
|
|
dateRangeFrom={fromDate}
|
|
|
|
dateRangeTo={toDate}
|
2020-09-23 23:40:52 +02:00
|
|
|
indicateNoData={state.indicateNoData}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{(!state.isLoading || state.persistedDoc) && (
|
2021-02-04 11:37:50 +01:00
|
|
|
<MemoizedEditorFrameWrapper
|
|
|
|
editorFrame={editorFrame}
|
|
|
|
resolvedDateRange={resolvedDateRange}
|
|
|
|
onError={onError}
|
|
|
|
showNoDataPopover={showNoDataPopover}
|
|
|
|
initialContext={initialContext}
|
|
|
|
setState={setState}
|
|
|
|
data={data}
|
|
|
|
notifications={notifications}
|
|
|
|
query={state.query}
|
|
|
|
filters={state.filters}
|
|
|
|
searchSessionId={state.searchSessionId}
|
|
|
|
isSaveable={state.isSaveable}
|
|
|
|
savedQuery={state.savedQuery}
|
|
|
|
persistedDoc={state.persistedDoc}
|
|
|
|
indexPatterns={state.indexPatternsForTopNav}
|
|
|
|
activeData={activeDataRef}
|
|
|
|
lastKnownDoc={lastKnownDocRef}
|
2019-10-16 01:40:30 +02:00
|
|
|
/>
|
|
|
|
)}
|
2020-09-23 23:40:52 +02:00
|
|
|
</div>
|
2020-12-08 22:39:24 +01:00
|
|
|
<SaveModal
|
|
|
|
isVisible={state.isSaveModalVisible}
|
2020-12-10 16:19:27 +01:00
|
|
|
originatingApp={state.isLinkedToOriginatingApp ? incomingState?.originatingApp : undefined}
|
2020-12-08 22:39:24 +01:00
|
|
|
allowByValueEmbeddables={dashboardFeatureFlag.allowByValueEmbeddables}
|
|
|
|
savedObjectsTagging={savedObjectsTagging}
|
|
|
|
tagsIds={tagsIds}
|
|
|
|
onSave={runSave}
|
|
|
|
onClose={() => {
|
|
|
|
setState((s) => ({ ...s, isSaveModalVisible: false }));
|
|
|
|
}}
|
|
|
|
getAppNameFromId={() => getOriginatingAppName()}
|
|
|
|
lastKnownDoc={lastKnownDoc}
|
|
|
|
returnToOriginSwitchLabel={
|
|
|
|
getIsByValueMode() && initialInput
|
|
|
|
? i18n.translate('xpack.lens.app.updatePanel', {
|
|
|
|
defaultMessage: 'Update panel on {originatingAppName}',
|
|
|
|
values: { originatingAppName: getOriginatingAppName() },
|
|
|
|
})
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
2020-09-23 23:40:52 +02:00
|
|
|
</>
|
2019-09-17 20:57:53 +02:00
|
|
|
);
|
|
|
|
}
|
2019-10-03 17:24:45 +02:00
|
|
|
|
2021-02-04 11:37:50 +01:00
|
|
|
const MemoizedEditorFrameWrapper = React.memo(function EditorFrameWrapper({
|
|
|
|
editorFrame,
|
|
|
|
query,
|
|
|
|
filters,
|
|
|
|
searchSessionId,
|
|
|
|
isSaveable: oldIsSaveable,
|
|
|
|
savedQuery,
|
|
|
|
persistedDoc,
|
|
|
|
indexPatterns: indexPatternsForTopNav,
|
|
|
|
resolvedDateRange,
|
|
|
|
onError,
|
|
|
|
showNoDataPopover,
|
|
|
|
initialContext,
|
|
|
|
setState,
|
|
|
|
data,
|
|
|
|
notifications,
|
|
|
|
lastKnownDoc,
|
|
|
|
activeData: activeDataRef,
|
|
|
|
}: {
|
|
|
|
editorFrame: EditorFrameInstance;
|
|
|
|
searchSessionId: string;
|
|
|
|
query: Query;
|
|
|
|
filters: Filter[];
|
|
|
|
isSaveable: boolean;
|
|
|
|
savedQuery?: SavedQuery;
|
|
|
|
persistedDoc?: Document | undefined;
|
|
|
|
indexPatterns: IndexPatternInstance[];
|
|
|
|
resolvedDateRange: { fromDate: string; toDate: string };
|
|
|
|
onError: (e: { message: string }) => Toast;
|
|
|
|
showNoDataPopover: () => void;
|
|
|
|
initialContext: VisualizeFieldContext | undefined;
|
|
|
|
setState: React.Dispatch<React.SetStateAction<LensAppState>>;
|
|
|
|
data: DataPublicPluginStart;
|
|
|
|
notifications: NotificationsStart;
|
|
|
|
lastKnownDoc: React.MutableRefObject<Document | undefined>;
|
|
|
|
activeData: React.MutableRefObject<Record<string, Datatable> | undefined>;
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<NativeRenderer
|
|
|
|
className="lnsApp__frame"
|
|
|
|
render={editorFrame.mount}
|
|
|
|
nativeProps={{
|
|
|
|
searchSessionId,
|
|
|
|
dateRange: resolvedDateRange,
|
|
|
|
query,
|
|
|
|
filters,
|
|
|
|
savedQuery,
|
|
|
|
doc: persistedDoc,
|
|
|
|
onError,
|
|
|
|
showNoDataPopover,
|
|
|
|
initialContext,
|
|
|
|
onChange: ({ filterableIndexPatterns, doc, isSaveable, activeData }) => {
|
|
|
|
if (isSaveable !== oldIsSaveable) {
|
|
|
|
setState((s) => ({ ...s, isSaveable }));
|
|
|
|
}
|
|
|
|
if (!_.isEqual(persistedDoc, doc) && !_.isEqual(lastKnownDoc.current, doc)) {
|
|
|
|
setState((s) => ({ ...s, lastKnownDoc: doc }));
|
|
|
|
}
|
|
|
|
if (!_.isEqual(activeDataRef.current, activeData)) {
|
|
|
|
setState((s) => ({ ...s, activeData }));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cached index patterns if the user made a change to any of them
|
|
|
|
if (
|
|
|
|
indexPatternsForTopNav.length !== filterableIndexPatterns.length ||
|
|
|
|
filterableIndexPatterns.some(
|
|
|
|
(id) => !indexPatternsForTopNav.find((indexPattern) => indexPattern.id === id)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
getAllIndexPatterns(filterableIndexPatterns, data.indexPatterns, notifications).then(
|
|
|
|
(indexPatterns) => {
|
|
|
|
if (indexPatterns) {
|
|
|
|
setState((s) => ({ ...s, indexPatternsForTopNav: indexPatterns }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-10-03 17:24:45 +02:00
|
|
|
export async function getAllIndexPatterns(
|
2020-08-21 18:08:25 +02:00
|
|
|
ids: string[],
|
2019-12-09 12:31:49 +01:00
|
|
|
indexPatternsService: IndexPatternsContract,
|
2019-10-03 17:24:45 +02:00
|
|
|
notifications: NotificationsStart
|
|
|
|
): Promise<IndexPatternInstance[]> {
|
|
|
|
try {
|
2020-08-21 18:08:25 +02:00
|
|
|
return await Promise.all(ids.map((id) => indexPatternsService.get(id)));
|
2019-10-03 17:24:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
notifications.toasts.addDanger(
|
2019-10-16 01:40:30 +02:00
|
|
|
i18n.translate('xpack.lens.app.indexPatternLoadingError', {
|
2019-10-03 17:24:45 +02:00
|
|
|
defaultMessage: 'Error loading index patterns',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|