From 68ddeb4cc5bf914f68bf1c0314cd770d0b234795 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 25 Feb 2024 11:32:15 +0800 Subject: [PATCH] refactor: use committer instead of author --- src/Commands/Statistics.cs | 2 +- src/Models/Statistics.cs | 92 ++++++----- src/Resources/Locales/en_US.axaml | 4 +- src/Resources/Locales/zh_CN.axaml | 4 +- src/ViewModels/Statistics.cs | 28 +++- src/Views/Statistics.axaml | 246 ++++++++++++------------------ src/Views/Statistics.axaml.cs | 6 +- 7 files changed, 174 insertions(+), 208 deletions(-) diff --git a/src/Commands/Statistics.cs b/src/Commands/Statistics.cs index 9f1f6ec2..fb0e7ee0 100644 --- a/src/Commands/Statistics.cs +++ b/src/Commands/Statistics.cs @@ -7,7 +7,7 @@ namespace SourceGit.Commands { WorkingDirectory = repo; Context = repo; - Args = $"log --date-order --branches --remotes --since=\"{_statistics.Since()}\" --date=unix --pretty=format:\"%ad$%an\""; + Args = $"log --date-order --branches --remotes --since=\"{_statistics.Since()}\" --pretty=format:\"%ct$%cn\""; } public Models.Statistics Result() { diff --git a/src/Models/Statistics.cs b/src/Models/Statistics.cs index 8bc4c029..b6214dee 100644 --- a/src/Models/Statistics.cs +++ b/src/Models/Statistics.cs @@ -2,23 +2,41 @@ using System.Collections.Generic; namespace SourceGit.Models { - public class Sample { + public class StatisticsSample { public string Name { get; set; } public int Count { get; set; } } + public class StatisticsReport { + public int Total { get; set; } = 0; + public List Samples { get; set; } = new List(); + public List ByCommitter { get; set; } = new List(); + + public void AddCommit(int index, string committer) { + Total++; + Samples[index].Count++; + + if (_mapByCommitter.ContainsKey(committer)) { + _mapByCommitter[committer].Count++; + } else { + var sample = new StatisticsSample() { Name = committer, Count = 1 }; + _mapByCommitter.Add(committer, sample); + ByCommitter.Add(sample); + } + } + + public void Complete() { + ByCommitter.Sort((l, r) => r.Count - l.Count); + _mapByCommitter.Clear(); + } + + private Dictionary _mapByCommitter = new Dictionary(); + } + public class Statistics { - public int TotalYear { get; set; } = 0; - public int TotalMonth { get; set; } = 0; - public int TotalWeek { get; set; } = 0; - - public List Year { get; set; } = new List(); - public List Month { get; set; } = new List(); - public List Week { get; set; } = new List(); - - public List YearByAuthor { get; set; } = new List(); - public List MonthByAuthor { get; set; } = new List(); - public List WeekByAuthor { get; set; } = new List(); + public StatisticsReport Year { get; set; } = new StatisticsReport(); + public StatisticsReport Month { get; set; } = new StatisticsReport(); + public StatisticsReport Week { get; set; } = new StatisticsReport(); public Statistics() { _utcStart = DateTime.UnixEpoch; @@ -42,7 +60,7 @@ namespace SourceGit.Models { ]; for (int i = 0; i < monthNames.Length; i++) { - Year.Add(new Sample { + Year.Samples.Add(new StatisticsSample { Name = monthNames[i], Count = 0, }); @@ -50,7 +68,7 @@ namespace SourceGit.Models { var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month); for (int i = 0; i < monthDays; i++) { - Month.Add(new Sample { + Month.Samples.Add(new StatisticsSample { Name = $"{i + 1}", Count = 0, }); @@ -67,7 +85,7 @@ namespace SourceGit.Models { ]; for (int i = 0; i < weekDayNames.Length; i++) { - Week.Add(new Sample { + Week.Samples.Add(new StatisticsSample { Name = weekDayNames[i], Count = 0, }); @@ -78,52 +96,28 @@ namespace SourceGit.Models { return _today.ToString("yyyy-01-01 00:00:00"); } - public void AddCommit(string author, double timestamp) { - var authorTime = _utcStart.AddSeconds(timestamp); - if (authorTime.CompareTo(_thisWeekStart) >= 0 && authorTime.CompareTo(_thisWeekEnd) < 0) { - Week[(int)authorTime.DayOfWeek].Count++; - TotalWeek++; - AddByAuthor(_mapWeekByAuthor, WeekByAuthor, author); + public void AddCommit(string committer, double timestamp) { + var time = _utcStart.AddSeconds(timestamp); + if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0) { + Week.AddCommit((int)time.DayOfWeek, committer); } - if (authorTime.Month == _today.Month) { - Month[authorTime.Day - 1].Count++; - TotalMonth++; - AddByAuthor(_mapMonthByAuthor, MonthByAuthor, author); + if (time.Month == _today.Month) { + Month.AddCommit(time.Day - 1, committer); } - Year[authorTime.Month - 1].Count++; - TotalYear++; - AddByAuthor(_mapYearByAuthor, YearByAuthor, author); + Year.AddCommit(time.Month, committer); } public void Complete() { - _mapYearByAuthor.Clear(); - _mapMonthByAuthor.Clear(); - _mapWeekByAuthor.Clear(); - - YearByAuthor.Sort((l, r) => r.Count - l.Count); - MonthByAuthor.Sort((l, r) => r.Count - l.Count); - WeekByAuthor.Sort((l, r) => r.Count - l.Count); - } - - private void AddByAuthor(Dictionary map, List collection, string author) { - if (map.ContainsKey(author)) { - map[author].Count++; - } else { - var sample = new Sample { Name = author, Count = 1 }; - map.Add(author, sample); - collection.Add(sample); - } + Year.Complete(); + Month.Complete(); + Week.Complete(); } private DateTime _utcStart; private DateTime _today; private DateTime _thisWeekStart; private DateTime _thisWeekEnd; - - private Dictionary _mapYearByAuthor = new Dictionary(); - private Dictionary _mapMonthByAuthor = new Dictionary(); - private Dictionary _mapWeekByAuthor = new Dictionary(); } } diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 1834115e..4c898bf5 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -461,9 +461,9 @@ WEEK MONTH YEAR - Total Authors + Total Committers Total Commits - AUTHOR + COMMITTER COMMITS Git has NOT been configured. Please to go [Preference] and configure it first. diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index e8a43731..ed0c57ac 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -460,9 +460,9 @@ 本周 本月 本年 - 作者人数 + 提交者人数 总计提交次数 - 作者 + 提交者 提交次数 GIT尚未配置。请打开【偏好设置】配置GIT路径。 diff --git a/src/ViewModels/Statistics.cs b/src/ViewModels/Statistics.cs index b72df346..661c4e32 100644 --- a/src/ViewModels/Statistics.cs +++ b/src/ViewModels/Statistics.cs @@ -8,10 +8,17 @@ namespace SourceGit.ViewModels { get => _isLoading; private set => SetProperty(ref _isLoading, value); } + + public int SelectedIndex { + get => _selectedIndex; + set { + if (SetProperty(ref _selectedIndex, value)) RefreshReport(); + } + } - public Models.Statistics Data { - get => _data; - private set => SetProperty(ref _data, value); + public Models.StatisticsReport SelectedReport { + get => _selectedReport; + private set => SetProperty(ref _selectedReport, value); } public Statistics(string repo) { @@ -20,14 +27,27 @@ namespace SourceGit.ViewModels { Task.Run(() => { var result = new Commands.Statistics(_repo).Result(); Dispatcher.UIThread.Invoke(() => { + _data = result; + RefreshReport(); IsLoading = false; - Data = result; }); }); } + private void RefreshReport() { + if (_data == null) return; + + switch (_selectedIndex) { + case 0: SelectedReport = _data.Year; break; + case 1: SelectedReport = _data.Month; break; + default: SelectedReport = _data.Week; break; + } + } + private string _repo = string.Empty; private bool _isLoading = true; private Models.Statistics _data = null; + private Models.StatisticsReport _selectedReport = null; + private int _selectedIndex = 0; } } diff --git a/src/Views/Statistics.axaml b/src/Views/Statistics.axaml index 01cd4ce8..ac0c84e8 100644 --- a/src/Views/Statistics.axaml +++ b/src/Views/Statistics.axaml @@ -15,7 +15,7 @@ CanResize="False" ExtendClientAreaToDecorationsHint="True" ExtendClientAreaChromeHints="NoChrome"> - + - - - - - - - - - - - - - - - - - - + - - - - - + - - + + + - - - - - - - + + - - - - + + + + + - - - - - - - - + + + + + - - - - - + + + + + + - - + + + + + + + + + + + + - - - - - - - + + + + + - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + SetValue(ShapeBrushProperty, value); } - public static readonly StyledProperty> SamplesProperty = - AvaloniaProperty.Register>(nameof(Samples), null); + public static readonly StyledProperty> SamplesProperty = + AvaloniaProperty.Register>(nameof(Samples), null); - public List Samples { + public List Samples { get => GetValue(SamplesProperty); set => SetValue(SamplesProperty, value); }