Fix recording when timeouted

This commit is contained in:
amki 2021-02-05 07:32:29 +01:00
parent 56a412108f
commit 4a12f1361e
6 changed files with 42 additions and 11 deletions

View File

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "5.0.2",
"commands": [
"dotnet-ef"
]
}
}
}

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>0f3cc2c5-5728-4cf6-b618-bda6124ebd51</UserSecretsId>
</PropertyGroup>
<ItemGroup>

View File

@ -15,11 +15,7 @@ namespace ISPChk
private ISPChkContext _context;
public NetworkTest() {
var optionsBuilder = new DbContextOptionsBuilder<ISPChkContext>();
optionsBuilder.UseSqlite("Data Source=ispchk.db");
_context = new ISPChkContext(optionsBuilder.Options);
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
_context.Database.EnsureCreated();
_context = CreateDbContext();
}
public async void LoadHostsFromDatabase()
@ -31,10 +27,25 @@ namespace ISPChk
}
}
private ISPChkContext CreateDbContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ISPChkContext>();
optionsBuilder.UseSqlite("Data Source=ispchk.db");
var ctx = new ISPChkContext(optionsBuilder.Options);
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
ctx.Database.EnsureCreated();
return ctx;
}
private void StartPing(Host host)
{
var t = Task.Run(async () => {
// Do not use the outer context, not threadsafe?
var ctx = CreateDbContext();
var h = await ctx.Hosts.FindAsync(host.HostId);
ctx.Entry(h).Collection(hst => hst.PingItems).Load();
Ping pingSender = new Ping();
// Create a buffer of 32 bytes of data to be transmitted.
@ -42,7 +53,7 @@ namespace ISPChk
byte[] buffer = Encoding.ASCII.GetBytes(data);
// Wait 10 seconds for a reply.
int timeout = 5000;
int timeout = 3000;
// Set options for transmission:
// The data can go through 64 gateways or routers
@ -54,12 +65,13 @@ namespace ISPChk
while (true)
{
int numPings = 5;
long min = timeout + 1;
long max = 0;
long avg = 0;
int successes = 0;
int failures = 0;
for (var i = 0; i < 5; ++i)
for (var i = 0; i < numPings; ++i)
{
// Send the request.
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
@ -89,12 +101,18 @@ namespace ISPChk
}
Thread.Sleep(500);
}
avg = avg / successes;
if(successes > 0)
avg = avg / successes;
// FIXME: This is dirty but I don't want the graph to explode
if (min > timeout)
min = 0;
System.Diagnostics.Debug.WriteLine("min:" + min + " max:" + max + " avg:" + avg);
PingItem pi = new PingItem { Date = DateTimeOffset.UtcNow, Min = min, Max = max, Avg = avg, Failures = failures };
host.PingItems.Add(pi);
_context.SaveChanges();
Thread.Sleep(5000);
h.PingItems.Add(pi);
ctx.SaveChanges();
// Throttle if everything is ok
if(successes == numPings)
Thread.Sleep(5000);
}
});
}

Binary file not shown.

Binary file not shown.

Binary file not shown.