Browse Source

Fix PingItems being saved correctly after AddHost

master
amki 4 years ago
parent
commit
abb91f7baf
  1. 21
      ISPChk/Controllers/HostController.cs
  2. 4
      ISPChk/Models/Host.cs
  3. 1
      ISPChk/Models/ISPChkContext.cs
  4. 7
      ISPChk/Models/PingItem.cs
  5. 41
      ISPChk/NetworkTest.cs
  6. 1
      ISPChk/Startup.cs
  7. BIN
      ISPChk/ispchk.db
  8. BIN
      ISPChk/ispchk.db-shm
  9. BIN
      ISPChk/ispchk.db-wal
  10. 2
      ISPChk/wwwroot/index.html
  11. 15
      ISPChk/wwwroot/ispchk.js

21
ISPChk/Controllers/HostController.cs

@ -43,12 +43,27 @@ namespace ISPChk.Controllers
return host; return host;
} }
// GET: api/Host/5
[HttpGet("{id}/pings/{start}/{end?}")]
public async Task<ActionResult<Host>> GetPingsByTimeSpan(long id, long start, long end)
{
System.Diagnostics.Debug.WriteLine("called");
var host = await _context.Hosts.FindAsync(id);
if (host == null)
{
return NotFound();
}
return host;
}
// PUT: api/Host/5 // PUT: api/Host/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> PutHost(long id, Host host) public async Task<IActionResult> PutHost(long id, Host host)
{ {
if (id != host.Id) if (id != host.HostId)
{ {
return BadRequest(); return BadRequest();
} }
@ -84,7 +99,7 @@ namespace ISPChk.Controllers
_networkTest.AddHost(host); _networkTest.AddHost(host);
return CreatedAtAction("GetHost", new { id = host.Id }, host); return CreatedAtAction("GetHost", new { id = host.HostId }, host);
} }
// DELETE: api/Host/5 // DELETE: api/Host/5
@ -105,7 +120,7 @@ namespace ISPChk.Controllers
private bool HostExists(long id) private bool HostExists(long id)
{ {
return _context.Hosts.Any(e => e.Id == id); return _context.Hosts.Any(e => e.HostId == id);
} }
} }
} }

4
ISPChk/Models/Host.cs

@ -7,8 +7,10 @@ namespace ISPChk.Models
{ {
public class Host public class Host
{ {
public long Id { get; set; } public long HostId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string HostName { get; set; } public string HostName { get; set; }
public List<PingItem> PingItems { get; set; }
} }
} }

1
ISPChk/Models/ISPChkContext.cs

@ -14,6 +14,5 @@ namespace ISPChk.Models
} }
public DbSet<Host> Hosts { get; set; } public DbSet<Host> Hosts { get; set; }
public DbSet<PingItem> PingItems { get; set; }
} }
} }

7
ISPChk/Models/PingItem.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,11 +8,13 @@ namespace ISPChk.Models
{ {
public class PingItem public class PingItem
{ {
public long Id { get; set; } [Key]
public long HostId { get; set; } public long PingId { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
public float Min { get; set; } public float Min { get; set; }
public float Max { get; set; } public float Max { get; set; }
public float Avg { get; set; } public float Avg { get; set; }
public long HostId { get; set; }
public Host Host { get; set; }
} }
} }

41
ISPChk/NetworkTest.cs

@ -19,17 +19,27 @@ namespace ISPChk
optionsBuilder.UseSqlite("Data Source=ispchk.db"); optionsBuilder.UseSqlite("Data Source=ispchk.db");
_context = new ISPChkContext(optionsBuilder.Options); _context = new ISPChkContext(optionsBuilder.Options);
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
_context.Database.EnsureCreated();
}
public async void LoadHostsFromDatabase()
{
List<Host> hosts = await _context.Hosts.Include(host => host.PingItems).ToListAsync();
foreach (Host host in hosts)
{
AddHost(host);
}
} }
public void AddHost(Host host) public void AddHost(Host host)
{ {
System.Diagnostics.Debug.WriteLine("Host added!"); System.Diagnostics.Debug.WriteLine("Host added!");
var t = Task.Run(() => { var t = Task.Run(async () => {
Ping pingSender = new Ping(); Ping pingSender = new Ping();
// Create a buffer of 32 bytes of data to be transmitted. // Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data); byte[] buffer = Encoding.ASCII.GetBytes(data);
// Wait 10 seconds for a reply. // Wait 10 seconds for a reply.
@ -44,15 +54,31 @@ namespace ISPChk
System.Diagnostics.Debug.WriteLine("Pinging " + host.Name); System.Diagnostics.Debug.WriteLine("Pinging " + host.Name);
while (true) while (true)
{
long min = timeout+1;
long max = 0;
long avg = 0;
int successes = 0;
for(var i=0;i<5;++i)
{ {
// Send the request. // Send the request.
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
if (reply.Status == IPStatus.Success) if (reply.Status == IPStatus.Success)
{ {
successes++;
System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() + System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() +
": bytes=" + reply.Buffer.Length + ": bytes=" + reply.Buffer.Length +
" time=" + reply.RoundtripTime + "ms"); " time=" + reply.RoundtripTime + "ms");
avg += reply.RoundtripTime;
if(reply.RoundtripTime < min)
{
min = reply.RoundtripTime;
}
if(reply.RoundtripTime > max)
{
max = reply.RoundtripTime;
}
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl); //System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); //System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
} }
@ -60,7 +86,16 @@ namespace ISPChk
{ {
System.Diagnostics.Debug.WriteLine(reply.Status); System.Diagnostics.Debug.WriteLine(reply.Status);
} }
Thread.Sleep(1000); Thread.Sleep(500);
}
avg = avg / successes;
System.Diagnostics.Debug.WriteLine("min:" + min + " max:" + max + " avg:" + avg);
PingItem pi = new PingItem { Date = DateTime.UtcNow, Min = min, Max = max, Avg = avg };
if(host.PingItems == null)
host = _context.Hosts.Include(host => host.PingItems).Where(h => h.HostId == host.HostId).Single();
host.PingItems.Add(pi);
_context.SaveChanges();
Thread.Sleep(5000);
} }
}); });
} }

1
ISPChk/Startup.cs

@ -31,6 +31,7 @@ namespace ISPChk
opt.UseSqlite("Data Source=ispchk.db")); opt.UseSqlite("Data Source=ispchk.db"));
services.AddControllers(); services.AddControllers();
var networkTest = new NetworkTest(); var networkTest = new NetworkTest();
networkTest.LoadHostsFromDatabase();
services.AddSingleton<INetworkTest>(networkTest); services.AddSingleton<INetworkTest>(networkTest);
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {

BIN
ISPChk/ispchk.db

Binary file not shown.

BIN
ISPChk/ispchk.db-shm

Binary file not shown.

BIN
ISPChk/ispchk.db-wal

Binary file not shown.

2
ISPChk/wwwroot/index.html

@ -10,7 +10,7 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
One of three columns <button id="btnDebug">DEBUG</button>
</div> </div>
<div class="col-sm"> <div class="col-sm">
Hosts Hosts

15
ISPChk/wwwroot/ispchk.js

@ -9,9 +9,9 @@ function createHost(host) {
}); });
} }
function deleteHost(id) { function deleteHost(hostId) {
$.ajax({ $.ajax({
url: "/api/host/"+id, url: "/api/host/"+hostId,
type: "DELETE" type: "DELETE"
}).then(() => { }).then(() => {
console.log("DELETED"); console.log("DELETED");
@ -26,16 +26,23 @@ function fetchHosts() {
console.log("Got hosts: ", res); console.log("Got hosts: ", res);
for (let host of res) { for (let host of res) {
var row = $("<tr>",).appendTo(tbody); var row = $("<tr>",).appendTo(tbody);
$("<td>").text(host.id).appendTo(row); $("<td>").text(host.hostId).appendTo(row);
$("<td>").text(host.name).appendTo(row); $("<td>").text(host.name).appendTo(row);
$("<td>").text(host.hostName).appendTo(row); $("<td>").text(host.hostName).appendTo(row);
$("<td>").text("Delete").click(() => { $("<td>").text("Delete").click(() => {
deleteHost(host.id); deleteHost(host.hostId);
}).appendTo(row); }).appendTo(row);
} }
}); });
} }
$('#btnDebug').click(function () {
console.log("DEBUG");
$.getJSON("/api/host/" + id + "/pings/" + Date.now + "/" + Date.now).then((res) => {
console.log("CALLED: res ", res);
});
});
$('#btnHostAdd').click(function () { $('#btnHostAdd').click(function () {
var name = $("#inpNameAdd").val(); var name = $("#inpNameAdd").val();
var hostname = $("#inpHostNameAdd").val(); var hostname = $("#inpHostNameAdd").val();

Loading…
Cancel
Save