alertnotification.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/sqlchemy"
  21. "yunion.io/x/onecloud/pkg/apis/monitor"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/util/stringutils2"
  26. )
  27. const (
  28. AlertNotificationUsedByMeterAlert = monitor.AlertNotificationUsedByMeterAlert
  29. AlertNotificationUsedByNodeAlert = monitor.AlertNotificationUsedByNodeAlert
  30. AlertNotificationUsedByCommonAlert = monitor.AlertNotificationUsedByCommonAlert
  31. AlertNotificationUsedByMigrationAlert = monitor.AlertNotificationUsedByMigrationAlert
  32. )
  33. // +onecloud:swagger-gen-ignore
  34. type SAlertNotificationManager struct {
  35. SAlertJointsManager
  36. }
  37. var AlertNotificationManager *SAlertNotificationManager
  38. func init() {
  39. db.InitManager(func() {
  40. AlertNotificationManager = &SAlertNotificationManager{
  41. SAlertJointsManager: NewAlertJointsManager(
  42. SAlertnotification{},
  43. "alertnotifications_tbl",
  44. "alertnotification",
  45. "alertnotifications",
  46. NotificationManager),
  47. }
  48. AlertNotificationManager.SetVirtualObject(AlertNotificationManager)
  49. AlertNotificationManager.TableSpec().AddIndex(true, "notification_id", "alert_id")
  50. })
  51. }
  52. // +onecloud:swagger-gen-ignore
  53. type SAlertnotification struct {
  54. SAlertJointsBase
  55. NotificationId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
  56. State string `nullable:"false" list:"user" create:"required"`
  57. Index int8 `nullable:"false" default:"0" list:"user" list:"user" update:"user"`
  58. UsedBy string `width:"36" charset:"ascii" nullable:"true" list:"user"`
  59. Params jsonutils.JSONObject `nullable:"true" list:"user" update:"user"`
  60. }
  61. func (man *SAlertNotificationManager) GetSlaveFieldName() string {
  62. return "notification_id"
  63. }
  64. func (man *SAlertNotificationManager) Get(alertId string, notiId string) (*SAlertnotification, error) {
  65. q := man.Query().Equals("alert_id", alertId).Equals("notification_id", notiId)
  66. obj := new(SAlertnotification)
  67. err := q.First(obj)
  68. obj.SetModelManager(man, obj)
  69. return obj, err
  70. }
  71. func (man *SAlertNotificationManager) ListItemFilter(
  72. ctx context.Context,
  73. q *sqlchemy.SQuery,
  74. userCred mcclient.TokenCredential,
  75. query monitor.AlertNotificationListInput) (*sqlchemy.SQuery, error) {
  76. return man.SAlertJointsManager.ListItemFilter(ctx, q, userCred, query.AlertJointListInput)
  77. }
  78. func (man *SAlertNotificationManager) FetchCustomizeColumns(
  79. ctx context.Context,
  80. userCred mcclient.TokenCredential,
  81. query jsonutils.JSONObject,
  82. objs []interface{},
  83. fields stringutils2.SSortedStrings,
  84. isList bool,
  85. ) []monitor.AlertnotificationDetails {
  86. rows := make([]monitor.AlertnotificationDetails, len(objs))
  87. alertRows := man.SAlertJointsManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  88. notiIds := make([]string, len(rows))
  89. for i := range rows {
  90. rows[i] = monitor.AlertnotificationDetails{
  91. AlertJointResourceBaseDetails: alertRows[i],
  92. }
  93. notiIds[i] = objs[i].(*SAlertnotification).NotificationId
  94. }
  95. notis := make(map[string]SNotification)
  96. if err := db.FetchModelObjectsByIds(NotificationManager, "id", notiIds, notis); err != nil {
  97. return rows
  98. }
  99. for i := range rows {
  100. if noti, ok := notis[notiIds[i]]; ok {
  101. rows[i].Notification = noti.Name
  102. rows[i].Frequency = noti.Frequency
  103. }
  104. }
  105. return rows
  106. }
  107. func (man *SAlertNotificationManager) ValidateCreateData(
  108. ctx context.Context,
  109. userCred mcclient.TokenCredential,
  110. ownerId mcclient.IIdentityProvider,
  111. query jsonutils.JSONObject,
  112. input monitor.AlertnotificationCreateInput,
  113. ) (*jsonutils.JSONDict, error) {
  114. if input.AlertId == "" {
  115. return nil, httperrors.NewMissingParameterError("alert_id")
  116. }
  117. if input.NotificationId == "" {
  118. return nil, httperrors.NewMissingParameterError("notification_id")
  119. }
  120. _, err := AlertManager.FetchById(input.AlertId)
  121. if err != nil {
  122. if errors.Cause(err) == sql.ErrNoRows {
  123. return nil, httperrors.NewResourceNotFoundError("not find alert %s", input.AlertId)
  124. }
  125. return nil, err
  126. }
  127. _, err = NotificationManager.FetchById(input.NotificationId)
  128. if err != nil {
  129. if errors.Cause(err) == sql.ErrNoRows {
  130. return nil, httperrors.NewResourceNotFoundError("not find notification %s", input.NotificationId)
  131. }
  132. return nil, err
  133. }
  134. ret := input.JSON(input)
  135. ret.Add(jsonutils.NewString(string(monitor.AlertNotificationStateUnknown)), "state")
  136. return ret, nil
  137. }
  138. func (joint *SAlertnotification) getExtraDetails(noti SNotification, out monitor.AlertnotificationDetails) monitor.AlertnotificationDetails {
  139. out.Notification = noti.GetName()
  140. return out
  141. }
  142. func (joint *SAlertnotification) DoSave(ctx context.Context, userCred mcclient.TokenCredential) error {
  143. if err := AlertNotificationManager.TableSpec().Insert(ctx, joint); err != nil {
  144. return err
  145. }
  146. joint.SetModelManager(AlertNotificationManager, joint)
  147. return nil
  148. }
  149. func (joint *SAlertnotification) GetNotification() (*SNotification, error) {
  150. noti, err := NotificationManager.GetNotification(joint.NotificationId)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return noti, nil
  155. }
  156. func (join *SAlertnotification) ShouldSendNotification() (bool, error) {
  157. notification, err := join.GetNotification()
  158. if err != nil {
  159. return false, errors.Wrap(err, "Alertnotification GetNotification err")
  160. }
  161. return notification.ShouldSendNotification(), nil
  162. }
  163. func (joint *SAlertnotification) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
  164. return db.DeleteModel(ctx, userCred, joint)
  165. }
  166. func (joint *SAlertnotification) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
  167. return db.DetachJoint(ctx, userCred, joint)
  168. }
  169. func (joint *SAlertnotification) GetUsedBy() string {
  170. return joint.UsedBy
  171. }
  172. func (state *SAlertnotification) SetToPending() error {
  173. return state.setState(monitor.AlertNotificationStatePending)
  174. }
  175. func (state *SAlertnotification) SetToCompleted() error {
  176. return state.setState(monitor.AlertNotificationStateCompleted)
  177. }
  178. func (state *SAlertnotification) setState(changeState monitor.AlertNotificationStateType) error {
  179. _, err := db.Update(state, func() error {
  180. state.State = string(changeState)
  181. return nil
  182. })
  183. return err
  184. }
  185. func (state *SAlertnotification) GetState() monitor.AlertNotificationStateType {
  186. return monitor.AlertNotificationStateType(state.State)
  187. }
  188. func (state *SAlertnotification) GetParams() jsonutils.JSONObject {
  189. return state.Params
  190. }
  191. func (joint *SAlertnotification) UpdateSendTime() error {
  192. notification, err := joint.GetNotification()
  193. if err != nil {
  194. return errors.Wrap(err, "GetNotification err")
  195. }
  196. return notification.UpdateSendTime()
  197. }