diff options
author | zeripath <art27@cantab.net> | 2022-05-21 19:50:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-21 14:50:50 -0400 |
commit | ffb7ab31f2cb3629a80e8105f84575d5b1182051 (patch) | |
tree | ae76846ec63f2456245228651b3c4be8e1535376 /models | |
parent | 468387e9ced12367aecc8b863e20e105fbdd0c82 (diff) | |
download | gitea-ffb7ab31f2cb3629a80e8105f84575d5b1182051.tar.gz gitea-ffb7ab31f2cb3629a80e8105f84575d5b1182051.zip |
Estimate Action Count in Statistics (#19775)
Diffstat (limited to 'models')
-rw-r--r-- | models/db/context.go | 22 | ||||
-rw-r--r-- | models/statistic.go | 2 |
2 files changed, 23 insertions, 1 deletions
diff --git a/models/db/context.go b/models/db/context.go index c823952cf6..dae7be3a93 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/setting" "xorm.io/builder" + "xorm.io/xorm/schemas" ) // DefaultContext is the default context to run xorm queries in @@ -175,3 +176,24 @@ func CountByBean(ctx context.Context, bean interface{}) (int64, error) { func TableName(bean interface{}) string { return x.TableName(bean) } + +// EstimateCount returns an estimate of total number of rows in table +func EstimateCount(ctx context.Context, bean interface{}) (int64, error) { + e := GetEngine(ctx) + e.Context(ctx) + + var rows int64 + var err error + tablename := TableName(bean) + switch x.Dialect().URI().DBType { + case schemas.MYSQL: + _, err = e.Context(ctx).SQL("SELECT table_rows FROM information_schema.tables WHERE tables.table_name = ? AND tables.table_schema = ?;", tablename, x.Dialect().URI().DBName).Get(&rows) + case schemas.POSTGRES: + _, err = e.Context(ctx).SQL("SELECT reltuples AS estimate FROM pg_class WHERE relname = ?;", tablename).Get(&rows) + case schemas.MSSQL: + _, err = e.Context(ctx).SQL("sp_spaceused ?;", tablename).Get(&rows) + default: + return e.Context(ctx).Count(tablename) + } + return rows, err +} diff --git a/models/statistic.go b/models/statistic.go index 2b6b3a1820..dfc236ec58 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -56,7 +56,7 @@ func GetStatistic() (stats Statistic) { stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{}) stats.Counter.Watch, _ = e.Count(new(repo_model.Watch)) stats.Counter.Star, _ = e.Count(new(repo_model.Star)) - stats.Counter.Action, _ = e.Count(new(Action)) + stats.Counter.Action, _ = db.EstimateCount(db.DefaultContext, new(Action)) stats.Counter.Access, _ = e.Count(new(access_model.Access)) type IssueCount struct { |