Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions base/database/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package database

import (
"app/base"
"app/base/inventory"
"app/base/models"
"app/base/utils"
"fmt"
Expand All @@ -16,16 +15,9 @@ import (
)

const (
TestWorkspace1ID = "aaaaaaaa-0000-0000-0000-000000000001"
TestWorkspace2ID = "aaaaaaaa-0000-0000-0000-000000000002"
TestWorkspaceOtherID = "bbbbbbbb-0000-0000-0000-000000000003"
TestWorkspace1ID = "aaaaaaaa-0000-0000-0000-000000000001"
)

func TestWorkspacesGroup1() *inventory.Groups {
g := inventory.Groups{{ID: TestWorkspace1ID, Name: "group1"}}
return &g
}

func TestWorkspace1IDPtr() *uuid.UUID {
id := uuid.MustParse(TestWorkspace1ID)
return &id
Expand Down
32 changes: 0 additions & 32 deletions base/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package inventory

import (
"app/base/types"
"database/sql/driver"
"encoding/json"
"errors"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -86,35 +83,6 @@ type Group struct {
Name string `json:"name"`
}

// Groups is a slice of Group that implements driver.Valuer and sql.Scanner
// for storing/loading as JSONB in the database (e.g. system_inventory.workspaces).
type Groups []Group

// Value implements driver.Valuer for GORM: marshal to JSON for DB write.
func (g *Groups) Value() (driver.Value, error) {
if g == nil {
return nil, nil
}
return json.Marshal(g)
}

// Scan implements sql.Scanner for GORM: unmarshal from JSON on DB read.
func (g *Groups) Scan(value interface{}) error {
if value == nil {
*g = nil
return nil
}
b, ok := value.([]byte)
if !ok {
return errors.New("inventory.Groups: type assertion to []byte failed")
}
if len(b) == 0 {
*g = nil
return nil
}
return json.Unmarshal(b, g)
}

type Workloads struct {
Sap SapWorkload `json:"sap,omitempty"`
Ansible AnsibleWorkload `json:"ansible,omitempty"`
Expand Down
10 changes: 4 additions & 6 deletions base/models/models.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"app/base/inventory"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -71,11 +70,10 @@ type SystemInventory struct {
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
Arch *string
Bootc bool
Tags []byte `gorm:"column:tags"`
Created time.Time // set by trigger system_platform_insert_trigger
Workspaces *inventory.Groups `gorm:"column:workspaces"`
WorkspaceID *uuid.UUID `gorm:"column:workspace_id"`
WorkspaceName *string `gorm:"column:workspace_name"`
Tags []byte `gorm:"column:tags"`
Created time.Time // set by trigger system_platform_insert_trigger
WorkspaceID *uuid.UUID `gorm:"column:workspace_id"`
WorkspaceName *string `gorm:"column:workspace_name"`
StaleTimestamp *time.Time
StaleWarningTimestamp *time.Time
CulledTimestamp *time.Time
Expand Down
4 changes: 0 additions & 4 deletions conf/workspace_backfill.env

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5500,134 +5500,6 @@ data:
"title": "System culling",
"type": "timeseries"
},
{
"datasource": {
"uid": "$datasource"
},
"description": "Workspace backfill CronJob: rows updated per interval, batches completed, and batch errors (Pushgateway job workspace_backfill).",
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "Count",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 10,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"links": [

],
"mappings": [

],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green"
},
{
"color": "red",
"value": 80
}
]
},
"unit": "none"
},
"overrides": [

]
},
"gridPos": {
"h": 6,
"w": 12,
"x": 12,
"y": 103
},
"id": 77,
"options": {
"legend": {
"calcs": [

],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "multi",
"sort": "desc"
}
},
"pluginVersion": "11.6.3",
"targets": [
{
"datasource": {
"uid": "$datasource"
},
"expr": "sum(increase(patchman_engine_workspace_backfill_rows_updated_total{}[$interval]))",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "rows updated",
"refId": "A"
},
{
"datasource": {
"uid": "$datasource"
},
"expr": "sum(increase(patchman_engine_workspace_backfill_batches_total{}[$interval]))",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "batches",
"refId": "B"
},
{
"datasource": {
"uid": "$datasource"
},
"expr": "sum(increase(patchman_engine_workspace_backfill_batch_errors_total{}[$interval]))",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "batch errors",
"refId": "C"
}
],
"title": "Workspace backfill",
"type": "timeseries"
},
{
"collapsed": false,
"gridPos": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ALTER TABLE system_inventory ADD COLUMN IF NOT EXISTS workspaces JSONB;

UPDATE system_inventory
SET workspaces = JSONB_BUILD_ARRAY(JSONB_BUILD_OBJECT('id', workspace_id, 'name', workspace_name))
WHERE workspace_id IS NOT NULL
AND workspace_name IS NOT NULL
AND workspaces IS NULL;

CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);

