add temp endpoint for db cleanup
This commit is contained in:
@@ -35,6 +35,11 @@ INSERT INTO "Updates" (
|
|||||||
)
|
)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: CleanupUpdates :exec
|
||||||
|
DELETE FROM "Updates"
|
||||||
|
WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime')
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
-- name: CreateEvent :one
|
-- name: CreateEvent :one
|
||||||
INSERT INTO "Events" (
|
INSERT INTO "Events" (
|
||||||
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
|
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
|
||||||
|
@@ -10,6 +10,22 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const cleanupUpdates = `-- name: CleanupUpdates :exec
|
||||||
|
DELETE FROM "Updates"
|
||||||
|
WHERE "UpdateType" = ?1 AND "UpdateTime" <= ?2
|
||||||
|
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk
|
||||||
|
`
|
||||||
|
|
||||||
|
type CleanupUpdatesParams struct {
|
||||||
|
UpdateType string
|
||||||
|
UpdateTime sql.NullInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CleanupUpdates(ctx context.Context, arg CleanupUpdatesParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, cleanupUpdates, arg.UpdateType, arg.UpdateTime)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const createEvent = `-- name: CreateEvent :one
|
const createEvent = `-- name: CreateEvent :one
|
||||||
INSERT INTO "Events" (
|
INSERT INTO "Events" (
|
||||||
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
|
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
|
||||||
|
40
server/handler/updateCleanup.go
Normal file
40
server/handler/updateCleanup.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
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()
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
h.Logger.Error("Error received cleaning updates table", "error", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(w, "Delete Request unsuccessful %s\n", err)
|
||||||
|
} else {
|
||||||
|
h.Logger.Debug("Processed update cleanup successfully")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
// TODO - return some JSON
|
||||||
|
fmt.Fprintf(w, "Processed update cleanup successfully")
|
||||||
|
}
|
||||||
|
}
|
@@ -96,7 +96,7 @@ type ConfigSpec struct {
|
|||||||
CPUFeatureMask any `json:"CpuFeatureMask"`
|
CPUFeatureMask any `json:"CpuFeatureMask"`
|
||||||
CPUHotAddEnabled any `json:"CpuHotAddEnabled"`
|
CPUHotAddEnabled any `json:"CpuHotAddEnabled"`
|
||||||
CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"`
|
CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"`
|
||||||
CreateDate time.Time `json:"CreateDate"`
|
CreateDate string `json:"CreateDate"` // Modified from time.Time
|
||||||
Crypto any `json:"Crypto"`
|
Crypto any `json:"Crypto"`
|
||||||
DeviceChange []struct {
|
DeviceChange []struct {
|
||||||
Backing any `json:"Backing"`
|
Backing any `json:"Backing"`
|
||||||
@@ -151,7 +151,7 @@ type ConfigSpec struct {
|
|||||||
Profile []struct {
|
Profile []struct {
|
||||||
ProfileData struct {
|
ProfileData struct {
|
||||||
ExtensionKey string `json:"ExtensionKey"`
|
ExtensionKey string `json:"ExtensionKey"`
|
||||||
ObjectData time.Time `json:"ObjectData"`
|
ObjectData time.Time `json:"ObjectData"` // Modified from time.Time
|
||||||
} `json:"ProfileData"`
|
} `json:"ProfileData"`
|
||||||
ProfileID string `json:"ProfileId"`
|
ProfileID string `json:"ProfileId"`
|
||||||
ProfileParams any `json:"ProfileParams"`
|
ProfileParams any `json:"ProfileParams"`
|
||||||
|
@@ -26,6 +26,8 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
|
|||||||
mux.HandleFunc("/api/event/vm/modify", h.VmModify)
|
mux.HandleFunc("/api/event/vm/modify", h.VmModify)
|
||||||
mux.HandleFunc("/api/event/vm/delete", h.VmDelete)
|
mux.HandleFunc("/api/event/vm/delete", h.VmDelete)
|
||||||
mux.HandleFunc("/api/import/vm", h.VmImport)
|
mux.HandleFunc("/api/import/vm", h.VmImport)
|
||||||
|
// temporary endpoint
|
||||||
|
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
||||||
|
|
||||||
return middleware.NewLoggingMiddleware(logger, mux)
|
return middleware.NewLoggingMiddleware(logger, mux)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user