Exemple #1
0
func LoadConf() *Config {
	conf := &Config{SiteVerify: make(map[string]*Verification)}
	ms, c := db.Connect(DB, C_CONFIG)
	defer ms.Close()
	tmp := make(map[string]string)
	err := c.Find(nil).Select(bson.M{SITE_VERIFY: 1, "_id": 0}).One(tmp)
	if err != nil && !strings.Contains(err.Error(), "not found") {
		log.Error(err)
	}
	if str := tmp[SITE_VERIFY]; str != "" {
		err := json.Unmarshal([]byte(str), &conf.SiteVerify)
		if err != nil {
			log.Error(err)
		}
	}
	return conf
}
Exemple #2
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)]++
	}
}