def get_campaigns_by_date(srs, start, end, ignore=None): srs, is_single = tup(srs, ret_is_single=True) sr_names = ['' if isinstance(sr, DefaultSR) else sr.name for sr in srs] dates = set(get_date_range(start, end)) q = (PromotionWeights.query().filter( PromotionWeights.sr_name.in_(sr_names)).filter( PromotionWeights.date.in_(dates))) if ignore: q = q.filter(PromotionWeights.promo_idx != ignore._id) campaign_ids = {pw.promo_idx for pw in q} campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) transaction_ids = { camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION } if transaction_ids: transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) transaction_by_id = {bid.transaction: bid for bid in transactions} else: transaction_by_id = {} ret = {sr.name: dict.fromkeys(dates) for sr in srs} for srname, date_dict in ret.iteritems(): for date in date_dict: ret[srname][date] = [] for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[camp.trans_id] if not (transaction.is_auth() or transaction.is_charged()): continue sr_name = camp.sr_name or DefaultSR.name camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): ret[sr_name][date].append(camp) if is_single: return ret[srs[0].name] else: return ret
def get_campaigns_by_date(srs, start, end, ignore=None): srs, is_single = tup(srs, ret_is_single=True) sr_names = ['' if isinstance(sr, DefaultSR) else sr.name for sr in srs] dates = set(get_date_range(start, end)) q = (PromotionWeights.query() .filter(PromotionWeights.sr_name.in_(sr_names)) .filter(PromotionWeights.date.in_(dates))) if ignore: q = q.filter(PromotionWeights.promo_idx != ignore._id) campaign_ids = {pw.promo_idx for pw in q} campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) transaction_ids = {camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION} if transaction_ids: transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) transaction_by_id = {bid.transaction: bid for bid in transactions} else: transaction_by_id = {} ret = {sr.name: dict.fromkeys(dates) for sr in srs} for srname, date_dict in ret.iteritems(): for date in date_dict: ret[srname][date] = [] for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[camp.trans_id] if not (transaction.is_auth() or transaction.is_charged()): continue sr_names = camp.target.subreddit_names camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): for sr_name in sr_names: ret[sr_name][date].append(camp) if is_single: return ret[srs[0].name] else: return ret
def get_campaigns_by_date(srs, start, end, ignore=None): srs = tup(srs) sr_names = [sr.name for sr in srs] campaign_ids = PromotionWeights.get_campaign_ids(start, end=end, sr_names=sr_names) if ignore: campaign_ids.discard(ignore._id) campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) # filter out deleted campaigns that didn't have their PromotionWeights # deleted campaigns = filter(lambda camp: not camp._deleted, campaigns) transaction_ids = { camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION } if transaction_ids: transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) # index transactions by transaction and campaign id because freebies # reuse the same transaction id (they always use -link id) transaction_by_id = {(bid.transaction, bid.campaign): bid for bid in transactions} else: transaction_by_id = {} dates = set(get_date_range(start, end)) ret = {date: set() for date in dates} for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[(camp.trans_id, camp._id)] if not (transaction.is_auth() or transaction.is_charged()): continue camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): ret[date].add(camp) return ret
def get_transactions(link, campaigns): """Return Bids for specified campaigns on the link. A PromoCampaign can have several bids associated with it, but the most recent one is recorded on the trans_id attribute. This is the one that will be returned. """ campaigns = [c for c in campaigns if (c.trans_id != 0 and c.link_id == link._id)] if not campaigns: return {} bids = Bid.lookup(thing_id=link._id) bid_dict = {(b.campaign, b.transaction): b for b in bids} bids_by_campaign = {c._id: bid_dict[(c._id, c.trans_id)] for c in campaigns} return bids_by_campaign
def promote_v2(): # alter table bids add column campaign integer; # update bids set campaign = 0; from r2.models import Link, NotFound, PromoteDates, Bid from datetime import datetime from pylons import g for p in PromoteDates.query(): try: l = Link._by_fullname(p.thing_name, data = True, return_dict = False) if not l: raise NotFound, p.thing_name # update the promote status l.promoted = True l.promote_status = getattr(l, "promote_status", STATUS.unseen) l._date = datetime(*(list(p.start_date.timetuple()[:7]) + [g.tz])) set_status(l, l.promote_status) # add new campaign print (l, (p.start_date, p.end_date), p.bid, None) if not p.bid: print "no bid? ", l p.bid = 20 new_campaign(l, (p.start_date, p.end_date), p.bid, None) print "updated: %s (%s)" % (l, l._date) except NotFound: print "NotFound: %s" % p.thing_name print "updating campaigns" for b in Bid.query(): l = Link._byID(int(b.thing_id)) print "updating: ", l campaigns = getattr(l, "campaigns", {}).copy() indx = b.campaign if indx in campaigns: sd, ed, bid, sr, trans_id = campaigns[indx] campaigns[indx] = sd, ed, bid, sr, b.transaction l.campaigns = campaigns l._commit() else: print "no campaign information: ", l
def get_campaigns_by_date(srs, start, end, ignore=None): srs = tup(srs) sr_names = [sr.name for sr in srs] campaign_ids = PromotionWeights.get_campaign_ids( start, end=end, sr_names=sr_names) if ignore: campaign_ids.discard(ignore._id) campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) # filter out deleted campaigns that didn't have their PromotionWeights # deleted campaigns = filter(lambda camp: not camp._deleted, campaigns) transaction_ids = {camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION} if transaction_ids: transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) # index transactions by transaction and campaign id because freebies # reuse the same transaction id (they always use -link id) transaction_by_id = { (bid.transaction, bid.campaign): bid for bid in transactions} else: transaction_by_id = {} dates = set(get_date_range(start, end)) ret = {date: set() for date in dates} for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[(camp.trans_id, camp._id)] if not (transaction.is_auth() or transaction.is_charged()): continue camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): ret[date].add(camp) return ret
def get_sold_pageviews(srs, start, end, ignore=None): srs, is_single = tup(srs, ret_is_single=True) sr_names = ['' if isinstance(sr, DefaultSR) else sr.name for sr in srs] dates = set(get_date_range(start, end)) ignore = [] if ignore is None else ignore q = (PromotionWeights.query().filter( PromotionWeights.sr_name.in_(sr_names)).filter( PromotionWeights.date.in_(dates))) campaign_ids = {pw.promo_idx for pw in q} campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) transaction_ids = { camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION } transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) transaction_by_id = {bid.transaction: bid for bid in transactions} ret = {sr.name: dict.fromkeys(dates, 0) for sr in srs} for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if ignore and camp._id in ignore: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[camp.trans_id] if not (transaction.is_auth() or transaction.is_charged()): continue sr_name = camp.sr_name or DefaultSR.name daily_impressions = camp.impressions / camp.ndays camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): ret[sr_name][date] += daily_impressions if is_single: return ret[srs[0].name] else: return ret
def get_sold_pageviews(srs, start, end, ignore=None): srs, is_single = tup(srs, ret_is_single=True) sr_names = ['' if isinstance(sr, DefaultSR) else sr.name for sr in srs] dates = set(get_date_range(start, end)) ignore = [] if ignore is None else ignore q = (PromotionWeights.query() .filter(PromotionWeights.sr_name.in_(sr_names)) .filter(PromotionWeights.date.in_(dates))) campaign_ids = {pw.promo_idx for pw in q} campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False) transaction_ids = {camp.trans_id for camp in campaigns if camp.trans_id != NO_TRANSACTION} transactions = Bid.query().filter(Bid.transaction.in_(transaction_ids)) transaction_by_id = {bid.transaction: bid for bid in transactions} ret = {sr.name: dict.fromkeys(dates, 0) for sr in srs} for camp in campaigns: if camp.trans_id == NO_TRANSACTION: continue if ignore and camp._id in ignore: continue if camp.impressions <= 0: # pre-CPM campaign continue transaction = transaction_by_id[camp.trans_id] if not (transaction.is_auth() or transaction.is_charged()): continue sr_name = camp.sr_name or DefaultSR.name daily_impressions = camp.impressions / camp.ndays camp_dates = set(get_date_range(camp.start_date, camp.end_date)) for date in camp_dates.intersection(dates): ret[sr_name][date] += daily_impressions if is_single: return ret[srs[0].name] else: return ret