Esempio n. 1
0
def get_platform(mode, platform):
    """ Implements the query_event specification
    Get filtered results based on the platform of purchase
    :param platform: a list of platforms
    :return:
    """
    query = "SELECT fk_customer, misc FROM sales_order"

    def execute_fn(dbname):
        cursor = aux.execute_query(query, dbname)
        return map(
            lambda k: list(k),
            list(cursor)
        )

    init_result = aux.execute_on_database(mode, execute_fn)

    parsed_list = map(
        lambda l: [l[0], _get_plat_from_misc(l[1])],
        init_result
    )

    fin_result = filter(lambda k: k[1] in platform, parsed_list)
    keys, result = aux.convert_to_id_dict(fin_result)
    return keys, result, ['Platform']
def get_origination(months):
    query = """
    SELECT id_customer, created_at
    FROM customer
    WHERE EXTRACT(YEAR_MONTH FROM created_at) in ('%s')""" % "','".join(months)

    cursor = aux.execute_query(query, 'jerry_live')
    keys, result = aux.convert_to_id_dict(cursor)
    return keys, result, ['Origination Date']
def get_platform(mode, platform):
    """ Implements the query_event specification
    Get filtered results based on the platform of purchase
    :param platform: a list of platforms
    :return:
    """
    query = "SELECT fk_customer, misc FROM sales_order"

    def execute_fn(dbname):
        cursor = aux.execute_query(query, dbname)
        return map(lambda k: list(k), list(cursor))

    init_result = aux.execute_on_database(mode, execute_fn)

    parsed_list = map(lambda l: [l[0], _get_plat_from_misc(l[1])], init_result)

    fin_result = filter(lambda k: k[1] in platform, parsed_list)
    keys, result = aux.convert_to_id_dict(fin_result)
    return keys, result, ['Platform']
def get_customer(mode, language):
    """ Follows the query_event specifications

    Get customers from the database
    :param mode: Any one of
        1. 'all': no discrimination on region, default
        2. 'uae': UAE customers
        3. 'ksa': KSA customers
        4. 'both': Not implemented
        4. 'others': Not implemented
    :param language: An optional language
    :return: A tuple (
            keys: A set of id_customer,
            result: A dictionary { id_customer, [phone, language] },
            headers: A list of headers
        )
    """

    if mode == 'uae':
        where_clause = 'where cust.fk_country = 3'
    elif mode == 'ksa':
        where_clause = 'where cust.fk_country = 193'
    else:
        where_clause = ''

    query = """
    SELECT distinct cust.id_customer, phone.number, if(cust.fk_language=1, 'English', 'Arabic') as language, if(cust.fk_country=3, 'UAE', 'KSA') as country
    FROM customer cust INNER JOIN customer_phone phone
    ON phone.fk_customer = cust.id_customer
    %s
    """ % where_clause

    if language is not None:
        fquery = """
        SELECT * FROM (%s) AS T WHERE language = '%s'""" % (query, language)
    else:
        fquery = query
    cursor = aux.execute_query(fquery, 'jerry_live')

    result = {}
    keys = set()
    keys, result = aux.convert_to_id_dict(cursor)
    return keys, result, ['Phone', 'language', 'Country']