Skip to content

Instantly share code, notes, and snippets.

@lynxz
Last active July 18, 2018 07:57
Show Gist options
  • Save lynxz/f5fbd53f017743b63427122bbe6a5be4 to your computer and use it in GitHub Desktop.
Save lynxz/f5fbd53f017743b63427122bbe6a5be4 to your computer and use it in GitHub Desktop.
Index for batch searching
namespace SomeNamespace {
public class BatchSearch : AbstractIndexCreationTask<ProcessStepDocument, BatchResult> {
public BatchSearch( ) {
Map = docs => from doc in docs
let tests = LoadDocument<StepExecutions>( doc.StepExecutionsId )
where !string.IsNullOrWhiteSpace( doc.Batch )
where tests.Runs.Where( r => r.Batch == doc.Batch ).All( d => d.TimeTicks <= doc.StopTimeTicks )
select new BatchResult {
Batch = $"{doc.Project}-{doc.Batch}",
DeviceResults = new DeviceResult[ ] { new DeviceResult {
DocId = doc.Id,
DeviceId = doc.DeviceId,
StepId = doc.StepId,
StepName = doc.StepName,
Result = doc.Result.ToString( ),
Status = doc.Status.ToString( )
} },
FirstUpdated = doc.StopTime,
LastUpdated = doc.StopTime
};
Reduce = results => from result in results
group result by result.Batch into g
select new BatchResult {
Batch = g.Key,
DeviceResults = g.SelectMany( d => d.DeviceResults ).Distinct( ).OrderBy( y => y.DeviceId ).ToArray( ),
FirstUpdated = g.Min( d => d.FirstUpdated ),
LastUpdated = g.Max( d => d.LastUpdated )
};
Stores.Add( x => x.Batch, FieldStorage.Yes );
Stores.Add( x => x.LastUpdated, FieldStorage.Yes );
}
}
public class BatchResult : BaseResults {
public string Batch { get; set; }
}
public abstract class BaseResults {
public DeviceResult[ ] DeviceResults { get; set; }
public DateTimeOffset FirstUpdated { get; set; }
public DateTimeOffset LastUpdated { get; set; }
}
public class StepExecutions {
public StepExecutions( ) {
Runs = new List<Run>( );
}
public string Id { get; set; }
public List<Run> Runs { get; set; }
}
public class Run {
public string Batch { get; set; }
public long TimeTicks { get; set; }
}
public class DeviceResult {
public string DocId { get; set; }
public string DeviceId { get; set; }
public string StepId { get; set; }
public string StepName { get; set; }
public string Result { get; set; }
public string Status { get; set; }
}
public enum StepStatus {
NotStarted,
InProgress,
Aborted,
ExecutionError,
Finished
}
public enum StepResult {
NA,
Undetermined,
Pass,
Fail
}
public class ProcessStepDocument {
public string Id { get; set; }
public string DeviceId { get; set; }
public string StepId { get; set; }
public string StepName { get; set; }
public string StepExecutionsId { get; set; }
public StepStatus Status { get; set; }
public StepResult Result { get; set; }
public string Batch { get; set; }
public DateTimeOffset StartTime { get; set; }
public DateTimeOffset StopTime { get; set; }
public long StopTimeTicks { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment