From efcfd925171d6d61dbc3b12a019e1467ff919a5c Mon Sep 17 00:00:00 2001 From: Dhruv Dhruv Date: Thu, 26 Sep 2024 10:35:42 +0000 Subject: [PATCH] feat: [CDE-350]: Adding columns for active time start and end in gitspace table. (#2744) * feat: [CDE-350]: Adding columns for active time start and end in gitspace table. --- app/api/controller/gitspace/action.go | 8 +- app/api/controller/gitspace/delete.go | 5 ++ .../orchestrator/orchestrator_impl.go | 5 +- app/store/database/gitspace_instance.go | 73 +++++++++++-------- ...8_alter_gitspaces_add_active_time.down.sql | 3 + ...078_alter_gitspaces_add_active_time.up.sql | 21 ++++++ ...8_alter_gitspaces_add_active_time.down.sql | 3 + ...078_alter_gitspaces_add_active_time.up.sql | 21 ++++++ types/gitspace.go | 40 +++++----- 9 files changed, 126 insertions(+), 53 deletions(-) create mode 100644 app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.down.sql create mode 100644 app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.up.sql create mode 100644 app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.down.sql create mode 100644 app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.up.sql diff --git a/app/api/controller/gitspace/action.go b/app/api/controller/gitspace/action.go index d5c25a389..030c3d772 100644 --- a/app/api/controller/gitspace/action.go +++ b/app/api/controller/gitspace/action.go @@ -94,7 +94,7 @@ func (c *Controller) Action( return gitspaceConfig, err case enum.GitspaceActionTypeStop: c.emitGitspaceConfigEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStop) - err = c.stopGitspaceAction(ctx, gitspaceConfig) + err = c.stopGitspaceAction(ctx, gitspaceConfig, time.Now()) return gitspaceConfig, err default: return nil, fmt.Errorf("unknown action %s on gitspace : %s", string(in.Action), gitspaceConfig.Identifier) @@ -265,6 +265,7 @@ func (c *Controller) gitspaceBusyOperation( func (c *Controller) stopGitspaceAction( ctx context.Context, config *types.GitspaceConfig, + now time.Time, ) error { savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID) if err != nil { @@ -279,6 +280,11 @@ func (c *Controller) stopGitspaceAction( if err != nil { return err } + + activeTimeEnded := now.UnixMilli() + config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded + config.GitspaceInstance.TotalTimeUsed = + *(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted) config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping if err = c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance); err != nil { return fmt.Errorf("failed to update gitspace config for stopping %s %w", config.Identifier, err) diff --git a/app/api/controller/gitspace/delete.go b/app/api/controller/gitspace/delete.go index af40100b0..ceb58376b 100644 --- a/app/api/controller/gitspace/delete.go +++ b/app/api/controller/gitspace/delete.go @@ -17,6 +17,7 @@ package gitspace import ( "context" "fmt" + "time" apiauth "github.com/harness/gitness/app/api/auth" "github.com/harness/gitness/app/auth" @@ -65,6 +66,10 @@ func (c *Controller) Delete( } func (c *Controller) stopRunningGitspace(ctx context.Context, config types.GitspaceConfig) { + activeTimeEnded := time.Now().UnixMilli() + config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded + config.GitspaceInstance.TotalTimeUsed = + *(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted) config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping err := c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance) if err != nil { diff --git a/app/gitspace/orchestrator/orchestrator_impl.go b/app/gitspace/orchestrator/orchestrator_impl.go index 763c30f98..a4d9349c2 100644 --- a/app/gitspace/orchestrator/orchestrator_impl.go +++ b/app/gitspace/orchestrator/orchestrator_impl.go @@ -400,8 +400,9 @@ func (o orchestrator) ResumeStartGitspace( ideURLString := ideURL.String() gitspaceInstance.URL = &ideURLString - lastUsed := time.Now().UnixMilli() - gitspaceInstance.LastUsed = &lastUsed + now := time.Now().UnixMilli() + gitspaceInstance.LastUsed = &now + gitspaceInstance.ActiveTimeStarted = &now gitspaceInstance.State = enum.GitspaceInstanceStateRunning o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStartCompleted) diff --git a/app/store/database/gitspace_instance.go b/app/store/database/gitspace_instance.go index 0c6520355..4193b718f 100644 --- a/app/store/database/gitspace_instance.go +++ b/app/store/database/gitspace_instance.go @@ -50,7 +50,9 @@ const ( gits_machine_user, gits_uid, gits_access_key_ref, - gits_last_heartbeat` + gits_last_heartbeat, + gits_active_time_started, + gits_active_time_ended` gitspaceInstanceSelectColumns = "gits_id," + gitspaceInstanceInsertColumns gitspaceInstanceTable = `gitspaces` ) @@ -61,19 +63,21 @@ type gitspaceInstance struct { URL null.String `db:"gits_url"` State enum.GitspaceInstanceStateType `db:"gits_state"` // TODO: migrate to principal int64 id to use principal cache and consistent with Harness code. - UserUID string `db:"gits_user_uid"` - ResourceUsage null.String `db:"gits_resource_usage"` - SpaceID int64 `db:"gits_space_id"` - LastUsed null.Int `db:"gits_last_used"` - TotalTimeUsed int64 `db:"gits_total_time_used"` - TrackedChanges null.String `db:"gits_tracked_changes"` - AccessType enum.GitspaceAccessType `db:"gits_access_type"` - AccessKeyRef null.String `db:"gits_access_key_ref"` - MachineUser null.String `db:"gits_machine_user"` - Identifier string `db:"gits_uid"` - Created int64 `db:"gits_created"` - Updated int64 `db:"gits_updated"` - LastHeartbeat null.Int `db:"gits_last_heartbeat"` + UserUID string `db:"gits_user_uid"` + ResourceUsage null.String `db:"gits_resource_usage"` + SpaceID int64 `db:"gits_space_id"` + LastUsed null.Int `db:"gits_last_used"` + TotalTimeUsed int64 `db:"gits_total_time_used"` + TrackedChanges null.String `db:"gits_tracked_changes"` + AccessType enum.GitspaceAccessType `db:"gits_access_type"` + AccessKeyRef null.String `db:"gits_access_key_ref"` + MachineUser null.String `db:"gits_machine_user"` + Identifier string `db:"gits_uid"` + Created int64 `db:"gits_created"` + Updated int64 `db:"gits_updated"` + LastHeartbeat null.Int `db:"gits_last_heartbeat"` + ActiveTimeStarted null.Int `db:"gits_active_time_started"` + ActiveTimeEnded null.Int `db:"gits_active_time_ended"` } // NewGitspaceInstanceStore returns a new GitspaceInstanceStore. @@ -147,6 +151,8 @@ func (g gitspaceInstanceStore) Create(ctx context.Context, gitspaceInstance *typ gitspaceInstance.Identifier, gitspaceInstance.AccessKeyRef, gitspaceInstance.LastHeartbeat, + gitspaceInstance.ActiveTimeStarted, + gitspaceInstance.ActiveTimeEnded, ). Suffix(ReturningClause + "gits_id") sql, args, err := stmt.ToSql() @@ -171,6 +177,9 @@ func (g gitspaceInstanceStore) Update( Set("gits_last_used", gitspaceInstance.LastUsed). Set("gits_last_heartbeat", gitspaceInstance.LastHeartbeat). Set("gits_url", gitspaceInstance.URL). + Set("gits_active_time_started", gitspaceInstance.ActiveTimeStarted). + Set("gits_active_time_ended", gitspaceInstance.ActiveTimeEnded). + Set("gits_total_time_used", gitspaceInstance.TotalTimeUsed). Set("gits_updated", gitspaceInstance.Updated). Where("gits_id = ?", gitspaceInstance.ID) sql, args, err := stmt.ToSql() @@ -269,23 +278,25 @@ func (g gitspaceInstanceStore) mapToGitspaceInstance( in *gitspaceInstance, ) (*types.GitspaceInstance, error) { var res = &types.GitspaceInstance{ - ID: in.ID, - Identifier: in.Identifier, - GitSpaceConfigID: in.GitSpaceConfigID, - URL: in.URL.Ptr(), - State: in.State, - UserID: in.UserUID, - ResourceUsage: in.ResourceUsage.Ptr(), - LastUsed: in.LastUsed.Ptr(), - TotalTimeUsed: in.TotalTimeUsed, - TrackedChanges: in.TrackedChanges.Ptr(), - AccessType: in.AccessType, - AccessKeyRef: in.AccessKeyRef.Ptr(), - MachineUser: in.MachineUser.Ptr(), - SpaceID: in.SpaceID, - Created: in.Created, - Updated: in.Updated, - LastHeartbeat: in.LastHeartbeat.Ptr(), + ID: in.ID, + Identifier: in.Identifier, + GitSpaceConfigID: in.GitSpaceConfigID, + URL: in.URL.Ptr(), + State: in.State, + UserID: in.UserUID, + ResourceUsage: in.ResourceUsage.Ptr(), + LastUsed: in.LastUsed.Ptr(), + TotalTimeUsed: in.TotalTimeUsed, + TrackedChanges: in.TrackedChanges.Ptr(), + AccessType: in.AccessType, + AccessKeyRef: in.AccessKeyRef.Ptr(), + MachineUser: in.MachineUser.Ptr(), + SpaceID: in.SpaceID, + Created: in.Created, + Updated: in.Updated, + LastHeartbeat: in.LastHeartbeat.Ptr(), + ActiveTimeEnded: in.ActiveTimeEnded.Ptr(), + ActiveTimeStarted: in.ActiveTimeStarted.Ptr(), } return res, nil } diff --git a/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.down.sql b/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.down.sql new file mode 100644 index 000000000..4cc2e4d0d --- /dev/null +++ b/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE gitspaces DROP COLUMN gits_active_time_started; +ALTER TABLE gitspaces DROP COLUMN gits_active_time_ended; +UPDATE gitspaces SET gits_total_time_used = 0; \ No newline at end of file diff --git a/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.up.sql b/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.up.sql new file mode 100644 index 000000000..efbbb7bc9 --- /dev/null +++ b/app/store/database/migrate/postgres/0078_alter_gitspaces_add_active_time.up.sql @@ -0,0 +1,21 @@ +ALTER TABLE gitspaces ADD COLUMN gits_active_time_started BIGINT; +ALTER TABLE gitspaces ADD COLUMN gits_active_time_ended BIGINT; + +UPDATE gitspaces +SET gits_active_time_started = + CASE + WHEN gits_state = 'uninitialized' THEN NULL + ELSE gits_created + END; + +UPDATE gitspaces +SET gits_active_time_ended = + CASE + WHEN gits_state IN ('running', 'starting', 'uninitialized') THEN NULL + ELSE gits_updated + END; + +UPDATE gitspaces +SET gits_total_time_used = gits_active_time_ended - gits_active_time_started +WHERE gits_active_time_ended IS NOT NULL + AND gits_active_time_started IS NOT NULL; diff --git a/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.down.sql b/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.down.sql new file mode 100644 index 000000000..4cc2e4d0d --- /dev/null +++ b/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE gitspaces DROP COLUMN gits_active_time_started; +ALTER TABLE gitspaces DROP COLUMN gits_active_time_ended; +UPDATE gitspaces SET gits_total_time_used = 0; \ No newline at end of file diff --git a/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.up.sql b/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.up.sql new file mode 100644 index 000000000..efbbb7bc9 --- /dev/null +++ b/app/store/database/migrate/sqlite/0078_alter_gitspaces_add_active_time.up.sql @@ -0,0 +1,21 @@ +ALTER TABLE gitspaces ADD COLUMN gits_active_time_started BIGINT; +ALTER TABLE gitspaces ADD COLUMN gits_active_time_ended BIGINT; + +UPDATE gitspaces +SET gits_active_time_started = + CASE + WHEN gits_state = 'uninitialized' THEN NULL + ELSE gits_created + END; + +UPDATE gitspaces +SET gits_active_time_ended = + CASE + WHEN gits_state IN ('running', 'starting', 'uninitialized') THEN NULL + ELSE gits_updated + END; + +UPDATE gitspaces +SET gits_total_time_used = gits_active_time_ended - gits_active_time_started +WHERE gits_active_time_ended IS NOT NULL + AND gits_active_time_started IS NOT NULL; diff --git a/types/gitspace.go b/types/gitspace.go index 7e5dad27c..1bf0abc9e 100644 --- a/types/gitspace.go +++ b/types/gitspace.go @@ -55,25 +55,27 @@ type GitspaceUser struct { } type GitspaceInstance struct { - ID int64 `json:"-"` - GitSpaceConfigID int64 `json:"-"` - Identifier string `json:"identifier"` - URL *string `json:"url,omitempty"` - State enum.GitspaceInstanceStateType `json:"state"` - UserID string `json:"-"` - ResourceUsage *string `json:"resource_usage"` - LastUsed *int64 `json:"last_used,omitempty"` - TotalTimeUsed int64 `json:"total_time_used"` - TrackedChanges *string `json:"tracked_changes"` - AccessKey *string `json:"access_key,omitempty"` - AccessType enum.GitspaceAccessType `json:"access_type"` - AccessKeyRef *string `json:"access_key_ref"` - MachineUser *string `json:"machine_user,omitempty"` - SpacePath string `json:"space_path"` - SpaceID int64 `json:"-"` - Created int64 `json:"created"` - Updated int64 `json:"updated"` - LastHeartbeat *int64 `json:"last_heartbeat,omitempty"` + ID int64 `json:"-"` + GitSpaceConfigID int64 `json:"-"` + Identifier string `json:"identifier"` + URL *string `json:"url,omitempty"` + State enum.GitspaceInstanceStateType `json:"state"` + UserID string `json:"-"` + ResourceUsage *string `json:"resource_usage"` + LastUsed *int64 `json:"last_used,omitempty"` + TotalTimeUsed int64 `json:"total_time_used"` + TrackedChanges *string `json:"tracked_changes"` + AccessKey *string `json:"access_key,omitempty"` + AccessType enum.GitspaceAccessType `json:"access_type"` + AccessKeyRef *string `json:"access_key_ref"` + MachineUser *string `json:"machine_user,omitempty"` + SpacePath string `json:"space_path"` + SpaceID int64 `json:"-"` + Created int64 `json:"created"` + Updated int64 `json:"updated"` + LastHeartbeat *int64 `json:"last_heartbeat,omitempty"` + ActiveTimeStarted *int64 `json:"active_time_started,omitempty"` + ActiveTimeEnded *int64 `json:"active_time_ended,omitempty"` } type GitspaceFilter struct {