From ffb7ab31f2cb3629a80e8105f84575d5b1182051 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 21 May 2022 19:50:50 +0100 Subject: [PATCH] Estimate Action Count in Statistics (#19775) --- models/db/context.go | 22 ++++++++++++++++++++++ models/statistic.go | 2 +- options/locale/locale_en-US.ini | 2 +- 3 files changed, 24 insertions(+), 2 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 { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ba619b413c..5061c735ae 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2423,7 +2423,7 @@ dashboard.new_version_hint = Gitea %s is now available, you are running %s. Chec dashboard.statistic = Summary dashboard.operations = Maintenance Operations dashboard.system_status = System Status -dashboard.statistic_info = The Gitea database holds %d users, %d organizations, %d public keys, %d repositories, %d watches, %d stars, %d actions, %d accesses, %d issues, %d comments, %d social accounts, %d follows, %d mirrors, %d releases, %d authentication sources, %d webhooks, %d milestones, %d labels, %d hook tasks, %d teams, %d update tasks, %d attachments. +dashboard.statistic_info = The Gitea database holds %d users, %d organizations, %d public keys, %d repositories, %d watches, %d stars, ~%d actions, %d accesses, %d issues, %d comments, %d social accounts, %d follows, %d mirrors, %d releases, %d authentication sources, %d webhooks, %d milestones, %d labels, %d hook tasks, %d teams, %d update tasks, %d attachments. dashboard.operation_name = Operation Name dashboard.operation_switch = Switch dashboard.operation_run = Run