-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#115 chore: traffic drivers as plugins
+ implemented json traffic driver
- Loading branch information
1 parent
866bea4
commit f2b776e
Showing
69 changed files
with
1,356 additions
and
201 deletions.
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
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,15 @@ | ||
using Core.Test.Stubs; | ||
using Linguard.Core.Plugins; | ||
using Moq; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public class PluginEngineMock : Mock<IPluginEngine> { | ||
public PluginEngineMock() { | ||
SetupGet(p => p.Plugins).Returns(new[] { | ||
new TrafficStorageDriverStub(), | ||
new TrafficStorageDriverMock("Mock driver", "This is a mocked driver").Object, | ||
new TrafficStorageDriverMock("Mock driver 2", "This is another mocked driver").Object | ||
}); | ||
} | ||
} |
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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using Moq; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public class TrafficStorageDriverMock : Mock<ITrafficStorageDriver> { | ||
public TrafficStorageDriverMock(string name, string description) { | ||
SetupGet(d => d.Name).Returns(name); | ||
SetupGet(d => d.Description).Returns(description); | ||
SetupProperty(d => d.CollectionInterval, TimeSpan.FromHours(1)); | ||
SetupProperty(d => d.AdditionalOptions, new Dictionary<string, string>()); | ||
} | ||
} |
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,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Bogus; | ||
using ByteSizeLib; | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using Linguard.Core.Managers; | ||
using Linguard.Core.Models; | ||
|
||
|
||
namespace Core.Test.Stubs; | ||
|
||
public class TrafficStorageDriverStub : ITrafficStorageDriver { | ||
|
||
public string Name => "Stub driver"; | ||
public string Description => "This is a stub driver"; | ||
private IConfigurationManager _configurationManager; | ||
private readonly Faker _faker = new(); | ||
public void Initialize(IConfigurationManager configurationManager) { | ||
_configurationManager = configurationManager; | ||
} | ||
|
||
public TimeSpan CollectionInterval { get; set; } = TimeSpan.FromHours(1); | ||
|
||
public IDictionary<string, string> AdditionalOptions { get; set; } = new Dictionary<string, string> { | ||
{ "Fake", "Option" } | ||
}; | ||
public void Save(IEnumerable<ITrafficData> data) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public IEnumerable<ITrafficData> Load() { | ||
var interfaces = _configurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces; | ||
var data = new List<ITrafficData>(); | ||
var entries = (int) TimeSpan.FromDays(2).TotalHours; | ||
var timestampBase = DateTime.Now - TimeSpan.FromHours(entries); | ||
foreach (var iface in interfaces) { | ||
for (var i = 0; i < entries; i++) { | ||
data.Add(new TrafficData { | ||
Peer = iface, | ||
ReceivedData = ByteSize.FromBytes(_faker.Random.Number((int) ByteSize.BytesInMegaByte)), | ||
SentData = ByteSize.FromBytes(_faker.Random.Number((int) ByteSize.BytesInMegaByte)), | ||
TimeStamp = timestampBase + TimeSpan.FromHours(i) | ||
}); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
public object Clone() { | ||
return new TrafficStorageDriverStub { | ||
CollectionInterval = CollectionInterval, | ||
AdditionalOptions = new Dictionary<string, string>(AdditionalOptions) | ||
}; | ||
} | ||
} |
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
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,15 @@ | ||
namespace Linguard.Core.Annotations; | ||
|
||
/// <summary> | ||
/// Indicates that a property MAY have value. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class OptionalAttribute : Attribute { | ||
} | ||
|
||
/// <summary> | ||
/// Indicates that a property MUST have value. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class MandatoryAttribute : Attribute { | ||
} |
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
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
18 changes: 14 additions & 4 deletions
18
Linguard/Core/Drivers/TrafficStorage/ITrafficStorageDriver.cs
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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
using Linguard.Core.Models; | ||
using Linguard.Core.Plugins; | ||
|
||
namespace Linguard.Core.Drivers.TrafficStorage; | ||
|
||
public interface ITrafficStorageDriver { | ||
public string TimestampFormat { get; set; } | ||
public void Save(IEnumerable<ITrafficData> data); | ||
public IEnumerable<ITrafficData> Load(); | ||
public interface ITrafficStorageDriver : IPlugin { | ||
TimeSpan CollectionInterval { get; set; } | ||
IDictionary<string, string> AdditionalOptions { get; set; } | ||
/// <summary> | ||
/// Store the given traffic data. | ||
/// </summary> | ||
/// <param name="data"></param> | ||
void Save(IEnumerable<ITrafficData> data); | ||
/// <summary> | ||
/// Load all the stored traffic data. | ||
/// </summary> | ||
/// <returns></returns> | ||
IEnumerable<ITrafficData> Load(); | ||
} |
15 changes: 0 additions & 15 deletions
15
Linguard/Core/Drivers/TrafficStorage/JsonTrafficStorageDriver.cs
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
Linguard/Core/Drivers/TrafficStorage/TrafficStorageDriverBase.cs
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,19 @@ | ||
using Linguard.Core.Managers; | ||
using Linguard.Core.Models; | ||
|
||
namespace Linguard.Core.Drivers.TrafficStorage; | ||
|
||
public abstract class TrafficStorageDriverBase : ITrafficStorageDriver { | ||
public abstract string Name { get; } | ||
public abstract string Description { get; } | ||
public virtual TimeSpan CollectionInterval { get; set; } = TimeSpan.FromHours(1); | ||
public IDictionary<string, string> AdditionalOptions { get; set; } = new Dictionary<string, string>(); | ||
protected IConfigurationManager? ConfigurationManager { get; private set; } | ||
|
||
public void Initialize(IConfigurationManager configurationManager) { | ||
ConfigurationManager = configurationManager; | ||
} | ||
public abstract void Save(IEnumerable<ITrafficData> data); | ||
public abstract IEnumerable<ITrafficData> Load(); | ||
public abstract object Clone(); | ||
} |
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
Oops, something went wrong.