Lesson 01: add host engine and core domain models

This commit is contained in:
2025-11-28 23:33:51 -06:00
parent 570e8c3082
commit 0d472dccb1
4 changed files with 92 additions and 13 deletions

View File

@@ -0,0 +1,34 @@
namespace NetPulse.Core.Domain;
///<summary>
/// Result of a ping check against a Ping Target.
/// </summary>
public class PingResult
{
public Guid Id { get; init; } = Guid.NewGuid();
/// <summary>
/// The Target this result belongs to.
/// </summary>
public Guid TargetId { get; init; }
/// <summary>
/// When the check was performed (UTC).
/// </summary>
public DateTime TimeStampUtc { get; init; }
/// <summary>
/// If the ping succeeded.
/// </summary>
public bool Success { get; init; }
/// <summary>
/// Round-trip in milliseconds, if check succeeded.
/// </summary>
public double? LatencyMs { get; init; }
/// <summary>
/// Error detail if the pin failed.
/// </summary>
public string? ErrorMessage { get; init; }
}

View File

@@ -0,0 +1,27 @@
namespace NetPulse.Core.Domain;
/// <summary>
/// A host or service that NetPulse should Monitor
/// </summary>
public class PingTarget
{
/// <summary>
/// Internal Identifier (map to db key later)
/// </summary>
public Guid Id { get; init; } = Guid.NewGuid();
/// <summary>
/// Display name for the target
/// </summary>
public string Name { get; init; } = string.Empty;
/// <summary>
/// Hostname or Ip address to ping.
/// </summary>
public string Host { get; init; } = string.Empty;
/// <summary>
/// Whether this target is currently enabled for monitoring.
/// </summary>
public bool isEnabled { get; init; } = true;
}

View File

@@ -1,5 +1,6 @@
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NetPulse.Core.Domain;
namespace NetPulse.Core; namespace NetPulse.Core;
@@ -8,6 +9,12 @@ public class EngineLoop : BackgroundService
private readonly ILogger<EngineLoop> _logger; private readonly ILogger<EngineLoop> _logger;
private readonly TimeSpan _interval = TimeSpan.FromSeconds(2); private readonly TimeSpan _interval = TimeSpan.FromSeconds(2);
private readonly List<PingTarget> _targets = new()
{
new PingTarget { Name = "Router", Host = "192.168.0.1" },
new PingTarget { Name = "Cloudflare", Host = "1.1.1.1"}
};
public EngineLoop(ILogger<EngineLoop> logger) public EngineLoop(ILogger<EngineLoop> logger)
{ {
_logger = logger; _logger = logger;
@@ -21,12 +28,22 @@ public class EngineLoop : BackgroundService
{ {
try try
{ {
// ===== Core Heartbeat =====
_logger.LogInformation("Hearbeat at {time}", DateTime.UtcNow);
// Future: metrics Collection // ===== Core Heartbeat =====
// Future: dispatch events _logger.LogInformation("Heartbeat at {time}", DateTime.UtcNow);
// Future: run modules
foreach (var target in _targets)
{
_logger.LogInformation(
"Target {name} at {host} is configured (enabled = {enabled})",
target.Name,
target.Host,
target.isEnabled);
}
/// Future: metrics Collection
/// Future: dispatch events
/// Future: run modules
await Task.Delay(_interval, stoppingToken); await Task.Delay(_interval, stoppingToken);
} }
@@ -36,10 +53,10 @@ public class EngineLoop : BackgroundService
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Unexpected Engine error."); _logger.LogError(ex, "Unexpected engine error.");
} }
} }
_logger.LogInformation("EngineLoop Stopping"); _logger.LogInformation("EngineLoop stopping.");
} }
} }

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
namespace NetPulse.Core; namespace NetPulse.Core;
@@ -18,16 +19,16 @@ public class Program
logging.ClearProviders(); logging.ClearProviders();
logging.AddSimpleConsole(options => logging.AddSimpleConsole(options =>
{ {
options.TimestampFormat = "HH:MM:SS"; options.TimestampFormat = "HH:mm:ss ";
options.IncludeScopes = true; options.IncludeScopes = true;
}); });
}) })
.ConfigureServices((context, services) => .ConfigureServices((context, services) =>
{ {
services.AddHostedService<EngineLoop>(); services.AddHostedService<EngineLoop>();
}) })
.Build(); .Build();
await host.RunAsync(); await host.RunAsync();
} }
} }