feat: [CDE-353]: Adding gitspace instance store method to calculate active time for all gitspaces between a given time window and filtered by a list of space IDs. (#2748)

* feat: [CDE-353]: Adding gitspace instance store method to calculate active time for all gitspaces between a given time window and filtered by a list of space IDs.
This commit is contained in:
Dhruv Dhruv 2024-09-30 08:35:14 +00:00 committed by Harness
parent 077256b69c
commit a1a4e3fe06
2 changed files with 63 additions and 0 deletions

View File

@ -681,6 +681,10 @@ type (
ctx context.Context,
gitspaceConfigIDs []int64,
) ([]*types.GitspaceInstance, error)
// FindTotalUsage calculates the total time used in millis for all the instances within the time window
// defined by fromTime and toTime.
FindTotalUsage(ctx context.Context, fromTime int64, toTime int64, spaceIDs []int64) (int64, error)
}
InfraProviderConfigStore interface {

View File

@ -16,6 +16,7 @@ package database
import (
"context"
"database/sql"
"fmt"
"strings"
@ -91,6 +92,64 @@ type gitspaceInstanceStore struct {
db *sqlx.DB
}
func (g gitspaceInstanceStore) FindTotalUsage(
ctx context.Context,
fromTime int64,
toTime int64,
spaceIDs []int64,
) (int64, error) {
var greatest = "MAX"
var least = "MIN"
if g.db.DriverName() == "postgres" {
greatest = "GREATEST"
least = "LEAST"
}
innerQuery := squirrel.Select(
greatest+"(gits_active_time_started, ?) AS effective_start_time",
least+"(COALESCE(gits_active_time_ended, ?), ?) AS effective_end_time",
).
From(gitspaceInstanceTable).
Where(
squirrel.And{
squirrel.Lt{"gits_active_time_started": toTime},
squirrel.Or{
squirrel.Expr("gits_active_time_ended IS NULL"),
squirrel.Gt{"gits_active_time_ended": fromTime},
},
squirrel.Eq{"gits_space_id": spaceIDs},
},
)
innerQry, innerArgs, err := innerQuery.ToSql()
if err != nil {
return 0, err
}
query := squirrel.
Select("SUM(effective_end_time - effective_start_time) AS total_active_time").
From("(" + innerQry + ") AS subquery").PlaceholderFormat(squirrel.Dollar)
qry, _, err := query.ToSql()
if err != nil {
return 0, errors.Wrap(err, "Failed to convert squirrel builder to sql")
}
args := append([]any{fromTime, toTime, toTime}, innerArgs...)
var totalActiveTime sql.NullInt64
db := dbtx.GetAccessor(ctx, g.db)
err = db.GetContext(ctx, &totalActiveTime, qry, args...)
if err != nil {
return 0, err
}
if totalActiveTime.Valid {
return totalActiveTime.Int64, nil
}
return 0, nil
}
func (g gitspaceInstanceStore) Find(ctx context.Context, id int64) (*types.GitspaceInstance, error) {
stmt := database.Builder.
Select(gitspaceInstanceSelectColumns).