forked from reddit-archive/reddit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record trans_id for freebies in campaign things
Initial campiagn Thing writes were recording an enum value for freebie campaigns but it actually is useful to include the transaction id (which is negative for freebies) because it references the freebie's record in the bid table. Note: This commit also includes a one-off script for fixing the trans_id in existing campaign things.
- Loading branch information
Showing
3 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import sys | ||
from collections import defaultdict | ||
from r2.models import * | ||
|
||
def fix_trans_id(): | ||
bad_campaigns = list(PromoCampaign._query(PromoCampaign.c.trans_id == 1, data=True)) | ||
num_bad_campaigns = len(bad_campaigns) | ||
|
||
if not num_bad_campaigns: | ||
print "No campaigns with trans_id == 1" | ||
return | ||
|
||
# print some info and prompt user to continue | ||
print ("Found %d campaigns with trans_id == 1. \n" | ||
"Campaigns ids: %s \n" | ||
"Press 'c' to fix them or any other key to abort." % | ||
(num_bad_campaigns, [pc._id for pc in bad_campaigns])) | ||
input_char = sys.stdin.read(1) | ||
if input_char != 'c' and input_char != 'C': | ||
print "aborting..." | ||
return | ||
|
||
# log the ids for reference | ||
print ("Fixing %d campaigns with bad freebie trans_id: %s" % | ||
(num_bad_campaigns, [pc._id for pc in bad_campaigns])) | ||
|
||
# get corresponding links and copy trans_id from link data to campaign thing | ||
link_ids = set([campaign.link_id for campaign in bad_campaigns]) | ||
print "Fetching associated links: %s" % link_ids | ||
try: | ||
links = Link._byID(link_ids, data=True, return_dict=False) | ||
except NotFound, e: | ||
print("Invalid data: Some promocampaigns have invalid link_ids. " | ||
"Please delete these campaigns or fix the data before " | ||
"continuing. Exception: %s" % e) | ||
|
||
# organize bad campaigns by link_id | ||
bad_campaigns_by_link = defaultdict(list) | ||
for c in bad_campaigns: | ||
bad_campaigns_by_link[c.link_id].append(c) | ||
|
||
# iterate through links and copy trans_id from pickled list on the link to | ||
# the campaign thing | ||
failed = [] | ||
for link in links: | ||
link_campaigns = getattr(link, "campaigns") | ||
thing_campaigns = bad_campaigns_by_link[link._id] | ||
for campaign in thing_campaigns: | ||
try: | ||
sd, ed, bid, sr_name, trans_id = link_campaigns[campaign._id] | ||
if trans_id != campaign.trans_id: | ||
campaign.trans_id = trans_id | ||
campaign._commit() | ||
except: | ||
failed.append({ | ||
'link_id': link._id, | ||
'campaign_id': campaign._id, | ||
'exc type': sys.exc_info()[0], | ||
'exc msg': sys.exc_info()[1] | ||
}) | ||
|
||
# log the actions for future reference | ||
msg = ("%d of %d campaigns updated successfully. %d updates failed: %s" % | ||
(num_bad_campaigns, num_bad_campaigns - len(failed), len(failed), failed)) | ||
print msg | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters