Fix PingItems being saved correctly after AddHost
This commit is contained in:
parent
4cd0375a52
commit
abb91f7baf
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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.
|
||||||
@ -45,22 +55,47 @@ namespace ISPChk
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// Send the request.
|
long min = timeout+1;
|
||||||
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
|
long max = 0;
|
||||||
|
long avg = 0;
|
||||||
|
int successes = 0;
|
||||||
|
for(var i=0;i<5;++i)
|
||||||
|
{
|
||||||
|
// Send the request.
|
||||||
|
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
|
||||||
|
|
||||||
if (reply.Status == IPStatus.Success)
|
if (reply.Status == IPStatus.Success)
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine("Reply from "+reply.Address.ToString()+
|
successes++;
|
||||||
": bytes="+reply.Buffer.Length+
|
System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() +
|
||||||
" time="+ reply.RoundtripTime+"ms");
|
": bytes=" + reply.Buffer.Length +
|
||||||
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
|
" time=" + reply.RoundtripTime + "ms");
|
||||||
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
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("Don't fragment: {0}", reply.Options.DontFragment);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine(reply.Status);
|
||||||
|
}
|
||||||
|
Thread.Sleep(500);
|
||||||
}
|
}
|
||||||
else
|
avg = avg / successes;
|
||||||
{
|
System.Diagnostics.Debug.WriteLine("min:" + min + " max:" + max + " avg:" + avg);
|
||||||
System.Diagnostics.Debug.WriteLine(reply.Status);
|
PingItem pi = new PingItem { Date = DateTime.UtcNow, Min = min, Max = max, Avg = avg };
|
||||||
}
|
if(host.PingItems == null)
|
||||||
Thread.Sleep(1000);
|
host = _context.Hosts.Include(host => host.PingItems).Where(h => h.HostId == host.HostId).Single();
|
||||||
|
host.PingItems.Add(pi);
|
||||||
|
_context.SaveChanges();
|
||||||
|
Thread.Sleep(5000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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
BIN
ISPChk/ispchk.db
Binary file not shown.
BIN
ISPChk/ispchk.db-shm
Normal file
BIN
ISPChk/ispchk.db-shm
Normal file
Binary file not shown.
BIN
ISPChk/ispchk.db-wal
Normal file
BIN
ISPChk/ispchk.db-wal
Normal file
Binary file not shown.
@ -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
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user