diff --git a/db/queries/query.sql b/db/queries/query.sql index f9fdb50..563c76c 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -75,6 +75,11 @@ DELETE FROM "Updates" WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime') RETURNING *; +-- name: CleanupUpdatesNullVm :exec +DELETE FROM "Updates" +WHERE "InventoryId" IS NULL +RETURNING *; + -- name: CreateEvent :one INSERT INTO "Events" ( "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventType", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index 8f0f1d7..37f8d69 100644 --- a/db/queries/query.sql.go +++ b/db/queries/query.sql.go @@ -26,6 +26,17 @@ func (q *Queries) CleanupUpdates(ctx context.Context, arg CleanupUpdatesParams) return err } +const cleanupUpdatesNullVm = `-- name: CleanupUpdatesNullVm :exec +DELETE FROM "Updates" +WHERE "InventoryId" IS NULL +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name +` + +func (q *Queries) CleanupUpdatesNullVm(ctx context.Context) error { + _, err := q.db.ExecContext(ctx, cleanupUpdatesNullVm) + return err +} + const createEvent = `-- name: CreateEvent :one INSERT INTO "Events" ( "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventType", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" diff --git a/server/handler/updateCleanup.go b/server/handler/updateCleanup.go index d6baa6b..69ec92b 100644 --- a/server/handler/updateCleanup.go +++ b/server/handler/updateCleanup.go @@ -2,30 +2,31 @@ package handler import ( "context" - "database/sql" "fmt" "net/http" - "time" - "vctp/db/queries" ) // VmUpdate receives the CloudEvent for a VM modification or move func (h *Handler) UpdateCleanup(w http.ResponseWriter, r *http.Request) { - // Get the current time - now := time.Now() - // Get the start of the current day (midnight today) - midnightToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) - // Convert to Unix time - unixTime := midnightToday.Unix() + /* + // Get the current time + now := time.Now() + // Get the start of the current day (midnight today) + midnightToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + // Convert to Unix time + unixTime := midnightToday.Unix() - // create the database parameters - params := queries.CleanupUpdatesParams{ - UpdateType: "diskchange", - UpdateTime: sql.NullInt64{Int64: unixTime, Valid: unixTime > 0}, - } + // create the database parameters + params := queries.CleanupUpdatesParams{ + UpdateType: "diskchange", + UpdateTime: sql.NullInt64{Int64: unixTime, Valid: unixTime > 0}, + } - h.Logger.Debug("database params", "params", params) - err := h.Database.Queries().CleanupUpdates(context.Background(), params) + h.Logger.Debug("database params", "params", params) + err := h.Database.Queries().CleanupUpdates(context.Background(), params) + */ + + err := h.Database.Queries().CleanupUpdatesNullVm(context.Background()) if err != nil { h.Logger.Error("Error received cleaning updates table", "error", err) diff --git a/server/router/router.go b/server/router/router.go index 83796c9..8a2bfdc 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -41,7 +41,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st //mux.HandleFunc("/api/inventory/vm/update", h.VmUpdateDetails) // temporary endpoint - //mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) + mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) //mux.HandleFunc("/api/cleanup/vcenter", h.VcCleanup) mux.HandleFunc("/api/report/inventory", h.InventoryReportDownload)