refactor: standardized code

This commit is contained in:
710leo 2020-04-28 22:42:15 +08:00
parent 7f1a947226
commit 9c129acdb4
3 changed files with 93 additions and 93 deletions

View File

@ -18,20 +18,20 @@ type MaxFunction struct {
RightValue float64
}
func (this MaxFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f MaxFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
max := vs[0].Value
for i := 1; i < this.Limit; i++ {
for i := 1; i < f.Limit; i++ {
if max < vs[i].Value {
max = vs[i].Value
}
}
leftValue = max
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
return
}
@ -42,20 +42,20 @@ type MinFunction struct {
RightValue float64
}
func (this MinFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f MinFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
min := vs[0].Value
for i := 1; i < this.Limit; i++ {
for i := 1; i < f.Limit; i++ {
if min > vs[i].Value {
min = vs[i].Value
}
}
leftValue = min
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
return
}
@ -66,14 +66,14 @@ type AllFunction struct {
RightValue float64
}
func (this AllFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f AllFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
isTriggered = true
for i := 0; i < this.Limit; i++ {
isTriggered = checkIsTriggered(vs[i].Value, this.Operator, this.RightValue)
for i := 0; i < f.Limit; i++ {
isTriggered = checkIsTriggered(vs[i].Value, f.Operator, f.RightValue)
if !isTriggered {
break
}
@ -90,18 +90,18 @@ type SumFunction struct {
RightValue float64
}
func (this SumFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f SumFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
sum += vs[i].Value
}
leftValue = sum
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
return
}
@ -112,18 +112,18 @@ type AvgFunction struct {
RightValue float64
}
func (this AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
sum += vs[i].Value
}
leftValue = sum / dataobj.JsonFloat(this.Limit)
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
leftValue = sum / dataobj.JsonFloat(f.Limit)
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
return
}
@ -135,18 +135,18 @@ type DiffFunction struct {
}
// 只要有一个点的diff触发阈值就报警
func (this DiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f DiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
first := vs[0].Value
isTriggered = false
for i := 1; i < this.Limit; i++ {
for i := 1; i < f.Limit; i++ {
// diff是当前值减去历史值
leftValue = first - vs[i].Value
isTriggered = checkIsTriggered(leftValue, this.Operator, this.RightValue)
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
if isTriggered {
break
}
@ -163,20 +163,20 @@ type PDiffFunction struct {
RightValue float64
}
func (this PDiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f PDiffFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
first := vs[0].Value
isTriggered = false
for i := 1; i < this.Limit; i++ {
for i := 1; i < f.Limit; i++ {
if vs[i].Value == 0 {
continue
}
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 {
break
}
@ -193,11 +193,11 @@ type HappenFunction struct {
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++ {
if checkIsTriggered(vs[i].Value, this.Operator, this.RightValue) {
if checkIsTriggered(vs[i].Value, f.Operator, f.RightValue) {
n++
if n == this.Num {
if n == f.Num {
isTriggered = true
leftValue = vs[i].Value
return
@ -211,7 +211,7 @@ type NodataFunction struct {
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 {
if !math.IsNaN(float64(value.Value)) {
return value.Value, false
@ -228,20 +228,20 @@ type CAvgAbsFunction struct {
CompareValue float64
}
func (this CAvgAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f CAvgAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
sum += vs[i].Value
}
value := sum / dataobj.JsonFloat(this.Limit)
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(this.CompareValue)))
value := sum / dataobj.JsonFloat(f.Limit)
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
}
@ -253,19 +253,19 @@ type CAvgFunction struct {
CompareValue float64
}
func (this CAvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f CAvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
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
}
@ -277,20 +277,20 @@ type CAvgRateAbsFunction struct {
CompareValue float64
}
func (this CAvgRateAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f CAvgRateAbsFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
sum += vs[i].Value
}
value := sum / dataobj.JsonFloat(this.Limit)
leftValue = dataobj.JsonFloat(math.Abs(float64(value) - float64(this.CompareValue)))
value := sum / dataobj.JsonFloat(f.Limit)
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
}
@ -302,20 +302,20 @@ type CAvgRateFunction struct {
CompareValue float64
}
func (this CAvgRateFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < this.Limit {
func (f CAvgRateFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
if len(vs) < f.Limit {
return
}
sum := dataobj.JsonFloat(0.0)
for i := 0; i < this.Limit; i++ {
for i := 0; i < f.Limit; i++ {
sum += vs[i].Value
}
value := sum / dataobj.JsonFloat(this.Limit)
leftValue = (value - dataobj.JsonFloat(this.CompareValue)) / dataobj.JsonFloat(math.Abs(this.CompareValue))
value := sum / dataobj.JsonFloat(f.Limit)
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
}

View File

@ -36,49 +36,49 @@ func NewIndexCacheBase(max int) *IndexCacheBase {
return &IndexCacheBase{maxSize: max, data: make(map[interface{}]*dataobj.TsdbItem)}
}
func (this *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) {
this.Lock()
defer this.Unlock()
this.data[key] = item
func (i *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) {
i.Lock()
defer i.Unlock()
i.data[key] = item
}
func (this *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem {
this.RLock()
defer this.RUnlock()
return this.data[key]
func (i *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem {
i.RLock()
defer i.RUnlock()
return i.data[key]
}
func (this *IndexCacheBase) ContainsKey(key interface{}) bool {
this.RLock()
defer this.RUnlock()
return this.data[key] != nil
func (i *IndexCacheBase) ContainsKey(key interface{}) bool {
i.RLock()
defer i.RUnlock()
return i.data[key] != nil
}
func (this *IndexCacheBase) Size() int {
this.RLock()
defer this.RUnlock()
return len(this.data)
func (i *IndexCacheBase) Size() int {
i.RLock()
defer i.RUnlock()
return len(i.data)
}
func (this *IndexCacheBase) Keys() []interface{} {
this.RLock()
defer this.RUnlock()
func (i *IndexCacheBase) Keys() []interface{} {
i.RLock()
defer i.RUnlock()
count := len(this.data)
count := len(i.data)
if count == 0 {
return []interface{}{}
}
keys := make([]interface{}, 0, count)
for key := range this.data {
for key := range i.data {
keys = append(keys, key)
}
return keys
}
func (this *IndexCacheBase) Remove(key interface{}) {
this.Lock()
defer this.Unlock()
delete(this.data, key)
func (i *IndexCacheBase) Remove(key interface{}) {
i.Lock()
defer i.Unlock()
delete(i.data, key)
}

View File

@ -11,25 +11,25 @@ type ConsistentHashRing struct {
ring *consistent.Consistent
}
func (this *ConsistentHashRing) GetNode(pk string) (string, error) {
this.RLock()
defer this.RUnlock()
func (c *ConsistentHashRing) GetNode(pk string) (string, error) {
c.RLock()
defer c.RUnlock()
return this.ring.Get(pk)
return c.ring.Get(pk)
}
func (this *ConsistentHashRing) Set(r *consistent.Consistent) {
this.Lock()
defer this.Unlock()
this.ring = r
func (c *ConsistentHashRing) Set(r *consistent.Consistent) {
c.Lock()
defer c.Unlock()
c.ring = r
return
}
func (this *ConsistentHashRing) GetRing() *consistent.Consistent {
this.RLock()
defer this.RUnlock()
func (c *ConsistentHashRing) GetRing() *consistent.Consistent {
c.RLock()
defer c.RUnlock()
return this.ring
return c.ring
}
func NewConsistentHashRing(replicas int32, nodes []string) *ConsistentHashRing {