aggiunta l'opzione di debug
This commit is contained in:
parent
42bb907c47
commit
cd673bba2d
1 changed files with 32 additions and 6 deletions
|
@ -14,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
_VERSION = "v1.0.5"
|
||||
_VERSION = "v1.0.6"
|
||||
_tformat = "2006-01-02"
|
||||
_24h = (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59)
|
||||
_10m = (time.Minute * 10)
|
||||
|
@ -43,6 +43,7 @@ type Options struct {
|
|||
Interval time.Duration
|
||||
LogFile string
|
||||
Version bool
|
||||
Debug bool
|
||||
}
|
||||
|
||||
type LastLogin struct {
|
||||
|
@ -78,7 +79,7 @@ type Index struct {
|
|||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Println("Usage: lastlogin_consolidate -ms <mongo source uri> -md <mongo destination uri> -l <logfile> -d <date> -dd <duration> -i <interval> -v\n")
|
||||
fmt.Println("Usage: lastlogin_consolidate -ms <mongo source mongodb://IP:PORT> -md <mongo destination mongodb://IP:PORT> -l <logfile> -d <date> -dd <duration> -i <interval> -v\n")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
@ -97,6 +98,7 @@ func init() {
|
|||
flag.DurationVar(&opts.Duration, "dd", opts.Duration, "Duration")
|
||||
flag.DurationVar(&opts.Interval, "i", opts.Interval, "Duration")
|
||||
flag.BoolVar(&opts.Version, "v", false, "Version")
|
||||
flag.BoolVar(&opts.Debug, "debug", false, "Debug")
|
||||
}
|
||||
|
||||
func connectMongo() {
|
||||
|
@ -158,7 +160,8 @@ func main() {
|
|||
os.Exit(-1)
|
||||
}
|
||||
|
||||
// fmt.Println(y)
|
||||
// DEBUG
|
||||
//fmt.Printf("Start %+v\n\r", y)
|
||||
|
||||
var ys []time.Time
|
||||
var ye []time.Time
|
||||
|
@ -170,23 +173,38 @@ func main() {
|
|||
for i := 0; i < int(opts.Duration/(time.Hour*24)); i++ {
|
||||
// fmt.Println(i)
|
||||
yt := y.Add(time.Hour * time.Duration(24*i))
|
||||
// fmt.Println(yt)
|
||||
// fmt.Println(yt)
|
||||
ys = append(ys, time.Date(yt.Year(), yt.Month(), yt.Day(), 0, 0, 0, 0, time.UTC))
|
||||
ye = append(ye, ys[i].Add(_24h))
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Println(ys, ye)
|
||||
// DEBUG
|
||||
if opts.Debug {
|
||||
fmt.Printf("Start: %+v, Stop: %+v\n\r", ys, ye)
|
||||
}
|
||||
|
||||
for i := range ys {
|
||||
|
||||
pStart := time.Now()
|
||||
|
||||
qStart := time.Now()
|
||||
|
||||
q := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys[i], "$lte": ye[i]}}).Sort("user")
|
||||
|
||||
if opts.Debug {
|
||||
fmt.Printf("Find from date: %s\n\r", time.Since(qStart))
|
||||
}
|
||||
|
||||
qStart = time.Now()
|
||||
|
||||
ar := []string{}
|
||||
q.Distinct("user", &ar)
|
||||
|
||||
if opts.Debug {
|
||||
fmt.Printf("Distinct: %s\n\r", time.Since(qStart))
|
||||
}
|
||||
|
||||
fmt.Printf("Date: %s - %s\n", ys[i], ye[i])
|
||||
log.Printf("Date: %s - %s\n", ys[i], ye[i])
|
||||
|
||||
|
@ -194,8 +212,15 @@ func main() {
|
|||
ll := LastLoginDay{}
|
||||
ll.User = ar[u]
|
||||
ll.Date = ys[i]
|
||||
// fmt.Println(ar[u])
|
||||
|
||||
// DEBUG
|
||||
if opts.Debug {
|
||||
fmt.Printf("User: %s\n\r", ar[u])
|
||||
}
|
||||
|
||||
qStart = time.Now()
|
||||
nq := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys[i], "$lte": ye[i]}, "user": ar[u]}).Sort("date")
|
||||
//fmt.Printf("User: %s -> %s\n\r", ar[u], time.Since(qStart) )
|
||||
iter := nq.Iter()
|
||||
result := LastLogin{}
|
||||
ips := []IPs{}
|
||||
|
@ -221,6 +246,7 @@ func main() {
|
|||
log.Println("Iter: ", err)
|
||||
}
|
||||
ll.IPs = ips
|
||||
//fmt.Printf("Upsert %+v\n\r", ll)
|
||||
_, err := opts.lc.Upsert(Index{User: ll.User, Date: ll.Date}, ll)
|
||||
if err != nil {
|
||||
log.Println("Insert error: ", err)
|
||||
|
|
Loading…
Add table
Reference in a new issue