TFS 2013 leaderboard to provide friendly competition amongst co-workers

Program.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 sound board.
  35. var soundBoard = new BackgroundWorker();
  36. soundBoard.DoWork += (o, a) =>
  37. {
  38. while (!a.Cancel)
  39. {
  40. if (Sound.Queue.Count > 0)
  41. {
  42. Sound.Queue.Dequeue()();
  43. }
  44. }
  45. };
  46. soundBoard.RunWorkerAsync();
  47. // Start the TFS worker.
  48. var worker = new BackgroundWorker();
  49. worker.DoWork += (o, a) =>
  50. {
  51. now = DateTime.Now;
  52. dashboard.ClearHighlights();
  53. Debug.WriteLine("Worker started at " + DateTime.Now);
  54. if (a.Cancel)
  55. {
  56. return;
  57. }
  58. // Get all the project collections.
  59. var collections = configurationServer.CatalogNode.QueryChildren(
  60. new[] { CatalogResourceTypes.ProjectCollection },
  61. false,
  62. CatalogQueryOptions.None);
  63. foreach (var collection in collections)
  64. {
  65. if (a.Cancel)
  66. {
  67. return;
  68. }
  69. // Find the project collection.
  70. var projectCollectionId = new Guid(collection.Resource.Properties["InstanceId"]);
  71. var projectCollection = configurationServer.GetTeamProjectCollection(projectCollectionId);
  72. if (a.Cancel)
  73. {
  74. return;
  75. }
  76. // Get the projects.
  77. var projects = projectCollection.CatalogNode.QueryChildren(
  78. new[] { CatalogResourceTypes.TeamProject },
  79. false,
  80. CatalogQueryOptions.None);
  81. dashboard.AddProjects(projects.ToArray());
  82. if (a.Cancel)
  83. {
  84. return;
  85. }
  86. // Get the work history.
  87. var workItemStore = projectCollection.GetService<WorkItemStore>();
  88. var workItemCollection = workItemStore.Query("select * from workitems order by [changed date] asc");
  89. if (dashboard.DateRefresh.HasValue)
  90. {
  91. dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>().Where(x => x.ChangedDate > dashboard.DateRefresh.Value));
  92. }
  93. else
  94. {
  95. dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>());
  96. }
  97. if (a.Cancel)
  98. {
  99. return;
  100. }
  101. // Get the commit log.
  102. var versionControlServer = projectCollection.GetService<VersionControlServer>();
  103. var commits = versionControlServer.QueryHistory("$/", RecursionType.Full);
  104. if (dashboard.DateRefresh.HasValue)
  105. {
  106. dashboard.AddCommits(commits.Where(x => x.CreationDate > dashboard.DateRefresh.Value));
  107. }
  108. else
  109. {
  110. dashboard.AddCommits(commits);
  111. }
  112. }
  113. };
  114. worker.RunWorkerCompleted += (o, a) =>
  115. {
  116. Debug.WriteLine("Worker completed at " + DateTime.Now);
  117. dashboard.DateRefresh = now;
  118. dashboard.Cache();
  119. dashboard.Update();
  120. worker.RunWorkerAsync();
  121. };
  122. worker.RunWorkerAsync();
  123. var running = true;
  124. while (running)
  125. {
  126. dashboard.Update();
  127. // Listen for key events.
  128. if (Console.KeyAvailable)
  129. {
  130. var key = Console.ReadKey(true);
  131. switch (key.Key)
  132. {
  133. case ConsoleKey.PageDown:
  134. dashboard.SelectedItem += 20;
  135. break;
  136. case ConsoleKey.J:
  137. case ConsoleKey.DownArrow:
  138. // Move down.
  139. dashboard.SelectedItem++;
  140. break;
  141. case ConsoleKey.PageUp:
  142. dashboard.SelectedItem -= 20;
  143. if (dashboard.SelectedItem < 1)
  144. {
  145. dashboard.SelectedItem = 1;
  146. }
  147. break;
  148. case ConsoleKey.UpArrow:
  149. case ConsoleKey.K:
  150. // Move up.
  151. if (dashboard.SelectedItem > 0)
  152. {
  153. dashboard.SelectedItem--;
  154. }
  155. break;
  156. case ConsoleKey.Escape:
  157. case ConsoleKey.Q:
  158. // Exit the program.
  159. running = false;
  160. break;
  161. default:
  162. Debug.WriteLine("Unhandled key press: " + key.Key);
  163. break;
  164. }
  165. }
  166. if (running && autoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 0, 100)))
  167. {
  168. autoResetEvent.Reset();
  169. }
  170. }
  171. if (autoResetEvent.WaitOne(new TimeSpan(0, 0, 5)))
  172. {
  173. // Clean-up.
  174. try
  175. {
  176. worker.CancelAsync();
  177. }
  178. catch
  179. {
  180. }
  181. try
  182. {
  183. soundBoard.CancelAsync();
  184. }
  185. catch
  186. {
  187. }
  188. }
  189. }
  190. }
  191. }