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