Exemplo n.º 1
0
def get_reports():
    # extract hub_name` argument from request
    hub_name = request.args.get('hub_name', None)

    #  extract plan_type
    plan_type = request.args.get('plan_type', None)

    #  extract from
    from_d = request.args.get('from', None)

    #  extract to
    to_d = request.args.get('to', None)

    # By default, hub=None signify all hubs
    hub = None

    # if hub_name is passed in request arguments, then get hub's instance
    if hub_name:
        hub = Hub.first(name=hub_name)
        if not hub:
            return ({
                'error': 'No such hub found'
            }, status.HTTP_400_BAD_REQUEST)

    if plan_type:
        if plan_type not in PLAN_TYPES:
            return ({
                'error': 'No such plan type found'
            }, status.HTTP_400_BAD_REQUEST)

    if from_d or to_d:
        if not (is_date_format_valid(from_d, '%Y-%m')
                or is_date_format_valid(to_d, '%Y-%m')):
            return ({
                'error': 'Date should be in YYYY-MM format'
            }, status.HTTP_400_BAD_REQUEST)

    # intializise list to have results to return as response
    res = list()

    # get all hub plans for all above plans
    hub_plans = get_all_hub_plans_of_plan_type(hub, plan_type)

    # get member report's for all hub_plan's
    m_reports = get_all_member_reports_of_hub_plans(hub_plans, from_d, to_d)

    # serialize all member report's and append to them in result
    for mr in m_reports:
        res.append(mr.serialize())

    return (res, status.HTTP_200_OK)
Exemplo n.º 2
0
def get_reports():
    # extract hub_name` argument from request
    hub_name = request.args.get('hub_name', None)

    #  extract plan_type
    plan_type = request.args.get('plan_type', None)

    #  extract from
    from_d = request.args.get('from', None)

    #  extract to
    to_d = request.args.get('to', None)

    # By default, hub=None signify all hubs
    hub = None

    # if hub_name is passed in request arguments, then get hub's instance
    if hub_name:
        hub = Hub.first(name=hub_name)
        if not hub:
            return ({'error': 'No such hub found'},
                    status.HTTP_400_BAD_REQUEST)

    if plan_type:
        if plan_type not in PLAN_TYPES:
            return ({'error': 'No such plan type found'},
                    status.HTTP_400_BAD_REQUEST)

    if from_d or to_d:
        if not (is_date_format_valid(from_d, '%Y-%m') or
                is_date_format_valid(to_d, '%Y-%m')):
            return ({'error': 'Date should be in YYYY-MM format'},
                    status.HTTP_400_BAD_REQUEST)

    # intializise list to have results to return as response
    res = list()

    # get all hub plans for all above plans
    hub_plans = get_all_hub_plans_of_plan_type(hub, plan_type)

    # get member report's for all hub_plan's
    m_reports = get_all_member_reports_of_hub_plans(hub_plans, from_d, to_d)

    # serialize all member report's and append to them in result
    for mr in m_reports:
        res.append(mr.serialize())

    return (res, status.HTTP_200_OK)
Exemplo n.º 3
0
def get_all_member_reports_of_hub_plans(hub_plans, from_d=None, to_d=None):
    """
    """
    hub_plan_ids = [hp.id for hp in hub_plans if isinstance(hp, HubPlan)]

    query = and_(MemberReport.hub_plan_id.in_(hub_plan_ids))

    if is_date_format_valid(from_d, '%Y-%m'):
        date_str = get_first_date_of_month(from_d)
        query = and_(query, MemberReport.time.has(Time.date >= date_str))

    if is_date_format_valid(to_d, '%Y-%m'):
        date_str = get_first_date_of_month(to_d)
        query = and_(query, MemberReport.time.has(Time.date <= date_str))

    return MemberReport.find(query)
Exemplo n.º 4
0
def get_all_member_reports_of_hub_plans(hub_plans, from_d=None, to_d=None):
    """
    """
    hub_plan_ids = [hp.id for hp in hub_plans if isinstance(hp, HubPlan)]

    query = and_(MemberReport.hub_plan_id.in_(hub_plan_ids))

    if is_date_format_valid(from_d, '%Y-%m'):
            date_str = get_first_date_of_month(from_d)
            query = and_(query, MemberReport.time.has(Time.date >= date_str))

    if is_date_format_valid(to_d, '%Y-%m'):
            date_str = get_first_date_of_month(to_d)
            query = and_(query, MemberReport.time.has(Time.date <= date_str))

    return MemberReport.find(query)
