-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jorm-metrics-server): fixes voting power metrics to be accurate (#…
…178)
- Loading branch information
Showing
3 changed files
with
83 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
services/jorm-metrics-server/jorm_metrics_server/metrics/vote_power_dist.py
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
services/jorm-metrics-server/jorm_metrics_server/metrics/voting_power.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""This module provides various gauges for observing voting power.""" | ||
|
||
# cspell: words pydantic | ||
|
||
from dataclasses import dataclass | ||
|
||
from prometheus_client import Gauge | ||
|
||
from ..client import ApiClient | ||
|
||
VOTING_POWER_RANGE_VOTERS = Gauge( | ||
"voting_power_range_voters", | ||
"Total number of voters in fixed ranges of voting power", | ||
["range"], | ||
) | ||
|
||
VOTING_POWER_RANGE_SUM = Gauge( | ||
"voting_power_range_sum", | ||
"Sum of voting power in fixed ranges of voting power", | ||
["range"], | ||
) | ||
|
||
VOTING_POWER_TOTAL = Gauge( | ||
"voting_power_total", | ||
"Total voting power", | ||
) | ||
|
||
|
||
@dataclass | ||
class Bucket: | ||
"""A bucket for a range of voting power. | ||
Attributes: | ||
min (int): The minimum voting power. | ||
max (int): The maximum voting power. | ||
power (int): The sum of voting power in this bucket. | ||
voters (int): The number of voters in this bucket. | ||
""" | ||
|
||
min: int | ||
max: int | ||
power: int | ||
voters: int | ||
|
||
|
||
async def scrape(client: ApiClient): | ||
"""Scrape the voting power of all voters and update the gauges. | ||
Args: | ||
client (ApiClient): The API client to use. | ||
""" | ||
voters = await client.get_voters() | ||
|
||
bucket_ranges = [ | ||
(0, 1000), | ||
(1000, 10000), | ||
(10000, 100000), | ||
(100000, 1000000), | ||
(1000000, 10000000), | ||
(10000000, float("inf")), | ||
] | ||
|
||
buckets: list[Bucket] = [] | ||
for range in bucket_ranges: | ||
buckets.append(Bucket(range[0], range[1], 0, 0)) | ||
|
||
for bucket in buckets: | ||
for voter in voters.values(): | ||
if bucket.min <= voter.data.value < bucket.max: | ||
bucket.voters += 1 | ||
bucket.power += voter.data.value | ||
|
||
for bucket in buckets: | ||
VOTING_POWER_RANGE_VOTERS.labels(f"{bucket.min}-{bucket.max}").set( | ||
bucket.voters | ||
) | ||
VOTING_POWER_RANGE_SUM.labels(f"{bucket.min}-{bucket.max}").set( | ||
bucket.power | ||
) | ||
|
||
VOTING_POWER_TOTAL.set(sum([bucket.power for bucket in buckets])) |