83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
import * as path from "node:path";
|
|
import { cache, MediaFile } from "../db";
|
|
|
|
// Function to get file extension statistics
|
|
function getExtensionStats() {
|
|
// Get all files (not directories) from the database
|
|
const query = `
|
|
SELECT path FROM media_files
|
|
WHERE kind = ${MediaFile.Kind.file}
|
|
`;
|
|
|
|
// Use raw query to get all file paths
|
|
const rows = cache.query(query).all() as { path: string }[];
|
|
|
|
// Count extensions
|
|
const extensionCounts: Record<string, number> = {};
|
|
|
|
for (const row of rows) {
|
|
const extension = path.extname(row.path).toLowerCase();
|
|
extensionCounts[extension] = (extensionCounts[extension] || 0) + 1;
|
|
}
|
|
|
|
// Sort extensions by count (descending)
|
|
const sortedExtensions = Object.entries(extensionCounts)
|
|
.sort((a, b) => b[1] - a[1]);
|
|
|
|
return {
|
|
totalFiles: rows.length,
|
|
extensions: sortedExtensions,
|
|
};
|
|
}
|
|
|
|
// Function to print a visual table
|
|
function printExtensionTable() {
|
|
const stats = getExtensionStats();
|
|
|
|
// Calculate column widths
|
|
const extensionColWidth = Math.max(
|
|
...stats.extensions.map(([ext]) => ext.length),
|
|
"Extension".length,
|
|
) + 2;
|
|
|
|
const countColWidth = Math.max(
|
|
...stats.extensions.map(([_, count]) => count.toString().length),
|
|
"Count".length,
|
|
) + 2;
|
|
|
|
const percentColWidth = "Percentage".length + 2;
|
|
|
|
// Print header
|
|
console.log("MediaFile Extension Statistics");
|
|
console.log(`Total files: ${stats.totalFiles}`);
|
|
console.log();
|
|
|
|
// Print table header
|
|
console.log(
|
|
"Extension".padEnd(extensionColWidth) +
|
|
"Count".padEnd(countColWidth) +
|
|
"Percentage".padEnd(percentColWidth),
|
|
);
|
|
|
|
// Print separator
|
|
console.log(
|
|
"-".repeat(extensionColWidth) +
|
|
"-".repeat(countColWidth) +
|
|
"-".repeat(percentColWidth),
|
|
);
|
|
|
|
// Print rows
|
|
for (const [extension, count] of stats.extensions) {
|
|
const percentage = ((count / stats.totalFiles) * 100).toFixed(2);
|
|
const ext = extension || "(no extension)";
|
|
|
|
console.log(
|
|
ext.padEnd(extensionColWidth) +
|
|
count.toString().padEnd(countColWidth) +
|
|
`${percentage}%`.padEnd(percentColWidth),
|
|
);
|
|
}
|
|
}
|
|
|
|
// Run the program
|
|
printExtensionTable();
|