func (f BasicFilter) Do(ad *common.Ad, req *common.BidRequest) (int, bool) {
	if !ad.Active {
		println("basic filter ad active false: ", ad.Id)
		return 0, false
	}
	if req.Slots == nil || len(req.Slots) == 0 {
		println("basic filter: req slots 0")
		return 0, false
	}
	/* Now, we only deal with situation len(slots) == 1 */
	if req.Slots[0].W != ad.W || req.Slots[0].H != ad.H {
		println("basic filter: ad w,h error", ad.Id, " ad.W:", ad.W, "ad.H:", ad.H)
		return 0, false
	}

	/* type filter */
	if len(req.Slots[0].CreativeType) > 0 {
		typeIllegal := true
		for _, ctype := range req.Slots[0].CreativeType {
			println("basic filter: creative type: ", ctype, "ad type: ", ad.Type)
			if common.AdType(ctype) == ad.Type {
				typeIllegal = false
				break
			}
		}
		if typeIllegal {
			println("basic filter: type illegal: ", ad.Type)
			return 0, false
		}
	}
	println("basic filter pass")
	return 0, true
}
Example #2
0
func (c *Command) AddAd() bool {
	var data []interface{}
	if dataSlice, ok := c.Data.([]interface{}); ok {
		data = dataSlice
	} else if d, ok := c.Data.(map[string]interface{}); ok {
		data = make([]interface{}, 0, 1)
		data = append(data, d)
	} else {
		managerLogger.Log(logger.ERROR, "in AddAd data err")
		return false
	}

	type AdAddFmt struct {
		Ad_id              string
		Order_id           string
		Ad_type            string
		Duration           string
		Mimes              string
		Channel            string
		Cs                 string
		Size               string
		Tmp_name           string
		Priority           string
		Clickmonitor_url   string
		Displaymonitor_url string
		Thirdparty_url     string
		Landingpage_url    string
		Creative_url       string
		Html_snippet       string
		Exchange           []string // 在哪儿?
		Category           []string
		Category_product   []string
		Attribute          string
		Buyer_creative_id  string
		Advertiser_name    string
		Active             string

		Url_in    []string
		Url_out   []string
		Url_price []map[string]string

		Slotid_in    []string
		Slotid_out   []string
		Slotid_price []map[string]string
	}

	// len(data) should == 1
	if len(data) != 1 {
		managerLogger.Log(logger.ERROR, "add ad data slice length should be 1")
		return false
	}
	for _, d := range data {
		var adFmt AdAddFmt
		b, e := json.Marshal(d)
		if e != nil {
			managerLogger.Log(logger.ERROR, "Marshal json err: ", e)
			continue
		}
		dec := json.NewDecoder(bytes.NewReader(b))
		if e := dec.Decode(&adFmt); e != nil {
			continue
		}

		var ad common.Ad
		func(dst *common.Ad, src *AdAddFmt) {
			dst.Id = src.Ad_id
			dst.OrderId = src.Order_id
			switch src.Ad_type {
			case "1":
				dst.Type = common.AdType(common.Banner)
			case "2":
				dst.Type = common.AdType(common.Video)
			default:
				managerLogger.Log(logger.ERROR, "Ad_type unknow: ", src.Ad_type)
				dst.Type = common.AdType(common.AdtypeUnknown)
			}
			dst.Duration, _ = strconv.Atoi(src.Duration)
			switch src.Mimes {
			case "video":
				dst.Mime = common.MimeType(common.MimeVideo)
			case "x-flv":
				dst.Mime = common.MimeType(common.MimeX_FLV)
			default:
				dst.Mime = common.MimeType(common.MimeUnknown)
			}
			dst.Ch1 = src.Channel
			dst.Ch2 = src.Cs
			s := strings.SplitN(src.Size, "x", 2)
			if len(s) != 2 {
				s = strings.SplitN(src.Size, "X", 2)
			}
			if len(s) == 2 {
				w, _ := strconv.Atoi(s[0])
				h, _ := strconv.Atoi(s[1])
				if w > 0 && h > 0 {
					dst.W = w
					dst.H = h
				}
			}
			dst.TmpName = src.Tmp_name
			dst.Priority, _ = strconv.Atoi(src.Priority)
			dst.UrlDisplayMonitor = src.Displaymonitor_url
			dst.UrlClickMonitor = src.Clickmonitor_url
			dst.UrlThirdMonitor = src.Thirdparty_url
			dst.UrlLanding = src.Landingpage_url
			dst.UrlCreative = src.Creative_url
			dst.HtmlSnippet = src.Html_snippet
			dst.Category = make([]int, 0, len(src.Category))
			for _, cat := range src.Category {
				if c, err := strconv.Atoi(cat); err == nil {
					dst.Category = append(dst.Category, c)
				}
			}
			dst.CategoryProduct = make([]int, 0, len(src.Category_product))
			for _, cp := range src.Category_product {
				if c, err := strconv.Atoi(cp); err == nil {
					dst.CategoryProduct = append(dst.CategoryProduct, c)
				}
			}
			if attrNum, err := strconv.Atoi(src.Attribute); err != nil {
				dst.Attr = common.Attribute(attrNum)
			}
			dst.CreativeId, _ = strconv.Atoi(src.Buyer_creative_id)
			switch src.Active {
			case "0":
				dst.Active = false
			default:
				dst.Active = true
			}

			/* for url filter */
			if src.Url_out == nil {
				dst.UrlOut = nil
			} else {
				dst.UrlOut = make(map[string]bool)
				for _, url := range src.Url_out {
					dst.UrlOut[url] = true
				}
			}
			if src.Url_in == nil {
				dst.UrlIn = nil
				dst.UrlPrice = nil
			} else {
				dst.UrlIn = make(map[string]bool)
				for _, url := range src.Url_in {
					dst.UrlIn[url] = true
				}
				if src.Url_price != nil {
					dst.UrlPrice = make(map[string]int)
					for _, m := range src.Url_price {
						url := m["Url"]
						price, _ := strconv.Atoi(m["Price"])
						dst.UrlPrice[url] = price
					}
				}
			}

			/* for slot filter */
			if src.Slotid_out == nil {
				dst.SlotOut = nil
			} else {
				dst.SlotOut = make(map[string]bool)
				for _, slot := range src.Slotid_out {
					dst.SlotOut[slot] = true
				}
			}
			if src.Slotid_in == nil {
				dst.SlotIn = nil
				dst.SlotPrice = nil
			} else {
				dst.SlotIn = make(map[string]bool)
				for _, slot := range src.Slotid_in {
					dst.SlotIn[slot] = true
				}
				if src.Slotid_price != nil {
					dst.SlotPrice = make(map[string]int)
					for _, m := range src.Slotid_price {
						slot := m["Slotid"]
						price, _ := strconv.Atoi(m["Price"])
						dst.SlotPrice[slot] = price
					}
				}
			}
		}(&ad, &adFmt)
		if _, err := common.GOrderContainer.Find(ad.OrderId); err == nil {
			nad := common.GAdContainer.Add(&ad)
			managerLogger.Log(logger.INFO, "add ad successfully: ", d, ", ads: ", nad)
			return true
		} else {
			managerLogger.Log(logger.ERROR, "add ad error: ", d, ", err: ", err)
			return false
		}
	}
	return false
}