sourcegit/src/Views/Statistics.xaml.cs

113 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
namespace SourceGit.Views {
/// <summary>
/// 提交统计
/// </summary>
public partial class Statistics : Controls.Window {
private string repo = null;
public Statistics(string repo) {
this.repo = repo;
InitializeComponent();
Task.Run(Refresh);
}
private void Quit(object sender, RoutedEventArgs e) {
Close();
}
private void Refresh() {
var mapsWeek = new Dictionary<int, Models.StatisticSample>();
for (int i = 0; i < 7; i++) {
mapsWeek.Add(i, new Models.StatisticSample {
Name = App.Text($"Weekday.{i}"),
Count = 0,
});
}
var mapsMonth = new Dictionary<int, Models.StatisticSample>();
var today = DateTime.Now;
var maxDays = DateTime.DaysInMonth(today.Year, today.Month);
for (int i = 1; i <= maxDays; i++) {
mapsMonth.Add(i, new Models.StatisticSample {
Name = $"{i}",
Count = 0,
});
}
var mapCommitterWeek = new Dictionary<string, Models.StatisticSample>();
var mapCommitterMonth = new Dictionary<string, Models.StatisticSample>();
var weekStart = today.AddDays(-(int)today.DayOfWeek).AddSeconds(-today.Hour * 3600 - today.Minute * 60 - today.Second);
var weekEnd = weekStart.AddDays(7);
var limits = $"--branches --remotes --since=\"{today.ToString("yyyy-MM-01 00:00:00")}\"";
var commits = new Commands.Commits(repo, limits).Result();
var totalCommitsMonth = commits.Count;
var totalCommitsWeek = 0;
foreach (var c in commits) {
var commitTime = DateTime.Parse(c.Committer.Time);
if (commitTime.CompareTo(weekStart) >= 0 && commitTime.CompareTo(weekEnd) < 0) {
mapsWeek[(int)commitTime.DayOfWeek].Count++;
if (mapCommitterWeek.ContainsKey(c.Committer.Name)) {
mapCommitterWeek[c.Committer.Name].Count++;
} else {
mapCommitterWeek[c.Committer.Name] = new Models.StatisticSample {
Name = c.Committer.Name,
Count = 1,
};
}
totalCommitsWeek++;
}
mapsMonth[commitTime.Day].Count++;
if (mapCommitterMonth.ContainsKey(c.Committer.Name)) {
mapCommitterMonth[c.Committer.Name].Count++;
} else {
mapCommitterMonth[c.Committer.Name] = new Models.StatisticSample {
Name = c.Committer.Name,
Count = 1,
};
}
}
var samplesChartWeek = new List<Models.StatisticSample>();
var samplesChartMonth = new List<Models.StatisticSample>();
var samplesCommittersWeek = new List<Models.StatisticSample>();
var samplesCommittersMonth = new List<Models.StatisticSample>();
for (int i = 0; i < 7; i++) samplesChartWeek.Add(mapsWeek[i]);
for (int i = 1; i <= maxDays; i++) samplesChartMonth.Add(mapsMonth[i]);
foreach (var kv in mapCommitterWeek) samplesCommittersWeek.Add(kv.Value);
foreach (var kv in mapCommitterMonth) samplesCommittersMonth.Add(kv.Value);
mapsMonth.Clear();
mapsWeek.Clear();
mapCommitterMonth.Clear();
mapCommitterWeek.Clear();
commits.Clear();
samplesCommittersWeek.Sort((x, y) => y.Count - x.Count);
samplesCommittersMonth.Sort((x, y) => y.Count - x.Count);
Dispatcher.Invoke(() => {
loading.IsAnimating = false;
loading.Visibility = Visibility.Collapsed;
chartWeek.SetData(samplesChartWeek);
chartMonth.SetData(samplesChartMonth);
lstCommitterWeek.ItemsSource = samplesCommittersWeek;
lstCommitterMonth.ItemsSource = samplesCommittersMonth;
txtMemberCountWeek.Text = App.Text("Statistics.TotalCommitterCount", samplesCommittersWeek.Count);
txtMemberCountMonth.Text = App.Text("Statistics.TotalCommitterCount", samplesCommittersMonth.Count);
txtCommitCountWeek.Text = App.Text("Statistics.TotalCommitsCount", totalCommitsWeek);
txtCommitCountMonth.Text = App.Text("Statistics.TotalCommitsCount", totalCommitsMonth);
});
}
}
}