mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
* init * hovercard * adds button to install index advisor * hover card now now insert indexes * update * moved hook * align alert dialog to design syste, * Update index-advisor.utils.ts * shows all index statements now * Update query-performance.tsx * Some refactors * Clean up * Fix * One last nit refactor --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
125 lines
4.4 KiB
TypeScript
125 lines
4.4 KiB
TypeScript
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
|
|
import { useIsIndexAdvisorAvailable } from 'components/interfaces/QueryPerformance/hooks/useIsIndexAdvisorAvailable'
|
|
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
|
|
import { useDatabaseExtensionEnableMutation } from 'data/database-extensions/database-extension-enable-mutation'
|
|
import { useDatabaseExtensionsQuery } from 'data/database-extensions/database-extensions-query'
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
AlertDialogTrigger,
|
|
Badge,
|
|
Button,
|
|
InfoIcon,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
import { getIndexAdvisorExtensions } from './index-advisor.utils'
|
|
|
|
export const EnableIndexAdvisorButton = () => {
|
|
const { project } = useProjectContext()
|
|
const isAdvisorAvailable = useIsIndexAdvisorAvailable()
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false)
|
|
|
|
const { data: extensions } = useDatabaseExtensionsQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
})
|
|
const { hypopg, indexAdvisor } = getIndexAdvisorExtensions(extensions)
|
|
|
|
const { mutateAsync: enableExtension, isLoading: isEnablingExtension } =
|
|
useDatabaseExtensionEnableMutation()
|
|
|
|
const onEnableIndexAdvisor = async () => {
|
|
if (project === undefined) return toast.error('Project is required')
|
|
|
|
try {
|
|
// Enable hypopg extension if not already installed
|
|
if (hypopg?.installed_version === null) {
|
|
await enableExtension({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
name: hypopg.name,
|
|
schema: hypopg?.schema ?? 'extensions',
|
|
version: hypopg.default_version,
|
|
})
|
|
}
|
|
|
|
// Enable index_advisor extension if not already installed
|
|
if (indexAdvisor?.installed_version === null) {
|
|
await enableExtension({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
name: indexAdvisor.name,
|
|
schema: indexAdvisor?.schema ?? 'extensions',
|
|
version: indexAdvisor.default_version,
|
|
})
|
|
}
|
|
toast.success('Successfully enabled Index Advisor!')
|
|
setIsDialogOpen(false)
|
|
} catch (error: any) {
|
|
toast.error(`Failed to enable Index Advisor: ${error.message}`)
|
|
}
|
|
}
|
|
|
|
if (isAdvisorAvailable) return null
|
|
|
|
return (
|
|
<AlertDialog open={isDialogOpen} onOpenChange={() => setIsDialogOpen(!isDialogOpen)}>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<AlertDialogTrigger asChild>
|
|
<Button type="outline" className={`rounded-full`} icon={<InfoIcon />}>
|
|
Enable Index Advisor
|
|
</Button>
|
|
</AlertDialogTrigger>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="top">Recommends indexes to improve query performance</TooltipContent>
|
|
</Tooltip>
|
|
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Enable Index Advisor</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Index Advisor is a tool that helps you identify and simulate indexes that can improve
|
|
query performance. To use Index Advisor, you need to enable the following Postgres
|
|
extensions:
|
|
<ul className="list-disc pl-6 py-4 flex flex-col gap-2">
|
|
<li>
|
|
<Badge className="font-mono text-foreground">index_advisor</Badge> - Recommends
|
|
database indexes
|
|
</li>
|
|
<li>
|
|
<Badge className="font-mono text-foreground">hypopg</Badge> - For hypothetical
|
|
indexes simulation
|
|
</li>
|
|
</ul>
|
|
These extensions help identify and simulate indexes that can improve query performance
|
|
without having to create actual indexes.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={(e) => {
|
|
e.preventDefault()
|
|
onEnableIndexAdvisor()
|
|
}}
|
|
disabled={isEnablingExtension}
|
|
>
|
|
{isEnablingExtension ? 'Enabling...' : 'Enable Extensions'}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
)
|
|
}
|