From 588244794c1a919ae7a390864e4e80c5522a1e54 Mon Sep 17 00:00:00 2001 From: Miki Date: Tue, 24 Nov 2015 17:56:34 +0100 Subject: [PATCH] prima di inserire controlla se esiste gia' un doc con stesso user e data; nel caso verifica se != IP e lo inserisce, altrimenti continua. --- consumer.go | 53 +++++++++++++++++++++++++++++++++++++---------------- dbs.go | 4 ++++ main.go | 2 +- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/consumer.go b/consumer.go index 86f47e3..9b22cd5 100644 --- a/consumer.go +++ b/consumer.go @@ -4,6 +4,7 @@ package main import ( "fmt" // "github.com/garyburd/redigo/redis" + "gopkg.in/mgo.v2/bson" "log" "strconv" "strings" @@ -16,6 +17,15 @@ type consumed struct { logins []string } +func contains(s []Ips, e string) bool { + for _, a := range s { + if a.Ip == e { + return true + } + } + return false +} + func consumer(id int) { // var conn = dbs.rdb.Get() @@ -72,26 +82,37 @@ func consumer(id int) { Ip: sval[2], Date: time.Unix(date, 0), } - ind := Index{ - User: prod.user, - Date: time.Unix(date, 0), - } - // inserisce il login su Mongodb - - retval, err := dbs.ll.Upsert(ind, ml) - if err != nil { - log.Printf("Insert error: %+v - %s\n", err, cons.user) - // se l'errore non e' "duplicate key error" salta al prossimo senza cancellare niente - count.AddErr() - cons.error = true - continue - } - if retval.Updated == 1 { + // cerca se esiste gia' un documento con gli stessi User & Date + docfind := []Ips{} + iter := dbs.ll.Find(bson.M{"user": prod.user, "date": time.Unix(date, 0)}).Select(bson.M{"ip": 1, "_id": 0}).Iter() + iter.All(&docfind) + if len(docfind) > 0 { count.AddDuplicate() + if !contains(docfind, ml.Ip) { + fmt.Printf("Insert != IP for same date: user=%s - date=%s\n - newip=%s - oldip=%s\n", ml.User, ml.Date, docfind, ml.Ip) + // inserisce il login su Mongodb se gli IP sono != + err := dbs.ll.Insert(ml) + if err != nil { + log.Printf("Insert error: %+v - %s\n", err, cons.user) + count.AddErr() + cons.error = true + continue + } + } + } else { + // inserisce il login su Mongodb + err := dbs.ll.Insert(ml) + if err != nil { + log.Printf("Insert error: %+v - %s\n", err, cons.user) + count.AddErr() + cons.error = true + continue + } } + iter.Close() if opts.Debug { - log.Printf("%+v - %+v\n", ml, retval) + log.Printf("%+v - %+v\n", ml) } if i < (len(prod.logins) - 1) { diff --git a/dbs.go b/dbs.go index ae87eae..cafbf21 100644 --- a/dbs.go +++ b/dbs.go @@ -33,6 +33,10 @@ type MongoLogin struct { Date time.Time `json:"date"` } +type Ips struct { + Ip string `json:"ip"` +} + type UserLogin struct { User string `json:"user"` Date time.Time `json:"date"` diff --git a/main.go b/main.go index 105a555..3aff306 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - _VERSION = "v2.2.6" + _VERSION = "v2.3.0" ) var (