1// Copyright 2017 The go-github AUTHORS. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package github
7
8import (
9	"context"
10	"fmt"
11)
12
13// AdminStats represents a variety of stats of a Github Enterprise
14// installation.
15type AdminStats struct {
16	Issues     *IssueStats     `json:"issues,omitempty"`
17	Hooks      *HookStats      `json:"hooks,omitempty"`
18	Milestones *MilestoneStats `json:"milestones,omitempty"`
19	Orgs       *OrgStats       `json:"orgs,omitempty"`
20	Comments   *CommentStats   `json:"comments,omitempty"`
21	Pages      *PageStats      `json:"pages,omitempty"`
22	Users      *UserStats      `json:"users,omitempty"`
23	Gists      *GistStats      `json:"gists,omitempty"`
24	Pulls      *PullStats      `json:"pulls,omitempty"`
25	Repos      *RepoStats      `json:"repos,omitempty"`
26}
27
28func (s AdminStats) String() string {
29	return Stringify(s)
30}
31
32// IssueStats represents the number of total, open and closed issues.
33type IssueStats struct {
34	TotalIssues  *int `json:"total_issues,omitempty"`
35	OpenIssues   *int `json:"open_issues,omitempty"`
36	ClosedIssues *int `json:"closed_issues,omitempty"`
37}
38
39func (s IssueStats) String() string {
40	return Stringify(s)
41}
42
43// HookStats represents the number of total, active and inactive hooks.
44type HookStats struct {
45	TotalHooks    *int `json:"total_hooks,omitempty"`
46	ActiveHooks   *int `json:"active_hooks,omitempty"`
47	InactiveHooks *int `json:"inactive_hooks,omitempty"`
48}
49
50func (s HookStats) String() string {
51	return Stringify(s)
52}
53
54// MilestoneStats represents the number of total, open and close milestones.
55type MilestoneStats struct {
56	TotalMilestones  *int `json:"total_milestones,omitempty"`
57	OpenMilestones   *int `json:"open_milestones,omitempty"`
58	ClosedMilestones *int `json:"closed_milestones,omitempty"`
59}
60
61func (s MilestoneStats) String() string {
62	return Stringify(s)
63}
64
65// OrgStats represents the number of total, disabled organizations and the team
66// and team member count.
67type OrgStats struct {
68	TotalOrgs        *int `json:"total_orgs,omitempty"`
69	DisabledOrgs     *int `json:"disabled_orgs,omitempty"`
70	TotalTeams       *int `json:"total_teams,omitempty"`
71	TotalTeamMembers *int `json:"total_team_members,omitempty"`
72}
73
74func (s OrgStats) String() string {
75	return Stringify(s)
76}
77
78// CommentStats represents the number of total comments on commits, gists, issues
79// and pull requests.
80type CommentStats struct {
81	TotalCommitComments      *int `json:"total_commit_comments,omitempty"`
82	TotalGistComments        *int `json:"total_gist_comments,omitempty"`
83	TotalIssueComments       *int `json:"total_issue_comments,omitempty"`
84	TotalPullRequestComments *int `json:"total_pull_request_comments,omitempty"`
85}
86
87func (s CommentStats) String() string {
88	return Stringify(s)
89}
90
91// PageStats represents the total number of github pages.
92type PageStats struct {
93	TotalPages *int `json:"total_pages,omitempty"`
94}
95
96func (s PageStats) String() string {
97	return Stringify(s)
98}
99
100// UserStats represents the number of total, admin and suspended users.
101type UserStats struct {
102	TotalUsers     *int `json:"total_users,omitempty"`
103	AdminUsers     *int `json:"admin_users,omitempty"`
104	SuspendedUsers *int `json:"suspended_users,omitempty"`
105}
106
107func (s UserStats) String() string {
108	return Stringify(s)
109}
110
111// GistStats represents the number of total, private and public gists.
112type GistStats struct {
113	TotalGists   *int `json:"total_gists,omitempty"`
114	PrivateGists *int `json:"private_gists,omitempty"`
115	PublicGists  *int `json:"public_gists,omitempty"`
116}
117
118func (s GistStats) String() string {
119	return Stringify(s)
120}
121
122// PullStats represents the number of total, merged, mergable and unmergeable
123// pull-requests.
124type PullStats struct {
125	TotalPulls      *int `json:"total_pulls,omitempty"`
126	MergedPulls     *int `json:"merged_pulls,omitempty"`
127	MergablePulls   *int `json:"mergeable_pulls,omitempty"`
128	UnmergablePulls *int `json:"unmergeable_pulls,omitempty"`
129}
130
131func (s PullStats) String() string {
132	return Stringify(s)
133}
134
135// RepoStats represents the number of total, root, fork, organization repositories
136// together with the total number of pushes and wikis.
137type RepoStats struct {
138	TotalRepos  *int `json:"total_repos,omitempty"`
139	RootRepos   *int `json:"root_repos,omitempty"`
140	ForkRepos   *int `json:"fork_repos,omitempty"`
141	OrgRepos    *int `json:"org_repos,omitempty"`
142	TotalPushes *int `json:"total_pushes,omitempty"`
143	TotalWikis  *int `json:"total_wikis,omitempty"`
144}
145
146func (s RepoStats) String() string {
147	return Stringify(s)
148}
149
150// GetAdminStats returns a variety of metrics about a Github Enterprise
151// installation.
152//
153// Please note that this is only available to site administrators,
154// otherwise it will error with a 404 not found (instead of 401 or 403).
155//
156// GitHub API docs: https://developer.github.com/v3/enterprise-admin/admin_stats/
157func (s *AdminService) GetAdminStats(ctx context.Context) (*AdminStats, *Response, error) {
158	u := fmt.Sprintf("enterprise/stats/all")
159	req, err := s.client.NewRequest("GET", u, nil)
160	if err != nil {
161		return nil, nil, err
162	}
163
164	m := new(AdminStats)
165	resp, err := s.client.Do(ctx, req, m)
166	if err != nil {
167		return nil, resp, err
168	}
169
170	return m, resp, nil
171}
172