From 26bf3d0efe15ec323ad0d706959942a718586d9e Mon Sep 17 00:00:00 2001 From: Mali Akmanalp Date: Tue, 16 Jun 2015 14:40:46 -0400 Subject: [PATCH 1/2] Fix bug where data was being incorrectly populated in IndustryYear --- colombia/import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colombia/import.py b/colombia/import.py index b8f42ec..c9404bd 100644 --- a/colombia/import.py +++ b/colombia/import.py @@ -71,7 +71,7 @@ def inner(line): iy = models.IndustryYear() iy.industry = industry_map[line["i"]] iy.year = int(line["year"]) - iy.pci = line["pci"] + iy.complexity = line["pci"] return iy return inner From 97c19be44a13ae21058180d4f5e942fe7ebadd0c Mon Sep 17 00:00:00 2001 From: Mali Akmanalp Date: Tue, 16 Jun 2015 14:41:18 -0400 Subject: [PATCH 2/2] Add API for scatterplots --- colombia/data/views.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/colombia/data/views.py b/colombia/data/views.py index 4b90263..80e1a09 100644 --- a/colombia/data/views.py +++ b/colombia/data/views.py @@ -1,6 +1,6 @@ from flask import Blueprint, request from .models import (DepartmentProductYear, DepartmentIndustryYear, - ProductYear, Location) + ProductYear, Location, IndustryYear) from ..api_schemas import marshal from .. import api_schemas as schemas @@ -155,3 +155,40 @@ def industries_index(product_id=None): return marshal(schemas.department_industry_year, q) raise abort(400, body="Could not find data with the given parameters.") + + +@industries_app.route("//scatterplot") +def scatterplot(entity_type): + + location_id = int(request.args.get("location", None)) + year = int(request.args.get("year", None)) + + # Find type of location + if location_id is None: + raise abort(400, body="Must specify ?location=") + + location = Location.query.get_or_404(location_id) + + if location.level != "department": + raise abort(400, body="""Scatterplots are only available for + departments.""") + + if entity_type == "industries": + q = db.session.query(DepartmentIndustryYear.distance, + IndustryYear.complexity, + IndustryYear.industry_id + )\ + .filter_by(year=year, department_id=location_id)\ + .join(IndustryYear, (DepartmentIndustryYear.industry_id == IndustryYear.id) & (DepartmentIndustryYear.year == IndustryYear.year)) + return jsonify(data=[x._asdict() for x in q]) + elif entity_type == "products": + q = db.session.query(DepartmentProductYear.distance, + ProductYear.pci.label("complexity"), + ProductYear.product_id + )\ + .filter_by(year=year, department_id=location_id)\ + .join(ProductYear, (DepartmentProductYear.product_id == ProductYear.id) & (DepartmentProductYear.year == ProductYear.year)) + return jsonify(data=[x._asdict() for x in q]) + + + raise abort(400, body="Could not find data with the given parameters.")