store more payload

This commit is contained in:
Sebastien Blot 2024-04-19 18:02:19 +02:00
parent 33778ca87f
commit 0be7cc3cfc
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A

View file

@ -3,19 +3,18 @@ package v1
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-openapi/strfmt" "github.com/go-openapi/strfmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/database/ent"
"github.com/crowdsecurity/crowdsec/pkg/database/ent" "github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric" "github.com/crowdsecurity/crowdsec/pkg/models"
) )
// updateBaseMetrics updates the base metrics for a machine or bouncer // updateBaseMetrics updates the base metrics for a machine or bouncer
func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, baseMetrics *models.BaseMetrics, hubItems *models.HubItems) error { func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, baseMetrics *models.BaseMetrics, hubItems *models.HubItems) error {
switch { switch {
@ -30,24 +29,23 @@ func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, b
return nil return nil
} }
// UsageMetrics receives metrics from log processors and remediation components // UsageMetrics receives metrics from log processors and remediation components
func (c *Controller) UsageMetrics(gctx *gin.Context) { func (c *Controller) UsageMetrics(gctx *gin.Context) {
var input models.AllMetrics var input models.AllMetrics
// parse the payload // parse the payload
if err := gctx.ShouldBindJSON(&input); err != nil { if err := gctx.ShouldBindJSON(&input); err != nil {
log.Errorf("Failed to bind json: %s", err) log.Errorf("Failed to bind json: %s", err)
gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return return
} }
if err := input.Validate(strfmt.Default); err != nil { if err := input.Validate(strfmt.Default); err != nil {
log.Errorf("Failed to validate usage metrics: %s", err) log.Errorf("Failed to validate usage metrics: %s", err)
c.HandleDBErrors(gctx, err) c.HandleDBErrors(gctx, err)
return return
} }
// TODO: validate payload with the right type, depending on auth context // TODO: validate payload with the right type, depending on auth context
@ -74,9 +72,9 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) {
// TODO: if both or none are set, which error should we return? // TODO: if both or none are set, which error should we return?
var ( var (
payload map[string]any payload map[string]any
baseMetrics models.BaseMetrics baseMetrics models.BaseMetrics
hubItems models.HubItems hubItems models.HubItems
) )
switch len(input.LogProcessors) { switch len(input.LogProcessors) {
@ -89,6 +87,8 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) {
payload = map[string]any{ payload = map[string]any{
"console_options": item0.ConsoleOptions, "console_options": item0.ConsoleOptions,
"datasources": item0.Datasources, "datasources": item0.Datasources,
"metrics": item0.Metrics,
"meta": item0.Meta,
} }
baseMetrics = item0.BaseMetrics baseMetrics = item0.BaseMetrics
hubItems = item0.HubItems hubItems = item0.HubItems
@ -105,7 +105,9 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) {
case 1: case 1:
item0 := input.RemediationComponents[0][0] item0 := input.RemediationComponents[0][0]
payload = map[string]any{ payload = map[string]any{
"type": item0.Type, "type": item0.Type,
"metrics": item0.Metrics,
"meta": item0.Meta,
// TODO: RC stuff like traffic stats // TODO: RC stuff like traffic stats
} }
baseMetrics = item0.BaseMetrics baseMetrics = item0.BaseMetrics