|
@@ -22,6 +22,7 @@ func SetOptions(options Options) {
|
|
|
}
|
|
|
|
|
|
fetchMultiItem = options.FetchMultiItem
|
|
|
+ adjustValue = options.AdjustValue
|
|
|
}
|
|
|
|
|
|
func (m MultiEnvItem) GetProjectId() int64 {
|
|
@@ -219,6 +220,13 @@ func (e *EnvItem) getCurrentData() (*ItemValueResp, error) {
|
|
|
return nil, errors.New("not found envitem's value")
|
|
|
}
|
|
|
|
|
|
+ if adjustValue && cache != nil && len(res.Data) > 0 {
|
|
|
+ if rv, err := strconv.ParseFloat(res.Data[0].Val, 64); err == nil {
|
|
|
+ adjust, _ := e.GetAdjustFloat64Val()
|
|
|
+ res.Data[0].Val = fmt.Sprintf("%f", rv+adjust)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return res.Data[0], nil
|
|
|
}
|
|
|
|
|
@@ -359,6 +367,65 @@ func (e *EnvItem) GetItemPrevHtime() *time.Time {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (e *EnvItem) GetAdjustInt64Val() (int64, error) {
|
|
|
+ return getAdjustInt64Val(e.ProjectId, e.Item)
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EnvItem) GetAdjustFloat64Val() (float64, error) {
|
|
|
+ return getAdjustFloat64Val(e.ProjectId, e.Item)
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EnvItem) GetAdjustStringVal() (string, error) {
|
|
|
+ return getAdjustStringVal(e.ProjectId, e.Item)
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EnvItem) SetAdjust(value string, expire time.Duration) error {
|
|
|
+ if adjustValue == false {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return errors.New("not cache")
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, e.ProjectId, e.Item)
|
|
|
+ scmd := cache.Set(context.Background(), key, value, expire)
|
|
|
+ return scmd.Err()
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EnvItem) IncreAdjust(expire time.Duration) (int64, error) {
|
|
|
+ if adjustValue == false {
|
|
|
+ return 0, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return 0, errors.New("not cache")
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, e.ProjectId, e.Item)
|
|
|
+ intCmd := cache.Incr(context.Background(), key)
|
|
|
+ if intCmd.Err() != nil {
|
|
|
+ return 0, intCmd.Err()
|
|
|
+ }
|
|
|
+
|
|
|
+ cache.Expire(context.Background(), key, expire)
|
|
|
+
|
|
|
+ return intCmd.Val(), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EnvItem) ClearAdjust() {
|
|
|
+ if adjustValue == false {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, e.ProjectId, e.Item)
|
|
|
+ cache.Del(context.Background(), key)
|
|
|
+}
|
|
|
+
|
|
|
func (e *EnvItem) clearValue() {
|
|
|
e.Value = nil
|
|
|
e.Htime = ""
|
|
@@ -373,3 +440,54 @@ func (d *MultiEnvItem) Scan(input interface{}) error {
|
|
|
func (d MultiEnvItem) Value() (driver.Value, error) {
|
|
|
return json.Marshal(d)
|
|
|
}
|
|
|
+
|
|
|
+func getAdjustInt64Val(projectId int64, item string) (int64, error) {
|
|
|
+ if adjustValue == false {
|
|
|
+ return 0, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return 0, errors.New("not cache")
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, projectId, item)
|
|
|
+ scmd := cache.Get(context.Background(), key)
|
|
|
+ if scmd != nil && scmd.Err() != nil {
|
|
|
+ return 0, scmd.Err()
|
|
|
+ }
|
|
|
+ return scmd.Int64()
|
|
|
+}
|
|
|
+
|
|
|
+func getAdjustFloat64Val(projectId int64, item string) (float64, error) {
|
|
|
+ if adjustValue == false {
|
|
|
+ return 0, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return 0, errors.New("not cache")
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, projectId, item)
|
|
|
+ scmd := cache.Get(context.Background(), key)
|
|
|
+ if scmd != nil && scmd.Err() != nil {
|
|
|
+ return 0, scmd.Err()
|
|
|
+ }
|
|
|
+ return scmd.Float64()
|
|
|
+}
|
|
|
+
|
|
|
+func getAdjustStringVal(projectId int64, item string) (string, error) {
|
|
|
+ if adjustValue == false {
|
|
|
+ return "", nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if cache == nil {
|
|
|
+ return "", errors.New("not cache")
|
|
|
+ }
|
|
|
+
|
|
|
+ key := fmt.Sprintf(CACHE_ADJUST_VALUE_KEY, projectId, item)
|
|
|
+ scmd := cache.Get(context.Background(), key)
|
|
|
+ if scmd != nil && scmd.Err() != nil {
|
|
|
+ return "", scmd.Err()
|
|
|
+ }
|
|
|
+ return scmd.String(), nil
|
|
|
+}
|