baseddata.io/backend/api/schemas.py

90 lines
2.7 KiB
Python
Raw Normal View History

2024-11-07 21:40:26 +00:00
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_country_timeseries_schema(data):
return {
"year": str(data["year"]),
"total_n_pixels": int(data["total_n_pixels"]),
}
2024-11-07 21:40:26 +00:00
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"]),
2024-11-07 21:40:26 +00:00
}
2024-11-07 21:40:26 +00:00
def bitcoin_business_growth_percent_diff_schema(data):
return {
"country_name": str(data["country_name"]),
"date_range": str(f'{data["first_date"]} to {data["last_date"]}'),
"first_value": int(data["first_value"]),
"last_value": int(data["last_value"]),
"difference": int(data["difference"]),
"percent_difference": str(data["percent_difference"]),
2024-11-07 21:40:26 +00:00
}
2024-11-07 21:40:26 +00:00
def bitcoin_business_growth_timeseries_schema(data):
return {
"country_name": str(data["country_name"]),
"date": data["date"],
"cumulative_value": int(data["cumulative_value"]),
}
def miner_rewards_schema(data):
return {
"date": data["date"],
"block_subsidy": data["block_subsidy"],
"total_reward_usd": data["total_reward_usd"],
"totalfee_usd": data["totalfee_usd"],
"subsidy_usd": data["subsidy_usd"],
2024-11-07 21:40:26 +00:00
}
2025-01-11 15:10:53 +00:00
def feerate_percentiles_schema(data):
return {
"date": data["date"],
"feerate_10th": data["feerate_10th"],
"feerate_25th": data["feerate_25th"],
"feerate_50th": data["feerate_50th"],
"feerate_75th": data["feerate_75th"],
"feerate_90th": data["feerate_90th"],
"maxfeerate": data["maxfeerate"],
"minfeerate": data["minfeerate"],
}
def bitcoin_price_timeseries_schema(data):
return {
"date": data["date"],
"price": data["price"],
}
def bitcoin_hashrate_schema(data):
return {
"date": data["date"],
"hashrate": data["hashrate"],
"difficulty": data["difficulty"],
"hashrate28": data["hashrate28"],
"difficulty28": data["difficulty28"],
}
2024-11-07 21:40:26 +00:00
class DataSerializer:
def __init__(self, schema_func):
self.schema_func = schema_func
def serialize_one(self, data) -> dict:
return self.schema_func(dict(data))
2024-11-07 21:40:26 +00:00
def serialize_many(self, data_list) -> list:
return [self.serialize_one(data) for data in data_list]