TFS 11 2012 API Questions : query capacity and day

2019-01-26 15:11发布

问题:

I need to get several things done with the TFS API. Among those, I have to read the Resource planning information for the sprints of each Project to display in a WPF UI.

Tagging along this guide, I now have the following method:

    private TfsTeamService _teamService;
    private ICommonStructureService4 _structureService;
    TeamSettingsConfigurationService _teamSettingsConfigurationService;

    public void GetUserIterationAssignments(IList<ProjectInfo> projects)
    {
        foreach (ProjectInfo project in projects)
        {
            Console.WriteLine(project.Name);

            TeamFoundationTeam team = _teamService.QueryTeams(project.Uri).First();
            IList<Guid> teamGuids = new List<Guid>() { team.Identity.TeamFoundationId };
            TeamConfiguration config = _teamSettingsConfigurationService.GetTeamConfigurations(teamGuids).FirstOrDefault();
            if (config != null)
            {
                foreach (string nodePath in config.TeamSettings.IterationPaths)
                {
                    var projectNameIndex = nodePath.IndexOf("\\", 2);
                    var fullPath = nodePath.Insert(projectNameIndex, "\\Iteration");
                    var nodeInfo = _structureService.GetNodeFromPath(fullPath);
                    if (nodeInfo.StartDate != null &&
                       nodeInfo.FinishDate != null)
                    {
                        foreach (TeamFoundationIdentity member in team.GetMembers(_collection, MembershipQuery.Direct))
                        {
                            Console.WriteLine("{0} is in assigned to {1} from {2}", 
                                                    member.DisplayName, 
                                                    nodeInfo.Name,
                                                    nodeInfo.StartDate,
                                                    nodeInfo.FinishDate);
                        }
                    }
                }
            }
        }
    }

What I need to print to Console (just for this example of course) is most of the information shown in the Capacity view:

To be more precise, I need to access

  • daily capacity
  • days off (member)
  • days off (team)

Any ideas on how to do this?

回答1:

These values are only available from the Server Object Model (there is no Client Object Model equivalent at the moment). The interfaces and objects are all made Internal so even on the server you can't access these values.

internal TeamCapacity GetTeamIterationCapacity(Guid teamId, Guid iterationId);

Declaring Type: Microsoft.TeamFoundation.Server.WebAccess.WorkItemTracking.Common.DataAccess.TeamConfigurationComponent

Assembly: Microsoft.TeamFoundation.Server.WebAccess.WorkItemTracking.Common, Version=12.0.0.0

At the moment the only way to get to the data is through the json service (/tfs/{ProjectCollection}/{Team Project}/_api/_teamcapacity/updateteamcapacity/{Team}/{Iteration/Path}?__v=4) the Team Capacity page uses or directly from the ProjectCollection database from the tables mentioned by James Tupper.

It looks like the json service is using Request Verification which makes it hard to use from any external system.

Required Disclaimer:

The server databases of TFS are not meant for extensibility, any direct queries against them are not supported and the model can change without notice, even between service packs. Querying the TFS databases directly is not supported. Changing the values in the TFS databases through any other means than the official API's will essentially put your TFS server in an unsupported state and can cause major issues when upgrading to a newer version later on.



回答2:

It's not a supported approach, and may break in the future if you upgrade to a newer version of TFS, but if you only want to read the data, you can go directly to the SQL database for your TFS server.

The specific values you need are in (assuming you're using DefaultCollection)

  • [Tfs_DefaultCollection].[dbo].[tbl_TeamConfigurationCapacity]
  • [Tfs_DefaultCollection].[dbo].[tbl_TeamConfigurationCapacityDaysOffRange]

These have references to iterations and users via GUIDs.

  • The iteration GUIDs can be found in [Tfs_Warehouse].[dbo].[DimIteration]
  • The User GUIDs can be found by searching on the user name/SSID in [Tfs_Configuration].[dbo].[tbl_Identity]

Here's a quick example that queries my capacity in hours-per-day for our January iteration:

select Capacity from [Tfs_DefaultCollection].[dbo].[tbl_TeamConfigurationCapacity] as _cap
inner join [Tfs_Configuration].[dbo].[tbl_Identity] as _user
    on _user.[Id] = _cap.[TeamMemberId]
inner join [Tfs_Warehouse].[dbo].[DimIteration] as _iter
    on _iter.[IterationGUID] = _cap.[IterationId]
        where _iter.[IterationPath]='\Code\Current\401-Jan'
        and _user.[DisplayName]='Williams, Jason'

You can use a similar approach to read the start/end date ranges for each holiday from the tbl_TeamConfigurationCapacityDaysOffRange. However, it is somewhat more complex as there are Team days off and Individual days off.

Here is the query I am using to achieve this. (It seems to work, although after 6 months of working for all our users, one of our users suddenly disappeared out of the query and I discovered that for no obvious reason I now needed to look his ID up in the IdentityMap table. The joys of reverse engineering stuff from undocumented sources :-(

select [StartTime],[EndTime] from [Tfs_DefaultCollection].[dbo].[tbl_TeamConfigurationCapacityDaysOffRange] as _cap1
    inner join [Tfs_Warehouse].[dbo].[DimIteration] as _iter1
      on _iter1.[IterationGUID] = _cap1.[IterationId]
          where _iter1.[IterationPath]='\Code\Current\401-Jan'
              and _cap1.[TeamMemberId]='00000000-0000-0000-0000-000000000000'
union
    select [StartTime],[EndTime] from [Tfs_DefaultCollection].[dbo].[tbl_TeamConfigurationCapacityDaysOffRange] as _cap2
        inner join [Tfs_DefaultCollection].[dbo].[tbl_IdentityMap] as _map
            on _map.[localId] = _cap2.[TeamMemberId]
        inner join [Tfs_Configuration].[dbo].[tbl_Identity] as _user2
            on _user2.[Id] = _map.[masterId]
        inner join [Tfs_Warehouse].[dbo].[DimIteration] as _iter2
            on _iter2.[IterationGUID] = _cap2.[IterationId]
                where _iter2.[IterationPath]='\Code\Current\401-Jan'
                    and (_user2.[DisplayName]='Williams, Jason')

You will just need to substitute appropriate values for the two places that mention the iteration path, and the Username to query.



回答3:

There is another unsupported possiblity. Have a look on website http://{tfs-server}:8080/tfs/DefaultCollection/{project}/_backlogs/Capacity/{iterationpath}. The data is behind contains the capacity information in JSON format (team-capacity-data).

https://onedrive.live.com/redir?resid=88F0C013A4398D9B%21129