diff --git a/cmd/crowdsec/lpmetrics.go b/cmd/crowdsec/lpmetrics.go index 19e0ac798..cef905c91 100644 --- a/cmd/crowdsec/lpmetrics.go +++ b/cmd/crowdsec/lpmetrics.go @@ -125,7 +125,7 @@ func (m *MetricsProvider) metricsPayload() *models.AllMetrics { FeatureFlags: m.static.featureFlags, } - item0 := &models.LogProcessorsMetricsItems0{ + met := &models.LogProcessorsMetrics{ BaseMetrics: base, ConsoleOptions: m.static.consoleOptions, Datasources: m.static.datasourceMap, @@ -135,7 +135,7 @@ func (m *MetricsProvider) metricsPayload() *models.AllMetrics { // TODO: more metric details... ? return &models.AllMetrics{ - LogProcessors: []models.LogProcessorsMetrics{{item0}}, + LogProcessors: []*models.LogProcessorsMetrics{met}, } } @@ -153,7 +153,7 @@ func (m *MetricsProvider) Run(ctx context.Context, myTomb *tomb.Tomb) error { for { select { case <-ticker.C: - met.LogProcessors[0][0].Meta.UtcNowTimestamp = time.Now().Unix() + met.LogProcessors[0].Meta.UtcNowTimestamp = time.Now().Unix() ctxTime, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() diff --git a/pkg/apiserver/apic_metrics.go b/pkg/apiserver/apic_metrics.go index 6b32f74c3..9db8c80e7 100644 --- a/pkg/apiserver/apic_metrics.go +++ b/pkg/apiserver/apic_metrics.go @@ -42,7 +42,7 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) { for _, lpsMetric := range lpsMetrics { lpName := lpsMetric.GeneratedBy - metrics := models.LogProcessorsMetricsItems0{} + metrics := models.LogProcessorsMetrics{} err := json.Unmarshal([]byte(lpsMetric.Payload), &metrics) if err != nil { @@ -78,13 +78,13 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) { metrics.Name = lpName metrics.LastPush = lp.LastPush.UTC().Unix() - allMetrics.LogProcessors = append(allMetrics.LogProcessors, models.LogProcessorsMetrics{&metrics}) + allMetrics.LogProcessors = append(allMetrics.LogProcessors, &metrics) metricsIds = append(metricsIds, lpsMetric.ID) } for _, bouncersMetric := range bouncersMetrics { bouncerName := bouncersMetric.GeneratedBy - metrics := models.RemediationComponentsMetricsItems0{} + metrics := models.RemediationComponentsMetrics{} err := json.Unmarshal([]byte(bouncersMetric.Payload), &metrics) if err != nil { @@ -114,7 +114,7 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) { metrics.Name = bouncerName metrics.LastPull = bouncer.LastPull.UTC().Unix() - allMetrics.RemediationComponents = append(allMetrics.RemediationComponents, models.RemediationComponentsMetrics{&metrics}) + allMetrics.RemediationComponents = append(allMetrics.RemediationComponents, &metrics) metricsIds = append(metricsIds, bouncersMetric.ID) } diff --git a/pkg/apiserver/controllers/v1/usagemetrics.go b/pkg/apiserver/controllers/v1/usagemetrics.go index d630943fd..1016ce3df 100644 --- a/pkg/apiserver/controllers/v1/usagemetrics.go +++ b/pkg/apiserver/controllers/v1/usagemetrics.go @@ -83,7 +83,7 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { case 1: // the final slice can't have more than one item, // guaranteed by the swagger schema - item0 := input.LogProcessors[0][0] + item0 := input.LogProcessors[0] payload = map[string]any{ "console_options": item0.ConsoleOptions, "datasources": item0.Datasources, @@ -103,7 +103,7 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { case 0: break case 1: - item0 := input.RemediationComponents[0][0] + item0 := input.RemediationComponents[0] payload = map[string]any{ "type": item0.Type, "metrics": item0.Metrics, diff --git a/pkg/models/all_metrics.go b/pkg/models/all_metrics.go index 27f4ead8c..4f0b958bd 100644 --- a/pkg/models/all_metrics.go +++ b/pkg/models/all_metrics.go @@ -20,10 +20,10 @@ import ( type AllMetrics struct { // log processors metrics - LogProcessors []LogProcessorsMetrics `json:"log_processors"` + LogProcessors []*LogProcessorsMetrics `json:"log_processors"` // remediation components metrics - RemediationComponents []RemediationComponentsMetrics `json:"remediation_components"` + RemediationComponents []*RemediationComponentsMetrics `json:"remediation_components"` } // Validate validates this all metrics @@ -50,14 +50,19 @@ func (m *AllMetrics) validateLogProcessors(formats strfmt.Registry) error { } for i := 0; i < len(m.LogProcessors); i++ { + if swag.IsZero(m.LogProcessors[i]) { // not required + continue + } - if err := m.LogProcessors[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("log_processors" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("log_processors" + "." + strconv.Itoa(i)) + if m.LogProcessors[i] != nil { + if err := m.LogProcessors[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("log_processors" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("log_processors" + "." + strconv.Itoa(i)) + } + return err } - return err } } @@ -71,14 +76,19 @@ func (m *AllMetrics) validateRemediationComponents(formats strfmt.Registry) erro } for i := 0; i < len(m.RemediationComponents); i++ { + if swag.IsZero(m.RemediationComponents[i]) { // not required + continue + } - if err := m.RemediationComponents[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("remediation_components" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + if m.RemediationComponents[i] != nil { + if err := m.RemediationComponents[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + } + return err } - return err } } @@ -108,13 +118,20 @@ func (m *AllMetrics) contextValidateLogProcessors(ctx context.Context, formats s for i := 0; i < len(m.LogProcessors); i++ { - if err := m.LogProcessors[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("log_processors" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("log_processors" + "." + strconv.Itoa(i)) + if m.LogProcessors[i] != nil { + + if swag.IsZero(m.LogProcessors[i]) { // not required + return nil + } + + if err := m.LogProcessors[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("log_processors" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("log_processors" + "." + strconv.Itoa(i)) + } + return err } - return err } } @@ -126,13 +143,20 @@ func (m *AllMetrics) contextValidateRemediationComponents(ctx context.Context, f for i := 0; i < len(m.RemediationComponents); i++ { - if err := m.RemediationComponents[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("remediation_components" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + if m.RemediationComponents[i] != nil { + + if swag.IsZero(m.RemediationComponents[i]) { // not required + return nil + } + + if err := m.RemediationComponents[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("remediation_components" + "." + strconv.Itoa(i)) + } + return err } - return err } } diff --git a/pkg/models/localapi_swagger.yaml b/pkg/models/localapi_swagger.yaml index 4485490ee..55ef9e296 100644 --- a/pkg/models/localapi_swagger.yaml +++ b/pkg/models/localapi_swagger.yaml @@ -1026,46 +1026,45 @@ definitions: type: string RemediationComponentsMetrics: title: RemediationComponentsMetrics - type: array - maxItems: 1 - items: - allOf: - - $ref: '#/definitions/BaseMetrics' - - type: object - properties: - type: - type: string - description: type of the remediation component - name: - type: string - description: name of the remediation component - last_pull: - type: integer - description: last pull date + type: object + allOf: + - $ref: '#/definitions/BaseMetrics' + - properties: + type: + type: string + description: type of the remediation component + name: + type: string + description: name of the remediation component + last_pull: + type: integer + description: last pull date LogProcessorsMetrics: title: LogProcessorsMetrics - type: array - maxItems: 1 - items: - allOf: - - $ref: '#/definitions/BaseMetrics' - - type: object - properties: - console_options: - $ref: '#/definitions/ConsoleOptions' - hub_items: - $ref: '#/definitions/HubItems' - datasources: - type: object - description: Number of datasources per type - additionalProperties: - type: integer - name: - type: string - description: name of the log processor - last_push: + type: object + allOf: + - $ref: '#/definitions/BaseMetrics' + - properties: + console_options: + $ref: '#/definitions/ConsoleOptions' + hub_items: + $ref: '#/definitions/HubItems' + datasources: + type: object + description: Number of datasources per type + additionalProperties: type: integer - description: last push date + name: + type: string + description: name of the log processor + last_push: + type: integer + description: last push date + #items: + # allOf: + # - $ref: '#/definitions/BaseMetrics' + # - type: object + AllMetrics: title: AllMetrics type: object diff --git a/pkg/models/log_processors_metrics.go b/pkg/models/log_processors_metrics.go index f605e4ea4..c8c305d76 100644 --- a/pkg/models/log_processors_metrics.go +++ b/pkg/models/log_processors_metrics.go @@ -7,87 +7,16 @@ package models import ( "context" - "strconv" "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" - "github.com/go-openapi/validate" ) // LogProcessorsMetrics LogProcessorsMetrics // // swagger:model LogProcessorsMetrics -type LogProcessorsMetrics []*LogProcessorsMetricsItems0 - -// Validate validates this log processors metrics -func (m LogProcessorsMetrics) Validate(formats strfmt.Registry) error { - var res []error - - iLogProcessorsMetricsSize := int64(len(m)) - - if err := validate.MaxItems("", "body", iLogProcessorsMetricsSize, 1); err != nil { - return err - } - - for i := 0; i < len(m); i++ { - if swag.IsZero(m[i]) { // not required - continue - } - - if m[i] != nil { - if err := m[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName(strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName(strconv.Itoa(i)) - } - return err - } - } - - } - - if len(res) > 0 { - return errors.CompositeValidationError(res...) - } - return nil -} - -// ContextValidate validate this log processors metrics based on the context it is used -func (m LogProcessorsMetrics) ContextValidate(ctx context.Context, formats strfmt.Registry) error { - var res []error - - for i := 0; i < len(m); i++ { - - if m[i] != nil { - - if swag.IsZero(m[i]) { // not required - return nil - } - - if err := m[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName(strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName(strconv.Itoa(i)) - } - return err - } - } - - } - - if len(res) > 0 { - return errors.CompositeValidationError(res...) - } - return nil -} - -// LogProcessorsMetricsItems0 log processors metrics items0 -// -// swagger:model LogProcessorsMetricsItems0 -type LogProcessorsMetricsItems0 struct { +type LogProcessorsMetrics struct { BaseMetrics // console options @@ -107,7 +36,7 @@ type LogProcessorsMetricsItems0 struct { } // UnmarshalJSON unmarshals this object from a JSON structure -func (m *LogProcessorsMetricsItems0) UnmarshalJSON(raw []byte) error { +func (m *LogProcessorsMetrics) UnmarshalJSON(raw []byte) error { // AO0 var aO0 BaseMetrics if err := swag.ReadJSON(raw, &aO0); err != nil { @@ -145,7 +74,7 @@ func (m *LogProcessorsMetricsItems0) UnmarshalJSON(raw []byte) error { } // MarshalJSON marshals this object to a JSON structure -func (m LogProcessorsMetricsItems0) MarshalJSON() ([]byte, error) { +func (m LogProcessorsMetrics) MarshalJSON() ([]byte, error) { _parts := make([][]byte, 0, 2) aO0, err := swag.WriteJSON(m.BaseMetrics) @@ -183,8 +112,8 @@ func (m LogProcessorsMetricsItems0) MarshalJSON() ([]byte, error) { return swag.ConcatJSON(_parts...), nil } -// Validate validates this log processors metrics items0 -func (m *LogProcessorsMetricsItems0) Validate(formats strfmt.Registry) error { +// Validate validates this log processors metrics +func (m *LogProcessorsMetrics) Validate(formats strfmt.Registry) error { var res []error // validation for a type composition with BaseMetrics @@ -206,7 +135,7 @@ func (m *LogProcessorsMetricsItems0) Validate(formats strfmt.Registry) error { return nil } -func (m *LogProcessorsMetricsItems0) validateConsoleOptions(formats strfmt.Registry) error { +func (m *LogProcessorsMetrics) validateConsoleOptions(formats strfmt.Registry) error { if swag.IsZero(m.ConsoleOptions) { // not required return nil @@ -224,7 +153,7 @@ func (m *LogProcessorsMetricsItems0) validateConsoleOptions(formats strfmt.Regis return nil } -func (m *LogProcessorsMetricsItems0) validateHubItems(formats strfmt.Registry) error { +func (m *LogProcessorsMetrics) validateHubItems(formats strfmt.Registry) error { if swag.IsZero(m.HubItems) { // not required return nil @@ -244,8 +173,8 @@ func (m *LogProcessorsMetricsItems0) validateHubItems(formats strfmt.Registry) e return nil } -// ContextValidate validate this log processors metrics items0 based on the context it is used -func (m *LogProcessorsMetricsItems0) ContextValidate(ctx context.Context, formats strfmt.Registry) error { +// ContextValidate validate this log processors metrics based on the context it is used +func (m *LogProcessorsMetrics) ContextValidate(ctx context.Context, formats strfmt.Registry) error { var res []error // validation for a type composition with BaseMetrics @@ -267,7 +196,7 @@ func (m *LogProcessorsMetricsItems0) ContextValidate(ctx context.Context, format return nil } -func (m *LogProcessorsMetricsItems0) contextValidateConsoleOptions(ctx context.Context, formats strfmt.Registry) error { +func (m *LogProcessorsMetrics) contextValidateConsoleOptions(ctx context.Context, formats strfmt.Registry) error { if err := m.ConsoleOptions.ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { @@ -281,7 +210,7 @@ func (m *LogProcessorsMetricsItems0) contextValidateConsoleOptions(ctx context.C return nil } -func (m *LogProcessorsMetricsItems0) contextValidateHubItems(ctx context.Context, formats strfmt.Registry) error { +func (m *LogProcessorsMetrics) contextValidateHubItems(ctx context.Context, formats strfmt.Registry) error { if swag.IsZero(m.HubItems) { // not required return nil @@ -300,7 +229,7 @@ func (m *LogProcessorsMetricsItems0) contextValidateHubItems(ctx context.Context } // MarshalBinary interface implementation -func (m *LogProcessorsMetricsItems0) MarshalBinary() ([]byte, error) { +func (m *LogProcessorsMetrics) MarshalBinary() ([]byte, error) { if m == nil { return nil, nil } @@ -308,8 +237,8 @@ func (m *LogProcessorsMetricsItems0) MarshalBinary() ([]byte, error) { } // UnmarshalBinary interface implementation -func (m *LogProcessorsMetricsItems0) UnmarshalBinary(b []byte) error { - var res LogProcessorsMetricsItems0 +func (m *LogProcessorsMetrics) UnmarshalBinary(b []byte) error { + var res LogProcessorsMetrics if err := swag.ReadJSON(b, &res); err != nil { return err } diff --git a/pkg/models/remediation_components_metrics.go b/pkg/models/remediation_components_metrics.go index f3b5a0839..ba3845d87 100644 --- a/pkg/models/remediation_components_metrics.go +++ b/pkg/models/remediation_components_metrics.go @@ -7,87 +7,16 @@ package models import ( "context" - "strconv" "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" - "github.com/go-openapi/validate" ) // RemediationComponentsMetrics RemediationComponentsMetrics // // swagger:model RemediationComponentsMetrics -type RemediationComponentsMetrics []*RemediationComponentsMetricsItems0 - -// Validate validates this remediation components metrics -func (m RemediationComponentsMetrics) Validate(formats strfmt.Registry) error { - var res []error - - iRemediationComponentsMetricsSize := int64(len(m)) - - if err := validate.MaxItems("", "body", iRemediationComponentsMetricsSize, 1); err != nil { - return err - } - - for i := 0; i < len(m); i++ { - if swag.IsZero(m[i]) { // not required - continue - } - - if m[i] != nil { - if err := m[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName(strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName(strconv.Itoa(i)) - } - return err - } - } - - } - - if len(res) > 0 { - return errors.CompositeValidationError(res...) - } - return nil -} - -// ContextValidate validate this remediation components metrics based on the context it is used -func (m RemediationComponentsMetrics) ContextValidate(ctx context.Context, formats strfmt.Registry) error { - var res []error - - for i := 0; i < len(m); i++ { - - if m[i] != nil { - - if swag.IsZero(m[i]) { // not required - return nil - } - - if err := m[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName(strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName(strconv.Itoa(i)) - } - return err - } - } - - } - - if len(res) > 0 { - return errors.CompositeValidationError(res...) - } - return nil -} - -// RemediationComponentsMetricsItems0 remediation components metrics items0 -// -// swagger:model RemediationComponentsMetricsItems0 -type RemediationComponentsMetricsItems0 struct { +type RemediationComponentsMetrics struct { BaseMetrics // last pull date @@ -101,7 +30,7 @@ type RemediationComponentsMetricsItems0 struct { } // UnmarshalJSON unmarshals this object from a JSON structure -func (m *RemediationComponentsMetricsItems0) UnmarshalJSON(raw []byte) error { +func (m *RemediationComponentsMetrics) UnmarshalJSON(raw []byte) error { // AO0 var aO0 BaseMetrics if err := swag.ReadJSON(raw, &aO0); err != nil { @@ -131,7 +60,7 @@ func (m *RemediationComponentsMetricsItems0) UnmarshalJSON(raw []byte) error { } // MarshalJSON marshals this object to a JSON structure -func (m RemediationComponentsMetricsItems0) MarshalJSON() ([]byte, error) { +func (m RemediationComponentsMetrics) MarshalJSON() ([]byte, error) { _parts := make([][]byte, 0, 2) aO0, err := swag.WriteJSON(m.BaseMetrics) @@ -161,8 +90,8 @@ func (m RemediationComponentsMetricsItems0) MarshalJSON() ([]byte, error) { return swag.ConcatJSON(_parts...), nil } -// Validate validates this remediation components metrics items0 -func (m *RemediationComponentsMetricsItems0) Validate(formats strfmt.Registry) error { +// Validate validates this remediation components metrics +func (m *RemediationComponentsMetrics) Validate(formats strfmt.Registry) error { var res []error // validation for a type composition with BaseMetrics @@ -176,8 +105,8 @@ func (m *RemediationComponentsMetricsItems0) Validate(formats strfmt.Registry) e return nil } -// ContextValidate validate this remediation components metrics items0 based on the context it is used -func (m *RemediationComponentsMetricsItems0) ContextValidate(ctx context.Context, formats strfmt.Registry) error { +// ContextValidate validate this remediation components metrics based on the context it is used +func (m *RemediationComponentsMetrics) ContextValidate(ctx context.Context, formats strfmt.Registry) error { var res []error // validation for a type composition with BaseMetrics @@ -192,7 +121,7 @@ func (m *RemediationComponentsMetricsItems0) ContextValidate(ctx context.Context } // MarshalBinary interface implementation -func (m *RemediationComponentsMetricsItems0) MarshalBinary() ([]byte, error) { +func (m *RemediationComponentsMetrics) MarshalBinary() ([]byte, error) { if m == nil { return nil, nil } @@ -200,8 +129,8 @@ func (m *RemediationComponentsMetricsItems0) MarshalBinary() ([]byte, error) { } // UnmarshalBinary interface implementation -func (m *RemediationComponentsMetricsItems0) UnmarshalBinary(b []byte) error { - var res RemediationComponentsMetricsItems0 +func (m *RemediationComponentsMetrics) UnmarshalBinary(b []byte) error { + var res RemediationComponentsMetrics if err := swag.ReadJSON(b, &res); err != nil { return err }