mirror of https://github.com/harness/drone.git
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.pull/3566/head
parent
05b4a99061
commit
efcfd92517
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue