mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-31 01:35:56 +08:00
Merge pull request #11837 from zhaoxiangchun/fix/zxc-node-alert-vm
feat(monitor): add dashboard and panel clone
This commit is contained in:
@@ -26,4 +26,6 @@ func init() {
|
||||
cmd.List(new(options.AlertDashBoardListOptions))
|
||||
cmd.Show(new(options.AlertDashBoardShowOptions))
|
||||
cmd.Delete(new(options.AlertDashBoardDeleteOptions))
|
||||
cmd.Perform("clone-panel", new(options.AlertClonePanelOptions))
|
||||
cmd.Perform("clone-dashboard", new(options.AlertCloneDashboardOptions))
|
||||
}
|
||||
|
||||
@@ -44,3 +44,12 @@ type AlertPanelDetail struct {
|
||||
Setting jsonutils.JSONObject
|
||||
PanelDetails
|
||||
}
|
||||
|
||||
type AlertClonePanelInput struct {
|
||||
PanelId string `json:"panel_id"`
|
||||
ClonePanelName string `json:"clone_panel_name"`
|
||||
}
|
||||
|
||||
type AlertCloneDashboardInput struct {
|
||||
CloneName string `json:"clone_name"`
|
||||
}
|
||||
|
||||
@@ -62,3 +62,30 @@ func (o *AlertDashBoardDeleteOptions) GetId() string {
|
||||
func (o *AlertDashBoardDeleteOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return options.StructToParams(o)
|
||||
}
|
||||
|
||||
type AlertClonePanelOptions struct {
|
||||
ID string `help:"ID of alart " json:"-"`
|
||||
PanelId string `help:"ID of alertPanel" json:"panel_id"`
|
||||
ClonePanelName string `help:"new panel name" json:"clone_panel_name"`
|
||||
}
|
||||
|
||||
func (o *AlertClonePanelOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return options.StructToParams(o)
|
||||
}
|
||||
|
||||
func (o *AlertClonePanelOptions) GetId() string {
|
||||
return o.ID
|
||||
}
|
||||
|
||||
type AlertCloneDashboardOptions struct {
|
||||
ID string `help:"ID of alart " json:"-"`
|
||||
CloneName string `json:"clone_name" help:"new dashboard name"`
|
||||
}
|
||||
|
||||
func (o *AlertCloneDashboardOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return options.StructToParams(o)
|
||||
}
|
||||
|
||||
func (o *AlertCloneDashboardOptions) GetId() string {
|
||||
return o.ID
|
||||
}
|
||||
|
||||
@@ -267,3 +267,76 @@ func (manager *SAlertDashBoardManager) getDashboardByid(id string) (*SAlertDashB
|
||||
}
|
||||
return iModel.(*SAlertDashBoard), nil
|
||||
}
|
||||
|
||||
func (dash *SAlertDashBoard) AllowPerformClonePanel(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (dash *SAlertDashBoard) PerformClonePanel(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, input monitor.AlertClonePanelInput) (jsonutils.JSONObject, error) {
|
||||
|
||||
panel, err := AlertPanelManager.getPanelByid(input.PanelId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "getPanelByid:%s err", input.PanelId)
|
||||
}
|
||||
panelCloned, err := panel.ClonePanel(ctx, dash.GetId(), input)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ClonePanel err")
|
||||
}
|
||||
panelDetails := monitor.PanelDetails{}
|
||||
panelDetails, err = panelCloned.GetMoreDetails(panelDetails)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "panelCloned:%s GetMoreDetails err", panelCloned.Id)
|
||||
}
|
||||
output := jsonutils.Marshal(panelCloned)
|
||||
output.(*jsonutils.JSONDict).Update(jsonutils.Marshal(&panelDetails))
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (dash *SAlertDashBoard) AllowPerformCloneDashboard(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (dash *SAlertDashBoard) PerformCloneDashboard(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, input monitor.AlertCloneDashboardInput) (jsonutils.JSONObject, error) {
|
||||
iModel, err := db.NewModelObject(AlertDashBoardManager)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "AlertDashBoardManager NewModelObject err")
|
||||
}
|
||||
dashJson := jsonutils.Marshal(dash)
|
||||
dashJson.Unmarshal(iModel)
|
||||
iModel.(*SAlertDashBoard).Id = ""
|
||||
name, err := db.GenerateName2(ctx, AlertDashBoardManager, nil, input.CloneName, iModel, 1)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "clonePanel GenerateName err")
|
||||
}
|
||||
iModel.(*SAlertDashBoard).Name = name
|
||||
err = AlertDashBoardManager.TableSpec().Insert(ctx, iModel)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "insert clone dashboard err by id:%s", dash.GetId())
|
||||
}
|
||||
alertPanels, err := dash.getAttachPanels()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "dashboard:%s getAttachPanels err", dash.Id)
|
||||
}
|
||||
for _, panel := range alertPanels {
|
||||
err := panel.attachDashboard(ctx, iModel.(*SAlertDashBoard).Id)
|
||||
if err != nil {
|
||||
err := iModel.Delete(ctx, userCred)
|
||||
if err != nil {
|
||||
log.Errorf("delete cloneDashboard:%s err when panel attachDashboard err:%v", input.CloneName, err)
|
||||
}
|
||||
return nil, errors.Wrapf(err, "panel:%s attachCloneDashboard:%s err", panel.Name, input.CloneName)
|
||||
}
|
||||
}
|
||||
boardDetails, err := iModel.(*SAlertDashBoard).GetMoreDetails(monitor.AlertDashBoardDetails{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cloneDashboard getDetails err")
|
||||
}
|
||||
output := jsonutils.Marshal(iModel)
|
||||
output.(*jsonutils.JSONDict).Update((jsonutils.Marshal(&boardDetails)))
|
||||
return output, nil
|
||||
}
|
||||
|
||||
@@ -363,3 +363,34 @@ func (panel *SAlertPanel) getPanelJoint() (*SAlertDashboardPanel, error) {
|
||||
}
|
||||
return iModel.(*SAlertDashboardPanel), nil
|
||||
}
|
||||
|
||||
func (manager *SAlertPanelManager) getPanelByid(id string) (*SAlertPanel, error) {
|
||||
iModel, err := db.FetchById(manager, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return iModel.(*SAlertPanel), nil
|
||||
}
|
||||
|
||||
func (panel *SAlertPanel) ClonePanel(ctx context.Context, dashboardId string,
|
||||
input monitor.AlertClonePanelInput) (*SAlertPanel, error) {
|
||||
iModel, _ := db.NewModelObject(AlertPanelManager)
|
||||
panelJson := jsonutils.Marshal(panel)
|
||||
panelJson.Unmarshal(iModel)
|
||||
iModel.(*SAlertPanel).Id = ""
|
||||
name := input.ClonePanelName
|
||||
var err error
|
||||
if len(name) == 0 {
|
||||
name, err = db.GenerateName2(ctx, AlertPanelManager, nil, panel.GetName(), iModel, 1)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "clonePanel GenerateName err")
|
||||
}
|
||||
}
|
||||
iModel.(*SAlertPanel).Name = name
|
||||
err = AlertPanelManager.TableSpec().Insert(ctx, iModel)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "panel:%s clone err", panel.Id)
|
||||
}
|
||||
iModel.(*SAlertPanel).attachDashboard(ctx, dashboardId)
|
||||
return iModel.(*SAlertPanel), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user