Skip to content

Commit

Permalink
add more metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lilioid committed Feb 13, 2024
1 parent 3e670e5 commit 9b0007b
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/vinywaji/metrics/async_instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ def create_async_instruments():
description="The total number of transactions recorded in vinywaji",
unit="count",
)
vinywaji_meter.create_observable_gauge(
"vinywaji_transactions",
callbacks=[calc_transaction_aggregates],
description="The aggregated value (in euro-cent) of all transactions recorded in vinywaji",
unit="ct",
)
vinywaji_meter.create_observable_gauge(
"vinywaji_balances",
callbacks=[calc_asset_aggregates],
callbacks=[calc_balances],
description="The aggregated value of account balances recorded in vinywaji (in euro-cent) ",
unit="ct",
)
vinywaji_meter.create_observable_counter(
"vinywaji_users",
callbacks=[count_users],
description="How many users have an account registered in Vinywaji",
unit="count",
)


def count_transactions(_options: CallbackOptions) -> Iterable[Observation]:
Expand All @@ -34,31 +46,36 @@ def count_transactions(_options: CallbackOptions) -> Iterable[Observation]:
yield Observation(value=n_positive + n_negative, attributes={"transaction_type": "any"})


def calc_asset_aggregates(_options: CallbackOptions) -> Iterable[Observation]:
def count_users(_options: CallbackOptions) -> Iterable[Observation]:
n = models.User.objects.all().count()
yield Observation(value=n)


def calc_transaction_aggregates(_options: CallbackOptions) -> Iterable[Observation]:
negative_sum = models.Transaction.objects.all().filter(amount__gt=0).aggregate(sum=Sum("amount"))
positive_sum = models.Transaction.objects.all().filter(amount__lt=0).aggregate(sum=Sum("amount"))

yield Observation(
attributes={"transaction_type": "withdrawal"},
value=negative_sum["sum"],
)
yield Observation(attributes={"transaction_type": "deposit"}, value=positive_sum["sum"])
yield Observation(attributes={"transaction_type": "all"}, value=positive_sum["sum"] + negative_sum["sum"])


def calc_balances(_options: CallbackOptions) -> Iterable[Observation]:
# aggregate all negative transactions
balances = [
i["current_balance"]
for i in models.User.objects.all().annotate(current_balance=Sum("transactions__amount")).values()
]

yield Observation(attributes={"balances": "all", "aggregate_type": "sum"}, value=reduce(add, balances, 0))
yield Observation(
attributes={"balances": "all", "aggregate_type": "avg"},
value=reduce(add, balances, 0) / len(balances),
)
yield Observation(
attributes={"balances": "negative", "aggregate_type": "sum"},
value=reduce(add, (i for i in balances if i < 0), 0),
)
yield Observation(
attributes={"balances": "negative", "aggregate_type": "avg"},
value=reduce(add, (i for i in balances if i < 0), 0) / len(balances),
)
yield Observation(
attributes={"balances": "positive", "aggregate_type": "sum"},
value=reduce(add, (i for i in balances if i > 0), 0),
)
yield Observation(
attributes={"balances": "positive", "aggregate_type": "avg"},
value=reduce(add, (i for i in balances if i > 0), 0) / len(balances),
)

0 comments on commit 9b0007b

Please sign in to comment.