Skip to content

Commit 8a35b9e

Browse files
committed
IMPROVE: Make git stats calculations accurate and date-aware
- Implemented proper time-based filtering using actual week date ranges - Added date overlap calculations for partial week inclusion - Improved 24-hour stats with daily estimates from recent week - Enhanced table formatting with proper color coding - Added dynamic sparkline patterns based on activity levels - Better handling of zero-activity periods - More accurate net change calculations and display
1 parent 5ace1a6 commit 8a35b9e

1 file changed

Lines changed: 129 additions & 14 deletions

File tree

β€Žstatic/dashboard.htmlβ€Ž

Lines changed: 129 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,43 +2224,127 @@ <h3>Database Health</h3>
22242224
return { commits: 0, additions: 0, deletions: 0, netChange: 0 };
22252225
}
22262226

2227-
// For this simple implementation, use the most recent weeks
2227+
const now = new Date();
2228+
const cutoffDate = new Date(now);
2229+
cutoffDate.setDate(cutoffDate.getDate() - days);
2230+
2231+
console.log(`Calculating stats for last ${days} days (since ${cutoffDate.toISOString().split('T')[0]})`);
2232+
22282233
const weeklyData = gitData.weekly_stats;
2229-
const weeksToInclude = Math.min(Math.ceil(days / 7), weeklyData.length);
2230-
const recentWeeks = weeklyData.slice(-weeksToInclude);
2234+
let relevantWeeks = [];
2235+
2236+
// Filter weeks that overlap with our time period
2237+
for (const week of weeklyData) {
2238+
const weekStart = new Date(week.date_start);
2239+
const weekEnd = new Date(week.date_end);
2240+
2241+
// Check if this week overlaps with our period
2242+
if (weekEnd >= cutoffDate && weekStart <= now) {
2243+
relevantWeeks.push(week);
2244+
}
2245+
}
2246+
2247+
console.log(`Found ${relevantWeeks.length} relevant weeks for ${days}-day period:`,
2248+
relevantWeeks.map(w => w.week));
2249+
2250+
// For accurate calculation, we need to consider partial weeks
2251+
let totalCommits = 0;
2252+
let totalAdditions = 0;
2253+
let totalDeletions = 0;
22312254

2232-
const totalCommits = recentWeeks.reduce((sum, week) => sum + (week.commits || 0), 0);
2233-
const totalAdditions = recentWeeks.reduce((sum, week) => sum + (week.additions || 0), 0);
2234-
const totalDeletions = recentWeeks.reduce((sum, week) => sum + (week.deletions || 0), 0);
2255+
for (const week of relevantWeeks) {
2256+
const weekStart = new Date(week.date_start);
2257+
const weekEnd = new Date(week.date_end);
2258+
2259+
// Calculate what portion of this week falls within our period
2260+
const periodStart = new Date(Math.max(weekStart.getTime(), cutoffDate.getTime()));
2261+
const periodEnd = new Date(Math.min(weekEnd.getTime(), now.getTime()));
2262+
const weekDuration = weekEnd.getTime() - weekStart.getTime();
2263+
const periodDuration = periodEnd.getTime() - periodStart.getTime();
2264+
const fraction = Math.min(1, Math.max(0, periodDuration / weekDuration));
2265+
2266+
// Apply fraction to the week's stats
2267+
const weekCommits = Math.round((week.commits || 0) * fraction);
2268+
const weekAdditions = Math.round((week.additions || 0) * fraction);
2269+
const weekDeletions = Math.round((week.deletions || 0) * fraction);
2270+
2271+
totalCommits += weekCommits;
2272+
totalAdditions += weekAdditions;
2273+
totalDeletions += weekDeletions;
2274+
2275+
console.log(`Week ${week.week}: ${weekCommits} commits (${(fraction*100).toFixed(1)}% of ${week.commits})`);
2276+
}
2277+
2278+
// Special handling for very recent periods
2279+
if (days === 1) {
2280+
// For 24h, use only the most recent day's estimated activity
2281+
const mostRecentWeek = weeklyData[weeklyData.length - 1];
2282+
if (mostRecentWeek) {
2283+
// Estimate daily average from the most recent week
2284+
totalCommits = Math.round((mostRecentWeek.commits || 0) / 7);
2285+
totalAdditions = Math.round((mostRecentWeek.additions || 0) / 7);
2286+
totalDeletions = Math.round((mostRecentWeek.deletions || 0) / 7);
2287+
}
2288+
}
22352289

2236-
return {
2290+
const result = {
22372291
commits: totalCommits,
22382292
additions: totalAdditions,
22392293
deletions: totalDeletions,
22402294
netChange: totalAdditions - totalDeletions
22412295
};
2296+
2297+
console.log(`${days}-day stats:`, result);
2298+
return result;
22422299
}
22432300

22442301
function updateTableForPeriod(period, stats) {
22452302
console.log(`Updating table for period: ${period}`, stats);
22462303

2247-
// Update main branch (assuming all data is from main)
2304+
// Update main branch with calculated stats
22482305
updateElement('main-commits', stats.commits);
2249-
updateElement('main-added', '+' + stats.additions.toLocaleString());
2250-
updateElement('main-deleted', '-' + stats.deletions.toLocaleString());
2306+
updateElement('main-added', stats.additions > 0 ? '+' + stats.additions.toLocaleString() : '0');
2307+
updateElement('main-deleted', stats.deletions > 0 ? '-' + stats.deletions.toLocaleString() : '0');
22512308

2252-
// Update testing and develop branches (showing as inactive for now)
2309+
// Update testing and develop branches (showing as inactive for this period)
22532310
updateElement('testing-commits', '--');
2254-
updateElement('testing-added', '--');
2311+
updateElement('testing-added', '--');
22552312
updateElement('testing-deleted', '--');
22562313
updateElement('develop-commits', '--');
22572314
updateElement('develop-added', '--');
22582315
updateElement('develop-deleted', '--');
22592316

22602317
// Update activity summary
22612318
updateElement('total-commits', stats.commits);
2262-
updateElement('net-changes', (stats.netChange >= 0 ? '+' : '') + stats.netChange.toLocaleString());
2263-
updateElement('active-branches', '1');
2319+
const netChangeText = stats.netChange === 0 ? '0' :
2320+
(stats.netChange > 0 ? '+' : '') + stats.netChange.toLocaleString();
2321+
updateElement('net-changes', netChangeText);
2322+
2323+
// Count active branches (main is active if it has commits)
2324+
const activeBranches = stats.commits > 0 ? 1 : 0;
2325+
updateElement('active-branches', activeBranches);
2326+
2327+
// Color the net changes appropriately
2328+
const netChangesEl = document.getElementById('net-changes');
2329+
if (netChangesEl) {
2330+
if (stats.netChange > 0) {
2331+
netChangesEl.style.color = '#b8bb26'; // Green for positive
2332+
} else if (stats.netChange < 0) {
2333+
netChangesEl.style.color = '#fb4934'; // Red for negative
2334+
} else {
2335+
netChangesEl.style.color = '#ebdbb2'; // Default for zero
2336+
}
2337+
}
2338+
2339+
// Update main branch added/deleted colors
2340+
const mainAddedEl = document.getElementById('main-added');
2341+
const mainDeletedEl = document.getElementById('main-deleted');
2342+
if (mainAddedEl) {
2343+
mainAddedEl.style.color = stats.additions > 0 ? '#b8bb26' : '#a89984';
2344+
}
2345+
if (mainDeletedEl) {
2346+
mainDeletedEl.style.color = stats.deletions > 0 ? '#fb4934' : '#a89984';
2347+
}
22642348

22652349
// Update time filter active state
22662350
document.querySelectorAll('.time-filter').forEach(btn => {
@@ -2269,6 +2353,37 @@ <h3>Database Health</h3>
22692353
btn.classList.add('active');
22702354
}
22712355
});
2356+
2357+
// Update sparkline activity indicator for main branch
2358+
updateSparklineForPeriod('main-spark', period, stats);
2359+
}
2360+
2361+
function updateSparklineForPeriod(elementId, period, stats) {
2362+
const sparklineEl = document.getElementById(elementId);
2363+
if (!sparklineEl) return;
2364+
2365+
// Generate activity pattern based on actual data
2366+
let pattern = '';
2367+
if (stats.commits === 0) {
2368+
pattern = '▁▁▁▁▁▁▁▁▁▁▁▁'; // Flat line for no activity
2369+
} else if (stats.commits < 5) {
2370+
pattern = '▁▁▂▁▂▁▁▂▁▁▂▁'; // Light activity
2371+
} else if (stats.commits < 15) {
2372+
pattern = 'β–‚β–ƒβ–„β–‚β–…β–ƒβ–‚β–„β–ƒβ–‚β–ƒβ–‚'; // Moderate activity
2373+
} else {
2374+
pattern = 'β–ƒβ–…β–†β–„β–‡β–…β–ƒβ–†β–„β–ƒβ–…β–ƒ'; // High activity
2375+
}
2376+
2377+
sparklineEl.textContent = pattern;
2378+
2379+
// Color based on activity level
2380+
if (stats.commits === 0) {
2381+
sparklineEl.style.color = '#a89984'; // Dim gray
2382+
} else if (stats.commits < 10) {
2383+
sparklineEl.style.color = '#fabd2f'; // Yellow
2384+
} else {
2385+
sparklineEl.style.color = '#b8bb26'; // Green
2386+
}
22722387
}
22732388

22742389
function setupActivityTableControls() {

0 commit comments

Comments
Β (0)