add temp endpoint for db cleanup
This commit is contained in:
@@ -35,6 +35,11 @@ INSERT INTO "Updates" (
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CleanupUpdates :exec
|
||||
DELETE FROM "Updates"
|
||||
WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime')
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateEvent :one
|
||||
INSERT INTO "Events" (
|
||||
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
|
||||
|
@@ -10,6 +10,22 @@ import (
|
||||
"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
|
||||
INSERT INTO "Events" (
|
||||
"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")
|
||||
}
|
||||
}
|
@@ -85,19 +85,19 @@ type ConfigChangesReceived struct {
|
||||
|
||||
// This probably needs more fields added so not in use yet
|
||||
type ConfigSpec struct {
|
||||
AlternateGuestName string `json:"AlternateGuestName"`
|
||||
Annotation string `json:"Annotation"`
|
||||
BootOptions any `json:"BootOptions"`
|
||||
ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"`
|
||||
ChangeVersion string `json:"ChangeVersion"`
|
||||
ConsolePreferences any `json:"ConsolePreferences"`
|
||||
CPUAffinity any `json:"CpuAffinity"`
|
||||
CPUAllocation any `json:"CpuAllocation"`
|
||||
CPUFeatureMask any `json:"CpuFeatureMask"`
|
||||
CPUHotAddEnabled any `json:"CpuHotAddEnabled"`
|
||||
CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"`
|
||||
CreateDate time.Time `json:"CreateDate"`
|
||||
Crypto any `json:"Crypto"`
|
||||
AlternateGuestName string `json:"AlternateGuestName"`
|
||||
Annotation string `json:"Annotation"`
|
||||
BootOptions any `json:"BootOptions"`
|
||||
ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"`
|
||||
ChangeVersion string `json:"ChangeVersion"`
|
||||
ConsolePreferences any `json:"ConsolePreferences"`
|
||||
CPUAffinity any `json:"CpuAffinity"`
|
||||
CPUAllocation any `json:"CpuAllocation"`
|
||||
CPUFeatureMask any `json:"CpuFeatureMask"`
|
||||
CPUHotAddEnabled any `json:"CpuHotAddEnabled"`
|
||||
CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"`
|
||||
CreateDate string `json:"CreateDate"` // Modified from time.Time
|
||||
Crypto any `json:"Crypto"`
|
||||
DeviceChange []struct {
|
||||
Backing any `json:"Backing"`
|
||||
Device struct {
|
||||
@@ -151,7 +151,7 @@ type ConfigSpec struct {
|
||||
Profile []struct {
|
||||
ProfileData struct {
|
||||
ExtensionKey string `json:"ExtensionKey"`
|
||||
ObjectData time.Time `json:"ObjectData"`
|
||||
ObjectData time.Time `json:"ObjectData"` // Modified from time.Time
|
||||
} `json:"ProfileData"`
|
||||
ProfileID string `json:"ProfileId"`
|
||||
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/delete", h.VmDelete)
|
||||
mux.HandleFunc("/api/import/vm", h.VmImport)
|
||||
// temporary endpoint
|
||||
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
||||
|
||||
return middleware.NewLoggingMiddleware(logger, mux)
|
||||
}
|
||||
|
Reference in New Issue
Block a user