Skip to content

Commit

Permalink
Merge pull request #1722 from gchq/feature/BAI-1570-write-a-script-th…
Browse files Browse the repository at this point in the history
…at-calculates-the-total-size-of-all-files-in-bailo

BAI-1570 add new get-used-storage script
  • Loading branch information
PE39806 authored Jan 13, 2025
2 parents d8f045c + 6f2449f commit 64df7af
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions backend/src/scripts/getTotalStorageUsedMongo.ts
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()

0 comments on commit 64df7af

Please sign in to comment.