hxb
2020-09-23 2921da3e4ba99f0df28a172d47daa69e375431bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 
            {             
            }
        }   
    }
}