set pushed_at after sending metrics

This commit is contained in:
Sebastien Blot 2024-04-19 18:33:46 +02:00
parent 0be7cc3cfc
commit be64f619f2
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A
2 changed files with 31 additions and 19 deletions

View file

@ -19,36 +19,25 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models"
)
func (a *apic) GetUsageMetrics() (*models.AllMetrics, error) {
func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) {
lpsMetrics, err := a.dbClient.GetLPsUsageMetrics()
metricsIds := make([]int, 0)
if err != nil {
return nil, err
return nil, nil, err
}
//spew.Dump(lpsMetrics)
bouncersMetrics, err := a.dbClient.GetBouncersUsageMetrics()
if err != nil {
return nil, err
return nil, nil, err
}
//spew.Dump(bouncersMetrics)
allMetrics := &models.AllMetrics{}
/*allLps, err := a.dbClient.ListMachines()
if err != nil {
return nil, err
}
allBouncers, err := a.dbClient.ListBouncers()
if err != nil {
return nil, err
}*/
lpsCache := make(map[string]*ent.Machine)
bouncersCache := make(map[string]*ent.Bouncer)
@ -86,9 +75,9 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, error) {
metrics.FeatureFlags = strings.Split(lp.Featureflags, ",")
metrics.Version = &lp.Version
//TODO: meta
allMetrics.LogProcessors = append(allMetrics.LogProcessors, models.LogProcessorsMetrics{&metrics})
metricsIds = append(metricsIds, lpsMetric.ID)
}
for _, bouncersMetric := range bouncersMetrics {
@ -119,16 +108,21 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, error) {
}
metrics.Type = bouncer.Type
metrics.FeatureFlags = strings.Split(bouncer.Featureflags, ",")
//TODO: meta
metrics.Version = &bouncer.Version
allMetrics.RemediationComponents = append(allMetrics.RemediationComponents, models.RemediationComponentsMetrics{&metrics})
metricsIds = append(metricsIds, bouncersMetric.ID)
}
//bouncerInfos := make(map[string]string)
//TODO: add LAPI metrics
return allMetrics, nil
return allMetrics, metricsIds, nil
}
func (a *apic) MarkUsageMetricsAsSent(ids []int) error {
return a.dbClient.MarkUsageMetricsAsSent(ids)
}
func (a *apic) GetMetrics() (*models.Metrics, error) {
@ -290,7 +284,7 @@ func (a *apic) SendUsageMetrics() {
ticker.Stop()
return
case <-ticker.C:
metrics, err := a.GetUsageMetrics()
metrics, metricsId, err := a.GetUsageMetrics()
if err != nil {
log.Errorf("unable to get usage metrics (%s)", err)
}
@ -299,6 +293,11 @@ func (a *apic) SendUsageMetrics() {
log.Errorf("unable to marshal usage metrics (%s)", err)
}
fmt.Printf("Usage metrics: %s\n", string(jsonStr))
//TODO: actually send the data
err = a.MarkUsageMetricsAsSent(metricsId)
if err != nil {
log.Errorf("unable to mark usage metrics as sent (%s)", err)
}
}
}
}

View file

@ -69,3 +69,16 @@ func (c *Client) GetBouncersUsageMetrics() ([]*ent.Metric, error) {
return metrics, nil
}
func (c *Client) MarkUsageMetricsAsSent(ids []int) error {
_, err := c.Ent.Metric.Update().
Where(metric.IDIn(ids...)).
SetPushedAt(time.Now()).
Save(c.CTX)
if err != nil {
c.Log.Warningf("MarkUsageMetricsAsSent: %s", err)
return fmt.Errorf("marking usage metrics as sent: %w", err)
}
return nil
}