Ejemplo n.º 1
0
def compute_stats(pobj):
    attr_type = pobj.__class__.__name__.lower()
    stats = []
    if attr_type == "wld" and pobj.id == "all":
        attr_type = "bra"

    rais_year = parse_year(__year_range__["rais"][-1].split("-")[0])
    secex_year = parse_year(__year_range__["secex"][-1].split("-")[0])
    hedu_year = parse_year(__year_range__["hedu"][-1].split("-")[0])

    if attr_type == "bra":
        return bra_stats(pobj, rais_year, secex_year)
    elif attr_type == "cnae":
        return cnae_stats(pobj, rais_year)
    elif attr_type == "cbo":
        return cbo_stats(pobj, rais_year)
    elif attr_type == "hs":
        return hs_stats(pobj, secex_year)
    elif attr_type == "wld":
        return wld_stats(pobj, secex_year)
    elif attr_type == "university":
        return university_stats(pobj, hedu_year)
    elif attr_type == "course_hedu":
        return course_hedu_stats(pobj, hedu_year)
    elif attr_type == "course_sc":
        return course_sc_stats(pobj)

    return stats
Ejemplo n.º 2
0
def course_sc_stats(pobj):
    stats = []
    sc_year = parse_year(__year_range__["sc"][-1].split("-")[0])
    group = u'{1} {0}'.format(gettext("Enrollment Stats"), sc_year)
    key = "hedu"

    filters = [Ybc_sc.year == sc_year, Ybc_sc.course_sc_id == pobj.id, Ybc_sc.bra_id_len == 9]
    profile, value = get_top_stat(Ybc_sc, Ybc_sc.bra_id, Ybc_sc.enrolled, Bra, filters)
    stats.append(make_stat(key, group, gettext("Top Municipality by Enrollment"), profile=profile, value=value, mode="enrolled"))

    filters = [Yc_sc.year == sc_year, Yc_sc.course_sc_id == pobj.id]
    enrolled, age = get_stat_val(Ymw, [Yc_sc.enrolled, Yc_sc.age], filters)
    stats.append(make_stat(key, group, gettext("Total Enrollment"), desc=enrolled, mode="enrolled"))
    stats.append(make_stat(key, group, gettext("Average Age"), desc=age, mode="graduates"))
    return stats
