Skip to content

Commit 01e2062

Browse files
zeripathAbdulrhmnGhanem
authored andcommitted
Estimate Action Count in Statistics (go-gitea#19775)
1 parent 0ac1db4 commit 01e2062

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

models/db/context.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/modules/setting"
1212

1313
"xorm.io/builder"
14+
"xorm.io/xorm/schemas"
1415
)
1516

1617
// DefaultContext is the default context to run xorm queries in
@@ -175,3 +176,24 @@ func CountByBean(ctx context.Context, bean interface{}) (int64, error) {
175176
func TableName(bean interface{}) string {
176177
return x.TableName(bean)
177178
}
179+
180+
// EstimateCount returns an estimate of total number of rows in table
181+
func EstimateCount(ctx context.Context, bean interface{}) (int64, error) {
182+
e := GetEngine(ctx)
183+
e.Context(ctx)
184+
185+
var rows int64
186+
var err error
187+
tablename := TableName(bean)
188+
switch x.Dialect().URI().DBType {
189+
case schemas.MYSQL:
190+
_, 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)
191+
case schemas.POSTGRES:
192+
_, err = e.Context(ctx).SQL("SELECT reltuples AS estimate FROM pg_class WHERE relname = ?;", tablename).Get(&rows)
193+
case schemas.MSSQL:
194+
_, err = e.Context(ctx).SQL("sp_spaceused ?;", tablename).Get(&rows)
195+
default:
196+
return e.Context(ctx).Count(tablename)
197+
}
198+
return rows, err
199+
}

models/statistic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func GetStatistic() (stats Statistic) {
5656
stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{})
5757
stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
5858
stats.Counter.Star, _ = e.Count(new(repo_model.Star))
59-
stats.Counter.Action, _ = e.Count(new(Action))
59+
stats.Counter.Action, _ = db.EstimateCount(db.DefaultContext, new(Action))
6060
stats.Counter.Access, _ = e.Count(new(access_model.Access))
6161

6262
type IssueCount struct {

options/locale/locale_en-US.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,7 @@ dashboard.new_version_hint = Gitea %s is now available, you are running %s. Chec
24232423
dashboard.statistic = Summary
24242424
dashboard.operations = Maintenance Operations
24252425
dashboard.system_status = System Status
2426-
dashboard.statistic_info = The Gitea database holds <b>%d</b> users, <b>%d</b> organizations, <b>%d</b> public keys, <b>%d</b> repositories, <b>%d</b> watches, <b>%d</b> stars, <b>%d</b> actions, <b>%d</b> accesses, <b>%d</b> issues, <b>%d</b> comments, <b>%d</b> social accounts, <b>%d</b> follows, <b>%d</b> mirrors, <b>%d</b> releases, <b>%d</b> authentication sources, <b>%d</b> webhooks, <b>%d</b> milestones, <b>%d</b> labels, <b>%d</b> hook tasks, <b>%d</b> teams, <b>%d</b> update tasks, <b>%d</b> attachments.
2426+
dashboard.statistic_info = The Gitea database holds <b>%d</b> users, <b>%d</b> organizations, <b>%d</b> public keys, <b>%d</b> repositories, <b>%d</b> watches, <b>%d</b> stars, ~<b>%d</b> actions, <b>%d</b> accesses, <b>%d</b> issues, <b>%d</b> comments, <b>%d</b> social accounts, <b>%d</b> follows, <b>%d</b> mirrors, <b>%d</b> releases, <b>%d</b> authentication sources, <b>%d</b> webhooks, <b>%d</b> milestones, <b>%d</b> labels, <b>%d</b> hook tasks, <b>%d</b> teams, <b>%d</b> update tasks, <b>%d</b> attachments.
24272427
dashboard.operation_name = Operation Name
24282428
dashboard.operation_switch = Switch
24292429
dashboard.operation_run = Run

0 commit comments

Comments
 (0)