diff --git a/aggregate.go b/aggregate.go index 9c015b8..64c31bb 100644 --- a/aggregate.go +++ b/aggregate.go @@ -3,9 +3,10 @@ package main import ( "fmt" - "gopkg.in/mgo.v2/bson" "log" "time" + + "gopkg.in/mgo.v2/bson" ) func aggregate(ys time.Time, ye time.Time) { @@ -14,6 +15,7 @@ func aggregate(ys time.Time, ye time.Time) { 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"}}}) + idb.Pipe = time.Since(qStart) if opts.Debug { fmt.Printf("Aggregate user: %s\n\r", time.Since(qStart)) } @@ -25,7 +27,7 @@ func aggregate(ys time.Time, ye time.Time) { it := p.Iter() for it.Next(&ar) { - countTOT += 1 + idb.CountTOT += 1 ll := LastLoginDay{} ll.User = ar.User ll.Date = ys @@ -39,6 +41,13 @@ func aggregate(ys time.Time, ye time.Time) { nq := dbs.ll.Find(bson.M{"date": bson.M{"$gte": ys, "$lte": ye}, "user": ar.User}).Sort("date") //fmt.Printf("User: %s -> %s\n\r", ar[u], time.Since(qStart) ) iter := nq.Iter() + + if idb.Find == 0 { + idb.Find = time.Since(qStart) + } else { + idb.Find = (idb.Find + time.Since(qStart)) / 2 + } + result := LastLogin{} ips := []IPs{} lastip := IPs{} @@ -64,11 +73,18 @@ func aggregate(ys time.Time, ye time.Time) { } ll.IPs = ips //fmt.Printf("Upsert %+v\n\r", ll) + iStart := time.Now() _, err := dbs.lc.Upsert(Index{User: ll.User, Date: ll.Date}, ll) if err != nil { log.Println("Insert error: ", err) } - countOK += 1 + if idb.Insert == 0 { + idb.Insert = time.Since(iStart) + } else { + idb.Insert = (idb.Insert + time.Since(iStart)) / 2 + } + + idb.CountOK += 1 } } diff --git a/influxdb.go b/influxdb.go index e9daead..8590fdd 100644 --- a/influxdb.go +++ b/influxdb.go @@ -8,7 +8,16 @@ import ( influxdb "github.com/influxdata/influxdb/client/v2" ) -func writeStats(start time.Time, stop time.Duration) { +type InfluxdbOutput struct { + CountOK int + CountTOT int + Stop time.Duration + Pipe time.Duration + Find time.Duration + Insert time.Duration +} + +func writeStats(start time.Time) { if opts.Debug { fmt.Printf("writing to influxdb server: %s", opts.Influxdb) } @@ -34,9 +43,12 @@ func writeStats(start time.Time, stop time.Duration) { tags := map[string]string{"server": opts.Hostname} fields := map[string]interface{}{ - "LoginOK": countOK, - "LoginTOT": countTOT, - "stop": stop.Seconds(), + "LoginOK": idb.CountOK, + "LoginTOT": idb.CountTOT, + "stop": idb.Stop.Seconds(), + "pipe": idb.Pipe.Seconds(), + "find": idb.Find.Seconds(), + "insert": idb.Insert.Seconds(), } pt, err := influxdb.NewPoint("llday", tags, fields, start) if err != nil { diff --git a/lastlogin_consolidate.go b/lastlogin_consolidate.go index 226b50b..75f4402 100644 --- a/lastlogin_consolidate.go +++ b/lastlogin_consolidate.go @@ -12,7 +12,7 @@ import ( ) const ( - _VERSION = "v1.2.1" + _VERSION = "v1.2.2" _tformat = "2006-01-02" _24h = (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) _10m = (time.Minute * 10) @@ -30,8 +30,12 @@ var ( dbs = Dbs{} - countTOT = 0 - countOK = 0 + idb = InfluxdbOutput{ + CountTOT: 0, + CountOK: 0, + Insert: 0, + Find: 0, + } ) func main() { @@ -108,12 +112,12 @@ func main() { log.Printf("Stop %s: %s\n", ys[i], time.Since(pStart)) } - stop := time.Since(start) + idb.Stop = time.Since(start) - fmt.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", countOK, countTOT, stop) - log.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", countOK, countTOT, stop) + fmt.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.CountOK, idb.CountTOT, idb.Stop) + log.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.CountOK, idb.CountTOT, idb.Stop) if opts.Influxdb != "" { - writeStats(start, stop) + writeStats(start) } }