Skip to content

Commit

Permalink
Refactor GChat and Teams message for template data handling for DQ_Te…
Browse files Browse the repository at this point in the history
…mplate_Section and General_Template_Section.
  • Loading branch information
Siddhanttimeline committed Sep 30, 2024
1 parent 43a6c97 commit b244736
Show file tree
Hide file tree
Showing 3 changed files with 786 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.service.util.email.EmailUtil.getSmtpSettings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.openmetadata.schema.type.ChangeEvent;
import org.openmetadata.service.apps.bundles.changeEvent.gchat.GChatMessage;
import org.openmetadata.service.apps.bundles.changeEvent.gchat.GChatMessage.*;
Expand Down Expand Up @@ -106,33 +108,68 @@ private GChatMessage getGChatTestMessage(String publisherName) {

public GChatMessage createGeneralChangeEventMessage(
String publisherName, ChangeEvent event, OutgoingMessage outgoingMessage) {
Header header = new Header("Change Event Details", "https://imgur.com/kOOPEG4.png", "IMAGE");

List<Widget> detailsWidgets =
List.of(
createWidget("Event Type:", event.getEventType().value()),
createWidget("Updated By:", event.getUserName()),
createWidget("Entity Type:", event.getEntityType()),
createWidget("Publisher:", publisherName),
createWidget("Time:", new Date(event.getTimestamp()).toString()));
Map<General_Template_Section, Map<Enum<?>, Object>> data =
buildGeneralTemplateData(publisherName, event, outgoingMessage);

Map<Enum<?>, Object> eventDetails = data.get(General_Template_Section.EVENT_DETAILS);

Header header = createHeader();

List<Widget> additionalMessageWidgets =
outgoingMessage.getMessages().stream()
.map(message -> new Widget(new TextParagraph(message)))
.toList();

Section detailsSection = new Section(detailsWidgets);
Section detailsSection = new Section(createEventDetailsWidgets(eventDetails));
Section messageSection = new Section(additionalMessageWidgets);
Section fqnSection =
new Section(List.of(createWidget("FQN:", getFQNForChangeEventEntity(event))));
Section footerSection =
new Section(List.of(new Widget(new TextParagraph("Change Event By OpenMetadata"))));
new Section(
List.of(
createWidget(
"FQN:",
String.valueOf(eventDetails.getOrDefault(EventDetailsKeys.ENTITY_FQN, "-")))));
Section footerSection = createFooterSection();

Card card =
new Card(header, List.of(detailsSection, fqnSection, messageSection, footerSection));
return new GChatMessage(List.of(card));
}

private Map<General_Template_Section, Map<Enum<?>, Object>> buildGeneralTemplateData(
String publisherName, ChangeEvent event, OutgoingMessage outgoingMessage) {

TemplateDataBuilder<General_Template_Section> builder = new TemplateDataBuilder<>();
builder
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.EVENT_TYPE,
event.getEventType().value())
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.UPDATED_BY,
event.getUserName())
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.ENTITY_TYPE,
event.getEntityType())
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.ENTITY_FQN,
getFQNForChangeEventEntity(event))
.add(General_Template_Section.EVENT_DETAILS, EventDetailsKeys.PUBLISHER, publisherName)
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.TIME,
new Date(event.getTimestamp()).toString())
.add(
General_Template_Section.EVENT_DETAILS,
EventDetailsKeys.OUTGOING_MESSAGE,
outgoingMessage);

return builder.build();
}

public GChatMessage createConnectionTestMessage(String publisherName) {
Header header =
new Header("Connection Successful \u2705", "https://imgur.com/kOOPEG4.png", "IMAGE");
Expand All @@ -144,19 +181,278 @@ public GChatMessage createConnectionTestMessage(String publisherName) {
new TextParagraph(
"This is a Test Message, receiving this message confirms that you have successfully configured OpenMetadata to receive alerts."));

Widget footerWidget = new Widget(new TextParagraph("Change Event By OpenMetadata."));

Section publisherSection = new Section(List.of(publisherWidget));
Section descriptionSection = new Section(List.of(descriptionWidget));
Section footerSection = new Section(List.of(footerWidget));
Section footerSection = createFooterSection();

Card card =
new Card(header, Arrays.asList(publisherSection, descriptionSection, footerSection));

return new GChatMessage(List.of(card));
}

