grafana/pkg/services/live/pipeline/converter_json_auto.go
Alexander Emelin 4cd2575ad0
Live: generate ts definitions for pipeline configs (#41544)
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2021-11-15 22:45:35 +03:00

42 lines
1.0 KiB
Go

package pipeline
import (
"context"
"time"
)
type AutoJsonConverter struct {
config AutoJsonConverterConfig
nowTimeFunc func() time.Time
}
func NewAutoJsonConverter(c AutoJsonConverterConfig) *AutoJsonConverter {
return &AutoJsonConverter{config: c}
}
const ConverterTypeJsonAuto = "jsonAuto"
func (c *AutoJsonConverter) Type() string {
return ConverterTypeJsonAuto
}
// Automatic conversion works this way:
// * Time added automatically
// * Nulls dropped
// To preserve nulls we need FieldTips from a user.
// Custom time can be injected on FrameProcessor stage theoretically.
// Custom labels can be injected on FrameProcessor stage theoretically.
func (c *AutoJsonConverter) Convert(_ context.Context, vars Vars, body []byte) ([]*ChannelFrame, error) {
nowTimeFunc := c.nowTimeFunc
if nowTimeFunc == nil {
nowTimeFunc = time.Now
}
frame, err := jsonDocToFrame(vars.Path, body, c.config.FieldTips, nowTimeFunc)
if err != nil {
return nil, err
}
return []*ChannelFrame{
{Channel: "", Frame: frame},
}, nil
}