You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kylin.apache.org by GitBox <gi...@apache.org> on 2021/04/13 09:03:41 UTC

[GitHub] [kylin] yanghua commented on a change in pull request #1596: [KYLIN-4780] support dingtalk notify

yanghua commented on a change in pull request #1596:
URL: https://github.com/apache/kylin/pull/1596#discussion_r612261129



##########
File path: core-job/src/main/java/org/apache/kylin/job/execution/AbstractExecutable.java
##########
@@ -311,76 +317,59 @@ public final void setSubmitter(String submitter) {
         setParam(SUBMITTER, submitter);
     }
 
-    public final List<String> getNotifyList() {
-        final String str = getParam(NOTIFY_LIST);
+    public final List<String> getNotificationList(String key) {
+        final String str = getParam(key);
         if (str != null) {
             return Lists.newArrayList(StringUtils.split(str, ","));
         } else {
             return Collections.emptyList();
         }
     }
 
-    public final void setNotifyList(String notifications) {
-        setParam(NOTIFY_LIST, notifications);
+    public final void setNotificationList(String key, String notifications) {
+        setParam(key, notifications);
     }
 
-    public final void setNotifyList(List<String> notifications) {
-        setNotifyList(StringUtils.join(notifications, ","));
+    public final void setNotificationList(String key, List<String> notifications) {
+        setNotificationList(key, StringUtils.join(notifications, ","));
     }
 
-    protected Pair<String, String> formatNotifications(ExecutableContext executableContext, ExecutableState state) {
+    protected Pair<String[], Map<String, Object>>  formatNotifications(ExecutableContext executableContext, ExecutableState state) {
         return null;
     }
 
     protected final void notifyUserStatusChange(ExecutableContext context, ExecutableState state) {
         try {
-            List<String> users = getAllNofifyUsers(config);
+            Map<String, List<String>> users = getAllNofificationUsers(config);
             if (users.isEmpty()) {
                 logger.debug(NO_NEED_TO_SEND_EMAIL_USER_LIST_IS_EMPTY);
                 return;
             }
-            final Pair<String, String> email = formatNotifications(context, state);
-            doSendMail(config, users, email);
+            final Pair<String[], Map<String, Object>> notification = formatNotifications(context, state);
+            doSendNotification(config, users, state.name(), notification);
         } catch (Exception e) {
-            logger.error("error send email", e);
+            logger.error("error send notification", e);
         }
     }
 
-    private List<String> getAllNofifyUsers(KylinConfig kylinConfig) {
-        List<String> users = Lists.newArrayList();
-        users.addAll(getNotifyList());
+    private Map<String, List<String>> getAllNofificationUsers(KylinConfig kylinConfig) {
+        Map<String, List<String>> users = Maps.newHashMap();
+        users.put(NotificationConstants.NOTIFY_EMAIL_LIST, getNotificationList(NotificationConstants.NOTIFY_EMAIL_LIST));
+        users.put(NotificationConstants.NOTIFY_DINGTALK_LIST, getNotificationList(NotificationConstants.NOTIFY_DINGTALK_LIST));
         final String[] adminDls = kylinConfig.getAdminDls();
         if (null != adminDls) {
             for (String adminDl : adminDls) {
-                users.add(adminDl);
+                users.get(NotificationConstants.NOTIFY_EMAIL_LIST).add(adminDl);
             }
         }
         return users;
     }
 
-    private void doSendMail(KylinConfig kylinConfig, List<String> users, Pair<String, String> email) {
-        if (email == null) {
-            logger.warn("no need to send email, content is null");
-            return;
-        }
-        logger.info("prepare to send email to:{}", users);
+    private void doSendNotification(KylinConfig kylinConfig, Map<String, List<String>> receivers, String state, Pair<String[], Map<String, Object>> content) {

Review comment:
       too long...

##########
File path: core-common/src/main/java/org/apache/kylin/common/notify/NotificationContext.java
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kylin.common.notify;
+
+import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.util.Pair;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * All basic information of the notification.
+ */
+public class NotificationContext {
+    private KylinConfig config;
+
+    /**
+     * Notification recipient,include email and dingTalk
+     */
+    private Map<String, List<String>> receivers;
+
+    /**
+     * state of job
+     */
+    private String state;
+
+    /**
+     * content of notification, include title and detail
+     */
+    private Pair<String[], Map<String, Object>> content;
+
+    /**
+     * Send in HTML
+     */
+    private boolean isHtmlMsg = true;
+
+    /**
+     * when isHtmlMsg is false, subject and info will be sended as text
+     */
+    private String subject = "";
+
+    private String info = "";
+
+    public NotificationContext(KylinConfig config, Map<String, List<String>> receivers, String state, Pair<String[], Map<String, Object>> content) {
+        this.config = config;
+        this.receivers = receivers;
+        this.state = state;
+        this.content = content;
+    }
+
+    public NotificationContext(KylinConfig config, Map<String, List<String>> receivers, String subject, String info, boolean isHtmlMsg) {
+        this.config = config;
+        this.receivers = receivers;
+        this.subject = subject;
+        this.info = info;
+        this.isHtmlMsg = isHtmlMsg;
+    }
+
+    public KylinConfig getConfig() {
+        return config;
+    }
+
+    public void setConfig(KylinConfig config) {

Review comment:
       If we inject the property via constructors, we can remove these `setter`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org