add more routes to backend

This commit is contained in:
Sam 2024-09-22 14:27:43 +01:00
parent 0e6e19d483
commit 2d21aa98fc
3 changed files with 76 additions and 11 deletions

View File

@ -1,5 +1,29 @@
aggregate_mangrove_by_country = [ def mangrove_by_country_latest():
pipeline = [
{ {
"$match": {"year": "2020"}, "$match": {"year": "2020"},
}, },
] ]
return pipeline
def mangrove_by_country_agg(query):
pipeline = [
{"$match": {"country_with_parent": query["country_with_parent"]}},
{
"$group": {
"_id": {"country_with_parent": "$country_with_parent", "year": "$year"},
"total_pixels": {"$sum": "$total_n_pixels"},
}
},
{
"$project": {
"_id": 0,
"country_with_parent": "$_id.country_with_parent",
"year": "$_id.year",
"total_pixels": 1,
}
},
{"$sort": {"year": 1}},
]
return pipeline

View File

@ -4,18 +4,45 @@ from models.mongodb_handler import MongoDBHandler
import models.pipelines as pipelines import models.pipelines as pipelines
import schema.schemas as schemas import schema.schemas as schemas
from schema.schemas import DataSerializer from schema.schemas import DataSerializer
import ast
router = APIRouter() router = APIRouter()
@router.get("/mangrove_country_data") @router.get("/mangrove_by_country_latest")
async def mangrove_country_data(): async def mangrove_by_country_latest():
db = client.baseddata db = client.baseddata
collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"] collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"]
schema = schemas.mangrove_country_schema schema = schemas.mangrove_by_country_latest_schema
query = pipelines.aggregate_mangrove_by_country pipeline = pipelines.mangrove_by_country_latest()
serializer = DataSerializer(schema) serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name) handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(query) rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/mangrove_by_country_agg")
async def mangrove_by_country_agg(query: str):
query = ast.literal_eval(query)
db = client.baseddata
collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"]
schema = schemas.mangrove_by_country_agg_schema
pipeline = pipelines.mangrove_by_country_agg(query)
serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/bitcoin_business_growth")
async def bitcoin_business_growth(query: str):
query = ast.literal_eval(query)
db = client.baseddata
collection_name = db["final__bitcoin_business_growth_by_country"]
schema = schemas.bitcoin_business_growth
pipeline = pipelines.bitcoin_business_growth(query)
serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData) serializedData = serializer.serialize_many(rawData)
return serializedData return serializedData

View File

@ -1,4 +1,4 @@
def mangrove_country_schema(data): def mangrove_by_country_latest_schema(data):
return { return {
"country_with_parent": str(data["country_with_parent"]), "country_with_parent": str(data["country_with_parent"]),
"original_pixels": int(data["original_pixels"]), "original_pixels": int(data["original_pixels"]),
@ -7,6 +7,20 @@ def mangrove_country_schema(data):
"cumulative_pct_diff": float(data["cumulative_pct_diff"]), "cumulative_pct_diff": float(data["cumulative_pct_diff"]),
} }
def mangrove_by_country_agg_schema(data):
return {
"country_with_parent": str(data["country_with_parent"]),
"year": int(data["year"]),
"total_pixels": int(data["total_pixels"])
}
def bitcoin_business_growth_schema(data):
return {
"country_name": str(data["country_name"]),
"cumulative_current_value": str(data["cumulative_current_value"]),
"year": int(data["year"]),
"total_pixels": int(data["total_pixels"])
}
class DataSerializer: class DataSerializer:
def __init__(self, schema_func): def __init__(self, schema_func):