Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add collection retention days (#29469)

Jordi Enricf3b4f658a08e
diff --git a/apps/studio/components/interfaces/DataWarehouse/CreateWarehouseCollection.tsx b/apps/studio/components/interfaces/DataWarehouse/CreateWarehouseCollection.tsx
index 86886d0f2e8..fba32982b9d 100644
--- a/apps/studio/components/interfaces/DataWarehouse/CreateWarehouseCollection.tsx
+++ b/apps/studio/components/interfaces/DataWarehouse/CreateWarehouseCollection.tsx
@@ -1,6 +1,6 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { PlusIcon } from 'lucide-react'
+import { Info, PlusIcon } from 'lucide-react'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
@@ -13,13 +13,29 @@ import { useParams } from 'common'
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
import { useCreateCollection } from 'data/analytics/warehouse-collections-create-mutation'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
-import { Button, FormControl_Shadcn_, FormField_Shadcn_, Form_Shadcn_, Modal } from 'ui'
+import {
+ Admonition,
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ Button,
+ FormControl_Shadcn_,
+ FormField_Shadcn_,
+ Form_Shadcn_,
+ Modal,
+ WarningIcon,
+} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
+import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan'
+import { Alert } from '@ui/components/shadcn/ui/alert'
+import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'
export const CreateWarehouseCollectionModal = () => {
const [isOpen, setIsOpen] = useState(false)
const router = useRouter()
const { ref } = useParams()
+ const { plan, isLoading: planLoading } = useCurrentOrgPlan()
+ const currentOrg = useSelectedOrganization()
const canCreateCollection = useCheckPermissions(PermissionAction.ANALYTICS_WRITE, 'logflare')
@@ -34,12 +50,27 @@ export const CreateWarehouseCollectionModal = () => {
},
})
+ function getMaxRetentionDays() {
+ if (planLoading) return 1
+ if (plan?.id === 'free') return 3
+ else return 90
+ }
const FormSchema = z.object({
name: z.string().min(1),
+ retention_days: z.coerce
+ .number()
+ .min(1)
+ .max(getMaxRetentionDays())
+ .multipleOf(1)
+ .positive()
+ .int(),
})
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
+ defaultValues: {
+ retention_days: getMaxRetentionDays(),
+ },
})
useEffect(() => {
@@ -56,9 +87,27 @@ export const CreateWarehouseCollectionModal = () => {
createCollection({
projectRef: ref,
name: vals.name,
+ retention_days: vals.retention_days,
})
})
+ const formatDays = (days: number) => {
+ if (days === 1) return `1 day`
+ return `${days} days`
+ }
+ const retentionDescription = (days: number) => {
+ const maxRetentionDays = getMaxRetentionDays()
+ if (planLoading) return ''
+ if (!Boolean(days) || days < 1)
+ return <>Your plan allows for a maximum of {formatDays(maxRetentionDays)}.</>
+ return (
+ <>
+ Your logs will be removed after {formatDays(days)}. <br />
+ Your plan allows for a maximum of {formatDays(maxRetentionDays)}.
+ </>
+ )
+ }
+
return (
<>
<ButtonTooltip
@@ -79,7 +128,7 @@ export const CreateWarehouseCollectionModal = () => {
<Modal
size="medium"
onCancel={() => setIsOpen(!isOpen)}
- header="Create an event collection"
+ header="Create collection"
visible={isOpen}
hideFooter
>
@@ -91,22 +140,66 @@ export const CreateWarehouseCollectionModal = () => {
analytics system. You can use SQL to analyze this data without affecting the
performance of your main database operations.
</p>
-
- <FormField_Shadcn_
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItemLayout label="Collection name" layout="horizontal">
- <FormControl_Shadcn_>
- <Input placeholder="Events" {...field} />
- </FormControl_Shadcn_>
- </FormItemLayout>
- )}
- />
-
+ <div className="space-y-3">
+ <FormField_Shadcn_
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItemLayout label="Collection name" layout="horizontal">
+ <FormControl_Shadcn_>
+ <Input autoFocus placeholder="Events" {...field} />
+ </FormControl_Shadcn_>
+ </FormItemLayout>
+ )}
+ />
+ <FormField_Shadcn_
+ control={form.control}
+ name="retention_days"
+ render={({ field }) => (
+ <FormItemLayout
+ label="Retention"
+ layout="horizontal"
+ description={retentionDescription(Number(field.value))}
+ >
+ <FormControl_Shadcn_>
+ <div className="relative flex items-center gap-2">
+ <Input type="number" {...field} />
+ <span className="absolute right-3 text-foreground-lighter text-sm pointer-events-none">
+ days
+ </span>
+ </div>
+ </FormControl_Shadcn_>
+ </FormItemLayout>
+ )}
+ />
+ </div>
<FormMessage />
</Modal.Content>
+ {plan?.id === 'free' && (
+ <>
+ <Modal.Separator />
+ <Admonition
+ className="border-none bg-transparent"
+ type="default"
+ title="Upgrade your plan to increase retention"
+ description={
+ <div>
+ Upgrade to the pro plan to increase retention to 90 days.
+ <Button
+ className="mt-2"
+ type="primary"
+ onClick={() =>
+ router.push(`/org/${currentOrg?.slug}/billing?panel=subscriptionPlan`)
+ }
+ >
+ Upgrade plan
+ </Button>
+ </div>
+ }
+ />
+ </>
+ )}
<Modal.Content className="py-4 border-t flex items-center justify-end gap-2">
<Button size="tiny" type="default" onClick={() => setIsOpen(!isOpen)}>
Cancel
diff --git a/apps/studio/data/analytics/warehouse-collections-create-mutation.ts b/apps/studio/data/analytics/warehouse-collections-create-mutation.ts
index 3aa33a0edb7..f1ec421ae3d 100644
--- a/apps/studio/data/analytics/warehouse-collections-create-mutation.ts
+++ b/apps/studio/data/analytics/warehouse-collections-create-mutation.ts
@@ -7,12 +7,13 @@ import { analyticsKeys } from './keys'
export type CreateCollectionArgs = {
projectRef: string
name: string
+ retention_days: number
}
-export async function createCollection({ projectRef, name }: CreateCollectionArgs) {
+export async function createCollection({ projectRef, name, retention_days }: CreateCollectionArgs) {
const { data, error } = await post('/platform/projects/{ref}/analytics/warehouse/collections', {
params: { path: { ref: projectRef } },
- body: { name },
+ body: { name, retention_days },
} as any)
if (error) handleError(error)