123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type ItemHistoryDataMaxMinByTimeLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewItemHistoryDataMaxMinByTimeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemHistoryDataMaxMinByTimeLogic {
- return &ItemHistoryDataMaxMinByTimeLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- 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)
- }
- res := &pb.ItemHistoryDataMaxMinResp{
- MaxVal: modelData.MaxVal.Float64,
- MinVal: modelData.MinVal.Float64,
- AvgVal: modelData.AvgVal.Float64,
- }
- if useCache {
- if cacheValue, err := json.Marshal(res); err == nil {
- l.svcCtx.Cache.Setex(cacheKey, string(cacheValue), 86400)
- }
- }
- return res, nil
- }
|