Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Add Routes for Get Visualizations & Saved Search (#57)
Browse files Browse the repository at this point in the history
Added routes for getting all visualizations and saved searches, and merged all report source get APIs into one router
  • Loading branch information
davidcui1225 authored Sep 2, 2020
1 parent a4a758b commit 233a8f2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -166,6 +168,10 @@ export function ReportSettings(props) {
setDashboards(e);
};

const handleVisualizations = (e) => {
setVisualizations(e);
};

const handleReportName = (e: {
target: { value: React.SetStateAction<string> };
}) => {
Expand All @@ -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;
}
};

Expand All @@ -201,6 +211,8 @@ export function ReportSettings(props) {
target: { value: React.SetStateAction<string> };
}) => {
setVisualizationSourceSelect(e.target.value);
createReportDefinitionRequest['report_params']['url'] =
getVisualizationBaseUrl() + e.target.value;
};

const handleSavedSearchSelect = (e: {
Expand Down Expand Up @@ -328,7 +340,7 @@ export function ReportSettings(props) {
<EuiFormRow label="Select visualization">
<EuiSelect
id="reportSourceVisualizationSelect"
options={REPORT_SOURCE_VISUALIZATION_OPTIONS}
options={visualizations}
value={visualizationSourceSelect}
onChange={handleVisualizationSelect}
/>
Expand Down Expand Up @@ -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']
Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,58 @@ 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<IKibanaResponse<any | ResponseError>> => {
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,
});
} 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,
Expand Down
4 changes: 2 additions & 2 deletions kibana-reports/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ 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) {
registerVisualReportRoute(router);
registerReportDefinitionRoute(router);
registerDataReportMetadata(router);
registerDataReport(router);
registerDashboardRoute(router);
registerReportSourceRoute(router);
}
2 changes: 2 additions & 0 deletions kibana-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 233a8f2

Please sign in to comment.