CREATE OR REPLACE FUNCTION refresh_account_advisory_caches_multi(advisory_ids_in INTEGER[] DEFAULT NULL,
rh_account_id_in INTEGER DEFAULT NULL)
RETURNS VOID AS
$refresh_account_advisory$
BEGIN
PERFORM aa.rh_account_id, aa.workspace_id, aa.advisory_id
FROM account_advisory aa
WHERE (aa.advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (aa.rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL)
FOR UPDATE OF aa;

WITH current_counts AS (
SELECT sa.advisory_id, sa.rh_account_id, (ws->>'id')::UUID AS workspace_id,
count(sa.*) FILTER (WHERE sa.status_id = 0) AS systems_installable,
count(sa.*) AS systems_applicable
FROM system_advisories sa
JOIN system_inventory si
ON sa.rh_account_id = si.rh_account_id AND sa.system_id = si.id
JOIN system_patch sp
ON si.id = sp.system_id AND sp.rh_account_id = si.rh_account_id
CROSS JOIN LATERAL jsonb_array_elements(si.workspaces) AS ws
WHERE sp.last_evaluation IS NOT NULL
AND si.stale = FALSE
AND si.workspaces IS NOT NULL
AND (sa.advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (si.rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL)
GROUP BY sa.advisory_id, sa.rh_account_id, (ws->>'id')::UUID
),
upserted AS (
INSERT INTO account_advisory (advisory_id, rh_account_id, workspace_id, systems_installable, systems_applicable)
SELECT advisory_id, rh_account_id, workspace_id, systems_installable, systems_applicable
FROM current_counts
ON CONFLICT (rh_account_id, workspace_id, advisory_id) DO UPDATE SET
systems_installable = EXCLUDED.systems_installable,
systems_applicable = EXCLUDED.systems_applicable
)
DELETE FROM account_advisory
WHERE (advisory_id, rh_account_id, workspace_id) NOT IN (SELECT advisory_id, rh_account_id, workspace_id FROM current_counts)
AND (advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL);
END;
$refresh_account_advisory$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
CREATE OR REPLACE FUNCTION refresh_account_advisory_caches_multi(advisory_ids_in INTEGER[] DEFAULT NULL,
rh_account_id_in INTEGER DEFAULT NULL)
RETURNS VOID AS
$refresh_account_advisory$
BEGIN
PERFORM aa.rh_account_id, aa.workspace_id, aa.advisory_id
FROM account_advisory aa
WHERE (aa.advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (aa.rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL)
FOR UPDATE OF aa;

WITH current_counts AS (
SELECT sa.advisory_id, sa.rh_account_id, si.workspace_id,
count(sa.*) FILTER (WHERE sa.status_id = 0) AS systems_installable,
count(sa.*) AS systems_applicable
FROM system_advisories sa
JOIN system_inventory si
ON sa.rh_account_id = si.rh_account_id AND sa.system_id = si.id
JOIN system_patch sp
ON si.id = sp.system_id AND sp.rh_account_id = si.rh_account_id
WHERE sp.last_evaluation IS NOT NULL
AND si.stale = FALSE
AND si.workspace_id IS NOT NULL
AND (sa.advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (si.rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL)
GROUP BY sa.advisory_id, sa.rh_account_id, si.workspace_id
),
upserted AS (
INSERT INTO account_advisory (advisory_id, rh_account_id, workspace_id, systems_installable, systems_applicable)
SELECT advisory_id, rh_account_id, workspace_id, systems_installable, systems_applicable
FROM current_counts
ON CONFLICT (rh_account_id, workspace_id, advisory_id) DO UPDATE SET
systems_installable = EXCLUDED.systems_installable,
systems_applicable = EXCLUDED.systems_applicable
)
DELETE FROM account_advisory
WHERE (advisory_id, rh_account_id, workspace_id) NOT IN (SELECT advisory_id, rh_account_id, workspace_id FROM current_counts)
AND (advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL);
END;
$refresh_account_advisory$ LANGUAGE plpgsql;

DROP INDEX IF EXISTS system_inventory_workspaces_index;

ALTER TABLE system_inventory DROP COLUMN IF EXISTS workspaces;
11 changes: 4 additions & 7 deletions database_admin/schema/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations


INSERT INTO schema_migrations
VALUES (159, false);
VALUES (160, false);

-- ---------------------------------------------------------------------------
-- Functions
Expand Down Expand Up @@ -182,21 +182,20 @@ BEGIN
FOR UPDATE OF aa;

WITH current_counts AS (
SELECT sa.advisory_id, sa.rh_account_id, (ws->>'id')::UUID AS workspace_id,
SELECT sa.advisory_id, sa.rh_account_id, si.workspace_id,
count(sa.*) FILTER (WHERE sa.status_id = 0) AS systems_installable,
count(sa.*) AS systems_applicable
FROM system_advisories sa
JOIN system_inventory si
ON sa.rh_account_id = si.rh_account_id AND sa.system_id = si.id
JOIN system_patch sp
ON si.id = sp.system_id AND sp.rh_account_id = si.rh_account_id
CROSS JOIN LATERAL jsonb_array_elements(si.workspaces) AS ws
WHERE sp.last_evaluation IS NOT NULL
AND si.stale = FALSE
AND si.workspaces IS NOT NULL
AND si.workspace_id IS NOT NULL
AND (sa.advisory_id = ANY (advisory_ids_in) OR advisory_ids_in IS NULL)
AND (si.rh_account_id = rh_account_id_in OR rh_account_id_in IS NULL)
GROUP BY sa.advisory_id, sa.rh_account_id, (ws->>'id')::UUID
GROUP BY sa.advisory_id, sa.rh_account_id, si.workspace_id
),
upserted AS (
INSERT INTO account_advisory (advisory_id, rh_account_id, workspace_id, systems_installable, systems_applicable)
Expand Down Expand Up @@ -655,7 +654,6 @@ CREATE TABLE IF NOT EXISTS system_inventory
ansible_workload_controller_version TEXT CHECK (NOT empty(ansible_workload_controller_version)),
mssql_workload BOOLEAN NOT NULL DEFAULT false,
mssql_workload_version TEXT CHECK (NOT empty(mssql_workload_version)),
workspaces JSONB,
workspace_id UUID,
workspace_name TEXT CHECK (NOT empty(workspace_name)),
PRIMARY KEY (rh_account_id, id),
Expand Down Expand Up @@ -689,7 +687,6 @@ SELECT create_table_partition_triggers('system_inventory_on_update',
CREATE INDEX IF NOT EXISTS system_inventory_inventory_id_idx ON system_inventory (inventory_id);
CREATE INDEX IF NOT EXISTS system_inventory_tags_index ON system_inventory USING GIN (tags JSONB_PATH_OPS);
CREATE INDEX IF NOT EXISTS system_inventory_stale_timestamp_index ON system_inventory (stale_timestamp);
CREATE INDEX IF NOT EXISTS system_inventory_workspaces_index ON system_inventory USING GIN (workspaces);
CREATE INDEX IF NOT EXISTS system_inventory_workspace_id_index ON system_inventory (workspace_id);

CREATE TABLE IF NOT EXISTS deleted_system
Expand Down
Loading
Loading