mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-21 04:04:59 +00:00
feature: enhanced statistics panel (#493)
* replace the `YEAR` tab with `OVERVIEW` tab, which will analyze most recent 20K commits * use `LiveChartsCore.SkiaSharpView.Avalonia` instead of a custom chart view
This commit is contained in:
parent
983607e708
commit
af57c56cd7
13 changed files with 147 additions and 310 deletions
|
@ -1,122 +1,165 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using LiveChartsCore;
|
||||
using LiveChartsCore.Defaults;
|
||||
using LiveChartsCore.SkiaSharpView;
|
||||
using LiveChartsCore.SkiaSharpView.Painting;
|
||||
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SourceGit.Models
|
||||
{
|
||||
public class StatisticsSample(string name)
|
||||
public enum StaticsticsMode
|
||||
{
|
||||
All,
|
||||
ThisMonth,
|
||||
ThisWeek,
|
||||
}
|
||||
|
||||
public class StaticsticsAuthor(string name, int count)
|
||||
{
|
||||
public string Name { get; set; } = name;
|
||||
public int Count { get; set; } = 0;
|
||||
public int Count { get; set; } = count;
|
||||
}
|
||||
|
||||
public class StaticsticsSample(DateTime time, int count)
|
||||
{
|
||||
public DateTime Time { get; set; } = time;
|
||||
public int Count { get; set; } = count;
|
||||
}
|
||||
|
||||
public class StatisticsReport
|
||||
{
|
||||
public static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
|
||||
|
||||
public int Total { get; set; } = 0;
|
||||
public List<StatisticsSample> Samples { get; set; } = new List<StatisticsSample>();
|
||||
public List<StatisticsSample> ByAuthor { get; set; } = new List<StatisticsSample>();
|
||||
public List<StaticsticsAuthor> Authors { get; set; } = new List<StaticsticsAuthor>();
|
||||
public List<ISeries> Series { get; set; } = new List<ISeries>();
|
||||
public List<Axis> XAxes { get; set; } = new List<Axis>();
|
||||
public List<Axis> YAxes { get; set; } = new List<Axis>();
|
||||
|
||||
public void AddCommit(int index, string author)
|
||||
public StatisticsReport(StaticsticsMode mode, DateTime start)
|
||||
{
|
||||
Total++;
|
||||
Samples[index].Count++;
|
||||
_mode = mode;
|
||||
|
||||
if (_mapUsers.TryGetValue(author, out var value))
|
||||
YAxes = [new Axis() {
|
||||
TextSize = 10,
|
||||
MinLimit = 0,
|
||||
SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = .6f }
|
||||
}];
|
||||
|
||||
if (mode == StaticsticsMode.ThisWeek)
|
||||
{
|
||||
value.Count++;
|
||||
for (int i = 0; i < 7; i++)
|
||||
_mapSamples.Add(start.AddDays(i), 0);
|
||||
|
||||
XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(1), v => WEEKDAYS[(int)v.DayOfWeek]) { TextSize = 10 });
|
||||
}
|
||||
else if (mode == StaticsticsMode.ThisMonth)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var maxDays = DateTime.DaysInMonth(now.Year, now.Month);
|
||||
for (int i = 0; i < maxDays; i++)
|
||||
_mapSamples.Add(start.AddDays(i), 0);
|
||||
|
||||
XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(1), v => $"{v:MM/dd}") { TextSize = 10 });
|
||||
}
|
||||
else
|
||||
{
|
||||
var sample = new StatisticsSample(author);
|
||||
sample.Count++;
|
||||
|
||||
_mapUsers.Add(author, sample);
|
||||
ByAuthor.Add(sample);
|
||||
XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(365), v => $"{v:yyyy/MM}") { TextSize = 10 });
|
||||
}
|
||||
}
|
||||
|
||||
public void AddCommit(DateTime time, string author)
|
||||
{
|
||||
Total++;
|
||||
|
||||
var normalized = DateTime.MinValue;
|
||||
if (_mode == StaticsticsMode.ThisWeek || _mode == StaticsticsMode.ThisMonth)
|
||||
normalized = time.Date;
|
||||
else
|
||||
normalized = new DateTime(time.Year, time.Month, 1).ToLocalTime();
|
||||
|
||||
if (_mapSamples.TryGetValue(normalized, out var vs))
|
||||
_mapSamples[normalized] = vs + 1;
|
||||
else
|
||||
_mapSamples.Add(normalized, 1);
|
||||
|
||||
if (_mapUsers.TryGetValue(author, out var vu))
|
||||
_mapUsers[author] = vu + 1;
|
||||
else
|
||||
_mapUsers.Add(author, 1);
|
||||
}
|
||||
|
||||
public void Complete()
|
||||
{
|
||||
ByAuthor.Sort((l, r) => r.Count - l.Count);
|
||||
var samples = new List<DateTimePoint>();
|
||||
foreach (var kv in _mapSamples)
|
||||
samples.Add(new DateTimePoint(kv.Key, kv.Value));
|
||||
|
||||
Series.Add(
|
||||
new LineSeries<DateTimePoint>()
|
||||
{
|
||||
Values = samples,
|
||||
Stroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 1 },
|
||||
GeometrySize = 8,
|
||||
GeometryStroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 2 }
|
||||
}
|
||||
);
|
||||
|
||||
foreach (var kv in _mapUsers)
|
||||
Authors.Add(new StaticsticsAuthor(kv.Key, kv.Value));
|
||||
|
||||
Authors.Sort((l, r) => r.Count - l.Count);
|
||||
|
||||
_mapUsers.Clear();
|
||||
_mapSamples.Clear();
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, StatisticsSample> _mapUsers = new Dictionary<string, StatisticsSample>();
|
||||
private StaticsticsMode _mode = StaticsticsMode.All;
|
||||
private Dictionary<string, int> _mapUsers = new Dictionary<string, int>();
|
||||
private Dictionary<DateTime, int> _mapSamples = new Dictionary<DateTime, int>();
|
||||
}
|
||||
|
||||
public class Statistics
|
||||
{
|
||||
public StatisticsReport Year { get; set; } = new StatisticsReport();
|
||||
public StatisticsReport Month { get; set; } = new StatisticsReport();
|
||||
public StatisticsReport Week { get; set; } = new StatisticsReport();
|
||||
public StatisticsReport All { get; set; }
|
||||
public StatisticsReport Month { get; set; }
|
||||
public StatisticsReport Week { get; set; }
|
||||
|
||||
public Statistics()
|
||||
{
|
||||
_today = DateTime.Today;
|
||||
_thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24 - _today.Hour * 3600 - _today.Minute * 60 - _today.Second);
|
||||
_thisWeekEnd = _thisWeekStart.AddDays(7);
|
||||
_today = DateTime.Now.ToLocalTime().Date;
|
||||
_thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24);
|
||||
_thisMonthStart = _today.AddDays(1 - _today.Day);
|
||||
|
||||
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($"{i + 1}"));
|
||||
|
||||
string[] weekDayNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
|
||||
for (int i = 0; i < weekDayNames.Length; i++)
|
||||
Week.Samples.Add(new StatisticsSample(weekDayNames[i]));
|
||||
}
|
||||
|
||||
public string Since()
|
||||
{
|
||||
return _today.AddMonths(-11).ToString("yyyy-MM-01 00:00:00");
|
||||
All = new StatisticsReport(StaticsticsMode.All, DateTime.MinValue);
|
||||
Month = new StatisticsReport(StaticsticsMode.ThisMonth, _thisMonthStart);
|
||||
Week = new StatisticsReport(StaticsticsMode.ThisWeek, _thisWeekStart);
|
||||
}
|
||||
|
||||
public void AddCommit(string author, double timestamp)
|
||||
{
|
||||
var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
|
||||
if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0)
|
||||
Week.AddCommit((int)time.DayOfWeek, author);
|
||||
if (time >= _thisWeekStart)
|
||||
Week.AddCommit(time, author);
|
||||
|
||||
if (time.Month == _today.Month)
|
||||
Month.AddCommit(time.Day - 1, author);
|
||||
if (time >= _thisMonthStart)
|
||||
Month.AddCommit(time, author);
|
||||
|
||||
Year.AddCommit(time.Month - 1, author);
|
||||
All.AddCommit(time, author);
|
||||
}
|
||||
|
||||
public void Complete()
|
||||
{
|
||||
Year.Complete();
|
||||
All.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;
|
||||
private readonly DateTime _thisMonthStart;
|
||||
private readonly DateTime _thisWeekStart;
|
||||
private readonly DateTime _thisWeekEnd;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue