|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 |
|
| 4 | +using LiveChartsCore; |
| 5 | +using LiveChartsCore.Defaults; |
| 6 | +using LiveChartsCore.SkiaSharpView; |
| 7 | +using LiveChartsCore.SkiaSharpView.Painting; |
| 8 | + |
| 9 | +using SkiaSharp; |
| 10 | + |
4 | 11 | namespace SourceGit.Models |
5 | 12 | { |
6 | | - public class StatisticsSample(string name) |
| 13 | + public enum StaticsticsMode |
| 14 | + { |
| 15 | + All, |
| 16 | + ThisMonth, |
| 17 | + ThisWeek, |
| 18 | + } |
| 19 | + |
| 20 | + public class StaticsticsAuthor(string name, int count) |
7 | 21 | { |
8 | 22 | public string Name { get; set; } = name; |
9 | | - public int Count { get; set; } = 0; |
| 23 | + public int Count { get; set; } = count; |
| 24 | + } |
| 25 | + |
| 26 | + public class StaticsticsSample(DateTime time, int count) |
| 27 | + { |
| 28 | + public DateTime Time { get; set; } = time; |
| 29 | + public int Count { get; set; } = count; |
10 | 30 | } |
11 | 31 |
|
12 | 32 | public class StatisticsReport |
13 | 33 | { |
| 34 | + public static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; |
| 35 | + |
14 | 36 | public int Total { get; set; } = 0; |
15 | | - public List<StatisticsSample> Samples { get; set; } = new List<StatisticsSample>(); |
16 | | - public List<StatisticsSample> ByAuthor { get; set; } = new List<StatisticsSample>(); |
| 37 | + public List<StaticsticsAuthor> Authors { get; set; } = new List<StaticsticsAuthor>(); |
| 38 | + public List<ISeries> Series { get; set; } = new List<ISeries>(); |
| 39 | + public List<Axis> XAxes { get; set; } = new List<Axis>(); |
| 40 | + public List<Axis> YAxes { get; set; } = new List<Axis>(); |
17 | 41 |
|
18 | | - public void AddCommit(int index, string author) |
| 42 | + public StatisticsReport(StaticsticsMode mode, DateTime start) |
19 | 43 | { |
20 | | - Total++; |
21 | | - Samples[index].Count++; |
| 44 | + _mode = mode; |
| 45 | + |
| 46 | + YAxes = [new Axis() { |
| 47 | + TextSize = 10, |
| 48 | + MinLimit = 0, |
| 49 | + SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = .6f } |
| 50 | + }]; |
| 51 | + |
| 52 | + if (mode == StaticsticsMode.ThisWeek) |
| 53 | + { |
| 54 | + for (int i = 0; i < 7; i++) |
| 55 | + _mapSamples.Add(start.AddDays(i), 0); |
22 | 56 |
|
23 | | - if (_mapUsers.TryGetValue(author, out var value)) |
| 57 | + XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(1), v => WEEKDAYS[(int)v.DayOfWeek]) { TextSize = 10 }); |
| 58 | + } |
| 59 | + else if (mode == StaticsticsMode.ThisMonth) |
24 | 60 | { |
25 | | - value.Count++; |
| 61 | + var now = DateTime.Now; |
| 62 | + var maxDays = DateTime.DaysInMonth(now.Year, now.Month); |
| 63 | + for (int i = 0; i < maxDays; i++) |
| 64 | + _mapSamples.Add(start.AddDays(i), 0); |
| 65 | + |
| 66 | + XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(1), v => $"{v:MM/dd}") { TextSize = 10 }); |
26 | 67 | } |
27 | 68 | else |
28 | 69 | { |
29 | | - var sample = new StatisticsSample(author); |
30 | | - sample.Count++; |
31 | | - |
32 | | - _mapUsers.Add(author, sample); |
33 | | - ByAuthor.Add(sample); |
| 70 | + XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(365), v => $"{v:yyyy/MM}") { TextSize = 10 }); |
34 | 71 | } |
35 | 72 | } |
36 | 73 |
|
| 74 | + public void AddCommit(DateTime time, string author) |
| 75 | + { |
| 76 | + Total++; |
| 77 | + |
| 78 | + var normalized = DateTime.MinValue; |
| 79 | + if (_mode == StaticsticsMode.ThisWeek || _mode == StaticsticsMode.ThisMonth) |
| 80 | + normalized = time.Date; |
| 81 | + else |
| 82 | + normalized = new DateTime(time.Year, time.Month, 1).ToLocalTime(); |
| 83 | + |
| 84 | + if (_mapSamples.TryGetValue(normalized, out var vs)) |
| 85 | + _mapSamples[normalized] = vs + 1; |
| 86 | + else |
| 87 | + _mapSamples.Add(normalized, 1); |
| 88 | + |
| 89 | + if (_mapUsers.TryGetValue(author, out var vu)) |
| 90 | + _mapUsers[author] = vu + 1; |
| 91 | + else |
| 92 | + _mapUsers.Add(author, 1); |
| 93 | + } |
| 94 | + |
37 | 95 | public void Complete() |
38 | 96 | { |
39 | | - ByAuthor.Sort((l, r) => r.Count - l.Count); |
| 97 | + var samples = new List<DateTimePoint>(); |
| 98 | + foreach (var kv in _mapSamples) |
| 99 | + samples.Add(new DateTimePoint(kv.Key, kv.Value)); |
| 100 | + |
| 101 | + Series.Add( |
| 102 | + new LineSeries<DateTimePoint>() |
| 103 | + { |
| 104 | + Values = samples, |
| 105 | + Stroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 1 }, |
| 106 | + Fill = new SolidColorPaint(SKColors.SkyBlue.WithAlpha(90)), |
| 107 | + GeometrySize = 8, |
| 108 | + GeometryStroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 2 } |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + foreach (var kv in _mapUsers) |
| 113 | + Authors.Add(new StaticsticsAuthor(kv.Key, kv.Value)); |
| 114 | + |
| 115 | + Authors.Sort((l, r) => r.Count - l.Count); |
| 116 | + |
40 | 117 | _mapUsers.Clear(); |
| 118 | + _mapSamples.Clear(); |
41 | 119 | } |
42 | 120 |
|
43 | | - private readonly Dictionary<string, StatisticsSample> _mapUsers = new Dictionary<string, StatisticsSample>(); |
| 121 | + private StaticsticsMode _mode = StaticsticsMode.All; |
| 122 | + private Dictionary<string, int> _mapUsers = new Dictionary<string, int>(); |
| 123 | + private Dictionary<DateTime, int> _mapSamples = new Dictionary<DateTime, int>(); |
44 | 124 | } |
45 | 125 |
|
46 | 126 | public class Statistics |
47 | 127 | { |
48 | | - public StatisticsReport Year { get; set; } = new StatisticsReport(); |
49 | | - public StatisticsReport Month { get; set; } = new StatisticsReport(); |
50 | | - public StatisticsReport Week { get; set; } = new StatisticsReport(); |
| 128 | + public StatisticsReport All { get; set; } |
| 129 | + public StatisticsReport Month { get; set; } |
| 130 | + public StatisticsReport Week { get; set; } |
51 | 131 |
|
52 | 132 | public Statistics() |
53 | 133 | { |
54 | | - _today = DateTime.Today; |
55 | | - _thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24 - _today.Hour * 3600 - _today.Minute * 60 - _today.Second); |
56 | | - _thisWeekEnd = _thisWeekStart.AddDays(7); |
57 | | - |
58 | | - for (int i = 0; i < 12; i++) |
59 | | - Year.Samples.Add(new StatisticsSample("")); |
60 | | - |
61 | | - var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month); |
62 | | - for (int i = 0; i < monthDays; i++) |
63 | | - Month.Samples.Add(new StatisticsSample($"{i + 1}")); |
| 134 | + _today = DateTime.Now.ToLocalTime().Date; |
| 135 | + _thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24); |
| 136 | + _thisMonthStart = _today.AddDays(1 - _today.Day); |
64 | 137 |
|
65 | | - string[] weekDayNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; |
66 | | - for (int i = 0; i < weekDayNames.Length; i++) |
67 | | - Week.Samples.Add(new StatisticsSample(weekDayNames[i])); |
68 | | - } |
69 | | - |
70 | | - public string Since() |
71 | | - { |
72 | | - return _today.AddMonths(-11).ToString("yyyy-MM-01 00:00:00"); |
| 138 | + All = new StatisticsReport(StaticsticsMode.All, DateTime.MinValue); |
| 139 | + Month = new StatisticsReport(StaticsticsMode.ThisMonth, _thisMonthStart); |
| 140 | + Week = new StatisticsReport(StaticsticsMode.ThisWeek, _thisWeekStart); |
73 | 141 | } |
74 | 142 |
|
75 | 143 | public void AddCommit(string author, double timestamp) |
76 | 144 | { |
77 | 145 | var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime(); |
78 | | - if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0) |
79 | | - Week.AddCommit((int)time.DayOfWeek, author); |
| 146 | + if (time >= _thisWeekStart) |
| 147 | + Week.AddCommit(time, author); |
80 | 148 |
|
81 | | - if (time.Month == _today.Month) |
82 | | - Month.AddCommit(time.Day - 1, author); |
| 149 | + if (time >= _thisMonthStart) |
| 150 | + Month.AddCommit(time, author); |
83 | 151 |
|
84 | | - Year.AddCommit(time.Month - 1, author); |
| 152 | + All.AddCommit(time, author); |
85 | 153 | } |
86 | 154 |
|
87 | 155 | public void Complete() |
88 | 156 | { |
89 | | - Year.Complete(); |
| 157 | + All.Complete(); |
90 | 158 | Month.Complete(); |
91 | 159 | Week.Complete(); |
92 | | - |
93 | | - // Year is start from 11 months ago from now. |
94 | | - var thisYear = _today.Year; |
95 | | - var start = _today.AddMonths(-11); |
96 | | - if (start.Month == 1) |
97 | | - { |
98 | | - for (int i = 0; i < 12; i++) |
99 | | - Year.Samples[i].Name = $"{thisYear}/{i + 1:00}"; |
100 | | - } |
101 | | - else |
102 | | - { |
103 | | - var lastYearIdx = start.Month - 1; |
104 | | - var lastYearMonths = Year.Samples.GetRange(lastYearIdx, 12 - lastYearIdx); |
105 | | - for (int i = 0; i < lastYearMonths.Count; i++) |
106 | | - lastYearMonths[i].Name = $"{thisYear - 1}/{lastYearIdx + i + 1:00}"; |
107 | | - |
108 | | - var thisYearMonths = Year.Samples.GetRange(0, lastYearIdx); |
109 | | - for (int i = 0; i < thisYearMonths.Count; i++) |
110 | | - thisYearMonths[i].Name = $"{thisYear}/{i + 1:00}"; |
111 | | - |
112 | | - Year.Samples.Clear(); |
113 | | - Year.Samples.AddRange(lastYearMonths); |
114 | | - Year.Samples.AddRange(thisYearMonths); |
115 | | - } |
116 | 160 | } |
117 | 161 |
|
118 | 162 | private readonly DateTime _today; |
| 163 | + private readonly DateTime _thisMonthStart; |
119 | 164 | private readonly DateTime _thisWeekStart; |
120 | | - private readonly DateTime _thisWeekEnd; |
121 | 165 | } |
122 | 166 | } |
0 commit comments