superset实现新的通知(告警&报告)方式

版本:superset 2.0.1

上一张成品图吧,效果如下:

前端配置页面1:

说明:如果要替换图标,直接在vs code 全局搜索下,这个img-path,找到jsx文件修改图标生成逻辑即可,还有个本地静态资源icons这个文件夹内需要加一个svg素材。
 

 前端配置页面2:

说明:钉钉机器人通知,webhook - token复制丢进去就行了。

钉钉actionCard实现效果:

 说明:生成的这个gpt返回的结果呢,写个js,每隔5秒钟异步一次,如果gpt响应了内容,那么更新内容,js检测span内容是否发生变更,或者用关键字校验一下就行了,判断是否执行成功或者啥的。

 源码部分:

 1.superset\models\reports.py
找到以下内容,添加新的通知类型:
class ReportRecipientType(str, enum.Enum):
    EMAIL = "Email"
    SLACK = "Slack"
    DINGDING = "Dingding"

2.superset\views\base.py

        frontend_config["ALERT_REPORTS_NOTIFICATION_METHODS"] = [

            ReportRecipientType.EMAIL,

            ReportRecipientType.DINGDING,

tip:重新编译后执行,配置面板就会有新的通知方式-DingDing,

具体实现
改下类名跟send方法实现即可

send内写怎么推送即可,具体可以参考钉钉官方的webhook通知方式
            当然也可以将csv数据回传给GPT,生成报告,然后用uuid动态生成html模板,嵌入到钉钉,实现指标告警+AI自分析,一条龙服务。


温馨提示:不过好像钉钉要生成图片+资源,都需要链接形式才行,用uuid动态生成图片跟scv的名称,存在服务器自己随便建立个目录下,然后生成链接,传给web hook模板就行,当然也可以上传到钉钉接口,生成独有媒体文件id,然后传给web hook模板也行,2种方法,不过好像会过期耶,我测试就用了第一种,应该不可能有人猜出来uuid吧,哈哈哈!

—init—.py

记得初始化加上一下,不然GG了,
文件位于:superset\reports\notifications\__init__.py
from superset.reports.notifications.dingding import DingdingNotification

后端:

superset\reports\notifications\email.py
这个文件copy一份,改为dingding.py

# -*- coding: utf-8 -*-
# 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.
import json
import logging
from dataclasses import dataclass
from typing import Optional

from flask_babel import gettext as __

from superset.models.reports import ReportRecipientType
from superset.reports.notifications.base import BaseNotification
from superset.reports.notifications.exceptions import NotificationError

logger = logging.getLogger(__name__)


@dataclass
class DingDingContent:
    body: str
    data: Optional[Dict[str, Any]] = None
    images: Optional[Dict[str, bytes]] = None

    def __str__(self):
        """返回一个对象的描述信息"""
        return f"""
            ===================================================
            ===== body:     {self.body}
            ===== data:     {self.data}
            ===== images:      {self.images}
            ===================================================
        """


class DingDingNotification(BaseNotification):

    """
    系统消息通知入口
    """
    type = ReportRecipientType.DINGDING

    def _get_content(self) -> DingDingContent:
        """
        封装数据
        """
        if self._content.text:
            return None
        self.url = "/" + "/".join(self._content.url.split('/')[3:])
        self.note = self._content.description

        return SysMsgContent(name=self._get_title(), url=self.url,
                             note=self.note, userNames=self._get_to(),
                             title=self._get_title())

    def _get_title(self) -> str:
        """
            获取title
        """
        print('name:{}'.format(self._content.name))
        return __("%(title)s", title=self._content.name.split(':')[0])

    def _get_to(self) -> str:
        """
            获取收件人信息
        """
        return json.loads(self._recipient.recipient_config_json)["target"]

    def send(self) -> None:
        """
            这里写怎么推送即可,具体可以参考钉钉官方的webhook通知方式
            当然也可以将csv数据回传给GPT,生成报告,然后用uuid动态生成html模板,嵌入到钉钉,
            实现指标告警+AI自分析,一条龙服务。
        """
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值