file-watcher/Helpers/HelperCamstarOracle.cs
2024-10-22 09:48:28 -07:00

116 lines
5.7 KiB
C#

using File_Watcher.Infineon.Monitoring.MonA;
using File_Watcher.Models;
using System.Diagnostics;
using System.Globalization;
namespace File_Watcher.Helpers;
internal static partial class HelperCamstarOracle
{
private static IMonIn? _MonIn;
private static int? _LastValue;
private static Calendar? _Calendar;
private static DateTime? _LastUpload;
internal static void Heartbeat(AppSettings appSettings, IHttpClientFactory httpClientFactory, ILogger<Worker> logger, State state, CancellationToken cancellationToken)
{
_MonIn ??= MonIn.GetInstance(httpClientFactory);
CamstarOracleConfiguration camstarOracleConfiguration = appSettings.CamstarOracleConfiguration;
Task<HttpResponseMessage> httpResponseMessage = _MonIn.SendStatus(camstarOracleConfiguration.MonitorApplicationSite, camstarOracleConfiguration.MonitorApplicationResource, "Heartbeat", state);
httpResponseMessage.Wait(cancellationToken);
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
Task<string> body = httpResponseMessage.Result.Content.ReadAsStringAsync(cancellationToken);
body.Wait(cancellationToken);
logger.LogDebug(body.Result);
}
#pragma warning disable CA1416
private static List<EventLogEntry> GetOracleEventLogEntries(CamstarOracleConfiguration camstarOracleConfiguration, DateTime dateTime, CancellationToken cancellationToken)
{
List<EventLogEntry> results = [];
EventLog[] eventLogs = EventLog.GetEventLogs();
long ticks = dateTime.AddMinutes(-camstarOracleConfiguration.RollingMinutes).Ticks;
foreach (EventLog eventLog in eventLogs)
{
if (cancellationToken.IsCancellationRequested)
break;
if (!eventLog.Log.Contains(camstarOracleConfiguration.LogFilter))
continue;
foreach (object? item in eventLog.Entries)
{
if (cancellationToken.IsCancellationRequested)
break;
if (item is not EventLogEntry eventLogEntry)
continue;
if (eventLogEntry.TimeGenerated.Ticks < ticks)
continue;
if (!camstarOracleConfiguration.MessageFilters.Any(eventLogEntry.Message.Contains))
continue;
results.Add(eventLogEntry);
}
}
return results;
}
private static List<(DateTime, string)> GetOracleEventLogEntryMessages(CamstarOracleConfiguration camstarOracleConfiguration, DateTime dateTime, CancellationToken cancellationToken)
{
List<(DateTime, string)> results = [];
List<EventLogEntry> collection = GetOracleEventLogEntries(camstarOracleConfiguration, dateTime, cancellationToken);
foreach (EventLogEntry eventLogEntry in collection)
{
if (cancellationToken.IsCancellationRequested)
break;
results.Add(new(eventLogEntry.TimeGenerated, eventLogEntry.Message));
}
return results;
}
internal static bool Check(AppSettings appSettings, ILogger<Worker> logger, CancellationToken cancellationToken)
{
if (_MonIn is null)
throw new NullReferenceException(nameof(_MonIn));
string directory;
DateTime dateTime;
string weekOfYear;
string weekDirectory;
string formattedDateTime;
List<string> lines = [];
_Calendar ??= new CultureInfo("en-US").Calendar;
CamstarOracleConfiguration camstarOracleConfiguration = appSettings.CamstarOracleConfiguration;
string performanceName = string.Concat(camstarOracleConfiguration.MonitorApplicationResource, "_Count");
lines.Clear();
dateTime = DateTime.Now;
weekOfYear = _Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
weekDirectory = $"{dateTime:yyyy}_Week_{weekOfYear}{@"\"}{dateTime:yyyy-MM-dd}";
directory = Path.Combine(camstarOracleConfiguration.Directory, weekDirectory);
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
List<(DateTime, string)> collection = GetOracleEventLogEntryMessages(camstarOracleConfiguration, dateTime, cancellationToken);
foreach ((DateTime timeGenerated, string message) in collection)
{
if (cancellationToken.IsCancellationRequested)
break;
formattedDateTime = timeGenerated.ToString(camstarOracleConfiguration.DateFormat);
lines.Add($"{formattedDateTime}\t{message}");
}
File.WriteAllLines(Path.Combine(directory, $"{dateTime.Ticks}.tsv"), lines);
if (_LastValue is null || _LastUpload is null || _LastValue.Value != lines.Count || new TimeSpan(dateTime.Ticks - _LastUpload.Value.Ticks).TotalMinutes > 5)
{
Task<HttpResponseMessage> httpResponseMessage = _MonIn.SendPerformanceMessage(camstarOracleConfiguration.MonitorApplicationSite, camstarOracleConfiguration.MonitorApplicationResource, performanceName, value: lines.Count, description: string.Empty);
httpResponseMessage.Wait(cancellationToken);
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
Task<string> body = httpResponseMessage.Result.Content.ReadAsStringAsync(cancellationToken);
body.Wait(cancellationToken);
logger.LogDebug(body.Result);
_LastUpload = DateTime.Now;
_LastValue = lines.Count;
}
return true;
}
}