[Cases] Rounding the file size to avoid decimals (#155542)

This PR fixes a bug in the telemetry code around the average file size.
A float can be returned by the average aggregation. To avoid the float
we'll round it before saving the value in the document.
This commit is contained in:
Jonathan Buttner 2023-04-24 08:14:48 -04:00 committed by GitHub
parent 792c7868b1
commit fe75fbd55d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 1 deletions

View file

@ -565,6 +565,88 @@ describe('utils', () => {
});
describe('files', () => {
it('rounds the average file size when it is a decimal', () => {
const attachmentFramework: AttachmentFrameworkAggsResult = {
externalReferenceTypes: {
buckets: [
{
doc_count: 5,
key: '.files',
references: {
cases: {
max: {
value: 10,
},
},
},
},
],
},
persistableReferenceTypes: {
buckets: [],
},
};
expect(
getAttachmentsFrameworkStats({
attachmentAggregations: attachmentFramework,
totalCasesForOwner: 5,
filesAggregations: {
averageSize: { value: 1.1 },
topMimeTypes: {
buckets: [],
},
},
}).attachmentFramework.files
).toMatchInlineSnapshot(`
Object {
"average": 1,
"averageSize": 1,
"maxOnACase": 10,
"topMimeTypes": Array [],
"total": 5,
}
`);
});
it('sets the average file size to 0 when the aggregation does not exist', () => {
const attachmentFramework: AttachmentFrameworkAggsResult = {
externalReferenceTypes: {
buckets: [
{
doc_count: 5,
key: '.files',
references: {
cases: {
max: {
value: 10,
},
},
},
},
],
},
persistableReferenceTypes: {
buckets: [],
},
};
expect(
getAttachmentsFrameworkStats({
attachmentAggregations: attachmentFramework,
totalCasesForOwner: 5,
}).attachmentFramework.files
).toMatchInlineSnapshot(`
Object {
"average": 1,
"averageSize": 0,
"maxOnACase": 10,
"topMimeTypes": Array [],
"total": 5,
}
`);
});
it('sets the files stats to empty when the file aggregation results is the empty version', () => {
const attachmentFramework: AttachmentFrameworkAggsResult = {
externalReferenceTypes: {

View file

@ -230,7 +230,7 @@ export const getAttachmentsFrameworkStats = ({
return emptyAttachmentFramework();
}
const averageFileSize = filesAggregations?.averageSize?.value;
const averageFileSize = getAverageFileSize(filesAggregations);
const topMimeTypes = filesAggregations?.topMimeTypes;
return {
@ -253,6 +253,14 @@ export const getAttachmentsFrameworkStats = ({
};
};
const getAverageFileSize = (filesAggregations?: FileAttachmentAggsResult) => {
if (filesAggregations?.averageSize?.value == null) {
return 0;
}
return Math.round(filesAggregations.averageSize.value);
};
const getAttachmentRegistryStats = (
registryResults: BucketsWithMaxOnCase,
totalCasesForOwner: number