﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ctrip.SOA.Comm.Metrics;
using Arch.CFramework.AppInternals.Components.MetricComponents.Attributes;

namespace esb.request
{
    [MetricScheduling(Circle = 1)]
    public class RequestDistributionMetric : EsbMetricsBase
    {

        public RequestDistributionMetric()
        {
            _tags.Add("Distribution", "");
        }

        [Tag]
        public string Distribution
        {
            get
            {
                return _tags["Distribution"];
            }
            set
            {
                _tags["Distribution"] = value;
            }
        }

        [CounterMetric("distribution", Duration = 2)]
        public int Value { get; set; }

        public override void Set(double val)
        {
            if (val < 10)
            {
                this.Distribution = "0~10ms";
            }
            else if (val >= 10 && val < 50)
            {
                this.Distribution = "10~50ms";
            }
            else if (val >= 50 && val < 200)
            {
                this.Distribution = "50~200ms";
            }
            else if (val >= 200 && val < 500)
            {
                this.Distribution = "200~500ms";
            }
            else if (val >= 500 && val < 1000)
            {
                this.Distribution = "500~1s";
            }
            else if (val >= 1000 && val < 5000)
            {
                this.Distribution = "1~5s";
            }
            else if (val >= 5000 && val < 10000)
            {
                this.Distribution = "5~10s";
            }
            else if (val >= 10000 && val < 30000)
            {
                this.Distribution = "10~30s";
            }
            else if (val >= 30000 && val < 100000)
            {
                this.Distribution = "30~100s";
            }
            else
            {
                this.Distribution = ">100s";
            }
            
            base.Set(1);
        }
    }
}
