using HDLCloudMonitorSupportCtrlOldUdpGate.Context;
|
using HDLCloudMonitorSupportCtrlOldUdpGate.Logs;
|
using System;
|
using System.Linq;
|
using System.Collections.Generic;
|
using System.Threading.Tasks;
|
using System.Text;
|
using JumpKick.HttpLib;
|
using Newtonsoft.Json;
|
using System.Net;
|
|
namespace HDLCloudMonitorSupportCtrlOldUdpGate
|
{
|
public static class EarlyWarningTimerServoce
|
{
|
private const int AvgCount= 360;
|
private const int EarlyWarningAvgTime= 150;
|
private const int EarlyWarningCount = 500;
|
|
private static List<string> executeList = new List<string>();
|
|
public static async Task StartEarlyWarning()
|
{
|
await Task.Run(async() =>
|
{
|
while (true)
|
{
|
try
|
{
|
string currentDateHourStr = DateTime.Now.ToString(Consts.DateTimeFormatHoursStr);
|
if (!executeList.Contains(currentDateHourStr))
|
{
|
string lastHour = DateTime.Now.AddHours(-1).ToString(Consts.DateTimeFormatHoursStr);
|
|
//拿上一小时日志数据
|
var currentPointDataList = MqttLogHp.ReadDataByDateHours(lastHour);
|
|
//执行第1种策略
|
ExecuteFirstStrategy(currentPointDataList, lastHour);
|
|
//执行第2种策略
|
ExecuteSecondStrategy(currentPointDataList, lastHour);
|
|
if (executeList.Count > 2)
|
{
|
executeList.Clear();
|
}
|
executeList.Add(currentDateHourStr);
|
}
|
|
await Task.Delay(TimeSpan.FromSeconds(1));
|
}
|
catch(Exception ex)
|
{
|
}
|
}
|
});
|
}
|
|
private static void ExecuteFirstStrategy(List<CtrlRecordDto> currentPointDataList,string lastHour)
|
{
|
try
|
{
|
/*
|
第一种策略:
|
去网关时间 求平均超过150,发邮件
|
网关回复控制端 求平均超过150,发邮件
|
*/
|
double arrivalsAvg = currentPointDataList.Sum(s => s.ArrivalsMilliseconds) / AvgCount;
|
double comebackAvg = currentPointDataList.Sum(s => s.ComebackMilliseconds) / AvgCount;
|
|
var sb = new StringBuilder();
|
|
if (arrivalsAvg > EarlyWarningAvgTime)
|
{
|
sb.AppendLine($"第一种策略:{lastHour}去的时间平均值:{arrivalsAvg}<br/>");
|
}
|
|
if (comebackAvg > EarlyWarningAvgTime)
|
{
|
sb.AppendLine($"第一种策略:{lastHour}回的时间平均值:{comebackAvg}");
|
}
|
|
string notifyContent = sb.ToString();
|
if (string.IsNullOrEmpty(notifyContent))
|
{
|
notifyContent = $"第一种策略:{lastHour}过滤程序去与回监控时间正常!";
|
}
|
|
//发邮件通知
|
|
string reqStr = JsonConvert.SerializeObject(new { RequestVersion = "RequestVersion11", EarlyWarningNotifyEmailList = Config.EarlyWarningNotifyEmailList, NotifyContent = notifyContent });
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) =>
|
{
|
return true;
|
};
|
Http.Post(Config.EarlyWarningNotifyEmailListReqUrl).Body("application/json", reqStr).Go();
|
}
|
catch
|
{
|
}
|
}
|
|
private static void ExecuteSecondStrategy(List<CtrlRecordDto> currentPointDataList, string lastHour)
|
{
|
/*
|
第二种策略:
|
(不管是去还是回,时间超过500ms的10次报警依次预警)
|
*/
|
try
|
{
|
bool arrivalsIsNoOk = currentPointDataList.Where(w => w.ArrivalsMilliseconds > EarlyWarningCount).ToList().Count > Consts.TEN;
|
bool comebackIsNoOk = currentPointDataList.Where(w => w.ComebackMilliseconds > EarlyWarningCount).ToList().Count > Consts.TEN;
|
|
var sb = new StringBuilder();
|
if (arrivalsIsNoOk)
|
{
|
sb.AppendLine($"第二种策略:{lastHour}去的时间有超过10次过500ms!<br/>");
|
}
|
|
|
if (comebackIsNoOk)
|
{
|
sb.AppendLine($"第二种策略:{lastHour}回的时间有超过10次过500ms!<br/>");
|
}
|
|
if (!arrivalsIsNoOk && !comebackIsNoOk)
|
{
|
sb.AppendLine($"第二种策略:{lastHour}过滤程序去与回监控超过500次数正常!");
|
}
|
|
string notifyContent = sb.ToString();
|
string reqStr = JsonConvert.SerializeObject(new { RequestVersion = "RequestVersion11", EarlyWarningNotifyEmailList = Config.EarlyWarningNotifyEmailList, NotifyContent = notifyContent });
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) =>
|
{
|
return true;
|
};
|
Http.Post(Config.EarlyWarningNotifyEmailListReqUrl).Body("application/json", reqStr).Go();
|
}
|
catch
|
{
|
}
|
}
|
}
|
}
|