TFS 2013 leaderboard to provide friendly competition amongst co-workers

Program.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using Microsoft.TeamFoundation.Client;
  7. using Microsoft.TeamFoundation.Framework.Common;
  8. using Microsoft.TeamFoundation.VersionControl.Client;
  9. using Microsoft.TeamFoundation.WorkItemTracking.Client;
  10. namespace pulse
  11. {
  12. /// <summary>
  13. /// The program.
  14. /// </summary>
  15. public class Program
  16. {
  17. /// <summary>
  18. /// Mains the specified arguments.
  19. /// </summary>
  20. /// <param name="args">The arguments.</param>
  21. public static void Main(string[] args)
  22. {
  23. if (args.Length != 1)
  24. {
  25. Console.WriteLine("Missing path to TFS project collection.");
  26. return;
  27. }
  28. var now = DateTime.Now;
  29. var autoResetEvent = new AutoResetEvent(true);
  30. var uri = new Uri(args[0]);
  31. var dashboard = new Dashboard(uri);
  32. var configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(uri, new UICredentialsProvider());
  33. configurationServer.EnsureAuthenticated();
  34. // Start the TFS worker.
  35. var worker = new BackgroundWorker();
  36. worker.DoWork += (o, a) =>
  37. {
  38. now = DateTime.Now;
  39. dashboard.ClearHighlights();
  40. Debug.WriteLine("Worker started at " + DateTime.Now);
  41. if (a.Cancel)
  42. {
  43. return;
  44. }
  45. // Get all the project collections.
  46. var collections = configurationServer.CatalogNode.QueryChildren(
  47. new[] { CatalogResourceTypes.ProjectCollection },
  48. false,
  49. CatalogQueryOptions.None);
  50. foreach (var collection in collections)
  51. {
  52. if (a.Cancel)
  53. {
  54. return;
  55. }
  56. // Find the project collection.
  57. var projectCollectionId = new Guid(collection.Resource.Properties["InstanceId"]);
  58. var projectCollection = configurationServer.GetTeamProjectCollection(projectCollectionId);
  59. if (a.Cancel)
  60. {
  61. return;
  62. }
  63. // Get the projects.
  64. var projects = projectCollection.CatalogNode.QueryChildren(
  65. new[] { CatalogResourceTypes.TeamProject },
  66. false,
  67. CatalogQueryOptions.None);
  68. dashboard.AddProjects(projects.ToArray());
  69. if (a.Cancel)
  70. {
  71. return;
  72. }
  73. // Get the work history.
  74. var workItemStore = projectCollection.GetService<WorkItemStore>();
  75. var workItemCollection = workItemStore.Query("select * from workitems order by [changed date] asc");
  76. if (dashboard.DateRefresh.HasValue)
  77. {
  78. dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>().Where(x => x.ChangedDate > dashboard.DateRefresh.Value));
  79. }
  80. else
  81. {
  82. dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>());
  83. }
  84. if (a.Cancel)
  85. {
  86. return;
  87. }
  88. // Get the commit log.
  89. var versionControlServer = projectCollection.GetService<VersionControlServer>();
  90. var commits = versionControlServer.QueryHistory("$/", RecursionType.Full);
  91. if (dashboard.DateRefresh.HasValue)
  92. {
  93. dashboard.AddCommits(commits.Where(x => x.CreationDate > dashboard.DateRefresh.Value));
  94. }
  95. else
  96. {
  97. dashboard.AddCommits(commits);
  98. }
  99. }
  100. };
  101. worker.RunWorkerCompleted += (o, a) =>
  102. {
  103. Debug.WriteLine("Worker completed at " + DateTime.Now);
  104. dashboard.DateRefresh = now;
  105. dashboard.Cache();
  106. dashboard.Update();
  107. // Play any queued sounds.
  108. while (Sound.Queue.Count > 0)
  109. {
  110. Sound.Queue.Dequeue()();
  111. }
  112. var pause = new AutoResetEvent(false);
  113. if (pause.WaitOne(new TimeSpan(0, 15, 0)))
  114. {
  115. worker.RunWorkerAsync();
  116. }
  117. };
  118. worker.RunWorkerAsync();
  119. var running = true;
  120. while (running)
  121. {
  122. dashboard.Update();
  123. // Listen for key events.
  124. if (Console.KeyAvailable)
  125. {
  126. var key = Console.ReadKey(true);
  127. switch (key.Key)
  128. {
  129. case ConsoleKey.PageDown:
  130. dashboard.SelectedItem += 20;
  131. break;
  132. case ConsoleKey.J:
  133. case ConsoleKey.DownArrow:
  134. // Move down.
  135. dashboard.SelectedItem++;
  136. break;
  137. case ConsoleKey.PageUp:
  138. dashboard.SelectedItem -= 20;
  139. if (dashboard.SelectedItem < 1)
  140. {
  141. dashboard.SelectedItem = 1;
  142. }
  143. break;
  144. case ConsoleKey.UpArrow:
  145. case ConsoleKey.K:
  146. // Move up.
  147. if (dashboard.SelectedItem > 0)
  148. {
  149. dashboard.SelectedItem--;
  150. }
  151. break;
  152. case ConsoleKey.Escape:
  153. case ConsoleKey.Q:
  154. // Exit the program.
  155. running = false;
  156. break;
  157. default:
  158. Debug.WriteLine("Unhandled key press: " + key.Key);
  159. break;
  160. }
  161. }
  162. if (running && autoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 0, 100)))
  163. {
  164. autoResetEvent.Reset();
  165. }
  166. }
  167. if (autoResetEvent.WaitOne(new TimeSpan(0, 0, 5)))
  168. {
  169. // Clean-up.
  170. try
  171. {
  172. worker.CancelAsync();
  173. }
  174. catch
  175. {
  176. }
  177. }
  178. }
  179. }
  180. }