2021-09-19 14:49:59 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
2021-09-24 14:32:56 +03:00
import (
2021-12-10 11:14:24 +03:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2022-01-02 16:12:35 +03:00
"code.gitea.io/gitea/models/auth"
2021-09-24 14:32:56 +03:00
"code.gitea.io/gitea/models/db"
2021-11-19 16:39:57 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-17 12:58:31 +03:00
user_model "code.gitea.io/gitea/models/user"
2021-11-10 08:13:16 +03:00
"code.gitea.io/gitea/models/webhook"
2021-10-04 01:46:44 +03:00
"code.gitea.io/gitea/modules/setting"
2021-09-24 14:32:56 +03:00
)
2021-09-19 14:49:59 +03:00
// Statistic contains the database statistics
type Statistic struct {
Counter struct {
User , Org , PublicKey ,
Repo , Watch , Star , Action , Access ,
Issue , IssueClosed , IssueOpen ,
Comment , Oauth , Follow ,
2022-01-02 16:12:35 +03:00
Mirror , Release , AuthSource , Webhook ,
2021-09-19 14:49:59 +03:00
Milestone , Label , HookTask ,
2021-10-02 04:32:15 +03:00
Team , UpdateTask , Project ,
ProjectBoard , Attachment int64
2021-10-05 21:39:37 +03:00
IssueByLabel [ ] IssueByLabelCount
IssueByRepository [ ] IssueByRepositoryCount
2021-09-19 14:49:59 +03:00
}
}
2021-10-04 01:46:44 +03:00
// IssueByLabelCount contains the number of issue group by label
type IssueByLabelCount struct {
Count int64
Label string
}
2021-10-05 21:39:37 +03:00
// IssueByRepositoryCount contains the number of issue group by repository
type IssueByRepositoryCount struct {
Count int64
OwnerName string
Repository string
}
2021-09-19 14:49:59 +03:00
// GetStatistic returns the database statistics
func GetStatistic ( ) ( stats Statistic ) {
2021-10-02 04:32:15 +03:00
e := db . GetEngine ( db . DefaultContext )
2021-11-24 12:49:20 +03:00
stats . Counter . User = user_model . CountUsers ( )
2021-09-19 14:49:59 +03:00
stats . Counter . Org = CountOrganizations ( )
2021-12-10 11:14:24 +03:00
stats . Counter . PublicKey , _ = e . Count ( new ( asymkey_model . PublicKey ) )
2021-12-10 04:27:50 +03:00
stats . Counter . Repo = repo_model . CountRepositories ( true )
2021-12-12 18:48:20 +03:00
stats . Counter . Watch , _ = e . Count ( new ( repo_model . Watch ) )
stats . Counter . Star , _ = e . Count ( new ( repo_model . Star ) )
2021-10-02 04:32:15 +03:00
stats . Counter . Action , _ = e . Count ( new ( Action ) )
stats . Counter . Access , _ = e . Count ( new ( Access ) )
2021-09-19 14:49:59 +03:00
type IssueCount struct {
Count int64
IsClosed bool
}
2021-10-04 01:46:44 +03:00
if setting . Metrics . EnabledIssueByLabel {
stats . Counter . IssueByLabel = [ ] IssueByLabelCount { }
_ = e . Select ( "COUNT(*) AS count, l.name AS label" ) .
Join ( "LEFT" , "label l" , "l.id=il.label_id" ) .
Table ( "issue_label il" ) .
GroupBy ( "l.name" ) .
Find ( & stats . Counter . IssueByLabel )
}
2021-10-05 21:39:37 +03:00
if setting . Metrics . EnabledIssueByRepository {
stats . Counter . IssueByRepository = [ ] IssueByRepositoryCount { }
_ = e . Select ( "COUNT(*) AS count, r.owner_name, r.name AS repository" ) .
Join ( "LEFT" , "repository r" , "r.id=i.repo_id" ) .
Table ( "issue i" ) .
GroupBy ( "r.owner_name, r.name" ) .
Find ( & stats . Counter . IssueByRepository )
}
2021-09-19 14:49:59 +03:00
issueCounts := [ ] IssueCount { }
2021-10-02 04:32:15 +03:00
_ = e . Select ( "COUNT(*) AS count, is_closed" ) . Table ( "issue" ) . GroupBy ( "is_closed" ) . Find ( & issueCounts )
2021-09-19 14:49:59 +03:00
for _ , c := range issueCounts {
if c . IsClosed {
stats . Counter . IssueClosed = c . Count
} else {
stats . Counter . IssueOpen = c . Count
}
}
stats . Counter . Issue = stats . Counter . IssueClosed + stats . Counter . IssueOpen
2021-10-02 04:32:15 +03:00
stats . Counter . Comment , _ = e . Count ( new ( Comment ) )
2021-09-19 14:49:59 +03:00
stats . Counter . Oauth = 0
2021-11-17 12:58:31 +03:00
stats . Counter . Follow , _ = e . Count ( new ( user_model . Follow ) )
2021-12-10 04:27:50 +03:00
stats . Counter . Mirror , _ = e . Count ( new ( repo_model . Mirror ) )
2021-10-02 04:32:15 +03:00
stats . Counter . Release , _ = e . Count ( new ( Release ) )
2022-01-02 16:12:35 +03:00
stats . Counter . AuthSource = auth . CountSources ( )
2021-11-10 08:13:16 +03:00
stats . Counter . Webhook , _ = e . Count ( new ( webhook . Webhook ) )
2021-10-02 04:32:15 +03:00
stats . Counter . Milestone , _ = e . Count ( new ( Milestone ) )
stats . Counter . Label , _ = e . Count ( new ( Label ) )
2021-11-10 08:13:16 +03:00
stats . Counter . HookTask , _ = e . Count ( new ( webhook . HookTask ) )
2021-10-02 04:32:15 +03:00
stats . Counter . Team , _ = e . Count ( new ( Team ) )
2021-11-19 16:39:57 +03:00
stats . Counter . Attachment , _ = e . Count ( new ( repo_model . Attachment ) )
2021-10-02 04:32:15 +03:00
stats . Counter . Project , _ = e . Count ( new ( Project ) )
stats . Counter . ProjectBoard , _ = e . Count ( new ( ProjectBoard ) )
2021-09-19 14:49:59 +03:00
return
}