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

Connect Notifications APIs to Reporting #131

Merged
merged 9 commits into from
Jul 30, 2021
14 changes: 14 additions & 0 deletions dashboards-reports/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ export const OPENSEARCH_REPORTS_API = {
LIST_REPORT_DEFINITIONS: `${BASE_REPORTS_URI}/definitions`,
POLL_REPORT_INSTANCE: `${BASE_REPORTS_URI}/poll_instance`,
};

const REPORTING_NOTIFICATIONS_API_PREFIX = '/api/reporting_notifications';
export const REPORTING_NOTIFICATIONS_DASHBOARDS_API = Object.freeze({
GET_CONFIGS: `${REPORTING_NOTIFICATIONS_API_PREFIX}/get_configs`,
GET_EVENT: `${REPORTING_NOTIFICATIONS_API_PREFIX}/get_event`,
SEND_TEST_MESSAGE: `${REPORTING_NOTIFICATIONS_API_PREFIX}/test_message`
});

const NOTIFICATIONS_API_BASE_PATH = '/_plugins/_notifications';
export const NOTIFICATIONS_API = Object.freeze({
CONFIGS: `${NOTIFICATIONS_API_BASE_PATH}/configs`,
EVENTS: `${NOTIFICATIONS_API_BASE_PATH}/events`,
TEST_MESSAGE: `${NOTIFICATIONS_API_BASE_PATH}/feature/test`
});
23 changes: 18 additions & 5 deletions dashboards-reports/public/components/main/main_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,33 @@
import 'babel-polyfill';
import { i18n } from '@osd/i18n';
import { HttpFetchOptions, HttpSetup } from '../../../../../src/core/public';
import { placeholderChannels } from '../report_definitions/delivery/delivery_constants';

