Skip to content

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

chore: Charts for functions added in, although still rough. Warning Banner added in for alpha message

Jonathan Summers-Muir5dfcce6071a3
diff --git a/studio/components/to-be-cleaned/Charts/ChartHandler.tsx b/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
index f6f76315dba..e48042078d3 100644
--- a/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
+++ b/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
@@ -31,6 +31,8 @@ interface Props {
hideChartType?: boolean
data?: ChartData
isLoading?: boolean
+ format?: string
+ highlightedValue?: string | number
onBarClick?: (v: any) => void
}
@@ -57,6 +59,8 @@ const ChartHandler: FC<Props> = ({
hideChartType = false,
data,
isLoading,
+ format,
+ highlightedValue,
onBarClick,
}) => {
const router = useRouter()
@@ -69,6 +73,8 @@ const ChartHandler: FC<Props> = ({
const chartData = data || fetchedData
const loading = isLoading || fetching
+ console.log('data in ChartHandler', data)
+
useEffect(() => {
let cancel = false
@@ -114,12 +120,13 @@ const ChartHandler: FC<Props> = ({
}
}, [startDate])
- const highlightedValue =
- provider === 'daily-stats'
- ? chartData?.total
- : provider === 'log-stats'
- ? chartData?.totalGrouped?.[attribute]
- : chartData?.totalAverage
+ highlightedValue = highlightedValue
+ ? highlightedValue
+ : provider === 'daily-stats'
+ ? chartData?.total
+ : provider === 'log-stats'
+ ? chartData?.totalGrouped?.[attribute]
+ : chartData?.totalAverage
if (loading) {
return (
@@ -131,6 +138,7 @@ const ChartHandler: FC<Props> = ({
}
if (isUndefined(chartData)) {
+ console.log('i am undefined chart data')
return (
<div className="w-full h-52 flex flex-col space-y-4 items-center justify-center">
<IconAlertCircle className="text-scale-700" />
@@ -160,7 +168,7 @@ const ChartHandler: FC<Props> = ({
data={chartData?.data ?? []}
attribute={attribute}
yAxisLimit={chartData?.yAxisLimit}
- format={chartData?.format}
+ format={format || chartData?.format}
highlightedValue={highlightedValue}
label={label}
customDateFormat={customDateFormat}
@@ -169,7 +177,7 @@ const ChartHandler: FC<Props> = ({
) : (
<AreaChart
data={chartData?.data ?? []}
- format={chartData?.format}
+ format={format || chartData?.format}
attribute={attribute}
yAxisLimit={chartData?.yAxisLimit}
highlightedValue={highlightedValue}
diff --git a/studio/components/ui/WarningBanner.tsx b/studio/components/ui/WarningBanner.tsx
new file mode 100644
index 00000000000..fe5ff13fa9d
--- /dev/null
+++ b/studio/components/ui/WarningBanner.tsx
@@ -0,0 +1,19 @@
+import { IconAlertCircle } from '@supabase/ui'
+
+const WarningBanner = ({ title, description }: { title: string; description: string }) => {
+ return (
+ <div className="bg-yellow-900 px-6 py-4">
+ <div className="flex gap-3">
+ <div className="mt-1 text-yellow-1100 dark:text-yellow-800">
+ <IconAlertCircle size={14} strokeWidth={2} />
+ </div>
+ <div className="flex flex-col lg:flex-row lg:items-center lg:gap-3">
+ <h1 className="text-base text-scale-1200 dark:text-scale-100">{title}</h1>
+ <p className="text-sm text-yellow-1100 dark:text-yellow-800">{description}</p>
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export default WarningBanner
diff --git a/studio/pages/project/[ref]/functions/[id]/details.tsx b/studio/pages/project/[ref]/functions/[id]/details.tsx
index adb4e0939ea..58e68b0eeed 100644
--- a/studio/pages/project/[ref]/functions/[id]/details.tsx
+++ b/studio/pages/project/[ref]/functions/[id]/details.tsx
@@ -96,7 +96,7 @@ const PageLayout = () => {
return (
<FunctionLayout>
- <div className="grid gap-y-12 lg:grid-cols-2 lg:gap-x-16 py-12">
+ <div className="grid gap-y-12 lg:grid-cols-2 lg:gap-x-16">
<div>
<div
className="
diff --git a/studio/pages/project/[ref]/functions/[id]/index.tsx b/studio/pages/project/[ref]/functions/[id]/index.tsx
index 7d6566cf847..52b659b3f6d 100644
--- a/studio/pages/project/[ref]/functions/[id]/index.tsx
+++ b/studio/pages/project/[ref]/functions/[id]/index.tsx
@@ -1,10 +1,215 @@
import { observer } from 'mobx-react-lite'
import { withAuth } from 'hooks'
-import FunctionLayout from './../interfaces/FunctionLayout'
+// import FunctionLayout from './../interfaces/FunctionLayout'
+
+// const PageLayout = () => {
+// return <FunctionLayout>Metrics</FunctionLayout>
+// }
+
+import useSWR from 'swr'
+import dayjs from 'dayjs'
+import Link from 'next/link'
+import { FC, useState } from 'react'
+import { useRouter } from 'next/router'
+import {
+ IconArchive,
+ IconDatabase,
+ IconKey,
+ IconZap,
+ Typography,
+ Button,
+ Dropdown,
+ IconChevronDown,
+} from '@supabase/ui'
+
+import Panel from 'components/to-be-cleaned/Panel'
+import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler'
+import { ProjectUsageMinimal } from 'components/to-be-cleaned/Usage'
+
+import { useFlag } from 'hooks'
+import { get } from 'lib/common/fetch'
+import { API_URL, METRICS, DATE_FORMAT } from 'lib/constants'
+import Table from 'components/to-be-cleaned/Table'
+import StackedAreaChart from 'components/ui/Charts/StackedAreaChart'
+import { USAGE_COLORS } from 'components/ui/Charts/Charts.constants'
+import {
+ EndpointResponse,
+ PathsDatum,
+ StatusCodesDatum,
+} from 'components/interfaces/Home/ChartData.types'
+import FunctionLayout from '../interfaces/FunctionLayout'
+// import { EndpointResponse, PathsDatum, StatusCodesDatum } from ' ChartData.types'
+
+const CHART_INTERVALS = [
+ {
+ key: '5min',
+ label: '5 min',
+ startValue: 1,
+ startUnit: 'hour',
+ format: 'MMM D, h:mma',
+ },
+ { key: '15min', label: '15 min', startValue: 24, startUnit: 'hour', format: 'MMM D, ha' },
+ { key: '1hr', label: '1 hour', startValue: 7, startUnit: 'day', format: 'MMM D' },
+ { key: '1day', label: '1 day', startValue: 7, startUnit: 'day', format: 'MMM D' },
+ { key: '7day', label: '7 days', startValue: 7, startUnit: 'day', format: 'MMM D' },
+]
+
+function calculateAverage(array: any, attribute: string) {
+ if (!array) return ''
+
+ var total = 0
+ var count = 0
+
+ array.forEach(function (item: any, index: number) {
+ const _item = item[attribute]
+ total += _item
+ count++
+ })
+
+ return total / count
+}
const PageLayout = () => {
- return <FunctionLayout>Metrics</FunctionLayout>
+ const [interval, setInterval] = useState<string>('15min')
+ const router = useRouter()
+ const { ref, id } = router.query
+
+ const url = `${API_URL}/projects/${ref}/analytics/endpoints/functions.inv-stats`
+
+ const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]
+ console.log('selectInterval', selectedInterval)
+
+ const { data, error }: any = useSWR(
+ `${url}?interval=${selectedInterval.key}&function_id=${id}`,
+ get
+ )
+ console.log('data', data)
+
+ const startDate = dayjs()
+ .subtract(selectedInterval.startValue, selectedInterval.startUnit)
+ .format(DATE_FORMAT)
+
+ const endDate = dayjs().format(DATE_FORMAT)
+
+ const charts: any = {}
+
+ charts.data = data?.result
+
+ const datetimeFormat = selectedInterval.format || 'MMM D, ha'
+
+ const handleBarClick = (v: any, search: string) => {
+ if (!v || !v.activePayload?.[0]?.payload) return
+ // returns rechart internal tooltip data type
+ const payload = v.activePayload[0].payload
+ const timestamp = payload.timestamp
+ const timestampDigits = String(timestamp).length
+ if (timestampDigits < 16) {
+ // pad unix timestamp with additional 0 and then forward
+ const paddedTimestamp = String(timestamp) + '0'.repeat(16 - timestampDigits)
+ router.push(`/project/${ref}/functions/${id}/logs?te=${paddedTimestamp}`)
+ } else {
+ router.push(`/project/${ref}/functions/${id}/logs?te=${timestamp}`)
+ }
+ }
+
+ console.log('average', data?.result ? calculateAverage(data?.result, 'avg_execution_time') : null)
+
+ return (
+ <FunctionLayout>
+ <div className="space-y-6">
+ <div className="flex flex-row items-center gap-2">
+ <Dropdown
+ side="bottom"
+ align="start"
+ overlay={
+ <Dropdown.RadioGroup value={interval} onChange={setInterval}>
+ {CHART_INTERVALS.map((i) => (
+ <Dropdown.Radio key={i.key} value={i.key}>
+ {i.label}
+ </Dropdown.Radio>
+ ))}
+ </Dropdown.RadioGroup>
+ }
+ >
+ <Button as="span" type="default" iconRight={<IconChevronDown />}>
+ {selectedInterval.label}
+ </Button>
+ </Dropdown>
+
+ <span className="text-scale-1000 text-xs">
+ Statistics for past {selectedInterval.label}
+ </span>
+ </div>
+ <div className="">
+ {startDate && endDate && (
+ <>
+ <div className="grid grid-cols-1 md:gap-4 md:grid-cols-2 lg:grid-cols-2 lg:gap-8">
+ <Panel key="database-chart">
+ <Panel.Content className="space-y-4">
+ <ChartHandler
+ label="Execution time"
+ defaultChartStyle="line"
+ startDate={startDate}
+ endDate={endDate}
+ attribute={'avg_execution_time'}
+ provider="log-stats"
+ // interval="1d"
+ hideChartType
+ customDateFormat={datetimeFormat}
+ data={charts}
+ format="ms"
+ isLoading={!charts.data && !error ? true : false}
+ highlightedValue={calculateAverage(data?.result, 'avg_execution_time')}
+ // onBarClick={(v) => handleBarClick(v, '/rest')}
+ />
+ </Panel.Content>
+ </Panel>
+ <Panel key="auth-chart">
+ <Panel.Content className="space-y-4">
+ <ChartHandler
+ label="Invocations"
+ startDate={startDate}
+ endDate={endDate}
+ attribute={'count'}
+ provider="log-stats"
+ // interval="1d"
+ hideChartType
+ customDateFormat={datetimeFormat}
+ data={charts}
+ isLoading={!charts.data && !error ? true : false}
+ highlightedValue={calculateAverage(data?.result, 'count')}
+ onBarClick={(v) => handleBarClick(v, '/auth')}
+ />
+ </Panel.Content>
+ </Panel>
+ </div>
+ </>
+ )}
+ </div>
+ </div>
+ </FunctionLayout>
+ )
+}
+// export default ProjectUsage
+
+const PanelHeader = (props: any) => {
+ const Tag = props?.href ? Link : 'div'
+ return (
+ <Tag href={props.href}>
+ <div
+ className={
+ 'flex items-center space-x-3 opacity-80 transition ' +
+ (props.href ? 'cursor-pointer hover:opacity-100 hover:text-gray-1200' : '')
+ }
+ >
+ <Typography.Text>{props.icon}</Typography.Text>
+ <span className="flex items-center space-x-1">
+ <h4 className="mb-0 text-lg">{props.title}</h4>
+ </span>
+ </div>
+ </Tag>
+ )
}
export default withAuth(observer(PageLayout))
diff --git a/studio/pages/project/[ref]/functions/index.tsx b/studio/pages/project/[ref]/functions/index.tsx
index 368356d851c..25572396808 100644
--- a/studio/pages/project/[ref]/functions/index.tsx
+++ b/studio/pages/project/[ref]/functions/index.tsx
@@ -180,7 +180,7 @@ const FunctionsList = ({ functions }: { functions: Function[] }) => {
</div>
</div>
- <div
+ {/* <div
className="
col-span-2
bg-scale-300 rounded px-10 py-8 flex flex-col gap-8"
@@ -248,7 +248,7 @@ const FunctionsList = ({ functions }: { functions: Function[] }) => {
</div>
</div>
</div>
- </div>
+ </div> */}
</>
)
}
@@ -257,8 +257,6 @@ const PageLayout = () => {
const router = useRouter()
const { ui, functions } = useStore()
- const [pageState, setPageState] = useState('list')
-
const project = ui.selectedProject
useEffect(() => {
@@ -271,13 +269,6 @@ const PageLayout = () => {
functions.load()
}, [ui.selectedProject])
- const Selection = () => (
- <div>
- <Button onClick={() => setPageState('empty')}>empty list</Button>
- <Button onClick={() => setPageState('list')}>list</Button>
- </div>
- )
-
if (functions.isLoading)
return (
<FunctionsLayout>
@@ -296,7 +287,6 @@ const PageLayout = () => {
return (
<FunctionsLayout>
<FunctionsList functions={functions.list()} />
- <Selection />
</FunctionsLayout>
)
}
diff --git a/studio/pages/project/[ref]/functions/interfaces/FunctionLayout.tsx b/studio/pages/project/[ref]/functions/interfaces/FunctionLayout.tsx
index 40673834c81..cd34b0e700a 100644
--- a/studio/pages/project/[ref]/functions/interfaces/FunctionLayout.tsx
+++ b/studio/pages/project/[ref]/functions/interfaces/FunctionLayout.tsx
@@ -1,12 +1,13 @@
import BaseLayout from 'components/layouts'
import { observer } from 'mobx-react-lite'
import { useStore, withAuth } from 'hooks'
-import { IconCode, IconSlash, Loading, Tabs } from '@supabase/ui'
+import { IconAlertCircle, IconCode, IconSlash, Loading, Tabs } from '@supabase/ui'
import { Tab } from '@headlessui/react'
import FunctionsNav from './FunctionsNav'
import React, { useEffect } from 'react'
import { useRouter } from 'next/router'
import Link from 'next/link'
+import WarningBanner from 'components/ui/WarningBanner'
const PageLayout = ({ children }: { children?: React.ReactNode }) => {
const { functions, ui } = useStore()
@@ -35,6 +36,11 @@ const PageLayout = ({ children }: { children?: React.ReactNode }) => {
return (
<BaseLayout>
+ <WarningBanner
+ title={' Supabase Functions is a pre release version'}
+ description={'We do advise you use Supabase Functions in production at this time'}
+ />
+
<div className="h-full flex flex-col flex-grow py-10">
<div
className="
diff --git a/studio/pages/project/[ref]/functions/interfaces/FunctionsLayout.tsx b/studio/pages/project/[ref]/functions/interfaces/FunctionsLayout.tsx
index 0bf6ba5a3a9..3418f5c409e 100644
--- a/studio/pages/project/[ref]/functions/interfaces/FunctionsLayout.tsx
+++ b/studio/pages/project/[ref]/functions/interfaces/FunctionsLayout.tsx
@@ -6,6 +6,7 @@ import { Tab } from '@headlessui/react'
import FunctionsNav from '../interfaces/FunctionsNav'
import React, { useEffect } from 'react'
import { useRouter } from 'next/router'
+import WarningBanner from 'components/ui/WarningBanner'
const PageLayout = ({ children, centered }: { children?: React.ReactNode; centered?: boolean }) => {
const { functions, ui } = useStore()
@@ -27,6 +28,10 @@ const PageLayout = ({ children, centered }: { children?: React.ReactNode; center
return (
<BaseLayout>
+ <WarningBanner
+ title={' Supabase Functions is a pre release version'}
+ description={'We do advise you use Supabase Functions in production at this time'}
+ />
{centered ? (
<>
<div className="mx-auto max-w-5xl py-32 px-5">
diff --git a/studio/pages/project/[ref]/functions/interfaces/FunctionsNav.tsx b/studio/pages/project/[ref]/functions/interfaces/FunctionsNav.tsx
index 2d50fe9f7ac..6d6982fbd99 100644
--- a/studio/pages/project/[ref]/functions/interfaces/FunctionsNav.tsx
+++ b/studio/pages/project/[ref]/functions/interfaces/FunctionsNav.tsx
@@ -19,7 +19,7 @@ const FunctionsNav = ({ item }) => {
>
<Tabs.Panel id="metrics" label="Metrics" />
<Tabs.Panel id="details" label="Details" />
- <Tabs.Panel id="triggers" label="Triggers" />
+ {/* <Tabs.Panel id="triggers" label="Triggers" /> */}
<Tabs.Panel id="invocations" label="Invocations" />
<Tabs.Panel id="logs" label="Logs" />
</Tabs>