Retrieves statistics data from persistent storage

Loads the complete statistics data structure from the application's user data directory, performing data validation and repair operations to ensure integrity. If no statistics file exists, it creates a new one with default values.

The function performs several important operations:

  • Ensures the data directory exists before attempting to read
  • Creates default statistics if no file is found
  • Converts serialized arrays back to Set objects for uniqueness tracking
  • Repairs any missing or corrupted data fields
  • Preserves backward compatibility with older data formats

Error if file operations fail or data cannot be processed

// Get statistics for dashboard display
try {
const stats = await getStatistics();
displaySkipRate(stats.overallSkipRate);
renderTimeChart(stats.hourlyDistribution);
} catch (error) {
showErrorMessage("Failed to load statistics");
}
export const getStatistics = async (): Promise<StatisticsData> => {
try {
await ensureDir(join(app.getPath("userData"), "data"));

if (!existsSync(statisticsFilePath)) {
await saveStatistics(defaultStatisticsData);
return { ...defaultStatisticsData };
}

const statistics: StatisticsData = readJsonSync(statisticsFilePath);

// Process any potential issues with the loaded data
const fixedStatistics = processLoadedStatistics(statistics);

return fixedStatistics;
} catch (error) {
console.error("Error reading statistics:", error);
throw error;
}
};