2024-02-23 03:16:28 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Models {
|
2024-02-24 19:32:15 -08:00
|
|
|
|
public class StatisticsSample {
|
2024-02-23 03:16:28 -08:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
|
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++;
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
|
if (_mapByCommitter.ContainsKey(committer)) {
|
|
|
|
|
_mapByCommitter[committer].Count++;
|
|
|
|
|
} else {
|
|
|
|
|
var sample = new StatisticsSample() { Name = committer, Count = 1 };
|
|
|
|
|
_mapByCommitter.Add(committer, sample);
|
|
|
|
|
ByCommitter.Add(sample);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
|
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 StatisticsReport Year { get; set; } = new StatisticsReport();
|
|
|
|
|
public StatisticsReport Month { get; set; } = new StatisticsReport();
|
|
|
|
|
public StatisticsReport Week { get; set; } = new StatisticsReport();
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
|
|
|
|
public Statistics() {
|
|
|
|
|
_utcStart = DateTime.UnixEpoch;
|
|
|
|
|
_today = DateTime.Today;
|
|
|
|
|
_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++) {
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Year.Samples.Add(new StatisticsSample {
|
2024-02-23 03:16:28 -08:00
|
|
|
|
Name = monthNames[i],
|
|
|
|
|
Count = 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month);
|
|
|
|
|
for (int i = 0; i < monthDays; i++) {
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Month.Samples.Add(new StatisticsSample {
|
2024-02-23 03:16:28 -08:00
|
|
|
|
Name = $"{i + 1}",
|
|
|
|
|
Count = 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] weekDayNames = [
|
|
|
|
|
"SUN",
|
|
|
|
|
"MON",
|
|
|
|
|
"TUE",
|
|
|
|
|
"WED",
|
|
|
|
|
"THU",
|
|
|
|
|
"FRI",
|
|
|
|
|
"SAT",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < weekDayNames.Length; i++) {
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Week.Samples.Add(new StatisticsSample {
|
2024-02-23 03:16:28 -08:00
|
|
|
|
Name = weekDayNames[i],
|
|
|
|
|
Count = 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Since() {
|
|
|
|
|
return _today.ToString("yyyy-01-01 00:00:00");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
|
public void AddCommit(string committer, double timestamp) {
|
2024-02-25 17:27:02 -08:00
|
|
|
|
var time = _utcStart.AddSeconds(timestamp).ToLocalTime();
|
2024-02-24 19:32:15 -08:00
|
|
|
|
if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0) {
|
|
|
|
|
Week.AddCommit((int)time.DayOfWeek, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
|
if (time.Month == _today.Month) {
|
|
|
|
|
Month.AddCommit(time.Day - 1, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 17:27:02 -08:00
|
|
|
|
Year.AddCommit(time.Month - 1, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Complete() {
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Year.Complete();
|
|
|
|
|
Month.Complete();
|
|
|
|
|
Week.Complete();
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DateTime _utcStart;
|
|
|
|
|
private DateTime _today;
|
|
|
|
|
private DateTime _thisWeekStart;
|
|
|
|
|
private DateTime _thisWeekEnd;
|
|
|
|
|
}
|
|
|
|
|
}
|