diff --git a/ISPChk/Controllers/HostController.cs b/ISPChk/Controllers/HostController.cs index e2722d9..79d1b3b 100644 --- a/ISPChk/Controllers/HostController.cs +++ b/ISPChk/Controllers/HostController.cs @@ -43,12 +43,27 @@ namespace ISPChk.Controllers return host; } + // GET: api/Host/5 + [HttpGet("{id}/pings/{start}/{end?}")] + public async Task> 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 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPut("{id}")] public async Task PutHost(long id, Host host) { - if (id != host.Id) + if (id != host.HostId) { return BadRequest(); } @@ -84,7 +99,7 @@ namespace ISPChk.Controllers _networkTest.AddHost(host); - return CreatedAtAction("GetHost", new { id = host.Id }, host); + return CreatedAtAction("GetHost", new { id = host.HostId }, host); } // DELETE: api/Host/5 @@ -105,7 +120,7 @@ namespace ISPChk.Controllers private bool HostExists(long id) { - return _context.Hosts.Any(e => e.Id == id); + return _context.Hosts.Any(e => e.HostId == id); } } } diff --git a/ISPChk/Models/Host.cs b/ISPChk/Models/Host.cs index 6c33caf..c99d34a 100644 --- a/ISPChk/Models/Host.cs +++ b/ISPChk/Models/Host.cs @@ -7,8 +7,10 @@ namespace ISPChk.Models { public class Host { - public long Id { get; set; } + public long HostId { get; set; } public string Name { get; set; } public string HostName { get; set; } + + public List PingItems { get; set; } } } diff --git a/ISPChk/Models/ISPChkContext.cs b/ISPChk/Models/ISPChkContext.cs index 3b6f147..5f3ecd1 100644 --- a/ISPChk/Models/ISPChkContext.cs +++ b/ISPChk/Models/ISPChkContext.cs @@ -14,6 +14,5 @@ namespace ISPChk.Models } public DbSet Hosts { get; set; } - public DbSet PingItems { get; set; } } } diff --git a/ISPChk/Models/PingItem.cs b/ISPChk/Models/PingItem.cs index e237c7f..46238bf 100644 --- a/ISPChk/Models/PingItem.cs +++ b/ISPChk/Models/PingItem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -7,11 +8,13 @@ namespace ISPChk.Models { public class PingItem { - public long Id { get; set; } - public long HostId { get; set; } + [Key] + public long PingId { get; set; } public DateTime Date { get; set; } public float Min { get; set; } public float Max { get; set; } public float Avg { get; set; } + public long HostId { get; set; } + public Host Host { get; set; } } } \ No newline at end of file diff --git a/ISPChk/NetworkTest.cs b/ISPChk/NetworkTest.cs index 1941c68..78f0310 100644 --- a/ISPChk/NetworkTest.cs +++ b/ISPChk/NetworkTest.cs @@ -19,17 +19,27 @@ namespace ISPChk optionsBuilder.UseSqlite("Data Source=ispchk.db"); _context = new ISPChkContext(optionsBuilder.Options); System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); + _context.Database.EnsureCreated(); + } + + public async void LoadHostsFromDatabase() + { + List hosts = await _context.Hosts.Include(host => host.PingItems).ToListAsync(); + foreach (Host host in hosts) + { + AddHost(host); + } } public void AddHost(Host host) { System.Diagnostics.Debug.WriteLine("Host added!"); - var t = Task.Run(() => { + var t = Task.Run(async () => { Ping pingSender = new Ping(); // Create a buffer of 32 bytes of data to be transmitted. - string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte[] buffer = Encoding.ASCII.GetBytes(data); // Wait 10 seconds for a reply. @@ -45,22 +55,47 @@ namespace ISPChk while (true) { - // Send the request. - PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); - - if (reply.Status == IPStatus.Success) + long min = timeout+1; + long max = 0; + long avg = 0; + int successes = 0; + for(var i=0;i<5;++i) { - System.Diagnostics.Debug.WriteLine("Reply from "+reply.Address.ToString()+ - ": bytes="+reply.Buffer.Length+ - " time="+ reply.RoundtripTime+"ms"); - //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); + // Send the request. + PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); + + if (reply.Status == IPStatus.Success) + { + successes++; + System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() + + ": bytes=" + reply.Buffer.Length + + " 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("Don't fragment: {0}", reply.Options.DontFragment); + } + else + { + System.Diagnostics.Debug.WriteLine(reply.Status); + } + Thread.Sleep(500); } - Thread.Sleep(1000); + 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); } }); } diff --git a/ISPChk/Startup.cs b/ISPChk/Startup.cs index 477aa27..3cbc5eb 100644 --- a/ISPChk/Startup.cs +++ b/ISPChk/Startup.cs @@ -31,6 +31,7 @@ namespace ISPChk opt.UseSqlite("Data Source=ispchk.db")); services.AddControllers(); var networkTest = new NetworkTest(); + networkTest.LoadHostsFromDatabase(); services.AddSingleton(networkTest); services.AddSwaggerGen(c => { diff --git a/ISPChk/ispchk.db b/ISPChk/ispchk.db index 6d22f6a..854669c 100644 Binary files a/ISPChk/ispchk.db and b/ISPChk/ispchk.db differ diff --git a/ISPChk/ispchk.db-shm b/ISPChk/ispchk.db-shm new file mode 100644 index 0000000..a5c1b7d Binary files /dev/null and b/ISPChk/ispchk.db-shm differ diff --git a/ISPChk/ispchk.db-wal b/ISPChk/ispchk.db-wal new file mode 100644 index 0000000..32d8cc9 Binary files /dev/null and b/ISPChk/ispchk.db-wal differ diff --git a/ISPChk/wwwroot/index.html b/ISPChk/wwwroot/index.html index eee7ae7..f7bd2e9 100644 --- a/ISPChk/wwwroot/index.html +++ b/ISPChk/wwwroot/index.html @@ -10,7 +10,7 @@
- One of three columns +
Hosts diff --git a/ISPChk/wwwroot/ispchk.js b/ISPChk/wwwroot/ispchk.js index 479cf9e..b23783f 100644 --- a/ISPChk/wwwroot/ispchk.js +++ b/ISPChk/wwwroot/ispchk.js @@ -9,9 +9,9 @@ function createHost(host) { }); } -function deleteHost(id) { +function deleteHost(hostId) { $.ajax({ - url: "/api/host/"+id, + url: "/api/host/"+hostId, type: "DELETE" }).then(() => { console.log("DELETED"); @@ -26,16 +26,23 @@ function fetchHosts() { console.log("Got hosts: ", res); for (let host of res) { var row = $("",).appendTo(tbody); - $("").text(host.id).appendTo(row); + $("").text(host.hostId).appendTo(row); $("").text(host.name).appendTo(row); $("").text(host.hostName).appendTo(row); $("").text("Delete").click(() => { - deleteHost(host.id); + deleteHost(host.hostId); }).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 () { var name = $("#inpNameAdd").val(); var hostname = $("#inpHostNameAdd").val();