rename to mocksnow

This commit is contained in:
2025-03-22 17:05:42 +11:00
parent c8c475911b
commit 112ad8da1a
33 changed files with 192 additions and 1802 deletions

BIN
.!31628!.DS_Store Normal file

Binary file not shown.

View File

@@ -21,4 +21,4 @@ run:
@go run main.go
build:
@echo "Building..."
@go build -o ./build/wnzl-snow -ldflags="-s -w -X version.Value=1.0.0"
@go build -o ./build/mocksnow -ldflags="-s -w -X version.Value=1.0.0"

View File

@@ -1,13 +1,13 @@
package core
import "wnzl-snow/version"
import "mocksnow/version"
templ Header() {
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="wnzl-snow API endpoint"/>
<title>wnzl-snow API</title>
<meta name="description" content="mocksnow API endpoint"/>
<title>mocksnow API</title>
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
</head>

View File

@@ -8,7 +8,7 @@ package core
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "wnzl-snow/version"
import "mocksnow/version"
func Header() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
@@ -31,7 +31,7 @@ func Header() templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta name=\"description\" content=\"wnzl-snow API endpoint\"><title>wnzl-snow API</title><script src=\"/assets/js/htmx@v2.0.2.min.js\"></script><link href=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta name=\"description\" content=\"mocksnow API endpoint\"><title>mocksnow API</title><script src=\"/assets/js/htmx@v2.0.2.min.js\"></script><link href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

View File

@@ -1,6 +1,6 @@
package core
import "wnzl-snow/version"
import "mocksnow/version"
templ HTML(title string, content templ.Component) {
<!DOCTYPE html>

View File

@@ -2,7 +2,7 @@ package views
import (
"strconv"
"wnzl-snow/components/core"
"mocksnow/components/core"
)
type IncomingRow struct {

View File

@@ -10,7 +10,7 @@ import templruntime "github.com/a-h/templ/runtime"
import (
"strconv"
"wnzl-snow/components/core"
"mocksnow/components/core"
)
type IncomingRow struct {

View File

@@ -1,7 +1,7 @@
package views
import (
"wnzl-snow/components/core"
"mocksnow/components/core"
)
type BuildInfo struct {

View File

@@ -9,7 +9,7 @@ import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import (
"wnzl-snow/components/core"
"mocksnow/components/core"
)
type BuildInfo struct {

View File

@@ -5,7 +5,7 @@ import (
"embed"
"log/slog"
"reflect"
"wnzl-snow/db/queries"
"mocksnow/db/queries"
"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"

View File

@@ -4,7 +4,7 @@ import (
"database/sql"
"fmt"
"log/slog"
"wnzl-snow/db/queries"
"mocksnow/db/queries"
//_ "github.com/tursodatabase/libsql-client-go/libsql"
"github.com/jmoiron/sqlx"

View File

@@ -8,71 +8,19 @@ import (
"database/sql"
)
type Events struct {
Eid int64
CloudId string
Source string
EventTime sql.NullInt64
ChainId string
VmId sql.NullString
EventKey sql.NullString
DatacenterName sql.NullString
ComputeResourceName sql.NullString
UserName sql.NullString
Processed int64
DatacenterId sql.NullString
ComputeResourceId sql.NullString
VmName sql.NullString
EventType sql.NullString
}
type Inventory struct {
Iid int64
Name string
Vcenter string
VmId sql.NullString
EventKey sql.NullString
CloudId sql.NullString
CreationTime sql.NullInt64
DeletionTime sql.NullInt64
ResourcePool sql.NullString
VmType sql.NullString
Datacenter sql.NullString
Cluster sql.NullString
Folder sql.NullString
ProvisionedDisk sql.NullFloat64
InitialVcpus sql.NullInt64
InitialRam sql.NullInt64
IsTemplate interface{}
PoweredOn interface{}
SrmPlaceholder interface{}
VmUuid sql.NullString
}
type InventoryHistory struct {
Hid int64
InventoryId sql.NullInt64
ReportDate sql.NullInt64
UpdateTime sql.NullInt64
PreviousVcpus sql.NullInt64
PreviousRam sql.NullInt64
PreviousResourcePool sql.NullString
PreviousProvisionedDisk sql.NullFloat64
}
type Updates struct {
Uid int64
InventoryId sql.NullInt64
UpdateTime sql.NullInt64
UpdateType string
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString
EventKey sql.NullString
EventId sql.NullString
NewProvisionedDisk sql.NullFloat64
UserName sql.NullString
PlaceholderChange sql.NullString
Name sql.NullString
RawChangeString []byte
}
type Incoming struct {
ID int64
IncidentNumber sql.NullString
Description sql.NullString
ShortDescription sql.NullString
Urgency sql.NullString
Impact sql.NullString
State sql.NullString
ExternalID sql.NullString
WorkNotes sql.NullString
AssignmentGroup sql.NullString
AssignedTo sql.NullString
Category sql.NullString
Subcategory sql.NullString
CreatedAt sql.NullTime
}

View File

@@ -1,121 +1,12 @@
-- name: ListInventory :many
SELECT * FROM "Inventory"
ORDER BY "Name";
-- name: ListIncoming :many
SELECT * FROM "Incoming"
ORDER BY "id";
-- name: GetReportInventory :many
SELECT * FROM "Inventory"
ORDER BY "CreationTime";
-- name: GetInventoryByName :many
SELECT * FROM "Inventory"
WHERE "Name" = ?;
-- name: GetInventoryByVcenter :many
SELECT * FROM "Inventory"
WHERE "Vcenter" = ?;
-- name: GetInventoryVmId :one
SELECT * FROM "Inventory"
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');
-- name: GetInventoryVmUuid :one
SELECT * FROM "Inventory"
WHERE "VmUuid" = sqlc.arg('vmUuid') AND "Datacenter" = sqlc.arg('datacenterName');
-- name: GetInventoryVcUrl :many
SELECT * FROM "Inventory"
WHERE "Vcenter" = sqlc.arg('vc');
-- name: GetInventoryEventId :one
SELECT * FROM "Inventory"
WHERE "CloudId" = ? LIMIT 1;
-- name: CreateInventory :one
INSERT INTO "Inventory" (
"Name", "Vcenter", "VmId", "VmUuid", "EventKey", "CloudId", "CreationTime", "ResourcePool", "VmType", "IsTemplate", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder", "PoweredOn"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;
-- name: InventoryUpdate :exec
UPDATE "Inventory"
SET "VmUuid" = sqlc.arg('uuid'), "SrmPlaceholder" = sqlc.arg('srmPlaceholder')
WHERE "Iid" = sqlc.arg('iid');
-- name: InventoryMarkDeleted :exec
UPDATE "Inventory"
SET "DeletionTime" = sqlc.arg('deletionTime')
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');
-- name: InventoryCleanup :exec
DELETE FROM "Inventory"
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName')
RETURNING *;
-- name: InventoryCleanupVcenter :exec
DELETE FROM "Inventory"
WHERE "Vcenter" = sqlc.arg('vc')
RETURNING *;
-- name: InventoryCleanupTemplates :exec
DELETE FROM "Inventory"
WHERE "IsTemplate" = "TRUE"
RETURNING *;
-- name: CreateUpdate :one
INSERT INTO "Updates" (
"InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange", "RawChangeString"
-- name: CreateIncoming :one
INSERT INTO "Incoming" (
"incident_number", "description", "short_description", "urgency", "impact", "state", "external_id", "work_notes", "assignment_group", "assigned_to", "category", "subcategory", "created_at"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;
-- name: GetReportUpdates :many
SELECT * FROM "Updates"
ORDER BY "UpdateTime";
-- name: GetVmUpdates :many
SELECT * FROM "Updates"
WHERE "UpdateType" = sqlc.arg('updateType') AND "InventoryId" = sqlc.arg('InventoryId');
-- name: CleanupUpdates :exec
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"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;
-- name: ListEvents :many
SELECT * FROM "Events"
ORDER BY "EventTime";
-- name: ListUnprocessedEvents :many
SELECT * FROM "Events"
WHERE "Processed" = 0
AND "EventTime" > sqlc.arg('eventTime')
ORDER BY "EventTime";
-- name: UpdateEventsProcessed :exec
UPDATE "Events"
SET "Processed" = 1
WHERE "Eid" = sqlc.arg('eid');
-- name: CreateInventoryHistory :one
INSERT INTO "InventoryHistory" (
"InventoryId", "ReportDate", "UpdateTime", "PreviousVcpus", "PreviousRam", "PreviousResourcePool", "PreviousProvisionedDisk"
) VALUES(
?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

View File

@@ -10,311 +10,96 @@ 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, UserName, PlaceholderChange, Name, RawChangeString
`
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 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, RawChangeString
`
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"
const createIncoming = `-- name: CreateIncoming :one
INSERT INTO "Incoming" (
"incident_number", "description", "short_description", "urgency", "impact", "state", "external_id", "work_notes", "assignment_group", "assigned_to", "category", "subcategory", "created_at"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType
RETURNING id, incident_number, description, short_description, urgency, impact, state, external_id, work_notes, assignment_group, assigned_to, category, subcategory, created_at
`
type CreateEventParams struct {
CloudId string
Source string
EventTime sql.NullInt64
ChainId string
VmId sql.NullString
VmName sql.NullString
EventType sql.NullString
EventKey sql.NullString
DatacenterId sql.NullString
DatacenterName sql.NullString
ComputeResourceId sql.NullString
ComputeResourceName sql.NullString
UserName sql.NullString
type CreateIncomingParams struct {
IncidentNumber sql.NullString
Description sql.NullString
ShortDescription sql.NullString
Urgency sql.NullString
Impact sql.NullString
State sql.NullString
ExternalID sql.NullString
WorkNotes sql.NullString
AssignmentGroup sql.NullString
AssignedTo sql.NullString
Category sql.NullString
Subcategory sql.NullString
CreatedAt sql.NullTime
}
func (q *Queries) CreateEvent(ctx context.Context, arg CreateEventParams) (Events, error) {
row := q.db.QueryRowContext(ctx, createEvent,
arg.CloudId,
arg.Source,
arg.EventTime,
arg.ChainId,
arg.VmId,
arg.VmName,
arg.EventType,
arg.EventKey,
arg.DatacenterId,
arg.DatacenterName,
arg.ComputeResourceId,
arg.ComputeResourceName,
arg.UserName,
func (q *Queries) CreateIncoming(ctx context.Context, arg CreateIncomingParams) (Incoming, error) {
row := q.db.QueryRowContext(ctx, createIncoming,
arg.IncidentNumber,
arg.Description,
arg.ShortDescription,
arg.Urgency,
arg.Impact,
arg.State,
arg.ExternalID,
arg.WorkNotes,
arg.AssignmentGroup,
arg.AssignedTo,
arg.Category,
arg.Subcategory,
arg.CreatedAt,
)
var i Events
var i Incoming
err := row.Scan(
&i.Eid,
&i.CloudId,
&i.Source,
&i.EventTime,
&i.ChainId,
&i.VmId,
&i.EventKey,
&i.DatacenterName,
&i.ComputeResourceName,
&i.UserName,
&i.Processed,
&i.DatacenterId,
&i.ComputeResourceId,
&i.VmName,
&i.EventType,
&i.ID,
&i.IncidentNumber,
&i.Description,
&i.ShortDescription,
&i.Urgency,
&i.Impact,
&i.State,
&i.ExternalID,
&i.WorkNotes,
&i.AssignmentGroup,
&i.AssignedTo,
&i.Category,
&i.Subcategory,
&i.CreatedAt,
)
return i, err
}
const createInventory = `-- name: CreateInventory :one
INSERT INTO "Inventory" (
"Name", "Vcenter", "VmId", "VmUuid", "EventKey", "CloudId", "CreationTime", "ResourcePool", "VmType", "IsTemplate", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder", "PoweredOn"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid
const listIncoming = `-- name: ListIncoming :many
SELECT id, incident_number, description, short_description, urgency, impact, state, external_id, work_notes, assignment_group, assigned_to, category, subcategory, created_at FROM "Incoming"
ORDER BY "id"
`
type CreateInventoryParams struct {
Name string
Vcenter string
VmId sql.NullString
VmUuid sql.NullString
EventKey sql.NullString
CloudId sql.NullString
CreationTime sql.NullInt64
ResourcePool sql.NullString
VmType sql.NullString
IsTemplate interface{}
Datacenter sql.NullString
Cluster sql.NullString
Folder sql.NullString
ProvisionedDisk sql.NullFloat64
InitialVcpus sql.NullInt64
InitialRam sql.NullInt64
SrmPlaceholder interface{}
PoweredOn interface{}
}
func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams) (Inventory, error) {
row := q.db.QueryRowContext(ctx, createInventory,
arg.Name,
arg.Vcenter,
arg.VmId,
arg.VmUuid,
arg.EventKey,
arg.CloudId,
arg.CreationTime,
arg.ResourcePool,
arg.VmType,
arg.IsTemplate,
arg.Datacenter,
arg.Cluster,
arg.Folder,
arg.ProvisionedDisk,
arg.InitialVcpus,
arg.InitialRam,
arg.SrmPlaceholder,
arg.PoweredOn,
)
var i Inventory
err := row.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
)
return i, err
}
const createInventoryHistory = `-- name: CreateInventoryHistory :one
INSERT INTO "InventoryHistory" (
"InventoryId", "ReportDate", "UpdateTime", "PreviousVcpus", "PreviousRam", "PreviousResourcePool", "PreviousProvisionedDisk"
) VALUES(
?, ?, ?, ?, ?, ?, ?
)
RETURNING Hid, InventoryId, ReportDate, UpdateTime, PreviousVcpus, PreviousRam, PreviousResourcePool, PreviousProvisionedDisk
`
type CreateInventoryHistoryParams struct {
InventoryId sql.NullInt64
ReportDate sql.NullInt64
UpdateTime sql.NullInt64
PreviousVcpus sql.NullInt64
PreviousRam sql.NullInt64
PreviousResourcePool sql.NullString
PreviousProvisionedDisk sql.NullFloat64
}
func (q *Queries) CreateInventoryHistory(ctx context.Context, arg CreateInventoryHistoryParams) (InventoryHistory, error) {
row := q.db.QueryRowContext(ctx, createInventoryHistory,
arg.InventoryId,
arg.ReportDate,
arg.UpdateTime,
arg.PreviousVcpus,
arg.PreviousRam,
arg.PreviousResourcePool,
arg.PreviousProvisionedDisk,
)
var i InventoryHistory
err := row.Scan(
&i.Hid,
&i.InventoryId,
&i.ReportDate,
&i.UpdateTime,
&i.PreviousVcpus,
&i.PreviousRam,
&i.PreviousResourcePool,
&i.PreviousProvisionedDisk,
)
return i, err
}
const createUpdate = `-- name: CreateUpdate :one
INSERT INTO "Updates" (
"InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange", "RawChangeString"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString
`
type CreateUpdateParams struct {
InventoryId sql.NullInt64
Name sql.NullString
EventKey sql.NullString
EventId sql.NullString
UpdateTime sql.NullInt64
UpdateType string
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString
NewProvisionedDisk sql.NullFloat64
UserName sql.NullString
PlaceholderChange sql.NullString
RawChangeString []byte
}
func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) {
row := q.db.QueryRowContext(ctx, createUpdate,
arg.InventoryId,
arg.Name,
arg.EventKey,
arg.EventId,
arg.UpdateTime,
arg.UpdateType,
arg.NewVcpus,
arg.NewRam,
arg.NewResourcePool,
arg.NewProvisionedDisk,
arg.UserName,
arg.PlaceholderChange,
arg.RawChangeString,
)
var i Updates
err := row.Scan(
&i.Uid,
&i.InventoryId,
&i.UpdateTime,
&i.UpdateType,
&i.NewVcpus,
&i.NewRam,
&i.NewResourcePool,
&i.EventKey,
&i.EventId,
&i.NewProvisionedDisk,
&i.UserName,
&i.PlaceholderChange,
&i.Name,
&i.RawChangeString,
)
return i, err
}
const getInventoryByName = `-- name: GetInventoryByName :many
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "Name" = ?
`
func (q *Queries) GetInventoryByName(ctx context.Context, name string) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, getInventoryByName, name)
func (q *Queries) ListIncoming(ctx context.Context) ([]Incoming, error) {
rows, err := q.db.QueryContext(ctx, listIncoming)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
var items []Incoming
for rows.Next() {
var i Inventory
var i Incoming
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
&i.ID,
&i.IncidentNumber,
&i.Description,
&i.ShortDescription,
&i.Urgency,
&i.Impact,
&i.State,
&i.ExternalID,
&i.WorkNotes,
&i.AssignmentGroup,
&i.AssignedTo,
&i.Category,
&i.Subcategory,
&i.CreatedAt,
); err != nil {
return nil, err
}
@@ -328,572 +113,3 @@ func (q *Queries) GetInventoryByName(ctx context.Context, name string) ([]Invent
}
return items, nil
}
const getInventoryByVcenter = `-- name: GetInventoryByVcenter :many
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "Vcenter" = ?
`
func (q *Queries) GetInventoryByVcenter(ctx context.Context, vcenter string) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, getInventoryByVcenter, vcenter)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
for rows.Next() {
var i Inventory
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getInventoryEventId = `-- name: GetInventoryEventId :one
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "CloudId" = ? LIMIT 1
`
func (q *Queries) GetInventoryEventId(ctx context.Context, cloudid sql.NullString) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryEventId, cloudid)
var i Inventory
err := row.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
)
return i, err
}
const getInventoryVcUrl = `-- name: GetInventoryVcUrl :many
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "Vcenter" = ?1
`
func (q *Queries) GetInventoryVcUrl(ctx context.Context, vc string) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, getInventoryVcUrl, vc)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
for rows.Next() {
var i Inventory
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getInventoryVmId = `-- name: GetInventoryVmId :one
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "VmId" = ?1 AND "Datacenter" = ?2
`
type GetInventoryVmIdParams struct {
VmId sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) GetInventoryVmId(ctx context.Context, arg GetInventoryVmIdParams) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryVmId, arg.VmId, arg.DatacenterName)
var i Inventory
err := row.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
)
return i, err
}
const getInventoryVmUuid = `-- name: GetInventoryVmUuid :one
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
WHERE "VmUuid" = ?1 AND "Datacenter" = ?2
`
type GetInventoryVmUuidParams struct {
VmUuid sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) GetInventoryVmUuid(ctx context.Context, arg GetInventoryVmUuidParams) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryVmUuid, arg.VmUuid, arg.DatacenterName)
var i Inventory
err := row.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
)
return i, err
}
const getReportInventory = `-- name: GetReportInventory :many
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
ORDER BY "CreationTime"
`
func (q *Queries) GetReportInventory(ctx context.Context) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, getReportInventory)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
for rows.Next() {
var i Inventory
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getReportUpdates = `-- name: GetReportUpdates :many
SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString FROM "Updates"
ORDER BY "UpdateTime"
`
func (q *Queries) GetReportUpdates(ctx context.Context) ([]Updates, error) {
rows, err := q.db.QueryContext(ctx, getReportUpdates)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Updates
for rows.Next() {
var i Updates
if err := rows.Scan(
&i.Uid,
&i.InventoryId,
&i.UpdateTime,
&i.UpdateType,
&i.NewVcpus,
&i.NewRam,
&i.NewResourcePool,
&i.EventKey,
&i.EventId,
&i.NewProvisionedDisk,
&i.UserName,
&i.PlaceholderChange,
&i.Name,
&i.RawChangeString,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getVmUpdates = `-- name: GetVmUpdates :many
SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString FROM "Updates"
WHERE "UpdateType" = ?1 AND "InventoryId" = ?2
`
type GetVmUpdatesParams struct {
UpdateType string
InventoryId sql.NullInt64
}
func (q *Queries) GetVmUpdates(ctx context.Context, arg GetVmUpdatesParams) ([]Updates, error) {
rows, err := q.db.QueryContext(ctx, getVmUpdates, arg.UpdateType, arg.InventoryId)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Updates
for rows.Next() {
var i Updates
if err := rows.Scan(
&i.Uid,
&i.InventoryId,
&i.UpdateTime,
&i.UpdateType,
&i.NewVcpus,
&i.NewRam,
&i.NewResourcePool,
&i.EventKey,
&i.EventId,
&i.NewProvisionedDisk,
&i.UserName,
&i.PlaceholderChange,
&i.Name,
&i.RawChangeString,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const inventoryCleanup = `-- name: InventoryCleanup :exec
DELETE FROM "Inventory"
WHERE "VmId" = ?1 AND "Datacenter" = ?2
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid
`
type InventoryCleanupParams struct {
VmId sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) InventoryCleanup(ctx context.Context, arg InventoryCleanupParams) error {
_, err := q.db.ExecContext(ctx, inventoryCleanup, arg.VmId, arg.DatacenterName)
return err
}
const inventoryCleanupTemplates = `-- name: InventoryCleanupTemplates :exec
DELETE FROM "Inventory"
WHERE "IsTemplate" = "TRUE"
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid
`
func (q *Queries) InventoryCleanupTemplates(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, inventoryCleanupTemplates)
return err
}
const inventoryCleanupVcenter = `-- name: InventoryCleanupVcenter :exec
DELETE FROM "Inventory"
WHERE "Vcenter" = ?1
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid
`
func (q *Queries) InventoryCleanupVcenter(ctx context.Context, vc string) error {
_, err := q.db.ExecContext(ctx, inventoryCleanupVcenter, vc)
return err
}
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
UPDATE "Inventory"
SET "DeletionTime" = ?1
WHERE "VmId" = ?2 AND "Datacenter" = ?3
`
type InventoryMarkDeletedParams struct {
DeletionTime sql.NullInt64
VmId sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) InventoryMarkDeleted(ctx context.Context, arg InventoryMarkDeletedParams) error {
_, err := q.db.ExecContext(ctx, inventoryMarkDeleted, arg.DeletionTime, arg.VmId, arg.DatacenterName)
return err
}
const inventoryUpdate = `-- name: InventoryUpdate :exec
UPDATE "Inventory"
SET "VmUuid" = ?1, "SrmPlaceholder" = ?2
WHERE "Iid" = ?3
`
type InventoryUpdateParams struct {
Uuid sql.NullString
SrmPlaceholder interface{}
Iid int64
}
func (q *Queries) InventoryUpdate(ctx context.Context, arg InventoryUpdateParams) error {
_, err := q.db.ExecContext(ctx, inventoryUpdate, arg.Uuid, arg.SrmPlaceholder, arg.Iid)
return err
}
const listEvents = `-- name: ListEvents :many
SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType FROM "Events"
ORDER BY "EventTime"
`
func (q *Queries) ListEvents(ctx context.Context) ([]Events, error) {
rows, err := q.db.QueryContext(ctx, listEvents)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Events
for rows.Next() {
var i Events
if err := rows.Scan(
&i.Eid,
&i.CloudId,
&i.Source,
&i.EventTime,
&i.ChainId,
&i.VmId,
&i.EventKey,
&i.DatacenterName,
&i.ComputeResourceName,
&i.UserName,
&i.Processed,
&i.DatacenterId,
&i.ComputeResourceId,
&i.VmName,
&i.EventType,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listInventory = `-- name: ListInventory :many
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
ORDER BY "Name"
`
func (q *Queries) ListInventory(ctx context.Context) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, listInventory)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
for rows.Next() {
var i Inventory
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.CloudId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.IsTemplate,
&i.PoweredOn,
&i.SrmPlaceholder,
&i.VmUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listUnprocessedEvents = `-- name: ListUnprocessedEvents :many
SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType FROM "Events"
WHERE "Processed" = 0
AND "EventTime" > ?1
ORDER BY "EventTime"
`
func (q *Queries) ListUnprocessedEvents(ctx context.Context, eventtime sql.NullInt64) ([]Events, error) {
rows, err := q.db.QueryContext(ctx, listUnprocessedEvents, eventtime)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Events
for rows.Next() {
var i Events
if err := rows.Scan(
&i.Eid,
&i.CloudId,
&i.Source,
&i.EventTime,
&i.ChainId,
&i.VmId,
&i.EventKey,
&i.DatacenterName,
&i.ComputeResourceName,
&i.UserName,
&i.Processed,
&i.DatacenterId,
&i.ComputeResourceId,
&i.VmName,
&i.EventType,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateEventsProcessed = `-- name: UpdateEventsProcessed :exec
UPDATE "Events"
SET "Processed" = 1
WHERE "Eid" = ?1
`
func (q *Queries) UpdateEventsProcessed(ctx context.Context, eid int64) error {
_, err := q.db.ExecContext(ctx, updateEventsProcessed, eid)
return err
}

0
dist/.!31629!.DS_Store vendored Normal file
View File

0
dist/assets/.!31631!.DS_Store vendored Normal file
View File

2
go.mod
View File

@@ -1,4 +1,4 @@
module wnzl-snow
module mocksnow
go 1.24.1

View File

@@ -1,303 +0,0 @@
package report
import (
"bytes"
"context"
"database/sql"
"fmt"
"log/slog"
"reflect"
"strconv"
"time"
"unicode/utf8"
"wnzl-snow/db"
"github.com/xuri/excelize/v2"
)
func CreateInventoryReport(logger *slog.Logger, Database db.Database, ctx context.Context) ([]byte, error) {
//var xlsx *excelize.File
sheetName := "Inventory Report"
var buffer bytes.Buffer
var cell string
logger.Debug("Querying inventory table")
results, err := Database.Queries().GetReportInventory(ctx)
if err != nil {
logger.Error("Unable to query inventory table", "error", err)
return nil, err
}
if len(results) == 0 {
logger.Error("Empty inventory results")
return nil, fmt.Errorf("Empty inventory results")
}
// Create excel workbook
xlsx := excelize.NewFile()
err = xlsx.SetSheetName("Sheet1", sheetName)
if err != nil {
logger.Error("Error setting sheet name", "error", err, "sheet_name", sheetName)
return nil, err
}
// Set the document properties
err = xlsx.SetDocProps(&excelize.DocProperties{
Creator: "json2excel",
Created: time.Now().Format(time.RFC3339),
})
if err != nil {
logger.Error("Error setting document properties", "error", err, "sheet_name", sheetName)
}
// Use reflection to determine column headings from the first item
firstItem := results[0]
v := reflect.ValueOf(firstItem)
typeOfItem := v.Type()
// Create column headers dynamically
for i := 0; i < v.NumField(); i++ {
column := string(rune('A'+i)) + "1" // A1, B1, C1, etc.
xlsx.SetCellValue(sheetName, column, typeOfItem.Field(i).Name)
}
// Set autofilter on heading row
cell, _ = excelize.CoordinatesToCellName(v.NumField(), 1)
filterRange := "A1:" + cell
logger.Debug("Setting autofilter", "range", filterRange)
// As per docs any filters applied need to be manually processed by us (eg hiding rows with blanks)
err = xlsx.AutoFilter(sheetName, filterRange, nil)
if err != nil {
logger.Error("Error setting autofilter", "error", err)
}
// Bold top row
headerStyle, err := xlsx.NewStyle(&excelize.Style{
Font: &excelize.Font{
Bold: true,
},
})
if err != nil {
logger.Error("Error generating header style", "error", err)
} else {
err = xlsx.SetRowStyle(sheetName, 1, 1, headerStyle)
if err != nil {
logger.Error("Error setting header style", "error", err)
}
}
// Populate the Excel file with data from the Inventory table
for i, item := range results {
v = reflect.ValueOf(item)
for j := 0; j < v.NumField(); j++ {
column := string(rune('A'+j)) + strconv.Itoa(i+2) // Start from row 2
value := getFieldValue(v.Field(j))
xlsx.SetCellValue(sheetName, column, value)
}
}
// Freeze top row
err = xlsx.SetPanes(sheetName, &excelize.Panes{
Freeze: true,
Split: false,
XSplit: 0,
YSplit: 1,
TopLeftCell: "A2",
ActivePane: "bottomLeft",
Selection: []excelize.Selection{
{SQRef: "A2", ActiveCell: "A2", Pane: "bottomLeft"},
},
})
if err != nil {
logger.Error("Error freezing top row", "error", err)
}
// Set column autowidth
/*
err = SetColAutoWidth(xlsx, sheetName)
if err != nil {
fmt.Printf("Error setting auto width : '%s'\n", err)
}
*/
// Save the Excel file into a byte buffer
if err := xlsx.Write(&buffer); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func CreateUpdatesReport(logger *slog.Logger, Database db.Database, ctx context.Context) ([]byte, error) {
//var xlsx *excelize.File
sheetName := "Updates Report"
var buffer bytes.Buffer
var cell string
logger.Debug("Querying updates table")
results, err := Database.Queries().GetReportUpdates(ctx)
if err != nil {
logger.Error("Unable to query updates table", "error", err)
return nil, err
}
if len(results) == 0 {
logger.Error("Empty updates results")
return nil, fmt.Errorf("Empty updates results")
}
// Create excel workbook
xlsx := excelize.NewFile()
err = xlsx.SetSheetName("Sheet1", sheetName)
if err != nil {
logger.Error("Error setting sheet name", "error", err, "sheet_name", sheetName)
return nil, err
}
// Set the document properties
err = xlsx.SetDocProps(&excelize.DocProperties{
Creator: "json2excel",
Created: time.Now().Format(time.RFC3339),
})
if err != nil {
logger.Error("Error setting document properties", "error", err, "sheet_name", sheetName)
}
// Use reflection to determine column headings from the first item
firstItem := results[0]
v := reflect.ValueOf(firstItem)
typeOfItem := v.Type()
// Create column headers dynamically
for i := 0; i < v.NumField(); i++ {
column := string(rune('A'+i)) + "1" // A1, B1, C1, etc.
xlsx.SetCellValue(sheetName, column, typeOfItem.Field(i).Name)
}
// Set autofilter on heading row
cell, _ = excelize.CoordinatesToCellName(v.NumField(), 1)
filterRange := "A1:" + cell
logger.Debug("Setting autofilter", "range", filterRange)
// As per docs any filters applied need to be manually processed by us (eg hiding rows with blanks)
err = xlsx.AutoFilter(sheetName, filterRange, nil)
if err != nil {
logger.Error("Error setting autofilter", "error", err)
}
// Bold top row
headerStyle, err := xlsx.NewStyle(&excelize.Style{
Font: &excelize.Font{
Bold: true,
},
})
if err != nil {
logger.Error("Error generating header style", "error", err)
} else {
err = xlsx.SetRowStyle(sheetName, 1, 1, headerStyle)
if err != nil {
logger.Error("Error setting header style", "error", err)
}
}
// Populate the Excel file with data from the Inventory table
for i, item := range results {
v = reflect.ValueOf(item)
for j := 0; j < v.NumField(); j++ {
column := string(rune('A'+j)) + strconv.Itoa(i+2) // Start from row 2
value := getFieldValue(v.Field(j))
xlsx.SetCellValue(sheetName, column, value)
}
}
// Freeze top row
err = xlsx.SetPanes(sheetName, &excelize.Panes{
Freeze: true,
Split: false,
XSplit: 0,
YSplit: 1,
TopLeftCell: "A2",
ActivePane: "bottomLeft",
Selection: []excelize.Selection{
{SQRef: "A2", ActiveCell: "A2", Pane: "bottomLeft"},
},
})
if err != nil {
logger.Error("Error freezing top row", "error", err)
}
// Set column autowidth
/*
err = SetColAutoWidth(xlsx, sheetName)
if err != nil {
fmt.Printf("Error setting auto width : '%s'\n", err)
}
*/
// Save the Excel file into a byte buffer
if err := xlsx.Write(&buffer); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// Helper function to get the actual value of sql.Null types
func getFieldValue(field reflect.Value) interface{} {
switch field.Kind() {
case reflect.Struct:
// Handle sql.Null types based on their concrete type
switch field.Interface().(type) {
case sql.NullString:
ns := field.Interface().(sql.NullString)
if ns.Valid {
return ns.String
}
return ""
case sql.NullInt64:
ni := field.Interface().(sql.NullInt64)
if ni.Valid {
return ni.Int64
}
return -1
case sql.NullFloat64:
nf := field.Interface().(sql.NullFloat64)
if nf.Valid {
return nf.Float64
}
return nil
case sql.NullBool:
nb := field.Interface().(sql.NullBool)
if nb.Valid {
return nb.Bool
}
return false
}
}
return field.Interface() // Return the value as-is for non-sql.Null types
}
// Taken from https://github.com/qax-os/excelize/issues/92#issuecomment-821578446
func SetColAutoWidth(xlsx *excelize.File, sheetName string) error {
// Autofit all columns according to their text content
cols, err := xlsx.GetCols(sheetName)
if err != nil {
return err
}
for idx, col := range cols {
largestWidth := 0
for _, rowCell := range col {
cellWidth := utf8.RuneCountInString(rowCell) + 2 // + 2 for margin
if cellWidth > largestWidth {
largestWidth = cellWidth
}
}
//fmt.Printf("SetColAutoWidth calculated largest width for column index '%d' is '%d'\n", idx, largestWidth)
name, err := excelize.ColumnNumberToName(idx + 1)
if err != nil {
return err
}
xlsx.SetColWidth(sheetName, name, name, float64(largestWidth))
}
// No errors at this point
return nil
}

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"log/slog"
"os"
"wnzl-snow/internal/utils"
"mocksnow/internal/utils"
"gopkg.in/yaml.v2"
)

View File

@@ -2,8 +2,8 @@ package tasks
import (
"log/slog"
"wnzl-snow/db"
"wnzl-snow/internal/settings"
"mocksnow/db"
"mocksnow/internal/settings"
)
// CronTask stores runtime information to be used by tasks

View File

@@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"reflect"
"time"
)
@@ -66,3 +67,40 @@ func SleepWithContext(ctx context.Context, d time.Duration) {
case <-timer.C:
}
}
// Generic converter using reflection
func ConvertStruct(src interface{}, dst interface{}) {
srcVal := reflect.ValueOf(src)
srcType := reflect.TypeOf(src)
dstVal := reflect.ValueOf(dst).Elem()
for i := 0; i < srcVal.NumField(); i++ {
srcField := srcVal.Field(i)
dstField := dstVal.FieldByName(srcType.Field(i).Name)
if !dstField.IsValid() || !dstField.CanSet() {
continue
}
switch srcField.Type().Name() {
case "NullString":
if srcField.FieldByName("Valid").Bool() {
dstField.SetString(srcField.FieldByName("String").String())
} else {
dstField.SetString("")
}
case "NullTime":
if srcField.FieldByName("Valid").Bool() {
t := srcField.FieldByName("Time").Interface().(time.Time)
dstField.SetString(t.Format("2006-01-02 15:04:05"))
} else {
dstField.SetString("")
}
default:
// Handle int64 -> int conversion
if srcField.Kind() == reflect.Int64 && dstField.Kind() == reflect.Int {
dstField.SetInt(srcField.Int())
}
}
}
}

12
main.go
View File

@@ -5,12 +5,12 @@ import (
"os"
"runtime"
"time"
"wnzl-snow/db"
"wnzl-snow/internal/settings"
utils "wnzl-snow/internal/utils"
"wnzl-snow/log"
"wnzl-snow/server"
"wnzl-snow/server/router"
"mocksnow/db"
"mocksnow/internal/settings"
utils "mocksnow/internal/utils"
"mocksnow/log"
"mocksnow/server"
"mocksnow/server/router"
"github.com/go-co-op/gocron/v2"
"github.com/joho/godotenv"

View File

@@ -4,9 +4,9 @@ import (
"context"
"log/slog"
"net/http"
"wnzl-snow/db"
"wnzl-snow/internal/secrets"
"wnzl-snow/internal/settings"
"mocksnow/db"
"mocksnow/internal/secrets"
"mocksnow/internal/settings"
"github.com/a-h/templ"
)

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"runtime"
"time"
"wnzl-snow/components/views"
"mocksnow/components/views"
)
// Home handles the home page.

View File

@@ -5,80 +5,16 @@ import (
"encoding/json"
"fmt"
"net/http"
"wnzl-snow/components/views"
"mocksnow/components/views"
"mocksnow/db/queries"
"mocksnow/internal/utils"
)
func (h *Handler) RenderIncomingTable(w http.ResponseWriter, r *http.Request) {
/*
db, err := sql.Open("sqlite3", "./your.db")
if err != nil {
http.Error(w, "Failed to connect to the database", http.StatusInternalServerError)
return
}
defer db.Close()
query := `
SELECT
id,
incident_number,
description,
short_description,
urgency,
impact,
state,
external_id,
work_notes,
assignment_group,
assigned_to,
category,
subcategory,
created_at
FROM incoming
ORDER BY created_at DESC
`
rows, err := db.Query(query)
if err != nil {
http.Error(w, "Failed to query the database", http.StatusInternalServerError)
return
}
defer rows.Close()
var results []views.IncomingRow
for rows.Next() {
var row views.IncomingRow
err := rows.Scan(
&row.ID,
&row.IncidentNumber,
&row.Description,
&row.ShortDescription,
&row.Urgency,
&row.Impact,
&row.State,
&row.ExternalID,
&row.WorkNotes,
&row.AssignmentGroup,
&row.AssignedTo,
&row.Category,
&row.Subcategory,
&row.CreatedAt,
)
if err != nil {
log.Printf("Scan error: %v", err)
continue
}
results = append(results, row)
}
if err := rows.Err(); err != nil {
log.Printf("Row iteration error: %v", err)
}
*/
ctx := context.Background()
h.Logger.Debug("Querying updates table")
results, err := h.Database.Queries().GetReportUpdates(ctx)
results, err := h.Database.Queries().ListIncoming(ctx)
if err != nil {
h.Logger.Error("Unable to query incoming table", "error", err)
w.Header().Set("Content-Type", "application/json")
@@ -101,5 +37,16 @@ func (h *Handler) RenderIncomingTable(w http.ResponseWriter, r *http.Request) {
return
}
views.IncomingTable(results).Render(r.Context(), w)
views.IncomingTable(ConvertIncomingList(results)).Render(r.Context(), w)
}
// Converts a slice of Incoming to []IncomingRow
func ConvertIncomingList(list []queries.Incoming) []views.IncomingRow {
rows := make([]views.IncomingRow, 0, len(list))
for _, in := range list {
var row views.IncomingRow
utils.ConvertStruct(in, &row)
rows = append(rows, row)
}
return rows
}

View File

@@ -1,9 +1,11 @@
package handler
import (
"encoding/json"
"fmt"
"io"
"net/http"
"mocksnow/server/models"
)
// NewSnow receives data from the DMSP Snow New() function
@@ -19,4 +21,15 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
} else {
h.Logger.Debug("received input data", "length", len(reqBody))
}
// Decode the JSON body
var inc models.Incident
if err := json.Unmarshal(reqBody, &inc); err != nil {
h.Logger.Error("unable to decode json", "error", err)
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
} else {
h.Logger.Debug("successfully decoded JSON")
prettyPrint(inc)
}
}

View File

@@ -1,61 +0,0 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"net/http"
"wnzl-snow/internal/report"
)
func (h *Handler) InventoryReportDownload(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
// Generate the XLSX report
reportData, err := report.CreateInventoryReport(h.Logger, h.Database, ctx)
if err != nil {
h.Logger.Error("Failed to create report", "error", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Unable to create xlsx report: '%s'", err),
})
return
}
// Set HTTP headers to indicate file download
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", `attachment; filename="inventory_report.xlsx"`)
w.Header().Set("File-Name", "inventory_report.xlsx")
// Write the XLSX file to the HTTP response
w.Write(reportData)
}
func (h *Handler) UpdateReportDownload(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
// Generate the XLSX report
reportData, err := report.CreateUpdatesReport(h.Logger, h.Database, ctx)
if err != nil {
h.Logger.Error("Failed to create report", "error", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Unable to create xlsx report: '%s'", err),
})
return
}
// Set HTTP headers to indicate file download
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", `attachment; filename="updates_report.xlsx"`)
w.Header().Set("File-Name", "updates_report.xlsx")
// Write the XLSX file to the HTTP response
w.Write(reportData)
}

View File

@@ -1,42 +0,0 @@
package handler
import (
"context"
"fmt"
"net/http"
)
// 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)
*/
//err := h.Database.Queries().InventoryCleanupTemplates(context.Background())
err := h.Database.Queries().CleanupUpdatesNullVm(context.Background())
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")
}
}

View File

@@ -2,7 +2,7 @@ package middleware
import (
"net/http"
"wnzl-snow/version"
"mocksnow/version"
)
// CacheMiddleware sets the Cache-Control header based on the version.

View File

@@ -1,9 +1,5 @@
package models
import (
"encoding/json"
)
type IncidentResponse struct {
ImportSet string `json:"import_set"`
StagingTable string `json:"staging_table"`
@@ -37,256 +33,3 @@ type Incident struct {
Category string `json:"category,omitempty"`
SubCategory string `json:"subcategory,omitempty"`
}
type CloudEventReceived struct {
CloudEvent struct {
ID string `json:"id"`
Specversion string `json:"specversion"`
Source string `json:"source"`
Type string `json:"type"`
Time string `json:"time"` // Modified from time.Time
Data struct {
ChainID int `json:"ChainId"`
ChangeTag string `json:"ChangeTag"`
ComputeResource struct {
ComputeResource struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"ComputeResource"`
Name string `json:"Name"`
} `json:"ComputeResource"`
CreatedTime string `json:"CreatedTime"` // Modified from time.Time
Datacenter struct {
Datacenter struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Datacenter"`
Name string `json:"Name"`
} `json:"Datacenter"`
Ds interface{} `json:"Ds"`
Dvs interface{} `json:"Dvs"`
FullFormattedMessage string `json:"FullFormattedMessage"`
Host struct {
Host struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Host"`
Name string `json:"Name"`
} `json:"Host"`
Key int `json:"Key"`
Net interface{} `json:"Net"`
NewParent *CloudEventResourcePool `json:"NewParent"`
OldParent *CloudEventResourcePool `json:"OldParent"`
SrcTemplate *CloudEventVm `json:"SrcTemplate"`
Template bool `json:"Template"`
UserName string `json:"UserName"`
VM struct {
Name string `json:"Name"`
VM struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Vm"`
} `json:"Vm"`
ConfigSpec *json.RawMessage `json:"configSpec"`
ConfigChanges *ConfigChangesReceived `json:"configChanges"` // Modified to separate struct
} `json:"data"`
} `json:"cloudEvent"`
}
type CloudEventResourcePool struct {
Name string `json:"Name"`
ResourcePool struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"ResourcePool"`
}
type CloudEventVm struct {
Name string `json:"Name"`
VM struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Vm"`
}
type ImportReceived struct {
Name string `json:"Name"`
Vcenter string `json:"Vcenter"`
VmId string `json:"VmId"`
InitialRam int `json:"InitialRam"`
PowerState int `json:"PowerState"`
CreationTime int `json:"CreationTime"`
InitialVcpus int `json:"InitialVcpus"`
ProvisionedDisk float64 `json:"ProvisionedDisk"`
Folder string `json:"Folder"`
ResourcePool string `json:"ResourcePool"`
Datacenter string `json:"Datacenter"`
Cluster string `json:"Cluster"`
}
type ConfigChangesReceived struct {
Modified string `json:"modified"`
}
// 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 string `json:"CreateDate"` // Modified from time.Time
Crypto any `json:"Crypto"`
DeviceChange []struct {
Backing any `json:"Backing"`
Device struct {
Backing *BackingSpec `json:"Backing,omitempty"`
CapacityInBytes int `json:"CapacityInBytes"`
CapacityInKB int `json:"CapacityInKB"`
Connectable struct {
AllowGuestControl bool `json:"AllowGuestControl"`
Connected bool `json:"Connected"`
MigrateConnect string `json:"MigrateConnect"`
StartConnected bool `json:"StartConnected"`
Status string `json:"Status"`
} `json:"Connectable"`
ControllerKey int `json:"ControllerKey"`
DeviceInfo struct {
Label string `json:"Label"`
Summary string `json:"Summary"`
} `json:"DeviceInfo"`
ExternalID string `json:"ExternalId"`
MacAddress string `json:"MacAddress"`
ResourceAllocation struct {
Limit int `json:"Limit"`
Reservation int `json:"Reservation"`
Share struct {
Level string `json:"Level"`
Shares int `json:"Shares"`
} `json:"Share"`
} `json:"ResourceAllocation"`
SlotInfo any `json:"SlotInfo"`
UnitNumber int `json:"UnitNumber"`
UptCompatibilityEnabled bool `json:"UptCompatibilityEnabled"`
WakeOnLanEnabled bool `json:"WakeOnLanEnabled"`
DiskObjectID string `json:"DiskObjectId"`
Iofilter any `json:"Iofilter"`
Key int `json:"Key"`
NativeUnmanagedLinkedClone any `json:"NativeUnmanagedLinkedClone"`
Shares any `json:"Shares"`
StorageIOAllocation struct {
Limit int `json:"Limit"`
Reservation any `json:"Reservation"`
Shares struct {
Level string `json:"Level"`
Shares int `json:"Shares"`
} `json:"Shares"`
} `json:"StorageIOAllocation"`
VDiskID any `json:"VDiskId"`
VFlashCacheConfigInfo any `json:"VFlashCacheConfigInfo"`
} `json:"Device,omitempty"`
FileOperation string `json:"FileOperation"`
Operation string `json:"Operation"`
Profile []struct {
ProfileData struct {
ExtensionKey string `json:"ExtensionKey"`
ObjectData string `json:"ObjectData"` // Modified from time.Time
} `json:"ProfileData"`
ProfileID string `json:"ProfileId"`
ProfileParams any `json:"ProfileParams"`
ReplicationSpec any `json:"ReplicationSpec"`
} `json:"Profile"`
} `json:"DeviceChange"`
ExtraConfig any `json:"ExtraConfig"`
Files struct {
FtMetadataDirectory string `json:"FtMetadataDirectory"`
LogDirectory string `json:"LogDirectory"`
SnapshotDirectory string `json:"SnapshotDirectory"`
SuspendDirectory string `json:"SuspendDirectory"`
VMPathName string `json:"VmPathName"`
} `json:"Files"`
Firmware string `json:"Firmware"`
Flags any `json:"Flags"`
FtInfo any `json:"FtInfo"`
GuestAutoLockEnabled any `json:"GuestAutoLockEnabled"`
GuestID string `json:"GuestId"`
GuestMonitoringModeInfo any `json:"GuestMonitoringModeInfo"`
InstanceUUID string `json:"InstanceUuid"`
LatencySensitivity any `json:"LatencySensitivity"`
LocationID string `json:"LocationId"`
ManagedBy any `json:"ManagedBy"`
MaxMksConnections int `json:"MaxMksConnections"`
MemoryAffinity any `json:"MemoryAffinity"`
MemoryAllocation any `json:"MemoryAllocation"`
MemoryHotAddEnabled any `json:"MemoryHotAddEnabled"`
MemoryMB int `json:"MemoryMB"`
MemoryReservationLockedToMax any `json:"MemoryReservationLockedToMax"`
MessageBusTunnelEnabled any `json:"MessageBusTunnelEnabled"`
MigrateEncryption string `json:"MigrateEncryption"`
Name string `json:"Name"`
NestedHVEnabled any `json:"NestedHVEnabled"`
NetworkShaper any `json:"NetworkShaper"`
NpivDesiredNodeWwns int `json:"NpivDesiredNodeWwns"`
NpivDesiredPortWwns int `json:"NpivDesiredPortWwns"`
NpivNodeWorldWideName any `json:"NpivNodeWorldWideName"`
NpivOnNonRdmDisks any `json:"NpivOnNonRdmDisks"`
NpivPortWorldWideName any `json:"NpivPortWorldWideName"`
NpivTemporaryDisabled any `json:"NpivTemporaryDisabled"`
NpivWorldWideNameOp string `json:"NpivWorldWideNameOp"`
NpivWorldWideNameType string `json:"NpivWorldWideNameType"`
NumCPUs int `json:"NumCPUs"`
NumCoresPerSocket int `json:"NumCoresPerSocket"`
PowerOpInfo any `json:"PowerOpInfo"`
RepConfig any `json:"RepConfig"`
ScheduledHardwareUpgradeInfo any `json:"ScheduledHardwareUpgradeInfo"`
SevEnabled any `json:"SevEnabled"`
SgxInfo any `json:"SgxInfo"`
SwapPlacement string `json:"SwapPlacement"`
Tools any `json:"Tools"`
UUID string `json:"Uuid"`
VAppConfig any `json:"VAppConfig"`
VAppConfigRemoved any `json:"VAppConfigRemoved"`
VAssertsEnabled any `json:"VAssertsEnabled"`
VPMCEnabled any `json:"VPMCEnabled"`
VcpuConfig any `json:"VcpuConfig"`
Version string `json:"Version"`
VirtualICH7MPresent any `json:"VirtualICH7MPresent"`
VirtualSMCPresent any `json:"VirtualSMCPresent"`
VMProfile any `json:"VmProfile"`
}
type BackingSpec struct {
Port struct {
ConnectionCookie int `json:"ConnectionCookie"`
PortKey string `json:"PortKey"`
PortgroupKey string `json:"PortgroupKey"`
SwitchUUID string `json:"SwitchUuid"`
} `json:"Port"`
BackingObjectID string `json:"BackingObjectId"`
ChangeID string `json:"ChangeId"`
ContentID string `json:"ContentId"`
Datastore struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Datastore"`
DeltaDiskFormat string `json:"DeltaDiskFormat"`
DeltaDiskFormatVariant string `json:"DeltaDiskFormatVariant"`
DeltaGrainSize int `json:"DeltaGrainSize"`
DigestEnabled any `json:"DigestEnabled"`
DiskMode string `json:"DiskMode"`
EagerlyScrub bool `json:"EagerlyScrub"`
FileName string `json:"FileName"`
KeyID any `json:"KeyId"`
Parent any `json:"Parent"`
Sharing string `json:"Sharing"`
Split any `json:"Split"`
ThinProvisioned bool `json:"ThinProvisioned"`
UUID string `json:"Uuid"`
WriteThrough any `json:"WriteThrough"`
}

View File

@@ -4,11 +4,11 @@ import (
"log/slog"
"net/http"
"net/http/pprof"
"wnzl-snow/db"
"wnzl-snow/dist"
"wnzl-snow/internal/settings"
"wnzl-snow/server/handler"
"wnzl-snow/server/middleware"
"mocksnow/db"
"mocksnow/dist"
"mocksnow/internal/settings"
"mocksnow/server/handler"
"mocksnow/server/middleware"
)
func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver string, goVersion string, settings *settings.Settings) http.Handler {

0
styles/.!31660!.DS_Store Normal file
View File

View File

@@ -7,4 +7,4 @@ fi
new_module=$1
find . -type d -name .git -prune -o -type f -exec sed -i '' -e "s/wnzl-snow/${new_module}/g" {} \;
find . -type d -name .git -prune -o -type f -exec sed -i '' -e "s/mocksnow/${new_module}/g" {} \;