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("/miner_rewards") async def miner_rewards(query: str): args = parse_args_to_dict(query) pipeline = pipelines.miner_rewards(args) handler = PostgresHandler() schema = schemas.miner_rewards_schema serializer = DataSerializer(schema) rawData = handler.execute_query(pipeline) serializedData = serializer.serialize_many(rawData) return serializedData @router.get("/feerate_percentiles") async def feerate_percentiles(query: str): args = parse_args_to_dict(query) pipeline = pipelines.feerate_percentiles(args) handler = PostgresHandler() schema = schemas.feerate_percentiles_schema serializer = DataSerializer(schema) rawData = handler.execute_query(pipeline) serializedData = serializer.serialize_many(rawData) return serializedData @router.get("/bitcoin_price_timeseries") async def bitcoin_price_timeseries(query: str): args = parse_args_to_dict(query) pipeline = pipelines.bitcoin_price_timeseries(args) handler = PostgresHandler() schema = schemas.bitcoin_price_timeseries_schema serializer = DataSerializer(schema) rawData = handler.execute_query(pipeline) serializedData = serializer.serialize_many(rawData) return serializedData @router.get("/bitcoin_hashrate") async def bitcoin_hashrate(query: str): args = parse_args_to_dict(query) pipeline = pipelines.bitcoin_hashrate(args) handler = PostgresHandler() schema = schemas.bitcoin_hashrate_schema serializer = DataSerializer(schema) rawData = handler.execute_query(pipeline) serializedData = serializer.serialize_many(rawData) return serializedData