Пример #1
0
func (m *UserMgr) Register(user *User) int {
	m.lock.Lock()
	defer m.lock.Unlock()
	err := db.Update(DB, C_USER, bson.M{"username": user.UserName}, *user)
	if err != nil {
		log.Warn(err)
		return RS.RS_register_failed
	}
	m.Users[user.UserName] = user
	return RS.RS_success
}
Пример #2
0
func NewRequest(r *http.Request) *Request {
	request := &Request{Time: time.Now()}
	request.Referer = r.Referer()
	request.URL = r.URL.String()
	request.Major = r.ProtoMajor
	request.RemoteAddr = r.RemoteAddr[:strings.LastIndex(r.RemoteAddr, ":")]
	request.UserAgent = useragent.ParseByRequest(r)
	sessionid, err := r.Cookie("SESSIONID")
	if err != nil {
		log.Warn(err)
	}
	request.SessionID = sessionid.Value
	return request
}
Пример #3
0
func (b *BaseData) loadData(typ string) {
	b.lock.RLock()
	defer b.lock.RUnlock()
	ms, c := db.Connect(DB, C_REQUEST)
	c.EnsureIndexKey("time")
	defer ms.Close()

	now := tm.New(time.Now())
	var Begin, End time.Time
	if typ == TODAY {
		Begin = now.BeginningOfDay()
		End = now.EndOfDay()
	} else if typ == YESTERDAY {
		Begin = now.BeginningOfDay().Add(-24 * time.Hour)
		End = now.EndOfDay().Add(-24 * time.Hour)
	}
	if typ == TODAY {
		err := c.Find(nil).Sort("-time").Skip(0).Limit(pageCount).All(&b.Latest)
		if err != nil {
			log.Error(err)
		}
	}
	count, err := c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Count()
	if err != nil {
		log.Error(err)
	}
	b.PV[typ] = count
	var sessions []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("sessionid", &sessions)
	if err != nil {
		log.Error(err)
	}
	b.UV[typ] = len(sessions)
	var ips []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("remoteaddr", &ips)
	if err != nil {
		log.Error(err)
	}
	b.China = make(map[string]*Area)
	b.World = make(map[string]*Area)
	for _, v := range ips {
		info, err := ip17mon.Find(v)
		if err != nil {
			log.Warn(err)
			continue
		}
		if info.Country == "中国" {
			if city := b.China[info.City]; city == nil {
				if info.Region == "台湾" {
					b.China[info.Region] = &Area{Name: info.Region, Value: 1}
					continue
				}
				b.China[info.City] = &Area{Name: info.Region, Value: 1}
			} else {
				city.Value++
			}
		} else {
			if country := b.World[info.Country]; country == nil {
				b.World[info.Country] = &Area{Name: info.Country, Value: 1}
			} else {
				country.Value++
			}
		}
	}

	b.IP[typ] = len(ips)
	var ts []*Request
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Select(bson.M{"time": 1}).All(&ts)
	if err != nil {
		log.Error(err)
	}
	b.TimePV[typ] = make([]int, 145)
	for _, v := range ts {
		b.TimePV[typ][ParseTime(v.Time)]++
	}
}