feat: [CDE-263]: Changes to track Gitspaces usage across installations. (#2584)

* feat: [CDE-263]: Changes to track Gitspaces usage across installations.
This commit is contained in:
Dhruv Dhruv 2024-08-26 08:24:06 +00:00 committed by Harness
parent 2595b5dba5
commit 01e26bdb82
5 changed files with 46 additions and 26 deletions

View File

@ -40,18 +40,20 @@ type metricData struct {
Repos int64 `json:"repo_count"` Repos int64 `json:"repo_count"`
Pipelines int64 `json:"pipeline_count"` Pipelines int64 `json:"pipeline_count"`
Executions int64 `json:"execution_count"` Executions int64 `json:"execution_count"`
Gitspaces int64 `json:"gitspace_count"`
} }
type Collector struct { type Collector struct {
hostname string hostname string
enabled bool enabled bool
endpoint string endpoint string
token string token string
userStore store.PrincipalStore userStore store.PrincipalStore
repoStore store.RepoStore repoStore store.RepoStore
pipelineStore store.PipelineStore pipelineStore store.PipelineStore
executionStore store.ExecutionStore executionStore store.ExecutionStore
scheduler *job.Scheduler scheduler *job.Scheduler
gitspaceConfigStore store.GitspaceConfigStore
} }
func (c *Collector) Register(ctx context.Context) error { func (c *Collector) Register(ctx context.Context) error {
@ -107,6 +109,12 @@ func (c *Collector) Handle(ctx context.Context, _ string, _ job.ProgressReporter
return "", fmt.Errorf("failed to get executions total count: %w", err) return "", fmt.Errorf("failed to get executions total count: %w", err)
} }
// total gitspaces (configs) in the system
totalGitspaces, err := c.gitspaceConfigStore.Count(ctx, &types.GitspaceFilter{IncludeDeleted: true})
if err != nil {
return "", fmt.Errorf("failed to get gitspace total count: %w", err)
}
data := metricData{ data := metricData{
Hostname: c.hostname, Hostname: c.hostname,
Installer: users[0].Email, Installer: users[0].Email,
@ -116,6 +124,7 @@ func (c *Collector) Handle(ctx context.Context, _ string, _ job.ProgressReporter
Repos: totalRepos, Repos: totalRepos,
Pipelines: totalPipelines, Pipelines: totalPipelines,
Executions: totalExecutions, Executions: totalExecutions,
Gitspaces: totalGitspaces,
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)

View File

@ -34,17 +34,19 @@ func ProvideCollector(
executionStore store.ExecutionStore, executionStore store.ExecutionStore,
scheduler *job.Scheduler, scheduler *job.Scheduler,
executor *job.Executor, executor *job.Executor,
gitspaceConfigStore store.GitspaceConfigStore,
) (*Collector, error) { ) (*Collector, error) {
job := &Collector{ job := &Collector{
hostname: config.InstanceID, hostname: config.InstanceID,
enabled: config.Metric.Enabled, enabled: config.Metric.Enabled,
endpoint: config.Metric.Endpoint, endpoint: config.Metric.Endpoint,
token: config.Metric.Token, token: config.Metric.Token,
userStore: userStore, userStore: userStore,
repoStore: repoStore, repoStore: repoStore,
pipelineStore: pipelineStore, pipelineStore: pipelineStore,
executionStore: executionStore, executionStore: executionStore,
scheduler: scheduler, scheduler: scheduler,
gitspaceConfigStore: gitspaceConfigStore,
} }
err := executor.Register(jobType, job) err := executor.Register(jobType, job)

View File

@ -100,10 +100,18 @@ func (s gitspaceConfigStore) Count(ctx context.Context, filter *types.GitspaceFi
db := dbtx.GetAccessor(ctx, s.db) db := dbtx.GetAccessor(ctx, s.db)
countStmt := database.Builder. countStmt := database.Builder.
Select("COUNT(*)"). Select("COUNT(*)").
From(gitspaceConfigsTable). From(gitspaceConfigsTable)
Where(squirrel.Eq{"gconf_is_deleted": false}).
Where(squirrel.Eq{"gconf_user_uid": filter.UserID}). if !filter.IncludeDeleted {
Where(squirrel.Eq{"gconf_space_id": filter.SpaceIDs}) countStmt = countStmt.Where(squirrel.Eq{"gconf_is_deleted": false})
}
if filter.UserID != "" {
countStmt = countStmt.Where(squirrel.Eq{"gconf_user_uid": filter.UserID})
}
if filter.SpaceIDs != nil {
countStmt = countStmt.Where(squirrel.Eq{"gconf_space_id": filter.SpaceIDs})
}
sql, args, err := countStmt.ToSql() sql, args, err := countStmt.ToSql()
if err != nil { if err != nil {
return 0, errors.Wrap(err, "Failed to convert squirrel builder to sql") return 0, errors.Wrap(err, "Failed to convert squirrel builder to sql")

View File

@ -448,7 +448,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
collector, err := metric.ProvideCollector(config, principalStore, repoStore, pipelineStore, executionStore, jobScheduler, executor) collector, err := metric.ProvideCollector(config, principalStore, repoStore, pipelineStore, executionStore, jobScheduler, executor, gitspaceConfigStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -77,7 +77,8 @@ type GitspaceInstance struct {
} }
type GitspaceFilter struct { type GitspaceFilter struct {
QueryFilter ListQueryFilter QueryFilter ListQueryFilter
UserID string UserID string
SpaceIDs []int64 SpaceIDs []int64
IncludeDeleted bool
} }