简体   繁体   English

将课程解释为文本

[英]Interpreting class as text

(Apologies for the long question) (对长问题抱歉)

Hi, We have a service running that monitors our queues every 15 minutes and outputs the result to a number of things (company IM, logs, Table storage). 嗨,我们有一个运行服务,每15分钟监视一次我们的队列,并将结果输出到很多东西(公司IM,日志,表存储)。 I am looking for a nice way to translate the data into "smart" text to output to our IM. 我正在寻找一种很好的方法将数据转换为“智能”文本输出到我们的IM。

The service monitors 3 things (Alerts) and the result object looks like 该服务监视3件事(警报),结果对象看起来像

    public class ServiceMonitorResult 
    {
        public string Name
        public int Count 
        public int Age 
        public int Delta
        // our 3 alerts
        public IAlert CountAlert // can be null 
        public IAlert DeltaAlert // can be null 
        public IAlert AgeAlert   // can be null 


    }  

    public interface IAlert
    {
        AlertStatus Status// enum : New - Continuing - Ended
        ...
        public string MessageText   //  usually like $"{ServiceName (very long)} {alert status} breached  {Alert}  with value X at {DateTime.Now}. Started At {startedAt}"
    }

When this result gets sent out on our compay IM, the result can sometimes be very verbose. 当这个结果在我们的compay IM上发出时,结果有时可能非常冗长。 We have over 200 services, if we have an average of 10% breaches, that is 20 messages/15 mins !! 我们有超过200项服务,如果我们平均有10%的违规,那就是20条消息/ 15分钟!

You can imagine in times when there are multiple fauliures how many message you would get. 你可以想象,当有多个faulures你会得到多少消息。

I have removed the MessageText from the IAlert to have another class that is responsible for building the message based on the Alert status. 我已经从IAlert中删除了MessageText,以便有另一个类负责根据Alert状态构建消息。 The reason for that is the result of this monitoring needs to be displayed differently - for example in IM it needs to be compact and clear ; 原因是这种监测的结果需要以不同的方式显示 - 例如在IM中它需要紧凑和清晰; it doesnt matter much in table storage because its used for analysis and is not read by humans . 它在表存储中并不重要,因为它用于分析并且不被人类读取

Given that: 鉴于:

  1. These alerts can be null , and if they are not they can have a status of New, Continuing or ended. 这些警报可以为空,如果不是,则可以具有“新建”,“继续”或“结束”状态。
  2. I am interested in showing services that have at least one alert with status New 我有兴趣展示至少有一个状态为New的警报的服务
  3. I am interested in showing services that have at least one alert with status Ended 我有兴趣显示至少有一个状态为Ended的警报的服务

What I am trying do is use sort of design pattern/algorithim instead of having nested if else statements that will be turn into spagehetti and will be unefficient to maintain/understand . 我正在尝试做的是使用一种设计模式/算法而不是嵌套if else语句将变成spagehetti并且维护/理解将是无效的。 I have had a look at the interpretor pattern but it doesnt look like it will work here. 我已经看过解释器模式,但它看起来不像在这里工作。 Any ideas? 有任何想法吗?


Example of monitoring input 监视输入的示例

ServiceName:CountAlertStatus /AgeAlertSatus / DeltaAlertStatus ServiceName:CountAlertStatus / AgeAlertSatus / DeltaAlertStatus

  1. service100:N/-/E service100:N / - / E
  2. Service101:N/N/C Service101:N / N / C
  3. Service102:-/-/- Service102: - / - / -
  4. Service103:C/C/C Service103:C / C / C
  5. Service104:C/E/N Service104:C / E / N
  6. Service105:N/N/N Service105:N / N / N
  7. Service106:E/E/E Service106:电子/ E / E

(N:new , C: continuring:, E: ended, -:null alert) (N:新,C:持续:,E:结束, - :null警报)

Desired output 期望的输出

  • service100 : Count (X) breached and Delta is not breached anymore at {Now} service100:违反了计数(X),并且{Now}不再违反Delta
  • service101 : Count (X) and Age (Y) breached. service101:违反了计数(X)和年龄(Y)。 Delta is still breached (Z) at {Now} 在{Now}仍然违反了Delta(Z)
  • Service104 : Delta (X) breached and Age not breached anymore. 服务104:Delta(X)违反并且Age不再被破坏。 Count Still breached at {Now} 伯爵仍在{Now}突破
  • Service105 : Count (X) Age(Y) and Delta (Z) breached at {now} Service105:在{now}处违反计数(X)年龄(Y)和Delta(Z)
  • Service106 : Count (X) Age(Y) and Delta (Z) breach ended at {now} Service106:计数(X)年龄(Y)和Delta(Z)违规结束于{now}

service102 & service103 will NOT show any alerts because they are null / Continuing and therefore we do not care service102和service103不会显示任何警报,因为它们为空/继续,因此我们不在乎

Have an array of handlers. 有一系列处理程序。 Each handler has the following signature: 每个处理程序都具有以下签名:

bool handle(char countAlertStatus, char ageAlertSatus, char deltaAlertStatus, out string message)
{
   //...
}

If you can handle the combination of CountAlertStatus /AgeAlertSatus / DeltaAlertStatus, return true and set message otherwise return false . 如果您可以处理CountAlertStatus / AgeAlertSatus / DeltaAlertStatus的组合,则返回true并设置message否则返回false

You can register new lambdas with the array of handlers easily enough then. 您可以轻松地使用处理程序数组注册新的lambda。

You then simply iterate over your handlers and stop when one returns true . 然后,您只需遍历处理程序并在返回true时停止。

Example: 例:

using System;
using System.Collections.Generic;

delegate bool MessageHandler(char countAlertStatus, char ageAlertSatus, char deltaAlertStatus, out string message);

class Program
{
    static void Main(string[] args)
    {
        var handlers = new List<MessageHandler>();

        //register lambda with array
        handlers.Add(Handler1);

        foreach (var handler in handlers)
        {
            // some fake input to each handler C/-/-
            if (handler('C', '-', '-', out string message))
            {
                Console.WriteLine("handled: " + message);
            }
        }

        Console.WriteLine("Done...");
        Console.Read();
    }

    static bool Handler1(char countAlertStatus, char ageAlertSatus, char deltaAlertStatus, out string message)
    {
        if (countAlertStatus == 'C')
        {
            message = "Handled";

            return true;
        }
        else
        {
            message = null;

            return false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM