Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAI-1570 add new get-used-storage script #1722

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()
Loading