refactor: collect the commits for the most recent year instead of just the current year (#414)

This commit is contained in:
leo 2024-08-28 18:16:57 +08:00
parent c90abd0ca2
commit 83b802e357
No known key found for this signature in database
6 changed files with 47 additions and 74 deletions

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
namespace SourceGit.Commands namespace SourceGit.Commands
{ {

View file

@ -3,10 +3,10 @@ using System.Collections.Generic;
namespace SourceGit.Models namespace SourceGit.Models
{ {
public class StatisticsSample public class StatisticsSample(string name)
{ {
public string Name { get; set; } public string Name { get; set; } = name;
public int Count { get; set; } public int Count { get; set; } = 0;
} }
public class StatisticsReport public class StatisticsReport
@ -26,7 +26,9 @@ namespace SourceGit.Models
} }
else else
{ {
var sample = new StatisticsSample() { Name = committer, Count = 1 }; var sample = new StatisticsSample(committer);
sample.Count++;
_mapByCommitter.Add(committer, sample); _mapByCommitter.Add(committer, sample);
ByCommitter.Add(sample); ByCommitter.Add(sample);
} }
@ -53,77 +55,31 @@ namespace SourceGit.Models
_thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24 - _today.Hour * 3600 - _today.Minute * 60 - _today.Second); _thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24 - _today.Hour * 3600 - _today.Minute * 60 - _today.Second);
_thisWeekEnd = _thisWeekStart.AddDays(7); _thisWeekEnd = _thisWeekStart.AddDays(7);
string[] monthNames = [ for (int i = 0; i < 12; i++)
"Jan", Year.Samples.Add(new StatisticsSample(""));
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
for (int i = 0; i < monthNames.Length; i++)
{
Year.Samples.Add(new StatisticsSample
{
Name = monthNames[i],
Count = 0,
});
}
var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month); var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month);
for (int i = 0; i < monthDays; i++) for (int i = 0; i < monthDays; i++)
{ Month.Samples.Add(new StatisticsSample($"{i + 1}"));
Month.Samples.Add(new StatisticsSample
{
Name = $"{i + 1}",
Count = 0,
});
}
string[] weekDayNames = [
"SUN",
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT",
];
string[] weekDayNames = [ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" ];
for (int i = 0; i < weekDayNames.Length; i++) for (int i = 0; i < weekDayNames.Length; i++)
{ Week.Samples.Add(new StatisticsSample(weekDayNames[i]));
Week.Samples.Add(new StatisticsSample
{
Name = weekDayNames[i],
Count = 0,
});
}
} }
public string Since() public string Since()
{ {
return _today.ToString("yyyy-01-01 00:00:00"); return _today.AddMonths(-11).ToString("yyyy-MM-01 00:00:00");
} }
public void AddCommit(string committer, double timestamp) public void AddCommit(string committer, double timestamp)
{ {
var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime(); var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0) if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0)
{
Week.AddCommit((int)time.DayOfWeek, committer); Week.AddCommit((int)time.DayOfWeek, committer);
}
if (time.Month == _today.Month) if (time.Month == _today.Month)
{
Month.AddCommit(time.Day - 1, committer); Month.AddCommit(time.Day - 1, committer);
}
Year.AddCommit(time.Month - 1, committer); Year.AddCommit(time.Month - 1, committer);
} }
@ -133,6 +89,30 @@ namespace SourceGit.Models
Year.Complete(); Year.Complete();
Month.Complete(); Month.Complete();
Week.Complete(); Week.Complete();
// Year is start from 11 months ago from now.
var thisYear = _today.Year;
var start = _today.AddMonths(-11);
if (start.Month == 1)
{
for (int i = 0; i < 12; i++)
Year.Samples[i].Name = $"{thisYear}/{i + 1:00}";
}
else
{
var lastYearIdx = start.Month - 1;
var lastYearMonths = Year.Samples.GetRange(lastYearIdx, 12 - lastYearIdx);
for (int i = 0; i < lastYearMonths.Count; i++)
lastYearMonths[i].Name = $"{thisYear - 1}/{lastYearIdx + i + 1:00}";
var thisYearMonths = Year.Samples.GetRange(0, lastYearIdx);
for (int i = 0; i < thisYearMonths.Count; i++)
thisYearMonths[i].Name = $"{thisYear}/{i + 1:00}";
Year.Samples.Clear();
Year.Samples.AddRange(lastYearMonths);
Year.Samples.AddRange(thisYearMonths);
}
} }
private readonly DateTime _today; private readonly DateTime _today;

View file

@ -557,7 +557,7 @@
<x:String x:Key="Text.Statistics.Committer" xml:space="preserve">提交者</x:String> <x:String x:Key="Text.Statistics.Committer" xml:space="preserve">提交者</x:String>
<x:String x:Key="Text.Statistics.ThisMonth" xml:space="preserve">本月</x:String> <x:String x:Key="Text.Statistics.ThisMonth" xml:space="preserve">本月</x:String>
<x:String x:Key="Text.Statistics.ThisWeek" xml:space="preserve">本周</x:String> <x:String x:Key="Text.Statistics.ThisWeek" xml:space="preserve">本周</x:String>
<x:String x:Key="Text.Statistics.ThisYear" xml:space="preserve">年</x:String> <x:String x:Key="Text.Statistics.ThisYear" xml:space="preserve">最近一年</x:String>
<x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次数: </x:String> <x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次数: </x:String>
<x:String x:Key="Text.Statistics.TotalCommitters" xml:space="preserve">提交者: </x:String> <x:String x:Key="Text.Statistics.TotalCommitters" xml:space="preserve">提交者: </x:String>
<x:String x:Key="Text.Submodule" xml:space="preserve">子模块</x:String> <x:String x:Key="Text.Submodule" xml:space="preserve">子模块</x:String>

