Skip to content

Commit 221ac33

Browse files
Introduce a new experimental time estimation feature
1 parent 06f1782 commit 221ac33

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

Src/FinderOuter/Models/Report.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ namespace FinderOuter.Models
1313
{
1414
public class Report : ReactiveObject, IReport
1515
{
16-
public Report()
16+
public Report() : this(Dispatcher.UIThread)
1717
{
18-
UIThread = Dispatcher.UIThread;
1918
}
2019

2120
public Report(IDispatcher dispatcher)
2221
{
2322
UIThread = dispatcher;
23+
24+
updateTimer = new System.Timers.Timer(TimeSpan.FromSeconds(3).TotalMilliseconds);
25+
updateTimer.Elapsed += UpdateTimer_Elapsed;
2426
}
2527

2628

@@ -66,7 +68,7 @@ public void SetTotal(BigInteger value)
6668
AddMessageSafe($"Total number of permutations to check: {Total:n0}");
6769
}
6870

69-
public void SetTotal(int value, int exponent)
71+
public void SetTotal(int value, int exponent)
7072
{
7173
SetTotal(BigInteger.Pow(value, exponent));
7274
}
@@ -81,6 +83,11 @@ public void Init()
8183
IsProgressVisible = false;
8284
Timer.Reset();
8385
Total = BigInteger.Zero;
86+
87+
Speed = 0;
88+
TotalChecked = 0;
89+
Remaining = TimeSpan.Zero;
90+
updateTimer.Stop();
8491
}
8592

8693
public bool Finalize(bool success)
@@ -108,6 +115,7 @@ public bool Finalize()
108115

109116
CurrentState = FoundAnyResult ? State.FinishedSuccess : State.FinishedFail;
110117
Progress = 100;
118+
updateTimer.Stop();
111119
return FoundAnyResult;
112120
}
113121

@@ -159,6 +167,7 @@ public void SetProgressStep(int splitSize)
159167
AddMessageSafe("Running in parallel.");
160168
percent = (double)100 / splitSize;
161169
_ = UIThread.InvokeAsync(() => IsProgressVisible = true);
170+
updateTimer.Start();
162171
}
163172

164173
private readonly object lockObj = new();
@@ -169,5 +178,46 @@ public void IncrementProgress()
169178
Progress += percent;
170179
}
171180
}
181+
182+
private double _speed;
183+
public double Speed
184+
{
185+
get => _speed;
186+
set => this.RaiseAndSetIfChanged(ref _speed, value);
187+
}
188+
189+
private double _totCh;
190+
public double TotalChecked
191+
{
192+
get => _totCh;
193+
set => this.RaiseAndSetIfChanged(ref _totCh, value);
194+
}
195+
196+
private TimeSpan _rem;
197+
public TimeSpan Remaining
198+
{
199+
get => _rem;
200+
set => this.RaiseAndSetIfChanged(ref _rem, value);
201+
}
202+
203+
204+
private readonly System.Timers.Timer updateTimer;
205+
206+
private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
207+
{
208+
if (Progress is > 0 and <= 100)
209+
{
210+
TotalChecked = (double)Total * Progress / 100;
211+
double time = Timer.Elapsed.TotalSeconds;
212+
if (time != 0 && TotalChecked != 0)
213+
{
214+
Speed = TotalChecked / time;
215+
double remKeys = (double)Total - TotalChecked;
216+
Debug.Assert(remKeys >= 0);
217+
double d = remKeys / Speed;
218+
Remaining = TimeSpan.FromSeconds(d);
219+
}
220+
}
221+
}
172222
}
173223
}

0 commit comments

Comments
 (0)