events.go 964 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package logic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/google/uuid"
  8. "github.com/gravitl/netmaker/db"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/schema"
  11. )
  12. var EventActivityCh = make(chan models.Event, 100)
  13. func LogEvent(a *models.Event) {
  14. EventActivityCh <- *a
  15. }
  16. func EventWatcher() {
  17. for e := range EventActivityCh {
  18. if e.Action == models.Update {
  19. // check if diff
  20. if cmp.Equal(e.Diff.Old, e.Diff.New) {
  21. continue
  22. }
  23. }
  24. sourceJson, _ := json.Marshal(e.Source)
  25. dstJson, _ := json.Marshal(e.Target)
  26. diff, _ := json.Marshal(e.Diff)
  27. a := schema.Event{
  28. ID: uuid.New().String(),
  29. Action: e.Action,
  30. Source: sourceJson,
  31. Target: dstJson,
  32. Origin: e.Origin,
  33. NetworkID: e.NetworkID,
  34. TriggeredBy: e.TriggeredBy,
  35. Diff: diff,
  36. TimeStamp: time.Now().UTC(),
  37. }
  38. a.Create(db.WithContext(context.TODO()))
  39. }
  40. }