You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_data_analysis.go 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/services/mailer"
  13. "github.com/360EntSecGroup-Skylar/excelize/v2"
  14. )
  15. const (
  16. PAGE_SIZE = 2000
  17. )
  18. func getUserMetricsExcelHeader(ctx *context.Context) map[string]string {
  19. excelHeader := make([]string, 0)
  20. excelHeader = append(excelHeader, ctx.Tr("user.metrics.date"))
  21. excelHeader = append(excelHeader, ctx.Tr("user.metrics.newregistuser"))
  22. excelHeader = append(excelHeader, ctx.Tr("user.metrics.newregistandactiveuser"))
  23. excelHeader = append(excelHeader, ctx.Tr("user.metrics.hasactivateuser"))
  24. excelHeader = append(excelHeader, ctx.Tr("user.metrics.newregistnotactiveuser"))
  25. excelHeader = append(excelHeader, ctx.Tr("user.metrics.newuseractiveindex"))
  26. excelHeader = append(excelHeader, ctx.Tr("user.metrics.totalregistuser"))
  27. excelHeader = append(excelHeader, ctx.Tr("user.metrics.totalactiveduser"))
  28. excelHeader = append(excelHeader, ctx.Tr("user.metrics.totalhasactivityuser"))
  29. excelHeaderMap := make(map[string]string, 0)
  30. var i byte
  31. i = 0
  32. for _, value := range excelHeader {
  33. excelColumn := getColumn(i) + fmt.Sprint(1)
  34. excelHeaderMap[excelColumn] = value
  35. i++
  36. }
  37. return excelHeaderMap
  38. }
  39. func writeUserMetricsExcel(row int, xlsx *excelize.File, sheetName string, userMetrics *models.UserMetrics) {
  40. rows := fmt.Sprint(row)
  41. var tmp byte
  42. tmp = 0
  43. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.DisplayDate)
  44. tmp = tmp + 1
  45. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.ActivateRegistUser+userMetrics.NotActivateRegistUser)
  46. tmp = tmp + 1
  47. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.ActivateRegistUser)
  48. tmp = tmp + 1
  49. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.RegistActivityUser)
  50. tmp = tmp + 1
  51. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.NotActivateRegistUser)
  52. tmp = tmp + 1
  53. t := userMetrics.ActivateIndex * 100
  54. value := "-"
  55. if t < 100 && t > 0 {
  56. value = fmt.Sprintf("%.2f", t) + "%"
  57. } else if t >= 100 {
  58. value = "100%"
  59. }
  60. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, value)
  61. tmp = tmp + 1
  62. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.TotalUser)
  63. tmp = tmp + 1
  64. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.TotalActivateRegistUser)
  65. tmp = tmp + 1
  66. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userMetrics.TotalHasActivityUser)
  67. }
  68. func getExcelHeader(ctx *context.Context) map[string]string {
  69. excelHeader := make([]string, 0)
  70. excelHeader = append(excelHeader, ctx.Tr("user.static.id"))
  71. excelHeader = append(excelHeader, ctx.Tr("user.static.name"))
  72. excelHeader = append(excelHeader, ctx.Tr("user.static.UserIndex"))
  73. excelHeader = append(excelHeader, ctx.Tr("user.static.UserIndexPrimitive"))
  74. excelHeader = append(excelHeader, ctx.Tr("user.static.codemergecount"))
  75. excelHeader = append(excelHeader, ctx.Tr("user.static.commitcount"))
  76. excelHeader = append(excelHeader, ctx.Tr("user.static.issuecount"))
  77. excelHeader = append(excelHeader, ctx.Tr("user.static.commentcount"))
  78. excelHeader = append(excelHeader, ctx.Tr("user.static.focusrepocount"))
  79. excelHeader = append(excelHeader, ctx.Tr("user.static.starrepocount"))
  80. excelHeader = append(excelHeader, ctx.Tr("user.static.logincount"))
  81. excelHeader = append(excelHeader, ctx.Tr("user.static.watchedcount"))
  82. excelHeader = append(excelHeader, ctx.Tr("user.static.commitcodesize"))
  83. excelHeader = append(excelHeader, ctx.Tr("user.static.solveissuecount"))
  84. excelHeader = append(excelHeader, ctx.Tr("user.static.encyclopediascount"))
  85. excelHeader = append(excelHeader, ctx.Tr("user.static.createrepocount"))
  86. excelHeader = append(excelHeader, ctx.Tr("user.static.openiindex"))
  87. excelHeader = append(excelHeader, ctx.Tr("user.static.CloudBrainTaskNum"))
  88. excelHeader = append(excelHeader, ctx.Tr("user.static.CloudBrainRunTime"))
  89. excelHeader = append(excelHeader, ctx.Tr("user.static.CommitDatasetNum"))
  90. excelHeader = append(excelHeader, ctx.Tr("user.static.CommitModelCount"))
  91. excelHeader = append(excelHeader, ctx.Tr("user.static.FocusOtherUser"))
  92. excelHeader = append(excelHeader, ctx.Tr("user.static.CollectDataset"))
  93. excelHeader = append(excelHeader, ctx.Tr("user.static.CollectedDataset"))
  94. excelHeader = append(excelHeader, ctx.Tr("user.static.RecommendDataset"))
  95. excelHeader = append(excelHeader, ctx.Tr("user.static.CollectImage"))
  96. excelHeader = append(excelHeader, ctx.Tr("user.static.CollectedImage"))
  97. excelHeader = append(excelHeader, ctx.Tr("user.static.RecommendImage"))
  98. excelHeader = append(excelHeader, ctx.Tr("user.static.registdate"))
  99. excelHeader = append(excelHeader, ctx.Tr("user.static.countdate"))
  100. excelHeaderMap := make(map[string]string, 0)
  101. var i byte
  102. i = 0
  103. for _, value := range excelHeader {
  104. excelColumn := getColumn(i) + fmt.Sprint(1)
  105. log.Info("excelColumn=" + excelColumn)
  106. excelHeaderMap[excelColumn] = value
  107. i++
  108. }
  109. return excelHeaderMap
  110. }
  111. func writeExcel(row int, xlsx *excelize.File, sheetName string, userRecord *models.UserBusinessAnalysisAll) {
  112. rows := fmt.Sprint(row)
  113. var tmp byte
  114. tmp = 0
  115. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.ID)
  116. tmp = tmp + 1
  117. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.Name)
  118. tmp = tmp + 1
  119. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, fmt.Sprintf("%.2f", userRecord.UserIndex))
  120. tmp = tmp + 1
  121. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, fmt.Sprintf("%.2f", userRecord.UserIndexPrimitive))
  122. tmp = tmp + 1
  123. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CodeMergeCount)
  124. tmp = tmp + 1
  125. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CommitCount)
  126. tmp = tmp + 1
  127. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.IssueCount)
  128. tmp = tmp + 1
  129. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CommentCount)
  130. tmp = tmp + 1
  131. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.FocusRepoCount)
  132. tmp = tmp + 1
  133. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.StarRepoCount)
  134. tmp = tmp + 1
  135. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.LoginCount)
  136. tmp = tmp + 1
  137. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.WatchedCount)
  138. tmp = tmp + 1
  139. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CommitCodeSize)
  140. tmp = tmp + 1
  141. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.SolveIssueCount)
  142. tmp = tmp + 1
  143. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.EncyclopediasCount)
  144. tmp = tmp + 1
  145. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CreateRepoCount)
  146. tmp = tmp + 1
  147. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, fmt.Sprintf("%.2f", userRecord.OpenIIndex))
  148. tmp = tmp + 1
  149. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CloudBrainTaskNum)
  150. tmp = tmp + 1
  151. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, fmt.Sprintf("%.2f", float64(userRecord.CloudBrainRunTime)/3600))
  152. tmp = tmp + 1
  153. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CommitDatasetNum)
  154. tmp = tmp + 1
  155. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CommitModelCount)
  156. tmp = tmp + 1
  157. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.FocusOtherUser)
  158. tmp = tmp + 1
  159. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CollectDataset)
  160. tmp = tmp + 1
  161. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CollectedDataset)
  162. tmp = tmp + 1
  163. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.RecommendDataset)
  164. tmp = tmp + 1
  165. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CollectImage)
  166. tmp = tmp + 1
  167. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.CollectedImage)
  168. tmp = tmp + 1
  169. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.RecommendImage)
  170. tmp = tmp + 1
  171. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  172. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3])
  173. tmp = tmp + 1
  174. formatTime = userRecord.DataDate
  175. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime)
  176. }
  177. func getColumn(tmp byte) string {
  178. var tmpA byte
  179. tmpA = 'A'
  180. if tmp < 26 {
  181. return string(tmpA + tmp)
  182. } else {
  183. return "A" + string(tmpA+(tmp-26))
  184. }
  185. }
  186. func queryUserDataPage(ctx *context.Context, tableName string, queryObj interface{}) {
  187. page := ctx.QueryInt("page")
  188. if page <= 0 {
  189. page = 1
  190. }
  191. pageSize := ctx.QueryInt("pageSize")
  192. if pageSize <= 0 {
  193. pageSize = setting.UI.IssuePagingNum
  194. }
  195. userName := ctx.Query("userName")
  196. IsReturnFile := ctx.QueryBool("IsReturnFile")
  197. if IsReturnFile {
  198. //writer exec file.
  199. xlsx := excelize.NewFile()
  200. sheetName := ctx.Tr("user.static.sheetname")
  201. index := xlsx.NewSheet(sheetName)
  202. xlsx.DeleteSheet("Sheet1")
  203. dataHeader := getExcelHeader(ctx)
  204. for k, v := range dataHeader {
  205. //设置单元格的值
  206. xlsx.SetCellValue(sheetName, k, v)
  207. }
  208. _, count := models.QueryUserStaticDataByTableName(1, 1, tableName, queryObj, userName)
  209. var indexTotal int64
  210. indexTotal = 0
  211. row := 1
  212. for {
  213. re, _ := models.QueryUserStaticDataByTableName(int(indexTotal), PAGE_SIZE, tableName, queryObj, "")
  214. log.Info("return count=" + fmt.Sprint(count))
  215. for _, userRecord := range re {
  216. row++
  217. writeExcel(row, xlsx, sheetName, userRecord)
  218. }
  219. indexTotal += PAGE_SIZE
  220. if indexTotal >= count {
  221. break
  222. }
  223. }
  224. //设置默认打开的表单
  225. xlsx.SetActiveSheet(index)
  226. filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx"
  227. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  228. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  229. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  230. log.Info("writer exel error." + err.Error())
  231. }
  232. } else {
  233. re, count := models.QueryUserStaticDataByTableName((page-1)*pageSize, pageSize, tableName, queryObj, userName)
  234. mapInterface := make(map[string]interface{})
  235. mapInterface["data"] = re
  236. mapInterface["count"] = count
  237. ctx.JSON(http.StatusOK, mapInterface)
  238. }
  239. }
  240. func queryMetrics(ctx *context.Context, tableName string, startTime time.Time, endTime time.Time) {
  241. page := ctx.QueryInt("page")
  242. if page <= 0 {
  243. page = 1
  244. }
  245. pageSize := ctx.QueryInt("pageSize")
  246. if pageSize <= 0 {
  247. pageSize = setting.UI.IssuePagingNum
  248. }
  249. IsReturnFile := ctx.QueryBool("IsReturnFile")
  250. var count int64
  251. result := make([]*models.UserMetrics, 0)
  252. if tableName == "public.user_business_analysis_current_year" {
  253. result = models.QueryMetricsForYear(startTime.Unix(), endTime.Unix())
  254. count = int64(len(result))
  255. } else if tableName == "public.user_business_analysis_all" {
  256. result = models.QueryMetricsForAll(startTime.Unix(), endTime.Unix())
  257. count = int64(len(result))
  258. } else {
  259. result, count = models.QueryMetricsPage(startTime.Unix(), endTime.Unix(), page, pageSize)
  260. }
  261. if IsReturnFile {
  262. //writer exec file.
  263. xlsx := excelize.NewFile()
  264. sheetName := ctx.Tr("user.metrics.sheetname")
  265. index := xlsx.NewSheet(sheetName)
  266. xlsx.DeleteSheet("Sheet1")
  267. dataHeader := getUserMetricsExcelHeader(ctx)
  268. for k, v := range dataHeader {
  269. //设置单元格的值
  270. xlsx.SetCellValue(sheetName, k, v)
  271. }
  272. row := 1
  273. log.Info("return count=" + fmt.Sprint(count))
  274. for _, userRecord := range result {
  275. row++
  276. writeUserMetricsExcel(row, xlsx, sheetName, userRecord)
  277. }
  278. //设置默认打开的表单
  279. xlsx.SetActiveSheet(index)
  280. filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx"
  281. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  282. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  283. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  284. log.Info("writer exel error." + err.Error())
  285. }
  286. } else {
  287. mapInterface := make(map[string]interface{})
  288. mapInterface["data"] = result
  289. mapInterface["count"] = count
  290. if tableName == "public.user_business_analysis_yesterday" {
  291. mapInterface["datarecordbegintime"] = setting.RadarMap.GrowthBeginTime
  292. if len(result) > 0 {
  293. dateTime := time.Unix(result[0].CountDate, 0)
  294. mapInterface["lastUpdatedTime"] = dateTime.Format("2006-01-02 15:04:05")
  295. } else {
  296. mapInterface["lastUpdatedTime"] = ""
  297. }
  298. }
  299. ctx.JSON(http.StatusOK, mapInterface)
  300. }
  301. }
  302. func QueryRankingList(ctx *context.Context) {
  303. key := ctx.Query("key")
  304. tableName := ctx.Query("tableName")
  305. limit := ctx.QueryInt("limit")
  306. result, count := models.QueryRankList(key, tableName, limit)
  307. mapInterface := make(map[string]interface{})
  308. mapInterface["data"] = result
  309. mapInterface["count"] = count
  310. ctx.JSON(http.StatusOK, mapInterface)
  311. }
  312. func QueryUserMetricsCurrentMonth(ctx *context.Context) {
  313. currentTimeNow := time.Now()
  314. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  315. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  316. pageStartTime = getStartTime(pageStartTime)
  317. queryMetrics(ctx, "public.user_business_analysis_current_month", pageStartTime, pageEndTime)
  318. }
  319. func QueryUserStaticCurrentMonth(ctx *context.Context) {
  320. queryUserDataPage(ctx, "public.user_business_analysis_current_month", new(models.UserBusinessAnalysisCurrentMonth))
  321. }
  322. func getStartTime(pageStartTime time.Time) time.Time {
  323. t, _ := time.ParseInLocation("2006-01-02", setting.RadarMap.GrowthBeginTime, time.Local)
  324. t = t.UTC()
  325. if pageStartTime.Before(t) {
  326. pageStartTime = t
  327. }
  328. return pageStartTime
  329. }
  330. func QueryUserMetricsCurrentWeek(ctx *context.Context) {
  331. currentTimeNow := time.Now()
  332. offset := int(time.Monday - currentTimeNow.Weekday())
  333. if offset > 0 {
  334. offset = -6
  335. }
  336. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  337. pageStartTime = getStartTime(pageStartTime)
  338. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  339. queryMetrics(ctx, "public.user_business_analysis_current_week", pageStartTime, pageEndTime)
  340. }
  341. func QueryUserStaticCurrentWeek(ctx *context.Context) {
  342. queryUserDataPage(ctx, "public.user_business_analysis_current_week", new(models.UserBusinessAnalysisCurrentWeek))
  343. }
  344. func QueryUserMetricsCurrentYear(ctx *context.Context) {
  345. currentTimeNow := time.Now()
  346. pageStartTime := time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location())
  347. pageStartTime = getStartTime(pageStartTime)
  348. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  349. queryMetrics(ctx, "public.user_business_analysis_current_year", pageStartTime, pageEndTime)
  350. }
  351. func QueryUserStaticCurrentYear(ctx *context.Context) {
  352. queryUserDataPage(ctx, "public.user_business_analysis_current_year", new(models.UserBusinessAnalysisCurrentYear))
  353. }
  354. func QueryUserMetricsLast30Day(ctx *context.Context) {
  355. currentTimeNow := time.Now()
  356. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30)
  357. pageStartTime = getStartTime(pageStartTime)
  358. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  359. queryMetrics(ctx, "public.user_business_analysis_last30_day", pageStartTime, pageEndTime)
  360. }
  361. func QueryUserStaticLast30Day(ctx *context.Context) {
  362. queryUserDataPage(ctx, "public.user_business_analysis_last30_day", new(models.UserBusinessAnalysisLast30Day))
  363. }
  364. func QueryUserMetricsLastMonth(ctx *context.Context) {
  365. currentTimeNow := time.Now()
  366. thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  367. pageStartTime := thisMonth.AddDate(0, -1, 0)
  368. pageStartTime = getStartTime(pageStartTime)
  369. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  370. queryMetrics(ctx, "public.user_business_analysis_last_month", pageStartTime, pageEndTime)
  371. }
  372. func QueryUserStaticLastMonth(ctx *context.Context) {
  373. queryUserDataPage(ctx, "public.user_business_analysis_last_month", new(models.UserBusinessAnalysisLastMonth))
  374. }
  375. func QueryUserMetricsYesterday(ctx *context.Context) {
  376. currentTimeNow := time.Now()
  377. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local)
  378. pageStartTime = getStartTime(pageStartTime)
  379. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  380. queryMetrics(ctx, "public.user_business_analysis_yesterday", pageStartTime, pageEndTime)
  381. }
  382. func QueryUserStaticYesterday(ctx *context.Context) {
  383. queryUserDataPage(ctx, "public.user_business_analysis_yesterday", new(models.UserBusinessAnalysisYesterday))
  384. }
  385. func QueryUserMetricsAll(ctx *context.Context) {
  386. currentTimeNow := time.Now()
  387. pageStartTime := time.Date(2022, 4, 5, 0, 0, 0, 0, currentTimeNow.Location())
  388. pageStartTime = getStartTime(pageStartTime)
  389. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  390. queryMetrics(ctx, "public.user_business_analysis_all", pageStartTime, pageEndTime)
  391. }
  392. func QueryUserStaticAll(ctx *context.Context) {
  393. queryUserDataPage(ctx, "public.user_business_analysis_all", new(models.UserBusinessAnalysisAll))
  394. }
  395. func QueryUserMetricDataPage(ctx *context.Context) {
  396. startDate := ctx.Query("startDate")
  397. endDate := ctx.Query("endDate")
  398. startTime, _ := time.ParseInLocation("2006-01-02", startDate, time.Local)
  399. endTime, _ := time.ParseInLocation("2006-01-02", endDate, time.Local)
  400. page := ctx.QueryInt("page")
  401. if page <= 0 {
  402. page = 1
  403. }
  404. pageSize := ctx.QueryInt("pageSize")
  405. if pageSize <= 0 {
  406. pageSize = setting.UI.IssuePagingNum
  407. }
  408. result, count := models.QueryMetricsPage(startTime.Unix(), endTime.Unix(), page, pageSize)
  409. mapInterface := make(map[string]interface{})
  410. mapInterface["data"] = result
  411. mapInterface["count"] = count
  412. ctx.JSON(http.StatusOK, mapInterface)
  413. }
  414. func QueryUserStaticDataPage(ctx *context.Context) {
  415. startDate := ctx.Query("startDate")
  416. endDate := ctx.Query("endDate")
  417. page := ctx.QueryInt("page")
  418. if page <= 0 {
  419. page = 1
  420. }
  421. pageSize := ctx.QueryInt("pageSize")
  422. if pageSize <= 0 {
  423. pageSize = setting.UI.IssuePagingNum
  424. }
  425. userName := ctx.Query("userName")
  426. IsReturnFile := ctx.QueryBool("IsReturnFile")
  427. log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page))
  428. var startTime time.Time
  429. var endTime time.Time
  430. var isAll bool
  431. if startDate == "all" {
  432. isAll = true
  433. startTime = time.Now()
  434. endTime = time.Now()
  435. } else {
  436. startTime, _ = time.ParseInLocation("2006-01-02", startDate, time.Local)
  437. startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 12, 0, 0, 0, startTime.Location())
  438. settingStartTime, _ := time.Parse("2006-01-02", setting.RadarMap.RecordBeginTime)
  439. if startTime.Unix() < settingStartTime.Unix() {
  440. startTime = settingStartTime
  441. startDate = settingStartTime.Format("2006-01-02")
  442. }
  443. endTime, _ = time.ParseInLocation("2006-01-02", endDate, time.Local)
  444. endTime = endTime.AddDate(0, 0, 1)
  445. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 0, startTime.Location())
  446. isAll = false
  447. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  448. }
  449. if IsReturnFile {
  450. page = -1
  451. pageSize = -1
  452. }
  453. pageOpts := &models.UserBusinessAnalysisQueryOptions{
  454. ListOptions: models.ListOptions{
  455. Page: page,
  456. PageSize: pageSize,
  457. },
  458. UserName: userName,
  459. StartTime: startTime.Unix(),
  460. EndTime: endTime.Unix(),
  461. IsAll: isAll,
  462. }
  463. if IsReturnFile {
  464. re, count := models.QueryUserStaticDataAll(pageOpts)
  465. log.Info("return count=" + fmt.Sprint(count))
  466. //writer exec file.
  467. xlsx := excelize.NewFile()
  468. sheetName := ctx.Tr("user.static.sheetname")
  469. index := xlsx.NewSheet(sheetName)
  470. xlsx.DeleteSheet("Sheet1")
  471. dataHeader := getExcelHeader(ctx)
  472. for k, v := range dataHeader {
  473. //设置单元格的值
  474. xlsx.SetCellValue(sheetName, k, v)
  475. }
  476. for i, userRecord := range re {
  477. row := i + 2
  478. writeExcel(row, xlsx, sheetName, userRecord)
  479. }
  480. //设置默认打开的表单
  481. xlsx.SetActiveSheet(index)
  482. filename := sheetName + "_" + ctx.Tr("user.static.all") + ".xlsx"
  483. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  484. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  485. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  486. log.Info("writer exel error." + err.Error())
  487. }
  488. } else {
  489. mapInterface := make(map[string]interface{})
  490. re, count := models.QueryUserStaticDataPage(pageOpts)
  491. mapInterface["data"] = re
  492. mapInterface["count"] = count
  493. ctx.JSON(http.StatusOK, mapInterface)
  494. }
  495. }
  496. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  497. t, _ := time.Parse("2006-01-02", date)
  498. startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  499. endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
  500. //query wiki data
  501. log.Info("start to time count data")
  502. wikiMap := make(map[string]int)
  503. warnEmailMessage := "用户统计信息入库失败,请尽快定位。"
  504. repoList, err := models.GetAllRepositories()
  505. if err != nil {
  506. log.Error("query repo error." + err.Error())
  507. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  508. return
  509. }
  510. log.Info("start to query wiki data")
  511. for _, repoRecord := range repoList {
  512. wikiPath := models.WikiPath(repoRecord.OwnerName, repoRecord.Name)
  513. time, err := git.GetLatestCommitTime(wikiPath)
  514. if err == nil {
  515. log.Info("last commit time:" + time.Format("2006-01-02 15:04:05") + " wikiPath=" + wikiPath)
  516. if time.After(startTime) {
  517. wikiRepo, _, err := FindWikiRepoCommitByWikiPath(wikiPath)
  518. if err != nil {
  519. log.Error("wiki not exist. wikiPath=" + wikiPath)
  520. } else {
  521. log.Info("wiki exist, wikiPath=" + wikiPath)
  522. list, err := wikiRepo.GetCommitByPathAndDays(wikiPath, 1)
  523. if err != nil {
  524. log.Info("err,err=v%", err)
  525. } else {
  526. for logEntry := list.Front(); logEntry != nil; logEntry = logEntry.Next() {
  527. commit := logEntry.Value.(*git.Commit)
  528. log.Info("commit msg=" + commit.CommitMessage + " time=" + commit.Committer.When.Format("2006-01-02 15:04:05") + " user=" + commit.Committer.Name)
  529. if _, ok := wikiMap[commit.Committer.Name]; !ok {
  530. wikiMap[commit.Committer.Name] = 1
  531. } else {
  532. wikiMap[commit.Committer.Name] += 1
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. }
  540. //other user info data
  541. err = models.CounDataByDateAndReCount(wikiMap, startTime, endTime, isReCount)
  542. if err != nil {
  543. log.Error("count user info error." + err.Error())
  544. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  545. }
  546. log.Info("start to count all user info data")
  547. //models.RefreshUserStaticAllTabel(wikiMap)
  548. log.Info("end to count all user info data")
  549. }
  550. func TimingCountDataByDate(date string) {
  551. TimingCountDataByDateAndReCount(date, true)
  552. }
  553. func TimingCountData() {
  554. log.Info("start to time count data")
  555. currentTimeNow := time.Now()
  556. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  557. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  558. TimingCountDataByDateAndReCount(startTime, false)
  559. }