baseddata.io/backend/models/pipelines.py

124 lines
3.9 KiB
Python

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