Files
supabase/apps/studio/components/interfaces/Observability/DatabaseConnections/DatabaseConnections.ai.ts
Joshen Lim cf7da58eb3 Add overview section for database connections (#48147)
## Context

Building on top of "Database Connections" - this adds a top summary
section, again from `pg_stat_activity`
<img width="948" height="324" alt="image"
src="https://github.com/user-attachments/assets/f4968193-0a5f-4754-a630-40685b747999"
/>

Each block comes with a tooltip in hopes to educate the significance of
each metric
- Connections: Spread of connections per database role
<img width="313" height="164" alt="image"
src="https://github.com/user-attachments/assets/8ceeab5d-b960-4be3-9a5b-8600bd5cf303"
/>
- Active queries: Rough representative of activity
<img width="350" height="196" alt="image"
src="https://github.com/user-attachments/assets/f9705ff1-a869-409a-86b6-50170a169674"
/>
- Idle in transaction: Important to identify as this indicates locks
(Suggests root cause)
<img width="350" height="196" alt="image"
src="https://github.com/user-attachments/assets/f9705ff1-a869-409a-86b6-50170a169674"
/>
- Blocked queries: Also important to identify stuck queries
<img width="335" height="183" alt="image"
src="https://github.com/user-attachments/assets/57255fb8-24f6-4ddd-aa54-850a77173b5c"
/>
- Longest running query: Might be useful to identify unusually long
queries
- Will be `text-warning` if exceeds 30 seconds for active queries,
`text-destructive` if exceeds 10 seconds for queries idle in transaction
<img width="342" height="119" alt="image"
src="https://github.com/user-attachments/assets/f6783b43-058a-4a32-a40c-0bc64f23d2ce"
/>

"Summarize activity" CTA leverages on the Assistant to give a quick
overview - highlights any potential issues for quick reference
<img width="1918" height="958" alt="image"
src="https://github.com/user-attachments/assets/340121fe-3186-48a5-8023-fbac2a93397a"
/>

## Other changes
- Hides "View running queries" in SQL Editor if `topForPostgres` feature
flag is enabled (since this UI is meant to replace that)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added a Database Connections observability overview with metric cards
(connections, longest-running, active, blocked, idle-in-transaction) and
an interactive “Longest running” PID selector.
* Added a “Summarize activity” AI assistant dropdown that starts a
timestamped, activity-aware summary chat.
* **Improvements**
* Enhanced live activity refresh (including window-focus updates) and
standardized duration warning thresholds for active and
idle-in-transaction sessions.
* Improved hover details for query previews and allowed richer tooltip
content for metric labels.
* **Feature Changes**
  * Gated the “View running queries” bottom panel behind a feature flag.
* **Bug Fixes**
* Refined running-too-long badge and warning styling for
idle-in-transaction cases.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-22 17:25:56 +08:00

36 lines
1.8 KiB
TypeScript

import { type DatabaseActivity } from '@/data/database/activity-query'
export const buildDatabaseConnectionsSummaryPrompt = ({
activities,
timestamp,
}: {
activities: DatabaseActivity[]
timestamp: string
}) => {
const prompt = `
Data from \`pg_stat_activity\` captured at: ${timestamp}
\`\`\`
${JSON.stringify(activities)}
\`\`\`
Summarize the sessions in the above data for a developer who isn't a DBA. Cover, in this order, only the categories that have at least one match:
1. Idle-in-transaction sessions — cite the PID and role; note it's almost always a missing commit/rollback in their app.
2. Blocked sessions — cite the blocked PID and the PID/query blocking it.
3. Active queries running longer than 30 seconds — cite the PID and duration.
Rules:
- Only include a bullet for a category if it has at least one match. Do not mention categories with nothing to report.
- If none of the four apply, respond with a single plain sentence confirming things look healthy — no bullets, no "if you want..." offer, no restating the data.
- Reference every finding by PID so the user can find the row in the table.
- Compute the duration based on the timestamps provided against the captured at timestamp and blocked_by fields from the data
- Rows with status as "active" should be computed using the "query_start" property
- Rows with status as "idle in transaction" or "idle in transaction (aborted) should be computed using the "transaction_start" property
Format:
- Bold every PID (e.g. **PID 511**).
- Display the data for each category in a table format.
- If anything was flagged, end with one short line inviting the user to ask for more detail on a specific PID. If everything is healthy, stop after the health sentence — do not ask for more data.
`
return prompt
}