diff --git a/models/integrations.go b/models/integrations.go new file mode 100644 index 00000000..1014e585 --- /dev/null +++ b/models/integrations.go @@ -0,0 +1,43 @@ +package models + +import ( + "time" + + "github.com/google/uuid" +) + +type IntegrationType string + +const ( + IntegrationTypeScraper IntegrationType = "scrapers" + IntegrationTypeLoggingBackends IntegrationType = "logging_backends" + IntegrationTypeTopology IntegrationType = "topology" +) + +type Integration struct { + ID uuid.UUID `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Integration IntegrationType `json:"integration"` + Source string `json:"source"` + AgentID *uuid.UUID `json:"agent_id,omitempty"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt *time.Time `json:"deleted_at,omitempty"` + CreatedBy *uuid.UUID `json:"created_by,omitempty"` + JobName string `json:"job_name"` + JobSuccess int `json:"job_success_count"` + JobError int `json:"job_error_count"` + JobDetails string `json:"job_details"` + JobHostname string `json:"job_hostname"` + JobDuration int `json:"job_duration_millis"` + JobResource string `json:"job_resource_type"` + JobStatus string `json:"job_status"` + JobTimeStart time.Time `json:"job_time_start"` + JobTimeEnd time.Time `json:"job_time_end"` + JobCreatedAt time.Time `json:"job_created_at"` +} + +func (t *Integration) TableName() string { + return "integrations_with_status" +} diff --git a/models/logging_backends.go b/models/logging_backends.go index 1bc34634..800c0949 100644 --- a/models/logging_backends.go +++ b/models/logging_backends.go @@ -13,6 +13,7 @@ type LoggingBackend struct { Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"` Spec string `json:"spec,omitempty"` Source string `json:"source,omitempty"` + AgentID *uuid.UUID `json:"agent_id,omitempty"` CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` DeletedAt *time.Time `json:"deleted_at,omitempty"` diff --git a/models/source.go b/models/source.go index 5c33637b..4b43f527 100644 --- a/models/source.go +++ b/models/source.go @@ -4,4 +4,5 @@ const ( SourceCRD = "KubernetesCRD" SourceConfigFile = "ConfigFile" SourceUI = "UI" + SourceTopology = "Topology" ) diff --git a/models/topology.go b/models/topology.go index deeeb819..2911db7a 100644 --- a/models/topology.go +++ b/models/topology.go @@ -14,6 +14,7 @@ type Topology struct { Name string Namespace string Labels types.JSONStringMap `json:"labels,omitempty" gorm:"default:null"` + Source string `json:"source"` Spec types.JSON `gorm:"default:null"` Schedule *string CreatedAt *time.Time `json:"created_at,omitempty" time_format:"postgres_timestamp"` diff --git a/schema/components.hcl b/schema/components.hcl index 6d426303..2ac46e76 100644 --- a/schema/components.hcl +++ b/schema/components.hcl @@ -22,6 +22,11 @@ table "topologies" { null = true type = jsonb } + column "source" { + null = false + type = enum.source + default = "Topology" + } column "spec" { null = true type = jsonb diff --git a/schema/logging_backends.hcl b/schema/logging_backends.hcl index de451b2c..0a58a028 100644 --- a/schema/logging_backends.hcl +++ b/schema/logging_backends.hcl @@ -1,6 +1,6 @@ enum "source" { schema = schema.public - values = ["KubernetesCRD", "ConfigFile", "UI"] + values = ["KubernetesCRD", "ConfigFile", "UI", "Topology"] } table "logging_backends" { @@ -26,6 +26,10 @@ table "logging_backends" { null = true type = enum.source } + column "agent_id" { + null = true + type = uuid + } column "created_at" { null = true type = timestamptz diff --git a/tests/fixtures/dummy/topologies.go b/tests/fixtures/dummy/topologies.go index 4a5fa621..404d871f 100644 --- a/tests/fixtures/dummy/topologies.go +++ b/tests/fixtures/dummy/topologies.go @@ -9,6 +9,7 @@ var LogisticsTopology = models.Topology{ ID: uuid.MustParse("df39086e-506b-4ad9-9af7-baf5275c382b"), Name: "logistics", Namespace: "default", + Source: models.SourceUI, } var AllDummyTopologies = []models.Topology{ diff --git a/tests/integrations_test.go b/tests/integrations_test.go new file mode 100644 index 00000000..be915c9f --- /dev/null +++ b/tests/integrations_test.go @@ -0,0 +1,17 @@ +package tests + +import ( + "github.com/flanksource/duty/models" + "github.com/flanksource/duty/testutils" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Integration view", func() { + It("should be able to call integrations view", func() { + var integrations []models.Integration + err := testutils.DefaultContext.DB().Find(&integrations).Error + Expect(err).ToNot(HaveOccurred()) + Expect(len(integrations)).To(BeNumerically(">", 0)) + }) +}) diff --git a/views/015_job_history.sql b/views/015_job_history.sql index 12c2988b..6a5cee01 100644 --- a/views/015_job_history.sql +++ b/views/015_job_history.sql @@ -19,7 +19,8 @@ FROM AND job_history.created_at = latest_job_history.max_created_at; -- Topologies with job status -DROP VIEW IF EXISTS topologies_with_status; +DROP VIEW IF EXISTS topologies_with_status CASCADE; + CREATE OR REPLACE VIEW topologies_with_status AS SELECT @@ -104,7 +105,7 @@ WHERE teams.deleted_at IS NULL; -- Config scrapers View -DROP VIEW IF EXISTS config_scrapers_with_status; +DROP VIEW IF EXISTS config_scrapers_with_status CASCADE; CREATE OR REPLACE VIEW config_scrapers_with_status AS diff --git a/views/024_integrations.sql b/views/024_integrations.sql new file mode 100644 index 00000000..2d08d23a --- /dev/null +++ b/views/024_integrations.sql @@ -0,0 +1,76 @@ +CREATE +OR REPLACE VIEW integrations_with_status AS +SELECT + id, + NAME, + description, + 'scrapers' AS integration_type, + source, + agent_id, + created_at, + updated_at, + deleted_at, + created_by, + job_name, + job_success_count, + job_error_count, + job_details, + job_hostname, + job_duration_millis, + job_resource_type, + job_status, + job_time_start, + job_time_end, + job_created_at +FROM + config_scrapers_with_status +UNION +SELECT + id, + NAME, + '', + 'topologies' AS integration_type, + source, + agent_id, + created_at, + updated_at, + deleted_at, + created_by, + job_name, + job_success_count, + job_error_count, + job_details, + job_hostname, + job_duration_millis, + job_resource_type, + job_status, + job_time_start, + job_time_end, + job_created_at +FROM + topologies_with_status +UNION +SELECT + id, + NAME, + '', + 'logging_backends' AS integration_type, + source, + agent_id, + created_at, + updated_at, + deleted_at, + created_by, + '', + 0, + 0, + NULL, + '', + 0, + NULL, + '', + NULL, + NULL, + NULL +FROM + logging_backends; \ No newline at end of file