Skip to content

Commit

Permalink
chore: fix null on application stats
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 23, 2024
1 parent fee5265 commit 7df5e3a
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions icon_stats/crons/application_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ async def get_tokens_sum(column, application_id) -> int:
result = await session.execute(query)
return result.all()[0][0]

async def set_field_not_zero(
application: Application,
target_column: str,
func: Callable[[str, str], Awaitable[int]],
source_column: str = None,
):
out = await func(source_column, application.id)
setattr(application, target_column, out if out is not None else 0)


async def set_field(
application: Application,
column: str,
func: Callable[[str, str], Awaitable[int]],
target_column: str = None,
):
for i in ["24h", "7d", "30d"]:
column_name = f"{column}_{i}"
if target_column is None:
target_column = column_name
prev_column_name = f"{column_name}_prev"
setattr(application, column_name, await func(target_column, application.id))
setattr(application, prev_column_name, await func(target_column, application.id))
await set_field_not_zero(application, column_name, func, column_name)
await set_field_not_zero(application, prev_column_name, func, prev_column_name)


async def run_application_stats():
Expand All @@ -65,13 +71,31 @@ async def run_application_stats():
]:
await set_field(i, token_column, get_tokens_sum)
# Unique transaction addresses
i.transaction_addresses_24h = await get_contracts_sum("unique_addresses_24h", i.id)
i.transaction_addresses_7d = await get_contracts_sum("unique_addresses_7d", i.id)
i.transaction_addresses_30d = await get_contracts_sum("unique_addresses_30d", i.id)
await set_field_not_zero(
i, "transaction_addresses_24h",
get_contracts_sum, "unique_addresses_24h"
)
await set_field_not_zero(
i, "transaction_addresses_7d",
get_contracts_sum, "unique_addresses_7d"
)
await set_field_not_zero(
i, "transaction_addresses_30d",
get_contracts_sum, "unique_addresses_30d"
)
# Unique token transfer addresses
i.token_transfer_addresses_24h = await get_tokens_sum("unique_addresses_24h", i.id)
i.token_transfer_addresses_7d = await get_tokens_sum("unique_addresses_7d", i.id)
i.token_transfer_addresses_30d = await get_tokens_sum("unique_addresses_30d", i.id)
await set_field_not_zero(
i, "token_transfer_addresses_24h",
get_tokens_sum, "unique_addresses_24h"
)
await set_field_not_zero(
i, "token_transfer_addresses_7d",
get_tokens_sum, "unique_addresses_7d"
)
await set_field_not_zero(
i, "token_transfer_addresses_30d",
get_tokens_sum, "unique_addresses_30d"
)

i.last_updated_timestamp = datetime.now(timezone.utc).timestamp()
await i.upsert()
Expand Down

0 comments on commit 7df5e3a

Please sign in to comment.