예제 #1
0
def sk_sync_status(person_id, date=None, threshold=0):
    """Determines the sync status of an individual shopkeeper

  Args:
    person_id (str): Shopkeeper ID
    date (str, optional): ISO8601 date to use as a reference to determine the
      sync status. Defaults to the country's current date.
    threshold (int, optional): Number of days from the reference to determine
      the sync status. Defaults to 0.

  Returns:
    list: [ls, sync_status, sync_threshold] where ls = last sync, sync_status a 
      boolean and sync_threshold the ISO8601 date that determines synced/not 
      synced.
  """
    s = Search(using=elastic, index=CONFIG['ES']['PEOPLE']) \
      .query('term', doctype='client') \
      .query('term', person_id=person_id)

    res = s[:10000].execute()

    country = max([hit.country for hit in res.hits])

    if date is None:
        date = local_date_str(country)

    ls = sync_log.df(person_id=person_id)
    ls = ls['sync_date'].max()
    ls = '' if (isinstance(ls, float)) else ls
    sync_threshold = shift_date_str(date, days=-threshold)
    sync_status = True if ls >= sync_threshold else False

    return [ls, sync_status, sync_threshold]
예제 #2
0
def agent_sync_status(agent_id, date=None, threshold=0):
    """Determines the sync status of an individual agent.

  Args:
    agent_id (str): Agent ID
    date (str, optional): ISO8601 date to use as a reference to determine the
      sync status. Defaults to the country's current date.
    threshold (int, optional): Number of days from the reference to determine
      the sync status. Defaults to 0.

  Returns:
    list: [ls, sync_status, sync_threshold] where ls = last sync, sync_status a 
      boolean and sync_threshold the ISO8601 date that determines synced/not 
      synced.
  """
    info = hierarchy.employee_info(agent_id)
    country = info['country']
    agent_map = agent_mapping.df()

    if date is None:
        date = local_date_str(country)

    agent_id = (agent_map.loc[agent_id].squeeze()
                if agent_id in agent_map.index else agent_id)

    ls = sync_log.df(agent_id=agent_id)
    ls = ls['sync_date'].max()
    ls = '' if (isinstance(ls, float)) else ls
    sync_threshold = shift_date_str(date, days=-threshold)
    sync_status = True if ls >= sync_threshold else False

    return [ls, sync_status, sync_threshold]
예제 #3
0
def coordinator_agent_sync_status(coordinator_id, date=None, threshold=0):
    """Determines the sync status of all agents under a coordinator.

  Args:
    coordinator_id (str): Coordinator ID
    date (str, optional): ISO8601 date to use as a reference to determine the
      sync status. Defaults to the country's current date.
    threshold (int, optional): Number of days from the reference to determine
      the sync status. Defaults to 0.

  Returns:
    dict: { 'coordinator_id', 'count', 'synced', 'perc_synced' } where
      perc_synced = synced/count.
  """
    info = hierarchy.employee_info(coordinator_id)
    country = info['country']
    agent_map = agent_mapping.df()

    if date is None:
        date = local_date_str(country)

    agent_list = [
        agent_map.loc[x].squeeze() if x in agent_map.index else x
        for x in info['agent_id']
    ]

    ls = sync_log.df(agent_id=agent_list)
    ls = DataFrame(ls.groupby('agent_id').max()['sync_date'].fillna(''))
    ls['sync_status'] = ls['sync_date'].apply(
        lambda x: True
        if x >= shift_date_str(date, days=-threshold) else False)

    count = len(ls)
    synced = ls['sync_status'].sum()
    perc_synced = synced / count if count != 0 else 0

    obj = {
        'coordinator_id': coordinator_id,
        'count': count,
        'synced': synced,
        'perc_synced': perc_synced
    }

    df = DataFrame(obj, index=[0])

    return df
예제 #4
0
def sync_log(agent_id=None, person_id=None, f=None):
    return _sync_log.df(agent_id, person_id, f)