From bfe90b8ea3d20c5b06e5d3a3fe0063605bc8dbd0 Mon Sep 17 00:00:00 2001 From: Lucca Dukic <109136188+LuccaBitfly@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:10:29 +0100 Subject: [PATCH] refactor: replace switch with map See: BEDS-635 --- backend/pkg/api/handlers/handler_service.go | 89 ++++++++------------- 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/backend/pkg/api/handlers/handler_service.go b/backend/pkg/api/handlers/handler_service.go index d7ec9fd4d..7c89dbd1a 100644 --- a/backend/pkg/api/handlers/handler_service.go +++ b/backend/pkg/api/handlers/handler_service.go @@ -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 {