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.

repo_dashbord.go 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package repo
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "path"
  9. "strconv"
  10. "time"
  11. "github.com/360EntSecGroup-Skylar/excelize/v2"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. const DEFAULT_PAGE_SIZE = 10
  18. const DATE_FORMAT = "2006-01-02"
  19. const EXCEL_DATE_FORMAT = "20060102"
  20. type ProjectsPeriodData struct {
  21. RecordBeginTime string `json:"recordBeginTime"`
  22. LastUpdatedTime string `json:"lastUpdatedTime"`
  23. PageSize int `json:"pageSize"`
  24. TotalPage int `json:"totalPage"`
  25. TotalCount int64 `json:"totalCount"`
  26. PageRecords []*models.RepoStatistic `json:"pageRecords"`
  27. }
  28. type UserInfo struct {
  29. User string `json:"user"`
  30. Mode int `json:"mode"`
  31. PR int64 `json:"pr"`
  32. Commit int `json:"commit"`
  33. RelAvatarLink string `json:"relAvatarLink"`
  34. Email string `json:"email"`
  35. }
  36. type ProjectLatestData struct {
  37. RecordBeginTime string `json:"recordBeginTime"`
  38. LastUpdatedTime string `json:"lastUpdatedTime"`
  39. CreatTime string `json:"creatTime"`
  40. OpenI float64 `json:"openi"`
  41. Comment int64 `json:"comment"`
  42. View int64 `json:"view"`
  43. Download int64 `json:"download"`
  44. IssueClosedRatio float32 `json:"issueClosedRatio"`
  45. Impact float64 `json:"impact"`
  46. Completeness float64 `json:"completeness"`
  47. Liveness float64 `json:"liveness"`
  48. ProjectHealth float64 `json:"projectHealth"`
  49. TeamHealth float64 `json:"teamHealth"`
  50. Growth float64 `json:"growth"`
  51. Description string `json:"description"`
  52. Top10 []UserInfo `json:"top10"`
  53. }
  54. func RestoreForkNumber(ctx *context.Context) {
  55. repos, err := models.GetAllRepositories()
  56. if err != nil {
  57. log.Error("GetAllRepositories failed: %v", err.Error())
  58. return
  59. }
  60. for _, repo := range repos {
  61. models.RestoreRepoStatFork(int64(repo.NumForks), repo.ID)
  62. }
  63. ctx.JSON(http.StatusOK, struct{}{})
  64. }
  65. func GetAllProjectsPeriodStatistics(ctx *context.Context) {
  66. recordBeginTime, err := getRecordBeginTime()
  67. if err != nil {
  68. log.Error("Can not get record begin time", err)
  69. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  70. return
  71. }
  72. beginTime, endTime, err := getTimePeroid(ctx, recordBeginTime)
  73. if err != nil {
  74. log.Error("Parameter is wrong", err)
  75. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
  76. return
  77. }
  78. q := ctx.QueryTrim("q")
  79. page := ctx.QueryInt("page")
  80. if page <= 0 {
  81. page = 1
  82. }
  83. pageSize := ctx.QueryInt("pagesize")
  84. if pageSize <= 0 {
  85. pageSize = DEFAULT_PAGE_SIZE
  86. }
  87. orderBy := getOrderBy(ctx)
  88. latestUpdatedTime, latestDate, err := models.GetRepoStatLastUpdatedTime()
  89. if err != nil {
  90. log.Error("Can not query the last updated time.", err)
  91. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.last_update_time_error"))
  92. return
  93. }
  94. countSql := generateCountSql(beginTime, endTime, latestDate, q)
  95. total, err := models.CountRepoStatByRawSql(countSql)
  96. if err != nil {
  97. log.Error("Can not query total count.", err)
  98. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.total_count_get_error"))
  99. return
  100. }
  101. sql := generateSqlByType(ctx, beginTime, endTime, latestDate, q, orderBy, page, pageSize)
  102. projectsPeriodData := ProjectsPeriodData{
  103. RecordBeginTime: recordBeginTime.Format(DATE_FORMAT),
  104. PageSize: pageSize,
  105. TotalPage: getTotalPage(total, pageSize),
  106. TotalCount: total,
  107. LastUpdatedTime: latestUpdatedTime,
  108. PageRecords: models.GetRepoStatisticByRawSql(sql),
  109. }
  110. ctx.JSON(http.StatusOK, projectsPeriodData)
  111. }
  112. func generateSqlByType(ctx *context.Context, beginTime time.Time, endTime time.Time, latestDate string, q string, orderBy string, page int, pageSize int) string {
  113. sql := ""
  114. if ctx.QueryTrim("type") == "all" {
  115. sql = generateTypeAllSql(beginTime, endTime, latestDate, q, orderBy, page, pageSize)
  116. } else {
  117. sql = generatePageSql(beginTime, endTime, latestDate, q, orderBy, page, pageSize)
  118. }
  119. return sql
  120. }
  121. func ServeAllProjectsPeriodStatisticsFile(ctx *context.Context) {
  122. recordBeginTime, err := getRecordBeginTime()
  123. if err != nil {
  124. log.Error("Can not get record begin time", err)
  125. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  126. return
  127. }
  128. beginTime, endTime, err := getTimePeroid(ctx, recordBeginTime)
  129. if err != nil {
  130. log.Error("Parameter is wrong", err)
  131. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
  132. return
  133. }
  134. q := ctx.QueryTrim("q")
  135. page := ctx.QueryInt("page")
  136. if page <= 0 {
  137. page = 1
  138. }
  139. pageSize := 1000
  140. orderBy := getOrderBy(ctx)
  141. _, latestDate, err := models.GetRepoStatLastUpdatedTime()
  142. if err != nil {
  143. log.Error("Can not query the last updated time.", err)
  144. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.last_update_time_error"))
  145. return
  146. }
  147. countSql := generateCountSql(beginTime, endTime, latestDate, q)
  148. total, err := models.CountRepoStatByRawSql(countSql)
  149. if err != nil {
  150. log.Error("Can not query total count.", err)
  151. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.total_count_get_error"))
  152. return
  153. }
  154. var projectAnalysis = ctx.Tr("repo.repo_stat_inspect")
  155. fileName, frontName := getFileName(ctx, beginTime, endTime, projectAnalysis)
  156. if err := os.MkdirAll(setting.RadarMap.Path, os.ModePerm); err != nil {
  157. ctx.Error(http.StatusBadRequest, fmt.Errorf("Failed to create dir %s: %v", setting.AvatarUploadPath, err).Error())
  158. }
  159. totalPage := getTotalPage(total, pageSize)
  160. f := excelize.NewFile()
  161. index := f.NewSheet(projectAnalysis)
  162. for k, v := range allProjectsPeroidHeader(ctx) {
  163. f.SetCellValue(projectAnalysis, k, v)
  164. }
  165. var row = 2
  166. for i := 0; i <= totalPage; i++ {
  167. pageRecords := models.GetRepoStatisticByRawSql(generateSqlByType(ctx, beginTime, endTime, latestDate, q, orderBy, i+1, pageSize))
  168. for _, record := range pageRecords {
  169. for k, v := range allProjectsPeroidValues(row, record, ctx) {
  170. f.SetCellValue(projectAnalysis, k, v)
  171. }
  172. row++
  173. }
  174. }
  175. f.SetActiveSheet(index)
  176. if err := f.SaveAs(fileName); err != nil {
  177. log.Error("Can not generate file.", err)
  178. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.generate_statistic_file_error"))
  179. return
  180. }
  181. ctx.ServeFile(fileName, url.QueryEscape(frontName))
  182. }
  183. func getFileName(ctx *context.Context, beginTime time.Time, endTime time.Time, projectAnalysis string) (string, string) {
  184. baseName := setting.RadarMap.Path + "/" + projectAnalysis + "_"
  185. if ctx.QueryTrim("q") != "" {
  186. baseName = baseName + ctx.QueryTrim("q") + "_"
  187. }
  188. if ctx.QueryTrim("type") == "all" {
  189. baseName = baseName + ctx.Tr("repo.all")
  190. } else {
  191. baseName = baseName + beginTime.AddDate(0, 0, -1).Format(EXCEL_DATE_FORMAT) + "_" + endTime.AddDate(0, 0, -1).Format(EXCEL_DATE_FORMAT)
  192. }
  193. frontName := baseName + ".xlsx"
  194. localName := baseName + "_" + strconv.FormatInt(time.Now().Unix(), 10) + ".xlsx"
  195. return localName, path.Base(frontName)
  196. }
  197. func ClearUnusedStatisticsFile() {
  198. fileInfos, err := ioutil.ReadDir(setting.RadarMap.Path)
  199. if err != nil {
  200. log.Warn("can not read dir: "+setting.RadarMap.Path, err)
  201. return
  202. }
  203. for _, fileInfo := range fileInfos {
  204. if !fileInfo.IsDir() && fileInfo.ModTime().Before(time.Now().AddDate(0, 0, -1)) {
  205. os.Remove(path.Join(setting.RadarMap.Path, fileInfo.Name()))
  206. }
  207. }
  208. }
  209. func allProjectsPeroidHeader(ctx *context.Context) map[string]string {
  210. return map[string]string{"A1": ctx.Tr("admin.repos.id"), "B1": ctx.Tr("admin.repos.projectName"), "C1": ctx.Tr("admin.repos.isPrivate"), "D1": ctx.Tr("admin.repos.openi"), "E1": ctx.Tr("admin.repos.visit"), "F1": ctx.Tr("admin.repos.download"), "G1": ctx.Tr("admin.repos.pr"), "H1": ctx.Tr("admin.repos.commit"),
  211. "I1": ctx.Tr("admin.repos.watches"), "J1": ctx.Tr("admin.repos.stars"), "K1": ctx.Tr("admin.repos.forks"), "L1": ctx.Tr("admin.repos.issues"), "M1": ctx.Tr("admin.repos.closedIssues"), "N1": ctx.Tr("admin.repos.contributor")}
  212. }
  213. func allProjectsPeroidValues(row int, rs *models.RepoStatistic, ctx *context.Context) map[string]string {
  214. return map[string]string{getCellName("A", row): strconv.FormatInt(rs.RepoID, 10), getCellName("B", row): constructDistinctName(rs), getCellName("C", row): getIsPrivateDisplay(rs.IsPrivate, ctx), getCellName("D", row): strconv.FormatFloat(rs.RadarTotal, 'f', 2, 64),
  215. getCellName("E", row): strconv.FormatInt(rs.NumVisits, 10), getCellName("F", row): strconv.FormatInt(rs.NumDownloads, 10), getCellName("G", row): strconv.FormatInt(rs.NumPulls, 10), getCellName("H", row): strconv.FormatInt(rs.NumCommits, 10),
  216. getCellName("I", row): strconv.FormatInt(rs.NumWatches, 10), getCellName("J", row): strconv.FormatInt(rs.NumStars, 10), getCellName("K", row): strconv.FormatInt(rs.NumForks, 10), getCellName("L", row): strconv.FormatInt(rs.NumIssues, 10),
  217. getCellName("M", row): strconv.FormatInt(rs.NumClosedIssues, 10), getCellName("N", row): strconv.FormatInt(rs.NumContributor, 10),
  218. }
  219. }
  220. func getCellName(col string, row int) string {
  221. return col + strconv.Itoa(row)
  222. }
  223. func constructDistinctName(rs *models.RepoStatistic) string {
  224. if rs.OwnerName == "" {
  225. return rs.Name
  226. }
  227. return rs.OwnerName + "/" + rs.Name
  228. }
  229. func getIsPrivateDisplay(private bool, ctx *context.Context) string {
  230. if private {
  231. return ctx.Tr("admin.repos.yes")
  232. } else {
  233. return ctx.Tr("admin.repos.no")
  234. }
  235. }
  236. func GetProjectLatestStatistics(ctx *context.Context) {
  237. repoId := ctx.Params(":id")
  238. recordBeginTime, err := getRecordBeginTime()
  239. if err != nil {
  240. log.Error("Can not get record begin time", err)
  241. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  242. return
  243. }
  244. latestUpdatedTime, latestDate, err := models.GetRepoStatLastUpdatedTime(repoId)
  245. repoIdInt, _ := strconv.ParseInt(repoId, 10, 64)
  246. repoStat, err := models.GetRepoStatisticByDateAndRepoId(latestDate, repoIdInt)
  247. if err != nil {
  248. log.Error("Can not get the repo statistics "+repoId, err)
  249. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.get_repo_stat_error"))
  250. return
  251. }
  252. repository, err := models.GetRepositoryByID(repoIdInt)
  253. if err != nil {
  254. log.Error("Can not get the repo info "+repoId, err)
  255. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.get_repo_info_error"))
  256. return
  257. }
  258. projectLatestData := ProjectLatestData{
  259. RecordBeginTime: recordBeginTime.Format(DATE_FORMAT),
  260. CreatTime: time.Unix(int64(repository.CreatedUnix), 0).Format(DATE_FORMAT),
  261. LastUpdatedTime: latestUpdatedTime,
  262. OpenI: repoStat.RadarTotal,
  263. Comment: repoStat.NumComments,
  264. View: repoStat.NumVisits,
  265. Download: repoStat.NumDownloads,
  266. IssueClosedRatio: repoStat.IssueFixedRate,
  267. Impact: repoStat.Impact,
  268. Completeness: repoStat.Completeness,
  269. Liveness: repoStat.Liveness,
  270. ProjectHealth: repoStat.ProjectHealth,
  271. TeamHealth: repoStat.TeamHealth,
  272. Growth: repoStat.Growth,
  273. Description: repository.Description,
  274. }
  275. contributors, err := models.GetTop10Contributor(repository.RepoPath())
  276. if err != nil {
  277. log.Error("can not get contributors", err)
  278. }
  279. users := make([]UserInfo, 0)
  280. for _, contributor := range contributors {
  281. mode := repository.GetCollaboratorMode(contributor.UserId)
  282. if mode == -1 {
  283. if contributor.IsAdmin {
  284. mode = int(models.AccessModeAdmin)
  285. }
  286. if contributor.UserId == repository.OwnerID {
  287. mode = int(models.AccessModeOwner)
  288. }
  289. }
  290. pr := models.GetPullCountByUserAndRepoId(repoIdInt, contributor.UserId)
  291. userInfo := UserInfo{
  292. User: contributor.Committer,
  293. Commit: contributor.CommitCnt,
  294. Mode: mode,
  295. PR: pr,
  296. RelAvatarLink: contributor.RelAvatarLink,
  297. }
  298. users = append(users, userInfo)
  299. }
  300. projectLatestData.Top10 = users
  301. ctx.JSON(http.StatusOK, projectLatestData)
  302. }
  303. func GetProjectPeriodStatistics(ctx *context.Context) {
  304. repoId := ctx.Params(":id")
  305. recordBeginTime, err := getRecordBeginTime()
  306. if err != nil {
  307. log.Error("Can not get record begin time", err)
  308. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  309. return
  310. }
  311. repoIdInt, _ := strconv.ParseInt(repoId, 10, 64)
  312. if err != nil {
  313. log.Error("Can not get record begin time", err)
  314. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  315. return
  316. }
  317. beginTime, endTime, err := getTimePeroid(ctx, recordBeginTime)
  318. isOpenI := ctx.QueryBool("openi")
  319. var repositorys []*models.RepoStatistic
  320. if isOpenI {
  321. repositorys = models.GetRepoStatisticByRawSql(generateRadarSql(beginTime, endTime, repoIdInt))
  322. } else {
  323. repositorys = models.GetRepoStatisticByRawSql(generateTargetSql(beginTime, endTime, repoIdInt))
  324. }
  325. ctx.JSON(http.StatusOK, repositorys)
  326. }
  327. func generateRadarSql(beginTime time.Time, endTime time.Time, repoId int64) string {
  328. sql := "SELECT date, impact, completeness, liveness, project_health, team_health, growth, radar_total FROM repo_statistic" +
  329. " where repo_id=" + strconv.FormatInt(repoId, 10) + " and created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  330. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  331. return sql
  332. }
  333. func generateTargetSql(beginTime time.Time, endTime time.Time, repoId int64) string {
  334. sql := "SELECT date, num_visits,num_downloads,num_commits FROM repo_statistic" +
  335. " where repo_id=" + strconv.FormatInt(repoId, 10) + " and created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  336. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  337. return sql
  338. }
  339. func generateCountSql(beginTime time.Time, endTime time.Time, latestDate string, q string) string {
  340. countSql := "SELECT count(*) FROM " +
  341. "(SELECT repo_id FROM repo_statistic where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  342. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) + " group by repo_id) A," +
  343. "(SELECT repo_id,name,is_private,radar_total from public.repo_statistic where date='" + latestDate + "') B" +
  344. " where A.repo_id=B.repo_id"
  345. if q != "" {
  346. countSql = countSql + " and B.name like '%" + q + "%'"
  347. }
  348. return countSql
  349. }
  350. func generateTypeAllSql(beginTime time.Time, endTime time.Time, latestDate string, q string, orderBy string, page int, pageSize int) string {
  351. sql := "SELECT A.repo_id,name,owner_name,is_private,radar_total,num_watches,num_visits,num_downloads,num_pulls,num_commits,num_stars,num_forks,num_issues,num_closed_issues,num_contributor FROM " +
  352. "(SELECT repo_id,sum(num_visits) as num_visits " +
  353. " FROM repo_statistic where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  354. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) + " group by repo_id) A," +
  355. "(SELECT repo_id,name,owner_name,is_private,radar_total,num_watches,num_downloads,num_pulls,num_commits,num_stars,num_forks,num_issues,num_closed_issues,num_contributor from public.repo_statistic where date='" + latestDate + "') B" +
  356. " where A.repo_id=B.repo_id"
  357. if q != "" {
  358. sql = sql + " and name like '%" + q + "%'"
  359. }
  360. sql = sql + " order by " + orderBy + " desc,repo_id" + " limit " + strconv.Itoa(pageSize) + " offset " + strconv.Itoa((page-1)*pageSize)
  361. return sql
  362. }
  363. func generatePageSql(beginTime time.Time, endTime time.Time, latestDate string, q string, orderBy string, page int, pageSize int) string {
  364. sql := "SELECT A.repo_id,name,owner_name,is_private,radar_total,num_watches,num_visits,num_downloads,num_pulls,num_commits,num_stars,num_forks,num_issues,num_closed_issues,num_contributor FROM " +
  365. "(SELECT repo_id,sum(num_watches_added) as num_watches,sum(num_visits) as num_visits, sum(num_downloads_added) as num_downloads,sum(num_pulls_added) as num_pulls,sum(num_commits_added) as num_commits,sum(num_stars_added) as num_stars,sum(num_forks_added) num_forks,sum(num_issues_added) as num_issues,sum(num_closed_issues_added) as num_closed_issues,sum(num_contributor_added) as num_contributor " +
  366. " FROM repo_statistic where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  367. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) + " group by repo_id) A," +
  368. "(SELECT repo_id,name,owner_name,is_private,radar_total from public.repo_statistic where date='" + latestDate + "') B" +
  369. " where A.repo_id=B.repo_id"
  370. if q != "" {
  371. sql = sql + " and B.name like '%" + q + "%'"
  372. }
  373. sql = sql + " order by " + orderBy + " desc,A.repo_id" + " limit " + strconv.Itoa(pageSize) + " offset " + strconv.Itoa((page-1)*pageSize)
  374. return sql
  375. }
  376. func getOrderBy(ctx *context.Context) string {
  377. orderBy := ""
  378. switch ctx.Query("sort") {
  379. case "openi":
  380. orderBy = "B.radar_total"
  381. case "view":
  382. orderBy = "A.num_visits"
  383. case "download":
  384. orderBy = "A.num_downloads"
  385. case "pr":
  386. orderBy = "A.num_pulls"
  387. case "commit":
  388. orderBy = "A.num_commits"
  389. case "watch":
  390. orderBy = "A.num_watches"
  391. case "star":
  392. orderBy = "A.num_stars"
  393. case "fork":
  394. orderBy = "A.num_forks"
  395. case "issue":
  396. orderBy = "A.num_issues"
  397. case "issue_closed":
  398. orderBy = "A.num_closed_issues"
  399. case "contributor":
  400. orderBy = "A.num_contributor"
  401. default:
  402. orderBy = "B.radar_total"
  403. }
  404. return orderBy
  405. }
  406. func getTimePeroid(ctx *context.Context, recordBeginTime time.Time) (time.Time, time.Time, error) {
  407. queryType := ctx.QueryTrim("type")
  408. now := time.Now()
  409. recordBeginTimeTemp := recordBeginTime.AddDate(0, 0, 1)
  410. beginTimeStr := ctx.QueryTrim("beginTime")
  411. endTimeStr := ctx.QueryTrim("endTime")
  412. var beginTime time.Time
  413. var endTime time.Time
  414. var err error
  415. if queryType != "" {
  416. if queryType == "all" {
  417. beginTime = recordBeginTimeTemp
  418. endTime = now
  419. } else if queryType == "yesterday" {
  420. endTime = now
  421. beginTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  422. } else if queryType == "current_week" {
  423. beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+2) //begin from monday
  424. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  425. endTime = now
  426. } else if queryType == "current_month" {
  427. endTime = now
  428. beginTime = time.Date(endTime.Year(), endTime.Month(), 2, 0, 0, 0, 0, now.Location())
  429. } else if queryType == "monthly" {
  430. endTime = now
  431. beginTime = now.AddDate(0, -1, 1)
  432. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  433. } else if queryType == "current_year" {
  434. endTime = now
  435. beginTime = time.Date(endTime.Year(), 1, 2, 0, 0, 0, 0, now.Location())
  436. } else if queryType == "last_month" {
  437. lastMonthTime := now.AddDate(0, -1, 0)
  438. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 2, 0, 0, 0, 0, now.Location())
  439. endTime = time.Date(now.Year(), now.Month(), 2, 0, 0, 0, 0, now.Location())
  440. } else {
  441. return now, now, fmt.Errorf("The value of type parameter is wrong.")
  442. }
  443. } else {
  444. if beginTimeStr == "" || endTimeStr == "" {
  445. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  446. beginTime = recordBeginTimeTemp
  447. endTime = now
  448. } else {
  449. beginTime, err = time.Parse("2006-01-02", beginTimeStr)
  450. if err != nil {
  451. return now, now, err
  452. }
  453. endTime, err = time.Parse("2006-01-02", endTimeStr)
  454. if err != nil {
  455. return now, now, err
  456. }
  457. beginTime = beginTime.AddDate(0, 0, 1)
  458. endTime = endTime.AddDate(0, 0, 1)
  459. }
  460. }
  461. if beginTime.Before(recordBeginTimeTemp) {
  462. beginTime = recordBeginTimeTemp
  463. }
  464. return beginTime, endTime, nil
  465. }
  466. func getRecordBeginTime() (time.Time, error) {
  467. return time.Parse(DATE_FORMAT, setting.RadarMap.RecordBeginTime)
  468. }
  469. func getTotalPage(total int64, pageSize int) int {
  470. another := 0
  471. if int(total)%pageSize != 0 {
  472. another = 1
  473. }
  474. return int(total)/pageSize + another
  475. }