2015-11-25 15:08:29 +01:00
|
|
|
// aggregate
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-04-15 15:28:52 +02:00
|
|
|
// "sort"
|
2015-11-25 15:08:29 +01:00
|
|
|
"time"
|
2016-04-06 12:05:40 +02:00
|
|
|
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
2015-11-25 15:08:29 +01:00
|
|
|
)
|
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
func consolidate(ys time.Time, ye time.Time) {
|
2015-11-25 15:08:29 +01:00
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
limit := 10000
|
|
|
|
|
|
|
|
pre := dbs.mdbDst.DB("dovecot").C("pre_lastlogin_day")
|
2015-11-25 15:08:29 +01:00
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
query := pre.Find(bson.M{})
|
2015-11-25 15:08:29 +01:00
|
|
|
|
|
|
|
if opts.Debug {
|
2016-04-15 15:28:52 +02:00
|
|
|
tot, _ := query.Count()
|
|
|
|
fmt.Printf("Users: %d\n", tot)
|
2015-11-25 15:08:29 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
query.Batch(limit)
|
|
|
|
iter := query.Iter()
|
2015-11-25 15:08:29 +01:00
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
if opts.Bulk {
|
|
|
|
dbs.bulk = dbs.lc.Bulk()
|
|
|
|
dbs.bulk.Unordered()
|
|
|
|
}
|
|
|
|
|
|
|
|
result := Users{}
|
2015-11-25 15:08:29 +01:00
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
for iter.Next(&result) {
|
2016-04-06 12:05:40 +02:00
|
|
|
idb.CountTOT += 1
|
2015-11-25 15:08:29 +01:00
|
|
|
ll := LastLoginDay{}
|
2016-04-15 15:28:52 +02:00
|
|
|
ll.User = result.User
|
2015-11-25 15:08:29 +01:00
|
|
|
ll.Date = ys
|
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
logins := result.Logins
|
2016-04-06 12:05:40 +02:00
|
|
|
|
2015-11-25 15:08:29 +01:00
|
|
|
ips := []IPs{}
|
|
|
|
lastip := IPs{}
|
2016-04-15 15:28:52 +02:00
|
|
|
for l := range logins {
|
|
|
|
if logins[l].IP == lastip.IP && logins[l].Date.Sub(lastip.Date) < opts.Interval {
|
|
|
|
/*
|
|
|
|
if opts.Debug {
|
|
|
|
fmt.Println("IPs: ", logins[l].IP, logins[l].Date, logins[l].Date.Sub(lastip.Date))
|
|
|
|
}
|
|
|
|
*/
|
2015-11-25 15:08:29 +01:00
|
|
|
} else {
|
2016-04-15 15:28:52 +02:00
|
|
|
ips = append(ips, IPs{IP: logins[l].IP, Date: logins[l].Date, Protocol: logins[l].Protocol})
|
|
|
|
lastip.Date = logins[l].Date
|
|
|
|
lastip.IP = logins[l].IP
|
2015-11-25 15:08:29 +01:00
|
|
|
}
|
2016-04-15 15:28:52 +02:00
|
|
|
switch logins[l].Protocol {
|
2015-11-25 15:08:29 +01:00
|
|
|
case "pop3", "pop":
|
|
|
|
ll.Protocols.Pop += 1
|
|
|
|
case "imap":
|
|
|
|
ll.Protocols.Imap += 1
|
|
|
|
case "web":
|
|
|
|
ll.Protocols.Web += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ll.IPs = ips
|
2016-04-15 15:28:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
if opts.Debug {
|
|
|
|
fmt.Printf("Insert %+v\n\r", ll)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-04-06 12:05:40 +02:00
|
|
|
iStart := time.Now()
|
2016-04-15 15:28:52 +02:00
|
|
|
|
|
|
|
if opts.Bulk {
|
|
|
|
//dbs.bulk.Upsert(Index{User: ll.User, Date: ll.Date}, ll)
|
|
|
|
dbs.bulk.Insert(ll)
|
2016-04-06 12:05:40 +02:00
|
|
|
} else {
|
2016-04-15 15:28:52 +02:00
|
|
|
info, err := dbs.lc.Upsert(Index{User: ll.User, Date: ll.Date}, ll)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Insert error: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Change: %+v\n", info)
|
2016-04-06 12:05:40 +02:00
|
|
|
}
|
2016-04-15 15:28:52 +02:00
|
|
|
idb.Insert += time.Since(iStart)
|
2016-04-06 12:05:40 +02:00
|
|
|
|
|
|
|
idb.CountOK += 1
|
2015-11-25 15:08:29 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 15:28:52 +02:00
|
|
|
if opts.Bulk {
|
|
|
|
res, err := dbs.bulk.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Insert error: ", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Bulk res: %+v\n", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Debug {
|
|
|
|
fmt.Printf("Insert: %d\n", idb.CountOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
pre.DropCollection()
|
|
|
|
}
|
|
|
|
|
|
|
|
func aggregate(ys time.Time, ye time.Time) {
|
|
|
|
|
|
|
|
qStart := time.Now()
|
|
|
|
|
|
|
|
p := dbs.ll.Pipe([]bson.M{
|
|
|
|
{"$match": bson.M{"date": bson.M{"$gte": ys, "$lte": ye}}},
|
|
|
|
{"$group": bson.M{"_id": "$user"}},
|
|
|
|
{"$project": bson.M{"user": "$_id"}}})
|
|
|
|
|
|
|
|
/*
|
|
|
|
p := dbs.ll.Pipe([]bson.M{
|
|
|
|
{"$match": bson.M{"date": bson.M{"$gte": ys, "$lte": ye}}},
|
|
|
|
// {"$sort": bson.M{"user": -1, "date": 1}},
|
|
|
|
{"$group": bson.M{"_id": "$user",
|
|
|
|
"logins": bson.M{"$push": bson.M{"protocol": "$protocol", "date": "$date", "ip": "$ip"}}}}})
|
|
|
|
// {"$sort": bson.M{"_id": -1}},
|
|
|
|
// {"$project": bson.M{"user": "$_id", "_id": 0, "logins": 1}},
|
|
|
|
// {"$out": "pre_lastlogin_day"}})
|
|
|
|
*/
|
|
|
|
|
|
|
|
idb.Pipe = idb.Pipe + time.Since(qStart)
|
|
|
|
if opts.Debug {
|
|
|
|
res := new(interface{})
|
|
|
|
err := p.Explain(res)
|
|
|
|
fmt.Printf("Pipe: %+v\nErr: %+v\n", *res, err)
|
|
|
|
res = new(interface{})
|
|
|
|
err = p.One(res)
|
|
|
|
fmt.Printf("user: %+v\nErr: %+v\n", *res, err)
|
|
|
|
fmt.Printf("Aggregate user: %s\n\r", time.Since(qStart))
|
|
|
|
}
|
|
|
|
|
|
|
|
consolidate(ys, ye)
|
|
|
|
|
|
|
|
fmt.Printf("Date: %s - %s\n", ys, ye)
|
|
|
|
log.Printf("Date: %s - %s\n", ys, ye)
|
2015-11-25 15:08:29 +01:00
|
|
|
}
|