add bitcoin business growth to new backend
This commit is contained in:
parent
a0a3f38ebd
commit
ab37400522
|
@ -27,3 +27,97 @@ def mangrove_by_country_agg(query):
|
||||||
{"$sort": {"year": 1}},
|
{"$sort": {"year": 1}},
|
||||||
]
|
]
|
||||||
return pipeline
|
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
|
||||||
|
|
|
@ -34,13 +34,26 @@ async def mangrove_by_country_agg(query: str):
|
||||||
serializedData = serializer.serialize_many(rawData)
|
serializedData = serializer.serialize_many(rawData)
|
||||||
return serializedData
|
return serializedData
|
||||||
|
|
||||||
@router.get("/bitcoin_business_growth")
|
@router.get("/bitcoin_business_growth_percent_diff")
|
||||||
async def bitcoin_business_growth(query: str):
|
async def bitcoin_business_growth_percent_diff(query: str):
|
||||||
query = ast.literal_eval(query)
|
query = ast.literal_eval(query)
|
||||||
db = client.baseddata
|
db = client.baseddata
|
||||||
collection_name = db["final__bitcoin_business_growth_by_country"]
|
collection_name = db["final__bitcoin_business_growth_by_country"]
|
||||||
schema = schemas.bitcoin_business_growth
|
schema = schemas.bitcoin_business_growth_percent_diff_schema
|
||||||
pipeline = pipelines.bitcoin_business_growth(query)
|
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)
|
serializer = DataSerializer(schema)
|
||||||
handler = MongoDBHandler(collection_name)
|
handler = MongoDBHandler(collection_name)
|
||||||
rawData = handler.aggregate(pipeline)
|
rawData = handler.aggregate(pipeline)
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
def dt_to_date(datetime):
|
||||||
|
return datetime.date()
|
||||||
|
|
||||||
def mangrove_by_country_latest_schema(data):
|
def mangrove_by_country_latest_schema(data):
|
||||||
return {
|
return {
|
||||||
"country_with_parent": str(data["country_with_parent"]),
|
"country_with_parent": str(data["country_with_parent"]),
|
||||||
|
@ -14,12 +17,21 @@ def mangrove_by_country_agg_schema(data):
|
||||||
"total_pixels": int(data["total_pixels"])
|
"total_pixels": int(data["total_pixels"])
|
||||||
}
|
}
|
||||||
|
|
||||||
def bitcoin_business_growth_schema(data):
|
def bitcoin_business_growth_percent_diff_schema(data):
|
||||||
return {
|
return {
|
||||||
"country_name": str(data["country_name"]),
|
"country_name": str(data["country_name"]),
|
||||||
"cumulative_current_value": str(data["cumulative_current_value"]),
|
"first_value": int(data["first_value"]),
|
||||||
"year": int(data["year"]),
|
"date_range": str(f'{dt_to_date(data["first_date"])} to {dt_to_date(data["last_date"])}'),
|
||||||
"total_pixels": int(data["total_pixels"])
|
"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:
|
class DataSerializer:
|
||||||
|
|
|
@ -15,8 +15,10 @@ You can select the growth period of interest from the drop-down, which updates t
|
||||||
The chart always reflects the countries selected in the table.
|
The chart always reflects the countries selected in the table.
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
{{< chart src="/js/bitcoin-business-growth-chart.js" >}}
|
{{< dropdown_filter id="days_ago" options="1 day:1,7 day:7,28 day:28,1 year:365,5 year:1826,10 year:3652,all time:10000" default_selection="7 day" >}}
|
||||||
{{< table src="/js/bitcoin-business-growth-table.js" >}}
|
{{< table id="bitcoin_business_growth" endpoint="bitcoin_business_growth_percent_diff" headers="{'country_name': 'Country', 'date_range': 'Date Range', 'last_value': 'Previous #', 'first_value': 'Current #', 'difference': 'Diff', 'percent_difference': '% Diff'}" maxHeight="400px" sortable="true" valueId="country_name" selectableRows="multi" >}}
|
||||||
|
|
||||||
|
{{< chart id="bitcoin_business_growth" endpoint="bitcoin_business_growth_timeseries" chartType="line" xAxisField="date" yAxisField="cumulative_value" scaleChart=true >}}
|
||||||
|
|
||||||
#### Attribution and License
|
#### Attribution and License
|
||||||
Data obtained from © [OpenStreetMap](https://www.openstreetmap.org/copyright)
|
Data obtained from © [OpenStreetMap](https://www.openstreetmap.org/copyright)
|
||||||
|
|
Loading…
Reference in New Issue