event.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "strconv"
  18. "time"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. )
  23. type SEventManager struct {
  24. db.SLogBaseManager
  25. }
  26. var EventManager *SEventManager
  27. func InitEventLog() {
  28. EventManager = &SEventManager{
  29. SLogBaseManager: db.NewLogBaseManager(SEvent{}, "events2_tbl", "notifyevent", "notifyevents", "created_at", consts.OpsLogWithClickhouse),
  30. }
  31. EventManager.SetVirtualObject(EventManager)
  32. }
  33. type SEvent struct {
  34. db.SLogBase
  35. // 资源创建时间
  36. CreatedAt time.Time `nullable:"false" created_at:"true" index:"true" get:"user" list:"user" json:"created_at"`
  37. Message string `length:"medium"`
  38. Event string `width:"64" nullable:"true"`
  39. ResourceType string `width:"64" nullable:"true"`
  40. Action string `width:"64" nullable:"true"`
  41. AdvanceDays int
  42. TopicId string `width:"128" nullable:"true" index:"true"`
  43. }
  44. func (e *SEventManager) CreateEvent(ctx context.Context, event, topicId, message, action, resourceType string, advanceDays int) (*SEvent, error) {
  45. eve := &SEvent{
  46. Message: message,
  47. Event: event,
  48. AdvanceDays: advanceDays,
  49. TopicId: topicId,
  50. Action: action,
  51. ResourceType: resourceType,
  52. }
  53. err := e.TableSpec().Insert(ctx, eve)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return eve, nil
  58. }
  59. func (e *SEventManager) GetEvent(id string) (*SEvent, error) {
  60. if len(id) == 0 {
  61. return nil, nil
  62. }
  63. if consts.OpsLogWithClickhouse {
  64. eventModel, err := e.FetchById(id)
  65. if err != nil {
  66. return nil, errors.Wrap(err, "fetch event by id")
  67. }
  68. event := eventModel.(*SEvent)
  69. return event, nil
  70. }
  71. event := &SEvent{}
  72. event.SetModelManager(EventManager, event)
  73. eventId, err := strconv.Atoi(id)
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "Atoi(%s)", id)
  76. }
  77. event.Id = int64(eventId)
  78. err = EventManager.GetSplitTable().Fetch(event)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return event, nil
  83. }
  84. func (e *SEvent) GetRecordTime() time.Time {
  85. return e.CreatedAt
  86. }