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; | ||||
|         } | ||||
| 
 | ||||
|         // 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 | ||||
|         // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||||
|         [HttpPut("{id}")] | ||||
|         public async Task<IActionResult> 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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<PingItem> PingItems { get; set; } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -14,6 +14,5 @@ namespace ISPChk.Models | ||||
|         } | ||||
| 
 | ||||
|         public DbSet<Host> Hosts { get; set; } | ||||
|         public DbSet<PingItem> PingItems { get; set; } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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; } | ||||
|     } | ||||
| } | ||||
| @ -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<Host> 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); | ||||
|                     long min = timeout+1; | ||||
|                     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) | ||||
|                     { | ||||
|                         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); | ||||
|                         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); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         System.Diagnostics.Debug.WriteLine(reply.Status); | ||||
|                     } | ||||
|                     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); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|  | ||||
| @ -31,6 +31,7 @@ namespace ISPChk | ||||
|                                                opt.UseSqlite("Data Source=ispchk.db")); | ||||
|             services.AddControllers(); | ||||
|             var networkTest = new NetworkTest(); | ||||
|             networkTest.LoadHostsFromDatabase(); | ||||
|             services.AddSingleton<INetworkTest>(networkTest); | ||||
|             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="row"> | ||||
|             <div class="col-sm"> | ||||
|                 One of three columns | ||||
|                 <button id="btnDebug">DEBUG</button> | ||||
|             </div> | ||||
|             <div class="col-sm"> | ||||
|                 Hosts | ||||
|  | ||||
| @ -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 = $("<tr>",).appendTo(tbody); | ||||
|             $("<td>").text(host.id).appendTo(row); | ||||
|             $("<td>").text(host.hostId).appendTo(row); | ||||
|             $("<td>").text(host.name).appendTo(row); | ||||
|             $("<td>").text(host.hostName).appendTo(row); | ||||
|             $("<td>").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(); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user