public GChatMessage createDQTemplate(
String publisherName, ChangeEvent event, OutgoingMessage outgoingMessage) {

// todo complete buildDQTemplateData fn
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData =
buildDQTemplateData(publisherName, event, outgoingMessage);

List<Section> sections = new ArrayList<>();
Header header = createHeader();

addChangeEventDetailsSection(templateData, sections);
addOutgoingMessageSection(templateData, sections);
addTestCaseDetailsSection(templateData, sections);
addTestCaseFQNSection(templateData, sections);
addTestCaseResultSection(templateData, sections);
addInspectionQuerySection(templateData, sections);
addTestDefinitionSection(templateData, sections);
addSampleDataSection(templateData, sections);

sections.add(createFooterSection());

// Create the card with all sections
Card card = new Card(header, sections);
return new GChatMessage(List.of(card));
}

// todo complete buildDQTemplateData fn
private Map<DQ_Template_Section, Map<Enum<?>, Object>> buildDQTemplateData(
String publisherName, ChangeEvent event, OutgoingMessage outgoingMessage) {

TemplateDataBuilder<DQ_Template_Section> builder = new TemplateDataBuilder<>();
builder
.add(
DQ_Template_Section.EVENT_DETAILS,
EventDetailsKeys.EVENT_TYPE,
event.getEventType().value())
.add(DQ_Template_Section.EVENT_DETAILS, EventDetailsKeys.UPDATED_BY, event.getUserName())
.add(DQ_Template_Section.EVENT_DETAILS, EventDetailsKeys.ENTITY_TYPE, event.getEntityType())
.add(
DQ_Template_Section.EVENT_DETAILS,
EventDetailsKeys.ENTITY_FQN,
getFQNForChangeEventEntity(event))
.add(DQ_Template_Section.EVENT_DETAILS, EventDetailsKeys.PUBLISHER, publisherName)
.add(
DQ_Template_Section.EVENT_DETAILS,
EventDetailsKeys.TIME,
new Date(event.getTimestamp()).toString())
.add(DQ_Template_Section.EVENT_DETAILS, EventDetailsKeys.OUTGOING_MESSAGE, outgoingMessage);

return builder.build();
}

private void addChangeEventDetailsSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.EVENT_DETAILS)) {
Map<Enum<?>, Object> eventDetails = templateData.get(DQ_Template_Section.EVENT_DETAILS);
if (!nullOrEmpty(eventDetails)) {
sections.add(new Section(createEventDetailsWidgets(eventDetails)));
}
}
}

private void addOutgoingMessageSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.EVENT_DETAILS)) {
Map<Enum<?>, Object> outgoingMessage = templateData.get(DQ_Template_Section.EVENT_DETAILS);

if (!nullOrEmpty(outgoingMessage)) {
List<Widget> outgoingMessageWidget =
List.of(
createWidget("Summary:", ""),
createWidget(
"",
String.valueOf(
outgoingMessage.getOrDefault(EventDetailsKeys.OUTGOING_MESSAGE, "-"))));

sections.add(new Section(outgoingMessageWidget));
}
}
}

private void addTestCaseDetailsSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.TEST_CASE_DETAILS)) {
Map<Enum<?>, Object> testCaseDetails =
templateData.get(DQ_Template_Section.TEST_CASE_DETAILS);

if (testCaseDetails != null) {
List<Widget> testCaseDetailsWidgets =
List.of(
createWidget("TEST CASE", ""),
createWidget(
"ID:",
String.valueOf(testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.ID, "-"))),
createWidget(
"Name:",
String.valueOf(testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.NAME, "-"))),
createWidget(
"Owners:",
String.valueOf(
testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.OWNERS, "-"))),
createWidget(
"Tags:",
String.valueOf(
testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.TAGS, "-"))));

sections.add(new Section(testCaseDetailsWidgets));
}
}
}

private void addTestCaseFQNSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.TEST_CASE_DETAILS)) {
Map<Enum<?>, Object> testCaseDetails =
templateData.get(DQ_Template_Section.TEST_CASE_DETAILS);

if (!nullOrEmpty(testCaseDetails)
&& testCaseDetails.containsKey(DQ_TestCaseDetailsKeys.TEST_CASE_FQN)) {
Widget testCaseFQNWidget =
createWidget(
"Test Case FQN:",
String.valueOf(
testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.TEST_CASE_FQN, "-")));
sections.add(new Section(List.of(testCaseFQNWidget)));
}
}
}

private void addTestCaseResultSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.TEST_CASE_RESULT)) {
Map<Enum<?>, Object> testCaseResult = templateData.get(DQ_Template_Section.TEST_CASE_RESULT);

if (!nullOrEmpty(testCaseResult)) {
List<Widget> statusParameterWidgets = new ArrayList<>();
statusParameterWidgets.add(createWidget("TEST CASE RESULT", ""));

statusParameterWidgets.add(
createWidget(
"Status:",
String.valueOf(testCaseResult.getOrDefault(DQ_TestCaseResultKeys.STATUS, "-"))));

statusParameterWidgets.add(
createWidget(
"Parameter Value:",
String.valueOf(
testCaseResult.getOrDefault(DQ_TestCaseResultKeys.PARAMETER_VALUE, "-"))));

statusParameterWidgets.add(
createWidget(
"Result Message:",
String.valueOf(
testCaseResult.getOrDefault(DQ_TestCaseResultKeys.RESULT_MESSAGE, "-"))));

sections.add(new Section(statusParameterWidgets));
}
}
}

