using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; namespace SourceGit.Views { /// /// 提交统计 /// 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(); for (int i = 0; i < 7; i++) { mapsWeek.Add(i, new Models.StatisticSample { Name = App.Text($"Weekday.{i}"), Count = 0, }); } var mapsMonth = new Dictionary(); 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(); var mapCommitterMonth = new Dictionary(); 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(); var samplesChartMonth = new List(); var samplesCommittersWeek = new List(); var samplesCommittersMonth = new List(); 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); }); } } }