Exemple #1
0
def consumption(country, year):
    connect(
        db="comp9321ass3",
        username="******",
        password="******",
        host="ds117540.mlab.com",
        port=17540
    )

    if ConsumptionPercapita.objects(country__iexact=country, year=year).count():
        for r in ConsumptionPercapita.objects(country__iexact=country, year=year):
            result = {
                "country": country,
                "year": year,
                "consumption_percapita": {
                    "amount": r.consumption_percapita,
                    "unit": "kWh"
                }
            }
            response = jsonify(result)
            response.headers._list.append(('Access-Control-Allow-Origin', '*'))
            return response, 200
    else:
        cons = EnergyConsumption.objects(country__iexact=country, year=year).count()
        pop = Population.objects(country__iexact=country, year=year).count()
        access = EnergyAccess.objects(country__iexact=country, year=year).count()

        if not cons or not pop or not access:
            response = jsonify(country__iexact=country, year=year)
            response.headers._list.append(('Access-Control-Allow-Origin', '*'))
            return response, 404

        for r in EnergyConsumption.objects(country__iexact=country, year=year):
            cons = r.energy_consumption
        for r in Population.objects(country__iexact=country, year=year):
            pop = r.population
        for r in EnergyAccess.objects(country__iexact=country, year=year):
            access = r.energy_access

        if not cons or not pop or not access:
            response = jsonify(country__iexact=country, year=year)
            response.headers._list.append(('Access-Control-Allow-Origin', '*'))
            return response, 404

        per_capita = cons * 1000000 / (pop * access / 100)

        t = ConsumptionPercapita(country,year,per_capita)
        t.save()

        result = {
            "country": country,
            "year": year,
            "consumption_percapita": {
                "amount": per_capita,
                "unit": "kWh"
            }
        }
        response = jsonify(result)
        response.headers._list.append(('Access-Control-Allow-Origin', '*'))
        return response, 200
Exemple #2
0
def population(country, year):
    connect(
        db="comp9321ass3",
        username="******",
        password="******",
        host="ds117540.mlab.com",
        port=17540
    )

    pop = None
    for p in Population.objects(country__iexact=country, year=year):
        pop = p
    if not pop:
        response = jsonify(country__iexact=country, year=year)
        response.headers._list.append(('Access-Control-Allow-Origin', '*'))
        return response, 404

    population_response = {
        "country": country,
        "year": year,
        "population": pop.population
    }
    response = jsonify(population_response)
    response.headers._list.append(('Access-Control-Allow-Origin', '*'))
    return response, 200
Exemple #3
0
def consumption_growths(country):
    connect(
        db="comp9321ass3",
        username="******",
        password="******",
        host="ds117540.mlab.com",
        port=17540
    )
    result = list()

    for rec_1 in EnergyConsumption.objects(country__iexact=country):
        consumption = rec_1.energy_consumption
        access = 0
        for rec_2 in EnergyAccess.objects(country__iexact=country, year=rec_1.year):
            access = rec_2.energy_access
        population = 0
        for rec_2 in Population.objects(country__iexact=country, year=rec_1.year):
            population = rec_2.population
        if access and population:
            per_capita = consumption * 1000000 / (population * access / 100)
        else:
            per_capita = 0
        cons = ConsumptionPercapita(rec_1.country, rec_1.year, per_capita)
        cons.save()
        result.append([
            rec_1.year,
            round(per_capita, 2)
        ])
    if not result:
        response = jsonify(country=country)
        response.headers._list.append(('Access-Control-Allow-Origin', '*'))
        return response, 404
    response = jsonify(result)
    response.headers._list.append(('Access-Control-Allow-Origin', '*'))
    return response, 200
Exemple #4
0
def countries_list(parameter):
    connect(
        db="comp9321ass3",
        username="******",
        password="******",
        host="ds117540.mlab.com",
        port=17540
    )
    ls = Population.objects(country__icontains=parameter).distinct(field="country")
    response = jsonify(ls)
    response.headers._list.append(('Access-Control-Allow-Origin', '*'))
    return response, 200
Exemple #5
0
def upload_population():

    tree1 = ET.parse('dataset/population.xml')
    root1 = tree1.getroot()

    connect(db="comp9321ass3",
            username="******",
            password="******",
            host="ds117540.mlab.com",
            port=17540)

    for record in root1.find('.//data'):
        country = record.find('.//field[1]').text
        year = record.find('.//field[3]').text
        try:
            amount = float(record.find('.//field[4]').text)
            raw = Population(country, year, amount)
            raw.save()
        except:
            amount = None
        print(country, year, amount)
Exemple #6
0
def mix(country, year):
    connect(db="comp9321ass3",
            username="******",
            password="******",
            host="ds117540.mlab.com",
            port=17540)
    EnergyReport.objects(country=country, year=year).delete()
    for base in EnergyMix.objects(country=country, year=year):
        pop = None
        access = None
        consumption = None
        for temp in Population.objects(country=base.country, year=base.year):
            pop = temp.population

        for temp in EnergyAccess.objects(country=base.country, year=base.year):
            access = temp.energy_access

        for temp in EnergyConsumption.objects(country=base.country,
                                              year=base.year):
            consumption = temp.energy_consumption

        sources = list()

        source = EnergySource('solar', base.solar)
        sources.append(source)

        source = EnergySource('wind', base.wind)
        sources.append(source)

        source = EnergySource('hydro', base.hydro)
        sources.append(source)

        source = EnergySource('geothermal', base.geothermal)
        sources.append(source)

        source = EnergySource('combustibles', base.combustibles)
        sources.append(source)

        source = EnergySource('other', base.other)
        sources.append(source)

        report = EnergyReport(base.country, base.year, pop, access,
                              base.total_energy, sources, consumption)

        report.save()