bugly.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/marthe/model"
  5. "go-common/library/ecode"
  6. )
  7. // UpdateBuglyVersion update Bugly Version.
  8. func (s *Service) UpdateBuglyVersion(c context.Context, username string, req *model.AddVersionRequest) (rep map[string]interface{}, err error) {
  9. var (
  10. bugVersionID int64
  11. buglyProject *model.BuglyProject
  12. )
  13. // check project
  14. if buglyProject, err = s.dao.QueryBuglyProject(req.BuglyProjectID); err != nil {
  15. return
  16. }
  17. if buglyProject.ID == 0 {
  18. err = ecode.NothingFound
  19. return
  20. }
  21. if req.ID > 0 {
  22. // update
  23. var tmpVersion *model.BuglyVersion
  24. if tmpVersion, err = s.dao.QueryBuglyVersion(req.ID); err != nil {
  25. return
  26. }
  27. if tmpVersion.ID == 0 {
  28. err = ecode.NothingFound
  29. return
  30. }
  31. tmpVersion.BuglyProjectID = req.BuglyProjectID
  32. tmpVersion.Version = req.Version
  33. tmpVersion.Action = req.Action
  34. if err = s.dao.UpdateBuglyVersion(tmpVersion); err != nil {
  35. return
  36. }
  37. bugVersionID = tmpVersion.ID
  38. } else {
  39. // add
  40. var tmpVersion *model.BuglyVersion
  41. //check name
  42. if tmpVersion, err = s.dao.QueryBuglyVersionByVersion(req.Version); err != nil {
  43. return
  44. }
  45. if tmpVersion.ID > 0 {
  46. err = ecode.MartheDuplicateErr
  47. return
  48. }
  49. buglyVersion := &model.BuglyVersion{
  50. ID: req.ID,
  51. Version: req.Version,
  52. BuglyProjectID: req.BuglyProjectID,
  53. Action: req.Action,
  54. TaskStatus: model.BuglyVersionTaskStatusReady,
  55. UpdateBy: username,
  56. }
  57. if err = s.dao.InsertBuglyVersion(buglyVersion); err != nil {
  58. return
  59. }
  60. bugVersionID = buglyVersion.ID
  61. }
  62. rep = make(map[string]interface{})
  63. rep["bug_version_id"] = bugVersionID
  64. return
  65. }
  66. // QueryBuglyVersions Query Bugly Versions.
  67. func (s *Service) QueryBuglyVersions(c context.Context, req *model.QueryBuglyVersionRequest) (rep *model.PaginateBuglyProjectVersions, err error) {
  68. var (
  69. total int64
  70. buglyProjectVersions []*model.BuglyProjectVersion
  71. )
  72. if total, buglyProjectVersions, err = s.dao.FindBuglyProjectVersions(req); err != nil {
  73. return
  74. }
  75. rep = &model.PaginateBuglyProjectVersions{
  76. PaginationRep: model.PaginationRep{
  77. Total: total,
  78. PageSize: req.PageSize,
  79. PageNum: req.PageNum,
  80. },
  81. BuglyProjectVersions: buglyProjectVersions,
  82. }
  83. return
  84. }
  85. // BuglyVersionAndProjectList Bugly Version and project List.
  86. func (s *Service) BuglyVersionAndProjectList(c context.Context) (rep map[string]interface{}, err error) {
  87. var (
  88. versionList []string
  89. projectList []string
  90. )
  91. rep = make(map[string]interface{})
  92. if versionList, err = s.dao.QueryBuglyVersionList(); err != nil {
  93. return
  94. }
  95. if projectList, err = s.dao.QueryBuglyProjectList(); err != nil {
  96. return
  97. }
  98. rep["versions"] = versionList
  99. rep["projects"] = projectList
  100. return
  101. }
  102. // UpdateCookie update Cookie.
  103. func (s *Service) UpdateCookie(c context.Context, username string, req *model.AddCookieRequest) (rep map[string]interface{}, err error) {
  104. var cookieID int64
  105. if req.ID > 0 {
  106. buglyCookie := &model.BuglyCookie{
  107. ID: req.ID,
  108. QQAccount: req.QQAccount,
  109. Cookie: req.Cookie,
  110. Token: req.Token,
  111. Status: req.Status,
  112. UpdateBy: username,
  113. }
  114. if err = s.dao.UpdateCookie(buglyCookie); err != nil {
  115. return
  116. }
  117. cookieID = req.ID
  118. } else {
  119. buglyCookie := &model.BuglyCookie{
  120. QQAccount: req.QQAccount,
  121. Cookie: req.Cookie,
  122. Token: req.Token,
  123. Status: req.Status,
  124. UpdateBy: username,
  125. }
  126. if err = s.dao.InsertCookie(buglyCookie); err != nil {
  127. return
  128. }
  129. cookieID = buglyCookie.ID
  130. }
  131. rep = make(map[string]interface{})
  132. rep["cookie_id"] = cookieID
  133. return
  134. }
  135. // QueryCookies Add Cookie.
  136. func (s *Service) QueryCookies(c context.Context, req *model.QueryBuglyCookiesRequest) (rep *model.PaginateBuglyCookies, err error) {
  137. var (
  138. total int64
  139. buglyCookies []*model.BuglyCookie
  140. )
  141. if total, buglyCookies, err = s.dao.FindCookies(req); err != nil {
  142. return
  143. }
  144. rep = &model.PaginateBuglyCookies{
  145. PaginationRep: model.PaginationRep{
  146. Total: total,
  147. PageSize: req.PageSize,
  148. PageNum: req.PageNum,
  149. },
  150. BuglyCookies: buglyCookies,
  151. }
  152. return
  153. }
  154. // UpdateCookieStatus Update Cookie Status.
  155. func (s *Service) UpdateCookieStatus(c context.Context, cookieID int64, status int) (err error) {
  156. err = s.dao.UpdateCookieStatus(cookieID, status)
  157. return
  158. }
  159. // GetEnableCookie Get Enable Cookie.
  160. func (s *Service) GetEnableCookie() (buglyCookie *model.BuglyCookie, err error) {
  161. var buglyCookies []*model.BuglyCookie
  162. if buglyCookies, err = s.dao.QueryCookieByStatus(model.BuglyCookieStatusEnable); err != nil {
  163. return
  164. }
  165. /*for _, ele := range buglyCookies {
  166. if ele.UsageCount < s.c.Bugly.CookieUsageUpper {
  167. buglyCookie = ele
  168. err = s.dao.UpdateCookieUsageCount(buglyCookie.ID, buglyCookie.UsageCount+1)
  169. return
  170. }
  171. }*/
  172. if len(buglyCookies) > 0 {
  173. buglyCookie = buglyCookies[0]
  174. err = s.dao.UpdateCookieUsageCount(buglyCookie.ID, buglyCookie.UsageCount+1)
  175. return
  176. }
  177. //not found enable cookie
  178. s.DoWhenNoEnableCookie()
  179. err = ecode.MartheNoCookie
  180. return
  181. }
  182. // DisableCookie Disable Cookie.
  183. func (s *Service) DisableCookie(c context.Context, cookieID int64) (err error) {
  184. err = s.dao.UpdateCookieStatus(cookieID, model.BuglyCookieStatusDisable)
  185. return
  186. }
  187. // DoWhenNoEnableCookie Do When No Enable Cookie.
  188. func (s *Service) DoWhenNoEnableCookie() {
  189. // todo notice
  190. s.SendMail(s.c.Mail.NoticeOwner, "marthe has no enable cookie", "")
  191. }
  192. // QueryBuglyIssueRecords Query Bugly Issue Records
  193. func (s *Service) QueryBuglyIssueRecords(c context.Context, req *model.QueryBuglyIssueRequest) (rep *model.PaginateBuglyIssues, err error) {
  194. var (
  195. total int64
  196. buglyIssues []*model.BuglyIssue
  197. )
  198. if total, buglyIssues, err = s.dao.FindBuglyIssues(req); err != nil {
  199. return
  200. }
  201. rep = &model.PaginateBuglyIssues{
  202. PaginationRep: model.PaginationRep{
  203. Total: total,
  204. PageSize: req.PageSize,
  205. PageNum: req.PageNum,
  206. },
  207. BuglyIssues: buglyIssues,
  208. }
  209. return
  210. }
  211. // QueryBatchRuns Query Batch Runs
  212. func (s *Service) QueryBatchRuns(c context.Context, req *model.QueryBuglyBatchRunsRequest) (rep *model.PaginateBuglyBatchRuns, err error) {
  213. var (
  214. total int64
  215. buglyBatchRuns []*model.BuglyBatchRun
  216. )
  217. if total, buglyBatchRuns, err = s.dao.FindBuglyBatchRuns(req); err != nil {
  218. return
  219. }
  220. rep = &model.PaginateBuglyBatchRuns{
  221. PaginationRep: model.PaginationRep{
  222. Total: total,
  223. PageSize: req.PageSize,
  224. PageNum: req.PageNum,
  225. },
  226. BuglyBatchRuns: buglyBatchRuns,
  227. }
  228. return
  229. }
  230. // UpdateBuglyProject update bugly project.
  231. func (s *Service) UpdateBuglyProject(c context.Context, username string, req *model.AddProjectRequest) (rep map[string]interface{}, err error) {
  232. var buglyProjectID int64
  233. if req.ID > 0 {
  234. buglyProject := &model.BuglyProject{
  235. ID: req.ID,
  236. ProjectID: req.ProjectID,
  237. //ProjectName: req.ProjectName,
  238. PlatformID: req.PlatformID,
  239. ExceptionType: req.ExceptionType,
  240. UpdateBy: username,
  241. }
  242. if err = s.dao.UpdateBuglyProject(buglyProject); err != nil {
  243. return
  244. }
  245. buglyProjectID = req.ID
  246. } else {
  247. var buglyProjectInDB *model.BuglyProject
  248. if buglyProjectInDB, err = s.dao.QueryBuglyProjectByName(req.ProjectName); err != nil {
  249. return
  250. }
  251. if buglyProjectInDB.ID > 0 {
  252. err = ecode.MartheDuplicateErr
  253. return
  254. }
  255. buglyProject := &model.BuglyProject{
  256. ProjectID: req.ProjectID,
  257. ProjectName: req.ProjectName,
  258. PlatformID: req.PlatformID,
  259. ExceptionType: req.ExceptionType,
  260. UpdateBy: username,
  261. }
  262. if err = s.dao.InsertBuglyProject(buglyProject); err != nil {
  263. return
  264. }
  265. buglyProjectID = buglyProject.ID
  266. }
  267. rep = make(map[string]interface{})
  268. rep["bugly_project_id"] = buglyProjectID
  269. return
  270. }
  271. // QueryBuglyProjects Query Bugly Project.
  272. func (s *Service) QueryBuglyProjects(c context.Context, req *model.QueryBuglyProjectRequest) (rep *model.PaginateBuglyProjects, err error) {
  273. var (
  274. total int64
  275. buglyProjects []*model.BuglyProject
  276. )
  277. if total, buglyProjects, err = s.dao.FindBuglyProjects(req); err != nil {
  278. return
  279. }
  280. rep = &model.PaginateBuglyProjects{
  281. PaginationRep: model.PaginationRep{
  282. Total: total,
  283. PageSize: req.PageSize,
  284. PageNum: req.PageNum,
  285. },
  286. BuglyProjects: buglyProjects,
  287. }
  288. return
  289. }
  290. // QueryBuglyProject Query Bugly Project.
  291. func (s *Service) QueryBuglyProject(c context.Context, id int64) (buglyProject *model.BuglyProject, err error) {
  292. return s.dao.QueryBuglyProject(id)
  293. }
  294. // QueryAllBuglyProjects Query All Bugly Projects.
  295. func (s *Service) QueryAllBuglyProjects(c context.Context) (buglyProjects []*model.BuglyProject, err error) {
  296. return s.dao.QueryAllBuglyProjects()
  297. }
  298. // QueryBuglyProjectVersions Query Bugly Project Versions.
  299. func (s *Service) QueryBuglyProjectVersions(c context.Context, buglyProjectID int64) (rep map[string]interface{}, err error) {
  300. var (
  301. buglyProject *model.BuglyProject
  302. buglyCookie *model.BuglyCookie
  303. buglyVersions []*model.BugVersion
  304. versions []string
  305. )
  306. defer func() {
  307. if err != nil && err == ecode.MartheCookieExpired {
  308. s.DisableCookie(c, buglyCookie.ID)
  309. }
  310. }()
  311. if buglyProject, err = s.dao.QueryBuglyProject(buglyProjectID); err != nil {
  312. return
  313. }
  314. if buglyProject.ID == 0 {
  315. err = ecode.NothingFound
  316. return
  317. }
  318. //get enable cookie
  319. if buglyCookie, err = s.GetEnableCookie(); err != nil {
  320. return
  321. }
  322. if buglyVersions, err = s.dao.BuglyVersion(c, buglyCookie, buglyProject.ProjectID, buglyProject.PlatformID); err != nil || buglyVersions == nil {
  323. return
  324. }
  325. for _, buglyVersion := range buglyVersions {
  326. versions = append(versions, buglyVersion.Name)
  327. }
  328. rep = make(map[string]interface{})
  329. rep["versions"] = versions
  330. return
  331. }