animeic 2 жил өмнө
parent
commit
7715aef020

+ 95 - 211
queencount/api/service-asset.go

@@ -68,78 +68,53 @@ func GetAssetCount(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			countDefitem.DefKey = v.Collection
 			countDefitem.DefName = v.Label
 
-			parm := &repo.DbDocsSearchOptions{
-				Db: dbName,
-				// Query:       map[string]interface{}{"ownerId": apictx.User.ID},
-				CollectName: v.Collection,
-				Project:     []string{"_id"},
-			}
-
-			ok, res := repo.DbRepoSeachDocsMap(apictx.CreateRepoCtx(), parm)
-			if ok {
-				countDefitem.DefItemNum = len(res)
-				defMeshSet = append(defMeshSet, countDefitem)
-				assetDefCountInfo.Mesh = defMeshSet
+			count, err := repo.RepoDbCountDoc(apictx.CreateRepoCtx(), dbName, v.Collection, repo.Map{})
+			if err != nil {
+				return nil, err
 			}
+			countDefitem.DefItemNum = int(count)
+			defMeshSet = append(defMeshSet, countDefitem)
+			assetDefCountInfo.Mesh = defMeshSet
 
 		}
 		if v.Type == 20 {
 			countDefitem.DefKey = v.Collection
 			countDefitem.DefName = v.Label
 
-			parm := &repo.DbDocsSearchOptions{
-				Db: dbName,
-				// Query:       map[string]interface{}{"ownerId": apictx.User.ID},
-				CollectName: v.Collection,
-				Project:     []string{"_id"},
-			}
-
-			ok, res := repo.DbRepoSeachDocsMap(apictx.CreateRepoCtx(), parm)
-			if ok {
-				countDefitem.DefItemNum = len(res)
-				defImageSet = append(defImageSet, countDefitem)
-				assetDefCountInfo.Image = defImageSet
-
+			count, err := repo.RepoDbCountDoc(apictx.CreateRepoCtx(), dbName, v.Collection, repo.Map{})
+			if err != nil {
+				return nil, err
 			}
+			countDefitem.DefItemNum = int(count)
+			defImageSet = append(defImageSet, countDefitem)
+			assetDefCountInfo.Image = defImageSet
 
 		}
 		if v.Type == 30 {
 			countDefitem.DefKey = v.Collection
 			countDefitem.DefName = v.Label
 
-			parm := &repo.DbDocsSearchOptions{
-				Db: dbName,
-				// Query:       map[string]interface{}{"ownerId": apictx.User.ID},
-				CollectName: v.Collection,
-				Project:     []string{"_id"},
+			count, err := repo.RepoDbCountDoc(apictx.CreateRepoCtx(), dbName, v.Collection, repo.Map{})
+			if err != nil {
+				return nil, err
 			}
+			countDefitem.DefItemNum = int(count)
+			defMaterialSet = append(defMaterialSet, countDefitem)
+			assetDefCountInfo.Material = defMaterialSet
 
-			ok, res := repo.DbRepoSeachDocsMap(apictx.CreateRepoCtx(), parm)
-			if ok {
-				countDefitem.DefItemNum = len(res)
-				defMaterialSet = append(defMaterialSet, countDefitem)
-				assetDefCountInfo.Material = defMaterialSet
-
-			}
 		}
 		if v.Type == 40 {
 			countDefitem.DefKey = v.Collection
 			countDefitem.DefName = v.Label
 
-			parm := &repo.DbDocsSearchOptions{
-				Db: dbName,
-				// Query:       map[string]interface{}{"ownerId": apictx.User.ID},
-				CollectName: v.Collection,
-				Project:     []string{"_id"},
+			count, err := repo.RepoDbCountDoc(apictx.CreateRepoCtx(), dbName, v.Collection, repo.Map{})
+			if err != nil {
+				return nil, err
 			}
 
-			ok, res := repo.DbRepoSeachDocsMap(apictx.CreateRepoCtx(), parm)
-			if ok {
-				countDefitem.DefItemNum = len(res)
-				defEnv3dSet = append(defEnv3dSet, countDefitem)
-				assetDefCountInfo.Env3d = defEnv3dSet
-
-			}
+			countDefitem.DefItemNum = int(count)
+			defEnv3dSet = append(defEnv3dSet, countDefitem)
+			assetDefCountInfo.Env3d = defEnv3dSet
 		}
 
 	}
@@ -170,63 +145,28 @@ type DefInfo struct {
 	DefKey  string
 }
 
-type DefCateInfo struct {
-	Mesh     []DefInfo
-	Image    []DefInfo
-	Material []DefInfo
-	Env3d    []DefInfo
+type ReqAssetCount struct {
+	DbId      string `json:"dbId"`
+	Step      int    `json:"step"`
+	StartTime string `json:"startTime"`
+	EndTime   string `json:"endTime"`
 }
 
-// 查找类型分类信息
-func GetDefCate(dbId string, ctx *repo.RepoSession) (defCateInfo DefCateInfo, err error) {
-	var database model.Database
-	var defInfo DefInfo
-	query := make(map[string]interface{})
-	query["_id"], _ = primitive.ObjectIDFromHex(dbId)
-
-	param := &repo.DocSearchOptions{
-		CollectName: repo.CollectionDatabase,
-		Query:       query,
-		Project:     []string{"_id", "assets"},
-	}
-	ok, err := repo.RepoSeachDoc(ctx, param, &database)
-	if err != nil || !ok {
-		return defCateInfo, errors.New("查询数据错误")
-	}
-
-	for _, v := range database.Assets {
-		defInfo.DefKey = v.Collection
-		defInfo.DefName = v.Label
-		defInfo.DefId = v.Id
-
-		// mesh
-		if v.Type == 10 {
-			defCateInfo.Mesh = append(defCateInfo.Mesh, defInfo)
-		}
-		// image
-		if v.Type == 20 {
-			defCateInfo.Image = append(defCateInfo.Image, defInfo)
-		}
-		// material
-		if v.Type == 30 {
-			defCateInfo.Material = append(defCateInfo.Material, defInfo)
-		}
-		// env3d
-		if v.Type == 40 {
-			defCateInfo.Env3d = append(defCateInfo.Env3d, defInfo)
-		}
+const DateTimeLayout = "2006-01-02 15:04:05"
 
-	}
+func (req *ReqAssetCount) GetStartTime() (time.Time, error) {
+	return time.Parse(DateTimeLayout, req.StartTime)
+}
 
-	return defCateInfo, nil
+func (req *ReqAssetCount) GetEndTime() (time.Time, error) {
+	return time.Parse(DateTimeLayout, req.EndTime)
 }
 
-// TODO 需要优化
-type ReqAssetCount struct {
-	DbId      string `json:"dbId"`
-	Step      int    `json:"step"`
-	StartTime string `json:"startTime"`
-	EndTime   string `json:"endTime"`
+type TypeCount struct {
+	Time    []*model.AssetCount `json:"times"`
+	DefName string              `json:"defName"`
+	DefType int                 `json:"defType"`
+	DefId   string              `json:"defId"`
 }
 
 // 获取统计信息图表需要的信息
@@ -250,12 +190,17 @@ func GetInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if form.DbId == "" {
 		return nil, errors.New("数据库id不为空")
 	}
+	startTime, err := form.GetStartTime()
+	if err != nil {
+		return nil, err
+	}
+	endTime, err := form.GetEndTime()
+	if err != nil {
+		return nil, err
+	}
 
 	ctx := apictx.CreateRepoCtx()
-	timeSet := GetCountTimeSet(form)
 
-	query := make(map[string]interface{})
-	// query["dbId"], _ = primitive.ObjectIDFromHex(form.DbId)
 	dbId, _ := primitive.ObjectIDFromHex(form.DbId)
 
 	// 根据id查询数据库名
@@ -270,136 +215,75 @@ func GetInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("资产数据为空")
 	}
 
-	// 查数据名下对应的assetcount
-	parm := &repo.DocSearchOptions{
+	searchTime := time.Now()
+
+	listCounts := []*model.AssetCount{}
+	err = repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		Db:          databaseDoc.Name,          // 对应资产数据库
 		CollectName: repo.CollectionAssetCount, // 统计表
-		Project:     []string{"_id", "defId", "createTime"},
-		Sort:        bson.M{"createTime": -1}, // 降序 小于等于createTime中取时间最近的 第一条
-	}
-
-	var assetCount model.AssetCount
-
-	// 遍历获取 asset.id ->defId,defKey,defName
-
-	if databaseDoc.Assets == nil {
-		return nil, errors.New("资产不存在")
+		Query: repo.Map{"$and": []bson.M{
+			bson.M{"createTime": bson.M{"$lte": endTime}},
+			bson.M{"createTime": bson.M{"$gte": startTime}},
+		}},
+		Project: []string{"defId", "createTime", "count"},
+		// Sort:    bson.M{"createTime": -1}, // 降序 小于等于createTime中取时间最近的 第一条
+
+	}, &listCounts)
+	if err != nil {
+		return nil, err
 	}
-	// 返回的数据
-	var countInfo CountInfo
-	// 各项数据
-	var meshCountTime CountTime
-	var meshDefItem DefItem
-
-	var imageCountTime CountTime
-	var imageDefItem DefItem
-
-	var materialCountTime CountTime
-	var materialDefItem DefItem
-
-	var env3dCountTime CountTime
-	var env3dDefItem DefItem
-
-	for _, asset := range databaseDoc.Assets {
-		// mesh
-		if asset.Type == 10 {
-			meshDefItem.DefKey = asset.Collection
-			meshDefItem.DefName = asset.Label
-
-			for _, timePoint := range timeSet {
-				// 拼接条件 查询各个时间段的数据
-				query["defId"] = asset.Id
-				parm.Query = query
-				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-				if !bl || err != nil {
-					meshCountTime.Count = 0
-				} else {
-					meshCountTime.Count = assetCount.Count
-				}
-				meshCountTime.TimePoint = timePoint
-				meshDefItem.Time = append(meshDefItem.Time, meshCountTime)
 
-			}
-			countInfo.Mesh = append(countInfo.Mesh, meshDefItem)
+	defCountsMap := map[string]*TypeCount{}
+	listCountIndex := len(listCounts)
+	for {
+		if listCountIndex == 0 {
+			break
 		}
+		listCountIndex -= 1
 
-		// image
-
-		if asset.Type == 20 {
-			imageDefItem.DefKey = asset.Collection
-			imageDefItem.DefName = asset.Label
-
-			for _, timePoint := range timeSet {
-				// 拼接条件 查询各个增量时间段的数据
-				query["defId"] = asset.Id
-				parm.Query = query
-				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-				if !bl || err != nil {
-					imageCountTime.Count = 0
-				} else {
-					imageCountTime.Count = assetCount.Count
-				}
-				imageCountTime.TimePoint = timePoint
-				imageDefItem.Time = append(imageDefItem.Time, imageCountTime)
+		item := listCounts[listCountIndex]
+		defId := item.DefId
 
-			}
-			countInfo.Image = append(countInfo.Image, imageDefItem)
+		if defCountsMap[defId] == nil {
+			defCountsMap[defId] = &TypeCount{Time: []*model.AssetCount{item}}
+			continue
 		}
 
-		// material
-		if asset.Type == 30 {
-			materialDefItem.DefKey = asset.Collection
-			materialDefItem.DefName = asset.Label
-
-			for _, timePoint := range timeSet {
-				// 拼接条件 查询各个增量时间段的数据
-				query["defId"] = asset.Id
-				parm.Query = query
-				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-				if !bl || err != nil {
-					imageCountTime.Count = 0
-				} else {
-					materialCountTime.Count = assetCount.Count
-				}
-				materialCountTime.TimePoint = timePoint
-				materialDefItem.Time = append(materialDefItem.Time, materialCountTime)
+		typeList := defCountsMap[defId]
+		lastType := typeList.Time[len(typeList.Time)-1]
 
-			}
-			countInfo.Material = append(countInfo.Material, materialDefItem)
+		b, _ := time.ParseDuration(fmt.Sprintf("%ds", form.Step))
+		if item.CreateTime.Unix() <= lastType.CreateTime.Add(b*-1).Unix() {
+			typeList.Time = append(typeList.Time, item)
 		}
+	}
 
-		// env3d
-		if asset.Type == 40 {
-			env3dDefItem.DefKey = asset.Collection
-			env3dDefItem.DefName = asset.Label
-
-			for _, timePoint := range timeSet {
-				// 拼接条件 查询各个增量时间段的数据
-				query["defId"] = asset.Id
-				parm.Query = query
-				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-				if !bl || err != nil {
-					env3dCountTime.Count = 0
-				} else {
-					env3dCountTime.Count = assetCount.Count
-				}
-				env3dCountTime.TimePoint = timePoint
-				env3dDefItem.Time = append(env3dDefItem.Time, env3dCountTime)
+	out := []*TypeCount{}
 
+	for defId, _ := range defCountsMap {
+		for _, v := range databaseDoc.Assets {
+			if v.Id == defId {
+				defCountsMap[defId].DefType = v.Type
+				defCountsMap[defId].DefName = v.Label
+				defCountsMap[defId].DefId = v.Id
+				break
 			}
-			countInfo.Env3d = append(countInfo.Env3d, env3dDefItem)
 		}
-
+		out = append(out, defCountsMap[defId])
 	}
 
-	return countInfo, nil
+	return map[string]interface{}{
+		"types": out,
+		"time":  time.Now().Sub(searchTime).Seconds(),
+	}, nil
+
 }
 
 // 获取需要查询数据的时间点
 func GetCountTimeSet(form ReqAssetCount) (timeSet []time.Time) {
 	// 计算有多少个时间段
-	startTime, _ := time.Parse("2006-01-02 15:04:05", form.StartTime)
-	endTime, _ := time.Parse("2006-01-02 15:04:05", form.EndTime)
+	startTime, _ := time.Parse(DateTimeLayout, form.StartTime)
+	endTime, _ := time.Parse(DateTimeLayout, form.EndTime)
 	duration := endTime.Unix() - startTime.Unix()
 	counts := int(duration / int64(form.Step)) // 取整
 

+ 16 - 18
queencount/bus/asset_action.go

@@ -8,7 +8,6 @@ import (
 	"queencount/db/repo"
 	"time"
 
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -27,39 +26,38 @@ func AssetCountAction(actionName string, req *model.AssetEvent) {
 		// Db: "queentree",
 		CollectName: repo.CollectionDatabase,
 		Query:       map[string]interface{}{"_id": dbId},
-		Project:     []string{"_id", "name"},
+		Project:     []string{"_id", "name", "assets"},
 		// Sort: bson.M{},
 	}
 
 	// 获取对应资产库 - 数据库
-	repo.RepoSeachDoc(dbcxt, param, &databaseDoc)
+	ok, err := repo.RepoSeachDoc(dbcxt, param, &databaseDoc)
 	fmt.Printf("action database data =>\n%#v\n", databaseDoc)
+	if err != nil || !ok {
+		return
+	}
 
-	// 跨库查询 对应的 assetcount文档
-	param1 := &repo.DocSearchOptions{
-		Db:          databaseDoc.Name,
-		CollectName: repo.CollectionAssetCount,
-		Query:       map[string]interface{}{"defId": req.DefId},
-		Project:     []string{"_id", "count"},
-		Sort:        bson.M{"createTime": -1},
+	collection := ""
+	for _, v := range databaseDoc.Assets {
+		if v.Id == req.DefId {
+			collection = v.Collection
+		}
 	}
-	// 拿到最新一条数据 获取count
-	var countData model.AssetCount
-	repo.RepoSeachDoc(dbcxt, param1, &countData)
 
-	fmt.Printf("action assetCount data =>\n%#v\n", countData)
+	count, err := repo.RepoDbCountDoc(dbcxt, databaseDoc.Name, collection, repo.Map{})
+	if err != nil {
+		return
+	}
 
 	// 统计新增
 	in.DefId = req.DefId
 	if actionName == "add" {
-		in.Count = countData.Count + 1
+		in.Count = int(count)
 
 	}
 	if actionName == "remove" {
-		in.Count = countData.Count - 1
-
+		in.Count = int(count)
 	}
-
 	in.CreateTime = time.Now()
 	id, _ := repo.RepoDbAddDoc(dbcxt, databaseDoc.Name, repo.CollectionAssetCount, &in)
 	fmt.Printf("action add id =>\n%#v\n", id)

+ 22 - 1
queencount/db/repo/repo.go

@@ -272,7 +272,24 @@ func RepoPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult,
 	return
 }
 
-// PageSearch 单表分页查询
+func RepoDbCountDoc(ctx *RepoSession, db string, collectionName string, Query Map) (int64, error) {
+	colls := ctx.Client.GetDbCollection(db, collectionName)
+	filter := bson.M{}
+	if len(Query) > 0 {
+		for k, v := range Query {
+			if value, ok := v.(string); ok {
+				if len(value) > 0 {
+					filter[k] = v
+					continue
+				}
+			} else {
+				filter[k] = v
+			}
+		}
+	}
+	return colls.CountDocuments(ctx.Ctx, filter)
+}
+
 func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
 
 	colls := ctx.Client.GetCollection(collectionName)
@@ -296,6 +313,10 @@ func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, er
 func RepoDocsSearch(ctx *RepoSession, para *PageSearchOptions, out interface{}) (err error) {
 
 	colls := ctx.Client.GetCollection(para.CollectName)
+	if len(para.Db) > 0 {
+		colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
+	}
+
 	findoptions := &options.FindOptions{}
 
 	if para.Size > 0 {