AlertDao.java 7.9 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.dolphinscheduler.dao;
19

20
import org.apache.dolphinscheduler.common.enums.AlertEvent;
Q
qiaozhanwei 已提交
21 22
import org.apache.dolphinscheduler.common.enums.AlertStatus;
import org.apache.dolphinscheduler.common.enums.AlertType;
23
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel;
Q
qiaozhanwei 已提交
24
import org.apache.dolphinscheduler.common.enums.ShowType;
25 26
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
27
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
Q
qiaozhanwei 已提交
28
import org.apache.dolphinscheduler.dao.entity.Alert;
29
import org.apache.dolphinscheduler.dao.entity.ProcessAlertContent;
Q
qiaozhanwei 已提交
30 31
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
32
import org.apache.dolphinscheduler.dao.entity.ServerAlertContent;
Q
qiaozhanwei 已提交
33
import org.apache.dolphinscheduler.dao.entity.User;
34 35
import org.apache.dolphinscheduler.dao.mapper.AlertMapper;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
L
ligang 已提交
36

37
import java.util.ArrayList;
L
ligang 已提交
38 39 40
import java.util.Date;
import java.util.List;

41 42 43 44 45
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

L
ligang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
@Component
public class AlertDao extends AbstractBaseDao {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AlertMapper alertMapper;

    @Autowired
    private UserAlertGroupMapper userAlertGroupMapper;

    @Override
    protected void init() {
59 60
        alertMapper = ConnectionFactory.getInstance().getMapper(AlertMapper.class);
        userAlertGroupMapper = ConnectionFactory.getInstance().getMapper(UserAlertGroupMapper.class);
L
ligang 已提交
61 62 63 64
    }

    /**
     * insert alert
65
     *
66 67
     * @param alert alert
     * @return add alert result
L
ligang 已提交
68
     */
69
    public int addAlert(Alert alert) {
L
ligang 已提交
70 71 72 73 74
        return alertMapper.insert(alert);
    }

    /**
     * update alert
75
     *
76 77 78 79
     * @param alertStatus alertStatus
     * @param log log
     * @param id id
     * @return update alert result
L
ligang 已提交
80
     */
81
    public int updateAlert(AlertStatus alertStatus, String log, int id) {
B
bao liang 已提交
82 83 84 85 86
        Alert alert = alertMapper.selectById(id);
        alert.setAlertStatus(alertStatus);
        alert.setUpdateTime(new Date());
        alert.setLog(log);
        return alertMapper.updateById(alert);
L
ligang 已提交
87 88 89 90
    }

    /**
     * query user list by alert group id
91
     *
92 93
     * @param alerGroupId alerGroupId
     * @return user list
L
ligang 已提交
94
     */
95
    public List<User> queryUserByAlertGroupId(int alerGroupId) {
B
bao liang 已提交
96

97
        return userAlertGroupMapper.listUserByAlertgroupId(alerGroupId);
L
ligang 已提交
98
    }
99

L
ligang 已提交
100 101
    /**
     * MasterServer or WorkerServer stoped
102
     *
103 104 105
     * @param alertgroupId alertgroupId
     * @param host host
     * @param serverType serverType
L
ligang 已提交
106
     */
107
    public void sendServerStopedAlert(int alertgroupId, String host, String serverType) {
L
ligang 已提交
108
        Alert alert = new Alert();
109 110 111 112 113 114
        List<ServerAlertContent> serverAlertContents = new ArrayList<>(1);
        ServerAlertContent serverStopAlertContent = ServerAlertContent.newBuilder().
                type(serverType).host(host).event(AlertEvent.SERVER_DOWN).warningLevel(AlertWarnLevel.SERIOUS).
                build();
        serverAlertContents.add(serverStopAlertContent);
        String content = JSONUtils.toJsonString(serverAlertContents);
B
baoliang 已提交
115
        alert.setTitle("Fault tolerance warning");
J
John Liu 已提交
116
        saveTaskTimeoutAlert(alert, content, alertgroupId, null, null);
L
ligang 已提交
117 118
    }

B
baoliang 已提交
119 120
    /**
     * process time out alert
121
     *
122 123
     * @param processInstance processInstance
     * @param processDefinition processDefinition
B
baoliang 已提交
124
     */
125
    public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) {
B
baoliang 已提交
126 127 128 129
        int alertgroupId = processInstance.getWarningGroupId();
        String receivers = processDefinition.getReceivers();
        String receiversCc = processDefinition.getReceiversCc();
        Alert alert = new Alert();
130 131 132 133 134 135 136 137 138
        List<ProcessAlertContent> processAlertContentList = new ArrayList<>(1);
        ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
                .processId(processInstance.getId())
                .processName(processInstance.getName())
                .event(AlertEvent.TIME_OUT)
                .warningLevel(AlertWarnLevel.MIDDLE)
                .build();
        processAlertContentList.add(processAlertContent);
        String content = JSONUtils.toJsonString(processAlertContentList);
B
baoliang 已提交
139
        alert.setTitle("Process Timeout Warn");
J
John Liu 已提交
140 141 142
        saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
    }

