Skip to content

Commit

Permalink
Message backfill command can skip over a configurable label
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Mar 2, 2016
1 parent d14cd8f commit cfbaddf
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions casepro/msgs/management/commands/msgbackfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ def add_arguments(self, parser):
help="Whether to exclude labelled messages from the back-fill")
parser.add_argument('--no-recent', dest='ignore_recent', action='store_const', const=True, default=False,
help="Whether to exclude recent messages from the back-fill")
parser.add_argument('--exclude-label', dest='exclude_label', default=None,
help="Name of a label to exclude")

def handle(self, *args, **options):
org_ids = options['org_ids']
analyze = options['analyze']
ignore_actioned = options['ignore_actioned']
ignore_labelled = options['ignore_labelled']
ignore_recent = options['ignore_recent']
exclude_label = options['exclude_label']

if org_ids:
orgs = Org.objects.filter(pk__in=org_ids)
Expand All @@ -68,7 +71,7 @@ def handle(self, *args, **options):
if analyze:
self.analyze(org)
else:
self.backfill(org, ignore_actioned, ignore_labelled, ignore_recent)
self.backfill(org, ignore_actioned, ignore_labelled, ignore_recent, exclude_label)

def analyze(self, org):
self.stdout.write("Starting analysis for org %s [%d]..." % (org.name, org.pk))
Expand All @@ -80,14 +83,14 @@ def analyze(self, org):

self.stdout.write(" > Found %d message ids in cases and actions (%d missing locally)" % (len(rapidpro_msg_ids), num_missing))

def backfill(self, org, ignore_actioned, ignore_labelled, ignore_recent):
def backfill(self, org, ignore_actioned, ignore_labelled, ignore_recent, exclude_label):
self.stdout.write("Starting backfill for org %s [%d]..." % (org.name, org.pk))

if not ignore_actioned:
self.backfill_actioned(org)

if not ignore_labelled:
self.backfill_labelled(org)
self.backfill_labelled(org, exclude_label)

if not ignore_recent:
self.backfill_recent(org, num_weeks=NUM_WEEKS)
Expand Down Expand Up @@ -122,7 +125,7 @@ def backfill_actioned(self, org):

self.stdout.write(" - Synced %d messages..." % num_synced)

def backfill_labelled(self, org):
def backfill_labelled(self, org, exclude_label):
"""
Back-fills for each of the org's labels
"""
Expand All @@ -136,6 +139,10 @@ def progress_callback(num_fetched):
self.stdout.write(" - Synced %d messages (%d total v2 requests)..." % (num_fetched, self.api_v2_message_requests[org]))

for label in org.labels.all():
if exclude_label and label.name.lower() == exclude_label.lower():
self.stdout.write(" > Skipping messages for excluded label %s" % label.name)
continue

self.stdout.write(" > Fetching messages for label %s..." % label.name)

fetches = client.get_messages(label=label.name).iterfetches(retry_on_rate_exceed=True)
Expand Down

0 comments on commit cfbaddf

Please sign in to comment.