From ab374005222b0ddff4030d68dcb084f6ab1ef100 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 23 Sep 2024 17:46:02 +0100 Subject: [PATCH] add bitcoin business growth to new backend --- backend/models/pipelines.py | 94 ++++++++++++++++++++++ backend/routes/route.py | 21 ++++- backend/schema/schemas.py | 20 ++++- content/data-lab/global-business-growth.md | 6 +- 4 files changed, 131 insertions(+), 10 deletions(-) diff --git a/backend/models/pipelines.py b/backend/models/pipelines.py index c3efed9..6948b35 100644 --- a/backend/models/pipelines.py +++ b/backend/models/pipelines.py @@ -27,3 +27,97 @@ def mangrove_by_country_agg(query): {"$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 index f3f024d..0db87ce 100644 --- a/backend/routes/route.py +++ b/backend/routes/route.py @@ -34,13 +34,26 @@ async def mangrove_by_country_agg(query: str): serializedData = serializer.serialize_many(rawData) return serializedData -@router.get("/bitcoin_business_growth") -async def bitcoin_business_growth(query: str): +@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 - pipeline = pipelines.bitcoin_business_growth(query) + 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) diff --git a/backend/schema/schemas.py b/backend/schema/schemas.py index 41aaa42..995c72c 100644 --- a/backend/schema/schemas.py +++ b/backend/schema/schemas.py @@ -1,3 +1,6 @@ +def dt_to_date(datetime): + return datetime.date() + def mangrove_by_country_latest_schema(data): return { "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"]) } -def bitcoin_business_growth_schema(data): +def bitcoin_business_growth_percent_diff_schema(data): return { "country_name": str(data["country_name"]), - "cumulative_current_value": str(data["cumulative_current_value"]), - "year": int(data["year"]), - "total_pixels": int(data["total_pixels"]) + "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: diff --git a/content/data-lab/global-business-growth.md b/content/data-lab/global-business-growth.md index 7bdf5ea..94b9d19 100644 --- a/content/data-lab/global-business-growth.md +++ b/content/data-lab/global-business-growth.md @@ -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.
-{{< chart src="/js/bitcoin-business-growth-chart.js" >}} -{{< table src="/js/bitcoin-business-growth-table.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 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 Data obtained from © [OpenStreetMap](https://www.openstreetmap.org/copyright)