From fecc3bce183c62665ff2415ab25a4d981f008bf1 Mon Sep 17 00:00:00 2001 From: biwano Date: Tue, 10 Oct 2023 15:55:19 +0200 Subject: [PATCH] Adds a pool summary endpoint --- src/apps/api/app.py | 1 + src/apps/api/endpoints/__init__.py | 1 + src/apps/api/endpoints/credits.py | 21 ++++++++++++++++++--- src/apps/api/test.py | 1 + src/apps/services/credits.py | 24 +++++++++++++++++++----- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/apps/api/app.py b/src/apps/api/app.py index bbddf1f..a0b8a29 100644 --- a/src/apps/api/app.py +++ b/src/apps/api/app.py @@ -29,6 +29,7 @@ def output_json(data, code, headers=None): api.add_resource(endpoints.CreditsProjectsAggregation, '/credits/agg/projects') api.add_resource(endpoints.CreditsMethodologiesAggregation, '/credits/agg/methodologies') api.add_resource(endpoints.CreditsVintageAggregation, '/credits/agg/vintage') +api.add_resource(endpoints.CreditsPoolAggregation, '/credits/agg/pool') api.add_resource(endpoints.CreditsPoolVintageAggregation, '/credits/agg/pool/vintage') api.add_resource(endpoints.CreditsPoolMethodologyAggregation, '/credits/agg/pool/methodology') api.add_resource(endpoints.CreditsPoolDatesAggregation, '/credits/agg/pool/') diff --git a/src/apps/api/endpoints/__init__.py b/src/apps/api/endpoints/__init__.py index 39ace88..ba57fae 100644 --- a/src/apps/api/endpoints/__init__.py +++ b/src/apps/api/endpoints/__init__.py @@ -7,6 +7,7 @@ CreditsProjectsAggregation, CreditsMethodologiesAggregation, CreditsVintageAggregation, + CreditsPoolAggregation, CreditsPoolVintageAggregation, CreditsPoolMethodologyAggregation, CreditsPoolDatesAggregation, diff --git a/src/apps/api/endpoints/credits.py b/src/apps/api/endpoints/credits.py index c5286a9..9d18c81 100644 --- a/src/apps/api/endpoints/credits.py +++ b/src/apps/api/endpoints/credits.py @@ -63,6 +63,9 @@ def get_credits(self, bridge=None): # Return credits return Service().filter(bridge, pool, status) + def get_pool_credits(self, bridge=None): + return self.get_credits(bridge).pool_analysis() + class CreditsRaw(AbstractCredits): @layout_cache.cached(query_string=True) @@ -164,7 +167,19 @@ class CreditsPoolVintageAggregation(AbstractCredits): ) @helpers.with_output_formatter def get(self): - credits = self.get_credits().vintage_agg().pool_summary("vintage") + credits = self.get_pool_credits().vintage_agg().pool_summary("vintage") + return credits + + +class CreditsPoolAggregation(AbstractCredits): + @layout_cache.cached(query_string=True) + @helpers.with_errors_handler + @helpers.with_help( + f"""{BASE_HELP} + """ + ) + def get(self): + credits = self.get_pool_credits().pool_summary().resolve().to_dict(orient='records')[0] return credits @@ -178,7 +193,7 @@ class CreditsPoolMethodologyAggregation(AbstractCredits): ) @helpers.with_output_formatter def get(self): - credits = self.get_credits().methodologies_agg().pool_summary("methodology") + credits = self.get_pool_credits().methodologies_agg().pool_summary("methodology") return credits @@ -193,7 +208,7 @@ class CreditsPoolDatesAggregation(AbstractCredits): @helpers.with_output_formatter def get(self, freq): date_column = self.get_default_date_field() - credits = self.get_credits().date_agg(date_column, freq).pool_summary(date_column) + credits = self.get_pool_credits().date_agg(date_column, freq).pool_summary(date_column) return credits diff --git a/src/apps/api/test.py b/src/apps/api/test.py index 3f4a76b..ddd4c1d 100644 --- a/src/apps/api/test.py +++ b/src/apps/api/test.py @@ -13,6 +13,7 @@ "credits/agg/projects", "credits/agg/methodologies", "credits/agg/vintage", + "credits/agg/pool", "credits/agg/pool/vintage", "credits/agg/pool/methodology", "credits/agg/pool/daily", diff --git a/src/apps/services/credits.py b/src/apps/services/credits.py index e978daf..07d0463 100644 --- a/src/apps/services/credits.py +++ b/src/apps/services/credits.py @@ -110,8 +110,9 @@ def methodologies_agg(self, df): df = df.groupby("methodology") return df + @chained_cached_command() - def pool_summary(self, df, date_field): + def pool_summary(self, df, kept_fields=[]): columns = [ "quantity", "total_quantity", @@ -121,16 +122,27 @@ def pool_summary(self, df, date_field): "nbo_quantity", "mco2_quantity" ] + if isinstance(df, pd.DataFrame): + df = df.groupby(lambda x: True) + + if not isinstance(kept_fields, list): + kept_fields = [kept_fields] def summary(df): res_df = pd.DataFrame() - res_df[date_field] = [df[date_field].iloc[0]] + for kept_field in kept_fields: + res_df[kept_field] = [df[kept_field].iloc[0]] + not_pooled_quantity = df["total_quantity"].sum() for column in columns: if column in df: - res_df[column] = [df[column].sum()] + column_quantity = df[column].sum() + res_df[column] = [column_quantity] + if column not in ["quantity", "total_quantity"]: + not_pooled_quantity -= column_quantity + res_df["not_pooled_quantity"] = [not_pooled_quantity] + return res_df - df = df.apply(summary).reset_index(drop=True) return df @@ -187,7 +199,9 @@ def filter_pool_quantity(self, df, quantity_column): return df - def drop_duplicates(self, df): + @chained_cached_command() + def pool_analysis(self, df): + """When analysing pools we need a subset of the data""" df = df.drop_duplicates(subset=["token_address"], keep="first") df = df.reset_index(drop=True) return df