Skip to content

Commit

Permalink
refactor: replace switch with map
Browse files Browse the repository at this point in the history
See: BEDS-635
  • Loading branch information
LuccaBitfly committed Oct 30, 2024
1 parent 05df046 commit bfe90b8
Showing 1 changed file with 33 additions and 56 deletions.
89 changes: 33 additions & 56 deletions backend/pkg/api/handlers/handler_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,64 +500,41 @@ func mapIndexBlocksSlice(category string, validators []types.IndexBlocks) types.
// --------------------------------------
// notification event mapping

var dbEventToResponse = map[string]string{
string(commontypes.ValidatorIsOfflineEventName): "validator_offline",
string(commontypes.ValidatorIsOnlineEventName): "validator_online",
string(commontypes.ValidatorMissedAttestationEventName): "attestation_missed",
string(commontypes.ValidatorMissedProposalEventName): "proposal_missed",
string(commontypes.ValidatorExecutedProposalEventName): "proposal_success",
string(commontypes.ValidatorUpcomingProposalEventName): "proposal_upcoming",
string(commontypes.SyncCommitteeSoonEventName): "sync",
string(commontypes.ValidatorReceivedWithdrawalEventName): "withdrawal",
string(commontypes.ValidatorGotSlashedEventName): "validator_got_slashed",
string(commontypes.ValidatorDidSlashEventName): "validator_has_slashed",
string(commontypes.ValidatorGroupEfficiencyEventName): "group_efficiency_below",
string(commontypes.RocketpoolCollateralMinReachedEventName): "min_collateral",
string(commontypes.RocketpoolCollateralMaxReachedEventName): "max_collateral",
string(commontypes.IncomingTransactionEventName): "incoming_tx",
string(commontypes.OutgoingTransactionEventName): "outgoing_tx",
string(commontypes.ERC20TokenTransferEventName): "transfer_erc20",
string(commontypes.ERC721TokenTransferEventName): "transfer_erc721",
string(commontypes.ERC1155TokenTransferEventName): "transfer_erc1155",
string(commontypes.MonitoringMachineOfflineEventName): "offline",
string(commontypes.MonitoringMachineDiskAlmostFullEventName): "storage",
string(commontypes.MonitoringMachineCpuLoadEventName): "cpu",
string(commontypes.MonitoringMachineMemoryUsageEventName): "memory",
string(commontypes.RocketpoolNewClaimRoundStartedEventName): "new_reward_round",
string(commontypes.NetworkGasBelowThresholdEventName): "gas_below",
string(commontypes.NetworkGasAboveThresholdEventName): "gas_above",
string(commontypes.NetworkParticipationRateThresholdEventName): "participation_rate",
}

func mapNotificationEventName(event string) string {
switch event {
case string(commontypes.ValidatorIsOfflineEventName):
return "validator_offline"
case string(commontypes.ValidatorIsOnlineEventName):
return "validator_online"
case string(commontypes.ValidatorMissedAttestationEventName):
return "attestation_missed"
case string(commontypes.ValidatorMissedProposalEventName):
return "proposal_missed"
case string(commontypes.ValidatorExecutedProposalEventName):
return "proposal_success"
case string(commontypes.ValidatorUpcomingProposalEventName):
return "proposal_upcoming"
case string(commontypes.SyncCommitteeSoonEventName):
return "sync"
case string(commontypes.ValidatorReceivedWithdrawalEventName):
return "withdrawal"
case string(commontypes.ValidatorGotSlashedEventName):
return "validator_got_slashed"
case string(commontypes.ValidatorDidSlashEventName):
return "validator_has_slashed"
case string(commontypes.ValidatorGroupEfficiencyEventName):
return "group_efficiency_below"
case string(commontypes.RocketpoolCollateralMinReachedEventName):
return "min_collateral"
case string(commontypes.RocketpoolCollateralMaxReachedEventName):
return "max_collateral"
case string(commontypes.IncomingTransactionEventName):
return "incoming_tx"
case string(commontypes.OutgoingTransactionEventName):
return "outgoing_tx"
case string(commontypes.ERC20TokenTransferEventName):
return "transfer_erc20"
case string(commontypes.ERC721TokenTransferEventName):
return "transfer_erc721"
case string(commontypes.ERC1155TokenTransferEventName):
return "transfer_erc1155"
case string(commontypes.MonitoringMachineOfflineEventName):
return "offline"
case string(commontypes.MonitoringMachineDiskAlmostFullEventName):
return "storage"
case string(commontypes.MonitoringMachineCpuLoadEventName):
return "cpu"
case string(commontypes.MonitoringMachineMemoryUsageEventName):
return "memory"
case string(commontypes.RocketpoolNewClaimRoundStartedEventName):
return "new_reward_round"
case string(commontypes.NetworkGasBelowThresholdEventName):
return "gas_below"
case string(commontypes.NetworkGasAboveThresholdEventName):
return "gas_above"
case string(commontypes.NetworkParticipationRateThresholdEventName):
return "participation_rate"
default:
log.Warn("unknown event type", log.Fields{"event": event}) // if this happens, add the event type to the switch
return event
if name, ok := dbEventToResponse[event]; ok {
return name
}
log.Warn("unknown notification event", log.Fields{"event": event})
return event
}

func mapDashboardNotificationEvents(data []types.NotificationDashboardsTableRow) []types.NotificationDashboardsTableRow {
Expand Down

0 comments on commit bfe90b8

Please sign in to comment.