設計模式11

yydssc發表於2024-10-11
package com.example.principle.ocp;

public class ApiInfo {

    private String api;
    private long requestCount;
    private long errorCount;
    private long durationOfSeconds;

    // private long timeoutCount;


    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public long getRequestCount() {
        return requestCount;
    }

    public void setRequestCount(long requestCount) {
        this.requestCount = requestCount;
    }

    public long getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(long errorCount) {
        this.errorCount = errorCount;
    }

    public long getDurationOfSeconds() {
        return durationOfSeconds;
    }

    public void setDurationOfSeconds(long durationOfSeconds) {
        this.durationOfSeconds = durationOfSeconds;
    }
}
package com.example.principle.ocp;

// 儲存告警規則
public class AlertRule {


    public MatchRule getMatchedRule(String api) {
        return null;
    }

    class MatchRule {

        long maxTps;
        long maxErrorCount;

        //...... Maxtimeout


        public long getMaxTps() {
            return maxTps;
        }

        public void setMaxTps(long maxTps) {
            this.maxTps = maxTps;
        }

        public long getMaxErrorCount() {
            return maxErrorCount;
        }

        public void setMaxErrorCount(long maxErrorCount) {
            this.maxErrorCount = maxErrorCount;
        }
    }
}
package com.example.principle.ocp;

// 通知類 支援郵件、簡訊、微信、手機等多種通知渠道
public class Notification {

    // 通知
    void notify(NotificationEmergencyLevel level,String content) {
        //
    }


    // 表示通知的緊急程度,
    //包括 SEVERE(嚴重)、URGENCY(緊急)、NORMAL(普通)、TRIVIAL(無關緊要),不同的緊急程度對應不同的傳送渠道
     enum NotificationEmergencyLevel {

        SEVERE(0,0),URGENCY(1,1),NORMAL(2,2),TRIVIAL(3,3);

        int channel ; //渠道
        int level; // 等級

        NotificationEmergencyLevel(int channel, int level) {
            this.channel = channel;
            this.level = level;
        }

        public int getChannel() {
            return channel;
        }

        public void setChannel(int channel) {
            this.channel = channel;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }
    }
}
package com.example.principle.ocp;

public abstract class AlertHandler {

    protected AlertRule rule;
    protected Notification notification;

    public AlertHandler(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }


    public abstract void adaptiveAlert(ApiInfo apiInfo);
}

class TpsAlertHandler extends AlertHandler {

    public TpsAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo) {
        long tps = apiInfo.getRequestCount() / apiInfo.getDurationOfSeconds();
        if (tps > rule.getMatchedRule(apiInfo.getApi()).getMaxTps()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,".........");
        }
    }
}

class ErrorCountAlertHandler extends AlertHandler{

    public ErrorCountAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo) {
        if (apiInfo.getErrorCount() > rule.getMatchedRule(apiInfo.getApi()).getMaxErrorCount()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,"........");
        }
    }
}


class TimeoutAlertHandler extends AlertHandler {

    public TimeoutAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo) {

    }
}

相關文章