Ejemplo n.º 1
0
 def get_last_flow_run_fetch(self, obj):
     result = obj.get_task_result(constants.TaskType.fetch_runs)
     if result:
         return "%s (%d fetched)" % (format_datetime(
             ms_to_datetime(result['time'])), result.get(
                 'counts', {}).get('fetched', 0))
     else:
         return None
Ejemplo n.º 2
0
 def get_last_flow_run_fetch(self, obj):
     result = obj.get_task_result(constants.TaskType.fetch_runs)
     if result:
         return "%s (%d fetched)" % (
             format_datetime(ms_to_datetime(result['time'])),
             result.get('counts', {}).get('fetched', 0)
         )
     else:
         return None
Ejemplo n.º 3
0
 def get_last_contact_sync(self, obj):
     result = obj.get_task_result(TaskType.sync_contacts)
     if result:
         return "%s (%d created, %d updated, %d deleted, %d failed)" % (
             format_datetime(ms_to_datetime(result['time'])),
             result['counts']['created'], result['counts']['updated'],
             result['counts']['deleted'], result['counts']['failed'])
     else:
         return None
Ejemplo n.º 4
0
 def get_last_label_task(self, obj):
     result = obj.get_task_result(TaskType.label_messages)
     if result:
         when = format_datetime(ms_to_datetime(result['time']))
         num_messages = int(result['counts'].get('messages', 0))
         num_labelled = int(result['counts'].get('labelled', 0))
         return "%s (%d new messages, %d labelled)" % (when, num_messages, num_labelled)
     else:
         return None
Ejemplo n.º 5
0
 def get_last_label_task(self, obj):
     result = obj.get_task_result(TaskType.label_messages)
     if result:
         when = format_datetime(ms_to_datetime(result['time']))
         num_messages = int(result['counts'].get('messages', 0))
         num_labelled = int(result['counts'].get('labelled', 0))
         return "%s (%d new messages, %d labelled)" % (
             when, num_messages, num_labelled)
     else:
         return None
Ejemplo n.º 6
0
 def get_last_contact_sync(self, obj):
     result = obj.get_task_result(constants.TaskType.sync_contacts)
     if result:
         return "%s (%d created, %d updated, %d deleted, %d failed)" % (
             format_datetime(ms_to_datetime(result['time'])),
             result['counts']['created'],
             result['counts']['updated'],
             result['counts']['deleted'],
             result['counts']['failed'],
         )
     else:
         return None
Ejemplo n.º 7
0
def process_new_org_unsolicited(org):
    """
    Processes new unsolicited messages for an org in RapidPro
    """
    from .models import Message

    client = org.get_temba_client()

    # when was this task last run?
    last_result = org.get_task_result(TaskType.label_messages)
    if last_result:
        last_time = ms_to_datetime(last_result['time'])
    else:
        # if first time (or Redis bombed...) then we'll fetch back to 3 hours ago
        last_time = timezone.now() - timedelta(hours=3)

    this_time = timezone.now()

    num_messages = 0
    num_labelled = 0

    # grab all un-processed unsolicited messages
    pager = client.pager()
    while True:
        messages = client.get_messages(direction='I',
                                       _types=['I'],
                                       archived=False,
                                       after=last_time,
                                       before=this_time,
                                       pager=pager)
        num_messages += len(messages)
        num_labelled += Message.process_unsolicited(org, messages)

        if not pager.has_more():
            break

    print "Processed %d new unsolicited messages and labelled %d" % (
        num_messages, num_labelled)

    org.set_task_result(
        TaskType.label_messages, {
            'time': datetime_to_ms(this_time),
            'counts': {
                'messages': num_messages,
                'labelled': num_labelled
            }
        })
Ejemplo n.º 8
0
def process_new_org_unsolicited(org):
    """
    Processes new unsolicited messages for an org in RapidPro
    """
    from .models import Message

    client = org.get_temba_client()

    # when was this task last run?
    last_result = org.get_task_result(TaskType.label_messages)
    if last_result:
        last_time = ms_to_datetime(last_result['time'])
    else:
        # if first time (or Redis bombed...) then we'll fetch back to 3 hours ago
        last_time = timezone.now() - timedelta(hours=3)

    this_time = timezone.now()

    num_messages = 0
    num_labelled = 0

    # grab all un-processed unsolicited messages
    pager = client.pager()
    while True:
        messages = client.get_messages(direction='I', _types=['I'], archived=False,
                                       after=last_time, before=this_time, pager=pager)
        num_messages += len(messages)
        num_labelled += Message.process_unsolicited(org, messages)

        if not pager.has_more():
            break

    print "Processed %d new unsolicited messages and labelled %d" % (num_messages, num_labelled)

    org.set_task_result(TaskType.label_messages, {'time': datetime_to_ms(this_time),
                                                  'counts': {'messages': num_messages, 'labelled': num_labelled}})
Ejemplo n.º 9
0
 def get_last_flow_run_fetch(self, obj):
     result = obj.get_task_result(TaskType.fetch_runs)
     if result:
         return format_datetime(ms_to_datetime(result['time']))
     else:
         return None