Generates a concise summary of key statistics

Creates a high-level overview of the most important listening metrics for dashboard display and quick insights. This function extracts and calculates essential values from the full statistics data structure.

The summary includes:

  • Total number of unique tracks played
  • Total skips across all time periods
  • Overall skip percentage (ratio of skips to plays)
  • Today's skip count (current day statistics)
  • Current week's skip count
  • Current month's skip count
  • Average time before skipping (in seconds)

This summary provides a quick snapshot of listening behavior without the need to process the full statistics data structure, making it ideal for dashboard displays and regular UI updates.

// Display key metrics on dashboard
const summary = await getStatisticsSummary();
updateStatisticsWidget({
skips: summary.totalSkips,
rate: summary.skipPercentage.toFixed(1) + '%',
todayCount: summary.todaySkips
});
export const getStatisticsSummary = async (): Promise<{
totalTracks: number;
totalSkips: number;
skipPercentage: number;
todaySkips: number;
weekSkips: number;
monthSkips: number;
avgSkipTime: number;
}> => {
try {
const statistics = await getStatistics();

// Calculate total tracks and skips
const totalTracks = statistics.totalUniqueTracks || 0;

// Calculate skip percentage
let totalSkips = 0;
let totalPlays = 0;

// Count skips from track metrics
Object.values(statistics.trackMetrics).forEach((track) => {
totalSkips += track.skipCount || 0;
totalPlays += track.playCount || 0;
});

// Calculate skip percentage (avoid divide by zero)
const skipPercentage =
totalPlays > 0 ? Math.round((totalSkips / totalPlays) * 100) : 0;

// Calculate today's skips
const today = new Date().toISOString().split("T")[0];
const todayMetrics = statistics.dailyMetrics[today];
const todaySkips = todayMetrics ? todayMetrics.tracksSkipped : 0;

// Calculate this week's skips
const now = new Date();
const startOfWeek = new Date(now);
startOfWeek.setDate(now.getDate() - now.getDay()); // Set to beginning of week (Sunday)
let weekSkips = 0;

// Sum up skips for each day in the current week
Object.entries(statistics.dailyMetrics).forEach(([dateStr, metrics]) => {
const date = new Date(dateStr);
if (date >= startOfWeek && date <= now) {
weekSkips += metrics.tracksSkipped;
}
});

// Calculate this month's skips
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
let monthSkips = 0;

// Sum up skips for each day in the current month
Object.entries(statistics.dailyMetrics).forEach(([dateStr, metrics]) => {
const date = new Date(dateStr);
if (date >= startOfMonth && date <= now) {
monthSkips += metrics.tracksSkipped;
}
});

// Calculate average skip time (in seconds)
let totalSkipTimeMs = 0;
let skipCount = 0;

Object.values(statistics.trackMetrics).forEach((track) => {
if (track.skipCount > 0 && track.avgCompletionPercent !== undefined) {
// Only count tracks that have been skipped
const trackDuration = 180000; // Default to 3 minutes if we don't have actual duration
const avgSkipTimeForTrack =
(track.avgCompletionPercent / 100) * trackDuration;
totalSkipTimeMs += avgSkipTimeForTrack * track.skipCount;
skipCount += track.skipCount;
}
});

const avgSkipTime =
skipCount > 0
? Math.round(totalSkipTimeMs / skipCount / 1000) // Convert to seconds
: 60; // Default to 60 seconds if no data

return {
totalTracks,
totalSkips,
skipPercentage,
todaySkips,
weekSkips,
monthSkips,
avgSkipTime,
};
} catch (error) {
console.error("Error getting statistics summary:", error);
// Return default values if there's an error
return {
totalTracks: 0,
totalSkips: 0,
skipPercentage: 0,
todaySkips: 0,
weekSkips: 0,
monthSkips: 0,
avgSkipTime: 0,
};
}
};
  • Returns Promise<
        {
            totalTracks: number;
            totalSkips: number;
            skipPercentage: number;
            todaySkips: number;
            weekSkips: number;
            monthSkips: number;
            avgSkipTime: number;
        },
    >

    Promise resolving to a statistics summary object