Files
raven/server/AyaNova/biz/NotifyEventProcessor.cs
2020-07-16 19:36:08 +00:00

111 lines
4.2 KiB
C#

using System;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using AyaNova.Util;
using AyaNova.Models;
//using System.Diagnostics;
namespace AyaNova.Biz
{
//This class handles word breaking, processing keywords and searching for results
public static class NotifyEventProcessor
{
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("NotifyEventProcessor");
//Add operations message
public static async Task AddOpsProblemEvent(string message, Exception ex = null)
{
if (string.IsNullOrWhiteSpace(message) && ex == null)
return;
//Log as a backup in case there is no one to notify and also for the record and support
if (ex != null)
{
log.LogError(ex, $"Ops problem notification: \"{message}\"");
message += $"\nException error: {ExceptionUtil.ExtractAllExceptionMessages(ex)}";
}
else
log.LogWarning($"Ops problem notification: \"{message}\"");
await UpsertEvent(new NotifyEvent() { EventType = NotifyEventType.ServerOperationsProblem, Message = message });
}
//Add event if there are subscribers
//several optional conditional parameters
public static async Task UpsertEvent(NotifyEvent ev, List<string> inTags = null, long? IdValue = null, TimeSpan? AgeValue = null, decimal? DecValue = null)
{
log.LogTrace($"UpsertEvent processing: [{ev.ToString()}]");
try
{
// List<NotifySubscription> subs = new List<NotifySubscription>();
//Widget.updated.tags=yellow
//find subs where widget.updated and either no tags or tags=yellow if tags
//do I really need to remove old events? They should be zipping out the door pretty quickly.
//only I guess if it's delayed like a time delayed one or send later kind of situation?
var HasTags = inTags != null && inTags.Count > 0;
using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext)
{
//find all the subs that are relevant
//this first query ensures that the equality matching conditions of AyaType and EventType and idValue are matched leaving only more complex matches to rectify below
var subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == ev.EventType && z.AyaType == ev.AyaType && z.IdValue == ev.IdValue).ToListAsync();
foreach (var sub in subs)
{
//Tags condition
if (sub.Tags.Count > 0)
{
if (!HasTags)
continue;//not a match, this sub needs tags and we don't have any
//bool existsCheck = list1.All(x => list2.Any(y => x.SupplierId == y.SupplierId));
//would tell you if all of list1's items are in list2.
if (!sub.Tags.All(z => inTags.Any(x => x == z)))
continue;
}
//Decimal value, so far only for "The Andy" so the only condition is that the event value be greater than or equal to subscription setting
if (ev.EventType == NotifyEventType.WorkorderTotalExceedsThreshold && ev.DecValue < sub.DecValue)
continue;
//AgeValue
//Here we know the sub matches so create the NotifyEvent here
NotifyEvent ne = new NotifyEvent() { };
}
}
}
catch (Exception ex)
{
log.LogError(ex, $"Error adding notification event: [{ev.ToString()}]");
}
finally
{
log.LogTrace($"Notify event processed: [{ev.ToString()}]");
}
}
}//eoc
}//eons