Skip to content

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

feat: add api bots report

TzeYiingde385d38c919
diff --git a/studio/components/interfaces/Reports/Reports.constants.ts b/studio/components/interfaces/Reports/Reports.constants.ts
index e334c0485b8..ea6005b890c 100644
--- a/studio/components/interfaces/Reports/Reports.constants.ts
+++ b/studio/components/interfaces/Reports/Reports.constants.ts
@@ -35,7 +35,8 @@ export const DEFAULT_QUERY_PARAMS = {
export const PRESET_CONFIG : Record<Presets, PresetConfig> ={
- [Presets.OVERVIEW]: {
+
+ [Presets.API_OVERVIEW]: {
title: '',
sql: {
statusCodes: `
@@ -95,6 +96,57 @@ ORDER BY
`,
},
},
+ [Presets.API_BOTS]: {
+ title: "API Bots",
+ sql: {
+ userAgents: `
+select
+ h.user_agent as user_agent,
+ cf.asOrganization as request_source,
+ count(f.id) as count
+from
+ edge_logs as f
+ cross join unnest(f.metadata) as m
+ cross join unnest(m.request) as r
+ cross join unnest(r.cf) as cf
+ cross join unnest(r.headers) as h
+group by
+ user_agent,
+ request_source
+order by
+ count desc
+limit 12
+`,
+ botScores:`
+select
+ h.cf_connecting_ip as ip,
+ h.cf_ipcountry as country,
+ h.user_agent as user_agent,
+ r.path as path,
+ b.score as bot_score,
+ b.verifiedBot as bot_verified,
+ count(f.id) as count
+from
+ edge_logs as f
+ cross join unnest(f.metadata) as m
+ cross join unnest(m.request) as r
+ cross join unnest(r.cf) as cf
+ cross join unnest(r.headers) as h
+ cross join unnest(cf.botManagement) as b
+group by
+ ip,
+ country,
+ path,
+ user_agent,
+ bot_score,
+ bot_verified
+order by
+ bot_score desc
+limit 20
+
+ `
+ }
+ }
}
export const DATETIME_FORMAT = 'MMM D, ha'
diff --git a/studio/components/interfaces/Reports/Reports.types.ts b/studio/components/interfaces/Reports/Reports.types.ts
index 3679feeb5ca..359b9bd7569 100644
--- a/studio/components/interfaces/Reports/Reports.types.ts
+++ b/studio/components/interfaces/Reports/Reports.types.ts
@@ -1,5 +1,6 @@
export enum Presets {
- OVERVIEW = 'overview',
+ API_OVERVIEW = 'overview',
+ API_BOTS="api_bots"
}
export interface PresetConfig {
diff --git a/studio/components/interfaces/Reports/renderers/ApiBotsRenderers.tsx b/studio/components/interfaces/Reports/renderers/ApiBotsRenderers.tsx
new file mode 100644
index 00000000000..22f47142c32
--- /dev/null
+++ b/studio/components/interfaces/Reports/renderers/ApiBotsRenderers.tsx
@@ -0,0 +1,90 @@
+import Table from 'components/to-be-cleaned/Table'
+import { ReportWidgetProps } from '../ReportWidget'
+
+export const renderUserAgents = (
+ props: ReportWidgetProps<{
+ user_agent: string
+ request_source: string
+ count: number
+ }>
+) => {
+ return (
+ <Table
+ containerClassName="max-h-72 w-full overflow-y-auto"
+ className="relative rounded border border-scale-600"
+ head={
+ <>
+ <Table.th className="sticky top-0 z-10">User Agents</Table.th>
+ <Table.th className="sticky top-0 z-10">Request Source</Table.th>
+ <Table.th className="sticky top-0 z-10">Count</Table.th>
+ </>
+ }
+ body={
+ <>
+ {props.data.map((row, index) => {
+ return (
+ <Table.tr key={index}>
+ <Table.td className="max-w-sm lg:max-w-lg" style={{ padding: '0.3rem' }}>
+ {row.user_agent}
+ </Table.td>
+ <Table.td>{row.request_source}</Table.td>
+ <Table.td style={{ padding: '0.5rem' }} className="align-top text-xs">
+ {row.count}
+ </Table.td>
+ </Table.tr>
+ )
+ })}
+ </>
+ }
+ />
+ )
+}
+
+export const renderBotScores = (
+ props: ReportWidgetProps<{
+ ip: string
+ country: string
+ user_agent: string
+ path: string
+ bot_score: number
+ bot_verified: boolean
+ count: number
+ }>
+) => {
+ return (
+ <Table
+ containerClassName="max-h-72 w-full overflow-y-auto"
+ className="relative rounded border border-scale-600"
+ head={
+ <>
+ <Table.th className="sticky top-0 z-10">IP (Country)</Table.th>
+ <Table.th className="sticky top-0 z-10">Request Path</Table.th>
+ <Table.th className="sticky top-0 z-10">User Agent</Table.th>
+ <Table.th className="sticky top-0 z-10">Bot Score</Table.th>
+ <Table.th className="sticky top-0 z-10">Is Verified Bot</Table.th>
+ <Table.th className="sticky top-0 z-10">Count</Table.th>
+ </>
+ }
+ body={
+ <>
+ {props.data.map((row, index) => {
+ return (
+ <Table.tr key={index}>
+ <Table.td className="max-w-sm lg:max-w-lg" style={{ padding: '0.3rem' }}>
+ {row.ip} ({row.country})
+ </Table.td>
+ <Table.td>{row.path}</Table.td>
+ <Table.td className="max-w-xs truncate">{row.user_agent}</Table.td>
+ <Table.td>{row.bot_score}</Table.td>
+ <Table.td>{row.bot_verified ? 'Yes' : 'No'}</Table.td>
+ <Table.td style={{ padding: '0.5rem' }} className="align-top text-xs">
+ {row.count}
+ </Table.td>
+ </Table.tr>
+ )
+ })}
+ </>
+ }
+ />
+ )
+}
\ No newline at end of file
diff --git a/studio/components/layouts/ReportsLayout/ReportsMenu.utils.ts b/studio/components/layouts/ReportsLayout/ReportsMenu.utils.ts
index 49c75e4afa9..427476af448 100644
--- a/studio/components/layouts/ReportsLayout/ReportsMenu.utils.ts
+++ b/studio/components/layouts/ReportsLayout/ReportsMenu.utils.ts
@@ -8,7 +8,7 @@ export const generateReportsMenu = (project?: Project): ProductMenuGroup[] => {
return [
{
- title: 'Reports',
+ title: '',
items: [
{
name: 'Custom reports',
@@ -16,14 +16,23 @@ export const generateReportsMenu = (project?: Project): ProductMenuGroup[] => {
url: `/project/${ref}/reports`,
items: [],
},
+ ],
+ },
+ {
+ items: [
...(productReports
? [
{
- name: 'API',
+ name: 'API Overview',
key: 'api-overview',
url: `/project/${ref}/reports/api-overview`,
- items: []
+ items: [],
},
+ {
+ name: 'API Bots',
+ key: 'api-bots',
+ url: `/project/${ref}/reports/api-bots`,
+ items: [],
},
]
: []),
diff --git a/studio/components/ui/ProductMenu/ProductMenuGroup.tsx b/studio/components/ui/ProductMenu/ProductMenuGroup.tsx
new file mode 100644
index 00000000000..96f4ea20ef0
--- /dev/null
+++ b/studio/components/ui/ProductMenu/ProductMenuGroup.tsx
@@ -0,0 +1,3 @@
+const ProductMenuGroup = ()=>{
+
+}
\ No newline at end of file
diff --git a/studio/pages/project/[ref]/reports/api-bots.tsx b/studio/pages/project/[ref]/reports/api-bots.tsx
new file mode 100644
index 00000000000..40f518ae010
--- /dev/null
+++ b/studio/pages/project/[ref]/reports/api-bots.tsx
@@ -0,0 +1,51 @@
+import { observer } from 'mobx-react-lite'
+import { useRouter } from 'next/router'
+
+import { NextPageWithLayout } from 'types'
+import { Presets } from 'components/interfaces/Reports/Reports.types'
+import ReportsLayout from 'components/layouts/ReportsLayout/ReportsLayout'
+import { hooksFactory, usePresetReport } from 'components/interfaces/Reports/Reports.utils'
+import { PRESET_CONFIG } from 'components/interfaces/Reports/Reports.constants'
+import { renderBotScores, renderUserAgents } from 'components/interfaces/Reports/renderers/ApiBotsRenderers'
+import ReportWidget from 'components/interfaces/Reports/ReportWidget'
+
+export const ApiBotsPage: NextPageWithLayout = () => {
+ const router = useRouter()
+ const { ref } = router.query
+ const config = PRESET_CONFIG[Presets.API_BOTS]
+ const hooks = hooksFactory(ref as string, config)
+ const [userAgents, userAgentsHandlers] = hooks.userAgents()
+ const [botScores, botScoresHandlers] = hooks.botScores()
+ const { isLoading, Layout } = usePresetReport(
+ [userAgents, botScores],
+ [userAgentsHandlers, botScoresHandlers]
+ )
+ return (
+ <Layout title={config.title}>
+ <div className="grid lg:grid-cols-4">
+ <ReportWidget
+ isLoading={isLoading}
+ params={userAgents.params}
+ className="col-span-4 col-start-1"
+ title="Top User Agents"
+ description="The types of user agents using the API and their organization source."
+ data={userAgents.logData}
+ renderer={renderUserAgents}
+ />
+ <ReportWidget
+ isLoading={isLoading}
+ params={botScores.params}
+ className="col-span-4 col-start-1"
+ title="Bot-like API Requests"
+ description="Suspicious bot-like requests, as flagged by Cloudflare"
+ data={botScores.logData}
+ renderer={renderBotScores}
+ />
+ </div>
+ </Layout>
+ )
+}
+
+ApiBotsPage.getLayout = (page) => <ReportsLayout>{page}</ReportsLayout>
+
+export default observer(ApiBotsPage)
diff --git a/studio/pages/project/[ref]/reports/api-overview.tsx b/studio/pages/project/[ref]/reports/api-overview.tsx
index 40ed529d425..cd211996a34 100644
--- a/studio/pages/project/[ref]/reports/api-overview.tsx
+++ b/studio/pages/project/[ref]/reports/api-overview.tsx
@@ -16,7 +16,7 @@ import {
export const ApiOverviewPage: NextPageWithLayout = () => {
const router = useRouter()
const { ref } = router.query
- const config = PRESET_CONFIG[Presets.OVERVIEW]
+ const config = PRESET_CONFIG[Presets.API_OVERVIEW]
const hooks = hooksFactory(ref as string, config)
const [statusCodes, statusCodesHandlers] = hooks.statusCodes()
const [errorRate, errorRateHandlers] = hooks.errorRate()