tangxu
2024-01-09 ddc2780231ea76be74fadb7486401a3d0d17b101
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
namespace Yw.Server
{
    /// <summary>
    /// 指标评价任务辅助类
    /// </summary>
    public class QuotaEvaluateJobHelper : IJobHelper
    {
        private readonly RabbitMqExChangeHelper _queueHelper = new();
 
        /// <summary>
        /// 开始任务
        /// </summary>
        public Task StartJob()
        {
            return Task.Run(() =>
            {
                _queueHelper.Receive<List<Model.MonitorRecord>>(ConfigHelper.MonitorRunExchangeName, (monitorRecordList) =>
                {
                    try
                    {
                        if (monitorRecordList == null || monitorRecordList.Count < 1)
                        {
                            LogHelper.Info("指标健康评价任务中,数据序列化失败");
                            return true;
                        }
 
                        var index = new Service.HealthIndex().GetDefault();
                        if (index == null)
                        {
                            LogHelper.Info("指标健康评价任务中,尚未配置健康指数!");
                            return true;
                        }
 
                        var serviceBinding = new Lazy<Service.HealthQuotaEvaluationModelBinding>(() => new Service.HealthQuotaEvaluationModelBinding());
                        var serviceModel = new Lazy<Service.HealthQuotaEvaluationModel>(() => new Service.HealthQuotaEvaluationModel());
                        var serviceRecord = new Lazy<Service.HealthQuotaEvaluationRecord>(() => new Service.HealthQuotaEvaluationRecord());
 
                        //遍历处理
                        foreach (var monitorRecord in monitorRecordList)
                        {
                            var binding = serviceBinding.Value.GetValidBySignalID(monitorRecord.SignalID);
                            if (binding != null)
                            {
                                var model = serviceModel.Value.GetByID(binding.ModelID);
                                if (model != null)
                                {
                                    if (model.Way == Health.eEvaluateWay.Auto)
                                    {
                                        var dataValue = model.Evaluate(monitorRecord.DataValue, out Health.eEvaluateMode evaluateMode);
                                        if (dataValue < index.MinValue)
                                        {
                                            dataValue = index.MinValue;
                                        }
                                        if (dataValue > index.MaxValue)
                                        {
                                            dataValue = index.MaxValue;
                                        }
 
                                        var evaluateRecord = new Model.HealthQuotaEvaluationRecord()
                                        {
                                            SignalID = monitorRecord.SignalID,
                                            EvaluateModelID = model.ID,
                                            EvaluateMode = evaluateMode,
                                            EvaluateTime = DateTime.Now,
                                            EvaluateValue = dataValue
                                        };
 
                                        var bol = serviceRecord.Value.InsertLastRecord(evaluateRecord);
                                        if (bol)
                                        {
                                            LogHelper.Info($"指标健康评价任务中,信号id:{monitorRecord.SignalID},评价模型id:{model.ID},完成自动评价:{evaluateRecord.EvaluateValue}。");
                                        }
                                        else
                                        {
                                            LogHelper.Info($"指标健康评价任务中,信号id:{monitorRecord.SignalID},评价模型id:{model.ID},自动评价保存失败:{evaluateRecord.EvaluateValue}。");
                                        }
                                    }
                                }
                            }
                        }
 
                        return true;
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error("指标评价任务中,数据消息队列出错,自动跳过", ex);
                        return true;
                    }
                });
            });
 
        }
 
        /// <summary>
        /// 取消任务
        /// </summary>
        public Task CancelJob()
        {
            return Task.Run(() =>
            {
                if (_queueHelper == null)
                    return;
                _queueHelper.Close();
            });
        }
 
 
    }
}