Skip to content

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

Estimate the estimated count in table editor (#28549)

Joshen Lim534bcb9bd902
diff --git a/apps/studio/components/grid/components/footer/pagination/Pagination.tsx b/apps/studio/components/grid/components/footer/pagination/Pagination.tsx
index 4b2d520ebe2..73a53760b63 100644
--- a/apps/studio/components/grid/components/footer/pagination/Pagination.tsx
+++ b/apps/studio/components/grid/components/footer/pagination/Pagination.tsx
@@ -20,6 +20,7 @@ import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import { useParams } from 'common'
import { useDispatch, useTrackedState } from '../../../store/Store'
import { DropdownControl } from '../../common/DropdownControl'
+import { formatEstimatedCount } from './Pagination.utils'
const rowsPerPageOptions = [
{ value: 100, label: '100 rows' },
@@ -71,6 +72,7 @@ const Pagination = () => {
}
)
+ const count = data?.is_estimate ? formatEstimatedCount(data.count) : data?.count.toLocaleString()
const maxPages = Math.ceil((data?.count ?? 0) / snap.rowsPerPage)
const totalPages = (data?.count ?? 0) > 0 ? maxPages : 1
@@ -193,9 +195,7 @@ const Pagination = () => {
<div className="flex items-center gap-x-2">
<p className="text-xs text-foreground-light">
- {`${data.count.toLocaleString()} ${
- data.count === 0 || data.count > 1 ? `records` : 'record'
- }`}{' '}
+ {`${count} ${data.count === 0 || data.count > 1 ? `records` : 'record'}`}{' '}
{data.is_estimate ? '(estimated)' : ''}
</p>
diff --git a/apps/studio/components/grid/components/footer/pagination/Pagination.utils.test.ts b/apps/studio/components/grid/components/footer/pagination/Pagination.utils.test.ts
new file mode 100644
index 00000000000..dacae3c49be
--- /dev/null
+++ b/apps/studio/components/grid/components/footer/pagination/Pagination.utils.test.ts
@@ -0,0 +1,48 @@
+import { formatEstimatedCount } from './Pagination.utils'
+
+describe('formatEstimatedCount', () => {
+ test('Should estimate thousands correctly (I)', () => {
+ const output = formatEstimatedCount(1530)
+ expect(output).toStrictEqual('1.5K')
+ })
+ test('Should estimate thousands correctly (II)', () => {
+ const output = formatEstimatedCount(15310)
+ expect(output).toStrictEqual('15.3K')
+ })
+ test('Should estimate thousands correctly (III)', () => {
+ const output = formatEstimatedCount(153122)
+ expect(output).toStrictEqual('153.1K')
+ })
+ test('Should estimate millions correctly (I)', () => {
+ const output = formatEstimatedCount(1531021)
+ expect(output).toStrictEqual('1.5M')
+ })
+ test('Should estimate millions correctly (II)', () => {
+ const output = formatEstimatedCount(15310212)
+ expect(output).toStrictEqual('15.3M')
+ })
+ test('Should estimate millions correctly (III)', () => {
+ const output = formatEstimatedCount(153102121)
+ expect(output).toStrictEqual('153.1M')
+ })
+ test('Should estimate billions correctly (I)', () => {
+ const output = formatEstimatedCount(1531021211)
+ expect(output).toStrictEqual('1.5B')
+ })
+ test('Should estimate billions correctly (II)', () => {
+ const output = formatEstimatedCount(15310212112)
+ expect(output).toStrictEqual('15.3B')
+ })
+ test('Should estimate billions correctly (III)', () => {
+ const output = formatEstimatedCount(153102121123)
+ expect(output).toStrictEqual('153.1B')
+ })
+ test('Should estimate trillions correctly (I)', () => {
+ const output = formatEstimatedCount(1531021211232)
+ expect(output).toStrictEqual('1.5T')
+ })
+ test('Should estimate trillions correctly (II) and max out at trillions', () => {
+ const output = formatEstimatedCount(1531021211232222)
+ expect(output).toStrictEqual('1531.0T')
+ })
+})
diff --git a/apps/studio/components/grid/components/footer/pagination/Pagination.utils.ts b/apps/studio/components/grid/components/footer/pagination/Pagination.utils.ts
new file mode 100644
index 00000000000..4280c29cef5
--- /dev/null
+++ b/apps/studio/components/grid/components/footer/pagination/Pagination.utils.ts
@@ -0,0 +1,11 @@
+export const formatEstimatedCount = (value: number) => {
+ const sizes = ['', 'K', 'M', 'B', 'T']
+ if (value === 0) return '0'
+
+ const k = 1000
+ const i = Math.floor(Math.log(value) / Math.log(k))
+
+ const unit = i > 4 ? 'T' : sizes[i]
+
+ return `${(value / Math.pow(k, i > 4 ? 4 : i)).toFixed(1)}${unit}`
+}