func (s *rankStrategy) NextAd(client *models.Client) (*models.Ad, log.ServerError) { isNew := s.isNewRand.Float32() < s.dbConfig.NewTrafficPercentage() ad, _ := s.getNewAd(isNew, client.Info) if ad != nil { return ad, nil } adIDs, ad, err := s.getAdIDs(isNew, client.Info) if ad != nil { return ad, nil } else if err != nil { return nil, log.New(http.StatusNotFound, notFoundDesc, err) } if adIDs == nil { return nil, log.NewError(http.StatusNotFound, notFoundDesc) } viewsPerAd, conversionsPerAd := s.getStatistics(adIDs) rankPerAd, totalRank := calculateRanks(viewsPerAd, conversionsPerAd) adID := s.chooseAd(adIDs, rankPerAd, totalRank) ad, err = s.db.Ads().GetAdByID(&adID) if err != nil { return nil, log.New(http.StatusNotFound, notFoundDesc, err) } return ad, nil }
func (c *appsCollection) GetApp(r *http.Request) (*App, log.ServerError) { token := r.URL.Query().Get("appToken") if token == "" { return nil, log.NewError(http.StatusBadRequest, "App token is not specified") } var app App err := c.Find(&bson.M{"appToken": token}).One(&app) if err != nil { return nil, log.New(http.StatusForbidden, fmt.Sprintf("app token %v is not found", token), err) } return &app, nil }