예제 #1
0
파일: topic.go 프로젝트: ovh/tat
// ListTopics returns list of topics, matching criterias
// /!\ user arg could be nil
func ListTopics(criteria *tat.TopicCriteria, u *tat.User, isAdmin, withTags, withLabels bool) (int, []tat.Topic, error) {
	var topics []tat.Topic

	username := "******"
	if u != nil {
		username = u.Username
	}
	k := cache.CriteriaKey(criteria, "tat", "users", username, "topics", "list_topics", "isAdmin", strconv.FormatBool(isAdmin), "withTags", strconv.FormatBool(withTags), "withLabels", strconv.FormatBool(withLabels))
	kcount := cache.CriteriaKey(criteria, "tat", "users", username, "topics", "count_topics")

	bytes, _ := cache.Client().Get(k).Bytes()
	if len(bytes) > 0 {
		json.Unmarshal(bytes, &topics)
	}

	ccount, _ := cache.Client().Get(kcount).Int64()
	if len(topics) > 0 && ccount > 0 {
		log.Debugf("ListTopics: topics (%s) loaded from cache", k)
		return int(ccount), topics, nil
	}

	cursor, errl := listTopicsCursor(criteria, u)
	if errl != nil {
		return -1, nil, errl
	}
	count, errc := cursor.Count()
	if errc != nil {
		return -1, nil, fmt.Errorf("Error while count Topics %s", errc)
	}
	oneTopic := false
	if criteria.Topic != "" {
		oneTopic = true
	}

	sortBy := criteria.SortBy
	if sortBy == "" {
		sortBy = "topic"
	}
	err := cursor.Select(GetTopicSelectedFields(isAdmin, withTags, withLabels, oneTopic)).
		Sort(sortBy).
		Skip(criteria.Skip).
		Limit(criteria.Limit).
		All(&topics)

	if err != nil {
		log.Errorf("Error while Find Topics %s", err)
		return -1, nil, err
	}

	cache.Client().Set(kcount, count, time.Hour)
	bytes, _ = json.Marshal(topics)
	if len(bytes) > 0 {
		log.Debugf("ListTopics: Put %s in cache", k)
		cache.Client().Set(k, string(bytes), time.Hour)
	}
	ku := cache.Key("tat", "users", username, "topics")
	cache.Client().SAdd(ku, k, kcount)
	cache.Client().SAdd(cache.Key(cache.TatTopicsKeys()...), ku, k, kcount)
	return count, topics, err
}
예제 #2
0
파일: group.go 프로젝트: ovh/tat
// ListGroups return all groups matching given criteria
func ListGroups(criteria *tat.GroupCriteria, user *tat.User, isAdmin bool) (int, []tat.Group, error) {
	var groups []tat.Group

	username := "******"
	if user != nil {
		username = user.Username
	}
	k := cache.CriteriaKey(criteria, "tat", "users", username, "isadmin", strconv.FormatBool(isAdmin), "groups", "list_groups")
	kcount := cache.CriteriaKey(criteria, "tat", "users", username, "isadmin", strconv.FormatBool(isAdmin), "groups", "count_groups")

	bytes, _ := cache.Client().Get(k).Bytes()
	if len(bytes) > 0 {
		json.Unmarshal(bytes, &groups)
	}

	ccount, _ := cache.Client().Get(kcount).Int64()
	if len(groups) > 0 && ccount > 0 {
		log.Debugf("ListGroups: groups (%s) loaded from cache", k)
		return int(ccount), groups, nil
	}

	cursor, errl := listGroupsCursor(criteria, user)
	if errl != nil {
		return -1, groups, errl
	}
	count, err := cursor.Count()
	if err != nil {
		log.Errorf("Error while count Groups %s", err)
	}

	selectedFields := bson.M{}
	if criteria.Name == "" {
		selectedFields = bson.M{"name": 1, "description": 1, "users": 1, "adminUsers": 1, "dateCreation": 1}
	}

	q := cursor.Select(selectedFields).
		Sort("name").
		Skip(criteria.Skip)

	if criteria.Limit > 0 {
		q.Limit(criteria.Limit)
	}

	if errq := q.All(&groups); errq != nil {
		log.Errorf("Error while Find All Groups %s", errq)
	}

	cache.Client().Set(kcount, count, time.Hour)
	bytes, _ = json.Marshal(groups)
	if len(bytes) > 0 {
		log.Debugf("ListGroups: Put %s in cache", k)
		cache.Client().Set(k, string(bytes), time.Hour)
	}
	ku := cache.Key("tat", "users", username, "groups")
	cache.Client().SAdd(ku, k, kcount)
	cache.Client().SAdd(cache.Key(cache.TatGroupsKeys()...), ku, k, kcount)
	return count, groups, err

}
예제 #3
0
파일: user.go 프로젝트: ovh/tat
//FindByUsername retrieve information from user with username
func FindByUsername(user *tat.User, username string) (bool, error) {

	//Load from cache
	bytes, err := cache.Client().Get(cache.Key("tat", "users", username)).Bytes()
	if err != nil && err != redis.Nil {
		log.Warnf("Unable to get user from cache")
		goto loadFromDB
	}
	json.Unmarshal(bytes, user)
	//If the user has beeen successfully loaded
	if user.Username != "" {
		return true, nil
	}

loadFromDB:
	err = store.Tat().CUsers.
		Find(bson.M{"username": username}).
		Select(fieldsExceptAuth).
		One(user)

	if err == mgo.ErrNotFound {
		log.Infof("FindByUsername username %s not found", username)
		return false, nil
	} else if err != nil {
		log.Errorf("Error while fetching user with username %s err:%s", username, err)
		return false, err
	}

	//Push to cache
	bytes, err = json.Marshal(user)
	if err != nil {
		return false, err
	}
	cache.Client().Set(cache.Key("tat", "users", username), string(bytes), 12*time.Hour)
	return true, nil
}