Skip to content

Commit

Permalink
Adds credits bridge aggregation endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
biwano committed Oct 11, 2023
1 parent e1eb997 commit 961eaf3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/apps/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def output_json(data, code, headers=None):
api.add_resource(endpoints.CreditsPoolDatesAggregation, '/credits/agg/pool/<string:freq>')
api.add_resource(endpoints.CreditsBridgeVintageAggregation, '/credits/agg/bridge/vintage')
api.add_resource(endpoints.CreditsBridgeCountriesAggregation, '/credits/agg/bridge/countries')
api.add_resource(endpoints.CreditsBridgeDateAggregation, '/credits/agg/bridge/<string:freq>')
api.add_resource(endpoints.CreditsBridgeAggregation, '/credits/agg/bridge')


api.add_resource(endpoints.PoolsRaw, '/pools/raw')
Expand Down
2 changes: 2 additions & 0 deletions src/apps/api/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
CreditsPoolDatesAggregation,
CreditsBridgeVintageAggregation,
CreditsBridgeCountriesAggregation,
CreditsBridgeDateAggregation,
CreditsBridgeAggregation,
CreditsGlobalAggregation
)
from .pools import ( # noqa
Expand Down
45 changes: 43 additions & 2 deletions src/apps/api/endpoints/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
BRIDGES = ["all", "offchain", "toucan", "c3", "moss", "polygon", "eth"]
POOLS = ["ubo", "nbo", "nct", "bct", "all"]
STATUSES = ["issued", "bridged", "deposited", "redeemed", "retired", "all_retired", "all"]
OFFCHAIN_FILTER = ["tokenized", "toucan", "c3", "moss"]
DATE_FIELDS = [
"bridged_date",
"issuance_date",
Expand All @@ -21,6 +22,11 @@
credits_filter_parser.add_argument('bridge', type=helpers.validate_list(BRIDGES), default="all")
credits_filter_parser.add_argument('pool', type=helpers.validate_list(POOLS + [None]), default=None)
credits_filter_parser.add_argument('status', type=helpers.validate_list(STATUSES + [None]), default=None)
credits_filter_parser.add_argument(
'offchain_filter',
type=helpers.validate_list(OFFCHAIN_FILTER + [None]),
default=None
)


class AbstractCredits(Resource):
Expand All @@ -47,6 +53,7 @@ def get_credits(self, bridge=None):
bridge = args["bridge"]
pool = args["pool"]
status = args["status"]
offchain_filter = args["offchain_filter"]

# Accept a 'all' value for pools
if pool == "all":
Expand All @@ -60,8 +67,14 @@ def get_credits(self, bridge=None):
if status is None:
status = "issued" if bridge == "offchain" else "bridged"

# Return credits
return Service().filter(bridge, pool, status)
# Select credits
df = Service().filter(bridge, pool, status)

# Filter offchain credits
if offchain_filter:
df = df.offchain_filter(offchain_filter)

return df


class CreditsRaw(AbstractCredits):
Expand Down Expand Up @@ -225,6 +238,34 @@ def get(self):
return credits


class CreditsBridgeDateAggregation(AbstractCredits):
@layout_cache.cached(query_string=True)
@helpers.with_errors_handler
@helpers.with_help(
f"""{BASE_HELP}
{helpers.dates_aggregation_help(DATE_FIELDS)}
{helpers.OUTPUT_FORMATTER_HELP}
{helpers.DATES_FILTER_HELP}
"""
)
@helpers.with_output_formatter
def get(self, freq):
return self.get_credits(bridge="offchain").date_agg("issuance_date", freq).bridge_summary("issuance_date")


class CreditsBridgeAggregation(AbstractCredits):
@layout_cache.cached(query_string=True)
@helpers.with_errors_handler
@helpers.with_help(
f"""{BASE_HELP}
{helpers.dates_aggregation_help(DATE_FIELDS)}
{helpers.DATES_FILTER_HELP}
"""
)
def get(self):
return self.get_credits(bridge="offchain").bridge_summary("quantity").resolve().to_dict(orient='records')[0]


class CreditsGlobalAggregation(AbstractCredits):
@layout_cache.cached(query_string=True)
@helpers.with_errors_handler
Expand Down
2 changes: 2 additions & 0 deletions src/apps/api/endpoints/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def get(self):
"credits/agg/pool/monthly",
"credits/agg/bridge/vintage",
"credits/agg/bridge/countries",
"credits/agg/bridge/daily",
"credits/agg/bridge/monthly",
"pools/raw",
"pools/agg",
"pools/agg/daily",
Expand Down
8 changes: 8 additions & 0 deletions src/apps/services/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def load_df(self, bridge: str, pool: str, status: str):
if bridge in ["offchain"]:
if status == "issued":
df = s3.load("verra_data_v2")
elif status == "bridged":
df = s3.load("verra_data_v2")
df = df.query("toucan | c3 | moss")
# This is a hack to get all retired offsets even if the retirements occured offchain
elif status == "all_retired":
df = s3.load("verra_data_v2")
Expand Down Expand Up @@ -136,6 +139,10 @@ def summary(df):

@chained_cached_command()
def bridge_summary(self, df, kept_fields):
# Full aggregation if we are presented a dataframe not grouped yet
if isinstance(df, pd.DataFrame):
df = df.groupby(lambda x: True)

column = "quantity"
if not isinstance(kept_fields, list):
kept_fields = [kept_fields]
Expand All @@ -156,6 +163,7 @@ def summary(df):
return res_df

df = df.apply(summary).reset_index(drop=True)

return df

@final_cached_command()
Expand Down

0 comments on commit 961eaf3

Please sign in to comment.