From 9b0007bca9e48c346661ae2ab94ea937f47ab5ce Mon Sep 17 00:00:00 2001 From: ftsell Date: Tue, 13 Feb 2024 10:16:44 +0100 Subject: [PATCH] add more metrics --- src/vinywaji/metrics/async_instruments.py | 45 ++++++++++++++++------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/vinywaji/metrics/async_instruments.py b/src/vinywaji/metrics/async_instruments.py index b3a408d..17895ad 100644 --- a/src/vinywaji/metrics/async_instruments.py +++ b/src/vinywaji/metrics/async_instruments.py @@ -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]: @@ -34,7 +46,24 @@ 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"] @@ -42,23 +71,11 @@ def calc_asset_aggregates(_options: CallbackOptions) -> Iterable[Observation]: ] 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), - )