Skip to content

Commit

Permalink
feat(#2010): add more DRep metrics to network metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Nov 25, 2024
1 parent af1ca81 commit bdc59f3
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ changes.

### Added

-
- Add more useful metrics to the backend GET /network/metrics endpoint [Issue 2010](https://github.com/IntersectMBO/govtool/issues/2010)

### Fixed

Expand Down
151 changes: 139 additions & 12 deletions govtool/backend/sql/get-network-metrics.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
WITH current_epoch AS (
WITH DRepActivity AS (
SELECT
drep_activity AS drep_activity,
epoch_no AS epoch_no
FROM
epoch_param
WHERE
epoch_no IS NOT NULL
ORDER BY
epoch_no DESC
LIMIT 1
),
active_drep_boundary_epoch AS (
SELECT
epoch_no - drep_activity AS epoch_no
FROM
DRepActivity
),
RankedDRep AS (
SELECT
dh.raw AS drep_hash_raw,
b.epoch_no,
dr.deposit,
dr.voting_anchor_id,
ROW_NUMBER() OVER (PARTITION BY dh.raw ORDER BY b.epoch_no DESC) AS rank
FROM
drep_hash dh
JOIN
drep_registration dr ON dh.id = dr.drep_hash_id
JOIN
tx t ON dr.tx_id = t.id
JOIN
block b ON t.block_id = b.id
WHERE
dr.deposit >= 0
GROUP BY
dh.raw,
b.epoch_no,
dr.voting_anchor_id,
dr.deposit
),
current_epoch AS (
SELECT
Max(NO) AS no
FROM
Expand Down Expand Up @@ -36,11 +77,70 @@ total_drep_votes AS (
WHERE
voter_role = 'DRep'
),
total_stake_controlled_by_dreps AS (
SELECT
SUM(dd.amount)::bigint AS total
FROM
drep_distr dd
),
total_registered_direct_voters AS (
SELECT
COUNT(DISTINCT dh.raw) AS unique_direct_voters
FROM
drep_registration dr
JOIN
drep_hash dh
ON
dr.drep_hash_id = dh.id
LEFT JOIN
voting_anchor va
ON
dr.voting_anchor_id = va.id
WHERE
dr.deposit > 0
AND va.url IS NULL
),
total_registered_dreps AS (
SELECT
count(DISTINCT dh.raw) AS unique_registrations
FROM
drep_registration dr
JOIN
drep_hash dh
ON
dr.drep_hash_id = dh.id
WHERE
dr.deposit > 0
),
total_active_dreps AS (
SELECT
count(*) AS count
count(DISTINCT drep_hash_raw) AS unique_active_drep_registrations
FROM
RankedDRep
WHERE
epoch_no >= (SELECT epoch_no FROM active_drep_boundary_epoch)
AND rank = 1
),
total_inactive_dreps AS (
SELECT
total_registered_dreps.unique_registrations - total_active_dreps.unique_active_drep_registrations AS total_inactive_dreps
FROM
drep_hash
total_registered_dreps
CROSS JOIN
total_active_dreps
),
total_active_cip119_compliant_dreps AS (
SELECT
count(DISTINCT drep_hash_raw) AS unique_active_cip119_compliant_drep_registrations
FROM
RankedDRep
JOIN
voting_anchor va on va.id = RankedDRep.voting_anchor_id
JOIN off_chain_vote_data ocvd on ocvd.voting_anchor_id = va.id
JOIN off_chain_vote_drep_data ocvdd on ocvdd.off_chain_vote_data_id = ocvd.id
WHERE
-- given_name is the only compulsory field in CIP-119
ocvdd.given_name IS NOT NULL
),
always_abstain_voting_power AS (
SELECT
Expand All @@ -63,15 +163,20 @@ always_no_confidence_voting_power AS (
drep_hash.view = 'drep_always_no_confidence' ORDER BY epoch_no DESC LIMIT 1), 0) AS amount
)
SELECT
current_epoch.no,
current_epoch.no as epoch_no,
current_block.block_no,
unique_delegators.count,
total_delegations.count,
total_gov_action_proposals.count,
total_drep_votes.count,
total_registered_dreps.count,
always_abstain_voting_power.amount,
always_no_confidence_voting_power.amount,
unique_delegators.count as unique_delegators,
total_delegations.count as total_delegations,
total_gov_action_proposals.count as total_gov_action_proposals,
total_drep_votes.count as total_drep_votes,
total_registered_dreps.unique_registrations as total_registered_dreps,
total_stake_controlled_by_dreps.total as total_stake_controlled_by_dreps,
total_active_dreps.unique_active_drep_registrations as total_active_dreps,
total_inactive_dreps.total_inactive_dreps as total_inactive_dreps,
total_active_cip119_compliant_dreps.unique_active_cip119_compliant_drep_registrations as total_active_cip119_compliant_dreps,
total_registered_direct_voters.unique_direct_voters as total_registered_direct_voters,
always_abstain_voting_power.amount as always_abstain_voting_power,
always_no_confidence_voting_power.amount as always_no_confidence_voting_power,
network_name
FROM
current_epoch
Expand All @@ -81,6 +186,28 @@ FROM
CROSS JOIN total_gov_action_proposals
CROSS JOIN total_drep_votes
CROSS JOIN total_registered_dreps
CROSS JOIN total_stake_controlled_by_dreps
CROSS JOIN total_active_dreps
CROSS JOIN total_inactive_dreps
CROSS JOIN total_active_cip119_compliant_dreps
CROSS JOIN total_registered_direct_voters
CROSS JOIN always_abstain_voting_power
CROSS JOIN always_no_confidence_voting_power
CROSS JOIN meta;
CROSS JOIN meta
GROUP BY
current_epoch.no,
current_block.block_no,
unique_delegators.count,
total_delegations.count,
total_gov_action_proposals.count,
total_drep_votes.count,
total_registered_dreps.unique_registrations,
total_stake_controlled_by_dreps.total,
total_active_dreps.unique_active_drep_registrations,
total_inactive_dreps.total_inactive_dreps,
total_active_cip119_compliant_dreps.unique_active_cip119_compliant_drep_registrations,
total_registered_direct_voters.unique_direct_voters,
always_abstain_voting_power.amount,
always_no_confidence_voting_power.amount,
network_name;

5 changes: 5 additions & 0 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ getNetworkMetrics = do
, getNetworkMetricsResponseTotalGovernanceActions = networkMetricsTotalGovernanceActions
, getNetworkMetricsResponseTotalDRepVotes = networkMetricsTotalDRepVotes
, getNetworkMetricsResponseTotalRegisteredDReps = networkMetricsTotalRegisteredDReps
, getNetworkMetricsResponseTotalStakeControlledByDReps = networkMetricsTotalStakeControlledByDReps
, getNetworkMetricsResponseTotalActiveDReps = networkMetricsTotalActiveDReps
, getNetworkMetricsResponseTotalInactiveDReps = networkMetricsTotalInactiveDReps
, getNetworkMetricsResponseTotalActiveCIP119CompliantDReps = networkMetricsTotalActiveCIP119CompliantDReps
, getNetworkMetricsResponseTotalRegisteredDirectVoters = networkMetricsTotalRegisteredDirectVoters
, getNetworkMetricsResponseAlwaysAbstainVotingPower = networkMetricsAlwaysAbstainVotingPower
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower = networkMetricsAlwaysNoConfidenceVotingPower
, getNetworkMetricsResponseNetworkName = networkMetricsNetworkName
Expand Down
32 changes: 21 additions & 11 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -878,17 +878,22 @@ instance ToSchema DelegationResponse where

data GetNetworkMetricsResponse
= GetNetworkMetricsResponse
{ getNetworkMetricsResponseCurrentTime :: UTCTime
, getNetworkMetricsResponseCurrentEpoch :: Integer
, getNetworkMetricsResponseCurrentBlock :: Integer
, getNetworkMetricsResponseUniqueDelegators :: Integer
, getNetworkMetricsResponseTotalDelegations :: Integer
, getNetworkMetricsResponseTotalGovernanceActions :: Integer
, getNetworkMetricsResponseTotalDRepVotes :: Integer
, getNetworkMetricsResponseTotalRegisteredDReps :: Integer
, getNetworkMetricsResponseAlwaysAbstainVotingPower :: Integer
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower :: Integer
, getNetworkMetricsResponseNetworkName :: Text
{ getNetworkMetricsResponseCurrentTime :: UTCTime
, getNetworkMetricsResponseCurrentEpoch :: Integer
, getNetworkMetricsResponseCurrentBlock :: Integer
, getNetworkMetricsResponseUniqueDelegators :: Integer
, getNetworkMetricsResponseTotalDelegations :: Integer
, getNetworkMetricsResponseTotalGovernanceActions :: Integer
, getNetworkMetricsResponseTotalDRepVotes :: Integer
, getNetworkMetricsResponseTotalRegisteredDReps :: Integer
, getNetworkMetricsResponseTotalStakeControlledByDReps :: Integer
, getNetworkMetricsResponseTotalActiveDReps :: Integer
, getNetworkMetricsResponseTotalInactiveDReps :: Integer
, getNetworkMetricsResponseTotalActiveCIP119CompliantDReps :: Integer
, getNetworkMetricsResponseTotalRegisteredDirectVoters :: Integer
, getNetworkMetricsResponseAlwaysAbstainVotingPower :: Integer
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower :: Integer
, getNetworkMetricsResponseNetworkName :: Text
}

deriveJSON (jsonOptions "getNetworkMetricsResponse") ''GetNetworkMetricsResponse
Expand All @@ -903,6 +908,11 @@ exampleGetNetworkMetricsResponse =
<> "\"totalGovernanceActions\": 0,"
<> "\"totalDRepVotes\": 0,"
<> "\"totalRegisteredDReps\": 0,"
<> "\"totalStakeControlledByDReps\": 0,"
<> "\"totalActiveDReps\": 0,"
<> "\"totalInactiveDReps\": 0,"
<> "\"totalActiveCIP119CompliantDReps\": 0,"
<> "\"totalRegisteredDirectVoters\": 0,"
<> "\"alwaysAbstainVotingPower\": 0,"
<> "\"alwaysNoConfidenceVotingPower\": 0,"
<> "\"networkName\": \"Mainnet\"}"
Expand Down
10 changes: 10 additions & 0 deletions govtool/backend/src/VVA/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ networkMetrics = withPool $ \conn -> do
, total_gov_action_proposals
, total_drep_votes
, total_registered_dreps
, total_stake_controlled_by_dreps
, total_active_dreps
, total_inactive_dreps
, total_active_cip119_compliant_dreps
, total_registered_direct_voters
, always_abstain_voting_power
, always_no_confidence_voting_power
, network_name
Expand All @@ -54,6 +59,11 @@ networkMetrics = withPool $ \conn -> do
total_gov_action_proposals
total_drep_votes
total_registered_dreps
total_stake_controlled_by_dreps
total_active_dreps
total_inactive_dreps
total_active_cip119_compliant_dreps
total_registered_direct_voters
always_abstain_voting_power
always_no_confidence_voting_power
network_name
Expand Down
27 changes: 16 additions & 11 deletions govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,22 @@ data CacheEnv

data NetworkMetrics
= NetworkMetrics
{ networkMetricsCurrentTime :: UTCTime
, networkMetricsCurrentEpoch :: Integer
, networkMetricsCurrentBlock :: Integer
, networkMetricsUniqueDelegators :: Integer
, networkMetricsTotalDelegations :: Integer
, networkMetricsTotalGovernanceActions :: Integer
, networkMetricsTotalDRepVotes :: Integer
, networkMetricsTotalRegisteredDReps :: Integer
, networkMetricsAlwaysAbstainVotingPower :: Integer
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
, networkMetricsNetworkName :: Text
{ networkMetricsCurrentTime :: UTCTime
, networkMetricsCurrentEpoch :: Integer
, networkMetricsCurrentBlock :: Integer
, networkMetricsUniqueDelegators :: Integer
, networkMetricsTotalDelegations :: Integer
, networkMetricsTotalGovernanceActions :: Integer
, networkMetricsTotalDRepVotes :: Integer
, networkMetricsTotalRegisteredDReps :: Integer
, networkMetricsTotalStakeControlledByDReps :: Integer
, networkMetricsTotalActiveDReps :: Integer
, networkMetricsTotalInactiveDReps :: Integer
, networkMetricsTotalActiveCIP119CompliantDReps :: Integer
, networkMetricsTotalRegisteredDirectVoters :: Integer
, networkMetricsAlwaysAbstainVotingPower :: Integer
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
, networkMetricsNetworkName :: Text
}

data Delegation
Expand Down
5 changes: 5 additions & 0 deletions govtool/frontend/src/models/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export type NetworkMetrics = {
totalGovernanceActions: number;
totalDRepVotes: number;
totalRegisteredDReps: number;
totalStakeControlledByDReps: number;
totalActiveDReps: number;
totalInactiveDReps: number;
totalActiveCIP119CompliantDReps: number;
totalRegisteredDirectVoters: number;
alwaysAbstainVotingPower: number;
alwaysNoConfidenceVotingPower: number;
networkName: string;
Expand Down

0 comments on commit bdc59f3

Please sign in to comment.