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

Change Delivery Request Body for Notifications #85

Merged
merged 5 commits into from
Jun 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,10 @@ export function ReportDefinitionDetails(props) {
trigger_params: triggerParams,
} = trigger;
const {
delivery_type: deliveryType,
delivery_params: deliveryParams,
configIds: configIds,
title: title,
textDescription: textDescription,
htmlDescription: htmlDescription
} = delivery;
const {
core_params: {
Expand Down Expand Up @@ -357,17 +359,11 @@ export function ReportDefinitionDetails(props) {
scheduleDetails: triggerParams
? humanReadableScheduleDetails(data.report_definition.trigger)
: `\u2014`,
channel: deliveryType,
status: reportDefinition.status,
opensearchDashboardsRecipients: deliveryParams.opensearch_dashboards_recipients
? deliveryParams.opensearch_dashboards_recipients
: `\u2014`,
emailRecipients:
deliveryType === 'Channel' ? deliveryParams.recipients : `\u2014`,
emailSubject:
deliveryType === 'Channel' ? deliveryParams.title : `\u2014`,
emailBody:
deliveryType === 'Channel' ? deliveryParams.textDescription : `\u2014`,
configIds: configIds,
title: title,
textDescription: textDescription,
htmlDescription: htmlDescription
};
return reportDefinitionDetails;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ interface triggerType {
}

interface deliveryType {
delivery_type: string;
delivery_params: any;
configIds: Array<string>;
title: string;
textDescription: string;
htmlDescription: string;
}

export interface TriggerParamsType {
Expand Down Expand Up @@ -122,8 +124,10 @@ export function CreateReport(props) {
},
},
delivery: {
delivery_type: '',
delivery_params: {},
configIds: [],
title: '',
textDescription: '',
htmlDescription: ''
},
trigger: {
trigger_type: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ export function ReportDelivery(props: ReportDeliveryProps) {

const defaultCreateDeliveryParams = () => {
reportDefinitionRequest.delivery = {
delivery_type: DELIVERY_TYPE_OPTIONS[0].id,
delivery_params: { opensearch_dashboards_recipients: [] },
configIds: [],
title: '',
textDescription: '',
htmlDescription: ''
};
};

Expand Down
4 changes: 1 addition & 3 deletions dashboards-reports/server/model/backendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ export type IntervalType = {
};

export type DeliveryType = {
recipients: string[];
deliveryFormat: BACKEND_DELIVERY_FORMAT;
configIds: string[];
title: string;
textDescription: string;
htmlDescription?: string;
channelIds?: string[];
};

export enum BACKEND_DELIVERY_FORMAT {
Expand Down
14 changes: 4 additions & 10 deletions dashboards-reports/server/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,10 @@ export const triggerSchema = schema.object({
});

export const deliverySchema = schema.object({
delivery_type: schema.oneOf([
schema.literal(DELIVERY_TYPE.opensearchDashboardsUser),
schema.literal(DELIVERY_TYPE.channel),
]),
delivery_params: schema.conditional(
schema.siblingRef('delivery_type'),
DELIVERY_TYPE.opensearchDashboardsUser,
opensearchDashboardsUserSchema,
channelSchema
),
configIds: schema.arrayOf(schema.string()),
title: schema.string(),
textDescription: schema.string(),
htmlDescription: schema.string()
});

export const reportParamsSchema = schema.object({
Expand Down
4 changes: 0 additions & 4 deletions dashboards-reports/server/routes/reportDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,22 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
fromIndex: number;
maxItems: number;
};

try {
// @ts-ignore
const opensearchReportsClient: ILegacyScopedClusterClient = context.reporting_plugin.opensearchReportsClient.asScoped(
request
);

const opensearchResp = await opensearchReportsClient.callAsCurrentUser(
'opensearch_reports.getReportDefinitions',
{
fromIndex: fromIndex,
maxItems: maxItems || DEFAULT_MAX_SIZE,
}
);

const reportDefinitionsList = backendToUiReportDefinitionsList(
opensearchResp.reportDefinitionDetailsList,
basePath
);

return response.ok({
body: {
data: reportDefinitionsList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ const input = {
},
},
delivery: {
delivery_type: 'OpenSearch Dashboards user',
delivery_params: {
opensearch_dashboards_recipients: [],
},
configIds: [],
title: 'title',
textDescription: 'text description',
htmlDescription: 'html description'
},
trigger: {
trigger_type: 'On demand',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ const input = {
},
},
delivery: {
delivery_type: 'OpenSearch Dashboards user',
delivery_params: {
opensearch_dashboards_recipients: [],
},
configIds: [],
title: 'title',
textDescription: 'text description',
htmlDescription: 'html description'
},
trigger: {
trigger_type: 'On demand',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ const input: BackendReportInstanceType = {
},
},
delivery: {
recipients: ['szhongna@amazon.com'],
deliveryFormat: BACKEND_DELIVERY_FORMAT.embedded,
title: 'test email subject',
textDescription: '- test\n- optional\n- message',
htmlDescription:
'<ul>\n<li>test</li>\n<li>optional</li>\n<li>message</li>\n</ul>',
channelIds: [],
configIds: [],
},
},
},
Expand Down Expand Up @@ -129,15 +127,11 @@ const output = {
},
},
delivery: {
delivery_type: 'Channel',
delivery_params: {
recipients: ['szhongna@amazon.com'],
title: 'test email subject',
textDescription: '- test\n- optional\n- message',
htmlDescription:
'<ul>\n<li>test</li>\n<li>optional</li>\n<li>message</li>\n</ul>',
channelIds: [],
},
title: 'test email subject',
textDescription: '- test\n- optional\n- message',
htmlDescription:
'<ul>\n<li>test</li>\n<li>optional</li>\n<li>message</li>\n</ul>',
configIds: [],
},
time_created: 1605056426053,
last_updated: 1605056426053,
Expand Down
17 changes: 6 additions & 11 deletions dashboards-reports/server/routes/utils/converters/backendToUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export const backendToUiReportDefinition = (

const baseUrl = getBaseUrl(sourceType, sourceId);
const reportSource = getUiReportSource(sourceType);

let uiReportDefinition: ReportDefinitionSchemaType = {
report_params: {
report_name: name,
Expand Down Expand Up @@ -175,7 +174,6 @@ export const backendToUiReportDefinition = (
last_updated: lastUpdatedTimeMs,
status: getUiReportDefinitionStatus(isEnabled),
};

// validate to assign default values to some fields for UI model
uiReportDefinition = reportDefinitionSchema.validate(uiReportDefinition);
uiReportDefinition.report_params.core_params.base_url =
Expand Down Expand Up @@ -370,20 +368,17 @@ const getUiDeliveryParams = (
delivery: DeliveryType | undefined
): DeliverySchemaType => {
const opensearchDashboardsUserDeliveryParams = {
delivery_type: DELIVERY_TYPE.opensearchDashboardsUser,
delivery_params: {
opensearch_dashboards_recipients: [],
},
configIds: [],
title: '',
textDescription: '',
htmlDescription: ''
};

let params: any;
if (delivery) {
const { deliveryFormat, ...rest } = delivery;
const { ...rest } = delivery;
params = {
delivery_type: DELIVERY_TYPE.channel,
delivery_params: {
...rest,
},
...rest
};
} else {
params = opensearchDashboardsUserDeliveryParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const createReportDefinitionInput: ReportDefinitionSchemaType = {
},
},
delivery: {
delivery_type: DELIVERY_TYPE.opensearchDashboardsUser,
delivery_params: {
opensearch_dashboards_recipients: [],
},
configIds: [],
title: 'title',
textDescription: 'text description',
htmlDescription: 'html description'
},
trigger: {
trigger_type: TRIGGER_TYPE.onDemand,
Expand Down Expand Up @@ -80,10 +80,10 @@ const createReportDefinitionNotebookInput: ReportDefinitionSchemaType = {
},
},
delivery: {
delivery_type: DELIVERY_TYPE.opensearchDashboardsUser,
delivery_params: {
opensearch_dashboards_recipients: [],
},
configIds: [],
title: 'title',
textDescription: 'text description',
htmlDescription: 'html description'
},
trigger: {
trigger_type: TRIGGER_TYPE.onDemand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,12 @@ internal data class ReportDefinition(
* Report definition delivery data class
*/
internal data class Delivery(
val recipients: List<String>,
val deliveryFormat: DeliveryFormat,
val title: String,
val textDescription: String,
val htmlDescription: String?,
val channelIds: List<String>
val configIds: List<String>
) : ToXContentObject {
internal companion object {
private const val RECIPIENTS_TAG = "recipients"
private const val DELIVERY_FORMAT_TAG = "deliveryFormat"
private const val TITLE_TAG = "title"
private const val TEXT_DESCRIPTION_TAG = "textDescription"
Expand All @@ -403,34 +400,29 @@ internal data class ReportDefinition(
*/
fun parse(parser: XContentParser): Delivery {
var recipients: List<String> = listOf()
var deliveryFormat: DeliveryFormat? = null
var title: String? = null
var textDescription: String? = null
var htmlDescription: String? = null
var channelIds: List<String> = listOf()
var configIds: List<String> = listOf()
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser)
while (XContentParser.Token.END_OBJECT != parser.nextToken()) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
RECIPIENTS_TAG -> recipients = parser.stringList()
DELIVERY_FORMAT_TAG -> deliveryFormat = DeliveryFormat.valueOf(parser.text())
TITLE_TAG -> title = parser.text()
TEXT_DESCRIPTION_TAG -> textDescription = parser.text()
HTML_DESCRIPTION_TAG -> htmlDescription = parser.textOrNull()
CHANNEL_IDS_TAG -> channelIds = parser.stringList()
CHANNEL_IDS_TAG -> configIds = parser.stringList()
else -> log.info("$LOG_PREFIX: Delivery Unknown field $fieldName")
}
}
deliveryFormat ?: throw IllegalArgumentException("$DELIVERY_FORMAT_TAG field absent")
title ?: throw IllegalArgumentException("$TITLE_TAG field absent")
textDescription ?: throw IllegalArgumentException("$TEXT_DESCRIPTION_TAG field absent")
return Delivery(recipients,
deliveryFormat,
return Delivery(
title,
textDescription,
htmlDescription,
channelIds)
configIds)
}
}

Expand All @@ -440,14 +432,12 @@ internal data class ReportDefinition(
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
builder.startObject()
.field(RECIPIENTS_TAG, recipients)
.field(DELIVERY_FORMAT_TAG, deliveryFormat)
.field(TITLE_TAG, title)
.field(TEXT_DESCRIPTION_TAG, textDescription)
if (htmlDescription != null) {
builder.field(HTML_DESCRIPTION_TAG, htmlDescription)
}
builder.field(CHANNEL_IDS_TAG, channelIds)
builder.field(CHANNEL_IDS_TAG, configIds)
builder.endObject()
return builder
}
Expand Down