From abb91f7bafff38b960d2986e985ffc49fcf31be9 Mon Sep 17 00:00:00 2001 From: amki Date: Fri, 5 Feb 2021 02:54:12 +0100 Subject: [PATCH] Fix PingItems being saved correctly after AddHost --- ISPChk/Controllers/HostController.cs | 21 +++++++-- ISPChk/Models/Host.cs | 4 +- ISPChk/Models/ISPChkContext.cs | 1 - ISPChk/Models/PingItem.cs | 7 ++- ISPChk/NetworkTest.cs | 67 ++++++++++++++++++++------- ISPChk/Startup.cs | 1 + ISPChk/ispchk.db | Bin 16384 -> 20480 bytes ISPChk/ispchk.db-shm | Bin 0 -> 32768 bytes ISPChk/ispchk.db-wal | Bin 0 -> 24752 bytes ISPChk/wwwroot/index.html | 2 +- ISPChk/wwwroot/ispchk.js | 15 ++++-- 11 files changed, 90 insertions(+), 28 deletions(-) create mode 100644 ISPChk/ispchk.db-shm create mode 100644 ISPChk/ispchk.db-wal 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 6d22f6aa0cdfb3d0f6c1e11197f8ea3ef4b02f80..854669c821d6faa69be3289b71511621ce177f3a 100644 GIT binary patch delta 521 zcmZo@U~E{xI6q^mC+gVnu<-IS@cZ!n;w|9i-Pkyjr#_K|UEENXv5_$|FD13Y zGa^19GcVn}-lz)1mfOe~_!EyPtx$Yb2736oOpcT!UQwoLz$z zz|Jg2b}z^yF0MYVK;Jq$20J^txbkvo1~9RSYilz`g1t~&m@}D=-^)Vp2|O zKxSULXGv;qF`O~EpI4l_SV=*HmrDT%lqMJOiA z#Nw2clr$k076x(o)PnqS12CbNFT%&dAgSn{pP!zSnhqxQlJj$gfc_IzEJ-acF*MaP zG}kjU)-w{~nfy*(jE#~1D+B-6&4LcE`2{%{nZ+4B@{3D~`FKIWApjPh7{CtzR}@fW diff --git a/ISPChk/ispchk.db-shm b/ISPChk/ispchk.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..a5c1b7d7a7d93045efabae69841ce8776af212be GIT binary patch literal 32768 zcmeI)yA8rH5CBjo0cy&K1)yOI`jqT~7=abiu>%u8lLFB&06jB+9ib?#OQLtu*_OZZ z8{q2KHl!$H79lJbv7g6W-J9;Zd9>xTYIp17<#0ajZ_T@_Gxx`<`lw?28O3jU=+gQu zW_o)(9yS;dAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0Rj^cDEfLs6ZnWP5XcrN`$}8c z#~dIq0f8!{I(LEq0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF S5FkK+009C72oNCf4+3BNY9})Q literal 0 HcmV?d00001 diff --git a/ISPChk/ispchk.db-wal b/ISPChk/ispchk.db-wal new file mode 100644 index 0000000000000000000000000000000000000000..32d8cc9f86a3207f0f861b9b421969b5adbe3401 GIT binary patch literal 24752 zcmeI(zb^w}9LMqJ?m?Z9YgD6=XbhD$l2-5fyRirZ25d$#G-5!c-AIJppU}Z%kr)hO zGMNlmNEZn~#Asm9G@h$eNEkb`-rwY&CwcM=_kBIvH~FMC+qOmc&L@OhIHh07;&^c5 zXzjV=y`8?Us3ZCPyxe`W5ZarHbUAm;Ct<#-g+c%U1Q0*~0R#|0009ILKmdV;7U@_A<^M$->s>NxnHZK@@$ldpzJgtAu1rCK-G7lTNaef{F1Q0*~ z0R#|0009ILKmdW}5(wxnF{){rVMlE%9JRyI#6Z-_CSuuGJYuJlnWWX@^A2h*G4|^% zI~hsDtxUXnP0_SEUu`bn8TRi7bNXY4I@HZKVV2cGA%Fk^2q1s}0tg_000IagfIu?| zv`9nKr8ErH)Q=p&@%zPyJFx2~N6<{qO)m%_fB*srAbm$M~*=Log*-| zD(?LJ+<`L}kmgP`N1!$e0R#|0009ILKmY**5I_I{1nMmikbflv;#MS+$fVM?$LFnU yj^J$Oysvl}I(2dc(tN4r2-HR)fB*srAb
- 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();