script_snap.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/admin/ep/melloi/model"
  6. "go-common/library/log"
  7. )
  8. //AddSciptSnap add scriptSnap
  9. func (s *Service) AddSciptSnap(ptestParam model.DoPtestParam, excuteId string) (scriptSnapIds []int, err error) {
  10. var (
  11. scripts []*model.Script
  12. scriptSnapId int
  13. )
  14. //场景脚本快照保存逻辑
  15. if ptestParam.Type == model.SCENE_SCRIPT_TYPE {
  16. for _, script := range ptestParam.Scripts {
  17. script.SceneID = ptestParam.SceneID
  18. if scriptSnapId, err = s.AddScriptSnapInfo(script, excuteId); err != nil {
  19. log.Error("s.AddScriptSnapInfo err :(%v)", err)
  20. return
  21. }
  22. scriptSnapIds = append(scriptSnapIds, scriptSnapId)
  23. }
  24. return
  25. }
  26. //单场景http 脚本的快照保存
  27. if scripts, err = s.QueryScripts(&model.Script{ID: ptestParam.ScriptID}, 1, 5); err != nil {
  28. log.Error(" s.QueryScripts err :(%v)", err)
  29. return
  30. }
  31. if scriptSnapId, err = s.AddScriptSnapInfo(scripts[0], excuteId); err != nil {
  32. log.Error("s.AddScriptSnapInfo err :(%v)", err)
  33. return
  34. }
  35. scriptSnapIds = append(scriptSnapIds, scriptSnapId)
  36. return
  37. }
  38. //AddScriptSnapInfo Add scriptSnap Info
  39. func (s *Service) AddScriptSnapInfo(script *model.Script, excuteId string) (scriptSnapId int, err error) {
  40. var JSON []byte
  41. scriptSnap := model.ScriptSnap{ScriptID: script.ID}
  42. script.ID = 0
  43. if JSON, err = json.Marshal(script); err != nil {
  44. log.Error(" json.Marshal(script) error :(%v)", err)
  45. return
  46. }
  47. if err = json.Unmarshal([]byte(string(JSON)), &scriptSnap); err != nil {
  48. return
  49. }
  50. scriptSnap.ExecuteID = excuteId
  51. if scriptSnapId, err = s.dao.AddScriptSnap(&scriptSnap); err != nil {
  52. log.Error("s.dao.AddScriptSnap error :(%v)", err)
  53. return
  54. }
  55. return
  56. }
  57. //AddSnap add snap
  58. func (s *Service) AddSnap(c context.Context, ptestParam model.DoPtestParam, executeID, jobName, jobNamed string) (scriptSnapIDs []int, err error) {
  59. if ptestParam.Type == model.PROTOCOL_GRPC {
  60. if scriptSnapIDs, err = s.AddGRPCSnap(ptestParam.ScriptID, executeID); err != nil {
  61. log.Error("save grpc snap failed,(%v)", err)
  62. s.DeleteJob(context.TODO(), jobName)
  63. return
  64. }
  65. } else {
  66. //用jobNamed 表示执行id,即是 ExcuteId
  67. if scriptSnapIDs, err = s.AddSciptSnap(ptestParam, jobNamed); err != nil {
  68. log.Error("s.AddSciptSnap err :(%v)", err)
  69. s.DeleteJob(context.TODO(), jobName)
  70. return
  71. }
  72. }
  73. return
  74. }
  75. // AddGRPCSnap Add GRPC Snap
  76. func (s *Service) AddGRPCSnap(grpcID int, executeID string) (snapIDs []int, err error) {
  77. var (
  78. grpc *model.GRPC
  79. grpcSnap *model.GRPCSnap
  80. j []byte
  81. )
  82. if grpc, err = s.dao.QueryGRPCByID(grpcID); err != nil {
  83. return
  84. }
  85. grpcSnap = &model.GRPCSnap{GRPCID: grpc.ID, ExecuteID: executeID}
  86. grpc.ID = 0
  87. if j, err = json.Marshal(grpc); err != nil {
  88. return
  89. }
  90. if err = json.Unmarshal([]byte(string(j)), grpcSnap); err != nil {
  91. return
  92. }
  93. if err = s.dao.CreateGRPCSnap(grpcSnap); err != nil {
  94. return
  95. }
  96. return []int{grpcSnap.ID}, nil
  97. }