@ -19,6 +19,8 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"xorm.io/builder"
)
// Init initialize model
@ -27,7 +29,7 @@ func Init(ctx context.Context) error {
}
type repoChecker struct {
querySQL func ( ctx context . Context ) ( [ ] map [ string ] [ ] byte , error )
querySQL func ( ctx context . Context ) ( [ ] int64 , error )
correctSQL func ( ctx context . Context , id int64 ) error
desc string
}
@ -38,8 +40,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
log . Error ( "Select %s: %v" , checker . desc , err )
return
}
for _ , result := range results {
id , _ := strconv . ParseInt ( string ( result [ "id" ] ) , 10 , 64 )
for _ , id := range results {
select {
case <- ctx . Done ( ) :
log . Warn ( "CheckRepoStats: Cancelled before checking %s for with id=%d" , checker . desc , id )
@ -54,21 +55,23 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
}
}
func StatsCorrectSQL ( ctx context . Context , sql string , id int64 ) error {
_ , err := db . GetEngine ( ctx ) . Exec ( sql , id , id )
func StatsCorrectSQL ( ctx context . Context , sql any , ids ... any ) error {
args := [ ] any { sql }
args = append ( args , ids ... )
_ , err := db . GetEngine ( ctx ) . Exec ( args ... )
return err
}
func repoStatsCorrectNumWatches ( ctx context . Context , id int64 ) error {
return StatsCorrectSQL ( ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id )
return StatsCorrectSQL ( ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id , id )
}
func repoStatsCorrectNumStars ( ctx context . Context , id int64 ) error {
return StatsCorrectSQL ( ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id )
return StatsCorrectSQL ( ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id , id )
}
func labelStatsCorrectNumIssues ( ctx context . Context , id int64 ) error {
return StatsCorrectSQL ( ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id )
return StatsCorrectSQL ( ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id , id )
}
func labelStatsCorrectNumIssuesRepo ( ctx context . Context , id int64 ) error {
@ -105,11 +108,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
}
func userStatsCorrectNumRepos ( ctx context . Context , id int64 ) error {
return StatsCorrectSQL ( ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id )
return StatsCorrectSQL ( ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id , id )
}
func repoStatsCorrectIssueNumComments ( ctx context . Context , id int64 ) error {
return StatsCorrectSQL ( ctx , "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?" , id )
return StatsCorrectSQL ( ctx , issues_model . UpdateIssueNumCommentsBuilder ( id ) )
}
func repoStatsCorrectNumIssues ( ctx context . Context , id int64 ) error {
@ -128,9 +131,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
return repo_model . UpdateRepoIssueNumbers ( ctx , id , true , true )
}
func statsQuery ( args ... any ) func ( context . Context ) ( [ ] map [ string ] [ ] byte , error ) {
return func ( ctx context . Context ) ( [ ] map [ string ] [ ] byte , error ) {
return db . GetEngine ( ctx ) . Query ( args ... )
// statsQuery returns a function that queries the database for a list of IDs
// sql could be a string or a *builder.Builder
func statsQuery ( sql any , args ... any ) func ( context . Context ) ( [ ] int64 , error ) {
return func ( ctx context . Context ) ( [ ] int64 , error ) {
var ids [ ] int64
return ids , db . GetEngine ( ctx ) . SQL ( sql , args ... ) . Find ( & ids )
}
}
@ -201,7 +207,16 @@ func CheckRepoStats(ctx context.Context) error {
} ,
// Issue.NumComments
{
statsQuery ( "SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)" ) ,
statsQuery ( builder . Select ( "`issue`.id" ) . From ( "`issue`" ) . Where (
builder . Neq {
"`issue`.num_comments" : builder . Select ( "COUNT(*)" ) . From ( "`comment`" ) . Where (
builder . Expr ( "issue_id = `issue`.id" ) . And (
builder . In ( "type" , issues_model . ConversationCountedCommentType ( ) ) ,
) ,
) ,
} ,
) ,
) ,
repoStatsCorrectIssueNumComments ,
"issue count 'num_comments'" ,
} ,