用于确定是否发生故障的存储过程

时间:2016-03-08 01:10:14

标签: sql-server stored-procedures

我在Azure上运行SQL Server。我有一个表,我存储了我监控的每个区域和通道的故障率和计数。如果任何地区/频道的失败率高于2%且连续三天的计数超过5,我需要将我的采样率从5%调整到7.5%。我尝试使用平均值,但是如果我有两天的大数量然后是一个小计数,那么这不能正确地给我结果。

我尝试使用过去三天的平均故障率和平均故障数,但这不起作用。例如: 12.57%和20计数 10.25%和4计数 10.71%和3计数

即使比率高于2%,计数也连续三天没有超过5。但是如果你取平均值,那么就得到了这个 -

11.17%和9个计数是假的。

表 -

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    TimeOutPane tp = new TimeOutPane();
                    tp.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            tp.executeTimeout();
                        }
                    });

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(tp);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TimeOutPane extends JPanel {

        private BufferedImage background;
        private float progress = 0;
        private long startedAt;
        private int timeout = 5000;

        private Timer timer;

        public TimeOutPane() throws IOException {
            background = ImageIO.read(new File("/Volumes/Disk02/Dropbox/Ponies/url.png"));
        }

        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }

        public int getTimeout() {
            return timeout;
        }

        public void setProgress(float progress) {
            this.progress = progress;
            repaint();
        }

        public float getProgress() {
            return progress;
        }

        public void executeTimeout() {
            if (timer == null) {
                timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long diff = System.currentTimeMillis() - startedAt;
                        float progress = diff / (float) timeout;
                        if (diff >= timeout) {
                            progress = 1f;
                            timer.stop();
                        }
                        setProgress(progress);
                    }
                });
            } else if (timer.isRunning()) {
                timer.stop();
            }

            startedAt = System.currentTimeMillis();
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                applyQualityRenderingHints(g2d);
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);

                g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
                g2d.setColor(Color.BLACK);

                int radius = Math.max(getWidth(), getHeight()) / 2;

                g2d.fillArc(-radius, -radius, radius * 4, radius * 4, 90, (int) (360f * (1f - progress)));

                g2d.dispose();
            }
        }

        public void applyQualityRenderingHints(Graphics2D g2d) {
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        }

    }

}

当前存储过程 -

CREATE TABLE [dbo].[dfc_LoanRateRecords](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [rateDate] [datetime] NOT NULL,
    [usOnlineRate] [float] NOT NULL,
    [usOnlineCount] [int] NOT NULL,
    [usRetailRate] [float] NOT NULL,
    [usRetailCount] [int] NOT NULL,
    [cdnOnlineRate] [float] NOT NULL,
    [cdnOnlineCount] [int] NOT NULL,
    [cdnRetailRate] [float] NOT NULL,
    [cdnRetailCount] [int] NOT NULL,
    [ukOnlineRate] [float] NOT NULL,
    [ukOnlineCount] [int] NOT NULL,
    [ukRetailRate] [float] NOT NULL,
    [ukRetailCount] [int] NOT NULL,
 CONSTRAINT [PK_dbo.dfc_LoanRateRecords] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

数据 -

ALTER PROCEDURE [dbo].[dfc_sp_average_loan_failure]
AS
BEGIN
    SELECT   
             (CASE WHEN AVG(usOnlineRate) > 2.00 AND AVG(usOnlineCount) > 5 THEN 7.5 ELSE 5.0 END)      AS usOnlineRate
            ,(CASE WHEN AVG(usRetailRate) > 2.00 AND AVG(usRetailCount) > 5 THEN 7.5 ELSE 5.0 END)      AS usRetailRate
            ,(CASE WHEN AVG(cdnOnlineRate) > 2.00 AND AVG(cdnOnlineCount) > 5 THEN 7.5 ELSE 5.0 END)    AS cdnOnlineRate
            ,(CASE WHEN AVG(cdnRetailRate) > 2.00 AND AVG(cdnRetailCount) > 5 THEN 7.5 ELSE 5.0 END)    AS cdnRetailRate
            ,(CASE WHEN AVG(ukOnlineRate) > 2.00 AND AVG(ukOnlineCount) > 5 THEN 7.5 ELSE 5.0 END)      AS ukOnlineRate
            ,(CASE WHEN AVG(ukRetailRate) > 2.00 AND AVG(ukRetailCount) > 5 THEN 7.5 ELSE 5.0 END)      AS ukRetailRate

    FROM dfc_LoanRateRecords
    WHERE rateDate BETWEEN dateadd(day, -3, GetDate()) and GetDate()

0 个答案:

没有答案