sitesAnalysisRepo.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package mongo
  2. import (
  3. "context"
  4. "fwh-toolbox-server/database"
  5. "fwh-toolbox-server/domain"
  6. "fwh-toolbox-server/dto"
  7. "time"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. const (
  13. CollectionName_SiteAnalysisConfig = "SiteAnalysisConfig"
  14. )
  15. // 从数据库获取站点分析列表
  16. func GetSiteAnalysisManageList(page, size uint, sn *string, timePeriod *[2]string, progress *string, trigger *domain.Trigger) (*dto.ResPage[domain.SiteAnalysisInfo], error) {
  17. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  18. defer cancel()
  19. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  20. filter := bson.M{}
  21. // 条件
  22. if sn != nil {
  23. filter["sn"] = *sn
  24. }
  25. if timePeriod != nil {
  26. // filter["TimePeriod.0"] = bson.M{"$lte": timePeriod[1]} // DB开始 <= 查询结束
  27. // filter["TimePeriod.1"] = bson.M{"$gte": timePeriod[0]} // DB结束 >= 查询开始
  28. filter["TimePeriod.0"] = bson.M{"$gte": timePeriod[0]} // DB开始 >= 查询开始
  29. filter["TimePeriod.1"] = bson.M{"$lte": timePeriod[1]} // DB结束 <= 查询结束
  30. }
  31. if progress != nil {
  32. filter["progress"] = *progress
  33. }
  34. if trigger != nil {
  35. filter["trigger"] = *trigger
  36. }
  37. findOptions := options.Find().
  38. SetSort(bson.D{{Key: "_id", Value: -1}}).
  39. SetProjection(bson.M{
  40. "_id": 1,
  41. "sn": 1,
  42. "description": 1,
  43. "timePeriod": 1,
  44. "item": 1,
  45. "progress": 1,
  46. "trigger": 1,
  47. }).
  48. SetSkip(int64((page - 1) * size)).
  49. SetLimit(int64(size))
  50. cursor, err := collection.Find(ctx, filter, findOptions)
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer cursor.Close(ctx)
  55. var configList []domain.SiteAnalysisInfo
  56. for cursor.Next(ctx) {
  57. var cfg domain.SiteAnalysisInfo
  58. if err := cursor.Decode(&cfg); err != nil {
  59. return nil, err
  60. }
  61. configList = append(configList, cfg)
  62. }
  63. // 查询总数
  64. total, err := collection.CountDocuments(ctx, filter)
  65. if err != nil {
  66. return nil, err
  67. }
  68. res := dto.ResPage[domain.SiteAnalysisInfo]{
  69. Data: configList,
  70. PageSize: size,
  71. PageNum: page,
  72. Total: uint(total),
  73. }
  74. return &res, nil
  75. }
  76. // 从数据库获取一条站点分析配置
  77. func GetSiteAnalysisConfig(id primitive.ObjectID) (*domain.SiteAnalysisConfig, error) {
  78. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  79. defer cancel()
  80. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  81. var cfg domain.SiteAnalysisConfig
  82. err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &cfg, nil
  87. }
  88. // 插入一条站点分析配置到数据库
  89. // auto - 是否是自动触发数据
  90. func InsertSiteAnalysisConfig(writeConfig domain.SiteAnalysisConfigWrite, auto bool) (primitive.ObjectID, error) {
  91. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  92. defer cancel()
  93. trigger := domain.TriggerManual
  94. if auto {
  95. trigger = domain.TriggerAuto
  96. }
  97. config := domain.SiteAnalysisConfig{
  98. Id: writeConfig.Id,
  99. Sn: writeConfig.Sn,
  100. Description: writeConfig.Description,
  101. TimePeriod: writeConfig.TimePeriod,
  102. Options: writeConfig.Options,
  103. Item: []string{},
  104. Progress: domain.ProgressPending,
  105. Trigger: trigger,
  106. User: writeConfig.User,
  107. }
  108. // 生成Item标签
  109. if writeConfig.Options.EnableTimeline {
  110. config.Item = append(config.Item, "时间线")
  111. }
  112. if writeConfig.Options.EnableRuntime {
  113. config.Item = append(config.Item, "运行时")
  114. }
  115. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  116. res, err := collection.InsertOne(ctx, config)
  117. if err != nil {
  118. return primitive.ObjectID{}, err
  119. }
  120. return res.InsertedID.(primitive.ObjectID), nil
  121. }
  122. // 更新一条站点分析配置到数据库
  123. func UpdateSiteAnalysisConfig(id primitive.ObjectID, writeConfig domain.SiteAnalysisConfigWrite) error {
  124. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  125. defer cancel()
  126. config := domain.SiteAnalysisConfig{
  127. Id: writeConfig.Id,
  128. Sn: writeConfig.Sn,
  129. Description: writeConfig.Description,
  130. Options: writeConfig.Options,
  131. Item: []string{},
  132. Progress: domain.ProgressPending,
  133. }
  134. // 生成Item标签
  135. if writeConfig.Options.EnableTimeline {
  136. config.Item = append(config.Item, "时间线")
  137. }
  138. if writeConfig.Options.EnableRuntime {
  139. config.Item = append(config.Item, "运行时")
  140. }
  141. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  142. // _, err := collection.InsertOne(ctx, config)
  143. filter := bson.M{"_id": id} // 按ID匹配
  144. update := bson.M{
  145. // 如果存在就更新全部字段
  146. "$set": config,
  147. }
  148. opts := options.Update().SetUpsert(true)
  149. _, err := collection.UpdateOne(ctx, filter, update, opts)
  150. if err != nil {
  151. return err
  152. }
  153. return nil
  154. }
  155. // 更新一条站点分析描述到数据库
  156. func UpdateSiteAnalysisDescription(id primitive.ObjectID, desc string) error {
  157. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  158. defer cancel()
  159. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  160. // _, err := collection.InsertOne(ctx, config)
  161. filter := bson.M{"_id": id} // 按ID匹配
  162. update := bson.M{
  163. // 如果存在就更新全部字段
  164. "$set": bson.M{
  165. "description": desc,
  166. },
  167. }
  168. opts := options.Update().SetUpsert(true)
  169. _, err := collection.UpdateOne(ctx, filter, update, opts)
  170. if err != nil {
  171. return err
  172. }
  173. return nil
  174. }
  175. // 更新一条站点分析结果到数据库
  176. func UpdateSiteAnalysisResult(id primitive.ObjectID, result domain.Progress) error {
  177. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  178. defer cancel()
  179. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  180. // _, err := collection.InsertOne(ctx, config)
  181. filter := bson.M{"_id": id} // 按ID匹配
  182. update := bson.M{
  183. // 如果存在就更新非progress字段
  184. "$set": bson.M{
  185. "progress": result,
  186. },
  187. }
  188. opts := options.Update().SetUpsert(true)
  189. _, err := collection.UpdateOne(ctx, filter, update, opts)
  190. if err != nil {
  191. return err
  192. }
  193. return nil
  194. }
  195. // 删除一条站点分析配置
  196. func DeleteSiteAnalysisConfig(id primitive.ObjectID) error {
  197. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  198. defer cancel()
  199. filter := bson.M{"_id": id} // 删除条件
  200. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  201. _, err := collection.DeleteOne(ctx, filter)
  202. if err != nil {
  203. return err
  204. }
  205. return nil
  206. }
  207. // 插入完整分析结果数据
  208. func InsertSiteAnalysisData(id primitive.ObjectID, data domain.SiteAnalysisData) error {
  209. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  210. defer cancel()
  211. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  212. // _, err := collection.InsertOne(ctx, config)
  213. filter := bson.M{"_id": id} // 按ID匹配
  214. update := bson.M{
  215. // 如果存在就更新非progress字段
  216. "$set": bson.M{
  217. "data": data,
  218. },
  219. }
  220. opts := options.Update().SetUpsert(true)
  221. _, err := collection.UpdateOne(ctx, filter, update, opts)
  222. if err != nil {
  223. return err
  224. }
  225. return nil
  226. }
  227. // 插入分析结果timeline数据
  228. func InsertSiteAnalysisTimeline(id primitive.ObjectID, timeline []domain.TimelineItem) error {
  229. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  230. defer cancel()
  231. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  232. // _, err := collection.InsertOne(ctx, config)
  233. filter := bson.M{"_id": id} // 按ID匹配
  234. update := bson.M{
  235. // 如果存在就更新非progress字段
  236. "$set": bson.M{
  237. "data.timeline": timeline,
  238. },
  239. }
  240. opts := options.Update().SetUpsert(true)
  241. _, err := collection.UpdateOne(ctx, filter, update, opts)
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. }
  247. // 插入分析结果runtime数据
  248. func InsertSiteAnalysisRuntime(id primitive.ObjectID, runtime []domain.RuntimeItem) error {
  249. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  250. defer cancel()
  251. collection := database.GetCollection(CollectionName_SiteAnalysisConfig)
  252. // _, err := collection.InsertOne(ctx, config)
  253. filter := bson.M{"_id": id} // 按ID匹配
  254. update := bson.M{
  255. // 如果存在就更新非progress字段
  256. "$set": bson.M{
  257. "data.runtime": runtime,
  258. },
  259. }
  260. opts := options.Update().SetUpsert(true)
  261. _, err := collection.UpdateOne(ctx, filter, update, opts)
  262. if err != nil {
  263. return err
  264. }
  265. return nil
  266. }