alertjoint.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/reflectutils"
  21. "yunion.io/x/sqlchemy"
  22. "yunion.io/x/onecloud/pkg/apis/monitor"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/util/stringutils2"
  26. )
  27. // +onecloud:swagger-gen-ignore
  28. type SAlertJointsManager struct {
  29. db.SJointResourceBaseManager
  30. }
  31. func NewAlertJointsManager(
  32. dt interface{}, tableName string,
  33. keyword string, keywordPlural string,
  34. slave db.IStandaloneModelManager) SAlertJointsManager {
  35. return SAlertJointsManager{
  36. db.NewJointResourceBaseManager(
  37. dt, tableName, keyword, keywordPlural, AlertManager, slave),
  38. }
  39. }
  40. // +onecloud:swagger-gen-ignore
  41. type SAlertJointsBase struct {
  42. db.SVirtualJointResourceBase
  43. AlertId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  44. }
  45. func (b *SAlertJointsBase) getAlert() *SAlert {
  46. alert, _ := AlertManager.GetAlert(b.AlertId)
  47. return alert
  48. }
  49. func (man *SAlertJointsManager) GetMasterFieldName() string {
  50. return "alert_id"
  51. }
  52. func (man *SAlertJointsManager) ListItemFilter(
  53. ctx context.Context,
  54. q *sqlchemy.SQuery,
  55. userCred mcclient.TokenCredential,
  56. query monitor.AlertJointListInput) (*sqlchemy.SQuery, error) {
  57. q, err := man.SJointResourceBaseManager.ListItemFilter(ctx, q, userCred, query.JointResourceBaseListInput)
  58. if err != nil {
  59. return nil, errors.Wrap(err, "SJointResourceBaseManager.ListItemFilter")
  60. }
  61. if len(query.AlertIds) != 0 {
  62. q = q.In("alert_id", query.AlertIds)
  63. }
  64. return q, nil
  65. }
  66. func (man *SAlertJointsManager) FetchCustomizeColumns(
  67. ctx context.Context,
  68. userCred mcclient.TokenCredential,
  69. query jsonutils.JSONObject,
  70. objs []interface{},
  71. fields stringutils2.SSortedStrings,
  72. isList bool,
  73. ) []monitor.AlertJointResourceBaseDetails {
  74. rows := make([]monitor.AlertJointResourceBaseDetails, len(objs))
  75. jointRows := man.SJointResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  76. alertIds := make([]string, len(rows))
  77. for i := range jointRows {
  78. rows[i] = monitor.AlertJointResourceBaseDetails{
  79. JointResourceBaseDetails: jointRows[i],
  80. }
  81. var base *SAlertJointsBase
  82. reflectutils.FindAnonymouStructPointer(objs[i], &base)
  83. if base != nil && len(base.AlertId) > 0 {
  84. alertIds[i] = base.AlertId
  85. }
  86. }
  87. alertIdMaps, err := db.FetchIdNameMap2(AlertManager, alertIds)
  88. if err != nil {
  89. log.Errorf("alert joints FetchIdNameMap2 fail: %s", err)
  90. return rows
  91. }
  92. for i := range rows {
  93. if name, ok := alertIdMaps[alertIds[i]]; ok {
  94. rows[i].Alert = name
  95. }
  96. }
  97. return rows
  98. }