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 (
"encoding/json"
"fmt"
"net/http"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/go-openapi/strfmt"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/go-openapi/strfmt"
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/metric"
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
"github.com/crowdsecurity/crowdsec/pkg/models"
)
// 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 {
switch {
@ -30,24 +29,23 @@ func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, b
return nil
}
// UsageMetrics receives metrics from log processors and remediation components
func (c *Controller) UsageMetrics(gctx *gin.Context) {
var input models.AllMetrics
var input models.AllMetrics
// parse the payload
if err := gctx.ShouldBindJSON(&input); err != nil {
log.Errorf("Failed to bind json: %s", err)
gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
if err := gctx.ShouldBindJSON(&input); err != nil {
log.Errorf("Failed to bind json: %s", err)
gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
if err := input.Validate(strfmt.Default); err != nil {
log.Errorf("Failed to validate usage metrics: %s", err)
c.HandleDBErrors(gctx, err)
return
}
if err := input.Validate(strfmt.Default); err != nil {
log.Errorf("Failed to validate usage metrics: %s", err)
c.HandleDBErrors(gctx, err)
return
}
// 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?
var (
payload map[string]any
payload map[string]any
baseMetrics models.BaseMetrics
hubItems models.HubItems
hubItems models.HubItems
)
switch len(input.LogProcessors) {
@ -89,6 +87,8 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) {
payload = map[string]any{
"console_options": item0.ConsoleOptions,
"datasources": item0.Datasources,
"metrics": item0.Metrics,
"meta": item0.Meta,
}
baseMetrics = item0.BaseMetrics
hubItems = item0.HubItems
@ -105,7 +105,9 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) {
case 1:
item0 := input.RemediationComponents[0][0]
payload = map[string]any{
"type": item0.Type,
"type": item0.Type,
"metrics": item0.Metrics,
"meta": item0.Meta,
// TODO: RC stuff like traffic stats
}
baseMetrics = item0.BaseMetrics