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.Collections.Generic;
namespace SourceGit.Commands
{

View file

@ -3,10 +3,10 @@ using System.Collections.Generic;
namespace SourceGit.Models
{
public class StatisticsSample
public class StatisticsSample(string name)
{
public string Name { get; set; }
public int Count { get; set; }
public string Name { get; set; } = name;
public int Count { get; set; } = 0;
}
public class StatisticsReport
@ -26,7 +26,9 @@ namespace SourceGit.Models
}
else
{
var sample = new StatisticsSample() { Name = committer, Count = 1 };
var sample = new StatisticsSample(committer);
sample.Count++;
_mapByCommitter.Add(committer, 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);
_thisWeekEnd = _thisWeekStart.AddDays(7);
string[] monthNames = [
"Jan",
"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,
});
}
for (int i = 0; i < 12; i++)
Year.Samples.Add(new StatisticsSample(""));
var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month);
for (int i = 0; i < monthDays; i++)
{
Month.Samples.Add(new StatisticsSample
{
Name = $"{i + 1}",
Count = 0,
});
}
string[] weekDayNames = [
"SUN",
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT",
];
Month.Samples.Add(new StatisticsSample($"{i + 1}"));
string[] weekDayNames = [ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" ];
for (int i = 0; i < weekDayNames.Length; i++)
{
Week.Samples.Add(new StatisticsSample
{
Name = weekDayNames[i],
Count = 0,
});
}
Week.Samples.Add(new StatisticsSample(weekDayNames[i]));
}
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)
{
var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0)
{
Week.AddCommit((int)time.DayOfWeek, committer);
}
if (time.Month == _today.Month)
{
Month.AddCommit(time.Day - 1, committer);
}
Year.AddCommit(time.Month - 1, committer);
}
@ -133,6 +89,30 @@ namespace SourceGit.Models
Year.Complete();
Month.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;

View file

@ -557,7 +557,7 @@
<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.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.TotalCommitters" 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.ThisMonth" 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.TotalCommitters" 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:Name="ThisControl"
Title="{DynamicResource Text.Statistics}"
Width="800" Height="450"
Width="860" Height="500"
WindowStartupLocation="CenterOwner"
CanResize="False">
<Grid RowDefinitions="Auto,Auto,*">
@ -159,7 +159,7 @@
<ListBox.ItemTemplate>
<DataTemplate DataType="m:StatisticsSample">
<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">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
</Border>
@ -190,7 +190,7 @@
<!-- Graph -->
<v:Chart Grid.Column="1"
Margin="16,0,0,0"
Margin="16"
LabelBrush="{DynamicResource Brush.FG1}"
LineBrush="{DynamicResource Brush.Border2}"
ShapeBrush="{DynamicResource Brush.Accent}"

View file

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