54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using LeafWeb.Core.Entities;
|
|
using MathNet.Numerics;
|
|
|
|
namespace LeafWeb.Core.Utility
|
|
{
|
|
public class TimeInProgressEstimater
|
|
{
|
|
private readonly double[] _xData;
|
|
private readonly double[] _yData;
|
|
|
|
public TimeInProgressEstimater(IEnumerable<ILeafInput> leafInputs)
|
|
{
|
|
var xData = new List<double>();
|
|
var yData = new List<double>();
|
|
foreach (var leafInput in leafInputs)
|
|
{
|
|
xData.Add(leafInput.InputFiles.Count);
|
|
yData.Add(ConvertFromTimeSpan(leafInput.TimeInProgress));
|
|
}
|
|
_xData = xData.ToArray();
|
|
_yData = yData.ToArray();
|
|
}
|
|
|
|
public TimeSpan EstimateTimeInProgress(ILeafInput leafInput)
|
|
{
|
|
//var lineFunc = Fit.LineFunc(_xData, _yData);
|
|
var lineFunc = Fit.PolynomialFunc(_xData, _yData, 3);
|
|
var estimate = lineFunc(leafInput.InputFiles.Count);
|
|
return ConvertToTimeSpan(estimate);
|
|
}
|
|
|
|
private static double ConvertFromTimeSpan(TimeSpan timeSpan)
|
|
{
|
|
return timeSpan.TotalSeconds;
|
|
}
|
|
|
|
private static TimeSpan ConvertToTimeSpan(double estimate)
|
|
{
|
|
int int32;
|
|
try
|
|
{
|
|
int32 = Convert.ToInt32(estimate);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
int32 = int.MaxValue;
|
|
}
|
|
return TimeSpan.FromSeconds(int32);
|
|
}
|
|
}
|
|
}
|