From dd432c63e85eada01f8afb7c55bf8217568b51b8 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 31 May 2025 18:52:15 +0800 Subject: [PATCH] enhance: when counting commits in `Statistics`, if the authors have the same e-mail address, the commits are considered to be from the same person (#1380) Signed-off-by: leo --- src/Models/Statistics.cs | 53 +++++++++++++++++++++----------------- src/Views/Statistics.axaml | 4 ++- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/Models/Statistics.cs b/src/Models/Statistics.cs index d982a3ed..d6f5870d 100644 --- a/src/Models/Statistics.cs +++ b/src/Models/Statistics.cs @@ -26,25 +26,23 @@ namespace SourceGit.Models public class StatisticsReport { - public static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; - public int Total { get; set; } = 0; - public List Authors { get; set; } = new List(); - public List Series { get; set; } = new List(); - public List XAxes { get; set; } = new List(); - public List YAxes { get; set; } = new List(); + public List Authors { get; set; } = new(); + public List Series { get; set; } = new(); + public List XAxes { get; set; } = new(); + public List YAxes { get; set; } = new(); public StatisticsAuthor SelectedAuthor { get => _selectedAuthor; set => ChangeAuthor(value); } public StatisticsReport(StatisticsMode mode, DateTime start) { _mode = mode; - YAxes = [new Axis() + YAxes.Add(new Axis() { TextSize = 10, MinLimit = 0, SeparatorsPaint = new SolidColorPaint(new SKColor(0x40808080)) { StrokeThickness = 1 } - }]; + }); if (mode == StatisticsMode.ThisWeek) { @@ -72,7 +70,7 @@ namespace SourceGit.Models { Total++; - var normalized = DateTime.MinValue; + DateTime normalized; if (_mode == StatisticsMode.ThisWeek || _mode == StatisticsMode.ThisMonth) normalized = time.Date; else @@ -172,26 +170,27 @@ namespace SourceGit.Models ChangeColor(_fillColor); } - private StatisticsMode _mode = StatisticsMode.All; - private Dictionary _mapUsers = new Dictionary(); - private Dictionary _mapSamples = new Dictionary(); - private Dictionary> _mapUserSamples = new Dictionary>(); + private static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; + private StatisticsMode _mode; + private Dictionary _mapUsers = new(); + private Dictionary _mapSamples = new(); + private Dictionary> _mapUserSamples = new(); private StatisticsAuthor _selectedAuthor = null; private uint _fillColor = 255; } public class Statistics { - public StatisticsReport All { get; set; } - public StatisticsReport Month { get; set; } - public StatisticsReport Week { get; set; } + public StatisticsReport All { get; } + public StatisticsReport Month { get; } + public StatisticsReport Week { get; } public Statistics() { - _today = DateTime.Now.ToLocalTime().Date; - var weekOffset = (7 + (int)_today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) % 7; - _thisWeekStart = _today.AddDays(-weekOffset); - _thisMonthStart = _today.AddDays(1 - _today.Day); + var today = DateTime.Now.ToLocalTime().Date; + var weekOffset = (7 + (int)today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) % 7; + _thisWeekStart = today.AddDays(-weekOffset); + _thisMonthStart = today.AddDays(1 - today.Day); All = new StatisticsReport(StatisticsMode.All, DateTime.MinValue); Month = new StatisticsReport(StatisticsMode.ThisMonth, _thisMonthStart); @@ -200,7 +199,13 @@ namespace SourceGit.Models public void AddCommit(string author, double timestamp) { - var user = User.FindOrAdd(author); + var emailIdx = author.IndexOf('±', StringComparison.Ordinal); + var email = author.Substring(emailIdx + 1).ToLower(CultureInfo.CurrentCulture); + if (!_users.TryGetValue(email, out var user)) + { + user = User.FindOrAdd(author); + _users.Add(email, user); + } var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime(); if (time >= _thisWeekStart) @@ -214,13 +219,15 @@ namespace SourceGit.Models public void Complete() { + _users.Clear(); + All.Complete(); Month.Complete(); Week.Complete(); } - - private readonly DateTime _today; + private readonly DateTime _thisMonthStart; private readonly DateTime _thisWeekStart; + private readonly Dictionary _users = new(); } } diff --git a/src/Views/Statistics.axaml b/src/Views/Statistics.axaml index 577849df..a2d18393 100644 --- a/src/Views/Statistics.axaml +++ b/src/Views/Statistics.axaml @@ -162,7 +162,9 @@ - +