View file

@ -558,7 +558,7 @@
<x:String x:Key="Text.Statistics.Committer" xml:space="preserve">提交者</x:String> <x:String x:Key="Text.Statistics.Committer" xml:space="preserve">提交者</x:String>
<x:String x:Key="Text.Statistics.ThisMonth" xml:space="preserve">本月</x:String> <x:String x:Key="Text.Statistics.ThisMonth" xml:space="preserve">本月</x:String>
<x:String x:Key="Text.Statistics.ThisWeek" xml:space="preserve">本週</x:String> <x:String x:Key="Text.Statistics.ThisWeek" xml:space="preserve">本週</x:String>
<x:String x:Key="Text.Statistics.ThisYear" xml:space="preserve">年</x:String> <x:String x:Key="Text.Statistics.ThisYear" xml:space="preserve">最近一年</x:String>
<x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次數:</x:String> <x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次數:</x:String>
<x:String x:Key="Text.Statistics.TotalCommitters" xml:space="preserve">提交者:</x:String> <x:String x:Key="Text.Statistics.TotalCommitters" xml:space="preserve">提交者:</x:String>
<x:String x:Key="Text.Submodule" xml:space="preserve">子模組</x:String> <x:String x:Key="Text.Submodule" xml:space="preserve">子模組</x:String>

View file

@ -10,7 +10,7 @@
x:DataType="vm:Statistics" x:DataType="vm:Statistics"
x:Name="ThisControl" x:Name="ThisControl"
Title="{DynamicResource Text.Statistics}" Title="{DynamicResource Text.Statistics}"
Width="800" Height="450" Width="860" Height="500"
WindowStartupLocation="CenterOwner" WindowStartupLocation="CenterOwner"
CanResize="False"> CanResize="False">
<Grid RowDefinitions="Auto,Auto,*"> <Grid RowDefinitions="Auto,Auto,*">
@ -159,7 +159,7 @@
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate DataType="m:StatisticsSample"> <DataTemplate DataType="m:StatisticsSample">
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"> <Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}">
<Grid ColumnDefinitions="*,150"> <Grid ColumnDefinitions="*,100">
<Border Grid.Column="0" Padding="8,0" ClipToBounds="True"> <Border Grid.Column="0" Padding="8,0" ClipToBounds="True">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/> <TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
</Border> </Border>
@ -190,7 +190,7 @@
<!-- Graph --> <!-- Graph -->
<v:Chart Grid.Column="1" <v:Chart Grid.Column="1"
Margin="16,0,0,0" Margin="16"
LabelBrush="{DynamicResource Brush.FG1}" LabelBrush="{DynamicResource Brush.FG1}"
LineBrush="{DynamicResource Brush.Border2}" LineBrush="{DynamicResource Brush.Border2}"
ShapeBrush="{DynamicResource Brush.Accent}" ShapeBrush="{DynamicResource Brush.Accent}"

View file

@ -86,21 +86,18 @@ namespace SourceGit.Views
else else
maxV = (int)Math.Ceiling(maxV / 500.0) * 500; maxV = (int)Math.Ceiling(maxV / 500.0) * 500;
var fontFamily = this.FindResource("Fonts.Monospace") as FontFamily; var typeface = new Typeface("fonts:SourceGit#JetBrains Mono");
var typeface = new Typeface(fontFamily);
var pen = new Pen(LineBrush); var pen = new Pen(LineBrush);
var width = Bounds.Width; var width = Bounds.Width;
var height = Bounds.Height; var height = Bounds.Height;
// Transparent background to block mouse move events.
context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, width, height));
// Draw coordinate // Draw coordinate
var maxLabel = new FormattedText($"{maxV}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12.0, LabelBrush); var maxLabel = new FormattedText($"{maxV}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12.0, LabelBrush);
var horizonStart = maxLabel.Width + 8; var horizonStart = maxLabel.Width + 8;
var labelHeight = maxLabel.Height; var labelHeight = maxLabel.Height;
var bg = this.FindResource("Brush.Contents") as IBrush;
context.DrawText(maxLabel, new Point(0, -maxLabel.Height * 0.5)); context.DrawText(maxLabel, new Point(0, -maxLabel.Height * 0.5));
context.DrawRectangle(pen, new Rect(horizonStart, 0, width - horizonStart, height - labelHeight)); context.DrawRectangle(bg, pen, new Rect(horizonStart, 0, width - horizonStart, height - labelHeight));
if (samples.Count == 0) if (samples.Count == 0)
return; return;
@ -158,15 +155,12 @@ namespace SourceGit.Views
context.DrawRectangle(ShapeBrush, null, rect); context.DrawRectangle(ShapeBrush, null, rect);
if (stepX < 32) var test = (stepX - 4) / hLabel.Width;
if (test < 1.0)
{ {
var matrix = Matrix.CreateTranslation(hLabel.Width * 0.5, -hLabel.Height * 0.5) // Center of label var matrix = Matrix.CreateRotation(Math.Acos(test)) * Matrix.CreateTranslation(xLabel + hLabel.Width * 0.5, yLabel);
* Matrix.CreateRotation(Math.PI * 0.25) // Rotate
* Matrix.CreateTranslation(xLabel, yLabel); // Move
using (context.PushTransform(matrix)) using (context.PushTransform(matrix))
{
context.DrawText(hLabel, new Point(0, 0)); context.DrawText(hLabel, new Point(0, 0));
}
} }
else else
{ {