-
Notifications
You must be signed in to change notification settings - Fork 0
/
flush.go
44 lines (36 loc) · 1.1 KB
/
flush.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package insights
import (
"context"
"github.com/ChipArtem/k6/cloudapi/insights"
)
// Client is an interface for sending request metadatas to the Insights API.
type Client interface {
IngestRequestMetadatasBatch(context.Context, insights.RequestMetadatas) error
Close() error
}
// RequestMetadatasFlusher is an interface for flushing data to the cloud.
type RequestMetadatasFlusher interface {
Flush() error
}
// Flusher is an implementation of RequestMetadatasFlusher.
// Its purpose is to retrieve data from a collector
// and send it to the insights backend.
type Flusher struct {
client Client
collector RequestMetadatasCollector
}
// NewFlusher creates a new Flusher.
func NewFlusher(client Client, collector RequestMetadatasCollector) *Flusher {
return &Flusher{
client: client,
collector: collector,
}
}
// Flush retrieves data from the collector and sends it to the insights backend.
func (f *Flusher) Flush() error {
requestMetadatas := f.collector.PopAll()
if len(requestMetadatas) < 1 {
return nil
}
return f.client.IngestRequestMetadatasBatch(context.Background(), requestMetadatas)
}