refactor: standardized code
This commit is contained in:
parent
7f1a947226
commit
9c129acdb4
|
@ -18,20 +18,20 @@ type MaxFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this MaxFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f MaxFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
max := vs[0].Value
|
max := vs[0].Value
|
||||||
for i := 1; i < this.Limit; i++ {
|
for i := 1; i < f.Limit; i++ {
|
||||||
if max < vs[i].Value {
|
if max < vs[i].Value {
|
||||||
max = vs[i].Value
|
max = vs[i].Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = max
|
leftValue = max
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,20 +42,20 @@ type MinFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this MinFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f MinFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
min := vs[0].Value
|
min := vs[0].Value
|
||||||
for i := 1; i < this.Limit; i++ {
|
for i := 1; i < f.Limit; i++ {
|
||||||
if min > vs[i].Value {
|
if min > vs[i].Value {
|
||||||
min = vs[i].Value
|
min = vs[i].Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = min
|
leftValue = min
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,14 +66,14 @@ type AllFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this AllFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f AllFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isTriggered = true
|
isTriggered = true
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
isTriggered = checkIsTriggered(vs[i].Value, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(vs[i].Value, f.Operator, f.RightValue)
|
||||||
if !isTriggered {
|
if !isTriggered {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -90,18 +90,18 @@ type SumFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this SumFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f SumFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = sum
|
leftValue = sum
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,18 +112,18 @@ type AvgFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = sum / dataobj.JsonFloat(this.Limit)
|
leftValue = sum / dataobj.JsonFloat(f.Limit)
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,18 +135,18 @@ type DiffFunction struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 只要有一个点的diff触发阈值,就报警
|
// 只要有一个点的diff触发阈值,就报警
|
||||||
func (this DiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f DiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
first := vs[0].Value
|
first := vs[0].Value
|
||||||
|
|
||||||
isTriggered = false
|
isTriggered = false
|
||||||
for i := 1; i < this.Limit; i++ {
|
for i := 1; i < f.Limit; i++ {
|
||||||
// diff是当前值减去历史值
|
// diff是当前值减去历史值
|
||||||
leftValue = first - vs[i].Value
|
leftValue = first - vs[i].Value
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
if isTriggered {
|
if isTriggered {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -163,20 +163,20 @@ type PDiffFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this PDiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f PDiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
first := vs[0].Value
|
first := vs[0].Value
|
||||||
isTriggered = false
|
isTriggered = false
|
||||||
for i := 1; i < this.Limit; i++ {
|
for i := 1; i < f.Limit; i++ {
|
||||||
if vs[i].Value == 0 {
|
if vs[i].Value == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = (first - vs[i].Value) / vs[i].Value * 100.0
|
leftValue = (first - vs[i].Value) / vs[i].Value * 100.0
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
if isTriggered {
|
if isTriggered {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -193,11 +193,11 @@ type HappenFunction struct {
|
||||||
RightValue float64
|
RightValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this HappenFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f HappenFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
for n, i := 0, 0; i < len(vs); i++ {
|
for n, i := 0, 0; i < len(vs); i++ {
|
||||||
if checkIsTriggered(vs[i].Value, this.Operator, this.RightValue) {
|
if checkIsTriggered(vs[i].Value, f.Operator, f.RightValue) {
|
||||||
n++
|
n++
|
||||||
if n == this.Num {
|
if n == f.Num {
|
||||||
isTriggered = true
|
isTriggered = true
|
||||||
leftValue = vs[i].Value
|
leftValue = vs[i].Value
|
||||||
return
|
return
|
||||||
|
@ -211,7 +211,7 @@ type NodataFunction struct {
|
||||||
Function
|
Function
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this NodataFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f NodataFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
for _, value := range vs {
|
for _, value := range vs {
|
||||||
if !math.IsNaN(float64(value.Value)) {
|
if !math.IsNaN(float64(value.Value)) {
|
||||||
return value.Value, false
|
return value.Value, false
|
||||||
|
@ -228,20 +228,20 @@ type CAvgAbsFunction struct {
|
||||||
CompareValue float64
|
CompareValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this CAvgAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f CAvgAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
value := sum / dataobj.JsonFloat(this.Limit)
|
value := sum / dataobj.JsonFloat(f.Limit)
|
||||||
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(this.CompareValue)))
|
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(f.CompareValue)))
|
||||||
|
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,19 +253,19 @@ type CAvgFunction struct {
|
||||||
CompareValue float64
|
CompareValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this CAvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f CAvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
leftValue = sum/dataobj.JsonFloat(this.Limit) - dataobj.JsonFloat(this.CompareValue)
|
leftValue = sum/dataobj.JsonFloat(f.Limit) - dataobj.JsonFloat(f.CompareValue)
|
||||||
|
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,20 +277,20 @@ type CAvgRateAbsFunction struct {
|
||||||
CompareValue float64
|
CompareValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this CAvgRateAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f CAvgRateAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
value := sum / dataobj.JsonFloat(this.Limit)
|
value := sum / dataobj.JsonFloat(f.Limit)
|
||||||
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(this.CompareValue)))
|
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(f.CompareValue)))
|
||||||
|
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,20 +302,20 @@ type CAvgRateFunction struct {
|
||||||
CompareValue float64
|
CompareValue float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this CAvgRateFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
func (f CAvgRateFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
|
||||||
if len(vs) < this.Limit {
|
if len(vs) < f.Limit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := dataobj.JsonFloat(0.0)
|
sum := dataobj.JsonFloat(0.0)
|
||||||
for i := 0; i < this.Limit; i++ {
|
for i := 0; i < f.Limit; i++ {
|
||||||
sum += vs[i].Value
|
sum += vs[i].Value
|
||||||
}
|
}
|
||||||
|
|
||||||
value := sum / dataobj.JsonFloat(this.Limit)
|
value := sum / dataobj.JsonFloat(f.Limit)
|
||||||
leftValue = (value - dataobj.JsonFloat(this.CompareValue)) / dataobj.JsonFloat(math.Abs(this.CompareValue))
|
leftValue = (value - dataobj.JsonFloat(f.CompareValue)) / dataobj.JsonFloat(math.Abs(f.CompareValue))
|
||||||
|
|
||||||
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
|
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,49 +36,49 @@ func NewIndexCacheBase(max int) *IndexCacheBase {
|
||||||
return &IndexCacheBase{maxSize: max, data: make(map[interface{}]*dataobj.TsdbItem)}
|
return &IndexCacheBase{maxSize: max, data: make(map[interface{}]*dataobj.TsdbItem)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) {
|
func (i *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) {
|
||||||
this.Lock()
|
i.Lock()
|
||||||
defer this.Unlock()
|
defer i.Unlock()
|
||||||
this.data[key] = item
|
i.data[key] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem {
|
func (i *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem {
|
||||||
this.RLock()
|
i.RLock()
|
||||||
defer this.RUnlock()
|
defer i.RUnlock()
|
||||||
return this.data[key]
|
return i.data[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) ContainsKey(key interface{}) bool {
|
func (i *IndexCacheBase) ContainsKey(key interface{}) bool {
|
||||||
this.RLock()
|
i.RLock()
|
||||||
defer this.RUnlock()
|
defer i.RUnlock()
|
||||||
return this.data[key] != nil
|
return i.data[key] != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) Size() int {
|
func (i *IndexCacheBase) Size() int {
|
||||||
this.RLock()
|
i.RLock()
|
||||||
defer this.RUnlock()
|
defer i.RUnlock()
|
||||||
return len(this.data)
|
return len(i.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) Keys() []interface{} {
|
func (i *IndexCacheBase) Keys() []interface{} {
|
||||||
this.RLock()
|
i.RLock()
|
||||||
defer this.RUnlock()
|
defer i.RUnlock()
|
||||||
|
|
||||||
count := len(this.data)
|
count := len(i.data)
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return []interface{}{}
|
return []interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make([]interface{}, 0, count)
|
keys := make([]interface{}, 0, count)
|
||||||
for key := range this.data {
|
for key := range i.data {
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexCacheBase) Remove(key interface{}) {
|
func (i *IndexCacheBase) Remove(key interface{}) {
|
||||||
this.Lock()
|
i.Lock()
|
||||||
defer this.Unlock()
|
defer i.Unlock()
|
||||||
delete(this.data, key)
|
delete(i.data, key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,25 +11,25 @@ type ConsistentHashRing struct {
|
||||||
ring *consistent.Consistent
|
ring *consistent.Consistent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ConsistentHashRing) GetNode(pk string) (string, error) {
|
func (c *ConsistentHashRing) GetNode(pk string) (string, error) {
|
||||||
this.RLock()
|
c.RLock()
|
||||||
defer this.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
||||||
return this.ring.Get(pk)
|
return c.ring.Get(pk)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ConsistentHashRing) Set(r *consistent.Consistent) {
|
func (c *ConsistentHashRing) Set(r *consistent.Consistent) {
|
||||||
this.Lock()
|
c.Lock()
|
||||||
defer this.Unlock()
|
defer c.Unlock()
|
||||||
this.ring = r
|
c.ring = r
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ConsistentHashRing) GetRing() *consistent.Consistent {
|
func (c *ConsistentHashRing) GetRing() *consistent.Consistent {
|
||||||
this.RLock()
|
c.RLock()
|
||||||
defer this.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
||||||
return this.ring
|
return c.ring
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsistentHashRing(replicas int32, nodes []string) *ConsistentHashRing {
|
func NewConsistentHashRing(replicas int32, nodes []string) *ConsistentHashRing {
|
||||||
|
|
Loading…
Reference in New Issue