prima di inserire controlla se esiste gia' un doc con stesso user e data; nel caso verifica se != IP e lo inserisce, altrimenti continua.

This commit is contained in:
Miki 2015-11-24 17:56:34 +01:00
parent 6c3ffa7c7b
commit 588244794c
3 changed files with 42 additions and 17 deletions

View file

@ -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) {

4
dbs.go
View file

@ -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"`

View file

@ -11,7 +11,7 @@ import (
)
const (
_VERSION = "v2.2.6"
_VERSION = "v2.3.0"
)
var (