TFS 2013 leaderboard to provide friendly competition amongst co-workers

PollingService.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Microsoft.TeamFoundation.Client;
  6. using Microsoft.TeamFoundation.Framework.Common;
  7. using Microsoft.TeamFoundation.VersionControl.Client;
  8. using Microsoft.TeamFoundation.WorkItemTracking.Client;
  9. namespace pulse
  10. {
  11. /// <summary>
  12. /// The polling service.
  13. /// </summary>
  14. public class PollingService : BackgroundWorker
  15. {
  16. /// <summary>
  17. /// Gets or sets the team foundation server.
  18. /// </summary>
  19. /// <value>
  20. /// The team foundation server.
  21. /// </value>
  22. public TfsConfigurationServer TeamFoundationServer { get; set; }
  23. /// <summary>
  24. /// Gets or sets the dashboard.
  25. /// </summary>
  26. /// <value>
  27. /// The dashboard.
  28. /// </value>
  29. public Dashboard Dashboard { get; set; }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="PollingService"/> class.
  32. /// </summary>
  33. /// <param name="tfs">The TFS.</param>
  34. /// <param name="dashboard">The dashboard.</param>
  35. public PollingService(TfsConfigurationServer tfs, Dashboard dashboard)
  36. {
  37. this.TeamFoundationServer = tfs;
  38. this.Dashboard = dashboard;
  39. }
  40. /// <summary>
  41. /// Raises the <see cref="E:System.ComponentModel.BackgroundWorker.DoWork" /> event.
  42. /// </summary>
  43. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  44. protected override void OnDoWork(DoWorkEventArgs e)
  45. {
  46. base.OnDoWork(e);
  47. var now = DateTime.Now;
  48. this.Dashboard.ClearHighlights();
  49. Debug.WriteLine("Worker started at " + DateTime.Now);
  50. if (e.Cancel)
  51. {
  52. return;
  53. }
  54. // Get all the project collections.
  55. var collections = this.TeamFoundationServer.CatalogNode.QueryChildren(
  56. new[] { CatalogResourceTypes.ProjectCollection },
  57. false,
  58. CatalogQueryOptions.None);
  59. foreach (var collection in collections)
  60. {
  61. if (e.Cancel)
  62. {
  63. return;
  64. }
  65. // Find the project collection.
  66. var projectCollectionId = new Guid(collection.Resource.Properties["InstanceId"]);
  67. var projectCollection = this.TeamFoundationServer.GetTeamProjectCollection(projectCollectionId);
  68. if (e.Cancel)
  69. {
  70. return;
  71. }
  72. // Get the projects.
  73. var projects = projectCollection.CatalogNode.QueryChildren(
  74. new[] { CatalogResourceTypes.TeamProject },
  75. false,
  76. CatalogQueryOptions.None);
  77. this.Dashboard.AddProjects(projects.ToArray());
  78. if (e.Cancel)
  79. {
  80. return;
  81. }
  82. // Get the work history.
  83. var workItemStore = projectCollection.GetService<WorkItemStore>();
  84. var workItemCollection = workItemStore.Query("select * from workitems order by [changed date] asc");
  85. if (this.Dashboard.DateRefresh.HasValue)
  86. {
  87. this.Dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>().Where(x => x.ChangedDate > this.Dashboard.DateRefresh.Value));
  88. }
  89. else
  90. {
  91. this.Dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>());
  92. }
  93. if (e.Cancel)
  94. {
  95. return;
  96. }
  97. // Get the commit log.
  98. var versionControlServer = projectCollection.GetService<VersionControlServer>();
  99. var commits = versionControlServer.QueryHistory("$/", RecursionType.Full);
  100. if (this.Dashboard.DateRefresh.HasValue)
  101. {
  102. this.Dashboard.AddCommits(commits.Where(x => x.CreationDate > this.Dashboard.DateRefresh.Value));
  103. }
  104. else
  105. {
  106. this.Dashboard.AddCommits(commits);
  107. }
  108. }
  109. e.Result = now;
  110. }
  111. /// <summary>
  112. /// Raises the <see cref="E:System.ComponentModel.BackgroundWorker.RunWorkerCompleted" /> event.
  113. /// </summary>
  114. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  115. protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
  116. {
  117. base.OnRunWorkerCompleted(e);
  118. Debug.WriteLine("Worker completed at " + DateTime.Now);
  119. this.Dashboard.DateRefresh = (DateTime?)e.Result;
  120. this.Dashboard.Cache();
  121. }
  122. }
  123. }