2015-11-19 10:06:50 +01:00
|
|
|
// consumer
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-20 15:23:12 +01:00
|
|
|
// "github.com/garyburd/redigo/redis"
|
2017-01-04 17:40:59 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2015-11-19 10:06:50 +01:00
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-11-03 17:42:37 +01:00
|
|
|
|
|
|
|
"gopkg.in/mgo.v2"
|
2015-11-19 10:06:50 +01:00
|
|
|
)
|
|
|
|
|
2015-11-20 15:23:12 +01:00
|
|
|
type consumed struct {
|
|
|
|
user string
|
2015-11-24 12:08:43 +01:00
|
|
|
error bool
|
2015-11-20 15:23:12 +01:00
|
|
|
logins []string
|
2016-05-27 12:04:12 +02:00
|
|
|
empty bool
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:40:59 +01:00
|
|
|
func hash(val []byte) string {
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(val)
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:42:37 +01:00
|
|
|
func consumer() {
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
for {
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2016-11-03 17:42:37 +01:00
|
|
|
prod := <-consume
|
|
|
|
|
2016-11-04 11:50:27 +01:00
|
|
|
status = _Consumer
|
2016-11-03 17:42:37 +01:00
|
|
|
|
2016-11-04 11:50:27 +01:00
|
|
|
var bulk = make(map[string]*mgo.Bulk)
|
2016-11-10 09:01:10 +01:00
|
|
|
var rtbulk []MongoLogin
|
2016-11-04 11:50:27 +01:00
|
|
|
var col = make(map[string]*mgo.Collection)
|
|
|
|
var slogin = make(map[string][]string)
|
|
|
|
|
|
|
|
if opts.Bulk {
|
|
|
|
bulk[opts.Month] = dbs.ll.Bulk()
|
|
|
|
bulk[opts.Month].Unordered()
|
|
|
|
} else {
|
|
|
|
col[opts.Month] = dbs.ll
|
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
|
|
|
|
cons := consumed{
|
|
|
|
user: prod.user,
|
|
|
|
logins: make([]string, 0),
|
2015-11-24 12:08:43 +01:00
|
|
|
error: false,
|
2016-05-27 12:11:51 +02:00
|
|
|
empty: true,
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
for i := range prod.logins {
|
|
|
|
login := prod.logins[i]
|
|
|
|
// se la riga di login e' vuota
|
|
|
|
if login == "" {
|
|
|
|
log.Println("Login empty: ", prod.user)
|
|
|
|
cons.logins = append(cons.logins, login)
|
|
|
|
continue
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
sval := strings.Split(login, ":")
|
|
|
|
// se il formato della riga di login non e' corretto
|
|
|
|
if sval[1] == "" {
|
|
|
|
log.Println("Login format error: ", login, prod.user)
|
|
|
|
cons.logins = append(cons.logins, login)
|
|
|
|
continue
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
// se il timestamp della riga di login non e' corretto
|
|
|
|
date, err := strconv.ParseInt(sval[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Date Error: %+v - %s - %s\n", err, prod.user, login)
|
|
|
|
cons.logins = append(cons.logins, login)
|
2015-11-19 10:06:50 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
ml := MongoLogin{
|
2016-11-03 17:42:37 +01:00
|
|
|
// genera l' _ID con user e timestamp
|
2017-01-04 17:40:59 +01:00
|
|
|
ID: hash([]byte(fmt.Sprintf("%s%s", prod.user, time.Unix(date, 0).Format("20060102T1504")))), // Format("20060102T150405")
|
2015-11-20 15:23:12 +01:00
|
|
|
User: prod.user,
|
|
|
|
Protocol: sval[0],
|
2016-10-05 11:55:50 +02:00
|
|
|
IP: sval[2],
|
2015-11-20 15:23:12 +01:00
|
|
|
Date: time.Unix(date, 0),
|
2016-05-13 13:28:04 +02:00
|
|
|
Insert: time.Now(),
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
2015-11-23 18:07:07 +01:00
|
|
|
|
2016-05-13 12:11:52 +02:00
|
|
|
if opts.Month != ml.Date.Format("0601") {
|
2016-11-03 17:42:37 +01:00
|
|
|
dt := fmt.Sprintf("lastlogin_%s", ml.Date.Format("0601"))
|
2016-11-04 11:50:27 +01:00
|
|
|
if opts.Bulk {
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isMongodb() {
|
|
|
|
if _, ok := bulk[dt]; !ok {
|
|
|
|
bulk[dt] = dbs.mdb.DB("lastlogin").C(dt).Bulk()
|
|
|
|
bulk[dt].Unordered()
|
|
|
|
}
|
|
|
|
bulk[dt].Insert(ml)
|
|
|
|
slogin[dt] = append(slogin[dt], login)
|
|
|
|
}
|
|
|
|
if dbs.isRethink() {
|
|
|
|
rtbulk = append(rtbulk, ml)
|
|
|
|
slogin["rt"] = append(slogin["rt"], login)
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isMongodb() {
|
|
|
|
if _, ok := col[dt]; !ok {
|
|
|
|
col[dt] = dbs.mdb.DB("lastlogin").C(dt)
|
|
|
|
}
|
|
|
|
err = col[dt].Insert(ml)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "E11000") {
|
|
|
|
fmt.Printf("Mongo Insert Err: %+v\n", err)
|
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isRethink() {
|
|
|
|
_, err = dbs.rtdb.Insert(ml)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "Duplicate primary key") {
|
|
|
|
fmt.Printf("RT Insert Err: %+v\n", err)
|
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
|
2016-11-04 11:50:27 +01:00
|
|
|
cons.logins = append(cons.logins, login)
|
2016-05-13 12:11:52 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// inserisce il login su Mongodb
|
2016-11-04 11:50:27 +01:00
|
|
|
if opts.Bulk {
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isMongodb() {
|
|
|
|
bulk[opts.Month].Insert(ml)
|
|
|
|
slogin[opts.Month] = append(slogin[opts.Month], login)
|
|
|
|
}
|
|
|
|
if dbs.isRethink() {
|
|
|
|
rtbulk = append(rtbulk, ml)
|
|
|
|
slogin["rt"] = append(slogin["rt"], login)
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
} else {
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isMongodb() {
|
|
|
|
err = col[opts.Month].Insert(ml)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "E11000") {
|
|
|
|
fmt.Printf("Err: %+v\n", err)
|
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbs.isRethink() {
|
|
|
|
resp, err := dbs.rtdb.Insert(ml)
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "ins",
|
|
|
|
val: resp.Inserted,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "Duplicate primary key") {
|
|
|
|
fmt.Printf("RT Insert Err: %+v\n", err)
|
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cons.logins = append(cons.logins, login)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Bulk {
|
|
|
|
if dbs.isMongodb() {
|
|
|
|
for key, _ := range bulk {
|
|
|
|
_, err := bulk[key].Run()
|
2016-11-04 11:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "E11000") {
|
|
|
|
fmt.Printf("Err: %+v\n", err)
|
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: len(slogin[key]),
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: strings.Count(err.Error(), "E11000"),
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
cons.logins = append(cons.logins, slogin[key]...)
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
2015-11-24 11:38:01 +01:00
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
if dbs.isRethink() {
|
|
|
|
resp, err := dbs.rtdb.MultiInsert(rtbulk)
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "ins",
|
|
|
|
val: resp.Inserted,
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
if err != nil {
|
2016-11-10 09:01:10 +01:00
|
|
|
if !strings.Contains(err.Error(), "Duplicate primary key") {
|
2016-11-04 11:50:27 +01:00
|
|
|
cons.error = true
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "err",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
continue
|
|
|
|
} else {
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "dup",
|
|
|
|
val: 1,
|
|
|
|
}
|
2016-11-04 11:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 09:01:10 +01:00
|
|
|
cons.logins = append(cons.logins, slogin["rt"]...)
|
2016-11-03 17:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 15:37:32 +01:00
|
|
|
counter <- Counterchan{
|
|
|
|
tipo: "log",
|
|
|
|
val: len(prod.logins),
|
|
|
|
}
|
2015-11-23 17:39:17 +01:00
|
|
|
|
2016-05-27 12:11:51 +02:00
|
|
|
if opts.MaxLogins > -1 && len(prod.logins) < opts.MaxLogins {
|
2016-11-04 11:50:27 +01:00
|
|
|
cons.empty = false
|
2016-05-27 12:04:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-20 16:17:31 +01:00
|
|
|
if opts.Debug {
|
2016-11-03 17:42:37 +01:00
|
|
|
fmt.Printf("CONS: user=%s logins=%d in %v - active=%d\n", prod.user, len(prod.logins), time.Since(start), dbs.rdb.ActiveCount())
|
2015-11-20 16:17:31 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2016-11-04 11:50:27 +01:00
|
|
|
// wg.Done()
|
|
|
|
remove <- cons
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
|
|
|
}
|