mirror of https://github.com/harness/drone.git
feat: [CDE-263]: Changes to track Gitspaces usage across installations. (#2584)
* feat: [CDE-263]: Changes to track Gitspaces usage across installations.pull/3545/head
parent
2595b5dba5
commit
01e26bdb82
|
@ -40,18 +40,20 @@ type metricData struct {
|
|||
Repos int64 `json:"repo_count"`
|
||||
Pipelines int64 `json:"pipeline_count"`
|
||||
Executions int64 `json:"execution_count"`
|
||||
Gitspaces int64 `json:"gitspace_count"`
|
||||
}
|
||||
|
||||
type Collector struct {
|
||||
hostname string
|
||||
enabled bool
|
||||
endpoint string
|
||||
token string
|
||||
userStore store.PrincipalStore
|
||||
repoStore store.RepoStore
|
||||
pipelineStore store.PipelineStore
|
||||
executionStore store.ExecutionStore
|
||||
scheduler *job.Scheduler
|
||||
hostname string
|
||||
enabled bool
|
||||
endpoint string
|
||||
token string
|
||||
userStore store.PrincipalStore
|
||||
repoStore store.RepoStore
|
||||
pipelineStore store.PipelineStore
|
||||
executionStore store.ExecutionStore
|
||||
scheduler *job.Scheduler
|
||||
gitspaceConfigStore store.GitspaceConfigStore
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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{
|
||||
Hostname: c.hostname,
|
||||
Installer: users[0].Email,
|
||||
|
@ -116,6 +124,7 @@ func (c *Collector) Handle(ctx context.Context, _ string, _ job.ProgressReporter
|
|||
Repos: totalRepos,
|
||||
Pipelines: totalPipelines,
|
||||
Executions: totalExecutions,
|
||||
Gitspaces: totalGitspaces,
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
|
|
@ -34,17 +34,19 @@ func ProvideCollector(
|
|||
executionStore store.ExecutionStore,
|
||||
scheduler *job.Scheduler,
|
||||
executor *job.Executor,
|
||||
gitspaceConfigStore store.GitspaceConfigStore,
|
||||
) (*Collector, error) {
|
||||
job := &Collector{
|
||||
hostname: config.InstanceID,
|
||||
enabled: config.Metric.Enabled,
|
||||
endpoint: config.Metric.Endpoint,
|
||||
token: config.Metric.Token,
|
||||
userStore: userStore,
|
||||
repoStore: repoStore,
|
||||
pipelineStore: pipelineStore,
|
||||
executionStore: executionStore,
|
||||
scheduler: scheduler,
|
||||
hostname: config.InstanceID,
|
||||
enabled: config.Metric.Enabled,
|
||||
endpoint: config.Metric.Endpoint,
|
||||
token: config.Metric.Token,
|
||||
userStore: userStore,
|
||||
repoStore: repoStore,
|
||||
pipelineStore: pipelineStore,
|
||||
executionStore: executionStore,
|
||||
scheduler: scheduler,
|
||||
gitspaceConfigStore: gitspaceConfigStore,
|
||||
}
|
||||
|
||||
err := executor.Register(jobType, job)
|
||||
|
|
|
@ -100,10 +100,18 @@ func (s gitspaceConfigStore) Count(ctx context.Context, filter *types.GitspaceFi
|
|||
db := dbtx.GetAccessor(ctx, s.db)
|
||||
countStmt := database.Builder.
|
||||
Select("COUNT(*)").
|
||||
From(gitspaceConfigsTable).
|
||||
Where(squirrel.Eq{"gconf_is_deleted": false}).
|
||||
Where(squirrel.Eq{"gconf_user_uid": filter.UserID}).
|
||||
Where(squirrel.Eq{"gconf_space_id": filter.SpaceIDs})
|
||||
From(gitspaceConfigsTable)
|
||||
|
||||
if !filter.IncludeDeleted {
|
||||
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()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "Failed to convert squirrel builder to sql")
|
||||
|
|
|
@ -448,7 +448,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
|
|||
if err != nil {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -77,7 +77,8 @@ type GitspaceInstance struct {
|
|||
}
|
||||
|
||||
type GitspaceFilter struct {
|
||||
QueryFilter ListQueryFilter
|
||||
UserID string
|
||||
SpaceIDs []int64
|
||||
QueryFilter ListQueryFilter
|
||||
UserID string
|
||||
SpaceIDs []int64
|
||||
IncludeDeleted bool
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue