2024-02-23 03:16:28 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
|
|
|
|
public class StatisticsSample
|
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class StatisticsReport
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
public int Total { get; set; } = 0;
|
|
|
|
|
public List<StatisticsSample> Samples { get; set; } = new List<StatisticsSample>();
|
|
|
|
|
public List<StatisticsSample> ByCommitter { get; set; } = new List<StatisticsSample>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void AddCommit(int index, string committer)
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Total++;
|
|
|
|
|
Samples[index].Count++;
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_mapByCommitter.TryGetValue(committer, out var value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
value.Count++;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
var sample = new StatisticsSample() { Name = committer, Count = 1 };
|
|
|
|
|
_mapByCommitter.Add(committer, sample);
|
|
|
|
|
ByCommitter.Add(sample);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void Complete()
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
ByCommitter.Sort((l, r) => r.Count - l.Count);
|
|
|
|
|
_mapByCommitter.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Dictionary<string, StatisticsSample> _mapByCommitter = new Dictionary<string, StatisticsSample>();
|
2024-02-24 19:32:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class Statistics
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
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
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Statistics()
|
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
|
_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",
|
|
|
|
|
];
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int i = 0; i < monthNames.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int i = 0; i < monthDays; i++)
|
|
|
|
|
{
|
|
|
|
|
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",
|
|
|
|
|
];
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int i = 0; i < weekDayNames.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Week.Samples.Add(new StatisticsSample
|
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
|
Name = weekDayNames[i],
|
|
|
|
|
Count = 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Since()
|
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
|
return _today.ToString("yyyy-01-01 00:00:00");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void AddCommit(string committer, double timestamp)
|
|
|
|
|
{
|
2024-07-01 21:30:12 -07:00
|
|
|
|
var time = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (time.CompareTo(_thisWeekStart) >= 0 && time.CompareTo(_thisWeekEnd) < 0)
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Week.AddCommit((int)time.DayOfWeek, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (time.Month == _today.Month)
|
|
|
|
|
{
|
2024-02-24 19:32:15 -08:00
|
|
|
|
Month.AddCommit(time.Day - 1, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
Year.AddCommit(time.Month - 1, committer);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07: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
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly DateTime _today;
|
|
|
|
|
private readonly DateTime _thisWeekStart;
|
|
|
|
|
private readonly DateTime _thisWeekEnd;
|
2024-02-23 03:16:28 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|