TFS 2013 leaderboard to provide friendly competition amongst co-workers

Program.cs 5.0KB

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