Browse Source

Merge pull request '新增更新项目浏览量接口' (#936) from update-views-tool into V20211115

Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/936
Reviewed-by: ychao_1983 <ychao_1983@sina.com>
pull/946/head
ychao_1983 3 years ago
parent
commit
89d158e3d9
5 changed files with 70 additions and 7 deletions
  1. +1
    -1
      go.mod
  2. +7
    -0
      models/repo_statistic.go
  3. +2
    -1
      routers/private/internal.go
  4. +28
    -1
      routers/private/tool.go
  5. +32
    -4
      routers/repo/repo_statistic.go

+ 1
- 1
go.mod View File

@@ -17,6 +17,7 @@ require (
gitea.com/macaron/macaron v1.4.0 gitea.com/macaron/macaron v1.4.0
gitea.com/macaron/session v0.0.0-20191207215012-613cebf0674d gitea.com/macaron/session v0.0.0-20191207215012-613cebf0674d
gitea.com/macaron/toolbox v0.0.0-20190822013122-05ff0fc766b7 gitea.com/macaron/toolbox v0.0.0-20190822013122-05ff0fc766b7
github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.2
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/PuerkitoBio/goquery v1.5.0 github.com/PuerkitoBio/goquery v1.5.0
github.com/RichardKnop/machinery v1.6.9 github.com/RichardKnop/machinery v1.6.9
@@ -107,7 +108,6 @@ require (
github.com/unknwon/paginater v0.0.0-20151104151617-7748a72e0141 github.com/unknwon/paginater v0.0.0-20151104151617-7748a72e0141
github.com/urfave/cli v1.22.1 github.com/urfave/cli v1.22.1
github.com/xanzy/go-gitlab v0.31.0 github.com/xanzy/go-gitlab v0.31.0
github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.2
github.com/yohcop/openid-go v1.0.0 github.com/yohcop/openid-go v1.0.0
github.com/yuin/goldmark v1.1.27 github.com/yuin/goldmark v1.1.27
github.com/yuin/goldmark-meta v0.0.0-20191126180153-f0638e958b60 github.com/yuin/goldmark-meta v0.0.0-20191126180153-f0638e958b60


+ 7
- 0
models/repo_statistic.go View File

@@ -172,3 +172,10 @@ func UpdateRepoStat(repoStat *RepoStatistic) error {
_, err := xStatistic.Exec(sql, repoStat.Impact, repoStat.Completeness, repoStat.Liveness, repoStat.ProjectHealth, repoStat.TeamHealth, repoStat.Growth, repoStat.RadarTotal, repoStat.RepoID, repoStat.Date) _, err := xStatistic.Exec(sql, repoStat.Impact, repoStat.Completeness, repoStat.Liveness, repoStat.ProjectHealth, repoStat.TeamHealth, repoStat.Growth, repoStat.RadarTotal, repoStat.RepoID, repoStat.Date)
return err return err
} }

func UpdateRepoStatVisits(repoStat *RepoStatistic) error {
sql := "update repo_statistic set num_visits=? where repo_id=? and date=?"

_, err := xStatistic.Exec(sql, repoStat.NumVisits, repoStat.RepoID, repoStat.Date)
return err
}

+ 2
- 1
routers/private/internal.go View File

@@ -43,7 +43,8 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/manager/restart", Restart) m.Post("/manager/restart", Restart)
m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues) m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
m.Post("/tool/update_all_repo_commit_cnt", UpdateAllRepoCommitCnt) m.Post("/tool/update_all_repo_commit_cnt", UpdateAllRepoCommitCnt)
m.Post("/tool/repo_stat", RepoStatisticManually)
m.Post("/tool/repo_stat/:date", RepoStatisticManually)
m.Post("/tool/update_repo_visit/:date", UpdateRepoVisit)


}, CheckInternalToken) }, CheckInternalToken)
} }

+ 28
- 1
routers/private/tool.go View File

@@ -39,8 +39,35 @@ func UpdateAllRepoCommitCnt(ctx *macaron.Context) {
} }


