Skip to content

Commit d3a0ee7

Browse files
authored
fix: derive the calculated storage size in the model, display and sort that field COMPASS-9423 (#7005)
derive the calculated storage size in the model, display and sort that field
1 parent d4ff37f commit d3a0ee7

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

packages/collection-model/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface CollectionProps {
7474
avg_document_size: number | undefined;
7575
storage_size: number | undefined;
7676
free_storage_size: number | undefined;
77+
calculated_storage_size: number | undefined;
7778
index_count: number | undefined;
7879
index_size: number | undefined;
7980
isTimeSeries: boolean;

packages/collection-model/lib/model.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
239239
return getProperties(this);
240240
},
241241
},
242+
calculated_storage_size: {
243+
deps: ['storage_size', 'free_storage_size'],
244+
fn() {
245+
if (
246+
this.storage_size === undefined ||
247+
this.free_storage_size === undefined
248+
) {
249+
return undefined;
250+
}
251+
return this.storage_size - this.free_storage_size;
252+
},
253+
},
242254
},
243255

244256
/**

packages/databases-collections-list/src/collections.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const CollectionsList: React.FunctionComponent<{
106106
{ name: 'name', label: 'Collection Name' },
107107
{ name: 'document_count', label: 'Documents' },
108108
{ name: 'avg_document_size', label: 'Avg. document size' },
109-
{ name: 'storage_size', label: 'Storage size' },
109+
{ name: 'calculated_storage_size', label: 'Storage size' },
110110
{ name: 'index_count', label: 'Indexes' },
111111
{ name: 'index_size', label: 'Total index size' },
112112
]}
@@ -128,11 +128,8 @@ const CollectionsList: React.FunctionComponent<{
128128
{
129129
label: 'Storage size',
130130
value:
131-
coll.storage_size !== undefined &&
132-
coll.free_storage_size !== undefined
133-
? compactBytes(
134-
coll.storage_size - coll.free_storage_size
135-
)
131+
coll.calculated_storage_size !== undefined
132+
? compactBytes(coll.calculated_storage_size)
136133
: 'N/A',
137134
hint:
138135
coll.document_size !== undefined &&
@@ -145,11 +142,8 @@ const CollectionsList: React.FunctionComponent<{
145142
{
146143
label: 'Storage size',
147144
value:
148-
coll.storage_size !== undefined &&
149-
coll.free_storage_size !== undefined
150-
? compactBytes(
151-
coll.storage_size - coll.free_storage_size
152-
)
145+
coll.calculated_storage_size !== undefined
146+
? compactBytes(coll.calculated_storage_size)
153147
: 'N/A',
154148
hint:
155149
coll.document_size !== undefined &&

packages/databases-collections-list/src/index.spec.tsx

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function createDatabase(name) {
3535
};
3636
}
3737

38-
function createCollection(name) {
39-
return {
38+
function createCollection(name, props: any = {}) {
39+
const col = {
4040
_id: name,
4141
name: name,
4242
type: 'collection' as const,
@@ -71,12 +71,19 @@ function createCollection(name) {
7171
free_storage_size: 1000,
7272
index_count: 15,
7373
index_size: 16,
74+
...props,
7475
};
76+
77+
if (col.storage_size !== undefined && col.free_storage_size !== undefined) {
78+
col.calculated_storage_size = col.storage_size - col.free_storage_size;
79+
}
80+
81+
return col;
7582
}
7683

77-
function createTimeSeries(name) {
84+
function createTimeSeries(name, props: any = {}) {
7885
return {
79-
...createCollection(name),
86+
...createCollection(name, props),
8087
type: 'timeseries' as const,
8188
};
8289
}
@@ -89,10 +96,10 @@ const dbs = [
8996
];
9097

9198
const colls = [
92-
createCollection('foo.foo'),
93-
createCollection('bar.bar'),
94-
createCollection('buz.buz'),
95-
createTimeSeries('bat.bat'),
99+
createCollection('foo.foo', { storage_size: 1000, free_storage_size: 1000 }), // 1000
100+
createCollection('bar.bar', { storage_size: 2000, free_storage_size: 500 }), // 1500
101+
createCollection('buz.buz', { storage_size: 3000, free_storage_size: 2000 }), // 1000
102+
createTimeSeries('bat.bat', { storage_size: 4000, free_storage_size: 0 }), // 4000
96103
];
97104

98105
describe('databases and collections list', function () {
@@ -213,6 +220,35 @@ describe('databases and collections list', function () {
213220
expect(clickSpy).to.be.calledWith('bar.bar');
214221
});
215222

223+
it('should sort collections', function () {
224+
renderCollectionsList({
225+
namespace: 'db',
226+
collections: colls,
227+
});
228+
229+
screen
230+
.getByRole('button', {
231+
name: 'Sort by',
232+
})
233+
.click();
234+
235+
screen
236+
.getByRole('option', {
237+
name: 'Storage size',
238+
})
239+
.click();
240+
241+
const sorted = screen
242+
.getAllByRole('gridcell')
243+
.map((el: HTMLElement) => el.getAttribute('data-id'));
244+
expect(sorted).to.deep.equal([
245+
'foo.foo',
246+
'buz.buz',
247+
'bar.bar',
248+
'bat.bat',
249+
]);
250+
});
251+
216252
it('should not display statistics (except storage size) on timeseries collection card', function () {
217253
renderCollectionsList({
218254
namespace: 'db',

0 commit comments

Comments
 (0)