-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1722 from gchq/feature/BAI-1570-write-a-script-th…
…at-calculates-the-total-size-of-all-files-in-bailo BAI-1570 add new get-used-storage script
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import prettyBytes from 'pretty-bytes' | ||
|
||
import FileModel from '../models/File.js' | ||
import log from '../services/log.js' | ||
import { connectToMongoose, disconnectFromMongoose } from '../utils/database.js' | ||
|
||
async function main() { | ||
await connectToMongoose() | ||
|
||
const existingResults = await FileModel.find({}) | ||
// mongoose-delete plugin doesn't have correct typing so cast to any | ||
const deletedResults = await (FileModel as any).findDeleted() | ||
|
||
setTimeout(disconnectFromMongoose, 50) | ||
|
||
const totalExistingBytes = existingResults.reduce((sum, fileModel) => sum + fileModel.size, 0) | ||
const totalDeletedBytes = deletedResults.reduce((sum, fileModel) => sum + fileModel.size, 0) | ||
|
||
const totalBytes = { | ||
existing: totalExistingBytes, | ||
deleted: totalDeletedBytes, | ||
total: totalExistingBytes + totalDeletedBytes, | ||
} | ||
// Copy of totalBytes with human readable values | ||
const totalFormattedBytes = Object.fromEntries( | ||
Object.entries(totalBytes).map(([key, value]) => [key, prettyBytes(value)]), | ||
) | ||
|
||
// Print results | ||
log.info(totalBytes, 'Storage used:') | ||
log.info(totalFormattedBytes, 'Formatted storage used:') | ||
} | ||
|
||
main() |