2016-11-10 17:26:17 +01:00
|
|
|
// mongo
|
2016-11-17 10:15:59 +01:00
|
|
|
package main
|
2016-11-10 17:26:17 +01:00
|
|
|
|
2016-11-17 10:09:23 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
// "gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mongo struct {
|
|
|
|
mdb *mgo.Session
|
|
|
|
ll *mgo.Collection
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
// MongoLogin structure
|
|
|
|
type MongoLogin struct {
|
|
|
|
ID string `json:"_id" bson:"_id" gorethink:"id"`
|
|
|
|
User string `json:"user" bson:"user" gorethink:"user"`
|
|
|
|
Protocol string `json:"protocol" bson:"protocol" gorethink:"protocol"`
|
|
|
|
IP string `json:"ip" bson:"ip" gorethink:"ip"`
|
|
|
|
Date time.Time `json:"date" bson:"date" gorethink:"date"`
|
|
|
|
Insert time.Time `json:"insert" bson:"insert" gorethink:"insert"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMongoDB(uri string, db string) (*Mongo, error) {
|
|
|
|
var err error
|
|
|
|
mdb, err := mgo.Dial(uri)
|
|
|
|
if err != nil {
|
|
|
|
return &Mongo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ll := mdb.DB(db).C(fmt.Sprintf("lastlogin_%s", opts.Month))
|
|
|
|
|
|
|
|
return &Mongo{
|
|
|
|
mdb: mdb,
|
|
|
|
ll: ll,
|
|
|
|
count: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mongo) Find(limit int) []MongoLogin {
|
|
|
|
q := m.ll.Find(nil).Sort("$natural").Skip(m.count).Limit(limit)
|
|
|
|
iter := q.Iter()
|
|
|
|
defer iter.Close()
|
|
|
|
|
|
|
|
var retval []MongoLogin
|
|
|
|
iter.All(&retval)
|
|
|
|
|
|
|
|
m.count += limit
|
|
|
|
|
|
|
|
return retval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mongo) Close() {
|
|
|
|
m.mdb.Close()
|
|
|
|
}
|