-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Show /status on instances table #1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stage
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { Badge } from '@/components/ui/badge'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; | ||
| import { useInstanceClientIdParams } from '@/config/useInstanceClient'; | ||
| import { useOrganizationClusterInstancePermissions } from '@/hooks/usePermissions'; | ||
| import { Instance } from '@/integrations/api/api.patch'; | ||
| import { getStatusQueryOptions } from '@/integrations/api/instance/status/getStatus'; | ||
| import { useSetStatus } from '@/integrations/api/instance/status/setStatus'; | ||
| import { getOperationsUrlForInstance } from '@/lib/urls/getOperationsUrlForInstance'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { LoaderCircleIcon, ShieldCheckIcon, ShieldXIcon } from 'lucide-react'; | ||
| import { useEffect, useMemo, useState } from 'react'; | ||
|
|
||
| const DEFAULT_200_MSG = 'Status endpoint is reporting for duty.'; | ||
| const DEFAULT_404_MSG = 'Status endpoint is reporting downtime.'; | ||
|
|
||
| export function InstanceStatusCell( | ||
| { instance }: { readonly instance: Instance }, | ||
| ) { | ||
| const operationsUrl = useMemo(() => getOperationsUrlForInstance(instance), [instance]); | ||
| const instanceParams = useInstanceClientIdParams({ operationsUrl, instanceId: instance.id }); | ||
| const { update: canManage } = useOrganizationClusterInstancePermissions(); | ||
| const { mutate: setStatus, isPending: isSettingStatus } = useSetStatus(); | ||
|
|
||
| // We want to spread the initial requests across 5 seconds. | ||
| const [randomOffset] = useState(() => Math.floor(Math.random() * 5_000)); | ||
| const [ready, setReady] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| const timer = setTimeout(() => setReady(true), randomOffset); | ||
| return () => clearTimeout(timer); | ||
| }, [randomOffset]); | ||
|
|
||
| const { data: statusResponse, isLoading, isFetching } = useQuery(getStatusQueryOptions(instanceParams, ready)); | ||
|
|
||
| const isAvailable = statusResponse?.systemStatus?.[0]?.status === 'Available'; | ||
| const isUnavailable = statusResponse?.systemStatus?.[0]?.status === 'Unavailable'; | ||
| const statusMessage = typeof statusResponse?.data === 'string' | ||
| ? statusResponse.data | ||
| : (isAvailable ? DEFAULT_200_MSG : (isUnavailable ? DEFAULT_404_MSG : 'Unknown status')); | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-2"> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <div className="flex items-center"> | ||
| {isLoading || !ready || (isFetching && !statusResponse) | ||
| ? <LoaderCircleIcon className="animate-spin size-5 text-muted-foreground" /> | ||
| : ( | ||
| <Badge | ||
| variant={isAvailable ? 'success' : isUnavailable ? 'destructive' : 'default'} | ||
| className="size-4 rounded-full p-0" | ||
| > | ||
| <span className="sr-only">{isAvailable ? 'Online' : 'Offline'}</span> | ||
| </Badge> | ||
| )} | ||
| </div> | ||
| </TooltipTrigger> | ||
| <TooltipContent> | ||
| {isFetching && statusResponse ? 'Refreshing... ' : ''} | ||
| {statusMessage} | ||
| </TooltipContent> | ||
| </Tooltip> | ||
|
|
||
| {canManage && ( | ||
| <div className="flex gap-1"> | ||
| {isAvailable && ( | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="destructiveGhost" | ||
| size="icon" | ||
| className="size-7" | ||
| onClick={() => setStatus({ ...instanceParams, id: 'availability', status: 'Unavailable' })} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how we set it to be unavailable.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await server.operation({ operation: 'set_status', id: 'availability', status: 'Unavailable' }); Yeah looks right to me. |
||
| disabled={isSettingStatus} | ||
| > | ||
| {isSettingStatus | ||
| ? <LoaderCircleIcon className="animate-spin size-4" /> | ||
| : <ShieldXIcon className="size-4" />} | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent>Bring out of rotation</TooltipContent> | ||
| </Tooltip> | ||
| )} | ||
| {isUnavailable && ( | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon" | ||
| className="size-7" | ||
| onClick={() => setStatus({ ...instanceParams, id: 'availability', status: 'Available' })} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this is how we set it to be available.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that looks right. await server.operation({ operation: 'set_status', id: 'availability', status: 'Available' });
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, thanks! Experimentally it works too. :) |
||
| disabled={isSettingStatus} | ||
| > | ||
| {isSettingStatus | ||
| ? <LoaderCircleIcon className="animate-spin size-4" /> | ||
| : <ShieldCheckIcon className="size-4" />} | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent>Bring back into rotation</TooltipContent> | ||
| </Tooltip> | ||
| )} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { InstanceClientIdConfig } from '@/config/instanceClientConfig'; | ||
| import { SystemStatus } from '@/integrations/api/instance/status/getStatus'; | ||
| import { ReplicatedResponse } from '@/integrations/api/replication'; | ||
| import { queryClient } from '@/react-query/queryClient'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
|
|
||
| interface SetConfigurationParams extends Pick<SystemStatus, 'id' | 'status'>, InstanceClientIdConfig { | ||
| } | ||
|
|
||
| async function setStatus({ | ||
| instanceClient, | ||
| id, | ||
| status, | ||
| }: SetConfigurationParams): Promise<ReplicatedResponse> { | ||
| const { data } = await instanceClient.post('/', { | ||
| operation: 'set_status', | ||
| id, | ||
| status, | ||
| }); | ||
| return data; | ||
| } | ||
|
|
||
| export function useSetStatus() { | ||
| return useMutation({ | ||
| mutationFn: setStatus, | ||
| onSuccess: (_data, variables) => queryClient.invalidateQueries({ queryKey: [variables.entityId, 'get_status'] }), | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Instance } from '@/integrations/api/api.patch'; | ||
|
|
||
| export function getRestUrlForInstance( | ||
| instance: Pick<Instance, 'instanceFqdn'>, | ||
| ): string { | ||
| let fqdn = instance.instanceFqdn; | ||
| if (!fqdn.match(/^https?:\/\//i)) { | ||
| fqdn = `https://${fqdn}`; | ||
| } | ||
| const url = new URL(fqdn); | ||
| return url.toString(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how we check if the instance is available or not, based on the first systemStatus.