change backend structure
This commit is contained in:
parent
99ee771417
commit
54a32019c6
|
@ -1,5 +0,0 @@
|
|||
from pymongo.mongo_client import MongoClient
|
||||
|
||||
client = MongoClient(
|
||||
host=["10.0.10.35:27017"], username="admin", password="1234", authSource="admin"
|
||||
)
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
|
@ -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
|
|
@ -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
|
|
@ -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]
|
Loading…
Reference in New Issue