more accurate deletion times in aggregations
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -542,6 +542,53 @@ func MarkVmDeleted(ctx context.Context, dbConn *sqlx.DB, vcenter, vmID, vmUUID s
|
||||
return MarkVmDeletedWithDetails(ctx, dbConn, vcenter, vmID, vmUUID, "", "", deletedAt)
|
||||
}
|
||||
|
||||
// ApplyLifecycleDeletionToSummary updates DeletionTime values in a summary table from vm_lifecycle_cache.
|
||||
func ApplyLifecycleDeletionToSummary(ctx context.Context, dbConn *sqlx.DB, summaryTable string, start, end int64) (int64, error) {
|
||||
if err := ValidateTableName(summaryTable); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := EnsureVmLifecycleCache(ctx, dbConn); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE %[1]s
|
||||
SET "DeletionTime" = (
|
||||
SELECT MIN(l."DeletedAt")
|
||||
FROM vm_lifecycle_cache l
|
||||
WHERE l."Vcenter" = %[1]s."Vcenter"
|
||||
AND l."DeletedAt" IS NOT NULL AND l."DeletedAt" > 0
|
||||
AND l."DeletedAt" >= ? AND l."DeletedAt" < ?
|
||||
AND (
|
||||
(l."VmId" IS NOT NULL AND %[1]s."VmId" IS NOT NULL AND l."VmId" = %[1]s."VmId")
|
||||
OR (l."VmUuid" IS NOT NULL AND %[1]s."VmUuid" IS NOT NULL AND l."VmUuid" = %[1]s."VmUuid")
|
||||
OR (l."Name" IS NOT NULL AND %[1]s."Name" IS NOT NULL AND l."Name" = %[1]s."Name")
|
||||
)
|
||||
)
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM vm_lifecycle_cache l
|
||||
WHERE l."Vcenter" = %[1]s."Vcenter"
|
||||
AND l."DeletedAt" IS NOT NULL AND l."DeletedAt" > 0
|
||||
AND l."DeletedAt" >= ? AND l."DeletedAt" < ?
|
||||
AND (
|
||||
(l."VmId" IS NOT NULL AND %[1]s."VmId" IS NOT NULL AND l."VmId" = %[1]s."VmId")
|
||||
OR (l."VmUuid" IS NOT NULL AND %[1]s."VmUuid" IS NOT NULL AND l."VmUuid" = %[1]s."VmUuid")
|
||||
OR (l."Name" IS NOT NULL AND %[1]s."Name" IS NOT NULL AND l."Name" = %[1]s."Name")
|
||||
)
|
||||
AND (%[1]s."DeletionTime" IS NULL OR %[1]s."DeletionTime" = 0 OR l."DeletedAt" < %[1]s."DeletionTime")
|
||||
);
|
||||
`, summaryTable)
|
||||
bind := dbConn.Rebind(query)
|
||||
res, err := execLog(ctx, dbConn, bind, start, end, start, end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rows, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// UpsertVmDailyRollup writes/updates a daily rollup row.
|
||||
func UpsertVmDailyRollup(ctx context.Context, dbConn *sqlx.DB, day int64, v VmDailyRollupRow) error {
|
||||
if err := EnsureVmDailyRollup(ctx, dbConn); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user