log_day.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package access_log
  4. import (
  5. "gd_access_log/apis"
  6. "gd_access_log/errors"
  7. "github.com/astaxie/beego/orm"
  8. "time"
  9. )
  10. func deleteAccessOldMinute(o orm.Ormer, nowMinute int64) {
  11. o.Raw("delete from t_gd_access_log_day where timestamp < ?", nowMinute).Exec()
  12. }
  13. func deleteThirdOldMinute(o orm.Ormer, nowMinute int64) {
  14. o.Raw("delete from t_gd_thirdpart_log_day where timestamp < ?", nowMinute).Exec()
  15. }
  16. func DeleteLogDayTask() {
  17. go func() {
  18. o := orm.NewOrm()
  19. t := time.NewTicker(1 * time.Hour)
  20. for {
  21. select {
  22. case <-t.C:
  23. now := time.Now()
  24. if now.Hour() == 6 {
  25. deleteMinute := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -2).Unix()
  26. deleteAccessOldMinute(o, deleteMinute)
  27. deleteThirdOldMinute(o, deleteMinute)
  28. }
  29. }
  30. }
  31. }()
  32. }
  33. func writeLogDay(accessLog *apis.AccessLog, thirdLog []*apis.ThirdpartLog, o orm.Ormer) error {
  34. tab := apis.AccessLogDay{}
  35. tab.AccessLog = *accessLog
  36. tab.Id = 0
  37. accessId, err := o.Insert(&tab)
  38. if err != nil {
  39. return errors.DataBaseError
  40. }
  41. for _, v := range thirdLog {
  42. tab := apis.ThirdpartLogDay{}
  43. tab.ThirdpartLog = *v
  44. tab.Id = 0
  45. tab.AccessId = accessId
  46. _, err := o.Insert(&tab)
  47. if err != nil {
  48. return errors.DataBaseError
  49. }
  50. }
  51. return nil
  52. }