|
| 1 | +import { Table, Button, notification, Typography, Tooltip, Spin } from 'antd' |
| 2 | +import { usePollingEffect } from '../../utils/usePollingEffect' |
| 3 | +import React, { useState } from 'react' |
| 4 | +import { ColumnType } from 'antd/es/table' |
| 5 | + |
| 6 | +const { Paragraph } = Typography |
| 7 | + |
| 8 | +interface RunningQueryData { |
| 9 | + query: string |
| 10 | + read_rows: number |
| 11 | + read_rows_readable: string |
| 12 | + query_id: string |
| 13 | + total_rows_approx: number |
| 14 | + total_rows_approx_readable: string |
| 15 | + elapsed: number |
| 16 | + memory_usage: string |
| 17 | +} |
| 18 | + |
| 19 | +function KillQueryButton({ queryId }: any) { |
| 20 | + const [isLoading, setIsLoading] = useState(false) |
| 21 | + const [isKilled, setIsKilled] = useState(false) |
| 22 | + |
| 23 | + const killQuery = async () => { |
| 24 | + setIsLoading(true) |
| 25 | + try { |
| 26 | + const res = await fetch(`/api/analyze/${queryId}/kill_query`, { |
| 27 | + method: 'POST', |
| 28 | + headers: { |
| 29 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 30 | + }, |
| 31 | + body: new URLSearchParams({ |
| 32 | + query_id: queryId, |
| 33 | + }), |
| 34 | + }) |
| 35 | + setIsKilled(true) |
| 36 | + setIsLoading(false) |
| 37 | + return await res.json() |
| 38 | + } catch (err) { |
| 39 | + setIsLoading(false) |
| 40 | + notification.error({ |
| 41 | + message: 'Killing query failed', |
| 42 | + }) |
| 43 | + } |
| 44 | + } |
| 45 | + return ( |
| 46 | + <> |
| 47 | + {isKilled ? ( |
| 48 | + <Button disabled>Query killed</Button> |
| 49 | + ) : ( |
| 50 | + <Button danger onClick={killQuery} loading={isLoading}> |
| 51 | + Kill query |
| 52 | + </Button> |
| 53 | + )} |
| 54 | + </> |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +export default function Replication() { |
| 59 | + const [runningQueries, setRunningQueries] = useState([]) |
| 60 | + const [loadingRunningQueries, setLoadingRunningQueries] = useState(false) |
| 61 | + |
| 62 | + const columns: ColumnType<RunningQueryData>[] = [ |
| 63 | + { |
| 64 | + title: 'Query', |
| 65 | + dataIndex: 'normalized_query', |
| 66 | + key: 'query', |
| 67 | + render: (_: any, item) => { |
| 68 | + let index = 0 |
| 69 | + return ( |
| 70 | + <Paragraph |
| 71 | + style={{ maxWidth: '100%', fontFamily: 'monospace' }} |
| 72 | + ellipsis={{ |
| 73 | + rows: 2, |
| 74 | + expandable: true, |
| 75 | + }} |
| 76 | + > |
| 77 | + {item.query.replace(/(\?)/g, () => { |
| 78 | + index = index + 1 |
| 79 | + return '$' + index |
| 80 | + })} |
| 81 | + </Paragraph> |
| 82 | + ) |
| 83 | + }, |
| 84 | + }, |
| 85 | + { title: 'User', dataIndex: 'user' }, |
| 86 | + { title: 'Elapsed time', dataIndex: 'elapsed' }, |
| 87 | + { |
| 88 | + title: 'Rows read', |
| 89 | + dataIndex: 'read_rows', |
| 90 | + render: (_: any, item) => ( |
| 91 | + <Tooltip title={`~${item.read_rows}/${item.total_rows_approx}`}> |
| 92 | + ~{item.read_rows_readable}/{item.total_rows_approx_readable} |
| 93 | + </Tooltip> |
| 94 | + ), |
| 95 | + }, |
| 96 | + { title: 'Memory Usage', dataIndex: 'memory_usage' }, |
| 97 | + { |
| 98 | + title: 'Actions', |
| 99 | + render: (_: any, item) => <KillQueryButton queryId={item.query_id} />, |
| 100 | + }, |
| 101 | + ] |
| 102 | + |
| 103 | + usePollingEffect( |
| 104 | + async () => { |
| 105 | + setLoadingRunningQueries(true) |
| 106 | + const res = await fetch('/api/analyze/running_queries') |
| 107 | + const resJson = await res.json() |
| 108 | + setRunningQueries(resJson) |
| 109 | + setLoadingRunningQueries(false) |
| 110 | + }, |
| 111 | + [], |
| 112 | + { interval: 5000 } |
| 113 | + ) |
| 114 | + |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <h1 style={{ textAlign: 'left' }}>Running queries {loadingRunningQueries ? <Spin /> : null}</h1> |
| 118 | + <br /> |
| 119 | + <Table |
| 120 | + columns={columns} |
| 121 | + dataSource={runningQueries} |
| 122 | + loading={runningQueries.length == 0 && loadingRunningQueries} |
| 123 | + /> |
| 124 | + </> |
| 125 | + ) |
| 126 | +} |
0 commit comments