baseddata.io/backend/api/route.py

110 lines
3.9 KiB
Python

from fastapi import APIRouter
from api.postgres_handler import PostgresHandler
import api.pipelines as pipelines
import api.schemas as schemas
from api.schemas import DataSerializer
import json
router = APIRouter()
def parse_args_to_dict(query):
try:
return json.loads(query)
except json.JSONDecodeError as e:
return {"error": f"Invalid JSON: {e}"}
@router.get("/mangrove_by_country_latest")
async def mangrove_by_country_latest():
pipeline = pipelines.mangrove_by_country_latest()
handler = PostgresHandler()
schema = schemas.mangrove_by_country_latest_schema
serializer = DataSerializer(schema)
rawData = handler.execute_query(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/mangrove_country_timeseries")
async def mangrove_country_timeseries(query: str):
args = parse_args_to_dict(query)
pipeline = pipelines.mangrove_country_timeseries(args)
handler = PostgresHandler()
schema = schemas.mangrove_country_timeseries_schema
serializer = DataSerializer(schema)
rawData = handler.execute_query(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/bitcoin_business_growth_timeseries")
async def bitcoin_business_growth_timeseries(query: str):
args = parse_args_to_dict(query)
pipeline = pipelines.bitcoin_business_growth_timeseries(args)
handler = PostgresHandler()
schema = schemas.bitcoin_business_growth_timeseries_schema
serializer = DataSerializer(schema)
rawData = handler.execute_query(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/bitcoin_business_growth_percent_diff")
async def bitcoin_business_growth_percent_diff(query: str):
args = parse_args_to_dict(query)
pipeline = pipelines.bitcoin_business_growth_percent_diff_days_ago(args)
handler = PostgresHandler()
schema = schemas.bitcoin_business_growth_percent_diff_schema
serializer = DataSerializer(schema)
rawData = handler.execute_query(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
# @router.get("/bitcoin_business_growth_percent_diff")
# async def bitcoin_business_growth_percent_diff(query: str):
# query = ast.literal_eval(query)
#
# query = queries.bitcoin_business_growth_percent_diff_days_ago(query)
# handler = PostgresHandler(connection)
#
# schema = schemas.bitcoin_business_growth_percent_diff_schema
# pipeline = pipelines.bitcoin_business_growth_percent_diff_days_ago(query)
# serializer = DataSerializer(schema)
# handler = MongoDBHandler(collection_name)
# 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_timeseries")
# async def bitcoin_business_growth_timeseries(query: str):
# query = ast.literal_eval(query)
# db = client.baseddata
# collection_name = db["final__bitcoin_business_growth_by_country"]
# schema = schemas.bitcoin_business_growth_timeseries_schema
# pipeline = pipelines.bitcoin_business_growth_timeseries(query)
# serializer = DataSerializer(schema)
# handler = MongoDBHandler(collection_name)
# rawData = handler.aggregate(pipeline)
# serializedData = serializer.serialize_many(rawData)
# return serializedData