Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 5dd402b

Browse files
author
Noah Hanjun Lee
authored
Add new metrics for repository (#183)
* Fix the commit count metric * Add a new index for repo * Add new metrics for active repositories count * Add docs for metrics
1 parent d537567 commit 5dd402b

File tree

8 files changed

+117
-17
lines changed

8 files changed

+117
-17
lines changed

docs/concepts/metrics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ Gitploy provides the following Gitploy metrics. *Note that Some metrics are prov
4141
* **gitploy_total_commit_count**<br/> The total commit count of production deployments.
4242
* **gitploy_member_count**<br/> The total count of members.
4343
* **gitploy_member_limit**<br/> The limit count of members.
44+
* **gitploy_total_active_repo_count**<br/> The count of active repositories.
45+
* **gitploy_total_repo_count**<br/> The count of repositories.

ent/migrate/schema.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ent/schema/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,7 @@ func (Repo) Indexes() []ent.Index {
6969
index.Fields("namespace", "name").
7070
Unique(),
7171
index.Fields("name"),
72+
// Count active repositories for metrics.
73+
index.Fields("active"),
7274
}
7375
}

internal/interactor/interface.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
type (
1616
Store interface {
17+
CountUsers(context.Context) (int, error)
1718
ListUsers(ctx context.Context, login string, page, perPage int) ([]*ent.User, error)
1819
FindUserByID(ctx context.Context, id int64) (*ent.User, error)
1920
FindUserByHash(ctx context.Context, hash string) (*ent.User, error)
2021
FindUserByLogin(ctx context.Context, login string) (*ent.User, error)
21-
CountUsers(context.Context) (int, error)
2222
CreateUser(ctx context.Context, u *ent.User) (*ent.User, error)
2323
UpdateUser(ctx context.Context, u *ent.User) (*ent.User, error)
2424
DeleteUser(ctx context.Context, u *ent.User) error
@@ -28,6 +28,8 @@ type (
2828
UpdateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
2929
DeleteChatUser(ctx context.Context, cu *ent.ChatUser) error
3030

31+
CountActiveRepos(ctx context.Context) (int, error)
32+
CountRepos(ctx context.Context) (int, error)
3133
ListReposOfUser(ctx context.Context, u *ent.User, q, namespace, name string, sorted bool, page, perPage int) ([]*ent.Repo, error)
3234
FindRepoOfUserByID(ctx context.Context, u *ent.User, id int64) (*ent.Repo, error)
3335
FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, namespace, name string) (*ent.Repo, error)

internal/interactor/mock/pkg.go

Lines changed: 45 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/store/repo.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import (
1111
"github.com/gitploy-io/gitploy/vo"
1212
)
1313

14+
func (s *Store) CountActiveRepos(ctx context.Context) (int, error) {
15+
return s.c.Repo.
16+
Query().
17+
Where(repo.ActiveEQ(true)).
18+
Count(ctx)
19+
}
20+
21+
func (s *Store) CountRepos(ctx context.Context) (int, error) {
22+
return s.c.Repo.
23+
Query().
24+
Count(ctx)
25+
}
26+
1427
func (s *Store) ListReposOfUser(ctx context.Context, u *ent.User, q, namespace, name string, sorted bool, page, perPage int) ([]*ent.Repo, error) {
1528
// Build the query with parameters.
1629
qry := s.c.Repo.

internal/server/metrics/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
type (
1212
Interactor interface {
13+
CountActiveRepos(ctx context.Context) (int, error)
14+
CountRepos(ctx context.Context) (int, error)
1315
ListAllDeploymentStatistics(ctx context.Context) ([]*ent.DeploymentStatistics, error)
1416
ListDeploymentStatisticsGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
1517
GetLicense(ctx context.Context) (*vo.License, error)

internal/server/metrics/metrics.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
221221
nil,
222222
),
223223
prometheus.GaugeValue,
224-
float64(dc.LeadTimeSeconds),
224+
float64(dc.CommitCount),
225225
dc.Edges.Repo.Namespace, dc.Edges.Repo.Name, dc.Env,
226226
)
227227
}
@@ -267,4 +267,48 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
267267
string(lic.Kind),
268268
)
269269
}
270+
271+
{
272+
acnt, err := c.i.CountActiveRepos(ctx)
273+
if err != nil {
274+
c.log.Error("It has failed to count active repositories.", zap.Error(err))
275+
return
276+
}
277+
278+
cnt, err := c.i.CountRepos(ctx)
279+
if err != nil {
280+
c.log.Error("It has failed to count total repositories.", zap.Error(err))
281+
return
282+
}
283+
284+
ch <- prometheus.MustNewConstMetric(
285+
prometheus.NewDesc(
286+
prometheus.BuildFQName(
287+
namespace,
288+
"",
289+
"total_active_repo_count",
290+
),
291+
"The count of active repositories.",
292+
nil,
293+
nil,
294+
),
295+
prometheus.GaugeValue,
296+
float64(acnt),
297+
)
298+
299+
ch <- prometheus.MustNewConstMetric(
300+
prometheus.NewDesc(
301+
prometheus.BuildFQName(
302+
namespace,
303+
"",
304+
"total_repo_count",
305+
),
306+
"The count of repositories.",
307+
nil,
308+
nil,
309+
),
310+
prometheus.GaugeValue,
311+
float64(cnt),
312+
)
313+
}
270314
}

0 commit comments

Comments
 (0)