package server

import (
	"time"
)

func (server *Handler) startLogAutoCleaning() {
	//create the routine for auto clean trash
	go func() {
		for {
			<-time.After(60 * time.Second) //no rush, clean every five minute
			//if there is more than 1000 log, trim it.
			if len(server.log) >= 1000 {
				server.log = server.log[1000:]
			}
			//those are old old old logs, no longer need to store it.
			for i, data := range server.log {
				if time.Now().Sub(data.Timestamp).Minutes() >= 180 {
					server.removeLog(i)
				}
			}
		}
	}()
}

func (server *Handler) removeLog(i int) {
	if len(server.log)-1 != i {
		server.log = append(server.log[:i], server.log[i+1:]...)
	} else {
		// if it is the last item, just remove it
		server.log = server.log[:i]
	}
}
