Skip to content

Commit

Permalink
Migrate Reporting Notebooks Integration (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: David Cui <davidcui@amazon.com>
  • Loading branch information
davidcui1225 authored Apr 29, 2021
1 parent 806c2c6 commit 047b54c
Show file tree
Hide file tree
Showing 14 changed files with 1,261 additions and 869 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ const dashboardHits = {
timeTo: 'now',
title: 'Mock Dashboard',
},
notebook: {
name: 'mock notebook name'
}
},
},
],
Expand All @@ -89,6 +92,9 @@ const visualizationHits = {
description: 'mock visualization value',
title: 'Mock Visualization',
},
notebook: {
name: 'mock notebook name'
},
},
},
],
Expand All @@ -102,6 +108,9 @@ const savedSearchHits = {
search: {
title: 'Mock saved search value',
},
notebook: {
name: 'mock notebook name'
},
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ import {
getDashboardBaseUrlCreate,
getDashboardOptions,
handleDataToVisualReportSourceChange,
getNotebooksOptions,
getNotebooksBaseUrlCreate,
} from './report_settings_helpers';
import { TimeRangeSelect } from './time_range';
import { converter } from '../utils';
Expand Down Expand Up @@ -110,6 +112,9 @@ export function ReportSettings(props: ReportSettingProps) {
const [savedSearchSourceSelect, setSavedSearchSourceSelect] = useState([] as any);
const [savedSearches, setSavedSearches] = useState([] as any);

const [notebooksSourceSelect, setNotebooksSourceSelect] = useState([] as any);
const [notebooks, setNotebooks] = useState([] as any);

const [fileFormat, setFileFormat] = useState('pdf');

const handleDashboards = (e) => {
Expand All @@ -124,6 +129,10 @@ export function ReportSettings(props: ReportSettingProps) {
setSavedSearches(e);
};

const handleNotebooks = (e) => {
setNotebooks(e);
}

const handleReportName = (e: {
target: { value: React.SetStateAction<string> };
}) => {
Expand Down Expand Up @@ -172,6 +181,15 @@ export function ReportSettings(props: ReportSettingProps) {
reportDefinitionRequest.report_params.core_params.report_format = 'csv';
reportDefinitionRequest.report_params.core_params.limit = 10000;
reportDefinitionRequest.report_params.core_params.excel = true;
} else if (e === 'notebooksReportSource') {
reportDefinitionRequest.report_params.report_source = 'Notebook';
reportDefinitionRequest.report_params.core_params.base_url =
getNotebooksBaseUrlCreate(edit, editDefinitionId, fromInContext) +
notebooks[0].value;

// set params to visual report params after switch from saved search
handleDataToVisualReportSourceChange(reportDefinitionRequest);
setFileFormat('pdf');
}
};

Expand Down Expand Up @@ -229,6 +247,22 @@ export function ReportSettings(props: ReportSettingProps) {
}
};

const handleNotebooksSelect = (e) => {
setNotebooksSourceSelect(e);
let fromInContext = false;
if (window.location.href.includes('?')) {
fromInContext = true;
}
if (e.length > 0) {
reportDefinitionRequest.report_params.core_params.base_url =
getNotebooksBaseUrlCreate(edit, editDefinitionId, fromInContext) +
e[0].value;
}
else {
reportDefinitionRequest.report_params.core_params.base_url = "";
}
}

const handleFileFormat = (e: React.SetStateAction<string>) => {
setFileFormat(e);
reportDefinitionRequest.report_params.core_params.report_format = e.toString();
Expand Down Expand Up @@ -532,6 +566,7 @@ export function ReportSettings(props: ReportSettingProps) {
dashboard: [],
visualizations: [],
savedSearch: [],
notebooks: []
};
reportDefinitionRequest.report_params.core_params.report_format = fileFormat;
await httpClientProps
Expand Down Expand Up @@ -573,6 +608,17 @@ export function ReportSettings(props: ReportSettingProps) {
.catch((error) => {
console.log('error when fetching saved searches:', error);
});

await httpClientProps
.get('../api/notebooks/')
.then(async (response: any) => {
let notebooksOptions = getNotebooksOptions(response.data);
reportSourceOptions.notebooks = notebooksOptions;
await handleNotebooks(notebooksOptions);
})
.catch((error) => {
console.log('error when fetching notebooks:', error);
});
return reportSourceOptions;
};

Expand Down Expand Up @@ -681,7 +727,41 @@ export function ReportSettings(props: ReportSettingProps) {
</div>
);

const displayNotebooksSelect =
reportSourceId === 'notebooksReportSource' ? (
<div>
<EuiFormRow
label="Select notebook"
isInvalid={showSettingsReportSourceError}
error={settingsReportSourceErrorMessage}
>
<EuiComboBox
id="reportSourceNotebooksSelect"
placeholder="Select a notebook"
singleSelection={{ asPlainText: true }}
options={notebooks}
onChange={handleNotebooksSelect}
selectedOptions={notebooksSourceSelect}
/>
</EuiFormRow>
<EuiSpacer />
</div>
): null;

const displayTimeRangeSelect =
reportSourceId != 'notebooksReportSource' ? (
<div>
<TimeRangeSelect
timeRange={timeRange}
reportDefinitionRequest={reportDefinitionRequest}
edit={edit}
id={editDefinitionId}
httpClientProps={httpClientProps}
showTimeRangeError={showTimeRangeError}
/>
<EuiSpacer />
</div>
): null;

return (
<EuiPageContent panelPaddingSize={'l'}>
Expand Down Expand Up @@ -736,15 +816,17 @@ export function ReportSettings(props: ReportSettingProps) {
{displayDashboardSelect}
{displayVisualizationSelect}
{displaySavedSearchSelect}
<TimeRangeSelect
{/* <TimeRangeSelect
timeRange={timeRange}
reportDefinitionRequest={reportDefinitionRequest}
edit={edit}
id={editDefinitionId}
httpClientProps={httpClientProps}
showTimeRangeError={showTimeRangeError}
/>
<EuiSpacer />
<EuiSpacer /> */}
{displayNotebooksSelect}
{displayTimeRangeSelect}
{displayVisualReportsFormatAndMarkdown}
</EuiPageContentBody>
</EuiPageContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const REPORT_SOURCE_RADIOS = [
id: 'savedSearchReportSource',
label: 'Saved search',
},
{
id: 'notebooksReportSource',
label: 'Notebook'
}
];

export const PDF_PNG_FILE_FORMAT_OPTIONS = [
Expand Down Expand Up @@ -75,6 +79,7 @@ export const REPORT_SOURCE_TYPES = {
dashboard: 'Dashboard',
visualization: 'Visualization',
savedSearch: 'Saved search',
notebook: 'Notebook'
};

export const commonTimeRanges = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ export const getSavedSearchBaseUrlCreate = (
);
};

export const getNotebooksBaseUrlCreate = (
edit: boolean,
editDefinitionId: string,
fromInContext: boolean
) => {
let baseUrl;
if (!fromInContext) {
baseUrl = location.pathname + location.hash;
} else {
baseUrl = '/app/notebooks-dashboards#/';
}
if (edit) {
return baseUrl.replace(
`reports-dashboards#/edit/${editDefinitionId}`,
'notebooks-dashboards#/'
);
} else if (fromInContext) {
return baseUrl;
}
return baseUrl.replace(
'reports-dashboards#/create',
'notebooks-dashboards#/'
);
}

export const getDashboardOptions = (data) => {
let index;
let dashboard_options = [];
Expand Down Expand Up @@ -150,6 +175,19 @@ export const getSavedSearchOptions = (data: string | any[]) => {
return options;
};

export const getNotebooksOptions = (data: any) => {
let index;
let options = [];
for (index = 0; index < data.length; ++index) {
let entry = {
value: data[index]['id'],
label: data[index]['path']
}
options.push(entry);
}
return options;
}

export const handleDataToVisualReportSourceChange = (
reportDefinitionRequest
) => {
Expand Down
3 changes: 3 additions & 0 deletions dashboards-reports/server/model/backendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export enum BACKEND_REPORT_SOURCE {
dashboard = 'Dashboard',
visualization = 'Visualization',
savedSearch = 'SavedSearch',
notebook = 'Notebook'
}

export enum BACKEND_REPORT_STATE {
Expand Down Expand Up @@ -143,6 +144,7 @@ export const REPORT_SOURCE_DICT = {
[REPORT_TYPE.dashboard]: BACKEND_REPORT_SOURCE.dashboard,
[REPORT_TYPE.visualization]: BACKEND_REPORT_SOURCE.visualization,
[REPORT_TYPE.savedSearch]: BACKEND_REPORT_SOURCE.savedSearch,
[REPORT_TYPE.notebook]: BACKEND_REPORT_SOURCE.notebook
};

export const REPORT_FORMAT_DICT = {
Expand All @@ -166,4 +168,5 @@ export const URL_PREFIX_DICT = {
[BACKEND_REPORT_SOURCE.dashboard]: '/app/dashboards#/view/',
[BACKEND_REPORT_SOURCE.savedSearch]: '/app/discover#/view/',
[BACKEND_REPORT_SOURCE.visualization]: '/app/visualize#/edit/',
[BACKEND_REPORT_SOURCE.notebook]: '/app/notebooks-dashboards#/'
};
1 change: 1 addition & 0 deletions dashboards-reports/server/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const reportParamsSchema = schema.object({
schema.literal(REPORT_TYPE.dashboard),
schema.literal(REPORT_TYPE.visualization),
schema.literal(REPORT_TYPE.savedSearch),
schema.literal(REPORT_TYPE.notebook)
]),
description: schema.string(),
core_params: schema.conditional(
Expand Down
2 changes: 1 addition & 1 deletion dashboards-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const createReport = async (
isScheduledTask
);
} else {
// report source can only be one of [saved search, visualization, dashboard]
// report source can only be one of [saved search, visualization, dashboard, notebook]
// compose url
const relativeUrl = report.query_url.startsWith(basePath)
? report.query_url
Expand Down
3 changes: 1 addition & 2 deletions dashboards-reports/server/routes/reportSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ export default function (router: IRouter) {
size: DEFAULT_MAX_SIZE,
};
responseParams = params;
}
try {
} try {
const opensearchResp = await context.core.opensearch.legacy.client.callAsCurrentUser(
'search',
responseParams
Expand Down
26 changes: 26 additions & 0 deletions dashboards-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export enum REPORT_TYPE {
savedSearch = 'Saved search',
dashboard = 'Dashboard',
visualization = 'Visualization',
notebook = 'Notebook'
}

export enum DATA_REPORT_CONFIG {
Expand All @@ -79,6 +80,7 @@ export enum DELIVERY_TYPE {
export enum SELECTOR {
dashboard = '#dashboardViewport',
visualization = '.visEditor__content',
notebook = '.euiPageBody'
}

// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-from-size.html
Expand Down Expand Up @@ -167,6 +169,18 @@ export const GLOBAL_BASIC_COUNTER: CountersType = {
},
},
},
notebook: {
pdf: {
download: {
count: 0,
},
},
png: {
download: {
count: 0,
},
},
},
saved_search: {
csv: {
download: {
Expand Down Expand Up @@ -262,6 +276,18 @@ export const DEFAULT_ROLLING_COUNTER: CountersType = {
},
},
},
notebook: {
pdf: {
download: {
count: 0,
},
},
png: {
download: {
count: 0,
},
},
},
saved_search: {
csv: {
download: {
Expand Down
2 changes: 1 addition & 1 deletion dashboards-reports/server/routes/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface CreateReportResultType {
fileName: string;
}

type ReportSourceType = 'dashboard' | 'visualization' | 'saved_search';
type ReportSourceType = 'dashboard' | 'visualization' | 'saved_search' | 'notebook';
type ReportFormatType = 'pdf' | 'png' | 'csv';
type UsageActionType = 'download';
export type EntityType = 'report' | 'report_definition' | 'report_source';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export const createVisualReport = async (
visible: true,
});
break;
case REPORT_TYPE.notebook:
await page.waitForSelector(SELECTOR.notebook, {
visible: true,
});
break;
default:
throw Error(
`report source can only be one of [Dashboard, Visualization]`
Expand Down
Loading

0 comments on commit 047b54c

Please sign in to comment.