|
@@ -3,7 +3,10 @@ package logic
|
|
|
import (
|
|
|
"GtDataStore/common/xerr"
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
"github.com/pkg/errors"
|
|
|
+ "time"
|
|
|
|
|
|
"GtDataStore/app/cmd/organization/internal/svc"
|
|
|
"GtDataStore/app/cmd/organization/pb"
|
|
@@ -26,13 +29,47 @@ func NewItemHistoryDataMaxMinByTimeLogic(ctx context.Context, svcCtx *svc.Servic
|
|
|
}
|
|
|
|
|
|
func (l *ItemHistoryDataMaxMinByTimeLogic) ItemHistoryDataMaxMinByTime(in *pb.ItemHistoryDataByTimeReq) (*pb.ItemHistoryDataMaxMinResp, error) {
|
|
|
+ st, sterr := time.Parse("2006-01-02 15:04:05", in.Stime)
|
|
|
+ et, eterr := time.Parse("2006-01-02 15:04:05", in.Etime)
|
|
|
+
|
|
|
+ if sterr != nil || eterr != nil {
|
|
|
+ return nil, errors.New("time parse error")
|
|
|
+ }
|
|
|
+
|
|
|
+ useCache, cacheKey := false, ""
|
|
|
+ if et.Sub(st).Seconds() > 86400 {
|
|
|
+ useCache = true
|
|
|
+ std, etd := st.Format("2006-01-02"), et.Format("2006-01-02")
|
|
|
+ cacheKey = fmt.Sprintf("datastore:cache:min:max:%d:%s:%s:%s", in.ProjectId, in.ItemName, std, etd)
|
|
|
+ }
|
|
|
+
|
|
|
+ if useCache {
|
|
|
+ if s, err := l.svcCtx.Cache.Get(cacheKey); err == nil && s != "" {
|
|
|
+ res := pb.ItemHistoryDataMaxMinResp{}
|
|
|
+ if err = json.Unmarshal([]byte(s), &res); err != nil {
|
|
|
+ l.svcCtx.Cache.Del(cacheKey)
|
|
|
+ } else {
|
|
|
+ return &res, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
modelData, err := l.svcCtx.ItemHistoryData.QueryHistoryDataMaxMinByTime(l.ctx, in)
|
|
|
if err != nil {
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "model QueryHistoryDataMaxMinByTime get data err:%v", err)
|
|
|
}
|
|
|
- return &pb.ItemHistoryDataMaxMinResp{
|
|
|
+
|
|
|
+ res := &pb.ItemHistoryDataMaxMinResp{
|
|
|
MaxVal: modelData.MaxVal.Float64,
|
|
|
MinVal: modelData.MinVal.Float64,
|
|
|
AvgVal: modelData.AvgVal.Float64,
|
|
|
- }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if useCache {
|
|
|
+ if cacheValue, err := json.Marshal(res); err == nil {
|
|
|
+ l.svcCtx.Cache.Setex(cacheKey, string(cacheValue), 86400)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res, nil
|
|
|
}
|