code.gitea.io/gitea@v1.22.3/models/activities/statistic.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package activities
     5  
     6  import (
     7  	"context"
     8  
     9  	asymkey_model "code.gitea.io/gitea/models/asymkey"
    10  	"code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/models/db"
    12  	git_model "code.gitea.io/gitea/models/git"
    13  	issues_model "code.gitea.io/gitea/models/issues"
    14  	"code.gitea.io/gitea/models/organization"
    15  	access_model "code.gitea.io/gitea/models/perm/access"
    16  	project_model "code.gitea.io/gitea/models/project"
    17  	repo_model "code.gitea.io/gitea/models/repo"
    18  	user_model "code.gitea.io/gitea/models/user"
    19  	"code.gitea.io/gitea/models/webhook"
    20  	"code.gitea.io/gitea/modules/setting"
    21  )
    22  
    23  // Statistic contains the database statistics
    24  type Statistic struct {
    25  	Counter struct {
    26  		User, Org, PublicKey,
    27  		Repo, Watch, Star, Access,
    28  		Issue, IssueClosed, IssueOpen,
    29  		Comment, Oauth, Follow,
    30  		Mirror, Release, AuthSource, Webhook,
    31  		Milestone, Label, HookTask,
    32  		Team, UpdateTask, Project,
    33  		ProjectBoard, Attachment,
    34  		Branches, Tags, CommitStatus int64
    35  		IssueByLabel      []IssueByLabelCount
    36  		IssueByRepository []IssueByRepositoryCount
    37  	}
    38  }
    39  
    40  // IssueByLabelCount contains the number of issue group by label
    41  type IssueByLabelCount struct {
    42  	Count int64
    43  	Label string
    44  }
    45  
    46  // IssueByRepositoryCount contains the number of issue group by repository
    47  type IssueByRepositoryCount struct {
    48  	Count      int64
    49  	OwnerName  string
    50  	Repository string
    51  }
    52  
    53  // GetStatistic returns the database statistics
    54  func GetStatistic(ctx context.Context) (stats Statistic) {
    55  	e := db.GetEngine(ctx)
    56  	stats.Counter.User = user_model.CountUsers(ctx, nil)
    57  	stats.Counter.Org, _ = db.Count[organization.Organization](ctx, organization.FindOrgOptions{IncludePrivate: true})
    58  	stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
    59  	stats.Counter.Repo, _ = repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{})
    60  	stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
    61  	stats.Counter.Star, _ = e.Count(new(repo_model.Star))
    62  	stats.Counter.Access, _ = e.Count(new(access_model.Access))
    63  	stats.Counter.Branches, _ = e.Count(new(git_model.Branch))
    64  	stats.Counter.Tags, _ = e.Where("is_draft=?", false).Count(new(repo_model.Release))
    65  	stats.Counter.CommitStatus, _ = e.Count(new(git_model.CommitStatus))
    66  
    67  	type IssueCount struct {
    68  		Count    int64
    69  		IsClosed bool
    70  	}
    71  
    72  	if setting.Metrics.EnabledIssueByLabel {
    73  		stats.Counter.IssueByLabel = []IssueByLabelCount{}
    74  
    75  		_ = e.Select("COUNT(*) AS count, l.name AS label").
    76  			Join("LEFT", "label l", "l.id=il.label_id").
    77  			Table("issue_label il").
    78  			GroupBy("l.name").
    79  			Find(&stats.Counter.IssueByLabel)
    80  	}
    81  
    82  	if setting.Metrics.EnabledIssueByRepository {
    83  		stats.Counter.IssueByRepository = []IssueByRepositoryCount{}
    84  
    85  		_ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository").
    86  			Join("LEFT", "repository r", "r.id=i.repo_id").
    87  			Table("issue i").
    88  			GroupBy("r.owner_name, r.name").
    89  			Find(&stats.Counter.IssueByRepository)
    90  	}
    91  
    92  	var issueCounts []IssueCount
    93  
    94  	_ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
    95  	for _, c := range issueCounts {
    96  		if c.IsClosed {
    97  			stats.Counter.IssueClosed = c.Count
    98  		} else {
    99  			stats.Counter.IssueOpen = c.Count
   100  		}
   101  	}
   102  
   103  	stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen
   104  
   105  	stats.Counter.Comment, _ = e.Count(new(issues_model.Comment))
   106  	stats.Counter.Oauth = 0
   107  	stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
   108  	stats.Counter.Mirror, _ = e.Count(new(repo_model.Mirror))
   109  	stats.Counter.Release, _ = e.Count(new(repo_model.Release))
   110  	stats.Counter.AuthSource, _ = db.Count[auth.Source](ctx, auth.FindSourcesOptions{})
   111  	stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
   112  	stats.Counter.Milestone, _ = e.Count(new(issues_model.Milestone))
   113  	stats.Counter.Label, _ = e.Count(new(issues_model.Label))
   114  	stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
   115  	stats.Counter.Team, _ = e.Count(new(organization.Team))
   116  	stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
   117  	stats.Counter.Project, _ = e.Count(new(project_model.Project))
   118  	stats.Counter.ProjectBoard, _ = e.Count(new(project_model.Board))
   119  	return stats
   120  }