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 19 kB

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