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 <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-31 18:52:15 +08:00
parent b94f26a937
commit dd432c63e8
No known key found for this signature in database
2 changed files with 33 additions and 24 deletions

View file

@ -26,25 +26,23 @@ namespace SourceGit.Models
public class StatisticsReport public class StatisticsReport
{ {
public static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
public int Total { get; set; } = 0; public int Total { get; set; } = 0;
public List<StatisticsAuthor> Authors { get; set; } = new List<StatisticsAuthor>(); public List<StatisticsAuthor> Authors { get; set; } = new();
public List<ISeries> Series { get; set; } = new List<ISeries>(); public List<ISeries> Series { get; set; } = new();
public List<Axis> XAxes { get; set; } = new List<Axis>(); public List<Axis> XAxes { get; set; } = new();
public List<Axis> YAxes { get; set; } = new List<Axis>(); public List<Axis> YAxes { get; set; } = new();
public StatisticsAuthor SelectedAuthor { get => _selectedAuthor; set => ChangeAuthor(value); } public StatisticsAuthor SelectedAuthor { get => _selectedAuthor; set => ChangeAuthor(value); }
public StatisticsReport(StatisticsMode mode, DateTime start) public StatisticsReport(StatisticsMode mode, DateTime start)
{ {
_mode = mode; _mode = mode;
YAxes = [new Axis() YAxes.Add(new Axis()
{ {
TextSize = 10, TextSize = 10,
MinLimit = 0, MinLimit = 0,
SeparatorsPaint = new SolidColorPaint(new SKColor(0x40808080)) { StrokeThickness = 1 } SeparatorsPaint = new SolidColorPaint(new SKColor(0x40808080)) { StrokeThickness = 1 }
}]; });
if (mode == StatisticsMode.ThisWeek) if (mode == StatisticsMode.ThisWeek)
{ {
@ -72,7 +70,7 @@ namespace SourceGit.Models
{ {
Total++; Total++;
var normalized = DateTime.MinValue; DateTime normalized;
if (_mode == StatisticsMode.ThisWeek || _mode == StatisticsMode.ThisMonth) if (_mode == StatisticsMode.ThisWeek || _mode == StatisticsMode.ThisMonth)
normalized = time.Date; normalized = time.Date;
else else
@ -172,26 +170,27 @@ namespace SourceGit.Models
ChangeColor(_fillColor); ChangeColor(_fillColor);
} }
private StatisticsMode _mode = StatisticsMode.All; private static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
private Dictionary<User, int> _mapUsers = new Dictionary<User, int>(); private StatisticsMode _mode;
private Dictionary<DateTime, int> _mapSamples = new Dictionary<DateTime, int>(); private Dictionary<User, int> _mapUsers = new();
private Dictionary<User, Dictionary<DateTime, int>> _mapUserSamples = new Dictionary<User, Dictionary<DateTime, int>>(); private Dictionary<DateTime, int> _mapSamples = new();
private Dictionary<User, Dictionary<DateTime, int>> _mapUserSamples = new();
private StatisticsAuthor _selectedAuthor = null; private StatisticsAuthor _selectedAuthor = null;
private uint _fillColor = 255; private uint _fillColor = 255;
} }
public class Statistics public class Statistics
{ {
public StatisticsReport All { get; set; } public StatisticsReport All { get; }
public StatisticsReport Month { get; set; } public StatisticsReport Month { get; }
public StatisticsReport Week { get; set; } public StatisticsReport Week { get; }
public Statistics() public Statistics()
{ {
_today = DateTime.Now.ToLocalTime().Date; var today = DateTime.Now.ToLocalTime().Date;
var weekOffset = (7 + (int)_today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) % 7; var weekOffset = (7 + (int)today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) % 7;
_thisWeekStart = _today.AddDays(-weekOffset); _thisWeekStart = today.AddDays(-weekOffset);
_thisMonthStart = _today.AddDays(1 - _today.Day); _thisMonthStart = today.AddDays(1 - today.Day);
All = new StatisticsReport(StatisticsMode.All, DateTime.MinValue); All = new StatisticsReport(StatisticsMode.All, DateTime.MinValue);
Month = new StatisticsReport(StatisticsMode.ThisMonth, _thisMonthStart); Month = new StatisticsReport(StatisticsMode.ThisMonth, _thisMonthStart);
@ -200,7 +199,13 @@ namespace SourceGit.Models
public void AddCommit(string author, double timestamp) 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(); var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
if (time >= _thisWeekStart) if (time >= _thisWeekStart)
@ -214,13 +219,15 @@ namespace SourceGit.Models
public void Complete() public void Complete()
{ {
_users.Clear();
All.Complete(); All.Complete();
Month.Complete(); Month.Complete();
Week.Complete(); Week.Complete();
} }
private readonly DateTime _today;
private readonly DateTime _thisMonthStart; private readonly DateTime _thisMonthStart;
private readonly DateTime _thisWeekStart; private readonly DateTime _thisWeekStart;
private readonly Dictionary<string, User> _users = new();
} }
} }

View file

@ -162,7 +162,9 @@
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate DataType="m:StatisticsAuthor"> <DataTemplate DataType="m:StatisticsAuthor">
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"> <Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
Background="Transparent"
ToolTip.Tip="{Binding User}">
<Grid ColumnDefinitions="26,*,100"> <Grid ColumnDefinitions="26,*,100">
<v:Avatar Grid.Column="0" <v:Avatar Grid.Column="0"
Width="16" Height="16" Width="16" Height="16"