statistics.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/ep/saga/conf"
  7. "go-common/app/admin/ep/saga/model"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/log"
  10. "github.com/xanzy/go-gitlab"
  11. )
  12. // QueryProject query commit info according to project id.
  13. func (s *Service) QueryProject(c context.Context, object string, req *model.ProjectDataReq) (resp *model.ProjectDataResp, err error) {
  14. var (
  15. data []*model.DataWithTime
  16. queryDes string
  17. total int
  18. )
  19. log.Info("QuerySingleProjectData Type: %d", req.QueryType)
  20. switch req.QueryType {
  21. case model.LastYearPerMonth:
  22. queryDes = model.LastYearPerMonthNote
  23. case model.LastMonthPerDay:
  24. queryDes = model.LastMonthPerDayNote
  25. case model.LastYearPerDay:
  26. queryDes = model.LastYearPerDayNote
  27. default:
  28. log.Warn("QueryProjectCommit Type is not in range")
  29. return
  30. }
  31. queryDes = req.ProjectName + " " + object + queryDes
  32. if data, total, err = s.QueryProjectByTime(req.ProjectID, object, req.QueryType); err != nil {
  33. return
  34. }
  35. resp = &model.ProjectDataResp{
  36. ProjectName: req.ProjectName,
  37. QueryDes: queryDes,
  38. Total: total,
  39. Data: data,
  40. }
  41. return
  42. }
  43. // QueryProjectByTime ...
  44. func (s *Service) QueryProjectByTime(projectID int, object string, queryType int) (resp []*model.DataWithTime, allNum int, err error) {
  45. var (
  46. layout = "2006-01-02"
  47. fmtLayout = `%d-%d-%d 00:00:00`
  48. //response *gitlab.Response
  49. since time.Time
  50. until time.Time
  51. count int
  52. totalItems int
  53. )
  54. year, month, _ := time.Now().Date()
  55. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  56. if queryType == model.LastYearPerMonth {
  57. count = model.MonthNumPerYear
  58. } else if queryType == model.LastMonthPerDay {
  59. _, _, count = thisMonth.AddDate(0, 0, -1).Date()
  60. } else if queryType == model.LastYearPerDay {
  61. count = model.DayNumPerYear
  62. }
  63. for i := 1; i <= count; i++ {
  64. if queryType == model.LastYearPerMonth {
  65. since = thisMonth.AddDate(0, -i, 0)
  66. until = thisMonth.AddDate(0, -i+1, 0)
  67. } else if queryType == model.LastMonthPerDay {
  68. since = thisMonth.AddDate(0, 0, -i)
  69. until = thisMonth.AddDate(0, 0, -i+1)
  70. } else if queryType == model.LastYearPerDay {
  71. since = thisMonth.AddDate(0, 0, -i)
  72. until = thisMonth.AddDate(0, 0, -i+1)
  73. }
  74. sinceStr := fmt.Sprintf(fmtLayout, since.Year(), since.Month(), since.Day())
  75. untilStr := fmt.Sprintf(fmtLayout, until.Year(), until.Month(), until.Day())
  76. if object == model.ObjectCommit {
  77. /*if _, response, err = s.gitlab.ListProjectCommit(projectID, 1, &since, &until); err != nil {
  78. return
  79. }*/
  80. if totalItems, err = s.dao.CountCommitByTime(projectID, sinceStr, untilStr); err != nil {
  81. return
  82. }
  83. } else if object == model.ObjectMR {
  84. /*if _, response, err = s.gitlab.ListProjectMergeRequests(projectID, &since, &until, -1); err != nil {
  85. return
  86. }*/
  87. if totalItems, err = s.dao.CountMRByTime(projectID, sinceStr, untilStr); err != nil {
  88. return
  89. }
  90. } else {
  91. log.Warn("QueryProjectByTime object(%s) is not support!", object)
  92. return
  93. }
  94. perData := &model.DataWithTime{
  95. //TotalItem: response.TotalItems,
  96. TotalItem: totalItems,
  97. StartTime: since.Format(layout),
  98. EndTime: until.Format(layout),
  99. }
  100. resp = append(resp, perData)
  101. //allNum = allNum + response.TotalItems
  102. allNum = allNum + totalItems
  103. }
  104. return
  105. }
  106. // QueryTeam ...
  107. func (s *Service) QueryTeam(c context.Context, object string, req *model.TeamDataRequest) (resp *model.TeamDataResp, err error) {
  108. var (
  109. projectInfo []*model.ProjectInfo
  110. reqProject = &model.ProjectInfoRequest{}
  111. dataMap = make(map[string]*model.TeamDataResp)
  112. data []*model.DataWithTime
  113. queryDes string
  114. total int
  115. key string
  116. keyNotExist bool
  117. )
  118. if len(req.Department) <= 0 && len(req.Business) <= 0 {
  119. log.Warn("query department and business are empty!")
  120. return
  121. }
  122. //log.Info("QueryTeamCommit Query Type: %d", req.QueryType)
  123. switch req.QueryType {
  124. case model.LastYearPerMonth:
  125. queryDes = model.LastYearPerMonthNote
  126. case model.LastMonthPerDay:
  127. queryDes = model.LastMonthPerDayNote
  128. case model.LastYearPerDay:
  129. queryDes = model.LastYearPerDayNote
  130. default:
  131. log.Warn("QueryTeamCommit Type is not in range")
  132. return
  133. }
  134. queryDes = req.Department + " " + req.Business + " " + object + queryDes
  135. //get value from mc
  136. key = "saga_admin_" + req.Department + "_" + req.Business + "_" + model.KeyTypeConst[req.QueryType]
  137. if err = s.dao.GetData(c, key, &dataMap); err != nil {
  138. if err == memcache.ErrNotFound {
  139. log.Warn("no such key (%s) in cache, err (%s)", key, err.Error())
  140. keyNotExist = true
  141. } else {
  142. return
  143. }
  144. }
  145. if _, ok := dataMap[object]; !ok {
  146. keyNotExist = true
  147. } else {
  148. resp = dataMap[object]
  149. return
  150. }
  151. log.Info("sync team %s start => type= %d, Department= %s, Business= %s", object, req.QueryType, req.Department, req.Business)
  152. reqProject.Department = req.Department
  153. reqProject.Business = req.Business
  154. if _, projectInfo, err = s.dao.QueryProjectInfo(false, reqProject); err != nil {
  155. return
  156. }
  157. if len(projectInfo) <= 0 {
  158. log.Warn("Found no project!")
  159. return
  160. }
  161. if data, total, err = s.QueryTeamByTime(object, req, req.QueryType, projectInfo); err != nil {
  162. return
  163. }
  164. resp = &model.TeamDataResp{
  165. Department: req.Department,
  166. Business: req.Business,
  167. QueryDes: queryDes,
  168. Total: total,
  169. Data: data,
  170. }
  171. //set value to mc
  172. if keyNotExist {
  173. dataMap[object] = resp
  174. if err = s.dao.SetData(c, key, dataMap); err != nil {
  175. return
  176. }
  177. }
  178. log.Info("sync team %s end", object)
  179. return
  180. }
  181. // QueryTeamByTime query commit info per month of last year and per day of last month according to team.
  182. func (s *Service) QueryTeamByTime(object string, req *model.TeamDataRequest, queryType int, projectInfo []*model.ProjectInfo) (resp []*model.DataWithTime, allNum int, err error) {
  183. var (
  184. layout = "2006-01-02"
  185. response *gitlab.Response
  186. since time.Time
  187. until time.Time
  188. count int
  189. num int
  190. )
  191. year, month, _ := time.Now().Date()
  192. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  193. if queryType == model.LastYearPerMonth {
  194. count = model.MonthNumPerYear
  195. } else if queryType == model.LastMonthPerDay {
  196. _, _, count = thisMonth.AddDate(0, 0, -1).Date()
  197. } else if queryType == model.LastYearPerDay {
  198. count = model.DayNumPerYear
  199. }
  200. for i := 1; i <= count; i++ {
  201. if queryType == model.LastYearPerMonth {
  202. since = thisMonth.AddDate(0, -i, 0)
  203. until = thisMonth.AddDate(0, -i+1, 0)
  204. } else if queryType == model.LastMonthPerDay {
  205. since = thisMonth.AddDate(0, 0, -i)
  206. until = thisMonth.AddDate(0, 0, -i+1)
  207. } else if queryType == model.LastYearPerDay {
  208. since = thisMonth.AddDate(0, 0, -i)
  209. until = thisMonth.AddDate(0, 0, -i+1)
  210. }
  211. num = 0
  212. for _, project := range projectInfo {
  213. if object == model.ObjectCommit {
  214. if _, response, err = s.gitlab.ListProjectCommit(project.ProjectID, 1, &since, &until); err != nil {
  215. return
  216. }
  217. } else if object == model.ObjectMR {
  218. if _, response, err = s.gitlab.ListProjectMergeRequests(project.ProjectID, &since, &until, -1); err != nil {
  219. return
  220. }
  221. } else {
  222. log.Warn("QueryTeamByTime object(%s) is not support!", object)
  223. return
  224. }
  225. num = num + response.TotalItems
  226. }
  227. perData := &model.DataWithTime{
  228. TotalItem: num,
  229. StartTime: since.Format(layout),
  230. EndTime: until.Format(layout),
  231. }
  232. resp = append(resp, perData)
  233. allNum = allNum + num
  234. }
  235. return
  236. }
  237. // SyncData ...
  238. func (s *Service) SyncData(c context.Context) (err error) {
  239. log.Info("sync all data info start!")
  240. for _, de := range conf.Conf.Property.DeInfo {
  241. for _, bu := range conf.Conf.Property.BuInfo {
  242. for k, keyType := range model.KeyTypeConst {
  243. key := "saga_admin_" + de.Value + "_" + bu.Value + "_" + keyType
  244. if err = s.dao.DeleteData(c, key); err != nil {
  245. return
  246. }
  247. req := &model.TeamDataRequest{
  248. TeamParam: model.TeamParam{
  249. Department: de.Value,
  250. Business: bu.Value,
  251. },
  252. QueryType: k,
  253. }
  254. if k == 3 {
  255. if _, err = s.QueryTeamPipeline(c, req); err != nil {
  256. return
  257. }
  258. continue
  259. }
  260. if _, err = s.QueryTeamCommit(c, req); err != nil {
  261. return
  262. }
  263. if _, err = s.QueryTeamMr(c, req); err != nil {
  264. return
  265. }
  266. }
  267. }
  268. }
  269. log.Info("sync all data info end!")
  270. return
  271. }