-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from HicServices/threshold-assoc
Threshold assoc
- Loading branch information
Showing
4 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using NUnit.Framework; | ||
using Rdmp.Dicom.Cache.Pipeline; | ||
using ReusableLibraryCode.Progress; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rdmp.Dicom.Tests | ||
{ | ||
class PressureGaugeTests | ||
{ | ||
[Test] | ||
public void TestGauge_NotReached() | ||
{ | ||
bool someFact = false; | ||
|
||
var g = new PressureGauge(); | ||
g.ThresholdBeatsPerMinute = 4; | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
} | ||
[Test] | ||
public void TestGauge_NotReached_OverTime() | ||
{ | ||
bool someFact = false; | ||
|
||
var g = new PressureGauge(); | ||
g.ThresholdBeatsPerMinute = 1; | ||
|
||
// events are 1 minute appart so does not trigger | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 02, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 03, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
} | ||
[Test] | ||
public void TestGauge_Reached() | ||
{ | ||
bool someFact = false; | ||
|
||
var g = new PressureGauge(); | ||
g.ThresholdBeatsPerMinute = 4; | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsTrue(someFact); | ||
} | ||
|
||
[Test] | ||
public void TestGauge_Reached_OverTime() | ||
{ | ||
bool someFact = false; | ||
|
||
var g = new PressureGauge(); | ||
g.ThresholdBeatsPerMinute = 1; | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 01), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 30), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsTrue(someFact); | ||
} | ||
[Test] | ||
public void TestGauge_Reached_OverTime_Boundary() | ||
{ | ||
bool someFact = false; | ||
|
||
var g = new PressureGauge(); | ||
g.ThresholdBeatsPerMinute = 1; | ||
g.Tick(new DateTime(2001, 01, 01, 01, 01, 30), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsFalse(someFact); | ||
g.Tick(new DateTime(2001, 01, 01, 01, 02, 29), new ThrowImmediatelyDataLoadEventListener(), () => someFact = true); | ||
Assert.IsTrue(someFact); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using ReusableLibraryCode.Progress; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Rdmp.Dicom.Cache.Pipeline | ||
{ | ||
/// <summary> | ||
/// Tracks unspecified events and performs a delegate action when the frequency exceeds the <see cref="ThresholdBeatsPerMinute"/> | ||
/// </summary> | ||
public class PressureGauge | ||
{ | ||
/// <summary> | ||
/// Number of events that are allowable per minute | ||
/// </summary> | ||
public long ThresholdBeatsPerMinute { get; set; } | ||
|
||
List<DateTime> collection = new List<DateTime>(); | ||
object oLock = new object(); | ||
|
||
public PressureGauge() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Marks that an event happened at the current date time | ||
/// </summary> | ||
/// <param name="listener"></param> | ||
/// <param name="pressureTooHigh">Delegate to invoke if <see cref="ThresholdBeatsPerMinute"/> is exceeded</param> | ||
public void Tick(IDataLoadEventListener listener, Action pressureTooHigh) | ||
{ | ||
Tick(DateTime.Now, listener, pressureTooHigh); | ||
} | ||
|
||
/// <summary> | ||
/// Marks that an event happened at <paramref name="eventDate"/> | ||
/// </summary> | ||
/// <param name="eventDate">Time of event. Must be greater than any previous event dates</param> | ||
/// <param name="listener"></param> | ||
/// <param name="pressureTooHigh">Delegate to invoke if <see cref="ThresholdBeatsPerMinute"/> is exceeded</param> | ||
public void Tick(DateTime eventDate, IDataLoadEventListener listener, Action pressureTooHigh) | ||
{ | ||
bool exceeded = false; | ||
lock (oLock) | ||
{ | ||
// filter collection to only recent events | ||
collection = collection.Where(c => eventDate.Subtract(c) < TimeSpan.FromMinutes(1)).ToList(); | ||
|
||
collection.Add(eventDate); | ||
|
||
exceeded = collection.Count > ThresholdBeatsPerMinute; | ||
} | ||
|
||
if(exceeded) | ||
{ | ||
// Important to use log level Information here and not Error in case the listener breaks flow control e.g. ThrowImmediately listener | ||
listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, "ThresholdBeatsPerMinute exceeded, invoking delegate")); | ||
pressureTooHigh(); | ||
} | ||
} | ||
} | ||
} |