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
|
return gitspaceConfig, err
|
||||||
case enum.GitspaceActionTypeStop:
|
case enum.GitspaceActionTypeStop:
|
||||||
c.emitGitspaceConfigEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStop)
|
c.emitGitspaceConfigEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStop)
|
||||||
err = c.stopGitspaceAction(ctx, gitspaceConfig)
|
err = c.stopGitspaceAction(ctx, gitspaceConfig, time.Now())
|
||||||
return gitspaceConfig, err
|
return gitspaceConfig, err
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown action %s on gitspace : %s", string(in.Action), gitspaceConfig.Identifier)
|
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(
|
func (c *Controller) stopGitspaceAction(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
config *types.GitspaceConfig,
|
config *types.GitspaceConfig,
|
||||||
|
now time.Time,
|
||||||
) error {
|
) error {
|
||||||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -279,6 +280,11 @@ func (c *Controller) stopGitspaceAction(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activeTimeEnded := now.UnixMilli()
|
||||||
|
config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded
|
||||||
|
config.GitspaceInstance.TotalTimeUsed =
|
||||||
|
*(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted)
|
||||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||||
if err = c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance); err != nil {
|
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)
|
return fmt.Errorf("failed to update gitspace config for stopping %s %w", config.Identifier, err)
|
||||||
|
|
|
@ -17,6 +17,7 @@ package gitspace
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
apiauth "github.com/harness/gitness/app/api/auth"
|
apiauth "github.com/harness/gitness/app/api/auth"
|
||||||
"github.com/harness/gitness/app/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) {
|
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
|
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||||
err := c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance)
|
err := c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -400,8 +400,9 @@ func (o orchestrator) ResumeStartGitspace(
|
||||||
ideURLString := ideURL.String()
|
ideURLString := ideURL.String()
|
||||||
gitspaceInstance.URL = &ideURLString
|
gitspaceInstance.URL = &ideURLString
|
||||||
|
|
||||||
lastUsed := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
gitspaceInstance.LastUsed = &lastUsed
|
gitspaceInstance.LastUsed = &now
|
||||||
|
gitspaceInstance.ActiveTimeStarted = &now
|
||||||
gitspaceInstance.State = enum.GitspaceInstanceStateRunning
|
gitspaceInstance.State = enum.GitspaceInstanceStateRunning
|
||||||
|
|
||||||
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStartCompleted)
|
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStartCompleted)
|
||||||
|
|
|
@ -50,7 +50,9 @@ const (
|
||||||
gits_machine_user,
|
gits_machine_user,
|
||||||
gits_uid,
|
gits_uid,
|
||||||
gits_access_key_ref,
|
gits_access_key_ref,
|
||||||
gits_last_heartbeat`
|
gits_last_heartbeat,
|
||||||
|
gits_active_time_started,
|
||||||
|
gits_active_time_ended`
|
||||||
gitspaceInstanceSelectColumns = "gits_id," + gitspaceInstanceInsertColumns
|
gitspaceInstanceSelectColumns = "gits_id," + gitspaceInstanceInsertColumns
|
||||||
gitspaceInstanceTable = `gitspaces`
|
gitspaceInstanceTable = `gitspaces`
|
||||||
)
|
)
|
||||||
|
@ -74,6 +76,8 @@ type gitspaceInstance struct {
|
||||||
Created int64 `db:"gits_created"`
|
Created int64 `db:"gits_created"`
|
||||||
Updated int64 `db:"gits_updated"`
|
Updated int64 `db:"gits_updated"`
|
||||||
LastHeartbeat null.Int `db:"gits_last_heartbeat"`
|
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.
|
// NewGitspaceInstanceStore returns a new GitspaceInstanceStore.
|
||||||
|
@ -147,6 +151,8 @@ func (g gitspaceInstanceStore) Create(ctx context.Context, gitspaceInstance *typ
|
||||||
gitspaceInstance.Identifier,
|
gitspaceInstance.Identifier,
|
||||||
gitspaceInstance.AccessKeyRef,
|
gitspaceInstance.AccessKeyRef,
|
||||||
gitspaceInstance.LastHeartbeat,
|
gitspaceInstance.LastHeartbeat,
|
||||||
|
gitspaceInstance.ActiveTimeStarted,
|
||||||
|
gitspaceInstance.ActiveTimeEnded,
|
||||||
).
|
).
|
||||||
Suffix(ReturningClause + "gits_id")
|
Suffix(ReturningClause + "gits_id")
|
||||||
sql, args, err := stmt.ToSql()
|
sql, args, err := stmt.ToSql()
|
||||||
|
@ -171,6 +177,9 @@ func (g gitspaceInstanceStore) Update(
|
||||||
Set("gits_last_used", gitspaceInstance.LastUsed).
|
Set("gits_last_used", gitspaceInstance.LastUsed).
|
||||||
Set("gits_last_heartbeat", gitspaceInstance.LastHeartbeat).
|
Set("gits_last_heartbeat", gitspaceInstance.LastHeartbeat).
|
||||||
Set("gits_url", gitspaceInstance.URL).
|
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).
|
Set("gits_updated", gitspaceInstance.Updated).
|
||||||
Where("gits_id = ?", gitspaceInstance.ID)
|
Where("gits_id = ?", gitspaceInstance.ID)
|
||||||
sql, args, err := stmt.ToSql()
|
sql, args, err := stmt.ToSql()
|
||||||
|
@ -286,6 +295,8 @@ func (g gitspaceInstanceStore) mapToGitspaceInstance(
|
||||||
Created: in.Created,
|
Created: in.Created,
|
||||||
Updated: in.Updated,
|
Updated: in.Updated,
|
||||||
LastHeartbeat: in.LastHeartbeat.Ptr(),
|
LastHeartbeat: in.LastHeartbeat.Ptr(),
|
||||||
|
ActiveTimeEnded: in.ActiveTimeEnded.Ptr(),
|
||||||
|
ActiveTimeStarted: in.ActiveTimeStarted.Ptr(),
|
||||||
}
|
}
|
||||||
return res, nil
|
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;
|
|
@ -74,6 +74,8 @@ type GitspaceInstance struct {
|
||||||
Created int64 `json:"created"`
|
Created int64 `json:"created"`
|
||||||
Updated int64 `json:"updated"`
|
Updated int64 `json:"updated"`
|
||||||
LastHeartbeat *int64 `json:"last_heartbeat,omitempty"`
|
LastHeartbeat *int64 `json:"last_heartbeat,omitempty"`
|
||||||
|
ActiveTimeStarted *int64 `json:"active_time_started,omitempty"`
|
||||||
|
ActiveTimeEnded *int64 `json:"active_time_ended,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GitspaceFilter struct {
|
type GitspaceFilter struct {
|
||||||
|
|
Loading…
Reference in New Issue