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