diff options
author | 无闻 <u@gogs.io> | 2015-07-25 07:22:50 +0800 |
---|---|---|
committer | 无闻 <u@gogs.io> | 2015-07-25 07:22:50 +0800 |
commit | b838e949806b8b50b339d4a3fb0f08b873d74a83 (patch) | |
tree | c92a3f92161ce41bb7042fda96f8bdf019b5d9a1 /models/repo.go | |
parent | 752b52715205eb68cec5881529460da8644a5c3c (diff) | |
parent | 20a8d651f39eb138a8448257b0d8c5b4283ed6eb (diff) | |
download | gitea-b838e949806b8b50b339d4a3fb0f08b873d74a83.tar.gz gitea-b838e949806b8b50b339d4a3fb0f08b873d74a83.zip |
Merge pull request #1111 from vitalvas/develop
Add function in cron to autofix counters in repository
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/models/repo.go b/models/repo.go index 5610776a9f..0254e8f004 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1037,6 +1037,7 @@ var ( // Prevent duplicate tasks. isMirrorUpdating = false isGitFscking = false + isCheckRepos = false ) // MirrorUpdate checks and updates mirror repositories. @@ -1128,6 +1129,42 @@ func GitGcRepos() error { }) } +func CheckRepoStats() { + if isCheckRepos { + return + } + isCheckRepos = true + defer func() { isCheckRepos = false }() + + // Check count watchers + results_watch, err := x.Query("SELECT r.id FROM `repository` r WHERE r.num_watches!=(SELECT count(*) FROM `watch` WHERE repo_id=r.id)") + if err != nil { + log.Error(4, "select repository check 'watch': %v", err) + } + for _, repo_id := range results_watch { + log.Info("updating repository count 'watch'") + repoID := com.StrTo(repo_id["id"]).MustInt64() + _, err := x.Exec("UPDATE `repository` SET num_watches=(SELECT count(*) FROM `watch` WHERE repo_id=?) WHERE id=?", repoID, repoID) + if err != nil { + log.Error(4, "update repository check 'watch', repo %v: %v", repo_id, err) + } + } + + // Check count stars + results_star, err := x.Query("SELECT s.id FROM `repository` s WHERE s.num_stars!=(SELECT count(*) FROM `star` WHERE repo_id=s.id)") + if err != nil { + log.Error(4, "select repository check 'star': %v", err) + } + for _, repo_id := range results_star { + log.Info("updating repository count 'star'") + repoID := com.StrTo(repo_id["id"]).MustInt64() + _, err := x.Exec("UPDATE `repository` SET .num_stars=(SELECT count(*) FROM `star` WHERE repo_id=?) WHERE id=?", repoID, repoID) + if err != nil { + log.Error(4, "update repository check 'star', repo %v: %v", repo_id, err) + } + } +} + // _________ .__ .__ ___. __ .__ // \_ ___ \ ____ | | | | _____ \_ |__ ________________ _/ |_|__| ____ ____ // / \ \/ / _ \| | | | \__ \ | __ \ / _ \_ __ \__ \\ __\ |/ _ \ / \ |