Ejemplo n.º 1
0
        public virtual async Task <IActionResult> Create([FromBody, Required] Goal.GoalRequestJson goalData, [Required] String channelName, [Required] int definitionId)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .FirstOrDefaultAsync(c => c.Name == channelName);

            if (channel == null)
            {
                return(NotFound());
            }
            Data.Models.GoalTime goal = await _context.GoalTime
                                        .FirstOrDefaultAsync(g => g.DefinitionId == definitionId && g.ChannelId == channel.Id);

            if (goal == null)
            {
                goal = new Data.Models.GoalTime
                {
                    DefinitionId = definitionId,
                    Minutes      = goalData.Minutes,
                    ChannelId    = channel.Id
                };
                await _context.GoalTime.AddAsync(goal);
            }
            else
            {
                goal.Minutes = goalData.Minutes;
                _context.GoalTime.Update(goal);
            }
            await _context.SaveChangesAsync();

            return(Ok(new Goal(goal)));
        }
Ejemplo n.º 2
0
 public Goal([NotNull] Data.Models.GoalTime other)
 {
     if (other == null)
     {
         throw new ArgumentNullException(nameof(other));
     }
     DefinitionId = other.DefinitionId;
     Minutes      = other.Minutes;
     Channel      = other.Channel == null ? null : new v2018_07_16.Models.Channel(other.Channel);
 }
Ejemplo n.º 3
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
            });
        }
Ejemplo n.º 4
0
        public override async Task <IActionResult> GetGoalTimes([Required] int definitionId, [Required] string channelName)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .FirstOrDefaultAsync(c => c.Name == channelName);

            if (channel == null)
            {
                return(NotFound());
            }
            Data.Models.GoalTime goal = await _context.GoalTime
                                        .FirstOrDefaultAsync(g => g.DefinitionId == definitionId && g.ChannelId == channel.Id);

            if (goal == null)
            {
                return(NotFound());
            }
            return(Ok(new Goal(goal)));
        }
Ejemplo n.º 5
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
            });
        }