Skip to content

Commit

Permalink
Adds a pool summary endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
biwano committed Oct 10, 2023
1 parent e1eb997 commit fecc3bc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/apps/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<string:freq>')
Expand Down
1 change: 1 addition & 0 deletions src/apps/api/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CreditsProjectsAggregation,
CreditsMethodologiesAggregation,
CreditsVintageAggregation,
CreditsPoolAggregation,
CreditsPoolVintageAggregation,
CreditsPoolMethodologyAggregation,
CreditsPoolDatesAggregation,
Expand Down
21 changes: 18 additions & 3 deletions src/apps/api/endpoints/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand All @@ -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


Expand Down
1 change: 1 addition & 0 deletions src/apps/api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 19 additions & 5 deletions src/apps/services/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit fecc3bc

Please sign in to comment.