Example #1
0
        public async Task <BuildTime> GetBuildTimeAsync(int defaultChannelId, int days)
        {
            Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(defaultChannelId);

            if (defaultChannel == null)
            {
                return(new BuildTime
                {
                    DefaultChannelId = 0,
                    OfficialBuildTime = 0,
                    PrBuildTime = 0,
                    GoalTimeInMinutes = 0
                });
            }

            MultiProjectKustoQuery queries = SharedKustoQueries.CreateBuildTimesQueries(defaultChannel.Repository, defaultChannel.Branch, days);

            var results = await Task.WhenAll <IDataReader>(_kustoClientProvider.ExecuteKustoQueryAsync(queries.Internal),
                                                           _kustoClientProvider.ExecuteKustoQueryAsync(queries.Public));

            (int officialBuildId, TimeSpan officialBuildTime) = SharedKustoQueries.ParseBuildTime(results[0]);
            (int prBuildId, TimeSpan prBuildTime)             = SharedKustoQueries.ParseBuildTime(results[1]);

            double officialTime = 0;
            double prTime       = 0;
            int    goalTime     = 0;

            if (officialBuildId != -1)
            {
                officialTime = officialBuildTime.TotalMinutes;

                // Get goal time for definition id
                Data.Models.GoalTime goal = await _context.GoalTime
                                            .FirstOrDefaultAsync(g => g.DefinitionId == officialBuildId && g.ChannelId == defaultChannel.ChannelId);

                if (goal != null)
                {
                    goalTime = goal.Minutes;
                }
            }

            if (prBuildId != -1)
            {
                prTime = prBuildTime.TotalMinutes;
            }

            return(new BuildTime
            {
                DefaultChannelId = defaultChannelId,
                OfficialBuildTime = officialTime,
                PrBuildTime = prTime,
                GoalTimeInMinutes = goalTime
            });
        }
Example #2
0
        public async Task <BuildTime> GetBuildTimeAsync(int defaultChannelId, int days)
        {
            var defaultChannel = await _context.DefaultChannels
                                 .Where(dc => dc.Id == defaultChannelId)
                                 .Select(dc => new
            {
                Repository = dc.Repository,
                Branch     = dc.Branch,
                ChannelId  = dc.ChannelId,

                // Get AzDO BuildDefinitionId for the most recent build in the default channel.
                // It will be used to restrict the average build time query in Kusto
                // to official builds only.
                BuildDefinitionId = dc.Channel.BuildChannels
                                    .Select(bc => bc.Build)
                                    .Where(b => b.AzureDevOpsBuildDefinitionId.HasValue &&
                                           ((b.GitHubRepository == dc.Repository && b.GitHubBranch == dc.Branch) ||
                                            (b.AzureDevOpsRepository == dc.Repository && b.AzureDevOpsBranch == dc.Branch)))
                                    .OrderByDescending(b => b.DateProduced)
                                    .Select(b => b.AzureDevOpsBuildDefinitionId)
                                    .FirstOrDefault()
            })
                                 .FirstOrDefaultAsync();

            if (defaultChannel == null)
            {
                return(null);
            }

            MultiProjectKustoQuery queries = SharedKustoQueries.CreateBuildTimesQueries(
                defaultChannel.Repository,
                defaultChannel.Branch,
                days,
                defaultChannel.BuildDefinitionId);

            var results = await Task.WhenAll <IDataReader>(_kustoClientProvider.ExecuteKustoQueryAsync(queries.Internal),
                                                           _kustoClientProvider.ExecuteKustoQueryAsync(queries.Public));

            (int officialBuildId, TimeSpan officialBuildTime) = SharedKustoQueries.ParseBuildTime(results[0]);
            (int prBuildId, TimeSpan prBuildTime)             = SharedKustoQueries.ParseBuildTime(results[1]);

            double officialTime = 0;
            double prTime       = 0;
            int    goalTime     = 0;

            if (officialBuildId != -1)
            {
                officialTime = officialBuildTime.TotalMinutes;

                // Get goal time for definition id
                Data.Models.GoalTime goal = await _context.GoalTime
                                            .FirstOrDefaultAsync(g => g.DefinitionId == officialBuildId && g.ChannelId == defaultChannel.ChannelId);

                if (goal != null)
                {
                    goalTime = goal.Minutes;
                }
            }

            if (prBuildId != -1)
            {
                prTime = prBuildTime.TotalMinutes;
            }

            return(new BuildTime
            {
                DefaultChannelId = defaultChannelId,
                OfficialBuildTime = officialTime,
                PrBuildTime = prTime,
                GoalTimeInMinutes = goalTime
            });
        }