Exemplo n.º 5
0
def get_data_from_api_of_hub(date_str, hub):
    """
    Get data of memberships plans from cobot api of a particular specified day
    """
    # check date should be in valid format(i.e YYYY-MM-DD)
    if not is_date_format_valid(date_str):
        return None

    # check if hub instance is passed or not
    if not isinstance(hub, Hub):
        return None

    token = 'Bearer %s' % config.COBOT_TOKEN

    # create a API end-point url to get data
    membership_url = config.MEMBERSHIPS_URL_STR % hub.name

    # set Authorization header field
    headers = {
        'Authorization': token
    }

    # set arbitrary arguments to be passed with request
    params = {
        'as_of': date_str
    }

    # create a file name with hub_name as directory
    # <hub_name>/membership-<date_str>.json
    file_name = "{0}/memberships-{1}.json".format(hub.name, date_str)

    # get data from dumped file if exists
    data = get_data_from_file_if_exists(file_name)

    if data:
        return data

    logger.info("Requesting data from cobot of url {0} and date {0}".format(
        membership_url, date_str))

    # call end-point, send request and collect response
    response = requests.get(membership_url, headers=headers, params=params)

    # if call was made successfully, return data in JSON format
    if response.status_code == 200:
        logger.info('Data returned from api successfully of hub %s' % hub.name)
        data = response.json()

        # also dump data to file
        dump_data_to_file(data, file_name=file_name)
        return data
    else:
        logger.info('Data could not returned from api successfully of hub '
                    '{0} with status code {1}'.format(hub.name,
                                                      response.status_code))

    return None
Exemplo n.º 6
0
def get_data_from_api_of_hub(date_str, hub):
    """
    Get data of memberships plans from cobot api of a particular specified day
    """
    # check date should be in valid format(i.e YYYY-MM-DD)
    if not is_date_format_valid(date_str):
        return None

    # check if hub instance is passed or not
    if not isinstance(hub, Hub):
        return None

    token = 'Bearer %s' % config.COBOT_TOKEN

    # create a API end-point url to get data
    membership_url = config.MEMBERSHIPS_URL_STR % hub.name

    # set Authorization header field
    headers = {'Authorization': token}

    # set arbitrary arguments to be passed with request
    params = {'as_of': date_str}

    # create a file name with hub_name as directory
    # <hub_name>/membership-<date_str>.json
    file_name = "{0}/memberships-{1}.json".format(hub.name, date_str)

    # get data from dumped file if exists
    data = get_data_from_file_if_exists(file_name)

    if data:
        return data

    logger.info("Requesting data from cobot of url {0} and date {0}".format(
        membership_url, date_str))

    # call end-point, send request and collect response
    response = requests.get(membership_url, headers=headers, params=params)

    # if call was made successfully, return data in JSON format
    if response.status_code == 200:
        logger.info('Data returned from api successfully of hub %s' % hub.name)
        data = response.json()

        # also dump data to file
        dump_data_to_file(data, file_name=file_name)
        return data
    else:
        logger.info('Data could not returned from api successfully of hub '
                    '{0} with status code {1}'.format(hub.name,
                                                      response.status_code))

    return None
Exemplo n.º 7
0
def get_and_process_data_of_day(date_str, hub_name):
    """
    Get data of a particular given day and also process that data
    """
    # check date should be in valid format(i.e YYYY-MM-DD)
    if not is_date_format_valid(date_str):
        return None

    # check if hub_name is passed or not, if not then get all hubs to
    # process, otherwise just process data only for that passed hub
    hubs = Hub.find(name=hub_name) if hub_name else Hub.get_all()

    for hub in hubs:
        # get data of a hub for a day
        data = get_data_from_api_of_hub(date_str, hub=hub)

        if data:
            # process a data of a hub
            process_data_of_hub(hub, data, date_of_crawl=date_str)
Exemplo n.º 8
0
def get_and_process_data_of_day(date_str, hub_name):
    """
    Get data of a particular given day and also process that data
    """
    # check date should be in valid format(i.e YYYY-MM-DD)
    if not is_date_format_valid(date_str):
        return None

    # check if hub_name is passed or not, if not then get all hubs to
    # process, otherwise just process data only for that passed hub
    hubs = Hub.find(name=hub_name) if hub_name else Hub.get_all()

    for hub in hubs:
        # get data of a hub for a day
        data = get_data_from_api_of_hub(date_str, hub=hub)

        if data:
            # process a data of a hub
            process_data_of_hub(hub, data, date_of_crawl=date_str)
Exemplo n.º 9
0
def calculate_member_report_metrics_of_a_month(date_str, hub_name):
    """
    Calculate member report metrics for a given month from present data
    in database for all hub_plan's
    """
    if not is_date_format_valid(date_str, '%Y-%m'):
        return None

    # store all hub_plan's to process
    hub_plans = []

    # get all hub_plan's to process
    if hub_name:
        h = Hub.first(name=hub_name)
        hub_plans = HubPlan.find(hub=h)
    else:
        hub_plans = HubPlan.get_all()

    # calculate member report metrics for each plan of all hub
    for hub_plan in hub_plans:
        get_and_set_member_report_metrics_of_hub_plan(hub_plan, date_str)

    print "Total %s plans of all hub processed." % len(hub_plans)
Exemplo n.º 10
0
def calculate_member_report_metrics_of_a_month(date_str, hub_name):
    """
    Calculate member report metrics for a given month from present data
    in database for all hub_plan's
    """
    if not is_date_format_valid(date_str, '%Y-%m'):
        return None

    # store all hub_plan's to process
    hub_plans = []

    # get all hub_plan's to process
    if hub_name:
        h = Hub.first(name=hub_name)
        hub_plans = HubPlan.find(hub=h)
    else:
        hub_plans = HubPlan.get_all()

    # calculate member report metrics for each plan of all hub
    for hub_plan in hub_plans:
        get_and_set_member_report_metrics_of_hub_plan(hub_plan, date_str)

    print "Total %s plans of all hub processed." % len(hub_plans)