Ejemplo n.º 3
0
def bra_stats(pobj, rais_year, secex_year):
    stats = []

    gen_title = gettext("General Stats")
    key = "general"

    if pobj.id != "all":
        stat_ids = ["pop", "gini", "life_exp", "hdi", "gdp", "gdp_pc", "pop_density"]
        stats_year = parse_year(__year_range__["stats"][-1].split("-")[0])
        results = batch_stats(Ybs, pobj.id, stat_ids, stats_year)
        for stat, val in results:
            stats.append(make_stat(key, gen_title, stat.name(), desc=val, year=stats_year, mode=stat.id))

    group = u'{1} {0}'.format(gettext("Employment Stats"), rais_year)
    key = "rais"
    if pobj.id == "all":
        filters = [Yi.year == rais_year, Yi.cnae_id_len == 6]
        result = get_top_stat(Yi, Yi.cnae_id, Yi.num_jobs, Cnae, filters)
    else:
        filters = [Ybi.year == rais_year, Ybi.bra_id == pobj.id, Ybi.cnae_id_len == 6]
        result = get_top_stat(Ybi, Ybi.cnae_id, Ybi.num_jobs, Cnae, filters)
    if result:
        profile, value = result
        stat = make_stat(key, group, gettext("Top Industry by Employment"), profile=profile, value=value, mode="num_jobs")
        stats.append(stat)

    if pobj.id == "all":
        filters = [Yo.year == rais_year, Yo.cbo_id_len == 4]
        profile, value = get_top_stat(Yo, Yo.cbo_id, Yo.num_jobs, Cbo, filters)
    else:
        filters = [Ybo.year == rais_year, Ybo.bra_id == pobj.id, Ybo.cbo_id_len == 4]
        profile, value = get_top_stat(Ybo, Ybo.cbo_id, Ybo.num_jobs, Cbo, filters)
    stat = make_stat(key, group, gettext("Top Occupation by Employment"), profile=profile, value=value, mode="num_jobs")
    stats.append(stat)

    if pobj.id != "all":
        value = get_stat_val(Yb_rais, Yb_rais.wage, [Yb_rais.year == rais_year, Yb_rais.bra_id == pobj.id])
        stats.append(make_stat(key, group, gettext("Total Monthly Wage"), desc=value, mode="wage"))

    group = u'{1} {0}'.format(gettext("Trade Stats"), secex_year)
    key = "secex"
    if pobj.id == "all":
        filters = [Ymp.year == secex_year, Ymp.month == 0, Ymp.hs_id_len == 6]
        result = get_top_stat(Ymp, Ymp.hs_id, Ymp.export_val, Hs, filters)
    else:
        filters = [Ymbp.year == secex_year, Ymbp.month == 0, Ymbp.bra_id == pobj.id, Ymbp.hs_id_len == 6]
        result = get_top_stat(Ymbp, Ymbp.hs_id, Ymbp.export_val, Hs, filters)
    if result:
        profile, value = result
        stat = make_stat(key, group, gettext("Top Product by Export Value"), profile=profile, value=value, mode="export_val")
        stats.append(stat)

    if pobj.id == "all":
        filters = [Ymw.year == secex_year, Ymw.month == 0, Ymw.wld_id_len == 5]
        result = get_top_stat(Ymw, Ymw.wld_id, Ymw.export_val, Wld, filters)
    else:
        filters = [Ymbw.year == secex_year, Ymbw.month == 0, Ymbw.bra_id == pobj.id, Ymbw.wld_id_len == 5]
        result = get_top_stat(Ymbw, Ymbw.wld_id, Ymbw.export_val, Wld, filters)
    if result:
        profile,value=result
        stats.append(make_stat(key, group, gettext("Top Destination by Export Value"), profile=profile, value=value, mode="export_val"))

    if pobj.id != "all":
        filters = [Ymb.year == secex_year, Ymb.month == 0, Ymb.bra_id == pobj.id]
        result = get_stat_val(Ymb, [Ymb.export_val, Ymb.import_val], filters)
        if result:
            export_val, import_val = result
            stats.append(make_stat(key, group, gettext("Total Exports"), desc=export_val, mode="export_val"))
            stats.append(make_stat(key, group, gettext("Total Imports"), desc=import_val, mode="export_val"))

        key = "general"
        if len(pobj.id) > 1:
            geo = Bra.query.get(pobj.id[:1])
            stats.append(make_stat(key, gen_title, gettext("Region"), profile=geo))
        if len(pobj.id) > 3:
            geo = Bra.query.get(pobj.id[:3])
            stats.append(make_stat(key, gen_title, gettext("State"), profile=geo))
        if len(pobj.id) > 5:
            geo = Bra.query.get(pobj.id[:5])
            stats.append(make_stat(key, gen_title, gettext("Mesoregion"), profile=geo))
        if len(pobj.id) > 7:
            geo = Bra.query.get(pobj.id[:7])
            stats.append(make_stat(key, gen_title, gettext("Microregion"), profile=geo))

        if len(pobj.id) == 9:
            stat_ids = ["airport", "airport_dist", "seaport", "seaport_dist", "area", "capital_dist", "neighbors"]
            results = batch_stats(Bs, pobj.id, stat_ids)
            for stat, val in results:
                if stat.id == "neighbors":
                    bras = Bra.query.filter(Bra.id.in_(val.split(","))).all()
                    val = ", ".join([u"<a href='{}'>{}</a>".format(b.url(), b.name()) for b in bras])
                stats.append(make_stat(key, gen_title, stat.name(), desc=val, mode=stat.id))

    return stats