Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dashboard] Add Analytics No Data Page #132188

Merged
merged 15 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/dashboard/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"requiredPlugins": [
"data",
"dataViews",
"dataViewEditor",
"embeddable",
"controls",
"inspector",
Expand Down
13 changes: 11 additions & 2 deletions src/plugins/dashboard/public/application/dashboard_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import { History } from 'history';
import React, { useEffect, useMemo } from 'react';
import React, { useEffect, useMemo, useState } from 'react';

import { useKibana, useExecutionContext } from '@kbn/kibana-react-plugin/public';

import { useDashboardSelector } from './state';
import { useDashboardAppState } from './hooks';
import {
Expand All @@ -22,6 +23,7 @@ import { EmbeddableRenderer, ViewMode } from '../services/embeddable';
import { DashboardTopNav, isCompleteDashboardAppState } from './top_nav/dashboard_top_nav';
import { DashboardAppServices, DashboardEmbedSettings, DashboardRedirect } from '../types';
import { createKbnUrlStateStorage, withNotifyOnErrors } from '../services/kibana_utils';
import { DashboardAppNoDataPage } from './dashboard_app_no_data';
export interface DashboardAppProps {
history: History;
savedDashboardId?: string;
Expand All @@ -46,6 +48,8 @@ export function DashboardApp({
screenshotModeService,
} = useKibana<DashboardAppServices>().services;

const [showNoDataPage, setShowNoDataPage] = useState<boolean>(false);

const kbnUrlStateStorage = useMemo(
() =>
createKbnUrlStateStorage({
Expand All @@ -65,6 +69,8 @@ export function DashboardApp({
const dashboardState = useDashboardSelector((state) => state.dashboardStateReducer);
const dashboardAppState = useDashboardAppState({
history,
showNoDataPage,
setShowNoDataPage,
savedDashboardId,
kbnUrlStateStorage,
isEmbeddedExternally: Boolean(embedSettings),
Expand Down Expand Up @@ -125,7 +131,10 @@ export function DashboardApp({

return (
<>
{isCompleteDashboardAppState(dashboardAppState) && (
{showNoDataPage && (
<DashboardAppNoDataPage onDataViewCreated={() => setShowNoDataPage(false)} />
)}
{!showNoDataPage && isCompleteDashboardAppState(dashboardAppState) && (
<>
<DashboardTopNav
printMode={printMode}
Expand Down
47 changes: 47 additions & 0 deletions src/plugins/dashboard/public/application/dashboard_app_no_data.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import {
AnalyticsNoDataPageKibanaProvider,
AnalyticsNoDataPage,
} from '@kbn/shared-ux-page-analytics-no-data';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';

import { DashboardAppServices } from '../types';
import { useKibana } from '../services/kibana_react';

export const DashboardAppNoDataPage = ({
onDataViewCreated,
}: {
onDataViewCreated: () => void;
}) => {
const {
services: { core, data, dataViewEditor },
} = useKibana<DashboardAppServices>();
const analyticsServices = {
coreStart: core as unknown as React.ComponentProps<
typeof AnalyticsNoDataPageKibanaProvider
>['coreStart'],
dataViews: data.dataViews,
dataViewEditor,
};
return (
<AnalyticsNoDataPageKibanaProvider {...analyticsServices}>
<AnalyticsNoDataPage onDataViewCreated={onDataViewCreated} />;
</AnalyticsNoDataPageKibanaProvider>
);
};

export const isDashboardAppInNoDataState = async (
dataViews: DataPublicPluginStart['dataViews']
) => {
const hasUserDataView = await dataViews.hasData.hasUserDataView().catch(() => false);
const hasEsData = await dataViews.hasData.hasESData().catch(() => true);
return !hasUserDataView || !hasEsData;
};
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/application/dashboard_router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function mountApp({
visualizations,
presentationUtil,
screenshotMode,
dataViewEditor,
} = pluginsStart;

const activeSpaceId =
Expand All @@ -97,6 +98,7 @@ export async function mountApp({
onAppLeave,
savedObjects,
urlForwarding,
dataViewEditor,
visualizations,
usageCollection,
core: coreStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
getSavedDashboardMock,
makeDefaultServices,
} from '../test_helpers';
import { DataViewsContract } from '../../services/data';
import { DataView } from '../../services/data_views';
import type { Filter } from '@kbn/es-query';

Expand All @@ -54,14 +53,20 @@ const createDashboardAppStateProps = (): UseDashboardStateProps => ({
savedDashboardId: 'testDashboardId',
history: createBrowserHistory(),
isEmbeddedExternally: false,
showNoDataPage: false,
setShowNoDataPage: () => {},
});

const createDashboardAppStateServices = () => {
const defaults = makeDefaultServices();
const dataViews = {} as DataViewsContract;
const defaultDataView = { id: 'foo', fields: [{ name: 'bar' }] } as DataView;
dataViews.ensureDefaultDataView = jest.fn().mockImplementation(() => Promise.resolve(true));
dataViews.getDefault = jest.fn().mockImplementation(() => Promise.resolve(defaultDataView));
defaults.dataViews.getDefaultDataView = jest
.fn()
.mockImplementation(() => Promise.resolve(defaultDataView));

defaults.dataViews.getDefault = jest
.fn()
.mockImplementation(() => Promise.resolve(defaultDataView));

const data = dataPluginMock.createStartContract();
data.query.filterManager.getUpdates$ = jest.fn().mockImplementation(() => of(void 0));
Expand All @@ -71,7 +76,7 @@ const createDashboardAppStateServices = () => {
.fn()
.mockImplementation(() => of(void 0));

return { ...defaults, dataViews, data };
return { ...defaults, data };
};

const setupEmbeddableFactory = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ import {
areTimeRangesEqual,
areRefreshIntervalsEqual,
} from '../lib';
import { isDashboardAppInNoDataState } from '../dashboard_app_no_data';

export interface UseDashboardStateProps {
history: History;
showNoDataPage: boolean;
savedDashboardId?: string;
isEmbeddedExternally: boolean;
setShowNoDataPage: (showNoData: boolean) => void;
kbnUrlStateStorage: IKbnUrlStateStorage;
}

export const useDashboardAppState = ({
history,
savedDashboardId,
showNoDataPage,
setShowNoDataPage,
kbnUrlStateStorage,
isEmbeddedExternally,
}: UseDashboardStateProps) => {
Expand Down Expand Up @@ -138,6 +143,21 @@ export const useDashboardAppState = ({
};

(async () => {
/**
* Ensure default data view exists and there is data in elasticsearch
*/
const isEmpty = await isDashboardAppInNoDataState(dataViews);
if (showNoDataPage || isEmpty) {
setShowNoDataPage(true);
return;
}

const defaultDataView = await dataViews.getDefaultDataView();

if (!defaultDataView) {
return;
}

/**
* Load and unpack state from dashboard saved object.
*/
Expand Down Expand Up @@ -375,6 +395,8 @@ export const useDashboardAppState = ({
search,
query,
data,
showNoDataPage,
setShowNoDataPage,
spacesService?.ui,
screenshotModeService,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const loadSavedDashboardState = async ({
notifications.toasts.addWarning(getDashboard60Warning());
return;
}
await dataViews.ensureDefaultDataView();
try {
const savedDashboard = (await savedDashboards.get({
id: savedDashboardId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { ApplicationStart, SavedObjectsFindOptionsReference } from '@kbn/core/public';
import { useExecutionContext } from '@kbn/kibana-react-plugin/public';
import useMount from 'react-use/lib/useMount';
import { attemptLoadDashboardByTitle } from '../lib';
import { DashboardAppServices, DashboardRedirect } from '../../types';
import {
Expand All @@ -36,6 +37,7 @@ import { DashboardUnsavedListing } from './dashboard_unsaved_listing';
import { confirmCreateWithUnsaved, confirmDiscardUnsavedChanges } from './confirm_overlays';
import { getDashboardListItemLink } from './get_dashboard_list_item_link';
import { DASHBOARD_PANELS_UNSAVED_ID } from '../lib/dashboard_session_storage';
import { DashboardAppNoDataPage, isDashboardAppInNoDataState } from '../dashboard_app_no_data';

const SAVED_OBJECTS_LIMIT_SETTING = 'savedObjects:listingLimit';
const SAVED_OBJECTS_PER_PAGE_SETTING = 'savedObjects:perPage';
Expand All @@ -57,6 +59,7 @@ export const DashboardListing = ({
services: {
core,
data,
dataViews,
savedDashboards,
savedObjectsClient,
savedObjectsTagging,
Expand All @@ -66,6 +69,11 @@ export const DashboardListing = ({
},
} = useKibana<DashboardAppServices>();

const [showNoDataPage, setShowNoDataPage] = useState<boolean>(false);
useMount(() => {
(async () => setShowNoDataPage(await isDashboardAppInNoDataState(dataViews)))();
});

const [unsavedDashboardIds, setUnsavedDashboardIds] = useState<string[]>(
dashboardSessionStorage.getDashboardIdsWithUnsavedChanges()
);
Expand Down Expand Up @@ -286,37 +294,44 @@ export const DashboardListing = ({
const { getEntityName, getTableCaption, getTableListTitle, getEntityNamePlural } =
dashboardListingTable;
return (
<TableListView
createItem={!showWriteControls ? undefined : createItem}
deleteItems={!showWriteControls ? undefined : deleteItems}
initialPageSize={initialPageSize}
editItem={!showWriteControls ? undefined : editItem}
initialFilter={initialFilter ?? defaultFilter}
toastNotifications={core.notifications.toasts}
headingId="dashboardListingHeading"
findItems={fetchItems}
rowHeader="title"
entityNamePlural={getEntityNamePlural()}
tableListTitle={getTableListTitle()}
tableCaption={getTableCaption()}
entityName={getEntityName()}
{...{
emptyPrompt,
searchFilters,
listingLimit,
tableColumns,
}}
theme={core.theme}
application={core.application}
>
<DashboardUnsavedListing
redirectTo={redirectTo}
unsavedDashboardIds={unsavedDashboardIds}
refreshUnsavedDashboards={() =>
setUnsavedDashboardIds(dashboardSessionStorage.getDashboardIdsWithUnsavedChanges())
}
/>
</TableListView>
<>
{showNoDataPage && (
<DashboardAppNoDataPage onDataViewCreated={() => setShowNoDataPage(false)} />
)}
{!showNoDataPage && (
<TableListView
createItem={!showWriteControls ? undefined : createItem}
deleteItems={!showWriteControls ? undefined : deleteItems}
initialPageSize={initialPageSize}
editItem={!showWriteControls ? undefined : editItem}
initialFilter={initialFilter ?? defaultFilter}
toastNotifications={core.notifications.toasts}
headingId="dashboardListingHeading"
findItems={fetchItems}
rowHeader="title"
entityNamePlural={getEntityNamePlural()}
tableListTitle={getTableListTitle()}
tableCaption={getTableCaption()}
entityName={getEntityName()}
{...{
emptyPrompt,
searchFilters,
listingLimit,
tableColumns,
}}
theme={core.theme}
application={core.application}
>
<DashboardUnsavedListing
redirectTo={redirectTo}
unsavedDashboardIds={unsavedDashboardIds}
refreshUnsavedDashboards={() =>
setUnsavedDashboardIds(dashboardSessionStorage.getDashboardIdsWithUnsavedChanges())
}
/>
</TableListView>
)}
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { UrlForwardingStart } from '@kbn/url-forwarding-plugin/public';
import { embeddablePluginMock } from '@kbn/embeddable-plugin/public/mocks';
import { PluginInitializerContext, ScopedHistory } from '@kbn/core/public';
import { savedObjectsPluginMock } from '@kbn/saved-objects-plugin/public/mocks';
import { screenshotModePluginMock } from '@kbn/screenshot-mode-plugin/public/mocks';
import { visualizationsPluginMock } from '@kbn/visualizations-plugin/public/mocks';
import { PluginInitializerContext, ScopedHistory } from '@kbn/core/public';
import { screenshotModePluginMock } from '@kbn/screenshot-mode-plugin/public/mocks';
import { indexPatternEditorPluginMock } from '@kbn/data-view-editor-plugin/public/mocks';
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';

import { chromeServiceMock, coreMock, uiSettingsServiceMock } from '@kbn/core/public/mocks';
import { SavedObjectLoader, SavedObjectLoaderFindOptions } from '../../services/saved_objects';
import { DataViewsContract, SavedQueryService } from '../../services/data';
import { SavedQueryService } from '../../services/data';
import { DashboardAppServices, DashboardAppCapabilities } from '../../types';
import { NavigationPublicPluginStart } from '../../services/navigation';
import { getSavedDashboardMock } from './get_saved_dashboard_mock';
Expand Down Expand Up @@ -70,16 +73,17 @@ export function makeDefaultServices(): DashboardAppServices {

return {
screenshotModeService: screenshotModePluginMock.createSetupContract(),
dataViewEditor: indexPatternEditorPluginMock.createStartContract(),
visualizations: visualizationsPluginMock.createStartContract(),
savedObjects: savedObjectsPluginMock.createStartContract(),
embeddable: embeddablePluginMock.createInstance().doStart(),
uiSettings: uiSettingsServiceMock.createStartContract(),
dataViews: dataViewPluginMocks.createStartContract(),
chrome: chromeServiceMock.createStartContract(),
navigation: {} as NavigationPublicPluginStart,
savedObjectsClient: core.savedObjects.client,
dashboardCapabilities: defaultCapabilities,
data: dataPluginMock.createStartContract(),
dataViews: {} as DataViewsContract,
savedQueryService: {} as SavedQueryService,
scopedHistory: () => ({} as ScopedHistory),
setHeaderActionMenu: (mountPoint) => {},
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { VisualizationsStart } from '@kbn/visualizations-plugin/public';

import { replaceUrlHashQuery } from '@kbn/kibana-utils-plugin/public';
import { DataViewEditorStart } from '@kbn/data-view-editor-plugin/public';
import { createKbnUrlTracker } from './services/kibana_utils';
import { UsageCollectionSetup } from './services/usage_collection';
import { UiActionsSetup, UiActionsStart } from './services/ui_actions';
Expand Down Expand Up @@ -109,6 +110,7 @@ export interface DashboardStartDependencies {
spaces?: SpacesPluginStart;
visualizations: VisualizationsStart;
screenshotMode: ScreenshotModePluginStart;
dataViewEditor: DataViewEditorStart;
}

export interface DashboardSetup {
Expand Down
Loading