pcm-coordinator/internal/logic/monitoring/alertruleslogic.go

41 lines
1.7 KiB
Go

package monitoring
import (
"context"
"fmt"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type AlertRulesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAlertRulesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlertRulesLogic {
return &AlertRulesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AlertRulesLogic) AlertRules(req *types.AlertRulesReq) (resp *types.AlertRulesResp, err error) {
resp = &types.AlertRulesResp{}
var alertRules []types.AlertRule
sql := fmt.Sprintf("SELECT ar.*,GROUP_CONCAT(tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',') as cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id WHERE ar.alert_type = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType)
if req.AdapterId != "" {
sql = fmt.Sprintf("SELECT ar.*,GROUP_CONCAT( tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',' ) AS cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id JOIN t_adapter ta ON ta.id = tc.adapter_id WHERE ar.alert_type = %s AND ta.id = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType, req.AdapterId)
}
if req.ClusterId != "" {
sql = fmt.Sprintf("SELECT ar.*,GROUP_CONCAT(tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',') as cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id WHERE ar.alert_type = %s AND ar.cluster_id = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType, req.ClusterId)
}
l.svcCtx.DbEngin.Raw(sql).Scan(&alertRules)
resp.AlertRules = alertRules
return resp, nil
}