/// <summary> /// /// </summary> /// <param name="c"></param> /// <param name="n"></param> private async Task <bool> Send(EvaluationContext c, ModelAlertNotification n) { if (n.DisableResolveMessage && c.State == ED.Alerts.AlertState.Ok) { return(false); } if (n.IncludeImage) { await RenderImage(c); } using var client = new HttpClient(); var cmd = n.Build(c); var sw = Stopwatch.StartNew(); var response = await client.PostAsync(cmd.Url, cmd.Body); sw.Stop(); Logger.Debug($"Sent notification: {cmd.Url} [{response.StatusCode}][{sw.ElapsedMilliseconds}ms]"); return((( int )response.StatusCode) / 100 == 2); }
/// <summary> /// /// </summary> /// <param name="nt"></param> /// <returns></returns> public OperationResult <ModelAlertNotification> Create(ModelAlertNotification nt) { OperationResult <ModelAlertNotification> res; try { if (nt.IsDefault) { ResetIsDefault(); } var entity = nt .ToEntity() .IncludeActiveOrgId(DataContext); DataContext.AlertNotifications.Add(entity); DataContext.SaveChanges(); var model = entity .UpdateId(nt) .ToModel(); res = OperationResult <ModelAlertNotification> .Create(model); } catch (Exception e) { res = OperationResult <ModelAlertNotification> .Create(ErrorCode.BadCreateAlertNotification, e); } return(res); }
/// <summary> /// /// </summary> /// <returns></returns> public async Task <OperationResult <bool> > Test(ModelAlertNotification nt) { var c = new EvaluationContext() { Rule = new Rule() { Message = "Someone is testing the alert notification within easy dashboard.", Name = "Test notification" }, State = ED.Alerts.AlertState.Alerting, PanelId = 1, IsTestRun = true, Matches = new List <EvalMatch>() { new EvalMatch("Low value", 100), new EvalMatch("High value", 200), } }; var res = await Send(c, nt); return(OperationResult <bool> .Create( () => res, true, ErrorCode.BadTestAlertNotification)); }
/// <summary> /// /// </summary> /// <param name="nt"></param> /// <returns></returns> public OperationResult <ModelAlertNotification> Update(string uid, ModelAlertNotification nt) { OperationResult <ModelAlertNotification> res; try { if (nt.IsDefault) { ResetIsDefault(); } var entity = DataContext .AlertNotifications .ForActiveOrg() .FirstOrDefault(x => x.Uid == uid); if (null == entity) { return(OperationResult <ModelAlertNotification> .Create(ErrorCode.BadGetAlertNotification)); } entity.Update(nt); DataContext .AlertNotifications .Update(entity); DataContext.SaveChanges(); var model = entity .UpdateId(nt) .ToModel(); res = OperationResult <ModelAlertNotification> .Create(model); } catch (Exception e) { res = OperationResult <ModelAlertNotification> .Create(ErrorCode.BadUpdateAlertNotification, e); } return(res); }