| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package mongo
- import (
- "context"
- "fwh-toolbox-server/database"
- "fwh-toolbox-server/domain"
- "fwh-toolbox-server/dto"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- const (
- CollectionName_SiteAnalysisConfig = "SiteAnalysisConfig"
- )
- // 从数据库获取站点分析列表
- func GetSiteAnalysisManageList(page, size uint, sn *string, timePeriod *[2]string, progress *string, trigger *domain.Trigger) (*dto.ResPage[domain.SiteAnalysisInfo], error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- filter := bson.M{}
- // 条件
- if sn != nil {
- filter["sn"] = *sn
- }
- if timePeriod != nil {
- // filter["TimePeriod.0"] = bson.M{"$lte": timePeriod[1]} // DB开始 <= 查询结束
- // filter["TimePeriod.1"] = bson.M{"$gte": timePeriod[0]} // DB结束 >= 查询开始
- filter["TimePeriod.0"] = bson.M{"$gte": timePeriod[0]} // DB开始 >= 查询开始
- filter["TimePeriod.1"] = bson.M{"$lte": timePeriod[1]} // DB结束 <= 查询结束
- }
- if progress != nil {
- filter["progress"] = *progress
- }
- if trigger != nil {
- filter["trigger"] = *trigger
- }
- findOptions := options.Find().
- SetSort(bson.D{{Key: "_id", Value: -1}}).
- SetProjection(bson.M{
- "_id": 1,
- "sn": 1,
- "description": 1,
- "timePeriod": 1,
- "item": 1,
- "progress": 1,
- "trigger": 1,
- }).
- SetSkip(int64((page - 1) * size)).
- SetLimit(int64(size))
- cursor, err := collection.Find(ctx, filter, findOptions)
- if err != nil {
- return nil, err
- }
- defer cursor.Close(ctx)
- var configList []domain.SiteAnalysisInfo
- for cursor.Next(ctx) {
- var cfg domain.SiteAnalysisInfo
- if err := cursor.Decode(&cfg); err != nil {
- return nil, err
- }
- configList = append(configList, cfg)
- }
- // 查询总数
- total, err := collection.CountDocuments(ctx, filter)
- if err != nil {
- return nil, err
- }
- res := dto.ResPage[domain.SiteAnalysisInfo]{
- Data: configList,
- PageSize: size,
- PageNum: page,
- Total: uint(total),
- }
- return &res, nil
- }
- // 从数据库获取一条站点分析配置
- func GetSiteAnalysisConfig(id primitive.ObjectID) (*domain.SiteAnalysisConfig, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- var cfg domain.SiteAnalysisConfig
- err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
- if err != nil {
- return nil, err
- }
- return &cfg, nil
- }
- // 插入一条站点分析配置到数据库
- // auto - 是否是自动触发数据
- func InsertSiteAnalysisConfig(writeConfig domain.SiteAnalysisConfigWrite, auto bool) (primitive.ObjectID, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- trigger := domain.TriggerManual
- if auto {
- trigger = domain.TriggerAuto
- }
- config := domain.SiteAnalysisConfig{
- Id: writeConfig.Id,
- Sn: writeConfig.Sn,
- Description: writeConfig.Description,
- TimePeriod: writeConfig.TimePeriod,
- Options: writeConfig.Options,
- Item: []string{},
- Progress: domain.ProgressPending,
- Trigger: trigger,
- User: writeConfig.User,
- }
- // 生成Item标签
- if writeConfig.Options.EnableTimeline {
- config.Item = append(config.Item, "时间线")
- }
- if writeConfig.Options.EnableRuntime {
- config.Item = append(config.Item, "运行时")
- }
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- res, err := collection.InsertOne(ctx, config)
- if err != nil {
- return primitive.ObjectID{}, err
- }
- return res.InsertedID.(primitive.ObjectID), nil
- }
- // 更新一条站点分析配置到数据库
- func UpdateSiteAnalysisConfig(id primitive.ObjectID, writeConfig domain.SiteAnalysisConfigWrite) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- config := domain.SiteAnalysisConfig{
- Id: writeConfig.Id,
- Sn: writeConfig.Sn,
- Description: writeConfig.Description,
- Options: writeConfig.Options,
- Item: []string{},
- Progress: domain.ProgressPending,
- }
- // 生成Item标签
- if writeConfig.Options.EnableTimeline {
- config.Item = append(config.Item, "时间线")
- }
- if writeConfig.Options.EnableRuntime {
- config.Item = append(config.Item, "运行时")
- }
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新全部字段
- "$set": config,
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新一条站点分析描述到数据库
- func UpdateSiteAnalysisDescription(id primitive.ObjectID, desc string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新全部字段
- "$set": bson.M{
- "description": desc,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新一条站点分析结果到数据库
- func UpdateSiteAnalysisResult(id primitive.ObjectID, result domain.Progress) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新非progress字段
- "$set": bson.M{
- "progress": result,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 删除一条站点分析配置
- func DeleteSiteAnalysisConfig(id primitive.ObjectID) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- filter := bson.M{"_id": id} // 删除条件
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- _, err := collection.DeleteOne(ctx, filter)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入完整分析结果数据
- func InsertSiteAnalysisData(id primitive.ObjectID, data domain.SiteAnalysisData) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新非progress字段
- "$set": bson.M{
- "data": data,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入分析结果timeline数据
- func InsertSiteAnalysisTimeline(id primitive.ObjectID, timeline []domain.TimelineItem) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新非progress字段
- "$set": bson.M{
- "data.timeline": timeline,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入分析结果runtime数据
- func InsertSiteAnalysisRuntime(id primitive.ObjectID, runtime []domain.RuntimeItem) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新非progress字段
- "$set": bson.M{
- "data.runtime": runtime,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
|