// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package access_log import ( "gd_access_log/apis" "gd_access_log/errors" "github.com/astaxie/beego/orm" "time" ) func deleteAccessOldMinute(o orm.Ormer, nowMinute int64) { o.Raw("delete from t_gd_access_log_day where timestamp < ?", nowMinute).Exec() } func deleteThirdOldMinute(o orm.Ormer, nowMinute int64) { o.Raw("delete from t_gd_thirdpart_log_day where timestamp < ?", nowMinute).Exec() } func DeleteLogDayTask() { go func() { o := orm.NewOrm() t := time.NewTicker(1 * time.Hour) for { select { case <-t.C: now := time.Now() if now.Hour() == 6 { deleteMinute := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -2).Unix() deleteAccessOldMinute(o, deleteMinute) deleteThirdOldMinute(o, deleteMinute) } } } }() } func writeLogDay(accessLog *apis.AccessLog, thirdLog []*apis.ThirdpartLog, o orm.Ormer) error { tab := apis.AccessLogDay{} tab.AccessLog = *accessLog tab.Id = 0 accessId, err := o.Insert(&tab) if err != nil { return errors.DataBaseError } for _, v := range thirdLog { tab := apis.ThirdpartLogDay{} tab.ThirdpartLog = *v tab.Id = 0 tab.AccessId = accessId _, err := o.Insert(&tab) if err != nil { return errors.DataBaseError } } return nil }