• Exports detected patterns to CSV

    Creates a CSV export of algorithmically detected patterns in the user's skip behavior, providing insights into potentially meaningful listening preferences and habits. This export represents the highest level of analysis in the statistics system, focusing on identified patterns rather than raw data.

    The export includes:

    • Pattern type classification (genre-based, time-based, etc.)
    • Pattern name and description in human-readable format
    • Confidence scores indicating pattern reliability
    • Affected tracks count and related metadata
    • Significance metrics indicating pattern importance

    This export is particularly valuable for understanding the "why" behind skip behaviors, moving beyond simple metrics to behavioral insights. The data is formatted for easy import into analysis tools or presentation software.

    Parameters

    • mainWindow: BrowserWindow

      The Electron BrowserWindow to attach dialogs to

    • OptionaltargetPath: string

      Optional pre-defined export path (bypasses user prompt)

    Returns Promise<{ success: boolean; message?: string; filePath?: string }>

    Promise resolving to object containing success status, message, and path

    // Export detected patterns for presentation
    const result = await exportDetectedPatternsToCSV(mainWindow);
    if (result.success) {
    presentPatternFindings(result.filePath);
    } else {
    notifyNoPatterns("Insufficient data to detect meaningful patterns yet");
    }
    export async function exportDetectedPatternsToCSV(
    mainWindow: BrowserWindow,
    targetPath?: string,
    ): Promise<{ success: boolean; message?: string; filePath?: string }> {
    try {
    // Get detected patterns
    const detectedPatterns = await detectSkipPatterns();

    if (
    !detectedPatterns ||
    !detectedPatterns.data ||
    detectedPatterns.data.length === 0
    ) {
    return {
    success: false,
    message: "No pattern detection data available to export",
    };
    }

    // Define headers for CSV
    const headers = {
    type: "Pattern Type",
    name: "Pattern Name",
    description: "Description",
    confidence: "Confidence",
    affectedTracks: "Affected Tracks",
    significance: "Significance",
    };

    // Prepare data for CSV
    const csvData = detectedPatterns.data.map((pattern) => {
    return {
    type: pattern.type || "",
    name: pattern.type || "",
    description: pattern.description || "",
    confidence: pattern.confidence || 0,
    affectedTracks: Array.isArray(pattern.relatedItems)
    ? pattern.relatedItems.length
    : 0,
    significance: pattern.confidence || 0,
    };
    });

    // 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 = `detected_patterns_${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: "Detected patterns data exported successfully",
    filePath,
    };
    } catch (error) {
    console.error("Error exporting detected patterns to CSV:", error);
    return {
    success: false,
    message: `Error exporting data: ${error instanceof Error ? error.message : "Unknown error"}`,
    };
    }
    }