func RepoStatisticManually(ctx *macaron.Context) { func RepoStatisticManually(ctx *macaron.Context) {
date := ctx.Query("date")
date := ctx.Params("date")
repo.RepoStatisticDaily(date) repo.RepoStatisticDaily(date)
repo.SummaryStatisticDaily(date) repo.SummaryStatisticDaily(date)
repo.TimingCountDataByDate(date) repo.TimingCountDataByDate(date)
} }

func UpdateRepoVisit(ctx *macaron.Context) {
date := ctx.Params("date")
log.Info("date(%s)", date)

repos, err := models.GetAllRepositories()
if err != nil {
log.Error("GetAllRepositories failed:%v", err.Error(), ctx.Data["MsgID"])
ctx.JSON(http.StatusInternalServerError, map[string]string{
"error_msg": "GetAllRepositories failed",
})
return
}

for i, repoStat := range repos {
log.Info("%d:begin UpdateRepoVisits(id = %d, name = %s)", i, repoStat.ID, repoStat.Name)
if err = repo.UpdateRepoVisits(ctx, repoStat, date); err != nil {
log.Error("UpdateRepoVisits(id = %d, name = %s) failed:%v", repoStat.ID, repoStat.Name, err.Error())
continue
}
log.Info("%d:finish UpdateRepoVisits(id = %d, name = %s)", i, repoStat.ID, repoStat.Name)
}

ctx.JSON(http.StatusOK, map[string]string{
"error_msg": "",
})
}

+ 32
- 4
routers/repo/repo_statistic.go View File

@@ -1,15 +1,17 @@
package repo package repo


import ( import (
"errors"
"time" "time"


"code.gitea.io/gitea/services/mailer"

"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/normalization" "code.gitea.io/gitea/modules/normalization"
"code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/mailer"

"gitea.com/macaron/macaron"
) )


func StatisticAuto() { func StatisticAuto() {
@@ -48,7 +50,7 @@ func RepoStatisticDaily(date string) {
var maxRepoRadar models.RepoStatistic var maxRepoRadar models.RepoStatistic


for i, repo := range repos { for i, repo := range repos {
log.Info("start statistic: %s", repo.Name)
log.Info("start statistic: %s", getDistinctProjectName(repo))
var numDevMonths, numWikiViews, numContributor, numKeyContributor, numCommitsGrowth, numCommitLinesGrowth, numContributorsGrowth int64 var numDevMonths, numWikiViews, numContributor, numKeyContributor, numCommitsGrowth, numCommitLinesGrowth, numContributorsGrowth int64
repoGitStat, err := models.GetRepoKPIStats(repo) repoGitStat, err := models.GetRepoKPIStats(repo)
if err != nil { if err != nil {
@@ -230,7 +232,7 @@ func RepoStatisticDaily(date string) {


} }


log.Info("finish statistic: %s", repo.Name)
log.Info("finish statistic: %s", getDistinctProjectName(repo))
} }


//radar map //radar map
@@ -274,3 +276,29 @@ func getStatTime(timeStr string) (string, string) {


return beginTime, endTime return beginTime, endTime
} }

func UpdateRepoVisits(ctx *macaron.Context, repo *models.Repository, date string) error {
beginTime, endTime := getStatTime(date)
var numVisits int
numVisits, err := repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
if err != nil {
log.Error("AppointProjectView failed(%s): %v", getDistinctProjectName(repo), err)
return err
}

repoStat, err := models.GetRepoStatisticByDate(date, repo.ID)
if err != nil {
log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
return err
} else if len(repoStat) != 1 {
log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
return errors.New("not find repo")
}
repoStat[0].NumVisits = int64(numVisits)

if err = models.UpdateRepoStatVisits(repoStat[0]); err != nil {
log.Error("UpdateRepoStatVisits failed(%s): %v", getDistinctProjectName(repo), err)
return err
}
return nil
}

Loading…
Cancel
Save