Ejemplo n.º 1
0
def generic_join_breakdown(namespace,
                           params,
                           left_table,
                           right_table,
                           join_criteria,
                           columns,
                           order_col="ratio",
                           filters=[],
                           limit=10,
                           sort_order="desc",
                           offset=0,
                           col_select=None):

    cache_key = CAROUSEL_NS + namespace + "_" + str(params)

    prev = object_cache(cache_key)
    if prev:
        return pickle.loads(prev)

    order = desc(order_col) if sort_order != "asc" else asc(order_col)
    results = left_table.query.join(right_table, join_criteria) \
                            .with_entities(*columns) \
                            .filter(*filters) \
                            .order_by(order) \
                            .limit(limit) \
                            .offset(offset) \
                            .all()

    if not col_select:
        raise Exception("Please specify the column to select for results")

    results = [row.__dict__[col_select] for row in results]

    object_cache(cache_key, pickle.dumps(results))
    return results
Ejemplo n.º 2
0
def generic_join_breakdown(namespace, params, left_table, right_table, join_criteria, columns, order_col="ratio", filters=[], 
                           limit=10, sort_order="desc", offset=0, col_select=None):

    cache_key = CAROUSEL_NS + namespace + "_" + str(params)
    
    prev = object_cache(cache_key)
    if prev:
        return pickle.loads(prev) 

    order = desc(order_col) if sort_order != "asc" else asc(order_col)
    results = left_table.query.join(right_table, join_criteria) \
                            .with_entities(*columns) \
                            .filter(*filters) \
                            .order_by(order) \
                            .limit(limit) \
                            .offset(offset) \
                            .all()

    if not col_select:
        raise Exception("Please specify the column to select for results")

    results = [row.__dict__[col_select] for row in results]

    object_cache(cache_key, pickle.dumps(results))
    return results
Ejemplo n.º 3
0
def top_occupations(year, bra_id):
    cache_key = CAROUSEL_NS + "top_occupations" + str(year) + bra_id
    prev = object_cache(cache_key)
    if prev:
        return pickle.loads(prev)

    table = rais.Ybo
    filters = [table.bra_id == bra_id, table.year == year]
    raw = query_helper.query_table(table, [table.cbo_id],
                                   filters,
                                   order=table.wage_avg,
                                   limit=10,
                                   sort="desc")
    cbos = [x[0] for x in raw["data"]]
    table = raisd.Ybod
    filters = [
        table.bra_id == bra_id, table.year == year,
        table.cbo_id.in_(cbos),
        table.d_id.in_(["A", "B"])
    ]
    columns = [
        table.cbo_id, table.d_id, table.num_jobs, table.wage_avg,
        table.wage_growth
    ]
    results = query_helper.query_table(table,
                                       columns,
                                       filters,
                                       order=table.wage_avg)

    object_cache(cache_key, pickle.dumps(results))
    return results
Ejemplo n.º 4
0
def top_occupations(year, bra_id):
    cache_key = CAROUSEL_NS + "top_occupations" + str(year) + bra_id
    prev = object_cache(cache_key)
    if prev:
        return pickle.loads(prev)

    table = rais.Ybo
    filters = [table.bra_id == bra_id, table.year == year]
    raw = query_helper.query_table(table, [table.cbo_id], filters, order=table.wage_avg, limit=10, sort="desc")
    cbos = [x[0] for x in raw["data"]]
    table = raisd.Ybod
    filters = [table.bra_id == bra_id, table.year == year, table.cbo_id.in_(cbos), table.d_id.in_(["A", "B"])]
    columns = [table.cbo_id, table.d_id, table.num_jobs, table.wage_avg, table.wage_growth]
    results = query_helper.query_table(table, columns, filters, order=table.wage_avg)
    
    object_cache(cache_key, pickle.dumps(results))
    return results