From 54a32019c6f3b4d7d0d30aee3d872b440d1ce86d Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 7 Nov 2024 21:38:45 +0000 Subject: [PATCH] change backend structure --- backend/config/database.py | 5 -- backend/main.py | 11 +-- backend/models/mongodb_handler.py | 12 --- backend/models/pipelines.py | 123 ------------------------------ backend/routes/route.py | 61 --------------- backend/schema/schemas.py | 45 ----------- 6 files changed, 1 insertion(+), 256 deletions(-) delete mode 100644 backend/config/database.py delete mode 100644 backend/models/mongodb_handler.py delete mode 100644 backend/models/pipelines.py delete mode 100644 backend/routes/route.py delete mode 100644 backend/schema/schemas.py diff --git a/backend/config/database.py b/backend/config/database.py deleted file mode 100644 index a7bbee3..0000000 --- a/backend/config/database.py +++ /dev/null @@ -1,5 +0,0 @@ -from pymongo.mongo_client import MongoClient - -client = MongoClient( - host=["10.0.10.35:27017"], username="admin", password="1234", authSource="admin" -) diff --git a/backend/main.py b/backend/main.py index 23a4e59..22adbe0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,15 +1,6 @@ from fastapi import FastAPI -from pymongo.mongo_client import MongoClient -from routes.route import router +from api.route import router app = FastAPI() -client = MongoClient( - host=["10.0.10.35:27017"], username="admin", password="1234", authSource="admin" -) -try: - client.admin.command("ping") - print("Successfully pinged MongoDB deployment!") -except Exception as e: - print(e) app.include_router(router) diff --git a/backend/models/mongodb_handler.py b/backend/models/mongodb_handler.py deleted file mode 100644 index 7b22a8a..0000000 --- a/backend/models/mongodb_handler.py +++ /dev/null @@ -1,12 +0,0 @@ -class MongoDBHandler: - def __init__(self, collection): - self.collection = collection - - def find_limit(self, limit: int): - return self.collection.find().limit(limit) - - def find_one(self, query): - return self.collection.find_one(query) - - def aggregate(self, query): - return self.collection.aggregate(query) diff --git a/backend/models/pipelines.py b/backend/models/pipelines.py deleted file mode 100644 index 72a1807..0000000 --- a/backend/models/pipelines.py +++ /dev/null @@ -1,123 +0,0 @@ -def mangrove_by_country_latest(): - pipeline = [ - { - "$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 - - -def bitcoin_business_growth_timeseries(query): - pipeline = [ - { - "$match": { - "days_ago": {"$lte": int(query["days_ago"])}, - "country_name": query["country_name"], - } - }, - { - "$project": { - "country_name": "$country_name", - "date": "$date", - "cumulative_value": "$cumulative_value", - } - }, - {"$sort": {"country_name": 1, "days_ago": 1}}, - ] - return pipeline - - -def bitcoin_business_growth_percent_diff_days_ago(query): - pipeline = [ - {"$match": {"days_ago": {"$lte": int(query["days_ago"])}}}, - {"$sort": {"country_name": 1, "days_ago": 1}}, - { - "$group": { - "_id": "$country_name", - "firstValue": {"$first": "$cumulative_value"}, - "lastValue": {"$last": "$cumulative_value"}, - "firstDate": {"$min": "$date"}, - "lastDate": {"$max": "$date"}, - } - }, - { - "$project": { - "country_name": "$_id", - "first_value": "$firstValue", - "last_value": "$lastValue", - "difference": { - "$subtract": [ - {"$toDouble": "$firstValue"}, - {"$toDouble": "$lastValue"}, - ] - }, - "first_date": "$firstDate", - "last_date": "$lastDate", - "percent_difference": { - "$cond": { - "if": {"$eq": [{"$toDouble": "$lastValue"}, 0]}, - "then": { - "$cond": { - "if": {"$gt": [{"$toDouble": "$firstValue"}, 0]}, - "then": "new", - "else": "none", - } - }, - "else": { - "$round": [ - { - "$multiply": [ - { - "$divide": [ - { - "$subtract": [ - {"$toDouble": "$firstValue"}, - {"$toDouble": "$lastValue"}, - ] - }, - {"$toDouble": "$lastValue"}, - ] - }, - 100, - ] - } - ] - }, - } - }, - } - }, - ] - return pipeline - - -def bitcoin_business_growth_latest(query): - pipeline = [ - { - "$match": query["filter"], - }, - {"$sort": {"date": 1}}, - ] - return pipeline diff --git a/backend/routes/route.py b/backend/routes/route.py deleted file mode 100644 index 0db87ce..0000000 --- a/backend/routes/route.py +++ /dev/null @@ -1,61 +0,0 @@ -from fastapi import APIRouter -from config.database import client -from models.mongodb_handler import MongoDBHandler -import models.pipelines as pipelines -import schema.schemas as schemas -from schema.schemas import DataSerializer -import ast - -router = APIRouter() - - -@router.get("/mangrove_by_country_latest") -async def mangrove_by_country_latest(): - db = client.baseddata - collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"] - schema = schemas.mangrove_by_country_latest_schema - pipeline = pipelines.mangrove_by_country_latest() - 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_percent_diff") -async def bitcoin_business_growth_percent_diff(query: str): - query = ast.literal_eval(query) - db = client.baseddata - collection_name = db["final__bitcoin_business_growth_by_country"] - 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("/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 diff --git a/backend/schema/schemas.py b/backend/schema/schemas.py deleted file mode 100644 index 995c72c..0000000 --- a/backend/schema/schemas.py +++ /dev/null @@ -1,45 +0,0 @@ -def dt_to_date(datetime): - return datetime.date() - -def mangrove_by_country_latest_schema(data): - return { - "country_with_parent": str(data["country_with_parent"]), - "original_pixels": int(data["original_pixels"]), - "total_n_pixels": int(data["total_n_pixels"]), - "cumulative_pixels_diff": int(data["cumulative_pixels_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_percent_diff_schema(data): - return { - "country_name": str(data["country_name"]), - "first_value": int(data["first_value"]), - "date_range": str(f'{dt_to_date(data["first_date"])} to {dt_to_date(data["last_date"])}'), - "last_value": int(data["last_value"]), - "difference": int(data["difference"]), - "percent_difference": str(data["percent_difference"]) - } - -def bitcoin_business_growth_timeseries_schema(data): - return { - "country_name": str(data["country_name"]), - "date": dt_to_date(data["date"]), - "cumulative_value": int(data["cumulative_value"]) - } - -class DataSerializer: - def __init__(self, schema_func): - self.schema_func = schema_func - - def serialize_one(self, data) -> dict: - return self.schema_func(data) - - def serialize_many(self, data_list) -> list: - return [self.serialize_one(data) for data in data_list]