private void addInspectionQuerySection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {

if (templateData.containsKey(DQ_Template_Section.TEST_CASE_DETAILS)) {
Map<Enum<?>, Object> testCaseDetails =
templateData.get(DQ_Template_Section.TEST_CASE_DETAILS);

if (!nullOrEmpty(testCaseDetails)
&& testCaseDetails.containsKey(DQ_TestCaseDetailsKeys.INSPECTION_QUERY)) {
Widget inspectionQueryWidget =
createWidget(
"Inspection Query",
String.valueOf(
testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.INSPECTION_QUERY, "-")));

sections.add(new Section(List.of(inspectionQueryWidget)));
}
}
}

private void addTestDefinitionSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {

if (templateData.containsKey(DQ_Template_Section.TEST_DEFINITION)) {
Map<Enum<?>, Object> testDefinition = templateData.get(DQ_Template_Section.TEST_DEFINITION);

if (!nullOrEmpty(testDefinition)) {
List<Widget> testDefinitionWidgets = new ArrayList<>();
testDefinitionWidgets.add(createWidget("TEST DEFINITION", ""));
testDefinitionWidgets.add(
createWidget(
"Name:",
String.valueOf(
testDefinition.getOrDefault(DQ_TestDefinitionKeys.TEST_DEFINITION_NAME, "-"))));
testDefinitionWidgets.add(
createWidget(
"Description:",
String.valueOf(
testDefinition.getOrDefault(
DQ_TestDefinitionKeys.TEST_DEFINITION_DESCRIPTION, "-"))));

sections.add(new Section(testDefinitionWidgets));
}
}
}

private void addSampleDataSection(
Map<DQ_Template_Section, Map<Enum<?>, Object>> templateData, List<Section> sections) {
if (templateData.containsKey(DQ_Template_Section.TEST_CASE_DETAILS)) {
Map<Enum<?>, Object> testCaseDetails =
templateData.get(DQ_Template_Section.TEST_CASE_DETAILS);

if (!nullOrEmpty(testCaseDetails)) {
Widget sampleDataWidget =
createWidget(
"Sample Data:",
String.valueOf(
testCaseDetails.getOrDefault(DQ_TestCaseDetailsKeys.SAMPLE_DATA, "-")));

sections.add(new Section(List.of(sampleDataWidget)));
}
}
}

private List<Widget> createEventDetailsWidgets(Map<Enum<?>, Object> detailsMap) {
List<Widget> widgets = new ArrayList<>();
if (detailsMap.containsKey(EventDetailsKeys.EVENT_TYPE)) {
widgets.add(
createWidget("Event Type:", String.valueOf(detailsMap.get(EventDetailsKeys.EVENT_TYPE))));
}
if (detailsMap.containsKey(EventDetailsKeys.UPDATED_BY)) {
widgets.add(
createWidget("Updated By:", String.valueOf(detailsMap.get(EventDetailsKeys.UPDATED_BY))));
}
if (detailsMap.containsKey(EventDetailsKeys.ENTITY_TYPE)) {
widgets.add(
createWidget(
"Entity Type:", String.valueOf(detailsMap.get(EventDetailsKeys.ENTITY_TYPE))));
}
if (detailsMap.containsKey(EventDetailsKeys.PUBLISHER)) {
widgets.add(
createWidget("Publisher:", String.valueOf(detailsMap.get(EventDetailsKeys.PUBLISHER))));
}
if (detailsMap.containsKey(EventDetailsKeys.TIME)) {
widgets.add(createWidget("Time:", String.valueOf(detailsMap.get(EventDetailsKeys.TIME))));
}

return widgets;
}

// Helper Method to create widgets
private Widget createWidget(String label, String content) {
return new Widget(new TextParagraph(String.format(getBoldWithSpace(), label) + content));
}

// Helper Method to create header section
private Header createHeader() {
return new Header("Change Event Details", "https://imgur.com/kOOPEG4.png", "IMAGE");
}

// Helper Method to create footer section
private Section createFooterSection() {
return new Section(List.of(new Widget(new TextParagraph("Change Event By OpenMetadata"))));
}
}
Loading

0 comments on commit b244736

Please sign in to comment.