Пример #1
0
func (c *statisticsCollection) GetLast(adID *bson.ObjectId, n int) (*Statistic, log.ServerError) {
	var result Statistic
	err := c.Find(bson.M{"adId": *adID}).Sort("-at").Skip(n - 1).One(&result)
	if err != nil {
		return nil, log.NewInternalError(err)
	}

	return &result, nil
}
Пример #2
0
func (c *controller) Click(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	view, err := getStatistic(p.ByName("viewId"), c.app.Database().Views())
	if err != nil {
		c.writeError(w, err)
		return
	}

	clickID := c.app.Database().Clicks().SaveNextStatistic(view)
	ad, e := c.app.Database().Ads().GetAdByID(&view.AdID)
	if e != nil {
		c.writeError(w, log.NewInternalError(e))
		return
	}

	conversion, err := c.app.Database().Conversions().GetLast(&view.AdID, 1)
	if err == nil {
		click, err := c.app.Database().Clicks().GetLast(&view.AdID, 100)
		if err == nil && click != nil && (conversion == nil || click.At > conversion.At) {
			toggleAd(&view.AdID, c.app.Database(), false)
		}
	}

	url, e := url.ParseRequestURI(ad.ConversionURL + clickID.Hex())
	if e != nil {
		c.writeError(w, log.NewInternalError(e))
		return
	}

	d := func(req *http.Request) {
		req = r
		req.URL.Scheme = url.Scheme
		req.URL.Host = url.Host
		req.URL.Path = url.Path
		req.URL.RawQuery = url.RawQuery
	}

	proxy := &httputil.ReverseProxy{Director: d}
	proxy.ServeHTTP(w, r)
}
Пример #3
0
func (c *statisticsCollection) GetById(id *string) (*Statistic, log.ServerError) {
	if !bson.IsObjectIdHex(*id) {
		return nil, log.NewError(http.StatusBadRequest, "provided value is not valid object id")
	}

	var result Statistic
	err := c.FindId(bson.ObjectIdHex(*id)).One(&result)
	if err != nil {
		return nil, log.NewInternalError(err)
	}

	return &result, nil
}
Пример #4
0
func (c *controller) View(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	database := c.app.Database()
	apps := database.Apps()

	t := getAdType(r)
	if t == "" {
		c.writeError(w, log.NewError(http.StatusBadRequest, "type of request ad is not specified"))
		return
	}

	devApp, err := apps.GetApp(r)
	if err != nil {
		c.writeError(w, err)
		return
	}

	client, err := models.GetClient(c.app.GeoIP(), r)
	if err != nil {
		c.writeError(w, err)
		return
	}

	ad, err := c.strategy.NextAd(client)
	if err != nil {
		c.writeError(w, err)
		return
	}

	viewId := database.Views().SaveStatistic(ad.ID, devApp.ID, client)
	adInfo := getInfo(t, viewId, ad, r)

	jsonAd, e := json.Marshal(adInfo)
	if e != nil {
		c.writeError(w, log.NewInternalError(e))
		return
	}

	w.Write(jsonAd)
}