-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHashStats.cs
More file actions
82 lines (65 loc) · 2.08 KB
/
Copy pathHashStats.cs
File metadata and controls
82 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Herman Schoenfeld 2018 - Present. All rights reserved. (https://sphere10.com)
// Author: Herman Schoenfeld
//
// Distributed under the MIT software license, see the accompanying file
// LICENSE or visit http://www.opensource.org/licenses/mit-license.php.
//
// This notice must not be removed when duplicating this file or its contents, in whole or in part.
using System;
using System.Collections.Generic;
using Sphere10.Framework;
namespace Sphere10.Framework.Consensus;
public class PeriodicStatistics {
private IList<Statistics> _history;
private Statistics _currentPeriodStats;
private TimeSpan _period;
private bool _started;
private DateTime _startedOn;
public PeriodicStatistics(TimeSpan period, int historyLength)
: this(period, historyLength, new List<Statistics>()) {
}
public PeriodicStatistics(TimeSpan period, int historyLength, IList<Statistics> store) {
Guard.ArgumentNotNull(store, nameof(store));
Guard.Argument(period > TimeSpan.Zero, nameof(period), "Period must be positive.");
_period = period;
_currentPeriodStats = new Statistics();
_history = store;
_started = false;
}
public DateTime StartedOn {
get {
CheckStarted();
return _startedOn;
}
}
public int PeriodsAvailable {
get {
CheckStarted();
return (int)Math.Ceiling((DateTime.UtcNow - _startedOn).TotalSeconds / _period.TotalSeconds);
}
}
public void Start() {
CheckNotStarted();
_startedOn = DateTime.UtcNow;
_started = true;
}
public void RegisterEvent(double magnitude)
=> RegisterEvent(magnitude, 1);
public void RegisterEvent(double magnitude, int occurances) {
CheckStarted();
_currentPeriodStats.AddDatum(magnitude);
}
private void EnsurePeriodFresh() {
var now = DateTime.UtcNow;
var currentPeriodIndex = _history.Count;
var currentPeriodStart = _startedOn + _period * currentPeriodIndex;
var currentPeriodEnd = currentPeriodStart + _period;
var nowIndex = (now - _startedOn);
}
private void CheckNotStarted() {
Guard.Ensure(!_started, "Already started");
}
private void CheckStarted() {
Guard.Ensure(_started, "Not started");
}
}