The Electron BrowserWindow to attach dialogs to
Optional
targetPath: stringOptional pre-defined export path (bypasses user prompt)
Promise resolving to object containing success status, message, and path
// Export with dialog prompt
const result = await exportArtistMetricsToCSV(mainWindow);
if (result.success) {
console.log(`CSV saved to: ${result.filePath}`);
}
export async function exportArtistMetricsToCSV(
mainWindow: BrowserWindow,
targetPath?: string,
): Promise<{ success: boolean; message?: string; filePath?: string }> {
try {
// Get artist metrics data
const artistMetrics = await aggregateArtistSkipMetrics();
if (!artistMetrics || Object.keys(artistMetrics).length === 0) {
return {
success: false,
message: "No artist metrics data available to export",
};
}
// Define headers for CSV
const headers = {
artistName: "Artist Name",
totalSkips: "Total Skips",
uniqueTracksSkipped: "Unique Tracks Skipped",
skipRatio: "Skip Ratio",
manualSkips: "Manual Skips",
autoSkips: "Auto Skips",
averagePlayPercentage: "Average Play Percentage",
};
// Prepare data for CSV
const csvData = Object.values(artistMetrics).map((artist) => ({
artistName: artist.artistName,
totalSkips: artist.totalSkips,
uniqueTracksSkipped: artist.uniqueTracksSkipped.length,
skipRatio: artist.skipRatio,
manualSkips: artist.manualSkips,
autoSkips: artist.autoSkips,
averagePlayPercentage: artist.averagePlayPercentage,
}));
// Convert to CSV
const csv = objectToCSV(csvData, headers);
// Determine file path
let filePath = targetPath;
if (!filePath) {
const timestamp = new Date()
.toISOString()
.replace(/:/g, "-")
.split(".")[0];
const defaultFileName = `artist_metrics_${timestamp}.csv`;
filePath = await promptForExportLocation(
mainWindow,
join(ensureExportDir(), defaultFileName),
[{ name: "CSV Files", extensions: ["csv"] }],
);
if (!filePath) {
return { success: false, message: "Export was canceled" };
}
}
// Write the CSV file
writeFileSync(filePath, csv);
return {
success: true,
message: "Artist metrics data exported successfully",
filePath,
};
} catch (error) {
console.error("Error exporting artist metrics to CSV:", error);
return {
success: false,
message: `Error exporting data: ${error instanceof Error ? error.message : "Unknown error"}`,
};
}
}
Exports artist metrics to CSV
Creates a detailed CSV export containing aggregated statistics for each artist in the user's listening history. This export focuses on artist-level metrics, particularly skip behavior patterns, making it ideal for analyzing which artists are most frequently skipped.
The export includes:
This function handles data retrieval, formatting, user prompts for save location, and file writing with appropriate error handling throughout the process.