bugly.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os/exec"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "go-common/app/service/ep/footman/model"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. )
  15. const (
  16. _buglyOkCode = 200
  17. _issueDetailCode = 100000
  18. _issueDetailList = "/v2/lastCrashInfo/appId/%s/platformId/%s/issues/%s?offsetTop=56&fsn=6d0260aa-331f-48b9-8557-c2aaf6e0be90"
  19. _issueList = "/v2/issueList?sortOrder=desc&sortField=uploadTime&rows=50&fsn=45cdb5aa-eb6f-4bda-9bba-ba0b264bfc93&appId=%s&platformId=%s&version=%s&start=%s&rows=%s&exceptionTypeList=%s"
  20. _issueVersionList = "/v2/getSelector/appId/%s/platformId/%s?types=version&fsn=8b8782b5-053d-4f58-bc17-d5c43d7f5ece"
  21. _issueExceptionList = "/v2/issueInfo/appId/%s/platformId/%s/issueId/%s/exceptionTypeList/Crash,Native,ExtensionCrash?fsn=114a8d02-586d-4fe4-8c23-79003fbe6882"
  22. )
  23. // BugVersion Bug Version .
  24. func (d *Dao) BugVersion(c context.Context, projectID, platformID string) (ret []*model.BugVersion, err error) {
  25. var (
  26. req *http.Request
  27. res *model.BugVersionResponse
  28. cookie string
  29. token string
  30. hostStr string
  31. )
  32. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueVersionList, projectID, platformID)
  33. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  34. return
  35. }
  36. if cookie, token, err = d.cookieAndToken(); err != nil {
  37. return
  38. }
  39. req.Header.Set("Cookie", cookie)
  40. req.Header.Set("x-token", token)
  41. req.Header.Set("content-type", "application/json;charset=utf-8")
  42. req.Header.Set("x-csrf-token", "undefined")
  43. if err = d.httpClient.Do(c, req, &res); err != nil {
  44. log.Error("d.BugVersion url(%s) err(%v)", "BugVersion", err)
  45. return
  46. }
  47. if res.Status != _buglyOkCode {
  48. err = ecode.MartheBuglyErr
  49. log.Error("Status url(%s) res(%v) err(%v)", "BugVersion", res, err)
  50. log.Error("maybe need to update cookie and token")
  51. return
  52. }
  53. ret = res.Ret.BugVersionList
  54. return
  55. }
  56. // BuglyIssueAndRetry Bugly Issue And Retry.
  57. func (d *Dao) BuglyIssueAndRetry(c context.Context, bugIssueRequest *model.BugIssueRequest) (ret *model.BugRet, err error) {
  58. for i := 0; i < 3; i++ {
  59. if ret, err = d.BuglyIssue(c, bugIssueRequest); err == nil {
  60. break
  61. }
  62. }
  63. return
  64. }
  65. // BuglyIssue Get Issue.
  66. func (d *Dao) BuglyIssue(c context.Context, bugIssueRequest *model.BugIssueRequest) (ret *model.BugRet, err error) {
  67. var (
  68. req *http.Request
  69. res *model.BugIssueResponse
  70. cookie string
  71. token string
  72. hostStr string
  73. )
  74. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueList, bugIssueRequest.ProjectID, bugIssueRequest.PlatformID, bugIssueRequest.Version, strconv.Itoa(bugIssueRequest.StartNum), strconv.Itoa(bugIssueRequest.Rows), bugIssueRequest.ExceptionType)
  75. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  76. return
  77. }
  78. if cookie, token, err = d.cookieAndToken(); err != nil {
  79. return
  80. }
  81. req.Header.Set("Cookie", cookie)
  82. req.Header.Set("x-token", token)
  83. req.Header.Set("content-type", "application/json;charset=utf-8")
  84. req.Header.Set("x-csrf-token", "undefined")
  85. if err = d.httpClient.Do(c, req, &res); err != nil {
  86. log.Error("d.BuglyIssue url(%s) err(%v)", "BuglyIssue", err)
  87. return
  88. }
  89. if res.Status != _buglyOkCode {
  90. err = ecode.MartheBuglyErr
  91. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssue", res, err)
  92. log.Error("maybe need to update cookie and token")
  93. return
  94. }
  95. ret = res.Ret
  96. return
  97. }
  98. // BuglyIssueDetailAndRetry Bugly Issue Detail And Retry.
  99. func (d *Dao) BuglyIssueDetailAndRetry(c context.Context, projectID, platformID, issueNo string) (bugIssueDetail *model.BugIssueDetail, err error) {
  100. for i := 0; i < 3; i++ {
  101. if bugIssueDetail, err = d.BuglyIssueDetail(c, projectID, platformID, issueNo); err == nil {
  102. break
  103. }
  104. }
  105. return
  106. }
  107. // BuglyIssueDetail Get Issue Detail.
  108. func (d *Dao) BuglyIssueDetail(c context.Context, projectID, platformID, issueNo string) (bugIssueDetail *model.BugIssueDetail, err error) {
  109. var (
  110. req *http.Request
  111. res *model.BugIssueDetailResponse
  112. cookie string
  113. token string
  114. hostStr string
  115. )
  116. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueDetailList, projectID, platformID, issueNo)
  117. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  118. return
  119. }
  120. if cookie, token, err = d.cookieAndToken(); err != nil {
  121. return
  122. }
  123. req.Header.Set("Cookie", cookie)
  124. req.Header.Set("x-token", token)
  125. req.Header.Set("content-type", "application/json;charset=utf-8")
  126. req.Header.Set("x-csrf-token", "undefined")
  127. if err = d.httpClient.Do(c, req, &res); err != nil {
  128. log.Error("d.BuglyIssue url(%s) err(%v)", "BuglyIssue", err)
  129. return
  130. }
  131. if res.Code != _issueDetailCode {
  132. err = ecode.MartheBuglyErr
  133. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssue", res, err)
  134. return
  135. }
  136. bugIssueDetail = res.Data
  137. return
  138. }
  139. // BuglyIssueExceptionList Bugly Issue Exception List.
  140. func (d *Dao) BuglyIssueExceptionList(c context.Context, projectID, platformID, issueNo string) (bugIssueException *model.IssueException, err error) {
  141. var (
  142. req *http.Request
  143. res *model.BugIssueExceptionListResponse
  144. cookie string
  145. token string
  146. hostStr string
  147. )
  148. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueExceptionList, projectID, platformID, issueNo)
  149. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  150. return
  151. }
  152. if cookie, token, err = d.cookieAndToken(); err != nil {
  153. return
  154. }
  155. req.Header.Set("Cookie", cookie)
  156. req.Header.Set("x-token", token)
  157. req.Header.Set("content-type", "application/json;charset=utf-8")
  158. req.Header.Set("x-csrf-token", "undefined")
  159. if err = d.httpClient.Do(c, req, &res); err != nil {
  160. log.Error("d.BuglyIssueExceptionList url(%s) err(%v)", "BuglyIssueExceptionList", err)
  161. return
  162. }
  163. if res.Status != _buglyOkCode {
  164. err = ecode.MartheBuglyErr
  165. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssueExceptionList", res, err)
  166. return
  167. }
  168. if res.Ret != nil && len(res.Ret.IssueException) != 0 && res.Ret.IssueException[0].IssueID == issueNo {
  169. bugIssueException = res.Ret.IssueException[0]
  170. }
  171. return
  172. }
  173. func (d *Dao) cookieAndToken() (cookie, token string, err error) {
  174. var (
  175. cookieByte []byte
  176. tokenByte []byte
  177. )
  178. if cookieByte, err = ioutil.ReadFile(d.c.Bugly.Cookie); err != nil {
  179. return
  180. }
  181. if tokenByte, err = ioutil.ReadFile(d.c.Bugly.Token); err != nil {
  182. return
  183. }
  184. cookie = string(cookieByte)
  185. token = string(tokenByte)
  186. return
  187. }
  188. // UpdateToken Update Token.
  189. func (d *Dao) UpdateToken() (err error) {
  190. return d.updateCookieAndToken()
  191. }
  192. func (d *Dao) updateCookieAndToken() (err error) {
  193. _, dir, _, _ := runtime.Caller(1)
  194. currentPath := strings.Replace(dir, "bugly.go", "", -1)
  195. cmd := exec.Command("python", "bugly.py")
  196. cmd.Dir = currentPath
  197. //err = cmd.Run()
  198. return
  199. }