From 233a8f24681a4ac6c3b58eb18cfa9179d03e219c Mon Sep 17 00:00:00 2001 From: David Cui <53581635+davidcui-amzn@users.noreply.github.com> Date: Wed, 2 Sep 2020 12:43:43 -0700 Subject: [PATCH] Add Routes for Get Visualizations & Saved Search (#57) Added routes for getting all visualizations and saved searches, and merged all report source get APIs into one router --- .../report_settings/report_settings.tsx | 51 ++++++++++++++++++- .../{getDashboards.ts => getReportSource.ts} | 40 ++++++++++++--- kibana-reports/server/routes/index.ts | 4 +- .../server/routes/utils/constants.ts | 2 + 4 files changed, 85 insertions(+), 12 deletions(-) rename kibana-reports/server/routes/{getDashboards.ts => getReportSource.ts} (56%) diff --git a/kibana-reports/public/components/report_definitions/report_settings/report_settings.tsx b/kibana-reports/public/components/report_definitions/report_settings/report_settings.tsx index 75648df2..7eb1d7ab 100644 --- a/kibana-reports/public/components/report_definitions/report_settings/report_settings.tsx +++ b/kibana-reports/public/components/report_definitions/report_settings/report_settings.tsx @@ -151,6 +151,8 @@ export function ReportSettings(props) { const [visualizationSourceSelect, setVisualizationSourceSelect] = useState( REPORT_SOURCE_VISUALIZATION_OPTIONS[0].value ); + const [visualizations, setVisualizations] = useState([]); + const [savedSearchSourceSelect, setSavedSearchSourceSelect] = useState( REPORT_SOURCE_SAVED_SEARCH_OPTIONS[0].value ); @@ -166,6 +168,10 @@ export function ReportSettings(props) { setDashboards(e); }; + const handleVisualizations = (e) => { + setVisualizations(e); + }; + const handleReportName = (e: { target: { value: React.SetStateAction }; }) => { @@ -184,8 +190,12 @@ export function ReportSettings(props) { setReportSourceId(e); if (e === 'dashboardReportSource') { createReportDefinitionRequest['report_source'] = 'Dashboard'; + createReportDefinitionRequest['report_params']['url'] = + getDashboardBaseUrl() + dashboards[0].value; } else if (e === 'visualizationReportSource') { createReportDefinitionRequest['report_source'] = 'Visualization'; + createReportDefinitionRequest['report_params']['url'] = + getVisualizationBaseUrl() + visualizations[0].value; } }; @@ -201,6 +211,8 @@ export function ReportSettings(props) { target: { value: React.SetStateAction }; }) => { setVisualizationSourceSelect(e.target.value); + createReportDefinitionRequest['report_params']['url'] = + getVisualizationBaseUrl() + e.target.value; }; const handleSavedSearchSelect = (e: { @@ -328,7 +340,7 @@ export function ReportSettings(props) { @@ -402,9 +414,31 @@ export function ReportSettings(props) { ); }; + const getVisualizationOptions = (data) => { + let index; + let options = []; + for (index = 0; index < data.length; ++index) { + let entry = { + value: data[index]['_id'].substring(14), + text: data[index]['_source']['visualization']['title'], + }; + options.push(entry); + } + return options; + }; + + const getVisualizationBaseUrl = () => { + let baseUrl = window.location.href; + return baseUrl.replace( + 'opendistro_kibana_reports#/create', + 'kibana#/visualize/edit/' + ); + }; + useEffect(() => { + // get dashboard options httpClientProps - .get('../api/reporting/getDashboards') + .get('../api/reporting/getReportSource/dashboard') .then(async (response) => { let dashboardOptions = getReportSettingDashboardOptions( response['hits']['hits'] @@ -420,6 +454,19 @@ export function ReportSettings(props) { }); createReportDefinitionRequest['report_params']['report_format'] = 'pdf'; createReportDefinitionRequest['report_source'] = 'Dashboard'; + + httpClientProps + .get('../api/reporting/getReportSource/visualization') + .then(async (response) => { + let visualizationOptions = getVisualizationOptions( + response['hits']['hits'] + ); + await handleVisualizations(visualizationOptions); + await setVisualizationSourceSelect(visualizationOptions[0].value); + }) + .catch((error) => { + console.log('error when fetching visualizations:', error); + }); }, []); return ( diff --git a/kibana-reports/server/routes/getDashboards.ts b/kibana-reports/server/routes/getReportSource.ts similarity index 56% rename from kibana-reports/server/routes/getDashboards.ts rename to kibana-reports/server/routes/getReportSource.ts index a1e3bca1..4a6291a8 100644 --- a/kibana-reports/server/routes/getDashboards.ts +++ b/kibana-reports/server/routes/getReportSource.ts @@ -21,26 +21,50 @@ import { import { API_PREFIX } from '../../common'; import { parseEsErrorResponse } from './utils/helpers'; import { RequestParams } from '@elastic/elasticsearch'; +import { schema } from '@kbn/config-schema'; +import { DEFAULT_MAX_SIZE } from './utils/constants'; export default function (router: IRouter) { router.get( { - path: `${API_PREFIX}/getDashboards`, - validate: {}, + path: `${API_PREFIX}/getReportSource/{reportSourceType}`, + validate: { + params: schema.object({ + reportSourceType: schema.string(), + }), + }, }, async ( context, request, response ): Promise> => { - const params: RequestParams.Search = { - index: '.kibana', - q: 'type:dashboard', - }; + let responseParams; + if (request.params.reportSourceType === 'dashboard') { + const params: RequestParams.Search = { + index: '.kibana', + q: 'type:dashboard', + }; + responseParams = params; + } else if (request.params.reportSourceType === 'visualization') { + const params: RequestParams.Search = { + index: '.kibana', + q: 'type:visualization', + size: DEFAULT_MAX_SIZE, + }; + responseParams = params; + } else if (request.params.reportSourceType === 'search') { + const params: RequestParams.Search = { + index: '.kibana', + q: 'type:search', + size: DEFAULT_MAX_SIZE, + }; + responseParams = params; + } try { const esResp = await context.core.elasticsearch.adminClient.callAsInternalUser( 'search', - params + responseParams ); return response.ok({ body: esResp, @@ -48,7 +72,7 @@ export default function (router: IRouter) { } catch (error) { //@ts-ignore context.reporting_plugin.logger.error( - `Failed to get reports details: ${error}` + `Failed to get reports source for ${request.params.reportSourceType}: ${error}` ); return response.custom({ statusCode: error.statusCode, diff --git a/kibana-reports/server/routes/index.ts b/kibana-reports/server/routes/index.ts index 99baf338..aa99d92b 100644 --- a/kibana-reports/server/routes/index.ts +++ b/kibana-reports/server/routes/index.ts @@ -17,7 +17,7 @@ import registerVisualReportRoute from './visualReport'; import registerReportDefinitionRoute from './reportDefinition'; import registerDataReport from './dataReport'; import registerDataReportMetadata from './dataReportMetadata'; -import registerDashboardRoute from './getDashboards'; +import registerReportSourceRoute from './getReportSource'; import { IRouter } from '../../../../src/core/server'; export default function (router: IRouter) { @@ -25,5 +25,5 @@ export default function (router: IRouter) { registerReportDefinitionRoute(router); registerDataReportMetadata(router); registerDataReport(router); - registerDashboardRoute(router); + registerReportSourceRoute(router); } diff --git a/kibana-reports/server/routes/utils/constants.ts b/kibana-reports/server/routes/utils/constants.ts index 25e7d3f7..3886a7c5 100644 --- a/kibana-reports/server/routes/utils/constants.ts +++ b/kibana-reports/server/routes/utils/constants.ts @@ -45,3 +45,5 @@ export enum SCHEDULE_TYPE { export enum DATA_REPORT_CONFIG { excelDateFormat = 'MM/DD/YYYY h:mm:ss a', } + +export const DEFAULT_MAX_SIZE = 10000; \ No newline at end of file