export const displayDeliveryChannels = (configIds: Array<string>) => {
export const displayDeliveryChannels = (configIds: Array<string>, channels: Array<{label: string, id: string}>) => {
let displayChannels = [];
for (let i = 0; i < configIds.length; ++i) {
for (let j = 0; j < placeholderChannels.length; ++j) {
if (configIds[i] === placeholderChannels[j].id) {
displayChannels.push(placeholderChannels[i].label);
for (let j = 0; j < channels.length; ++j) {
if (configIds[i] === channels[j].id) {
davidcui1225 marked this conversation as resolved.
Show resolved Hide resolved
displayChannels.push(channels[j].label);
break;
}
}
}
return displayChannels.toString();
}

export const getAvailableNotificationsChannels = (configList: any) => {
let availableChannels = [];
for (let i = 0; i < configList.length; ++i) {
let channelEntry = {};
channelEntry = {
label: configList[i].config.name,
id: configList[i].config_id
}
availableChannels.push(channelEntry);
}
return availableChannels;
}

type fileFormatsOptions = {
[key: string]: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

const { container } = render(
Expand Down Expand Up @@ -137,6 +138,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

const { container } = render(
Expand Down Expand Up @@ -191,6 +193,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

const { container } = render(
Expand Down Expand Up @@ -234,6 +237,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

const component = mount(
Expand Down Expand Up @@ -292,6 +296,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

const component = mount(
Expand Down Expand Up @@ -351,6 +356,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

httpClientMock.put = jest.fn().mockResolvedValue({});
Expand Down Expand Up @@ -412,6 +418,7 @@ describe('<ReportDefinitionDetails /> panel', () => {

httpClientMock.get = jest.fn().mockResolvedValue({
report_definition,
config_list: []
});

httpClientMock.put = jest.fn().mockResolvedValue({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
displayDeliveryChannels,
fileFormatsUpper,
generateReportFromDefinitionId,
getAvailableNotificationsChannels,
} from '../main_utils';
import { ReportDefinitionSchemaType } from '../../../../server/model';
import moment from 'moment';
Expand All @@ -62,7 +63,7 @@ import {
permissionsMissingActions,
} from '../../utils/utils';
import { GenerateReportLoadingModal } from '../loading_modal';
import { placeholderChannels } from '../../report_definitions/delivery/delivery_constants';
import { getChannelsQueryObject } from '../../report_definitions/delivery/delivery_constants';

const ON_DEMAND = 'On demand';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np: move constancts like this to a common place for easy reuse. e.g. constants.ts, or /common/index.ts


Expand Down Expand Up @@ -113,6 +114,7 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
const [toasts, setToasts] = useState([]);
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [showLoading, setShowLoading] = useState(false);
const [channels, setChannels] = useState<Array<{ label: string; id: string; }>>([]);
const reportDefinitionId = props.match['params']['reportDefinitionId'];

const handleLoading = (e: boolean | ((prevState: boolean) => boolean)) => {
Expand Down Expand Up @@ -410,19 +412,10 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
return scheduleDetails;
};

// const displayDeliveryChannels = (configIds: Array<string>) => {
// let displayChannels = [];
// for (let i = 0; i < configIds.length; ++i) {
// for (let j = 0; j < placeholderChannels.length; ++j) {
// if (configIds[i] === placeholderChannels[j].id) {
// displayChannels.push(placeholderChannels[i].label);
// }
// }
// }
// return displayChannels.toString();
// }

const getReportDefinitionDetailsMetadata = (data: ReportDefinitionSchemaType) : ReportDefinitionDetails => {
const getReportDefinitionDetailsMetadata = (
data: ReportDefinitionSchemaType,
availableChannels: Array<{ label: string; id: string; }>
) : ReportDefinitionDetails => {
const reportDefinition: ReportDefinitionSchemaType = data;
const {
report_params: reportParams,
Expand Down Expand Up @@ -485,7 +478,7 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
? humanReadableScheduleDetails(data.trigger)
: `\u2014`,
status: reportDefinition.status,
configIds: (configIds.length > 0) ? displayDeliveryChannels(configIds) : `\u2014`,
configIds: (configIds.length > 0) ? displayDeliveryChannels(configIds, availableChannels) : `\u2014`,
title: (title !== '') ? title : `\u2014`,
textDescription: (textDescription !== '') ? textDescription : `\u2014`,
htmlDescription: (htmlDescription !== '') ? htmlDescription : `\u2014`
Expand All @@ -496,10 +489,20 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
useEffect(() => {
const { httpClient } = props;
httpClient
.get('../api/reporting_notifications/get_configs', {
query: getChannelsQueryObject
})
.then(async (response: any) => {
let availableChannels = getAvailableNotificationsChannels(response.config_list);
setChannels(availableChannels);
return availableChannels;
})
.then((availableChannels: any) => {
httpClient
.get(`../api/reporting/reportDefinitions/${reportDefinitionId}`)
.then((response: {report_definition: ReportDefinitionSchemaType}) => {
handleReportDefinitionRawResponse(response);
handleReportDefinitionDetails(getReportDefinitionDetailsMetadata(response.report_definition));
handleReportDefinitionDetails(getReportDefinitionDetailsMetadata(response.report_definition, availableChannels));
props.setBreadcrumbs([
{
text: i18n.translate(
Expand Down Expand Up @@ -534,6 +537,7 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
);
handleDetailsErrorToast();
});
})
}, []);

const downloadIconDownload = async () => {
Expand Down Expand Up @@ -584,7 +588,7 @@ export function ReportDefinitionDetails(props: { match?: any; setBreadcrumbs?: a
updatedRawResponse.report_definition = updatedReportDefinition;
handleReportDefinitionRawResponse(updatedRawResponse);
setReportDefinitionDetails(
getReportDefinitionDetailsMetadata(updatedReportDefinition)
getReportDefinitionDetailsMetadata(updatedReportDefinition, channels)
);
if (statusChange === 'Enable') {
handleSuccessChangingScheduleStatusToast('enable');
Expand Down
Loading