// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace BuildAgent.Issues
{
///
/// State tracked by the program between runs.
///
[DataContract]
class PersistentState
{
///
/// All open issues.
///
[DataMember]
public List Issues = new List();
///
/// Map of stream name to list of builds within it. Only retains a few builds in order to determine the last succesful CL for a build.
///
[DataMember]
public Dictionary> Streams = new Dictionary>();
///
/// Adds a new build for tracking
///
/// The stream containing the build
/// The build to add
public void AddBuild(string Stream, IssueBuild Build)
{
// Get the list of tracked builds for this stream
List Builds;
if (!Streams.TryGetValue(Stream, out Builds))
{
Builds = new List();
Streams.Add(Stream, Builds);
}
// Add this build to the tracked state data
int BuildIdx = Builds.BinarySearch(Build);
if (BuildIdx < 0)
{
BuildIdx = ~BuildIdx;
Builds.Insert(BuildIdx, Build);
}
}
///
/// Finds the last build before the one given which executes the same steps. Used to determine the last succesful build before a failure.
///
/// The stream to search for
/// The change to search for
/// The step names which the job step needs to have
/// The last build known before the given changelist
public IssueBuild FindBuildBefore(string Stream, int Change, string StepName)
{
IssueBuild Result = null;
List Builds;
if(Streams.TryGetValue(Stream, out Builds))
{
for(int Idx = 0; Idx < Builds.Count && Builds[Idx].Change < Change; Idx++)
{
if(Builds[Idx].JobStepName == StepName)
{
Result = Builds[Idx];
}
}
}
return Result;
}
///
/// Finds the first build after the one given which executes the same steps. Used to determine the last succesful build before a failure.
///
/// The stream to search for
/// The change to search for
/// Name of the job step to find
/// The last build known before the given changelist
public IssueBuild FindBuildAfter(string Stream, int Change, string StepName)
{
IssueBuild Result = null;
List Builds;
if (Streams.TryGetValue(Stream, out Builds))
{
for (int Idx = Builds.Count - 1; Idx >= 0 && Builds[Idx].Change > Change; Idx--)
{
if (Builds[Idx].JobStepName == StepName)
{
Result = Builds[Idx];
}
}
}
return Result;
}
}
}