Skip to content

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

Add link to edge function logs for cron history if cron is using edge function (#37531)

Joshen Lim81dda5b37129
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet.tsx
index 4bc501ffe0b..14337533f7a 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet.tsx
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet.tsx
@@ -119,6 +119,7 @@ const sqlFunctionSchema = z.object({
// When editing a cron job, we want to keep the original command as a snippet in case the user wants to manually edit it
snippet: z.string().trim(),
})
+
const sqlSnippetSchema = z.object({
type: z.literal('sql_snippet'),
snippet: z.string().trim().min(1),
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.tsx
index aa26e641095..b8c55cfddde 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.tsx
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.tsx
@@ -193,7 +193,7 @@ export const CronjobsTab = () => {
}}
onScroll={handleScroll}
renderers={{
- renderRow(key, props) {
+ renderRow(_, props) {
return (
<Row
key={props.row.jobid}
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/EdgeFunctionSection.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/EdgeFunctionSection.tsx
index da07ff1a466..4f4148ff620 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/EdgeFunctionSection.tsx
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/EdgeFunctionSection.tsx
@@ -34,8 +34,8 @@ const buildFunctionUrl = (slug: string, projectRef: string, restUrl?: string) =>
}
export const EdgeFunctionSection = ({ form }: HTTPRequestFieldsProps) => {
- const { project: selectedProject } = useProjectContext()
const { ref } = useParams()
+ const { project: selectedProject } = useProjectContext()
const { data: functions, isSuccess, isLoading } = useEdgeFunctionsQuery({ projectRef: ref })
const edgeFunctions = useMemo(() => functions ?? [], [functions])
@@ -52,6 +52,7 @@ export const EdgeFunctionSection = ({ form }: HTTPRequestFieldsProps) => {
form.setValue('values.edgeFunctionName', functionUrl)
}
}, [edgeFunctions, form, isSuccess, selectedProject?.ref, selectedProject?.restUrl])
+
return (
<SheetSection className="flex flex-col gap-6">
<FormField_Shadcn_
diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/PreviousRunsTab.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/PreviousRunsTab.tsx
index b94f6ca2a4c..64f3a709000 100644
--- a/apps/studio/components/interfaces/Integrations/CronJobs/PreviousRunsTab.tsx
+++ b/apps/studio/components/interfaces/Integrations/CronJobs/PreviousRunsTab.tsx
@@ -1,7 +1,7 @@
import { toString as CronToString } from 'cronstrue'
import { CircleCheck, CircleX, List, Loader } from 'lucide-react'
import Link from 'next/link'
-import { UIEvent, useCallback, useEffect, useMemo } from 'react'
+import { UIEvent, useCallback, useMemo } from 'react'
import DataGrid, { Column, Row } from 'react-data-grid'
import { useParams } from 'common'
@@ -11,6 +11,8 @@ import {
CronJobRun,
useCronJobRunsInfiniteQuery,
} from 'data/database-cron-jobs/database-cron-jobs-runs-infinite-query'
+import { useEdgeFunctionsQuery } from 'data/edge-functions/edge-functions-query'
+import dayjs from 'dayjs'
import {
Button,
cn,
@@ -20,8 +22,14 @@ import {
TooltipContent,
TooltipTrigger,
} from 'ui'
+import { TimestampInfo } from 'ui-patterns'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
-import { calculateDuration, formatDate, isSecondsFormat } from './CronJobs.utils'
+import {
+ calculateDuration,
+ formatDate,
+ isSecondsFormat,
+ parseCronJobCommand,
+} from './CronJobs.utils'
import CronJobsEmptyState from './CronJobsEmptyState'
const cronJobColumns = [
@@ -113,6 +121,19 @@ const columns = cronJobColumns.map((col) => {
renderCell: (props) => {
const value = col.value(props.row)
+ if (['start_time', 'end_time'].includes(col.id)) {
+ const formattedValue = dayjs((props.row as any)[(col as any).id]).valueOf()
+ return (
+ <div className="flex items-center">
+ <TimestampInfo
+ utcTimestamp={formattedValue}
+ labelFormat="DD MMM YYYY HH:mm:ss (ZZ)"
+ className="font-mono text-xs"
+ />
+ </div>
+ )
+ }
+
return (
<div
className={cn(
@@ -156,17 +177,17 @@ export const PreviousRunsTab = () => {
connectionString: project?.connectionString,
jobId: jobId,
},
- { enabled: !!jobId, staleTime: 30 }
+ { enabled: !!jobId, staleTime: 30000 }
)
- useEffect(() => {
- // Refetch only the first page
- const timerId = setInterval(() => {
- refetch({ refetchPage: (_page, index) => index === 0 })
- }, 30000)
+ const { data: edgeFunctions = [] } = useEdgeFunctionsQuery({ projectRef: project?.ref })
- return () => clearInterval(timerId)
- }, [refetch])
+ const cronJobRuns = useMemo(() => data?.pages.flatMap((p) => p) || [], [data?.pages])
+ const cronJobValues = parseCronJobCommand(job?.command || '', project?.ref!)
+ const edgeFunction =
+ cronJobValues.type === 'edge_function' ? cronJobValues.edgeFunctionName : undefined
+ const edgeFunctionSlug = edgeFunction?.split('/functions/v1/').pop()
+ const isValidEdgeFunction = edgeFunctions.some((x) => x.slug === edgeFunctionSlug)
const handleScroll = useCallback(
(event: UIEvent<HTMLDivElement>) => {
@@ -178,8 +199,6 @@ export const PreviousRunsTab = () => {
[fetchNextPage, isLoadingCronJobRuns]
)
- const cronJobRuns = useMemo(() => data?.pages.flatMap((p) => p) || [], [data?.pages])
-
return (
<div className="h-full flex flex-col">
<LoadingLine loading={isFetching} />
@@ -251,11 +270,16 @@ export const PreviousRunsTab = () => {
</SimpleCodeBlock>
<div className="bottom-0 left-0 right-0 h-8 bg-gradient-to-t from-background-200 to-transparent absolute " />
</TooltipTrigger>
- <TooltipContent side="bottom" align="center" className="max-w-[400px] text-wrap">
+ <TooltipContent
+ side="bottom"
+ align="center"
+ className="max-w-[400px] text-wrap p-0"
+ >
+ <p className="text-xs font-mono px-2 py-1 border-b bg-surface-100">Command</p>
<SimpleCodeBlock
showCopy={false}
className="sql"
- parentClassName=" [&>div>span]:text-xs bg-alternative-200 !p-2 rounded-md"
+ parentClassName=" [&>div>span]:text-xs bg-alternative-200 !p-3"
>
{job?.command}
</SimpleCodeBlock>
@@ -265,12 +289,29 @@ export const PreviousRunsTab = () => {
<div className="grid gap-y-2">
<h3 className="text-sm">Explore</h3>
- <Button asChild type="outline" icon={<List strokeWidth={1.5} size="14" />}>
- {/* [Terry] need to link to the exact jobid, but not currently supported */}
- <Link target="_blank" href={`/project/${project?.ref}/logs/pgcron-logs/`}>
- View logs
- </Link>
- </Button>
+ <div className="flex items-center gap-x-2">
+ <Button asChild type="outline" icon={<List strokeWidth={1.5} size="14" />}>
+ {/* [Terry] need to link to the exact jobid, but not currently supported */}
+ <Link
+ target="_blank"
+ rel="noopener noreferrer"
+ href={`/project/${project?.ref}/logs/pgcron-logs/`}
+ >
+ View Cron logs
+ </Link>
+ </Button>
+ {isValidEdgeFunction && (
+ <Button asChild type="outline">
+ <Link
+ target="_blank"
+ rel="noopener noreferrer"
+ href={`/project/${project?.ref}/functions/${edgeFunctionSlug}/logs`}
+ >
+ View Edge Function logs
+ </Link>
+ </Button>
+ )}
+ </div>
</div>
</>
)}