|
| 1 | +import { computed } from "vue"; |
| 2 | +import { storeToRefs } from "pinia"; |
| 3 | +import { CapabilityStatus, StatusIndicator } from "@/components/platformcapabilities/types"; |
| 4 | +import { useConnectionsAndStatsStore } from "@/stores/ConnectionsAndStatsStore"; |
| 5 | +import { type CapabilityComposable, type CapabilityStatusToStringMap, useCapabilityBase } from "./BaseCapability"; |
| 6 | + |
| 7 | +const ErrorDescriptions: CapabilityStatusToStringMap = { |
| 8 | + [CapabilityStatus.EndpointsNotConfigured]: "The ServiceControl Error instance is connected but no failed messages have been received yet. This may be because no failures have occurred, or recoverability is not configured.", |
| 9 | + [CapabilityStatus.Available]: "The ServiceControl Error instance is available and has received failed messages.", |
| 10 | +}; |
| 11 | + |
| 12 | +const ErrorHelpButtonText: CapabilityStatusToStringMap = { |
| 13 | + [CapabilityStatus.EndpointsNotConfigured]: "Learn More", |
| 14 | + [CapabilityStatus.Available]: "View Failed Messages", |
| 15 | +}; |
| 16 | + |
| 17 | +const ErrorHelpButtonUrl: CapabilityStatusToStringMap = { |
| 18 | + [CapabilityStatus.EndpointsNotConfigured]: "https://docs.particular.net/nservicebus/recoverability/", |
| 19 | + [CapabilityStatus.Available]: "#/failed-messages", |
| 20 | +}; |
| 21 | + |
| 22 | +enum ErrorIndicatorTooltip { |
| 23 | + InstanceAvailable = "The ServiceControl Error instance is configured and available", |
| 24 | + InstanceUnavailable = "The ServiceControl Error instance is not responding", |
| 25 | + FailedMessagesAvailable = "Failed messages have been received and are available for management", |
| 26 | + FailedMessagesUnavailable = "No failed messages have been received yet", |
| 27 | +} |
| 28 | + |
| 29 | +export function useErrorCapability(): CapabilityComposable { |
| 30 | + const { getIconForStatus, getDescriptionForStatus, getHelpButtonTextForStatus, getHelpButtonUrlForStatus, createIndicator } = useCapabilityBase(); |
| 31 | + |
| 32 | + // This tells us the connection state to the ServiceControl Error instance |
| 33 | + // and the failed message count. Auto refreshed every 5 seconds. |
| 34 | + const connectionsStore = useConnectionsAndStatsStore(); |
| 35 | + const connectionState = connectionsStore.connectionState; |
| 36 | + const { failedMessageCount } = storeToRefs(connectionsStore); |
| 37 | + |
| 38 | + // Determine if there are any failed messages |
| 39 | + const hasFailedMessages = computed(() => failedMessageCount.value > 0); |
| 40 | + |
| 41 | + // Determine overall error status |
| 42 | + const errorStatus = computed(() => { |
| 43 | + const connectionSuccessful = connectionState.connected && !connectionState.unableToConnect; |
| 44 | + |
| 45 | + // 1. Check if we are connected to the error instance |
| 46 | + if (!connectionSuccessful) { |
| 47 | + return CapabilityStatus.Unavailable; |
| 48 | + } |
| 49 | + |
| 50 | + // 2. Check if there are any failed messages |
| 51 | + if (!hasFailedMessages.value) { |
| 52 | + return CapabilityStatus.EndpointsNotConfigured; |
| 53 | + } |
| 54 | + |
| 55 | + // 3. If connected and has failed messages, the error instance is fully available |
| 56 | + return CapabilityStatus.Available; |
| 57 | + }); |
| 58 | + |
| 59 | + // Icon based on status |
| 60 | + const errorIcon = computed(() => getIconForStatus(errorStatus.value)); |
| 61 | + |
| 62 | + // Determine description based on status |
| 63 | + const errorDescription = computed(() => getDescriptionForStatus(errorStatus.value, ErrorDescriptions)); |
| 64 | + |
| 65 | + // Determine help button text based on status |
| 66 | + const errorHelpButtonText = computed(() => getHelpButtonTextForStatus(errorStatus.value, ErrorHelpButtonText)); |
| 67 | + |
| 68 | + // Determine help button URL based on status |
| 69 | + const errorHelpButtonUrl = computed(() => getHelpButtonUrlForStatus(errorStatus.value, ErrorHelpButtonUrl)); |
| 70 | + |
| 71 | + // Determine indicators |
| 72 | + const errorIndicators = computed(() => { |
| 73 | + const indicators: StatusIndicator[] = []; |
| 74 | + |
| 75 | + const connectionSuccessful = connectionState.connected && !connectionState.unableToConnect; |
| 76 | + const instanceTooltip = connectionSuccessful ? ErrorIndicatorTooltip.InstanceAvailable : ErrorIndicatorTooltip.InstanceUnavailable; |
| 77 | + |
| 78 | + indicators.push(createIndicator("Instance", connectionSuccessful ? CapabilityStatus.Available : CapabilityStatus.Unavailable, instanceTooltip)); |
| 79 | + |
| 80 | + // Only show failed messages indicator if instance is connected |
| 81 | + if (connectionSuccessful) { |
| 82 | + const messagesIndicatorTooltip = hasFailedMessages.value ? ErrorIndicatorTooltip.FailedMessagesAvailable : ErrorIndicatorTooltip.FailedMessagesUnavailable; |
| 83 | + indicators.push(createIndicator("Failed Messages", hasFailedMessages.value ? CapabilityStatus.Available : CapabilityStatus.EndpointsNotConfigured, messagesIndicatorTooltip)); |
| 84 | + } |
| 85 | + |
| 86 | + return indicators; |
| 87 | + }); |
| 88 | + |
| 89 | + // Loading state - error is loading if we haven't attempted connection yet |
| 90 | + const isLoading = computed(() => !connectionState.connected && !connectionState.unableToConnect && connectionState.connecting); |
| 91 | + |
| 92 | + return { |
| 93 | + status: errorStatus, |
| 94 | + icon: errorIcon, |
| 95 | + description: errorDescription, |
| 96 | + indicators: errorIndicators, |
| 97 | + isLoading, |
| 98 | + helpButtonText: errorHelpButtonText, |
| 99 | + helpButtonUrl: errorHelpButtonUrl, |
| 100 | + }; |
| 101 | +} |
0 commit comments