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

63 lines
1.8 KiB
Go

package monitoring
import (
"context"
"fmt"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
"k8s.io/apimachinery/pkg/util/json"
"github.com/zeromicro/go-zero/core/logx"
)
type AlertListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAlertListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlertListLogic {
return &AlertListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
type AlertListResp struct {
Mode int `json:"code"`
Msg string `json:"msg"`
Data map[string][]*v1.Alert `json:"data"`
}
func (l *AlertListLogic) AlertList(req *types.AlertListReq) (resp *types.AlertListResp, err error) {
resp = &types.AlertListResp{
AlertMap: make(map[string]interface{}),
}
// query server http url.
var clusterArray []string
sql := "select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ?"
if len(req.AdapterId) > 0 {
sql = fmt.Sprintf("select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ? and ta.id = %s", req.AdapterId)
}
if len(req.ClusterId) > 0 {
sql = fmt.Sprintf("select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ? and tc.id = %s", req.ClusterId)
}
l.svcCtx.DbEngin.Raw(sql, req.AlertType).Scan(&clusterArray)
for _, clusterName := range clusterArray {
getResult := l.svcCtx.RedisClient.Get(l.ctx, clusterName)
if len(getResult.Val()) != 0 {
var alerts []v1.Alert
json.Unmarshal([]byte(getResult.Val()), &alerts)
resp.AlertMap[clusterName] = alerts
}
}
return resp, nil
}