Beispiel #1
0
// RetrieveContactcollectionViaElastic calls the RetrieveContacts method from Contacts via RPC (Search client). Returns results via jsonapi.
func RetrieveContactCollectionViaElastic(w http.ResponseWriter, r *http.Request) {

	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)
	args.Search = new(models.Search)
	args.Search.Query = router.Context(r).Param("query")

	err := contactClient.Call("Search.RetrieveContacts", args, &reply)
	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	if reply.Contacts == nil {
		reply.Contacts = make([]models.Contact, 0)
	}

	Success(w, r, views.Contacts{Contacts: reply.Contacts}, http.StatusOK)
}
Beispiel #2
0
// CreateFacts receive a polygon and retrieves every contact in this polygon. It then calls the CreateFact method for each contact.
func CreateFactsByShape(w http.ResponseWriter, r *http.Request) {
	var sourceArgs = &models.FactsJson{GroupID: router.Context(r).Env["GroupID"].(uint)}

	if err := Request(sourceArgs, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"sourceArgs": err.Error()}, http.StatusBadRequest)
		return
	}

	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)
	args.Search = new(models.Search)
	args.Search.Polygon = sourceArgs.Points
	args.Search.Filter = sourceArgs.Filter

	err := contactClient.Call("Search.SearchIDViaGeoPolygon", args, &reply)
	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	var (
		replyFacts = models.FactReply{}
		factArgs   = models.FactArgs{Fact: &models.Fact{GroupID: sourceArgs.GroupID}}
	)

	DefaultFact := &models.Fact{}
	DefaultFact = &models.Fact{}
	DefaultFact.GroupID = sourceArgs.GroupID
	DefaultFact.Action.Name = sourceArgs.Name
	DefaultFact.Action.Pitch = sourceArgs.Pitch
	DefaultFact.Action.TypeData = sourceArgs.TypeData
	DefaultFact.Status = sourceArgs.Status

	if reply.IDs != nil {
		for _, ID := range reply.IDs {
			factArgs.Fact = DefaultFact
			factArgs.Fact.ContactID = ID
			err := contactClient.Call("Fact.Create", factArgs, &replyFacts)
			if err != nil {
				Error(w, r, err.Error(), http.StatusInternalServerError)
				return
			}

			if replyFacts.Fact.Contact.ID == 0 {
				logs.Debug("Contact %d didn't exist. Fact %d may therefore be incorrect", ID, replyFacts.Fact.ID)
			}

			factArgs.Fact = replyFacts.Fact

			err = contactClient.Call("Search.IndexFact", factArgs, &replyFacts)
			if err != nil {
				Error(w, r, err.Error(), http.StatusInternalServerError)
				return
			}

			logs.Debug("Fact %d created and indexed", replyFacts.Fact.ID)

			replyFacts.Facts = append(replyFacts.Facts, *replyFacts.Fact)
		}
	}

	Success(w, r, views.Facts{Facts: replyFacts.Facts}, http.StatusOK)
}
Beispiel #3
0
// Implementation of elasticsearch, gets the query terms from the request's url and calls the SearchContacts method from Contacts via RPC (Search client). Returns results via jsonapi.
func SearchContact(w http.ResponseWriter, r *http.Request) {
	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)

	args.Search = new(models.Search)
	//args.Search.Query = router.Context(r).Param("query")

	if err := Request(args.Search, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"contact": err.Error()}, http.StatusBadRequest)
		return
	}

	// choix de la fonction selon le type de recherche souhaité
	var filtre = ""

	if args.Search.Fields[1] == "address" {
		// recherche sur les addresses
		filtre = "Search.SearchAddressesAggs"

	} else if args.Search.Fields[1] == "geoloc" {
		// recherche par geoloc avec tri par distance
		filtre = "Search.SearchContactsGeoloc"

	} else {
		// recherche sur les contacts
		filtre = "Search.SearchContacts"
	}

	// appel des fonctions
	err := contactClient.Call(filtre, args, &reply)

	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	// traitement des résultats selon le type de recherche --------------------------------------------
	if args.Search.Fields[1] == "address" {

		if reply.AddressAggs == nil {
			reply.AddressAggs = make([]models.AddressAggReply, 0)
		}
		Success(w, r, views.AddressAggs{AddressAggs: reply.AddressAggs}, http.StatusOK)

	} else if args.Search.Fields[1] == "geoloc" {

		if reply.Contacts == nil {
			reply.Contacts = make([]models.Contact, 0)
		}

		var cs models.AddressAggReply
		var temp_contact models.Contact
		var compteur int = 0
		var size_nb_address_aggrege int

		if len(args.Search.Fields) == 3 {
			j, err := strconv.Atoi(args.Search.Fields[2])
			if err == nil {
				size_nb_address_aggrege = j
			} else {
				// par défaut
				size_nb_address_aggrege = 50
			}
		} else {
			// par défaut
			size_nb_address_aggrege = 50
		}

		for pos, res_contact := range reply.Contacts {
			//control si nous avons le nombre de fiche résultats suffisants:
			if compteur == size_nb_address_aggrege {
				break
			}
			// si il y'a égalité
			if temp_contact.Address.Location == res_contact.Address.Location { //égalité
				cs.Contacts = append(cs.Contacts, res_contact)
				//si il n'y a pas d'égalité
			} else { //pas d'égalité
				if pos == 0 { //première itération
					cs.Contacts = append(cs.Contacts, res_contact)
				} else { //pas la première itération
					reply.AddressAggs = append(reply.AddressAggs, cs)
					compteur = compteur + 1
					// init de cs
					var cs2 models.AddressAggReply
					cs = cs2
					cs.Contacts = append(cs.Contacts, res_contact)
				}
			}
			temp_contact = res_contact
		}
		reply.AddressAggs = append(reply.AddressAggs, cs)
		Success(w, r, views.AddressAggs{AddressAggs: reply.AddressAggs}, http.StatusOK)

	} else { // pas geoloc, pas address
		if reply.Contacts == nil {
			reply.Contacts = make([]models.Contact, 0)
		}
		Success(w, r, views.Contacts{Contacts: reply.Contacts}, http.StatusOK)
	}

}