143 144
    private void saveTaskTimeoutAlert(Alert alert, String content, int alertgroupId,
                                      String receivers, String receiversCc) {
B
baoliang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        alert.setShowType(ShowType.TABLE);
        alert.setContent(content);
        alert.setAlertType(AlertType.EMAIL);
        alert.setAlertGroupId(alertgroupId);
        if (StringUtils.isNotEmpty(receivers)) {
            alert.setReceivers(receivers);
        }
        if (StringUtils.isNotEmpty(receiversCc)) {
            alert.setReceiversCc(receiversCc);
        }
        alert.setCreateTime(new Date());
        alert.setUpdateTime(new Date());
        alertMapper.insert(alert);
    }

L
ligang 已提交
160 161
    /**
     * task timeout warn
162
     *
163 164 165
     * @param alertgroupId alertgroupId
     * @param receivers receivers
     * @param receiversCc receiversCc
Q
qiaozhanwei 已提交
166 167
     * @param processInstanceId processInstanceId
     * @param processInstanceName processInstanceName
168 169
     * @param taskId taskId
     * @param taskName taskName
L
ligang 已提交
170
     */
171 172
    public void sendTaskTimeoutAlert(int alertgroupId, String receivers, String receiversCc, int processInstanceId,
                                     String processInstanceName, int taskId, String taskName) {
L
ligang 已提交
173
        Alert alert = new Alert();
174 175 176 177 178 179 180 181 182 183 184
        List<ProcessAlertContent> processAlertContentList = new ArrayList<>(1);
        ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
                .processId(processInstanceId)
                .processName(processInstanceName)
                .taskId(taskId)
                .taskName(taskName)
                .event(AlertEvent.TIME_OUT)
                .warningLevel(AlertWarnLevel.MIDDLE)
                .build();
        processAlertContentList.add(processAlertContent);
        String content = JSONUtils.toJsonString(processAlertContentList);
L
ligang 已提交
185
        alert.setTitle("Task Timeout Warn");
J
John Liu 已提交
186
        saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
L
ligang 已提交
187 188 189 190
    }

    /**
     * list the alert information of waiting to be executed
191
     *
192
     * @return alert list
L
ligang 已提交
193
     */
194
    public List<Alert> listWaitExecutionAlert() {
L
ligang 已提交
195 196 197 198 199
        return alertMapper.listAlertByStatus(AlertStatus.WAIT_EXECUTION);
    }

    /**
     * list user information by alert group id
200
     *
201 202
     * @param alertgroupId alertgroupId
     * @return user list
L
ligang 已提交
203
     */
204
    public List<User> listUserByAlertgroupId(int alertgroupId) {
205
        return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId);
L
ligang 已提交
206 207
    }

208 209
    /**
     * for test
210
     *
Q
qiaozhanwei 已提交
211
     * @return AlertMapper
212 213 214 215
     */
    public AlertMapper getAlertMapper() {
        return alertMapper;
    }
216

L
ligang 已提交
217
}