refactor<Statistics>: use committer instead of author

This commit is contained in:
leo 2024-02-25 11:32:15 +08:00
parent e070b79d2c
commit 68ddeb4cc5
7 changed files with 174 additions and 208 deletions

View file

@ -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() {

View file

@ -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<StatisticsSample> Samples { get; set; } = new List<StatisticsSample>();
public List<StatisticsSample> ByCommitter { get; set; } = new List<StatisticsSample>();
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<string, StatisticsSample> _mapByCommitter = new Dictionary<string, StatisticsSample>();
}
public class Statistics {
public int TotalYear { get; set; } = 0;
public int TotalMonth { get; set; } = 0;
public int TotalWeek { get; set; } = 0;
public List<Sample> Year { get; set; } = new List<Sample>();
public List<Sample> Month { get; set; } = new List<Sample>();
public List<Sample> Week { get; set; } = new List<Sample>();
public List<Sample> YearByAuthor { get; set; } = new List<Sample>();
public List<Sample> MonthByAuthor { get; set; } = new List<Sample>();
public List<Sample> WeekByAuthor { get; set; } = new List<Sample>();
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<string, Sample> map, List<Sample> 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<string, Sample> _mapYearByAuthor = new Dictionary<string, Sample>();
private Dictionary<string, Sample> _mapMonthByAuthor = new Dictionary<string, Sample>();
private Dictionary<string, Sample> _mapWeekByAuthor = new Dictionary<string, Sample>();
}
}

View file

@ -461,9 +461,9 @@
<sys:String x:Key="Text.Statistics.ThisWeek">WEEK</sys:String>
<sys:String x:Key="Text.Statistics.ThisMonth">MONTH</sys:String>
<sys:String x:Key="Text.Statistics.ThisYear">YEAR</sys:String>
<sys:String x:Key="Text.Statistics.TotalAuthors">Total Authors</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommitters">Total Committers</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommits">Total Commits</sys:String>
<sys:String x:Key="Text.Statistics.AuthorName">AUTHOR</sys:String>
<sys:String x:Key="Text.Statistics.Committer">COMMITTER</sys:String>
<sys:String x:Key="Text.Statistics.CommitAmount">COMMITS</sys:String>
<sys:String x:Key="Text.NotConfigured">Git has NOT been configured. Please to go [Preference] and configure it first.</sys:String>

View file

@ -460,9 +460,9 @@
<sys:String x:Key="Text.Statistics.ThisWeek">本周</sys:String>
<sys:String x:Key="Text.Statistics.ThisMonth">本月</sys:String>
<sys:String x:Key="Text.Statistics.ThisYear">本年</sys:String>
<sys:String x:Key="Text.Statistics.TotalAuthors">作者人数</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommitters">提交者人数</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommits">总计提交次数</sys:String>
<sys:String x:Key="Text.Statistics.AuthorName">作者</sys:String>
<sys:String x:Key="Text.Statistics.Committer">提交者</sys:String>
<sys:String x:Key="Text.Statistics.CommitAmount">提交次数</sys:String>
<sys:String x:Key="Text.NotConfigured">GIT尚未配置。请打开【偏好设置】配置GIT路径。</sys:String>

View file

@ -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;
}
}

View file

@ -15,7 +15,7 @@
CanResize="False"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="NoChrome">
<Grid RowDefinitions="30,*">
<Grid RowDefinitions="30,Auto,*">
<!-- Title bar -->
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto">
<Border Grid.Column="0" Grid.ColumnSpan="3"
@ -52,164 +52,116 @@
</Button>
</Grid>
<!-- Body -->
<TabControl Grid.Row="1" Margin="0,8,0,0">
<TabControl.Styles>
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter">
<Setter Property="HorizontalAlignment" Value="Center"/>
<!-- View mode switcher -->
<ListBox Grid.Row="1"
Margin="0,8,0,0"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Transparent">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
</TabControl.Styles>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisYear}"/>
</TabItem.Header>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.YearByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<Style Selector="ListBoxItem:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Transparent"/>
</Style>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.YearByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<Style Selector="ListBoxItem:selected /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Transparent"/>
</Style>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Year}"/>
<Style Selector="ListBoxItem:selected Border.switcher_bg">
<Setter Property="Background" Value="{DynamicResource Brush.Contents}"/>
</Style>
<Style Selector="TextBlock.view_mode_switcher">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>
</Style>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalYear}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
<Style Selector="ListBoxItem:selected TextBlock.view_mode_switcher">
<Setter Property="Foreground" Value="{DynamicResource Brush.FG1}"/>
</Style>
</ListBox.Styles>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisMonth}"/>
</TabItem.Header>
<ListBoxItem>
<Border Classes="switcher_bg" Height="28" Padding="16,0" BorderThickness="1,1,0,1" BorderBrush="{DynamicResource Brush.Border1}" CornerRadius="14,0,0,14">
<TextBlock Classes="view_mode_switcher" Text="{DynamicResource Text.Statistics.ThisYear}"/>
</Border>
</ListBoxItem>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.MonthByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<ListBoxItem>
<Border Classes="switcher_bg" Height="28" Padding="16,0" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}">
<TextBlock Classes="view_mode_switcher" Text="{DynamicResource Text.Statistics.ThisMonth}"/>
</Border>
</ListBoxItem>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.MonthByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<ListBoxItem>
<Border Classes="switcher_bg" Height="28" Padding="16,0" BorderThickness="0,1,1,1" BorderBrush="{DynamicResource Brush.Border1}" CornerRadius="0,14,14,0">
<TextBlock Classes="view_mode_switcher" Text="{DynamicResource Text.Statistics.ThisWeek}"/>
</Border>
</ListBoxItem>
</ListBox>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Month}"/>
<!-- Contents -->
<ContentControl Grid.Row="2" Content="{Binding SelectedReport, Mode=OneWay}">
<ContentControl.DataTemplates>
<DataTemplate DataType="m:StatisticsReport">
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Committer -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding ByCommitter}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.Committer}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalMonth}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
<!-- Total Committers -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommitters}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding ByCommitter.Count}" Margin="4,0,0,0"/>
</StackPanel>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisWeek}"/>
</TabItem.Header>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Samples}"/>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.WeekByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.WeekByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Week}"/>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalWeek}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
</TabControl>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Total}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
<!-- Loading Mask -->
<Path Grid.Row="1"

View file

@ -33,10 +33,10 @@ namespace SourceGit.Views {
set => SetValue(ShapeBrushProperty, value);
}
public static readonly StyledProperty<List<Models.Sample>> SamplesProperty =
AvaloniaProperty.Register<Chart, List<Models.Sample>>(nameof(Samples), null);
public static readonly StyledProperty<List<Models.StatisticsSample>> SamplesProperty =
AvaloniaProperty.Register<Chart, List<Models.StatisticsSample>>(nameof(Samples), null);
public List<Models.Sample> Samples {
public List<Models.StatisticsSample> Samples {
get => GetValue(SamplesProperty);
set => SetValue(SamplesProperty, value);
}